b6feb4ee1d6b773a4d801722d2f67f8674a9ed9e
[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 #include <string.h>
30 #include <stdio.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_math.h"
36 #include "BLI_rand.h"
37 #include "BLI_utildefines.h"
38
39 #include "BKE_context.h"
40 #include "BKE_screen.h"
41
42 #include "ED_space_api.h"
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 *UNUSED(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_TOP;
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 *UNUSED(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 static void buttons_operatortypes(void)
182 {
183         WM_operatortype_append(BUTTONS_OT_toolbox);
184         WM_operatortype_append(BUTTONS_OT_file_browse);
185 }
186
187 static 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 /* add handlers, stuff you only do once or on area/region changes */
195 static void buttons_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
196 {
197         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
198 }
199
200 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
201 {
202         /* clear */
203         UI_ThemeClearColor(ED_screen_area_active(C)?TH_HEADER:TH_HEADERDESEL);
204         glClear(GL_COLOR_BUFFER_BIT);
205         
206         /* set view2d view matrix for scrolling (without scrollers) */
207         UI_view2d_view_ortho(&ar->v2d);
208         
209         buttons_header_buttons(C, ar);
210
211         /* restore view matrix? */
212         UI_view2d_view_restore(C);
213 }
214
215 /* draw a certain button set only if properties area is currently
216  * showing that button set, to reduce unnecessary drawing. */
217 static void buttons_area_redraw(ScrArea *sa, short buttons)
218 {
219         SpaceButs *sbuts= sa->spacedata.first;
220         
221         /* if the area's current button set is equal to the one to redraw */
222         if(sbuts->mainb == buttons)
223                 ED_area_tag_redraw(sa);
224 }
225
226 /* reused! */
227 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
228 {
229         SpaceButs *sbuts= sa->spacedata.first;
230
231         /* context changes */
232         switch(wmn->category) {
233                 case NC_SCENE:
234                         switch(wmn->data) {
235                                 case ND_RENDER_OPTIONS:
236                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
237                                         break;
238                                 case ND_FRAME:
239                                         /* any buttons area can have animated properties so redraw all */
240                                         ED_area_tag_redraw(sa);
241                                         sbuts->preview= 1;
242                                         break;
243                                 case ND_OB_ACTIVE:
244                                         ED_area_tag_redraw(sa);
245                                         sbuts->preview= 1;
246                                         break;
247                                 case ND_KEYINGSET:
248                                         buttons_area_redraw(sa, BCONTEXT_SCENE);
249                                         break;
250                                 case ND_RENDER_RESULT:
251                                         break;
252                                 case ND_MODE:
253                                 case ND_LAYER:
254                                 default:
255                                         ED_area_tag_redraw(sa);
256                                         break;
257                         }
258                         break;
259                 case NC_OBJECT:
260                         switch(wmn->data) {
261                                 case ND_TRANSFORM:
262                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
263                                         buttons_area_redraw(sa, BCONTEXT_DATA); /* autotexpace flag */
264                                         break;
265                                 case ND_POSE:
266                                 case ND_BONE_ACTIVE:
267                                 case ND_BONE_SELECT:
268                                         buttons_area_redraw(sa, BCONTEXT_BONE);
269                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
270                                         break;
271                                 case ND_MODIFIER:
272                                         if(wmn->action == NA_RENAME)
273                                                 ED_area_tag_redraw(sa);
274                                         else
275                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
276                                                 buttons_area_redraw(sa, BCONTEXT_PHYSICS);
277                                         break;
278                                 case ND_CONSTRAINT:
279                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
280                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
281                                         break;
282                                 case ND_PARTICLE:
283                                         if (wmn->action == NA_EDITED)
284                                                 buttons_area_redraw(sa, BCONTEXT_PARTICLE);
285                                         sbuts->preview= 1;
286                                         break;
287                                 case ND_DRAW:
288                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
289                                         buttons_area_redraw(sa, BCONTEXT_DATA);
290                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
291                                 case ND_SHADING:
292                                 case ND_SHADING_DRAW:
293                                         /* currently works by redraws... if preview is set, it (re)starts job */
294                                         sbuts->preview= 1;
295                                         break;
296                                 default:
297                                         /* Not all object RNA props have a ND_ notifier (yet) */
298                                         ED_area_tag_redraw(sa);
299                                         break;
300                         }
301                         break;
302                 case NC_GEOM:
303                         switch(wmn->data) {
304                                 case ND_SELECT:
305                                 case ND_DATA:
306                                         ED_area_tag_redraw(sa);
307                                         break;
308                         }
309                         break;
310                 case NC_MATERIAL:
311                         ED_area_tag_redraw(sa);
312                         switch(wmn->data) {
313                                 case ND_SHADING:
314                                 case ND_SHADING_DRAW:
315                                 case ND_NODES:
316                                         /* currently works by redraws... if preview is set, it (re)starts job */
317                                         sbuts->preview= 1;
318                                         break;
319                         }                                       
320                         break;
321                 case NC_WORLD:
322                         buttons_area_redraw(sa, BCONTEXT_WORLD);
323                         sbuts->preview= 1;
324                         break;
325                 case NC_LAMP:
326                         buttons_area_redraw(sa, BCONTEXT_DATA);
327                         sbuts->preview= 1;
328                         break;
329                 case NC_BRUSH:
330                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
331                         break;
332                 case NC_TEXTURE:
333                 case NC_IMAGE:
334                         ED_area_tag_redraw(sa);
335                         sbuts->preview= 1;
336                         break;
337                 case NC_SPACE:
338                         if(wmn->data == ND_SPACE_PROPERTIES)
339                                 ED_area_tag_redraw(sa);
340                         break;
341                 case NC_ID:
342                         if(wmn->action == NA_RENAME)
343                                 ED_area_tag_redraw(sa);
344                         break;
345                 case NC_ANIMATION:
346                         switch(wmn->data) {
347                                 case ND_KEYFRAME:
348                                         if (wmn->action == NA_EDITED)
349                                                 ED_area_tag_redraw(sa);
350                                         break;
351                         }
352                         break;
353                 case NC_NODE:
354                         if(wmn->action==NA_SELECTED)
355                                 ED_area_tag_redraw(sa);
356                         break;
357                 /* Listener for preview render, when doing an global undo. */
358                 case NC_WINDOW:
359                         ED_area_tag_redraw(sa);
360                         sbuts->preview= 1;
361                         break;
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