e2d80e9e7758a8b02693ba095a0298dcf696cdbf
[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                                 case ND_BONE_ACTIVE:
273                                 case ND_BONE_SELECT:
274                                         buttons_area_redraw(sa, BCONTEXT_BONE);
275                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
276                                         break;
277                                 case ND_MODIFIER:
278                                         if(wmn->action == NA_RENAME)
279                                                 ED_area_tag_redraw(sa);
280                                         else
281                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
282                                                 buttons_area_redraw(sa, BCONTEXT_PHYSICS);
283                                         break;
284                                 case ND_CONSTRAINT:
285                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
286                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
287                                         break;
288                                 case ND_PARTICLE:
289                                         if (wmn->action == NA_EDITED)
290                                                 buttons_area_redraw(sa, BCONTEXT_PARTICLE);
291                                         sbuts->preview= 1;
292                                         break;
293                                 case ND_DRAW:
294                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
295                                         buttons_area_redraw(sa, BCONTEXT_DATA);
296                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
297                                 case ND_SHADING:
298                                 case ND_SHADING_DRAW:
299                                         /* currently works by redraws... if preview is set, it (re)starts job */
300                                         sbuts->preview= 1;
301                                         break;
302                                 default:
303                                         /* Not all object RNA props have a ND_ notifier (yet) */
304                                         ED_area_tag_redraw(sa);
305                                         break;
306                         }
307                         break;
308                 case NC_GEOM:
309                         switch(wmn->data) {
310                                 case ND_SELECT:
311                                 case ND_DATA:
312                                         ED_area_tag_redraw(sa);
313                                         break;
314                         }
315                         break;
316                 case NC_MATERIAL:
317                         ED_area_tag_redraw(sa);
318                         switch(wmn->data) {
319                                 case ND_SHADING:
320                                 case ND_SHADING_DRAW:
321                                 case ND_NODES:
322                                         /* currently works by redraws... if preview is set, it (re)starts job */
323                                         sbuts->preview= 1;
324                                         break;
325                         }                                       
326                         break;
327                 case NC_WORLD:
328                         buttons_area_redraw(sa, BCONTEXT_WORLD);
329                         sbuts->preview= 1;
330                         break;
331                 case NC_LAMP:
332                         buttons_area_redraw(sa, BCONTEXT_DATA);
333                         sbuts->preview= 1;
334                         break;
335                 case NC_BRUSH:
336                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
337                         break;
338                 case NC_TEXTURE:
339                 case NC_IMAGE:
340                         ED_area_tag_redraw(sa);
341                         sbuts->preview= 1;
342                         break;
343                 case NC_SPACE:
344                         if(wmn->data == ND_SPACE_PROPERTIES)
345                                 ED_area_tag_redraw(sa);
346                         break;
347                 case NC_ID:
348                         if(wmn->action == NA_RENAME)
349                                 ED_area_tag_redraw(sa);
350                         break;
351                 case NC_ANIMATION:
352                         switch(wmn->data) {
353                                 case ND_KEYFRAME:
354                                         if (wmn->action == NA_EDITED)
355                                                 ED_area_tag_redraw(sa);
356                                         break;
357                         }
358                         break;
359                 case NC_NODE:
360                         if(wmn->action==NA_SELECTED) {
361                                 ED_area_tag_redraw(sa);
362                                 /* new active node, update texture preview */
363                                 if(sbuts->mainb == BCONTEXT_TEXTURE)
364                                         sbuts->preview= 1;
365                         }
366                         break;
367                 /* Listener for preview render, when doing an global undo. */
368                 case NC_WINDOW:
369                         ED_area_tag_redraw(sa);
370                         sbuts->preview= 1;
371                         break;
372         }
373
374         if(wmn->data == ND_KEYS)
375                 ED_area_tag_redraw(sa);
376 }
377
378 /* only called once, from space/spacetypes.c */
379 void ED_spacetype_buttons(void)
380 {
381         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
382         ARegionType *art;
383         
384         st->spaceid= SPACE_BUTS;
385         strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
386         
387         st->new= buttons_new;
388         st->free= buttons_free;
389         st->init= buttons_init;
390         st->duplicate= buttons_duplicate;
391         st->operatortypes= buttons_operatortypes;
392         st->keymap= buttons_keymap;
393         st->listener= buttons_area_listener;
394         st->context= buttons_context;
395         
396         /* regions: main window */
397         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
398         art->regionid = RGN_TYPE_WINDOW;
399         art->init= buttons_main_area_init;
400         art->draw= buttons_main_area_draw;
401         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
402         BLI_addhead(&st->regiontypes, art);
403
404         buttons_context_register(art);
405         
406         /* regions: header */
407         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
408         art->regionid = RGN_TYPE_HEADER;
409         art->prefsizey= HEADERY;
410         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
411         
412         art->init= buttons_header_area_init;
413         art->draw= buttons_header_area_draw;
414         BLI_addhead(&st->regiontypes, art);
415
416         BKE_spacetype_register(st);
417 }
418