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