Missed notifier was found when looking at #28393
[blender.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 /** \file blender/editors/space_buttons/space_buttons.c
30  *  \ingroup spbuttons
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41 #include "BLI_rand.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_context.h"
45 #include "BKE_screen.h"
46
47 #include "ED_space_api.h"
48 #include "ED_screen.h"
49
50 #include "BIF_gl.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "UI_resources.h"
56 #include "UI_view2d.h"
57
58 #include "ED_render.h"
59
60 #include "buttons_intern.h"     // own include
61
62 /* ******************** default callbacks for buttons space ***************** */
63
64 static SpaceLink *buttons_new(const bContext *UNUSED(C))
65 {
66         ARegion *ar;
67         SpaceButs *sbuts;
68         
69         sbuts= MEM_callocN(sizeof(SpaceButs), "initbuts");
70         sbuts->spacetype= SPACE_BUTS;
71         sbuts->align= BUT_VERTICAL;
72
73         /* header */
74         ar= MEM_callocN(sizeof(ARegion), "header for buts");
75         
76         BLI_addtail(&sbuts->regionbase, ar);
77         ar->regiontype= RGN_TYPE_HEADER;
78         ar->alignment= RGN_ALIGN_TOP;
79         
80 #if 0
81         /* context area */
82         ar= MEM_callocN(sizeof(ARegion), "context area for buts");
83         BLI_addtail(&sbuts->regionbase, ar);
84         ar->regiontype= RGN_TYPE_CHANNELS;
85         ar->alignment= RGN_ALIGN_TOP;
86 #endif
87
88         /* main area */
89         ar= MEM_callocN(sizeof(ARegion), "main area for buts");
90         
91         BLI_addtail(&sbuts->regionbase, ar);
92         ar->regiontype= RGN_TYPE_WINDOW;
93         
94         return (SpaceLink *)sbuts;
95 }
96
97 /* not spacelink itself */
98 static void buttons_free(SpaceLink *sl)
99 {       
100         SpaceButs *sbuts= (SpaceButs*) sl;
101         
102         if(sbuts->ri) { 
103                 if (sbuts->ri->rect) MEM_freeN(sbuts->ri->rect);
104                 MEM_freeN(sbuts->ri);
105         }
106
107         if(sbuts->path)
108                 MEM_freeN(sbuts->path);
109 }
110
111 /* spacetype; init callback */
112 static void buttons_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
113 {
114         SpaceButs *sbuts= sa->spacedata.first;
115
116         /* auto-align based on size */
117         if(sbuts->align == BUT_AUTO || !sbuts->align) {
118                 if(sa->winx > sa->winy)
119                         sbuts->align= BUT_HORIZONTAL;
120                 else
121                         sbuts->align= BUT_VERTICAL;
122         }
123 }
124
125 static SpaceLink *buttons_duplicate(SpaceLink *sl)
126 {
127         SpaceButs *sbutsn= MEM_dupallocN(sl);
128         
129         /* clear or remove stuff from old */
130         sbutsn->ri= NULL;
131         sbutsn->path= NULL;
132         
133         return (SpaceLink *)sbutsn;
134 }
135
136 /* add handlers, stuff you only do once or on area/region changes */
137 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
138 {
139         wmKeyMap *keymap;
140
141         ED_region_panels_init(wm, ar);
142
143         keymap= WM_keymap_find(wm->defaultconf, "Property Editor", SPACE_BUTS, 0);
144         WM_event_add_keymap_handler(&ar->handlers, keymap);
145 }
146
147 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
148 {
149         /* draw entirely, view changes should be handled here */
150         SpaceButs *sbuts= CTX_wm_space_buts(C);
151         int vertical= (sbuts->align == BUT_VERTICAL);
152
153         buttons_context_compute(C, sbuts);
154
155         if(sbuts->mainb == BCONTEXT_SCENE)
156                 ED_region_panels(C, ar, vertical, "scene", sbuts->mainb);
157         else if(sbuts->mainb == BCONTEXT_RENDER)
158                 ED_region_panels(C, ar, vertical, "render", sbuts->mainb);
159         else if(sbuts->mainb == BCONTEXT_WORLD)
160                 ED_region_panels(C, ar, vertical, "world", sbuts->mainb);
161         else if(sbuts->mainb == BCONTEXT_OBJECT)
162                 ED_region_panels(C, ar, vertical, "object", sbuts->mainb);
163         else if(sbuts->mainb == BCONTEXT_DATA)
164                 ED_region_panels(C, ar, vertical, "data", sbuts->mainb);
165         else if(sbuts->mainb == BCONTEXT_MATERIAL)
166                 ED_region_panels(C, ar, vertical, "material", sbuts->mainb);
167         else if(sbuts->mainb == BCONTEXT_TEXTURE)
168                 ED_region_panels(C, ar, vertical, "texture", sbuts->mainb);
169         else if(sbuts->mainb == BCONTEXT_PARTICLE)
170                 ED_region_panels(C, ar, vertical, "particle", sbuts->mainb);
171         else if(sbuts->mainb == BCONTEXT_PHYSICS)
172                 ED_region_panels(C, ar, vertical, "physics", sbuts->mainb);
173         else if(sbuts->mainb == BCONTEXT_BONE)
174                 ED_region_panels(C, ar, vertical, "bone", sbuts->mainb);
175         else if(sbuts->mainb == BCONTEXT_MODIFIER)
176                 ED_region_panels(C, ar, vertical, "modifier", sbuts->mainb);
177         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
178                 ED_region_panels(C, ar, vertical, "constraint", sbuts->mainb);
179         else if(sbuts->mainb == BCONTEXT_BONE_CONSTRAINT)
180                 ED_region_panels(C, ar, vertical, "bone_constraint", sbuts->mainb);
181
182         sbuts->re_align= 0;
183         sbuts->mainbo= sbuts->mainb;
184 }
185
186 static void buttons_operatortypes(void)
187 {
188         WM_operatortype_append(BUTTONS_OT_toolbox);
189         WM_operatortype_append(BUTTONS_OT_file_browse);
190         WM_operatortype_append(BUTTONS_OT_directory_browse);
191 }
192
193 static void buttons_keymap(struct wmKeyConfig *keyconf)
194 {
195         wmKeyMap *keymap= WM_keymap_find(keyconf, "Property Editor", SPACE_BUTS, 0);
196         
197         WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
198 }
199
200 /* add handlers, stuff you only do once or on area/region changes */
201 static void buttons_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
202 {
203         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
204 }
205
206 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
207 {
208         /* clear */
209         UI_ThemeClearColor(ED_screen_area_active(C)?TH_HEADER:TH_HEADERDESEL);
210         glClear(GL_COLOR_BUFFER_BIT);
211         
212         /* set view2d view matrix for scrolling (without scrollers) */
213         UI_view2d_view_ortho(&ar->v2d);
214         
215         buttons_header_buttons(C, ar);
216
217         /* restore view matrix? */
218         UI_view2d_view_restore(C);
219 }
220
221 /* draw a certain button set only if properties area is currently
222  * showing that button set, to reduce unnecessary drawing. */
223 static void buttons_area_redraw(ScrArea *sa, short buttons)
224 {
225         SpaceButs *sbuts= sa->spacedata.first;
226         
227         /* if the area's current button set is equal to the one to redraw */
228         if(sbuts->mainb == buttons)
229                 ED_area_tag_redraw(sa);
230 }
231
232 /* reused! */
233 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
234 {
235         SpaceButs *sbuts= sa->spacedata.first;
236
237         /* context changes */
238         switch(wmn->category) {
239                 case NC_SCENE:
240                         switch(wmn->data) {
241                                 case ND_RENDER_OPTIONS:
242                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
243                                         break;
244                                 case ND_FRAME:
245                                         /* any buttons area can have animated properties so redraw all */
246                                         ED_area_tag_redraw(sa);
247                                         sbuts->preview= 1;
248                                         break;
249                                 case ND_OB_ACTIVE:
250                                         ED_area_tag_redraw(sa);
251                                         sbuts->preview= 1;
252                                         break;
253                                 case ND_KEYINGSET:
254                                         buttons_area_redraw(sa, BCONTEXT_SCENE);
255                                         break;
256                                 case ND_RENDER_RESULT:
257                                         break;
258                                 case ND_MODE:
259                                 case ND_LAYER:
260                                 default:
261                                         ED_area_tag_redraw(sa);
262                                         break;
263                         }
264                         break;
265                 case NC_OBJECT:
266                         switch(wmn->data) {
267                                 case ND_TRANSFORM:
268                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
269                                         buttons_area_redraw(sa, BCONTEXT_DATA); /* autotexpace flag */
270                                         break;
271                                 case ND_POSE:
272                                         buttons_area_redraw(sa, BCONTEXT_DATA);
273                                 case ND_BONE_ACTIVE:
274                                 case ND_BONE_SELECT:
275                                         buttons_area_redraw(sa, BCONTEXT_BONE);
276                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
277                                         break;
278                                 case ND_MODIFIER:
279                                         if(wmn->action == NA_RENAME)
280                                                 ED_area_tag_redraw(sa);
281                                         else
282                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
283                                                 buttons_area_redraw(sa, BCONTEXT_PHYSICS);
284                                         break;
285                                 case ND_CONSTRAINT:
286                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
287                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
288                                         break;
289                                 case ND_PARTICLE:
290                                         if (wmn->action == NA_EDITED)
291                                                 buttons_area_redraw(sa, BCONTEXT_PARTICLE);
292                                         sbuts->preview= 1;
293                                         break;
294                                 case ND_DRAW:
295                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
296                                         buttons_area_redraw(sa, BCONTEXT_DATA);
297                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
298                                 case ND_SHADING:
299                                 case ND_SHADING_DRAW:
300                                         /* currently works by redraws... if preview is set, it (re)starts job */
301                                         sbuts->preview= 1;
302                                         break;
303                                 default:
304                                         /* Not all object RNA props have a ND_ notifier (yet) */
305                                         ED_area_tag_redraw(sa);
306                                         break;
307                         }
308                         break;
309                 case NC_GEOM:
310                         switch(wmn->data) {
311                                 case ND_SELECT:
312                                 case ND_DATA:
313                                         ED_area_tag_redraw(sa);
314                                         break;
315                         }
316                         break;
317                 case NC_MATERIAL:
318                         ED_area_tag_redraw(sa);
319                         switch(wmn->data) {
320                                 case ND_SHADING:
321                                 case ND_SHADING_DRAW:
322                                 case ND_NODES:
323                                         /* currently works by redraws... if preview is set, it (re)starts job */
324                                         sbuts->preview= 1;
325                                         break;
326                         }                                       
327                         break;
328                 case NC_WORLD:
329                         buttons_area_redraw(sa, BCONTEXT_WORLD);
330                         sbuts->preview= 1;
331                         break;
332                 case NC_LAMP:
333                         buttons_area_redraw(sa, BCONTEXT_DATA);
334                         sbuts->preview= 1;
335                         break;
336                 case NC_BRUSH:
337                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
338                         break;
339                 case NC_TEXTURE:
340                 case NC_IMAGE:
341                         ED_area_tag_redraw(sa);
342                         sbuts->preview= 1;
343                         break;
344                 case NC_SPACE:
345                         if(wmn->data == ND_SPACE_PROPERTIES)
346                                 ED_area_tag_redraw(sa);
347                         break;
348                 case NC_ID:
349                         if(wmn->action == NA_RENAME)
350                                 ED_area_tag_redraw(sa);
351                         break;
352                 case NC_ANIMATION:
353                         switch(wmn->data) {
354                                 case ND_KEYFRAME:
355                                         if (wmn->action == NA_EDITED)
356                                                 ED_area_tag_redraw(sa);
357                                         break;
358                         }
359                         break;
360                 case NC_NODE:
361                         if(wmn->action==NA_SELECTED) {
362                                 ED_area_tag_redraw(sa);
363                                 /* new active node, update texture preview */
364                                 if(sbuts->mainb == BCONTEXT_TEXTURE)
365                                         sbuts->preview= 1;
366                         }
367                         break;
368                 /* Listener for preview render, when doing an global undo. */
369                 case NC_WINDOW:
370                         ED_area_tag_redraw(sa);
371                         sbuts->preview= 1;
372                         break;
373         }
374
375         if(wmn->data == ND_KEYS)
376                 ED_area_tag_redraw(sa);
377 }
378
379 /* only called once, from space/spacetypes.c */
380 void ED_spacetype_buttons(void)
381 {
382         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
383         ARegionType *art;
384         
385         st->spaceid= SPACE_BUTS;
386         strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
387         
388         st->new= buttons_new;
389         st->free= buttons_free;
390         st->init= buttons_init;
391         st->duplicate= buttons_duplicate;
392         st->operatortypes= buttons_operatortypes;
393         st->keymap= buttons_keymap;
394         st->listener= buttons_area_listener;
395         st->context= buttons_context;
396         
397         /* regions: main window */
398         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
399         art->regionid = RGN_TYPE_WINDOW;
400         art->init= buttons_main_area_init;
401         art->draw= buttons_main_area_draw;
402         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
403         BLI_addhead(&st->regiontypes, art);
404
405         buttons_context_register(art);
406         
407         /* regions: header */
408         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
409         art->regionid = RGN_TYPE_HEADER;
410         art->prefsizey= HEADERY;
411         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
412         
413         art->init= buttons_header_area_init;
414         art->draw= buttons_header_area_draw;
415         BLI_addhead(&st->regiontypes, art);
416
417         BKE_spacetype_register(st);
418 }
419