8284744d5196035facf7177166051500f5cb8425
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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_arithb.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_previewrender.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->scaflag= BUTS_SENS_LINK|BUTS_SENS_ACT|BUTS_CONT_ACT|BUTS_ACT_ACT|BUTS_ACT_LINK;
74         sbuts->align= BUT_AUTO;
75
76         /* header */
77         ar= MEM_callocN(sizeof(ARegion), "header for buts");
78         
79         BLI_addtail(&sbuts->regionbase, ar);
80         ar->regiontype= RGN_TYPE_HEADER;
81         ar->alignment= RGN_ALIGN_BOTTOM;
82         
83 #if 0
84         /* context area */
85         ar= MEM_callocN(sizeof(ARegion), "context area for buts");
86         BLI_addtail(&sbuts->regionbase, ar);
87         ar->regiontype= RGN_TYPE_CHANNELS;
88         ar->alignment= RGN_ALIGN_TOP;
89 #endif
90
91         /* main area */
92         ar= MEM_callocN(sizeof(ARegion), "main area for buts");
93         
94         BLI_addtail(&sbuts->regionbase, ar);
95         ar->regiontype= RGN_TYPE_WINDOW;
96         
97         return (SpaceLink *)sbuts;
98 }
99
100 /* not spacelink itself */
101 static void buttons_free(SpaceLink *sl)
102 {       
103         SpaceButs *sbuts= (SpaceButs*) sl;
104         
105         if(sbuts->ri) { 
106                 if (sbuts->ri->rect) MEM_freeN(sbuts->ri->rect);
107                 MEM_freeN(sbuts->ri);
108         }
109
110         if(sbuts->path)
111                 MEM_freeN(sbuts->path);
112 }
113
114 /* spacetype; init callback */
115 static void buttons_init(struct wmWindowManager *wm, ScrArea *sa)
116 {
117         SpaceButs *sbuts= sa->spacedata.first;
118
119         /* auto-align based on size */
120         if(sbuts->align == BUT_AUTO || !sbuts->align) {
121                 if(sa->winx > sa->winy)
122                         sbuts->align= BUT_HORIZONTAL;
123                 else
124                         sbuts->align= BUT_VERTICAL;
125         }
126 }
127
128 static SpaceLink *buttons_duplicate(SpaceLink *sl)
129 {
130         SpaceButs *sbutsn= MEM_dupallocN(sl);
131         
132         /* clear or remove stuff from old */
133         sbutsn->ri= NULL;
134         sbutsn->path= NULL;
135         
136         return (SpaceLink *)sbutsn;
137 }
138
139 /* add handlers, stuff you only do once or on area/region changes */
140 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
141 {
142         ListBase *keymap;
143
144         ED_region_panels_init(wm, ar);
145
146         keymap= WM_keymap_listbase(wm, "Buttons Generic", SPACE_BUTS, 0);
147         WM_event_add_keymap_handler(&ar->handlers, keymap);
148 }
149
150 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
151 {
152         /* draw entirely, view changes should be handled here */
153         SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
154         int vertical= (sbuts->align == BUT_VERTICAL);
155
156         buttons_context_compute(C, sbuts);
157
158         if(sbuts->mainb == BCONTEXT_SCENE)
159                 ED_region_panels(C, ar, vertical, "scene");
160         else if(sbuts->mainb == BCONTEXT_WORLD)
161                 ED_region_panels(C, ar, vertical, "world");
162         else if(sbuts->mainb == BCONTEXT_OBJECT)
163                 ED_region_panels(C, ar, vertical, "object");
164         else if(sbuts->mainb == BCONTEXT_DATA)
165                 ED_region_panels(C, ar, vertical, "data");
166         else if(sbuts->mainb == BCONTEXT_MATERIAL)
167                 ED_region_panels(C, ar, vertical, "material");
168         else if(sbuts->mainb == BCONTEXT_TEXTURE)
169                 ED_region_panels(C, ar, vertical, "texture");
170         else if(sbuts->mainb == BCONTEXT_PARTICLE)
171                 ED_region_panels(C, ar, vertical, "particle");
172         else if(sbuts->mainb == BCONTEXT_PHYSICS)
173                 ED_region_panels(C, ar, vertical, "physics");
174         else if(sbuts->mainb == BCONTEXT_BONE)
175                 ED_region_panels(C, ar, vertical, "bone");
176         else if(sbuts->mainb == BCONTEXT_MODIFIER)
177                 ED_region_panels(C, ar, vertical, "modifier");
178         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
179                 ED_region_panels(C, ar, vertical, "constraint");
180
181     sbuts->re_align= 0;
182         sbuts->mainbo= sbuts->mainb;
183 }
184
185 void buttons_operatortypes(void)
186 {
187         WM_operatortype_append(OBJECT_OT_group_add);
188         WM_operatortype_append(OBJECT_OT_group_remove);
189
190         WM_operatortype_append(OBJECT_OT_material_slot_add);
191         WM_operatortype_append(OBJECT_OT_material_slot_remove);
192         WM_operatortype_append(OBJECT_OT_material_slot_assign);
193         WM_operatortype_append(OBJECT_OT_material_slot_select);
194         WM_operatortype_append(OBJECT_OT_material_slot_deselect);
195
196         WM_operatortype_append(MATERIAL_OT_new);
197         WM_operatortype_append(TEXTURE_OT_new);
198         WM_operatortype_append(WORLD_OT_new);
199
200         WM_operatortype_append(OBJECT_OT_particle_system_add);
201         WM_operatortype_append(OBJECT_OT_particle_system_remove);
202
203         WM_operatortype_append(PARTICLE_OT_new);
204         WM_operatortype_append(PARTICLE_OT_new_target);
205         WM_operatortype_append(PARTICLE_OT_remove_target);
206         WM_operatortype_append(PARTICLE_OT_target_move_up);
207         WM_operatortype_append(PARTICLE_OT_target_move_down);
208
209         WM_operatortype_append(SCENE_OT_render_layer_add);
210         WM_operatortype_append(SCENE_OT_render_layer_remove);
211
212         WM_operatortype_append(BUTTONS_OT_toolbox);
213 }
214
215 void buttons_keymap(struct wmWindowManager *wm)
216 {
217         ListBase *keymap= WM_keymap_listbase(wm, "Buttons Generic", SPACE_BUTS, 0);
218         
219         WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
220 }
221
222 //#define PY_HEADER
223 /* add handlers, stuff you only do once or on area/region changes */
224 static void buttons_header_area_init(wmWindowManager *wm, ARegion *ar)
225 {
226 #ifdef PY_HEADER
227         ED_region_header_init(ar);
228 #else
229         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
230 #endif
231 }
232
233 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
234 {
235 #ifdef PY_HEADER
236         ED_region_header(C, ar);
237 #else
238
239         float col[3];
240         
241         /* clear */
242         if(ED_screen_area_active(C))
243                 UI_GetThemeColor3fv(TH_HEADER, col);
244         else
245                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
246         
247         glClearColor(col[0], col[1], col[2], 0.0);
248         glClear(GL_COLOR_BUFFER_BIT);
249         
250         /* set view2d view matrix for scrolling (without scrollers) */
251         UI_view2d_view_ortho(C, &ar->v2d);
252         
253         buttons_header_buttons(C, ar);
254 #endif  
255
256         /* restore view matrix? */
257         UI_view2d_view_restore(C);
258 }
259
260 #if 0
261 /* add handlers, stuff you only do once or on area/region changes */
262 static void buttons_context_area_init(wmWindowManager *wm, ARegion *ar)
263 {
264         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
265 }
266
267 #define CONTEXTY        30
268
269 static void buttons_context_area_draw(const bContext *C, ARegion *ar)
270 {
271         SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
272         uiStyle *style= U.uistyles.first;
273         uiBlock *block;
274         uiLayout *layout;
275         View2D *v2d= &ar->v2d;
276         float col[3];
277         int x, y, w, h;
278
279         buttons_context_compute(C, sbuts);
280
281         w= v2d->cur.xmax - v2d->cur.xmin;
282         h= v2d->cur.ymax - v2d->cur.ymin;
283         UI_view2d_view_ortho(C, v2d);
284
285         /* create UI */
286         block= uiBeginBlock(C, ar, "buttons_context", UI_EMBOSS);
287         layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_PANEL,
288                 style->panelspace, h - (h-UI_UNIT_Y)/2, w, 20, style);
289
290         buttons_context_draw(C, layout);
291
292         uiBlockLayoutResolve(C, block, &x, &y);
293         uiEndBlock(C, block);
294
295         /* draw */
296         UI_SetTheme(SPACE_BUTS, RGN_TYPE_WINDOW); /* XXX */
297
298         UI_GetThemeColor3fv(TH_BACK, col);
299         glClearColor(col[0], col[1], col[2], 0.0);
300         glClear(GL_COLOR_BUFFER_BIT);
301
302         UI_view2d_totRect_set(v2d, x, -y);
303         UI_view2d_view_ortho(C, v2d);
304
305         uiDrawBlock(C, block);
306
307         /* restore view matrix */
308         UI_view2d_view_restore(C);
309 }
310 #endif
311
312 /* reused! */
313 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
314 {
315         SpaceButs *sbuts= sa->spacedata.first;
316
317         /* context changes */
318         switch(wmn->category) {
319                 case NC_SCENE:
320                         switch(wmn->data) {
321                                 case ND_FRAME:
322                                 case ND_MODE:
323                                         ED_area_tag_redraw(sa);
324                                         break;
325                                         
326                                 case ND_OB_ACTIVE:
327                                         ED_area_tag_redraw(sa);
328                                         sbuts->preview= 1;
329                                         break;
330                         }
331                         break;
332                 case NC_OBJECT:
333                         switch(wmn->data) {
334                                 case ND_TRANSFORM:
335                                 case ND_BONE_ACTIVE:
336                                 case ND_BONE_SELECT:
337                                 case ND_GEOM_SELECT:
338                                 case ND_CONSTRAINT:
339                                         ED_area_tag_redraw(sa);
340                                         break;
341                                 case ND_SHADING:
342                                 case ND_SHADING_DRAW:
343                                         /* currently works by redraws... if preview is set, it (re)starts job */
344                                         sbuts->preview= 1;
345                                         break;
346                         }
347                         break;
348                 case NC_MATERIAL:
349                         ED_area_tag_redraw(sa);
350                         
351                         switch(wmn->data) {
352                                 case ND_SHADING:
353                                 case ND_SHADING_DRAW:
354                                         /* currently works by redraws... if preview is set, it (re)starts job */
355                                         sbuts->preview= 1;
356                                         break;
357                         }                                       
358                         break;
359                 case NC_WORLD:
360                         ED_area_tag_redraw(sa);
361                         sbuts->preview= 1;
362                 case NC_LAMP:
363                         ED_area_tag_redraw(sa);
364                         sbuts->preview= 1;
365                 case NC_TEXTURE:
366                         ED_area_tag_redraw(sa);
367                         sbuts->preview= 1;
368         }
369
370         if(wmn->data == ND_KEYS)
371                 ED_area_tag_redraw(sa);
372 }
373
374 /* only called once, from space/spacetypes.c */
375 void ED_spacetype_buttons(void)
376 {
377         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
378         ARegionType *art;
379         
380         st->spaceid= SPACE_BUTS;
381         
382         st->new= buttons_new;
383         st->free= buttons_free;
384         st->init= buttons_init;
385         st->duplicate= buttons_duplicate;
386         st->operatortypes= buttons_operatortypes;
387         st->keymap= buttons_keymap;
388         st->listener= buttons_area_listener;
389         st->context= buttons_context;
390         
391         /* regions: main window */
392         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
393         art->regionid = RGN_TYPE_WINDOW;
394         art->init= buttons_main_area_init;
395         art->draw= buttons_main_area_draw;
396         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
397         BLI_addhead(&st->regiontypes, art);
398
399         buttons_context_register(art);
400         
401         /* regions: header */
402         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
403         art->regionid = RGN_TYPE_HEADER;
404         art->minsizey= BUTS_HEADERY;
405         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
406         
407         art->init= buttons_header_area_init;
408         art->draw= buttons_header_area_draw;
409         BLI_addhead(&st->regiontypes, art);
410
411 #if 0
412         /* regions: context */
413         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
414         art->regionid = RGN_TYPE_CHANNELS;
415         art->minsizey= CONTEXTY;
416         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
417         art->init= buttons_context_area_init;
418         art->draw= buttons_context_area_draw;;
419         art->listener= buttons_area_listener;
420         
421         BLI_addhead(&st->regiontypes, art);
422 #endif
423         
424         BKE_spacetype_register(st);
425 }
426