Merged changes in the trunk up to revision 52690.
[blender.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_RENDER_LAYER)
159                 ED_region_panels(C, ar, vertical, "render_layer", sbuts->mainb);
160         else if (sbuts->mainb == BCONTEXT_WORLD)
161                 ED_region_panels(C, ar, vertical, "world", sbuts->mainb);
162         else if (sbuts->mainb == BCONTEXT_OBJECT)
163                 ED_region_panels(C, ar, vertical, "object", sbuts->mainb);
164         else if (sbuts->mainb == BCONTEXT_DATA)
165                 ED_region_panels(C, ar, vertical, "data", sbuts->mainb);
166         else if (sbuts->mainb == BCONTEXT_MATERIAL)
167                 ED_region_panels(C, ar, vertical, "material", sbuts->mainb);
168         else if (sbuts->mainb == BCONTEXT_TEXTURE)
169                 ED_region_panels(C, ar, vertical, "texture", sbuts->mainb);
170         else if (sbuts->mainb == BCONTEXT_PARTICLE)
171                 ED_region_panels(C, ar, vertical, "particle", sbuts->mainb);
172         else if (sbuts->mainb == BCONTEXT_PHYSICS)
173                 ED_region_panels(C, ar, vertical, "physics", sbuts->mainb);
174         else if (sbuts->mainb == BCONTEXT_BONE)
175                 ED_region_panels(C, ar, vertical, "bone", sbuts->mainb);
176         else if (sbuts->mainb == BCONTEXT_MODIFIER)
177                 ED_region_panels(C, ar, vertical, "modifier", sbuts->mainb);
178         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
179                 ED_region_panels(C, ar, vertical, "constraint", sbuts->mainb);
180         else if (sbuts->mainb == BCONTEXT_BONE_CONSTRAINT)
181                 ED_region_panels(C, ar, vertical, "bone_constraint", sbuts->mainb);
182
183         sbuts->re_align = 0;
184         sbuts->mainbo = sbuts->mainb;
185 }
186
187 static void buttons_operatortypes(void)
188 {
189         WM_operatortype_append(BUTTONS_OT_toolbox);
190         WM_operatortype_append(BUTTONS_OT_file_browse);
191         WM_operatortype_append(BUTTONS_OT_directory_browse);
192 }
193
194 static void buttons_keymap(struct wmKeyConfig *keyconf)
195 {
196         wmKeyMap *keymap = WM_keymap_find(keyconf, "Property Editor", SPACE_BUTS, 0);
197         
198         WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
199 }
200
201 /* add handlers, stuff you only do once or on area/region changes */
202 static void buttons_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
203 {
204         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
205 }
206
207 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
208 {
209         /* clear */
210         UI_ThemeClearColor(ED_screen_area_active(C) ? TH_HEADER : TH_HEADERDESEL);
211         glClear(GL_COLOR_BUFFER_BIT);
212         
213         /* set view2d view matrix for scrolling (without scrollers) */
214         UI_view2d_view_ortho(&ar->v2d);
215         
216         buttons_header_buttons(C, ar);
217
218         /* restore view matrix? */
219         UI_view2d_view_restore(C);
220 }
221
222 /* draw a certain button set only if properties area is currently
223  * showing that button set, to reduce unnecessary drawing. */
224 static void buttons_area_redraw(ScrArea *sa, short buttons)
225 {
226         SpaceButs *sbuts = sa->spacedata.first;
227         
228         /* if the area's current button set is equal to the one to redraw */
229         if (sbuts->mainb == buttons)
230                 ED_area_tag_redraw(sa);
231 }
232
233 /* reused! */
234 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
235 {
236         SpaceButs *sbuts = sa->spacedata.first;
237
238         /* context changes */
239         switch (wmn->category) {
240                 case NC_SCENE:
241                         switch (wmn->data) {
242                                 case ND_RENDER_OPTIONS:
243                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
244                                         buttons_area_redraw(sa, BCONTEXT_RENDER_LAYER);
245                                         break;
246                                 case ND_FRAME:
247                                         /* any buttons area can have animated properties so redraw all */
248                                         ED_area_tag_redraw(sa);
249                                         sbuts->preview = 1;
250                                         break;
251                                 case ND_OB_ACTIVE:
252                                         ED_area_tag_redraw(sa);
253                                         sbuts->preview = 1;
254                                         break;
255                                 case ND_KEYINGSET:
256                                         buttons_area_redraw(sa, BCONTEXT_SCENE);
257                                         break;
258                                 case ND_RENDER_RESULT:
259                                         break;
260                                 case ND_MODE:
261                                 case ND_LAYER:
262                                 default:
263                                         ED_area_tag_redraw(sa);
264                                         break;
265                         }
266                         break;
267                 case NC_OBJECT:
268                         switch (wmn->data) {
269                                 case ND_TRANSFORM:
270                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
271                                         buttons_area_redraw(sa, BCONTEXT_DATA); /* autotexpace flag */
272                                         break;
273                                 case ND_POSE:
274                                         buttons_area_redraw(sa, BCONTEXT_DATA);
275                                 case ND_BONE_ACTIVE:
276                                 case ND_BONE_SELECT:
277                                         buttons_area_redraw(sa, BCONTEXT_BONE);
278                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
279                                         buttons_area_redraw(sa, BCONTEXT_DATA);
280                                         break;
281                                 case ND_MODIFIER:
282                                         if (wmn->action == NA_RENAME)
283                                                 ED_area_tag_redraw(sa);
284                                         else
285                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
286                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
287                                         break;
288                                 case ND_CONSTRAINT:
289                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
290                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
291                                         break;
292                                 case ND_PARTICLE:
293                                         if (wmn->action == NA_EDITED)
294                                                 buttons_area_redraw(sa, BCONTEXT_PARTICLE);
295                                         sbuts->preview = 1;
296                                         break;
297                                 case ND_DRAW:
298                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
299                                         buttons_area_redraw(sa, BCONTEXT_DATA);
300                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
301                                 case ND_SHADING:
302                                 case ND_SHADING_DRAW:
303                                 case ND_SHADING_LINKS:
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_SHADING_LINKS:
327                                 case ND_NODES:
328                                         /* currently works by redraws... if preview is set, it (re)starts job */
329                                         sbuts->preview = 1;
330                                         break;
331                         }
332                         break;
333                 case NC_WORLD:
334                         buttons_area_redraw(sa, BCONTEXT_WORLD);
335                         sbuts->preview = 1;
336                         break;
337                 case NC_LAMP:
338                         buttons_area_redraw(sa, BCONTEXT_DATA);
339                         sbuts->preview = 1;
340                         break;
341                 case NC_GROUP:
342                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
343                         break;
344                 case NC_BRUSH:
345                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
346                         break;
347                 case NC_TEXTURE:
348                 case NC_IMAGE:
349                         ED_area_tag_redraw(sa);
350                         sbuts->preview = 1;
351                         break;
352                 case NC_SPACE:
353                         if (wmn->data == ND_SPACE_PROPERTIES)
354                                 ED_area_tag_redraw(sa);
355                         break;
356                 case NC_ID:
357                         if (wmn->action == NA_RENAME)
358                                 ED_area_tag_redraw(sa);
359                         break;
360                 case NC_ANIMATION:
361                         switch (wmn->data) {
362                                 case ND_KEYFRAME:
363                                         if (wmn->action == NA_EDITED)
364                                                 ED_area_tag_redraw(sa);
365                                         break;
366                         }
367                         break;
368                 case NC_NODE:
369                         if (wmn->action == NA_SELECTED) {
370                                 ED_area_tag_redraw(sa);
371                                 /* new active node, update texture preview */
372                                 if (sbuts->mainb == BCONTEXT_TEXTURE)
373                                         sbuts->preview = 1;
374                         }
375                         break;
376                 /* Listener for preview render, when doing an global undo. */
377                 case NC_WINDOW:
378                         ED_area_tag_redraw(sa);
379                         sbuts->preview = 1;
380                         break;
381                 case NC_LINESTYLE:
382                         ED_area_tag_redraw(sa);
383                         sbuts->preview= 1;
384                         break;
385         }
386
387         if (wmn->data == ND_KEYS)
388                 ED_area_tag_redraw(sa);
389 }
390
391 /* only called once, from space/spacetypes.c */
392 void ED_spacetype_buttons(void)
393 {
394         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype buttons");
395         ARegionType *art;
396         
397         st->spaceid = SPACE_BUTS;
398         strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
399         
400         st->new = buttons_new;
401         st->free = buttons_free;
402         st->init = buttons_init;
403         st->duplicate = buttons_duplicate;
404         st->operatortypes = buttons_operatortypes;
405         st->keymap = buttons_keymap;
406         st->listener = buttons_area_listener;
407         st->context = buttons_context;
408         
409         /* regions: main window */
410         art = MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
411         art->regionid = RGN_TYPE_WINDOW;
412         art->init = buttons_main_area_init;
413         art->draw = buttons_main_area_draw;
414         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
415         BLI_addhead(&st->regiontypes, art);
416
417         buttons_context_register(art);
418         
419         /* regions: header */
420         art = MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
421         art->regionid = RGN_TYPE_HEADER;
422         art->prefsizey = HEADERY;
423         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
424         
425         art->init = buttons_header_area_init;
426         art->draw = buttons_header_area_draw;
427         BLI_addhead(&st->regiontypes, art);
428
429         BKE_spacetype_register(st);
430 }
431