Windows in Blender are decoupled from the "type" of a window. Each part within a Blender Screen (called 'Area' to distinguish it from 'Window') can be linked to specific SpaceType structs, like SpaceView3D, or SpaceSound, and so on. When such a link is established, the Area will behave as this specific type of window.

 

Since any Area stores a full list of "Space" structs it was ever used for, the data in such in such a struct should contain whatever is needed to fully restore a view. This is also the data as is saved in files.

Temporal data can be stored in such structs too, but make that dynamic allocated, and set on file read these pointers to NULL.

 

In the text below, we name the space "New", and go step by step over the code work to do. File paths are relative to bf-blender/blender/source/

 

You can also review the commit I did to add the Timeline window.

 

makesdna/DNA_space_types.h

 

- Add new space struct here. Check on the obligatory header of such struct! Like:

 

<ccode>

typedef struct SpaceNew {

SpaceLink *next, *prev;

int spacetype;

float blockscale;

struct ScrArea *area;

 

/* your stuff */

/* more stuff */

} SpaceNew;

</ccode>

- if you want the space to have builtin 2d features, such as scrollbars, zooming, etc, add a struct View2D v2d;

 

- in the bottom you can add the local defines if applicable.

 

 

makesdna/DNA_screen_types.h

 

- add a SPACE_NEW define in bottom, in the end!

 

blenloader/intern/writefile.c

 

- Save the new struct in function write_screens().

How to do is easy to copy from other spaces... reading files goes OK then.

 

src/spacetypes.c

 

- This is a wrapper to store the callbacks for the spaces. Add the space type in the spacetype_from_code() function by copying from the examples.

 

src/space.c

 

- add in the bottom the associated spacenew_get_type(), names for new callbacks are obvious too.

 

- Add this as prototype to the include/BIF_spacetypes.h

 

- now add the appropriate code in:

void newspace(ScrArea *sa, int type)

 

- add the new function called:

static void init_newspace(ScrArea *sa)

Do that above the section commented as *** SPACE: GENERAL ***

Use examples from the other space types!

 

- add a general redraw event in allqueue(unsigned short event, short val)

the #define REDRAWNEW will go in include/mydevice.h

 

 

- now decide where you want the callbacks... nicest is to create two new C files for it, but make sure they're prototyped. You can add these in the section of the previous step.

 

- We assume you make a drawnew.c and an editnew.c where the drawing and queue callbacks go to. The 'changed' callback is optional, this is only called when a window is resized. Typically that isn't needed, just add all view/window matrices correctly for each redraw.

 

- when you create the new C files, also make sure you have an include for it, this can be called include/BSE_new.c. Here you only put the exported functions for the space.

 

src/drawnew.c

 

- Add: void drawnewspace(ScrArea *sa, void *spacedata)

A good minimal drawing is code like this:

 

<ccode>

BIF_GetThemeColor3fv(TH_BACK, col);

glClearColor(col[0], col[1], col[2], 0.0);

glClear(GL_COLOR_BUFFER_BIT);

 

/* ortho at pixel level curarea */

myortho2(-0.375, curarea->winx-0.375, -0.375, curarea->winy-0.375);

 

draw_area_emboss(sa);

curarea->win_swap= WIN_BACK_OK;

</ccode>

 

- Make sure you get the appropriate header files;

 

<ccode>

#include "BIF_gl.h"

#include "BIF_mywindow.h"

#include "BIF_screen.h"

#include "BIF_resources.h"

</ccode>

- Of course, copying from similar C files usually works OK.

 

src/editnew.c

 

- Add a function like:

void winqreadnewspace(ScrArea *sa, void *spacedata, BWinEvent *evt)

 

- To enable buttons, at least put in code like;

 

<ccode>

if(evt->val) {

if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) evt->event= 0;

}

</ccode>

 

- Further it's up to you which events to handle!

 

- End with

scrarea_queue_winredraw(sa);

if you want to have the window queued for a redraw. Don't do drawing in this function

 

- Appropriate header files are:

 

<ccode>

#include "BIF_space.h"

#include "BIF_screen.h"

#include "BIF_interface.h"

#include "BIF_mywindow.h"

</ccode>

 

src/editscreen.c

 

- Add the corresponding case in areawinset(short win)

As you see, it's only needed when you set a global, but it is required when you add a View2D in the space.

 

- Add the corresponding case in scrarea_do_headdraw(ScrArea *area)

This will lead to another new C file with button handling for the header.

 

----- src/header_new.c

 

- Add a function like:

void new_buttons(ScrArea *sa)

To draw all buttons and menus for the header. The first button typically is the popup for choosing space type. Samples can be copied from any header_xxx.c function.

 

- End the function with setting sa->headbutlen to enable scrolling when the buttons don't fit.

 

- Add a prototype for the new_buttons() function in include/BSE_headerbuttons.h

 

- For button handling, we need a function like:

void do_new_buttons(struct ScrArea *sa, unsigned short event)

 

- Add a prototype for the do_new_buttons() function in include/BSE_headerbuttons.h

 

- Button event defines are in include/blendef.h

This is still a bit hardcoded stuff... with each spacetype having a range... currently goes up to 1000 only. Needs to be fixed.

 

src/headerbuttons.c

 

- Add the do_new_buttons() call in the bottom.

 

- Add a new option in the main header popup menu, to choose this space type:

char *windowtype_pup(void)

The code demands you type in the hardcoded value of SPACE_NEW define.

 

- Now you also need an icon... editing the icon image and adding one is another tutorial!

It will now just choose a blank icon.

 

Compile!

 

Now you should get it compiling fine. Some notes;

 

- Add for For Scons compiles the new C files in src/SConstruct

- Also for MSVC you need to update the project file.

- Most common errors are for missing includes... almost all Blender data is defined in the makesdna/ directory. For function prototypes check the include/ or blenkernel/ directories. General Blender help functions you can find in blenlib/

 

get View2D to work

 

- A lot of common View2d function are (still!) in drawipo.c

Do a search for SPACE_VIEW3D and check what you need to add yourself.

 

- Yes, it's messy on this level... the View2d functionality can use a cleanup!

 

Themes

 

Until now the BIF_Theme colors used were from the default View3D template. Add your own theme settings this way:

 

- makesnda/DNA_userdef_types.h

Add in end of struct bTheme a new bTheme tnew

 

- src/resources.c

Add the pointer to this bTheme in function BIF_ThemeGetColorPtr()

Initialize the builtin theme in BIF_InitTheme()

 

 

- src/usiblender.c

Now to get former saved .blend files working, initialize the themes in the function:

static void init_userdef_file(void)

 

 

- src/space.c

Add a themecolor choice in the menu; info_user_themebuts()

In same function make sure it gets handled (curmain)

 

- src/resources.c

Now you might add choices here; char *BIF_ThemeColorsPup(int spacetype)