Particles todo item: particle textures
[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., 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_screen.h"
43
44 #include "BIF_gl.h"
45
46 #include "WM_api.h"
47 #include "WM_types.h"
48
49 #include "UI_resources.h"
50 #include "UI_view2d.h"
51
52 #include "ED_render.h"
53
54 #include "buttons_intern.h"     // own include
55
56 /* ******************** default callbacks for buttons space ***************** */
57
58 static SpaceLink *buttons_new(const bContext *UNUSED(C))
59 {
60         ARegion *ar;
61         SpaceButs *sbuts;
62         
63         sbuts= MEM_callocN(sizeof(SpaceButs), "initbuts");
64         sbuts->spacetype= SPACE_BUTS;
65         sbuts->align= BUT_AUTO;
66
67         /* header */
68         ar= MEM_callocN(sizeof(ARegion), "header for buts");
69         
70         BLI_addtail(&sbuts->regionbase, ar);
71         ar->regiontype= RGN_TYPE_HEADER;
72         ar->alignment= RGN_ALIGN_TOP;
73         
74 #if 0
75         /* context area */
76         ar= MEM_callocN(sizeof(ARegion), "context area for buts");
77         BLI_addtail(&sbuts->regionbase, ar);
78         ar->regiontype= RGN_TYPE_CHANNELS;
79         ar->alignment= RGN_ALIGN_TOP;
80 #endif
81
82         /* main area */
83         ar= MEM_callocN(sizeof(ARegion), "main area for buts");
84         
85         BLI_addtail(&sbuts->regionbase, ar);
86         ar->regiontype= RGN_TYPE_WINDOW;
87         
88         return (SpaceLink *)sbuts;
89 }
90
91 /* not spacelink itself */
92 static void buttons_free(SpaceLink *sl)
93 {       
94         SpaceButs *sbuts= (SpaceButs*) sl;
95         
96         if(sbuts->ri) { 
97                 if (sbuts->ri->rect) MEM_freeN(sbuts->ri->rect);
98                 MEM_freeN(sbuts->ri);
99         }
100
101         if(sbuts->path)
102                 MEM_freeN(sbuts->path);
103 }
104
105 /* spacetype; init callback */
106 static void buttons_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
107 {
108         SpaceButs *sbuts= sa->spacedata.first;
109
110         /* auto-align based on size */
111         if(sbuts->align == BUT_AUTO || !sbuts->align) {
112                 if(sa->winx > sa->winy)
113                         sbuts->align= BUT_HORIZONTAL;
114                 else
115                         sbuts->align= BUT_VERTICAL;
116         }
117 }
118
119 static SpaceLink *buttons_duplicate(SpaceLink *sl)
120 {
121         SpaceButs *sbutsn= MEM_dupallocN(sl);
122         
123         /* clear or remove stuff from old */
124         sbutsn->ri= NULL;
125         sbutsn->path= NULL;
126         
127         return (SpaceLink *)sbutsn;
128 }
129
130 /* add handlers, stuff you only do once or on area/region changes */
131 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
132 {
133         wmKeyMap *keymap;
134
135         ED_region_panels_init(wm, ar);
136
137         keymap= WM_keymap_find(wm->defaultconf, "Property Editor", SPACE_BUTS, 0);
138         WM_event_add_keymap_handler(&ar->handlers, keymap);
139 }
140
141 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
142 {
143         /* draw entirely, view changes should be handled here */
144         SpaceButs *sbuts= CTX_wm_space_buts(C);
145         int vertical= (sbuts->align == BUT_VERTICAL);
146
147         buttons_context_compute(C, sbuts);
148
149         if(sbuts->mainb == BCONTEXT_SCENE)
150                 ED_region_panels(C, ar, vertical, "scene", sbuts->mainb);
151         else if(sbuts->mainb == BCONTEXT_RENDER)
152                 ED_region_panels(C, ar, vertical, "render", sbuts->mainb);
153         else if(sbuts->mainb == BCONTEXT_WORLD)
154                 ED_region_panels(C, ar, vertical, "world", sbuts->mainb);
155         else if(sbuts->mainb == BCONTEXT_OBJECT)
156                 ED_region_panels(C, ar, vertical, "object", sbuts->mainb);
157         else if(sbuts->mainb == BCONTEXT_DATA)
158                 ED_region_panels(C, ar, vertical, "data", sbuts->mainb);
159         else if(sbuts->mainb == BCONTEXT_MATERIAL)
160                 ED_region_panels(C, ar, vertical, "material", sbuts->mainb);
161         else if(sbuts->mainb == BCONTEXT_TEXTURE)
162                 ED_region_panels(C, ar, vertical, "texture", sbuts->mainb);
163         else if(sbuts->mainb == BCONTEXT_PARTICLE)
164                 ED_region_panels(C, ar, vertical, "particle", sbuts->mainb);
165         else if(sbuts->mainb == BCONTEXT_PHYSICS)
166                 ED_region_panels(C, ar, vertical, "physics", sbuts->mainb);
167         else if(sbuts->mainb == BCONTEXT_BONE)
168                 ED_region_panels(C, ar, vertical, "bone", sbuts->mainb);
169         else if(sbuts->mainb == BCONTEXT_MODIFIER)
170                 ED_region_panels(C, ar, vertical, "modifier", sbuts->mainb);
171         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
172                 ED_region_panels(C, ar, vertical, "constraint", sbuts->mainb);
173         else if(sbuts->mainb == BCONTEXT_BONE_CONSTRAINT)
174                 ED_region_panels(C, ar, vertical, "bone_constraint", sbuts->mainb);
175
176         sbuts->re_align= 0;
177         sbuts->mainbo= sbuts->mainb;
178 }
179
180 void buttons_operatortypes(void)
181 {
182         WM_operatortype_append(BUTTONS_OT_toolbox);
183         WM_operatortype_append(BUTTONS_OT_file_browse);
184 }
185
186 void buttons_keymap(struct wmKeyConfig *keyconf)
187 {
188         wmKeyMap *keymap= WM_keymap_find(keyconf, "Property Editor", SPACE_BUTS, 0);
189         
190         WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
191 }
192
193 /* add handlers, stuff you only do once or on area/region changes */
194 static void buttons_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
195 {
196         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
197 }
198
199 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
200 {
201         /* clear */
202         UI_ThemeClearColor(ED_screen_area_active(C)?TH_HEADER:TH_HEADERDESEL);
203         glClear(GL_COLOR_BUFFER_BIT);
204         
205         /* set view2d view matrix for scrolling (without scrollers) */
206         UI_view2d_view_ortho(&ar->v2d);
207         
208         buttons_header_buttons(C, ar);
209
210         /* restore view matrix? */
211         UI_view2d_view_restore(C);
212 }
213
214 /* draw a certain button set only if properties area is currently
215  * showing that button set, to reduce unnecessary drawing. */
216 static void buttons_area_redraw(ScrArea *sa, short buttons)
217 {
218         SpaceButs *sbuts= sa->spacedata.first;
219         
220         /* if the area's current button set is equal to the one to redraw */
221         if(sbuts->mainb == buttons)
222                 ED_area_tag_redraw(sa);
223 }
224
225 /* reused! */
226 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
227 {
228         SpaceButs *sbuts= sa->spacedata.first;
229
230         /* context changes */
231         switch(wmn->category) {
232                 case NC_SCENE:
233                         switch(wmn->data) {
234                                 case ND_RENDER_OPTIONS:
235                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
236                                         break;
237                                 case ND_FRAME:
238                                         /* any buttons area can have animated properties so redraw all */
239                                         ED_area_tag_redraw(sa);
240                                         sbuts->preview= 1;
241                                         break;
242                                 case ND_OB_ACTIVE:
243                                         ED_area_tag_redraw(sa);
244                                         sbuts->preview= 1;
245                                         break;
246                                 case ND_KEYINGSET:
247                                         buttons_area_redraw(sa, BCONTEXT_SCENE);
248                                         break;
249                                 case ND_RENDER_RESULT:
250                                         break;
251                                 case ND_MODE:
252                                 case ND_LAYER:
253                                 default:
254                                         ED_area_tag_redraw(sa);
255                                         break;
256                         }
257                         break;
258                 case NC_OBJECT:
259                         switch(wmn->data) {
260                                 case ND_TRANSFORM:
261                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
262                                         buttons_area_redraw(sa, BCONTEXT_DATA); /* autotexpace flag */
263                                         break;
264                                 case ND_POSE:
265                                 case ND_BONE_ACTIVE:
266                                 case ND_BONE_SELECT:
267                                         buttons_area_redraw(sa, BCONTEXT_BONE);
268                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
269                                         break;
270                                 case ND_MODIFIER:
271                                         if(wmn->action == NA_RENAME)
272                                                 ED_area_tag_redraw(sa);
273                                         else
274                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
275                                                 buttons_area_redraw(sa, BCONTEXT_PHYSICS);
276                                         break;
277                                 case ND_CONSTRAINT:
278                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
279                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
280                                         break;
281                                 case ND_PARTICLE:
282                                         if (wmn->action == NA_EDITED)
283                                                 buttons_area_redraw(sa, BCONTEXT_PARTICLE);
284                                         sbuts->preview= 1;
285                                         break;
286                                 case ND_DRAW:
287                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
288                                         buttons_area_redraw(sa, BCONTEXT_DATA);
289                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
290                                 case ND_SHADING:
291                                 case ND_SHADING_DRAW:
292                                         /* currently works by redraws... if preview is set, it (re)starts job */
293                                         sbuts->preview= 1;
294                                         break;
295                                 default:
296                                         /* Not all object RNA props have a ND_ notifier (yet) */
297                                         ED_area_tag_redraw(sa);
298                                         break;
299                         }
300                         break;
301                 case NC_GEOM:
302                         switch(wmn->data) {
303                                 case ND_SELECT:
304                                 case ND_DATA:
305                                         ED_area_tag_redraw(sa);
306                                         break;
307                         }
308                         break;
309                 case NC_MATERIAL:
310                         ED_area_tag_redraw(sa);
311                         switch(wmn->data) {
312                                 case ND_SHADING:
313                                 case ND_SHADING_DRAW:
314                                 case ND_NODES:
315                                         /* currently works by redraws... if preview is set, it (re)starts job */
316                                         sbuts->preview= 1;
317                                         break;
318                         }                                       
319                         break;
320                 case NC_WORLD:
321                         buttons_area_redraw(sa, BCONTEXT_WORLD);
322                         sbuts->preview= 1;
323                         break;
324                 case NC_LAMP:
325                         buttons_area_redraw(sa, BCONTEXT_DATA);
326                         sbuts->preview= 1;
327                         break;
328                 case NC_BRUSH:
329                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
330                         break;
331                 case NC_TEXTURE:
332                 case NC_IMAGE:
333                         ED_area_tag_redraw(sa);
334                         sbuts->preview= 1;
335                         break;
336                 case NC_SPACE:
337                         if(wmn->data == ND_SPACE_PROPERTIES)
338                                 ED_area_tag_redraw(sa);
339                         break;
340                 case NC_ID:
341                         if(wmn->action == NA_RENAME)
342                                 ED_area_tag_redraw(sa);
343                         break;
344                 case NC_ANIMATION:
345                         switch(wmn->data) {
346                                 case ND_KEYFRAME:
347                                         if (wmn->action == NA_EDITED)
348                                                 ED_area_tag_redraw(sa);
349                                         break;
350                         }
351                         break;
352                 case NC_NODE:
353                         if(wmn->action==NA_SELECTED)
354                                 ED_area_tag_redraw(sa);
355                         break;
356                 /* Listener for preview render, when doing an global undo. */
357                 case NC_WINDOW:
358                         ED_area_tag_redraw(sa);
359                         sbuts->preview= 1;
360                         break;
361         }
362
363         if(wmn->data == ND_KEYS)
364                 ED_area_tag_redraw(sa);
365 }
366
367 /* only called once, from space/spacetypes.c */
368 void ED_spacetype_buttons(void)
369 {
370         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
371         ARegionType *art;
372         
373         st->spaceid= SPACE_BUTS;
374         strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
375         
376         st->new= buttons_new;
377         st->free= buttons_free;
378         st->init= buttons_init;
379         st->duplicate= buttons_duplicate;
380         st->operatortypes= buttons_operatortypes;
381         st->keymap= buttons_keymap;
382         st->listener= buttons_area_listener;
383         st->context= buttons_context;
384         
385         /* regions: main window */
386         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
387         art->regionid = RGN_TYPE_WINDOW;
388         art->init= buttons_main_area_init;
389         art->draw= buttons_main_area_draw;
390         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
391         BLI_addhead(&st->regiontypes, art);
392
393         buttons_context_register(art);
394         
395         /* regions: header */
396         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
397         art->regionid = RGN_TYPE_HEADER;
398         art->prefsizey= BUTS_HEADERY;
399         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
400         
401         art->init= buttons_header_area_init;
402         art->draw= buttons_header_area_draw;
403         BLI_addhead(&st->regiontypes, art);
404
405         BKE_spacetype_register(st);
406 }
407