Restored Fluid Sim baking
[blender-staging.git] / source / blender / editors / space_buttons / space_buttons.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36 #include "BLI_math.h"
37 #include "BLI_rand.h"
38
39 #include "BKE_colortools.h"
40 #include "BKE_context.h"
41 #include "BKE_screen.h"
42
43 #include "ED_screen.h"
44
45 #include "BIF_gl.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "UI_resources.h"
51 #include "UI_view2d.h"
52
53 #include "ED_render.h"
54
55 #include "buttons_intern.h"     // own include
56
57 /* ******************** default callbacks for buttons space ***************** */
58
59 static SpaceLink *buttons_new(const bContext *C)
60 {
61         ARegion *ar;
62         SpaceButs *sbuts;
63         
64         sbuts= MEM_callocN(sizeof(SpaceButs), "initbuts");
65         sbuts->spacetype= SPACE_BUTS;
66         sbuts->align= BUT_AUTO;
67
68         /* header */
69         ar= MEM_callocN(sizeof(ARegion), "header for buts");
70         
71         BLI_addtail(&sbuts->regionbase, ar);
72         ar->regiontype= RGN_TYPE_HEADER;
73         ar->alignment= RGN_ALIGN_BOTTOM;
74         
75 #if 0
76         /* context area */
77         ar= MEM_callocN(sizeof(ARegion), "context area for buts");
78         BLI_addtail(&sbuts->regionbase, ar);
79         ar->regiontype= RGN_TYPE_CHANNELS;
80         ar->alignment= RGN_ALIGN_TOP;
81 #endif
82
83         /* main area */
84         ar= MEM_callocN(sizeof(ARegion), "main area for buts");
85         
86         BLI_addtail(&sbuts->regionbase, ar);
87         ar->regiontype= RGN_TYPE_WINDOW;
88         
89         return (SpaceLink *)sbuts;
90 }
91
92 /* not spacelink itself */
93 static void buttons_free(SpaceLink *sl)
94 {       
95         SpaceButs *sbuts= (SpaceButs*) sl;
96         
97         if(sbuts->ri) { 
98                 if (sbuts->ri->rect) MEM_freeN(sbuts->ri->rect);
99                 MEM_freeN(sbuts->ri);
100         }
101
102         if(sbuts->path)
103                 MEM_freeN(sbuts->path);
104 }
105
106 /* spacetype; init callback */
107 static void buttons_init(struct wmWindowManager *wm, ScrArea *sa)
108 {
109         SpaceButs *sbuts= sa->spacedata.first;
110
111         /* auto-align based on size */
112         if(sbuts->align == BUT_AUTO || !sbuts->align) {
113                 if(sa->winx > sa->winy)
114                         sbuts->align= BUT_HORIZONTAL;
115                 else
116                         sbuts->align= BUT_VERTICAL;
117         }
118 }
119
120 static SpaceLink *buttons_duplicate(SpaceLink *sl)
121 {
122         SpaceButs *sbutsn= MEM_dupallocN(sl);
123         
124         /* clear or remove stuff from old */
125         sbutsn->ri= NULL;
126         sbutsn->path= NULL;
127         
128         return (SpaceLink *)sbutsn;
129 }
130
131 /* add handlers, stuff you only do once or on area/region changes */
132 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
133 {
134         wmKeyMap *keymap;
135
136         ED_region_panels_init(wm, ar);
137
138         keymap= WM_keymap_find(wm->defaultconf, "Property Editor", SPACE_BUTS, 0);
139         WM_event_add_keymap_handler(&ar->handlers, keymap);
140 }
141
142 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
143 {
144         /* draw entirely, view changes should be handled here */
145         SpaceButs *sbuts= CTX_wm_space_buts(C);
146         int vertical= (sbuts->align == BUT_VERTICAL);
147
148         buttons_context_compute(C, sbuts);
149
150         if(sbuts->mainb == BCONTEXT_SCENE)
151                 ED_region_panels(C, ar, vertical, "scene", sbuts->mainb);
152         else if(sbuts->mainb == BCONTEXT_RENDER)
153                 ED_region_panels(C, ar, vertical, "render", sbuts->mainb);
154         else if(sbuts->mainb == BCONTEXT_WORLD)
155                 ED_region_panels(C, ar, vertical, "world", sbuts->mainb);
156         else if(sbuts->mainb == BCONTEXT_OBJECT)
157                 ED_region_panels(C, ar, vertical, "object", sbuts->mainb);
158         else if(sbuts->mainb == BCONTEXT_DATA)
159                 ED_region_panels(C, ar, vertical, "data", sbuts->mainb);
160         else if(sbuts->mainb == BCONTEXT_MATERIAL)
161                 ED_region_panels(C, ar, vertical, "material", sbuts->mainb);
162         else if(sbuts->mainb == BCONTEXT_TEXTURE)
163                 ED_region_panels(C, ar, vertical, "texture", sbuts->mainb);
164         else if(sbuts->mainb == BCONTEXT_PARTICLE)
165                 ED_region_panels(C, ar, vertical, "particle", sbuts->mainb);
166         else if(sbuts->mainb == BCONTEXT_PHYSICS)
167                 ED_region_panels(C, ar, vertical, "physics", sbuts->mainb);
168         else if(sbuts->mainb == BCONTEXT_BONE)
169                 ED_region_panels(C, ar, vertical, "bone", sbuts->mainb);
170         else if(sbuts->mainb == BCONTEXT_MODIFIER)
171                 ED_region_panels(C, ar, vertical, "modifier", sbuts->mainb);
172         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
173                 ED_region_panels(C, ar, vertical, "constraint", sbuts->mainb);
174         else if(sbuts->mainb == BCONTEXT_BONE_CONSTRAINT)
175                 ED_region_panels(C, ar, vertical, "bone_constraint", sbuts->mainb);
176
177         sbuts->re_align= 0;
178         sbuts->mainbo= sbuts->mainb;
179 }
180
181 void buttons_operatortypes(void)
182 {
183         WM_operatortype_append(BUTTONS_OT_toolbox);
184         WM_operatortype_append(BUTTONS_OT_file_browse);
185 }
186
187 void buttons_keymap(struct wmKeyConfig *keyconf)
188 {
189         wmKeyMap *keymap= WM_keymap_find(keyconf, "Property Editor", SPACE_BUTS, 0);
190         
191         WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
192 }
193
194 //#define PY_HEADER
195 /* add handlers, stuff you only do once or on area/region changes */
196 static void buttons_header_area_init(wmWindowManager *wm, ARegion *ar)
197 {
198 #ifdef PY_HEADER
199         ED_region_header_init(ar);
200 #else
201         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
202 #endif
203 }
204
205 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
206 {
207 #ifdef PY_HEADER
208         ED_region_header(C, ar);
209 #else
210
211         float col[3];
212         
213         /* clear */
214         if(ED_screen_area_active(C))
215                 UI_GetThemeColor3fv(TH_HEADER, col);
216         else
217                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
218         
219         glClearColor(col[0], col[1], col[2], 0.0);
220         glClear(GL_COLOR_BUFFER_BIT);
221         
222         /* set view2d view matrix for scrolling (without scrollers) */
223         UI_view2d_view_ortho(C, &ar->v2d);
224         
225         buttons_header_buttons(C, ar);
226 #endif  
227
228         /* restore view matrix? */
229         UI_view2d_view_restore(C);
230 }
231
232 /* draw a certain button set only if properties area is currently
233  * showing that button set, to reduce unnecessary drawing. */
234 static void buttons_area_redraw(ScrArea *sa, short buttons)
235 {
236         SpaceButs *sbuts= sa->spacedata.first;
237         
238         /* if the area's current button set is equal to the one to redraw */
239         if(sbuts->mainb == buttons)
240                 ED_area_tag_redraw(sa);
241 }
242
243 /* reused! */
244 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
245 {
246         SpaceButs *sbuts= sa->spacedata.first;
247
248         /* context changes */
249         switch(wmn->category) {
250                 case NC_SCENE:
251                         switch(wmn->data) {
252                                 case ND_RENDER_OPTIONS:
253                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
254                                         break;
255                                 case ND_FRAME:
256                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
257                                         buttons_area_redraw(sa, BCONTEXT_MATERIAL);
258                                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
259                                         buttons_area_redraw(sa, BCONTEXT_WORLD);
260                                         buttons_area_redraw(sa, BCONTEXT_DATA);
261                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
262                                         sbuts->preview= 1;
263                                         break;
264                                 case ND_OB_ACTIVE:
265                                         ED_area_tag_redraw(sa);
266                                         sbuts->preview= 1;
267                                         break;
268                                 case ND_KEYINGSET:
269                                         buttons_area_redraw(sa, BCONTEXT_SCENE);
270                                         break;
271                                 case ND_MODE:
272                                 case ND_LAYER:
273                                 default:
274                                         ED_area_tag_redraw(sa);
275                                         break;
276                         }
277                         break;
278                 case NC_OBJECT:
279                         switch(wmn->data) {
280                                 case ND_TRANSFORM:
281                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
282                                         break;
283                                 case ND_POSE:
284                                 case ND_BONE_ACTIVE:
285                                 case ND_BONE_SELECT:
286                                         buttons_area_redraw(sa, BCONTEXT_BONE);
287                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
288                                         break;
289                                 case ND_MODIFIER:
290                                         if(wmn->action == NA_RENAME)
291                                                 ED_area_tag_redraw(sa);
292                                         else
293                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
294                                         break;
295                                 case ND_CONSTRAINT:
296                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
297                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
298                                         break;
299                                 case ND_PARTICLE_DATA:
300                                         buttons_area_redraw(sa, BCONTEXT_PARTICLE);
301                                         break;
302                                 case ND_DRAW:
303                                 case ND_SHADING:
304                                 case ND_SHADING_DRAW:
305                                         /* currently works by redraws... if preview is set, it (re)starts job */
306                                         sbuts->preview= 1;
307                                         break;
308                                 default:
309                                         /* Not all object RNA props have a ND_ notifier (yet) */
310                                         ED_area_tag_redraw(sa);
311                                         break;
312                         }
313                         break;
314                 case NC_GEOM:
315                         switch(wmn->data) {
316                                 case ND_SELECT:
317                                 case ND_DATA:
318                                         ED_area_tag_redraw(sa);
319                                         break;
320                         }
321                         break;
322                 case NC_MATERIAL:
323                         ED_area_tag_redraw(sa);
324                         switch(wmn->data) {
325                                 case ND_SHADING:
326                                 case ND_SHADING_DRAW:
327                                 case ND_NODES:
328                                         /* currently works by redraws... if preview is set, it (re)starts job */
329                                         sbuts->preview= 1;
330                                         break;
331                         }                                       
332                         break;
333                 case NC_WORLD:
334                         buttons_area_redraw(sa, BCONTEXT_WORLD);
335                         sbuts->preview= 1;
336                         break;
337                 case NC_LAMP:
338                         buttons_area_redraw(sa, BCONTEXT_DATA);
339                         sbuts->preview= 1;
340                         break;
341                 case NC_BRUSH:
342                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
343                         sbuts->preview= 1;
344                         break;
345                 case NC_TEXTURE:
346                         ED_area_tag_redraw(sa);
347                         sbuts->preview= 1;
348                         break;
349                 case NC_SPACE:
350                         if(wmn->data == ND_SPACE_PROPERTIES)
351                                 ED_area_tag_redraw(sa);
352                         break;
353                 case NC_ID:
354                         if(wmn->action == NA_RENAME)
355                                 ED_area_tag_redraw(sa);
356                         break;
357                 case NC_ANIMATION:
358                         switch(wmn->data) {
359                                 case ND_KEYFRAME_EDIT:
360                                         ED_area_tag_redraw(sa);
361                                         break;
362                         }
363         }
364
365         if(wmn->data == ND_KEYS)
366                 ED_area_tag_redraw(sa);
367 }
368
369 /* only called once, from space/spacetypes.c */
370 void ED_spacetype_buttons(void)
371 {
372         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
373         ARegionType *art;
374         
375         st->spaceid= SPACE_BUTS;
376         strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
377         
378         st->new= buttons_new;
379         st->free= buttons_free;
380         st->init= buttons_init;
381         st->duplicate= buttons_duplicate;
382         st->operatortypes= buttons_operatortypes;
383         st->keymap= buttons_keymap;
384         st->listener= buttons_area_listener;
385         st->context= buttons_context;
386         
387         /* regions: main window */
388         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
389         art->regionid = RGN_TYPE_WINDOW;
390         art->init= buttons_main_area_init;
391         art->draw= buttons_main_area_draw;
392         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
393         BLI_addhead(&st->regiontypes, art);
394
395         buttons_context_register(art);
396         
397         /* regions: header */
398         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
399         art->regionid = RGN_TYPE_HEADER;
400         art->prefsizey= BUTS_HEADERY;
401         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
402         
403         art->init= buttons_header_area_init;
404         art->draw= buttons_header_area_draw;
405         BLI_addhead(&st->regiontypes, art);
406
407         BKE_spacetype_register(st);
408 }
409