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