2.5: Various Fixes
[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->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         ListBase *keymap;
142
143         ED_region_panels_init(wm, ar);
144
145         keymap= WM_keymap_listbase(wm, "Buttons Generic", 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= (SpaceButs*)CTX_wm_space_data(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");
159         else if(sbuts->mainb == BCONTEXT_WORLD)
160                 ED_region_panels(C, ar, vertical, "world");
161         else if(sbuts->mainb == BCONTEXT_OBJECT)
162                 ED_region_panels(C, ar, vertical, "object");
163         else if(sbuts->mainb == BCONTEXT_DATA)
164                 ED_region_panels(C, ar, vertical, "data");
165         else if(sbuts->mainb == BCONTEXT_MATERIAL)
166                 ED_region_panels(C, ar, vertical, "material");
167         else if(sbuts->mainb == BCONTEXT_TEXTURE)
168                 ED_region_panels(C, ar, vertical, "texture");
169         else if(sbuts->mainb == BCONTEXT_PARTICLE)
170                 ED_region_panels(C, ar, vertical, "particle");
171         else if(sbuts->mainb == BCONTEXT_PHYSICS)
172                 ED_region_panels(C, ar, vertical, "physics");
173         else if(sbuts->mainb == BCONTEXT_BONE)
174                 ED_region_panels(C, ar, vertical, "bone");
175         else if(sbuts->mainb == BCONTEXT_MODIFIER)
176                 ED_region_panels(C, ar, vertical, "modifier");
177         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
178                 ED_region_panels(C, ar, vertical, "constraint");
179
180     sbuts->re_align= 0;
181         sbuts->mainbo= sbuts->mainb;
182 }
183
184 void buttons_operatortypes(void)
185 {
186         WM_operatortype_append(OBJECT_OT_group_add);
187         WM_operatortype_append(OBJECT_OT_group_remove);
188
189         WM_operatortype_append(OBJECT_OT_material_slot_add);
190         WM_operatortype_append(OBJECT_OT_material_slot_remove);
191         WM_operatortype_append(OBJECT_OT_material_slot_assign);
192         WM_operatortype_append(OBJECT_OT_material_slot_select);
193         WM_operatortype_append(OBJECT_OT_material_slot_deselect);
194
195         WM_operatortype_append(MATERIAL_OT_new);
196         WM_operatortype_append(TEXTURE_OT_new);
197         WM_operatortype_append(WORLD_OT_new);
198
199         WM_operatortype_append(OBJECT_OT_particle_system_add);
200         WM_operatortype_append(OBJECT_OT_particle_system_remove);
201
202         WM_operatortype_append(PARTICLE_OT_new);
203         WM_operatortype_append(PARTICLE_OT_new_target);
204         WM_operatortype_append(PARTICLE_OT_remove_target);
205         WM_operatortype_append(PARTICLE_OT_target_move_up);
206         WM_operatortype_append(PARTICLE_OT_target_move_down);
207
208         WM_operatortype_append(SCENE_OT_render_layer_add);
209         WM_operatortype_append(SCENE_OT_render_layer_remove);
210
211         WM_operatortype_append(BUTTONS_OT_toolbox);
212 }
213
214 void buttons_keymap(struct wmWindowManager *wm)
215 {
216         ListBase *keymap= WM_keymap_listbase(wm, "Buttons Generic", SPACE_BUTS, 0);
217         
218         WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
219 }
220
221 //#define PY_HEADER
222 /* add handlers, stuff you only do once or on area/region changes */
223 static void buttons_header_area_init(wmWindowManager *wm, ARegion *ar)
224 {
225 #ifdef PY_HEADER
226         ED_region_header_init(ar);
227 #else
228         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
229 #endif
230 }
231
232 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
233 {
234 #ifdef PY_HEADER
235         ED_region_header(C, ar);
236 #else
237
238         float col[3];
239         
240         /* clear */
241         if(ED_screen_area_active(C))
242                 UI_GetThemeColor3fv(TH_HEADER, col);
243         else
244                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
245         
246         glClearColor(col[0], col[1], col[2], 0.0);
247         glClear(GL_COLOR_BUFFER_BIT);
248         
249         /* set view2d view matrix for scrolling (without scrollers) */
250         UI_view2d_view_ortho(C, &ar->v2d);
251         
252         buttons_header_buttons(C, ar);
253 #endif  
254
255         /* restore view matrix? */
256         UI_view2d_view_restore(C);
257 }
258
259 #if 0
260 /* add handlers, stuff you only do once or on area/region changes */
261 static void buttons_context_area_init(wmWindowManager *wm, ARegion *ar)
262 {
263         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
264 }
265
266 #define CONTEXTY        30
267
268 static void buttons_context_area_draw(const bContext *C, ARegion *ar)
269 {
270         SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
271         uiStyle *style= U.uistyles.first;
272         uiBlock *block;
273         uiLayout *layout;
274         View2D *v2d= &ar->v2d;
275         float col[3];
276         int x, y, w, h;
277
278         buttons_context_compute(C, sbuts);
279
280         w= v2d->cur.xmax - v2d->cur.xmin;
281         h= v2d->cur.ymax - v2d->cur.ymin;
282         UI_view2d_view_ortho(C, v2d);
283
284         /* create UI */
285         block= uiBeginBlock(C, ar, "buttons_context", UI_EMBOSS);
286         layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_PANEL,
287                 style->panelspace, h - (h-UI_UNIT_Y)/2, w, 20, style);
288
289         buttons_context_draw(C, layout);
290
291         uiBlockLayoutResolve(C, block, &x, &y);
292         uiEndBlock(C, block);
293
294         /* draw */
295         UI_SetTheme(SPACE_BUTS, RGN_TYPE_WINDOW); /* XXX */
296
297         UI_GetThemeColor3fv(TH_BACK, col);
298         glClearColor(col[0], col[1], col[2], 0.0);
299         glClear(GL_COLOR_BUFFER_BIT);
300
301         UI_view2d_totRect_set(v2d, x, -y);
302         UI_view2d_view_ortho(C, v2d);
303
304         uiDrawBlock(C, block);
305
306         /* restore view matrix */
307         UI_view2d_view_restore(C);
308 }
309 #endif
310
311 /* reused! */
312 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
313 {
314         SpaceButs *sbuts= sa->spacedata.first;
315
316         /* context changes */
317         switch(wmn->category) {
318                 case NC_SCENE:
319                         switch(wmn->data) {
320                                 case ND_FRAME:
321                                 case ND_MODE:
322                                         ED_area_tag_redraw(sa);
323                                         break;
324                                         
325                                 case ND_OB_ACTIVE:
326                                         ED_area_tag_redraw(sa);
327                                         sbuts->preview= 1;
328                                         break;
329                         }
330                         break;
331                 case NC_OBJECT:
332                         switch(wmn->data) {
333                                 case ND_TRANSFORM:
334                                 case ND_BONE_ACTIVE:
335                                 case ND_BONE_SELECT:
336                                 case ND_GEOM_SELECT:
337                                 case ND_CONSTRAINT:
338                                         ED_area_tag_redraw(sa);
339                                         break;
340                                 case ND_SHADING:
341                                 case ND_SHADING_DRAW:
342                                         /* currently works by redraws... if preview is set, it (re)starts job */
343                                         sbuts->preview= 1;
344                                         break;
345                         }
346                         break;
347                 case NC_MATERIAL:
348                         ED_area_tag_redraw(sa);
349                         
350                         switch(wmn->data) {
351                                 case ND_SHADING:
352                                 case ND_SHADING_DRAW:
353                                         /* currently works by redraws... if preview is set, it (re)starts job */
354                                         sbuts->preview= 1;
355                                         break;
356                         }                                       
357                         break;
358                 case NC_WORLD:
359                         ED_area_tag_redraw(sa);
360                         sbuts->preview= 1;
361                 case NC_LAMP:
362                         ED_area_tag_redraw(sa);
363                         sbuts->preview= 1;
364                 case NC_TEXTURE:
365                         ED_area_tag_redraw(sa);
366                         sbuts->preview= 1;
367         }
368
369         if(wmn->data == ND_KEYS)
370                 ED_area_tag_redraw(sa);
371 }
372
373 /* only called once, from space/spacetypes.c */
374 void ED_spacetype_buttons(void)
375 {
376         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
377         ARegionType *art;
378         
379         st->spaceid= SPACE_BUTS;
380         
381         st->new= buttons_new;
382         st->free= buttons_free;
383         st->init= buttons_init;
384         st->duplicate= buttons_duplicate;
385         st->operatortypes= buttons_operatortypes;
386         st->keymap= buttons_keymap;
387         st->listener= buttons_area_listener;
388         st->context= buttons_context;
389         
390         /* regions: main window */
391         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
392         art->regionid = RGN_TYPE_WINDOW;
393         art->init= buttons_main_area_init;
394         art->draw= buttons_main_area_draw;
395         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
396         BLI_addhead(&st->regiontypes, art);
397
398         buttons_context_register(art);
399         
400         /* regions: header */
401         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
402         art->regionid = RGN_TYPE_HEADER;
403         art->minsizey= BUTS_HEADERY;
404         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
405         
406         art->init= buttons_header_area_init;
407         art->draw= buttons_header_area_draw;
408         BLI_addhead(&st->regiontypes, art);
409
410 #if 0
411         /* regions: context */
412         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
413         art->regionid = RGN_TYPE_CHANNELS;
414         art->minsizey= CONTEXTY;
415         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
416         art->init= buttons_context_area_init;
417         art->draw= buttons_context_area_draw;;
418         art->listener= buttons_area_listener;
419         
420         BLI_addhead(&st->regiontypes, art);
421 #endif
422         
423         BKE_spacetype_register(st);
424 }
425