Merged changes in the trunk up to revision 46045.
[blender-staging.git] / source / blender / editors / space_buttons / space_buttons.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_buttons/space_buttons.c
28  *  \ingroup spbuttons
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_rand.h"
40 #include "BLI_utildefines.h"
41
42 #include "BKE_context.h"
43 #include "BKE_screen.h"
44
45 #include "ED_space_api.h"
46 #include "ED_screen.h"
47
48 #include "BIF_gl.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "UI_resources.h"
54 #include "UI_view2d.h"
55
56 #include "ED_render.h"
57
58 #include "buttons_intern.h"     // own include
59
60 /* ******************** default callbacks for buttons space ***************** */
61
62 static SpaceLink *buttons_new(const bContext *UNUSED(C))
63 {
64         ARegion *ar;
65         SpaceButs *sbuts;
66         
67         sbuts= MEM_callocN(sizeof(SpaceButs), "initbuts");
68         sbuts->spacetype= SPACE_BUTS;
69         sbuts->align= BUT_VERTICAL;
70
71         /* header */
72         ar= MEM_callocN(sizeof(ARegion), "header for buts");
73         
74         BLI_addtail(&sbuts->regionbase, ar);
75         ar->regiontype= RGN_TYPE_HEADER;
76         ar->alignment= RGN_ALIGN_TOP;
77         
78 #if 0
79         /* context area */
80         ar= MEM_callocN(sizeof(ARegion), "context area for buts");
81         BLI_addtail(&sbuts->regionbase, ar);
82         ar->regiontype= RGN_TYPE_CHANNELS;
83         ar->alignment= RGN_ALIGN_TOP;
84 #endif
85
86         /* main area */
87         ar= MEM_callocN(sizeof(ARegion), "main area for buts");
88         
89         BLI_addtail(&sbuts->regionbase, ar);
90         ar->regiontype= RGN_TYPE_WINDOW;
91         
92         return (SpaceLink *)sbuts;
93 }
94
95 /* not spacelink itself */
96 static void buttons_free(SpaceLink *sl)
97 {       
98         SpaceButs *sbuts= (SpaceButs*) sl;
99
100         if (sbuts->path)
101                 MEM_freeN(sbuts->path);
102         
103         if (sbuts->texuser) {
104                 ButsContextTexture *ct= sbuts->texuser;
105                 BLI_freelistN(&ct->users);
106                 MEM_freeN(ct);
107         }
108 }
109
110 /* spacetype; init callback */
111 static void buttons_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
112 {
113         SpaceButs *sbuts= sa->spacedata.first;
114
115         /* auto-align based on size */
116         if (sbuts->align == BUT_AUTO || !sbuts->align) {
117                 if (sa->winx > sa->winy)
118                         sbuts->align= BUT_HORIZONTAL;
119                 else
120                         sbuts->align= BUT_VERTICAL;
121         }
122 }
123
124 static SpaceLink *buttons_duplicate(SpaceLink *sl)
125 {
126         SpaceButs *sbutsn= MEM_dupallocN(sl);
127         
128         /* clear or remove stuff from old */
129         sbutsn->path= NULL;
130         sbutsn->texuser= NULL;
131         
132         return (SpaceLink *)sbutsn;
133 }
134
135 /* add handlers, stuff you only do once or on area/region changes */
136 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
137 {
138         wmKeyMap *keymap;
139
140         ED_region_panels_init(wm, ar);
141
142         keymap = WM_keymap_find(wm->defaultconf, "Property Editor", SPACE_BUTS, 0);
143         WM_event_add_keymap_handler(&ar->handlers, keymap);
144 }
145
146 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
147 {
148         /* draw entirely, view changes should be handled here */
149         SpaceButs *sbuts= CTX_wm_space_buts(C);
150         int vertical= (sbuts->align == BUT_VERTICAL);
151
152         buttons_context_compute(C, sbuts);
153
154         if (sbuts->mainb == BCONTEXT_SCENE)
155                 ED_region_panels(C, ar, vertical, "scene", sbuts->mainb);
156         else if (sbuts->mainb == BCONTEXT_RENDER)
157                 ED_region_panels(C, ar, vertical, "render", sbuts->mainb);
158         else if (sbuts->mainb == BCONTEXT_WORLD)
159                 ED_region_panels(C, ar, vertical, "world", sbuts->mainb);
160         else if (sbuts->mainb == BCONTEXT_OBJECT)
161                 ED_region_panels(C, ar, vertical, "object", sbuts->mainb);
162         else if (sbuts->mainb == BCONTEXT_DATA)
163                 ED_region_panels(C, ar, vertical, "data", sbuts->mainb);
164         else if (sbuts->mainb == BCONTEXT_MATERIAL)
165                 ED_region_panels(C, ar, vertical, "material", sbuts->mainb);
166         else if (sbuts->mainb == BCONTEXT_TEXTURE)
167                 ED_region_panels(C, ar, vertical, "texture", sbuts->mainb);
168         else if (sbuts->mainb == BCONTEXT_PARTICLE)
169                 ED_region_panels(C, ar, vertical, "particle", sbuts->mainb);
170         else if (sbuts->mainb == BCONTEXT_PHYSICS)
171                 ED_region_panels(C, ar, vertical, "physics", sbuts->mainb);
172         else if (sbuts->mainb == BCONTEXT_BONE)
173                 ED_region_panels(C, ar, vertical, "bone", sbuts->mainb);
174         else if (sbuts->mainb == BCONTEXT_MODIFIER)
175                 ED_region_panels(C, ar, vertical, "modifier", sbuts->mainb);
176         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
177                 ED_region_panels(C, ar, vertical, "constraint", sbuts->mainb);
178         else if (sbuts->mainb == BCONTEXT_BONE_CONSTRAINT)
179                 ED_region_panels(C, ar, vertical, "bone_constraint", sbuts->mainb);
180
181         sbuts->re_align= 0;
182         sbuts->mainbo= sbuts->mainb;
183 }
184
185 static void buttons_operatortypes(void)
186 {
187         WM_operatortype_append(BUTTONS_OT_toolbox);
188         WM_operatortype_append(BUTTONS_OT_file_browse);
189         WM_operatortype_append(BUTTONS_OT_directory_browse);
190 }
191
192 static void buttons_keymap(struct wmKeyConfig *keyconf)
193 {
194         wmKeyMap *keymap = WM_keymap_find(keyconf, "Property Editor", SPACE_BUTS, 0);
195         
196         WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
197 }
198
199 /* add handlers, stuff you only do once or on area/region changes */
200 static void buttons_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
201 {
202         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
203 }
204
205 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
206 {
207         /* clear */
208         UI_ThemeClearColor(ED_screen_area_active(C)?TH_HEADER:TH_HEADERDESEL);
209         glClear(GL_COLOR_BUFFER_BIT);
210         
211         /* set view2d view matrix for scrolling (without scrollers) */
212         UI_view2d_view_ortho(&ar->v2d);
213         
214         buttons_header_buttons(C, ar);
215
216         /* restore view matrix? */
217         UI_view2d_view_restore(C);
218 }
219
220 /* draw a certain button set only if properties area is currently
221  * showing that button set, to reduce unnecessary drawing. */
222 static void buttons_area_redraw(ScrArea *sa, short buttons)
223 {
224         SpaceButs *sbuts= sa->spacedata.first;
225         
226         /* if the area's current button set is equal to the one to redraw */
227         if (sbuts->mainb == buttons)
228                 ED_area_tag_redraw(sa);
229 }
230
231 /* reused! */
232 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
233 {
234         SpaceButs *sbuts= sa->spacedata.first;
235
236         /* context changes */
237         switch (wmn->category) {
238                 case NC_SCENE:
239                         switch (wmn->data) {
240                                 case ND_RENDER_OPTIONS:
241                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
242                                         break;
243                                 case ND_FRAME:
244                                         /* any buttons area can have animated properties so redraw all */
245                                         ED_area_tag_redraw(sa);
246                                         sbuts->preview= 1;
247                                         break;
248                                 case ND_OB_ACTIVE:
249                                         ED_area_tag_redraw(sa);
250                                         sbuts->preview= 1;
251                                         break;
252                                 case ND_KEYINGSET:
253                                         buttons_area_redraw(sa, BCONTEXT_SCENE);
254                                         break;
255                                 case ND_RENDER_RESULT:
256                                         break;
257                                 case ND_MODE:
258                                 case ND_LAYER:
259                                 default:
260                                         ED_area_tag_redraw(sa);
261                                         break;
262                         }
263                         break;
264                 case NC_OBJECT:
265                         switch (wmn->data) {
266                                 case ND_TRANSFORM:
267                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
268                                         buttons_area_redraw(sa, BCONTEXT_DATA); /* autotexpace flag */
269                                         break;
270                                 case ND_POSE:
271                                         buttons_area_redraw(sa, BCONTEXT_DATA);
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                                         buttons_area_redraw(sa, BCONTEXT_DATA);
277                                         break;
278                                 case ND_MODIFIER:
279                                         if (wmn->action == NA_RENAME)
280                                                 ED_area_tag_redraw(sa);
281                                         else
282                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
283                                                 buttons_area_redraw(sa, BCONTEXT_PHYSICS);
284                                         break;
285                                 case ND_CONSTRAINT:
286                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
287                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
288                                         break;
289                                 case ND_PARTICLE:
290                                         if (wmn->action == NA_EDITED)
291                                                 buttons_area_redraw(sa, BCONTEXT_PARTICLE);
292                                         sbuts->preview= 1;
293                                         break;
294                                 case ND_DRAW:
295                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
296                                         buttons_area_redraw(sa, BCONTEXT_DATA);
297                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
298                                 case ND_SHADING:
299                                 case ND_SHADING_DRAW:
300                                         /* currently works by redraws... if preview is set, it (re)starts job */
301                                         sbuts->preview= 1;
302                                         break;
303                                 default:
304                                         /* Not all object RNA props have a ND_ notifier (yet) */
305                                         ED_area_tag_redraw(sa);
306                                         break;
307                         }
308                         break;
309                 case NC_GEOM:
310                         switch (wmn->data) {
311                                 case ND_SELECT:
312                                 case ND_DATA:
313                                         ED_area_tag_redraw(sa);
314                                         break;
315                         }
316                         break;
317                 case NC_MATERIAL:
318                         ED_area_tag_redraw(sa);
319                         switch (wmn->data) {
320                                 case ND_SHADING:
321                                 case ND_SHADING_DRAW:
322                                 case ND_NODES:
323                                         /* currently works by redraws... if preview is set, it (re)starts job */
324                                         sbuts->preview= 1;
325                                         break;
326                         }                                       
327                         break;
328                 case NC_WORLD:
329                         buttons_area_redraw(sa, BCONTEXT_WORLD);
330                         sbuts->preview= 1;
331                         break;
332                 case NC_LAMP:
333                         buttons_area_redraw(sa, BCONTEXT_DATA);
334                         sbuts->preview= 1;
335                         break;
336                 case NC_BRUSH:
337                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
338                         break;
339                 case NC_TEXTURE:
340                 case NC_IMAGE:
341                         ED_area_tag_redraw(sa);
342                         sbuts->preview= 1;
343                         break;
344                 case NC_SPACE:
345                         if (wmn->data == ND_SPACE_PROPERTIES)
346                                 ED_area_tag_redraw(sa);
347                         break;
348                 case NC_ID:
349                         if (wmn->action == NA_RENAME)
350                                 ED_area_tag_redraw(sa);
351                         break;
352                 case NC_ANIMATION:
353                         switch (wmn->data) {
354                                 case ND_KEYFRAME:
355                                         if (wmn->action == NA_EDITED)
356                                                 ED_area_tag_redraw(sa);
357                                         break;
358                         }
359                         break;
360                 case NC_NODE:
361                         if (wmn->action==NA_SELECTED) {
362                                 ED_area_tag_redraw(sa);
363                                 /* new active node, update texture preview */
364                                 if (sbuts->mainb == BCONTEXT_TEXTURE)
365                                         sbuts->preview= 1;
366                         }
367                         break;
368                 /* Listener for preview render, when doing an global undo. */
369                 case NC_WINDOW:
370                         ED_area_tag_redraw(sa);
371                         sbuts->preview= 1;
372                         break;
373                 case NC_LINESTYLE:
374                         ED_area_tag_redraw(sa);
375                         sbuts->preview= 1;
376                         break;
377         }
378
379         if (wmn->data == ND_KEYS)
380                 ED_area_tag_redraw(sa);
381 }
382
383 /* only called once, from space/spacetypes.c */
384 void ED_spacetype_buttons(void)
385 {
386         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
387         ARegionType *art;
388         
389         st->spaceid= SPACE_BUTS;
390         strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
391         
392         st->new= buttons_new;
393         st->free= buttons_free;
394         st->init= buttons_init;
395         st->duplicate= buttons_duplicate;
396         st->operatortypes= buttons_operatortypes;
397         st->keymap= buttons_keymap;
398         st->listener= buttons_area_listener;
399         st->context= buttons_context;
400         
401         /* regions: main window */
402         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
403         art->regionid = RGN_TYPE_WINDOW;
404         art->init= buttons_main_area_init;
405         art->draw= buttons_main_area_draw;
406         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
407         BLI_addhead(&st->regiontypes, art);
408
409         buttons_context_register(art);
410         
411         /* regions: header */
412         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
413         art->regionid = RGN_TYPE_HEADER;
414         art->prefsizey= HEADERY;
415         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
416         
417         art->init= buttons_header_area_init;
418         art->draw= buttons_header_area_draw;
419         BLI_addhead(&st->regiontypes, art);
420
421         BKE_spacetype_register(st);
422 }
423