Merge from trunk -r 23000:23968.
[blender-staging.git] / source / blender / editors / space_view3d / space_view3d.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_action_types.h"
33 #include "DNA_armature_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_view3d_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_arithb.h"
44 #include "BLI_rand.h"
45
46 #include "BKE_action.h"
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_screen.h"
50 #include "BKE_utildefines.h"
51
52 #include "ED_armature.h"
53 #include "ED_space_api.h"
54 #include "ED_screen.h"
55 #include "ED_object.h"
56
57 #include "BIF_gl.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "UI_interface.h"
63 #include "UI_resources.h"
64 #include "UI_view2d.h"
65
66 #include "RNA_access.h"
67
68 #include "view3d_intern.h"      // own include
69
70 /* ******************** manage regions ********************* */
71
72 ARegion *view3d_has_buttons_region(ScrArea *sa)
73 {
74         ARegion *ar, *arnew;
75         
76         for(ar= sa->regionbase.first; ar; ar= ar->next)
77                 if(ar->regiontype==RGN_TYPE_UI)
78                         return ar;
79         
80         /* add subdiv level; after header */
81         for(ar= sa->regionbase.first; ar; ar= ar->next)
82                 if(ar->regiontype==RGN_TYPE_HEADER)
83                         break;
84         
85         /* is error! */
86         if(ar==NULL) return NULL;
87         
88         arnew= MEM_callocN(sizeof(ARegion), "buttons for view3d");
89         
90         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
91         arnew->regiontype= RGN_TYPE_UI;
92         arnew->alignment= RGN_ALIGN_RIGHT;
93         
94         arnew->flag = RGN_FLAG_HIDDEN;
95         
96         return arnew;
97 }
98
99 ARegion *view3d_has_tools_region(ScrArea *sa)
100 {
101         ARegion *ar, *artool=NULL, *arprops=NULL, *arhead;
102         
103         for(ar= sa->regionbase.first; ar; ar= ar->next) {
104                 if(ar->regiontype==RGN_TYPE_TOOLS)
105                         artool= ar;
106                 if(ar->regiontype==RGN_TYPE_TOOL_PROPS)
107                         arprops= ar;
108         }
109         
110         /* tool region hide/unhide also hides props */
111         if(arprops && artool) return artool;
112         
113         if(artool==NULL) {
114                 /* add subdiv level; after header */
115                 for(arhead= sa->regionbase.first; arhead; arhead= arhead->next)
116                         if(arhead->regiontype==RGN_TYPE_HEADER)
117                                 break;
118                 
119                 /* is error! */
120                 if(arhead==NULL) return NULL;
121                 
122                 artool= MEM_callocN(sizeof(ARegion), "tools for view3d");
123                 
124                 BLI_insertlinkafter(&sa->regionbase, arhead, artool);
125                 artool->regiontype= RGN_TYPE_TOOLS;
126                 artool->alignment= RGN_ALIGN_LEFT; //RGN_OVERLAP_LEFT;
127                 artool->flag = RGN_FLAG_HIDDEN;
128         }
129
130         if(arprops==NULL) {
131                 /* add extra subdivided region for tool properties */
132                 arprops= MEM_callocN(sizeof(ARegion), "tool props for view3d");
133                 
134                 BLI_insertlinkafter(&sa->regionbase, artool, arprops);
135                 arprops->regiontype= RGN_TYPE_TOOL_PROPS;
136                 arprops->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
137         }
138         
139         return artool;
140 }
141
142 /* ****************************************************** */
143
144 /* function to always find a regionview3d context inside 3D window */
145 RegionView3D *ED_view3d_context_rv3d(bContext *C)
146 {
147         RegionView3D *rv3d= CTX_wm_region_view3d(C);
148         
149         if(rv3d==NULL) {
150                 ScrArea *sa =CTX_wm_area(C);
151                 if(sa->spacetype==SPACE_VIEW3D) {
152                         ARegion *ar;
153                         for(ar= sa->regionbase.first; ar; ar= ar->next)
154                                 if(ar->regiontype==RGN_TYPE_WINDOW)
155                                         return ar->regiondata;
156                 }
157         }
158         return rv3d;
159 }
160
161 /* Most of the time this isn't needed since you could assume the view matrix was
162  * set while drawing, however when functions like mesh_foreachScreenVert are
163  * called by selection tools, we can't be sure this object was the last.
164  *
165  * for example, transparent objects are drawn after editmode and will cause
166  * the rv3d mat's to change and break selection.
167  *
168  * 'ED_view3d_init_mats_rv3d' should be called before
169  * view3d_project_short_clip and view3d_project_short_noclip in cases where
170  * these functions are not used during draw_object
171  */
172 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d)
173 {
174         wmMultMatrix(ob->obmat);
175         /* local viewmat and persmat, to calculate projections */
176         wmGetMatrix(rv3d->viewmatob);
177         wmGetSingleMatrix(rv3d->persmatob);
178 }
179
180 /* ******************** default callbacks for view3d space ***************** */
181
182 static SpaceLink *view3d_new(const bContext *C)
183 {
184         Scene *scene= CTX_data_scene(C);
185         ARegion *ar;
186         View3D *v3d;
187         RegionView3D *rv3d;
188         
189         v3d= MEM_callocN(sizeof(View3D), "initview3d");
190         v3d->spacetype= SPACE_VIEW3D;
191         v3d->blockscale= 0.7f;
192         v3d->lay= v3d->layact= 1;
193         if(scene) {
194                 v3d->lay= v3d->layact= scene->lay;
195                 v3d->camera= scene->camera;
196         }
197         v3d->scenelock= 1;
198         v3d->grid= 1.0f;
199         v3d->gridlines= 16;
200         v3d->gridsubdiv = 10;
201         v3d->drawtype= OB_WIRE;
202         
203         v3d->gridflag |= V3D_SHOW_X;
204         v3d->gridflag |= V3D_SHOW_Y;
205         v3d->gridflag |= V3D_SHOW_FLOOR;
206         v3d->gridflag &= ~V3D_SHOW_Z;
207         
208         v3d->lens= 35.0f;
209         v3d->near= 0.01f;
210         v3d->far= 500.0f;
211
212         v3d->twtype= V3D_MANIP_TRANSLATE;
213         
214         /* header */
215         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
216         
217         BLI_addtail(&v3d->regionbase, ar);
218         ar->regiontype= RGN_TYPE_HEADER;
219         ar->alignment= RGN_ALIGN_BOTTOM;
220         
221         /* toolbar */
222         ar= MEM_callocN(sizeof(ARegion), "toolbar for view3d");
223         
224         BLI_addtail(&v3d->regionbase, ar);
225         ar->regiontype= RGN_TYPE_UI;
226         ar->alignment= RGN_ALIGN_LEFT;
227         ar->flag = RGN_FLAG_HIDDEN;
228         
229         /* tool properties */
230         ar= MEM_callocN(sizeof(ARegion), "tool properties for view3d");
231         
232         BLI_addtail(&v3d->regionbase, ar);
233         ar->regiontype= RGN_TYPE_UI;
234         ar->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
235         ar->flag = RGN_FLAG_HIDDEN;
236         
237         /* buttons/list view */
238         ar= MEM_callocN(sizeof(ARegion), "buttons for view3d");
239         
240         BLI_addtail(&v3d->regionbase, ar);
241         ar->regiontype= RGN_TYPE_UI;
242         ar->alignment= RGN_ALIGN_RIGHT;
243         ar->flag = RGN_FLAG_HIDDEN;
244         
245         /* main area */
246         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
247         
248         BLI_addtail(&v3d->regionbase, ar);
249         ar->regiontype= RGN_TYPE_WINDOW;
250         
251         ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region view3d");
252         rv3d= ar->regiondata;
253         rv3d->viewquat[0]= 1.0f;
254         rv3d->persp= 1;
255         rv3d->view= 7;
256         rv3d->dist= 10.0;
257         
258         return (SpaceLink *)v3d;
259 }
260
261 /* not spacelink itself */
262 static void view3d_free(SpaceLink *sl)
263 {
264         View3D *vd= (View3D *) sl;
265         
266         if(vd->bgpic) {
267                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
268                 MEM_freeN(vd->bgpic);
269         }
270         
271         if(vd->localvd) MEM_freeN(vd->localvd);
272         
273         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
274         
275 }
276
277
278 /* spacetype; init callback */
279 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
280 {
281
282 }
283
284 static SpaceLink *view3d_duplicate(SpaceLink *sl)
285 {
286         View3D *v3do= (View3D *)sl;
287         View3D *v3dn= MEM_dupallocN(sl);
288         
289         /* clear or remove stuff from old */
290         
291 // XXX  BIF_view3d_previewrender_free(v3do);
292         
293         if(v3do->localvd) {
294                 v3do->localvd= NULL;
295                 v3do->properties_storage= NULL;
296                 v3do->lay= v3dn->localvd->lay;
297                 v3do->lay &= 0xFFFFFF;
298         }
299         
300         /* copy or clear inside new stuff */
301
302         if(v3dn->bgpic) {
303                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
304                 if(v3dn->bgpic->ima) v3dn->bgpic->ima->id.us++;
305         }
306         v3dn->properties_storage= NULL;
307         
308         return (SpaceLink *)v3dn;
309 }
310
311 /* add handlers, stuff you only do once or on area/region changes */
312 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
313 {
314         wmKeyMap *keymap;
315
316         /* object ops. */
317         
318         /* pose is not modal, operator poll checks for this */
319         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
320         WM_event_add_keymap_handler(&ar->handlers, keymap);
321         
322         keymap= WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
323         WM_event_add_keymap_handler(&ar->handlers, keymap);
324
325         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
326         WM_event_add_keymap_handler(&ar->handlers, keymap);
327
328         keymap= WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
329         WM_event_add_keymap_handler(&ar->handlers, keymap);
330
331         keymap= WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
332         WM_event_add_keymap_handler(&ar->handlers, keymap);
333
334         keymap= WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
335         WM_event_add_keymap_handler(&ar->handlers, keymap);
336         
337         keymap= WM_keymap_find(wm->defaultconf, "EditMesh", 0, 0);
338         WM_event_add_keymap_handler(&ar->handlers, keymap);
339         
340         keymap= WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
341         WM_event_add_keymap_handler(&ar->handlers, keymap);
342         
343         keymap= WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
344         WM_event_add_keymap_handler(&ar->handlers, keymap);
345
346         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
347         WM_event_add_keymap_handler(&ar->handlers, keymap);
348
349         keymap= WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
350         WM_event_add_keymap_handler(&ar->handlers, keymap);
351         
352         keymap= WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
353         WM_event_add_keymap_handler(&ar->handlers, keymap);
354
355         /* armature sketching needs to take over mouse */
356         keymap= WM_keymap_find(wm->defaultconf, "Armature_Sketch", 0, 0);
357         WM_event_add_keymap_handler(&ar->handlers, keymap);
358
359         keymap= WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
360         WM_event_add_keymap_handler(&ar->handlers, keymap);
361
362         /* editfont keymap swallows all... */
363         keymap= WM_keymap_find(wm->defaultconf, "Font", 0, 0);
364         WM_event_add_keymap_handler(&ar->handlers, keymap);
365
366         keymap= WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
367         WM_event_add_keymap_handler(&ar->handlers, keymap);
368
369         /* own keymap, last so modes can override it */
370         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
371         WM_event_add_keymap_handler(&ar->handlers, keymap);
372
373         keymap= WM_keymap_find(wm->defaultconf, "View3D", SPACE_VIEW3D, 0);
374         WM_event_add_keymap_handler(&ar->handlers, keymap);
375 }
376
377 /* type callback, not region itself */
378 static void view3d_main_area_free(ARegion *ar)
379 {
380         RegionView3D *rv3d= ar->regiondata;
381         
382         if(rv3d) {
383                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
384                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
385
386                 // XXX  retopo_free_view_data(rv3d);
387                 if(rv3d->ri) { 
388                         // XXX          BIF_view3d_previewrender_free(rv3d);
389                 }
390                 
391                 if(rv3d->depths) {
392                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
393                         MEM_freeN(rv3d->depths);
394                 }
395                 MEM_freeN(rv3d);
396                 ar->regiondata= NULL;
397         }
398 }
399
400 /* copy regiondata */
401 static void *view3d_main_area_duplicate(void *poin)
402 {
403         if(poin) {
404                 RegionView3D *rv3d= poin, *new;
405         
406                 new= MEM_dupallocN(rv3d);
407                 if(rv3d->localvd) 
408                         new->localvd= MEM_dupallocN(rv3d->localvd);
409                 if(rv3d->clipbb) 
410                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
411                 
412                 new->depths= NULL;
413                 new->retopo_view_data= NULL;
414                 new->ri= NULL;
415                 new->gpd= NULL;
416                 new->sms= NULL;
417                 new->smooth_timer= NULL;
418                 
419                 return new;
420         }
421         return NULL;
422 }
423
424 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
425 {
426         /* context changes */
427         switch(wmn->category) {
428                 case NC_ANIMATION:
429                         switch(wmn->data) {
430                                 case ND_KEYFRAME_EDIT:
431                                 case ND_KEYFRAME_PROP:
432                                 case ND_NLA_EDIT:
433                                 case ND_NLA_ACTCHANGE:
434                                 case ND_ANIMCHAN_SELECT:
435                                         ED_region_tag_redraw(ar);
436                                         break;
437                         }
438                         break;
439                 case NC_SCENE:
440                         switch(wmn->data) {
441                                 case ND_TRANSFORM:
442                                 case ND_FRAME:
443                                 case ND_OB_ACTIVE:
444                                 case ND_OB_SELECT:
445                                         ED_region_tag_redraw(ar);
446                                         break;
447                                 case ND_MODE:
448                                         ED_region_tag_redraw(ar);
449                                         break;
450                         }
451                         break;
452                 case NC_OBJECT:
453                         switch(wmn->data) {
454                                 case ND_BONE_ACTIVE:
455                                 case ND_BONE_SELECT:
456                                 case ND_TRANSFORM:
457                                 case ND_POSE:
458                                 case ND_DRAW:
459                                 case ND_MODIFIER:
460                                 case ND_CONSTRAINT:
461                                 case ND_KEYS:
462                                 case ND_PARTICLE_SELECT:
463                                 case ND_PARTICLE_DATA:
464                                         ED_region_tag_redraw(ar);
465                                         break;
466                         }
467                         break;
468                 case NC_GEOM:
469                         switch(wmn->data) {
470                                 case ND_DATA:
471                                 case ND_SELECT:
472                                         ED_region_tag_redraw(ar);
473                                         break;
474                         }
475                         break;
476                 case NC_GROUP:
477                         /* all group ops for now */
478                         ED_region_tag_redraw(ar);
479                         break;
480                 case NC_MATERIAL:
481                         switch(wmn->data) {
482                                 case ND_SHADING_DRAW:
483                                         ED_region_tag_redraw(ar);
484                                         break;
485                         }
486                         break;
487                 case NC_WORLD:
488                         switch(wmn->data) {
489                                 case ND_WORLD_DRAW:
490                                         ED_region_tag_redraw(ar);
491                                         break;
492                         }
493                         break;
494                 case NC_LAMP:
495                         switch(wmn->data) {
496                                 case ND_LIGHTING_DRAW:
497                                         ED_region_tag_redraw(ar);
498                                         break;
499                         }
500                         break;
501                 case NC_IMAGE:  
502                         /* this could be more fine grained checks if we had
503                          * more context than just the region */
504                         ED_region_tag_redraw(ar);
505                         break;
506                 case NC_SPACE:
507                         if(wmn->data == ND_SPACE_VIEW3D)
508                                 ED_region_tag_redraw(ar);
509                         break;
510         }
511 }
512
513 /* concept is to retrieve cursor type context-less */
514 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
515 {
516         Scene *scene= win->screen->scene;
517
518         if(scene->obedit) {
519                 WM_cursor_set(win, CURSOR_EDIT);
520         }
521         else {
522                 WM_cursor_set(win, CURSOR_STD);
523         }
524 }
525
526 /* add handlers, stuff you only do once or on area/region changes */
527 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
528 {
529         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
530         
531         WM_event_add_keymap_handler(&ar->handlers, keymap);
532
533         ED_region_header_init(ar);
534 }
535
536 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
537 {
538         ED_region_header(C, ar);
539 }
540
541 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
542 {
543         /* context changes */
544         switch(wmn->category) {
545                 case NC_SCENE:
546                         switch(wmn->data) {
547                                 case ND_FRAME:
548                                 case ND_OB_ACTIVE:
549                                 case ND_OB_SELECT:
550                                 case ND_MODE:
551                                         ED_region_tag_redraw(ar);
552                                         break;
553                         }
554                         break;
555                 case NC_SPACE:
556                         if(wmn->data == ND_SPACE_VIEW3D)
557                                 ED_region_tag_redraw(ar);
558                         break;
559         }
560 }
561
562 /* add handlers, stuff you only do once or on area/region changes */
563 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
564 {
565         wmKeyMap *keymap;
566
567         ED_region_panels_init(wm, ar);
568         
569         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
570         WM_event_add_keymap_handler(&ar->handlers, keymap);
571 }
572
573 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
574 {
575         ED_region_panels(C, ar, 1, NULL, -1);
576 }
577
578 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
579 {
580         /* context changes */
581         switch(wmn->category) {
582                 case NC_ANIMATION:
583                         switch(wmn->data) {
584                                 case ND_KEYFRAME_EDIT:
585                                 case ND_KEYFRAME_PROP:
586                                 case ND_NLA_EDIT:
587                                 case ND_NLA_ACTCHANGE:
588                                         ED_region_tag_redraw(ar);
589                                         break;
590                         }
591                         break;
592                 case NC_SCENE:
593                         switch(wmn->data) {
594                                 case ND_FRAME:
595                                 case ND_OB_ACTIVE:
596                                 case ND_OB_SELECT:
597                                 case ND_MODE:
598                                         ED_region_tag_redraw(ar);
599                                         break;
600                         }
601                         break;
602                 case NC_OBJECT:
603                         switch(wmn->data) {
604                                 case ND_BONE_ACTIVE:
605                                 case ND_BONE_SELECT:
606                                 case ND_TRANSFORM:
607                                 case ND_POSE:
608                                 case ND_DRAW:
609                                 case ND_KEYS:
610                                         ED_region_tag_redraw(ar);
611                                         break;
612                         }
613                         break;
614                 case NC_GEOM:
615                         switch(wmn->data) {
616                                 case ND_DATA:
617                                 case ND_SELECT:
618                                         ED_region_tag_redraw(ar);
619                                         break;
620                         }
621                         break;
622                 case NC_BRUSH:
623                         if(wmn->action==NA_EDITED)
624                                 ED_region_tag_redraw(ar);
625                         break;
626                 case NC_SPACE:
627                         if(wmn->data == ND_SPACE_VIEW3D)
628                                 ED_region_tag_redraw(ar);
629                         break;
630         }
631 }
632
633 /* add handlers, stuff you only do once or on area/region changes */
634 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
635 {
636         wmKeyMap *keymap;
637         
638         ED_region_panels_init(wm, ar);
639
640         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
641         WM_event_add_keymap_handler(&ar->handlers, keymap);
642 }
643
644 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
645 {
646         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
647 }
648
649 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
650 {
651         View3D *v3d= CTX_wm_view3d(C);
652         Scene *scene= CTX_data_scene(C);
653         Base *base;
654         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
655
656         if(CTX_data_dir(member)) {
657                 static const char *dir[] = {
658                         "selected_objects", "selected_bases", "selected_editable_objects",
659                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
660                         "active_base", "active_object", "visible_bones", "editable_bones",
661                         "selected_bones", "selected_editable_bones", "visible_pchans",
662                         "selected_pchans", "active_bone", "active_pchan", NULL};
663
664                 CTX_data_dir_set(result, dir);
665         }
666         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
667                 int selected_objects= CTX_data_equals(member, "selected_objects");
668
669                 for(base=scene->base.first; base; base=base->next) {
670                         if((base->flag & SELECT) && (base->lay & lay)) {
671                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
672                                         if(selected_objects)
673                                                 CTX_data_id_list_add(result, &base->object->id);
674                                         else
675                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
676                                 }
677                         }
678                 }
679
680                 return 1;
681         }
682         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
683                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
684
685                 for(base=scene->base.first; base; base=base->next) {
686                         if((base->flag & SELECT) && (base->lay & lay)) {
687                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
688                                         if(0==object_is_libdata(base->object)) {
689                                                 if(selected_editable_objects)
690                                                         CTX_data_id_list_add(result, &base->object->id);
691                                                 else
692                                                         CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
693                                         }
694                                 }
695                         }
696                 }
697                 
698                 return 1;
699         }
700         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
701                 int visible_objects= CTX_data_equals(member, "visible_objects");
702
703                 for(base=scene->base.first; base; base=base->next) {
704                         if(base->lay & lay) {
705                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
706                                         if(visible_objects)
707                                                 CTX_data_id_list_add(result, &base->object->id);
708                                         else
709                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
710                                 }
711                         }
712                 }
713                 
714                 return 1;
715         }
716         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
717                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
718
719                 for(base=scene->base.first; base; base=base->next) {
720                         if(base->lay & lay) {
721                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
722                                         if(selectable_objects)
723                                                 CTX_data_id_list_add(result, &base->object->id);
724                                         else
725                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
726                                 }
727                         }
728                 }
729                 
730                 return 1;
731         }
732         else if(CTX_data_equals(member, "active_base")) {
733                 if(scene->basact && (scene->basact->lay & lay))
734                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
735                                 CTX_data_pointer_set(result, &scene->id, &RNA_UnknownType, scene->basact);
736                 
737                 return 1;
738         }
739         else if(CTX_data_equals(member, "active_object")) {
740                 if(scene->basact && (scene->basact->lay & lay))
741                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
742                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
743                 
744                 return 1;
745         }
746         else if(CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
747                 Object *obedit= scene->obedit; // XXX get from context?
748                 bArmature *arm= (obedit) ? obedit->data : NULL;
749                 EditBone *ebone, *flipbone=NULL;
750                 int editable_bones= CTX_data_equals(member, "editable_bones");
751                 
752                 if (arm && arm->edbo) {
753                         /* Attention: X-Axis Mirroring is also handled here... */
754                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
755                                 /* first and foremost, bone must be visible and selected */
756                                 if (EBONE_VISIBLE(arm, ebone)) {
757                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
758                                          * so that most users of this data don't need to explicitly check for it themselves.
759                                          * 
760                                          * We need to make sure that these mirrored copies are not selected, otherwise some
761                                          * bones will be operated on twice.
762                                          */
763                                         if (arm->flag & ARM_MIRROR_EDIT)
764                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
765                                         
766                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
767                                         if (editable_bones) {
768                                                 /* only selected + editable */
769                                                 if (EBONE_EDITABLE(ebone)) {
770                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
771                                                 
772                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
773                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
774                                                 }
775                                         }
776                                         else {
777                                                 /* only include bones if visible */
778                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
779                                                 
780                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone)==0)
781                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
782                                         }
783                                 }
784                         }       
785                         
786                         return 1;
787                 }
788         }
789         else if(CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
790                 Object *obedit= scene->obedit; // XXX get from context?
791                 bArmature *arm= (obedit) ? obedit->data : NULL;
792                 EditBone *ebone, *flipbone=NULL;
793                 int selected_editable_bones= CTX_data_equals(member, "selected_editable_bones");
794                 
795                 if (arm && arm->edbo) {
796                         /* Attention: X-Axis Mirroring is also handled here... */
797                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
798                                 /* first and foremost, bone must be visible and selected */
799                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
800                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
801                                          * so that most users of this data don't need to explicitly check for it themselves.
802                                          * 
803                                          * We need to make sure that these mirrored copies are not selected, otherwise some
804                                          * bones will be operated on twice.
805                                          */
806                                         if (arm->flag & ARM_MIRROR_EDIT)
807                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
808                                         
809                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
810                                         if (selected_editable_bones) {
811                                                 /* only selected + editable */
812                                                 if (EBONE_EDITABLE(ebone)) {
813                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
814                                                 
815                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
816                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
817                                                 }
818                                         }
819                                         else {
820                                                 /* only include bones if selected */
821                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
822                                                 
823                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
824                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
825                                         }
826                                 }
827                         }       
828                         
829                         return 1;
830                 }
831         }
832         else if(CTX_data_equals(member, "visible_pchans")) {
833                 Object *obact= OBACT;
834                 bArmature *arm= (obact) ? obact->data : NULL;
835                 bPoseChannel *pchan;
836                 
837                 if (obact && arm) {
838                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
839                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
840                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
841                                         CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
842                                 }
843                         }
844                         
845                         return 1;
846                 }
847         }
848         else if(CTX_data_equals(member, "selected_pchans")) {
849                 Object *obact= OBACT;
850                 bArmature *arm= (obact) ? obact->data : NULL;
851                 bPoseChannel *pchan;
852                 
853                 if (obact && arm) {
854                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
855                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
856                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
857                                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) 
858                                                 CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
859                                 }
860                         }
861                         
862                         return 1;
863                 }
864         }
865         else if(CTX_data_equals(member, "active_bone")) {
866                 Object *obedit= scene->obedit; // XXX get from context?
867                 bArmature *arm= (obedit) ? obedit->data : NULL;
868                 EditBone *ebone;
869                 
870                 if (arm && arm->edbo) {
871                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
872                                 if (EBONE_VISIBLE(arm, ebone)) {
873                                         if (ebone->flag & BONE_ACTIVE) {
874                                                 CTX_data_pointer_set(result, &arm->id, &RNA_UnknownType, ebone);
875                                                 
876                                                 return 1;
877                                         }
878                                 }
879                         }
880                 }
881                 
882         }
883         else if(CTX_data_equals(member, "active_pchan")) {
884                 Object *obact= OBACT;
885                 bPoseChannel *pchan;
886                 
887                 pchan= get_active_posechannel(obact);
888                 if (pchan) {
889                         CTX_data_pointer_set(result, &obact->id, &RNA_PoseChannel, pchan);
890                         return 1;
891                 }
892         }
893
894         return 0;
895 }
896
897 /* only called once, from space/spacetypes.c */
898 void ED_spacetype_view3d(void)
899 {
900         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
901         ARegionType *art;
902         
903         st->spaceid= SPACE_VIEW3D;
904         
905         st->new= view3d_new;
906         st->free= view3d_free;
907         st->init= view3d_init;
908         st->duplicate= view3d_duplicate;
909         st->operatortypes= view3d_operatortypes;
910         st->keymap= view3d_keymap;
911         st->context= view3d_context;
912         
913         /* regions: main window */
914         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
915         art->regionid = RGN_TYPE_WINDOW;
916         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
917         art->draw= view3d_main_area_draw;
918         art->init= view3d_main_area_init;
919         art->free= view3d_main_area_free;
920         art->duplicate= view3d_main_area_duplicate;
921         art->listener= view3d_main_area_listener;
922         art->cursor= view3d_main_area_cursor;
923         BLI_addhead(&st->regiontypes, art);
924         
925         /* regions: listview/buttons */
926         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
927         art->regionid = RGN_TYPE_UI;
928         art->minsizex= 180; // XXX
929         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
930         art->listener= view3d_buttons_area_listener;
931         art->init= view3d_buttons_area_init;
932         art->draw= view3d_buttons_area_draw;
933         BLI_addhead(&st->regiontypes, art);
934
935         view3d_buttons_register(art);
936
937         /* regions: tool(bar) */
938         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
939         art->regionid = RGN_TYPE_TOOLS;
940         art->minsizex= 160; // XXX
941         art->minsizey= 50; // XXX
942         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
943         art->listener= view3d_buttons_area_listener;
944         art->init= view3d_tools_area_init;
945         art->draw= view3d_tools_area_draw;
946         BLI_addhead(&st->regiontypes, art);
947         
948         view3d_toolbar_register(art);
949
950         /* regions: tool properties */
951         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
952         art->regionid = RGN_TYPE_TOOL_PROPS;
953         art->minsizex= 0;
954         art->minsizey= 120;
955         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
956         art->listener= view3d_buttons_area_listener;
957         art->init= view3d_tools_area_init;
958         art->draw= view3d_tools_area_draw;
959         BLI_addhead(&st->regiontypes, art);
960         
961         view3d_tool_props_register(art);
962         
963         
964         /* regions: header */
965         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
966         art->regionid = RGN_TYPE_HEADER;
967         art->minsizey= HEADERY;
968         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
969         art->listener= view3d_header_area_listener;
970         art->init= view3d_header_area_init;
971         art->draw= view3d_header_area_draw;
972         BLI_addhead(&st->regiontypes, art);
973         
974         BKE_spacetype_register(st);
975 }
976