fix for more errors with switch missing break
[blender-staging.git] / source / blender / editors / space_buttons / space_buttons.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_buttons/space_buttons.c
28  *  \ingroup spbuttons
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_utildefines.h"
40
41 #include "BKE_context.h"
42 #include "BKE_screen.h"
43
44 #include "ED_space_api.h"
45 #include "ED_screen.h"
46
47 #include "BIF_gl.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "UI_resources.h"
53 #include "UI_view2d.h"
54
55 #include "ED_render.h"
56
57 #include "buttons_intern.h"  /* own include */
58
59 /* ******************** default callbacks for buttons space ***************** */
60
61 static SpaceLink *buttons_new(const bContext *UNUSED(C))
62 {
63         ARegion *ar;
64         SpaceButs *sbuts;
65         
66         sbuts = MEM_callocN(sizeof(SpaceButs), "initbuts");
67         sbuts->spacetype = SPACE_BUTS;
68         sbuts->align = BUT_VERTICAL;
69
70         /* header */
71         ar = MEM_callocN(sizeof(ARegion), "header for buts");
72         
73         BLI_addtail(&sbuts->regionbase, ar);
74         ar->regiontype = RGN_TYPE_HEADER;
75         ar->alignment = RGN_ALIGN_TOP;
76         
77 #if 0
78         /* context area */
79         ar = MEM_callocN(sizeof(ARegion), "context area for buts");
80         BLI_addtail(&sbuts->regionbase, ar);
81         ar->regiontype = RGN_TYPE_CHANNELS;
82         ar->alignment = RGN_ALIGN_TOP;
83 #endif
84
85         /* main area */
86         ar = MEM_callocN(sizeof(ARegion), "main area for buts");
87         
88         BLI_addtail(&sbuts->regionbase, ar);
89         ar->regiontype = RGN_TYPE_WINDOW;
90
91         return (SpaceLink *)sbuts;
92 }
93
94 /* not spacelink itself */
95 static void buttons_free(SpaceLink *sl)
96 {       
97         SpaceButs *sbuts = (SpaceButs *) sl;
98
99         if (sbuts->path)
100                 MEM_freeN(sbuts->path);
101         
102         if (sbuts->texuser) {
103                 ButsContextTexture *ct = sbuts->texuser;
104                 BLI_freelistN(&ct->users);
105                 MEM_freeN(ct);
106         }
107 }
108
109 /* spacetype; init callback */
110 static void buttons_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
111 {
112         SpaceButs *sbuts = sa->spacedata.first;
113
114         /* auto-align based on size */
115         if (sbuts->align == BUT_AUTO || !sbuts->align) {
116                 if (sa->winx > sa->winy)
117                         sbuts->align = BUT_HORIZONTAL;
118                 else
119                         sbuts->align = BUT_VERTICAL;
120         }
121 }
122
123 static SpaceLink *buttons_duplicate(SpaceLink *sl)
124 {
125         SpaceButs *sbutsn = MEM_dupallocN(sl);
126         
127         /* clear or remove stuff from old */
128         sbutsn->path = NULL;
129         sbutsn->texuser = NULL;
130         
131         return (SpaceLink *)sbutsn;
132 }
133
134 /* add handlers, stuff you only do once or on area/region changes */
135 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
136 {
137         wmKeyMap *keymap;
138
139         ED_region_panels_init(wm, ar);
140
141         keymap = WM_keymap_find(wm->defaultconf, "Property Editor", SPACE_BUTS, 0);
142         WM_event_add_keymap_handler(&ar->handlers, keymap);
143 }
144
145 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
146 {
147         /* draw entirely, view changes should be handled here */
148         SpaceButs *sbuts = CTX_wm_space_buts(C);
149         int vertical = (sbuts->align == BUT_VERTICAL);
150
151         buttons_context_compute(C, sbuts);
152
153         if (sbuts->mainb == BCONTEXT_SCENE)
154                 ED_region_panels(C, ar, vertical, "scene", sbuts->mainb);
155         else if (sbuts->mainb == BCONTEXT_RENDER)
156                 ED_region_panels(C, ar, vertical, "render", sbuts->mainb);
157         else if (sbuts->mainb == BCONTEXT_RENDER_LAYER)
158                 ED_region_panels(C, ar, vertical, "render_layer", 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         ED_region_header_init(ar);
204 }
205
206 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
207 {
208         SpaceButs *sbuts = CTX_wm_space_buts(C);
209
210         /* Needed for RNA to get the good values! */
211         buttons_context_compute(C, sbuts);
212
213         ED_region_header(C, ar);
214 }
215
216 /* draw a certain button set only if properties area is currently
217  * showing that button set, to reduce unnecessary drawing. */
218 static void buttons_area_redraw(ScrArea *sa, short buttons)
219 {
220         SpaceButs *sbuts = sa->spacedata.first;
221         
222         /* if the area's current button set is equal to the one to redraw */
223         if (sbuts->mainb == buttons)
224                 ED_area_tag_redraw(sa);
225 }
226
227 /* reused! */
228 static void buttons_area_listener(bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn)
229 {
230         SpaceButs *sbuts = sa->spacedata.first;
231
232         /* context changes */
233         switch (wmn->category) {
234                 case NC_SCENE:
235                         switch (wmn->data) {
236                                 case ND_RENDER_OPTIONS:
237                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
238                                         buttons_area_redraw(sa, BCONTEXT_RENDER_LAYER);
239                                         break;
240                                 case ND_FRAME:
241                                         /* any buttons area can have animated properties so redraw all */
242                                         ED_area_tag_redraw(sa);
243                                         sbuts->preview = 1;
244                                         break;
245                                 case ND_OB_ACTIVE:
246                                         ED_area_tag_redraw(sa);
247                                         sbuts->preview = 1;
248                                         break;
249                                 case ND_KEYINGSET:
250                                         buttons_area_redraw(sa, BCONTEXT_SCENE);
251                                         break;
252                                 case ND_RENDER_RESULT:
253                                         break;
254                                 case ND_MODE:
255                                 case ND_LAYER:
256                                 default:
257                                         ED_area_tag_redraw(sa);
258                                         break;
259                         }
260                         break;
261                 case NC_OBJECT:
262                         switch (wmn->data) {
263                                 case ND_TRANSFORM:
264                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
265                                         buttons_area_redraw(sa, BCONTEXT_DATA); /* autotexpace flag */
266                                         break;
267                                 case ND_POSE:
268                                         buttons_area_redraw(sa, BCONTEXT_DATA);
269                                 case ND_BONE_ACTIVE:
270                                 case ND_BONE_SELECT:
271                                         buttons_area_redraw(sa, BCONTEXT_BONE);
272                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
273                                         buttons_area_redraw(sa, BCONTEXT_DATA);
274                                         break;
275                                 case ND_MODIFIER:
276                                         if (wmn->action == NA_RENAME)
277                                                 ED_area_tag_redraw(sa);
278                                         else
279                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
280                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
281                                         break;
282                                 case ND_CONSTRAINT:
283                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
284                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
285                                         break;
286                                 case ND_PARTICLE:
287                                         if (wmn->action == NA_EDITED)
288                                                 buttons_area_redraw(sa, BCONTEXT_PARTICLE);
289                                         sbuts->preview = 1;
290                                         break;
291                                 case ND_DRAW:
292                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
293                                         buttons_area_redraw(sa, BCONTEXT_DATA);
294                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
295                                         break;
296                                 case ND_SHADING:
297                                 case ND_SHADING_DRAW:
298                                 case ND_SHADING_LINKS:
299                                 case ND_SHADING_PREVIEW:
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_SHADING_LINKS:
323                                 case ND_SHADING_PREVIEW:
324                                 case ND_NODES:
325                                         /* currently works by redraws... if preview is set, it (re)starts job */
326                                         sbuts->preview = 1;
327                                         break;
328                         }
329                         break;
330                 case NC_WORLD:
331                         buttons_area_redraw(sa, BCONTEXT_WORLD);
332                         sbuts->preview = 1;
333                         break;
334                 case NC_LAMP:
335                         buttons_area_redraw(sa, BCONTEXT_DATA);
336                         sbuts->preview = 1;
337                         break;
338                 case NC_GROUP:
339                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
340                         break;
341                 case NC_BRUSH:
342                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
343                         sbuts->preview = 1;
344                         break;
345                 case NC_TEXTURE:
346                 case NC_IMAGE:
347                         if (wmn->action != NA_PAINTING) {
348                                 ED_area_tag_redraw(sa);
349                                 sbuts->preview = 1;
350                         }
351                         break;
352                 case NC_SPACE:
353                         if (wmn->data == ND_SPACE_PROPERTIES)
354                                 ED_area_tag_redraw(sa);
355                         break;
356                 case NC_ID:
357                         if (wmn->action == NA_RENAME)
358                                 ED_area_tag_redraw(sa);
359                         break;
360                 case NC_ANIMATION:
361                         switch (wmn->data) {
362                                 case ND_KEYFRAME:
363                                         if (wmn->action == NA_EDITED)
364                                                 ED_area_tag_redraw(sa);
365                                         break;
366                         }
367                         break;
368                 case NC_NODE:
369                         if (wmn->action == NA_SELECTED) {
370                                 ED_area_tag_redraw(sa);
371                                 /* new active node, update texture preview */
372                                 if (sbuts->mainb == BCONTEXT_TEXTURE)
373                                         sbuts->preview = 1;
374                         }
375                         break;
376                 /* Listener for preview render, when doing an global undo. */
377                 case NC_WINDOW:
378                         ED_area_tag_redraw(sa);
379                         sbuts->preview = 1;
380                         break;
381 #ifdef WITH_FREESTYLE
382                 case NC_LINESTYLE:
383                         ED_area_tag_redraw(sa);
384                         sbuts->preview = 1;
385                         break;
386 #endif
387         }
388
389         if (wmn->data == ND_KEYS)
390                 ED_area_tag_redraw(sa);
391 }
392
393 /* only called once, from space/spacetypes.c */
394 void ED_spacetype_buttons(void)
395 {
396         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype buttons");
397         ARegionType *art;
398         
399         st->spaceid = SPACE_BUTS;
400         strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
401         
402         st->new = buttons_new;
403         st->free = buttons_free;
404         st->init = buttons_init;
405         st->duplicate = buttons_duplicate;
406         st->operatortypes = buttons_operatortypes;
407         st->keymap = buttons_keymap;
408         st->listener = buttons_area_listener;
409         st->context = buttons_context;
410         
411         /* regions: main window */
412         art = MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
413         art->regionid = RGN_TYPE_WINDOW;
414         art->init = buttons_main_area_init;
415         art->draw = buttons_main_area_draw;
416         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
417         BLI_addhead(&st->regiontypes, art);
418
419         buttons_context_register(art);
420         
421         /* regions: header */
422         art = MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
423         art->regionid = RGN_TYPE_HEADER;
424         art->prefsizey = HEADERY;
425         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
426         
427         art->init = buttons_header_area_init;
428         art->draw = buttons_header_area_draw;
429         BLI_addhead(&st->regiontypes, art);
430
431         BKE_spacetype_register(st);
432 }
433