svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22875:22935
[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_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
162 /* ******************** default callbacks for view3d space ***************** */
163
164 static SpaceLink *view3d_new(const bContext *C)
165 {
166         Scene *scene= CTX_data_scene(C);
167         ARegion *ar;
168         View3D *v3d;
169         RegionView3D *rv3d;
170         
171         v3d= MEM_callocN(sizeof(View3D), "initview3d");
172         v3d->spacetype= SPACE_VIEW3D;
173         v3d->blockscale= 0.7f;
174         v3d->lay= v3d->layact= 1;
175         if(scene) {
176                 v3d->lay= v3d->layact= scene->lay;
177                 v3d->camera= scene->camera;
178         }
179         v3d->scenelock= 1;
180         v3d->grid= 1.0f;
181         v3d->gridlines= 16;
182         v3d->gridsubdiv = 10;
183         v3d->drawtype= OB_WIRE;
184         
185         v3d->gridflag |= V3D_SHOW_X;
186         v3d->gridflag |= V3D_SHOW_Y;
187         v3d->gridflag |= V3D_SHOW_FLOOR;
188         v3d->gridflag &= ~V3D_SHOW_Z;
189         
190         v3d->lens= 35.0f;
191         v3d->near= 0.01f;
192         v3d->far= 500.0f;
193         
194         /* header */
195         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
196         
197         BLI_addtail(&v3d->regionbase, ar);
198         ar->regiontype= RGN_TYPE_HEADER;
199         ar->alignment= RGN_ALIGN_BOTTOM;
200         
201         /* buttons/list view */
202         ar= MEM_callocN(sizeof(ARegion), "buttons for view3d");
203         
204         BLI_addtail(&v3d->regionbase, ar);
205         ar->regiontype= RGN_TYPE_UI;
206         ar->alignment= RGN_ALIGN_LEFT;
207         ar->flag = RGN_FLAG_HIDDEN;
208         
209         /* main area */
210         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
211         
212         BLI_addtail(&v3d->regionbase, ar);
213         ar->regiontype= RGN_TYPE_WINDOW;
214         
215         ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region view3d");
216         rv3d= ar->regiondata;
217         rv3d->viewquat[0]= 1.0f;
218         rv3d->persp= 1;
219         rv3d->view= 7;
220         rv3d->dist= 10.0;
221         
222         return (SpaceLink *)v3d;
223 }
224
225 /* not spacelink itself */
226 static void view3d_free(SpaceLink *sl)
227 {
228         View3D *vd= (View3D *) sl;
229         
230         if(vd->bgpic) {
231                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
232                 MEM_freeN(vd->bgpic);
233         }
234         
235         if(vd->localvd) MEM_freeN(vd->localvd);
236         
237         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
238         
239 }
240
241
242 /* spacetype; init callback */
243 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
244 {
245
246 }
247
248 static SpaceLink *view3d_duplicate(SpaceLink *sl)
249 {
250         View3D *v3do= (View3D *)sl;
251         View3D *v3dn= MEM_dupallocN(sl);
252         
253         /* clear or remove stuff from old */
254         
255 // XXX  BIF_view3d_previewrender_free(v3do);
256         
257         if(v3do->localvd) {
258                 v3do->localvd= NULL;
259                 v3do->properties_storage= NULL;
260                 v3do->localview= 0;
261                 v3do->lay= v3dn->localvd->lay;
262                 v3do->lay &= 0xFFFFFF;
263         }
264         
265         /* copy or clear inside new stuff */
266
267         if(v3dn->bgpic) {
268                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
269                 if(v3dn->bgpic->ima) v3dn->bgpic->ima->id.us++;
270         }
271         v3dn->properties_storage= NULL;
272         
273         return (SpaceLink *)v3dn;
274 }
275
276 static void view3d_modal_keymaps(wmWindowManager *wm, ARegion *ar, int stype)
277 {
278         RegionView3D *rv3d= ar->regiondata;
279         ListBase *keymap;
280         
281         /* copy last mode, then we can re-init the region maps */
282         rv3d->lastmode= stype;
283         
284         keymap= WM_keymap_listbase(wm, "Object Mode", 0, 0);
285         if(ELEM(stype, 0, NS_MODE_OBJECT))
286                 WM_event_add_keymap_handler(&ar->handlers, keymap);
287         else
288                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
289         
290         keymap= WM_keymap_listbase(wm, "EditMesh", 0, 0);
291         if(stype==NS_EDITMODE_MESH)
292                 WM_event_add_keymap_handler(&ar->handlers, keymap);
293         else
294                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
295         
296         keymap= WM_keymap_listbase(wm, "Curve", 0, 0);
297         if(stype==NS_EDITMODE_CURVE)
298                 WM_event_add_keymap_handler(&ar->handlers, keymap);
299         else
300                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
301         
302         keymap= WM_keymap_listbase(wm, "Armature", 0, 0);
303         if(stype==NS_EDITMODE_ARMATURE)
304                 WM_event_add_keymap_handler(&ar->handlers, keymap);
305         else
306                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
307                 
308         keymap= WM_keymap_listbase(wm, "Pose", 0, 0);
309         if(stype==NS_MODE_POSE)
310                 WM_event_add_keymap_handler(&ar->handlers, keymap);
311         else
312                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
313
314         keymap= WM_keymap_listbase(wm, "Metaball", 0, 0);
315         if(stype==NS_EDITMODE_MBALL)
316                 WM_event_add_keymap_handler(&ar->handlers, keymap);
317         else
318                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
319         
320         keymap= WM_keymap_listbase(wm, "Lattice", 0, 0);
321         if(stype==NS_EDITMODE_LATTICE)
322                 WM_event_add_keymap_handler(&ar->handlers, keymap);
323         else
324                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
325
326         /* armature sketching needs to take over mouse */
327         keymap= WM_keymap_listbase(wm, "Armature_Sketch", 0, 0);
328         if(stype==NS_EDITMODE_ARMATURE)
329                 WM_event_add_keymap_handler_priority(&ar->handlers, keymap, 10);
330         else
331                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
332
333         keymap= WM_keymap_listbase(wm, "Particle", 0, 0);
334         if(stype==NS_MODE_PARTICLE)
335                 WM_event_add_keymap_handler(&ar->handlers, keymap);
336         else
337                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
338
339         /* editfont keymap swallows all... */
340         keymap= WM_keymap_listbase(wm, "Font", 0, 0);
341         if(stype==NS_EDITMODE_TEXT)
342                 WM_event_add_keymap_handler_priority(&ar->handlers, keymap, 10);
343         else
344                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
345 }
346
347 /* add handlers, stuff you only do once or on area/region changes */
348 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
349 {
350         RegionView3D *rv3d= ar->regiondata;
351         ListBase *keymap;
352         
353         /* own keymap */
354         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
355         WM_event_add_keymap_handler(&ar->handlers, keymap);
356         keymap= WM_keymap_listbase(wm, "View3D", SPACE_VIEW3D, 0);
357         WM_event_add_keymap_handler(&ar->handlers, keymap);
358         
359         /* object ops. */
360         keymap= WM_keymap_listbase(wm, "Object Non-modal", 0, 0);
361         WM_event_add_keymap_handler(&ar->handlers, keymap);
362         
363         /* pose is not modal, operator poll checks for this */
364         keymap= WM_keymap_listbase(wm, "Pose", 0, 0);
365         WM_event_add_keymap_handler(&ar->handlers, keymap);
366         
367         /* modal ops keymaps */
368         view3d_modal_keymaps(wm, ar, rv3d->lastmode);
369         /* operator poll checks for modes */
370         keymap= WM_keymap_listbase(wm, "ImagePaint", 0, 0);
371         WM_event_add_keymap_handler(&ar->handlers, keymap);
372 }
373
374 /* type callback, not region itself */
375 static void view3d_main_area_free(ARegion *ar)
376 {
377         RegionView3D *rv3d= ar->regiondata;
378         
379         if(rv3d) {
380                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
381                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
382
383                 // XXX  retopo_free_view_data(rv3d);
384                 if(rv3d->ri) { 
385                         // XXX          BIF_view3d_previewrender_free(rv3d);
386                 }
387                 
388                 if(rv3d->depths) {
389                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
390                         MEM_freeN(rv3d->depths);
391                 }
392                 MEM_freeN(rv3d);
393                 ar->regiondata= NULL;
394         }
395 }
396
397 /* copy regiondata */
398 static void *view3d_main_area_duplicate(void *poin)
399 {
400         if(poin) {
401                 RegionView3D *rv3d= poin, *new;
402         
403                 new= MEM_dupallocN(rv3d);
404                 if(rv3d->localvd) 
405                         new->localvd= MEM_dupallocN(rv3d->localvd);
406                 if(rv3d->clipbb) 
407                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
408                 
409                 new->depths= NULL;
410                 new->retopo_view_data= NULL;
411                 new->ri= NULL;
412                 new->gpd= NULL;
413                 new->sms= NULL;
414                 new->smooth_timer= NULL;
415                 
416                 return new;
417         }
418         return NULL;
419 }
420
421 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
422 {
423         /* context changes */
424         switch(wmn->category) {
425                 case NC_ANIMATION:
426                         switch(wmn->data) {
427                                 case ND_KEYFRAME_EDIT:
428                                 case ND_KEYFRAME_PROP:
429                                 case ND_NLA_EDIT:
430                                 case ND_NLA_ACTCHANGE:
431                                 case ND_ANIMCHAN_SELECT:
432                                         ED_region_tag_redraw(ar);
433                                         break;
434                         }
435                         break;
436                 case NC_SCENE:
437                         switch(wmn->data) {
438                                 case ND_TRANSFORM:
439                                 case ND_FRAME:
440                                 case ND_OB_ACTIVE:
441                                 case ND_OB_SELECT:
442                                         ED_region_tag_redraw(ar);
443                                         break;
444                                 case ND_MODE:
445                                         view3d_modal_keymaps(wmn->wm, ar, wmn->subtype);
446                                         ED_region_tag_redraw(ar);
447                                         break;
448                         }
449                         break;
450                 case NC_OBJECT:
451                         switch(wmn->data) {
452                                 case ND_BONE_ACTIVE:
453                                 case ND_BONE_SELECT:
454                                 case ND_TRANSFORM:
455                                 case ND_GEOM_SELECT:
456                                 case ND_GEOM_DATA:
457                                 case ND_DRAW:
458                                 case ND_MODIFIER:
459                                 case ND_CONSTRAINT:
460                                 case ND_KEYS:
461                                 case ND_PARTICLE:
462                                         ED_region_tag_redraw(ar);
463                                         break;
464                         }
465                 case NC_GROUP:
466                         /* all group ops for now */
467                         ED_region_tag_redraw(ar);
468                         break;
469                 case NC_MATERIAL:
470                         switch(wmn->data) {
471                                 case ND_SHADING_DRAW:
472                                         ED_region_tag_redraw(ar);
473                                         break;
474                         }
475                 case NC_LAMP:
476                         switch(wmn->data) {
477                                 case ND_LIGHTING_DRAW:
478                                         ED_region_tag_redraw(ar);
479                                         break;
480                         }
481                 case NC_IMAGE:  
482                         /* this could be more fine grained checks if we had
483                          * more context than just the region */
484                         ED_region_tag_redraw(ar);
485                         break;
486         }
487 }
488
489 /* concept is to retrieve cursor type context-less */
490 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
491 {
492         Scene *scene= win->screen->scene;
493
494         if(scene->obedit) {
495                 WM_cursor_set(win, CURSOR_EDIT);
496         }
497         else {
498                 WM_cursor_set(win, CURSOR_STD);
499         }
500 }
501
502 /* add handlers, stuff you only do once or on area/region changes */
503 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
504 {
505         ListBase *keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
506         
507         WM_event_add_keymap_handler(&ar->handlers, keymap);
508
509         ED_region_header_init(ar);
510 }
511
512 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
513 {
514         ED_region_header(C, ar);
515 }
516
517 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
518 {
519         /* context changes */
520         switch(wmn->category) {
521                 case NC_SCENE:
522                         switch(wmn->data) {
523                                 case ND_FRAME:
524                                 case ND_OB_ACTIVE:
525                                 case ND_OB_SELECT:
526                                 case ND_MODE:
527                                         ED_region_tag_redraw(ar);
528                                         break;
529                         }
530                         break;
531         }
532 }
533
534 /* add handlers, stuff you only do once or on area/region changes */
535 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
536 {
537         ListBase *keymap;
538
539         ED_region_panels_init(wm, ar);
540         
541         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
542         WM_event_add_keymap_handler(&ar->handlers, keymap);
543 }
544
545 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
546 {
547         ED_region_panels(C, ar, 1, NULL, -1);
548 }
549
550 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
551 {
552         /* context changes */
553         switch(wmn->category) {
554                 case NC_ANIMATION:
555                         switch(wmn->data) {
556                                 case ND_KEYFRAME_EDIT:
557                                 case ND_KEYFRAME_PROP:
558                                 case ND_NLA_EDIT:
559                                 case ND_NLA_ACTCHANGE:
560                                         ED_region_tag_redraw(ar);
561                                         break;
562                         }
563                         break;
564                 case NC_SCENE:
565                         switch(wmn->data) {
566                                 case ND_FRAME:
567                                 case ND_OB_ACTIVE:
568                                 case ND_OB_SELECT:
569                                 case ND_MODE:
570                                         ED_region_tag_redraw(ar);
571                                         break;
572                         }
573                         break;
574                 case NC_OBJECT:
575                         switch(wmn->data) {
576                                 case ND_BONE_ACTIVE:
577                                 case ND_BONE_SELECT:
578                                 case ND_TRANSFORM:
579                                 case ND_GEOM_SELECT:
580                                 case ND_GEOM_DATA:
581                                 case ND_DRAW:
582                                 case ND_KEYS:
583                                         ED_region_tag_redraw(ar);
584                                         break;
585                         }
586         }
587 }
588
589 /* add handlers, stuff you only do once or on area/region changes */
590 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
591 {
592         ListBase *keymap;
593         
594         ED_region_panels_init(wm, ar);
595
596         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
597         WM_event_add_keymap_handler(&ar->handlers, keymap);
598 }
599
600
601
602 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
603 {
604         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
605 }
606
607 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
608 {
609         View3D *v3d= CTX_wm_view3d(C);
610         Scene *scene= CTX_data_scene(C);
611         Base *base;
612         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
613
614         if(CTX_data_dir(member)) {
615                 static const char *dir[] = {
616                         "selected_objects", "selected_bases" "selected_editable_objects",
617                         "selected_editable_bases" "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
618                         "active_base", "active_object", "visible_bones", "editable_bones",
619                         "selected_bones", "selected_editable_bones" "visible_pchans",
620                         "selected_pchans", "active_bone", "active_pchan", NULL};
621
622                 CTX_data_dir_set(result, dir);
623         }
624         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
625                 int selected_objects= CTX_data_equals(member, "selected_objects");
626
627                 for(base=scene->base.first; base; base=base->next) {
628                         if((base->flag & SELECT) && (base->lay & lay)) {
629                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
630                                         if(selected_objects)
631                                                 CTX_data_id_list_add(result, &base->object->id);
632                                         else
633                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
634                                 }
635                         }
636                 }
637
638                 return 1;
639         }
640         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
641                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
642
643                 for(base=scene->base.first; base; base=base->next) {
644                         if((base->flag & SELECT) && (base->lay & lay)) {
645                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
646                                         if(0==object_is_libdata(base->object)) {
647                                                 if(selected_editable_objects)
648                                                         CTX_data_id_list_add(result, &base->object->id);
649                                                 else
650                                                         CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
651                                         }
652                                 }
653                         }
654                 }
655                 
656                 return 1;
657         }
658         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
659                 int visible_objects= CTX_data_equals(member, "visible_objects");
660
661                 for(base=scene->base.first; base; base=base->next) {
662                         if(base->lay & lay) {
663                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
664                                         if(visible_objects)
665                                                 CTX_data_id_list_add(result, &base->object->id);
666                                         else
667                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
668                                 }
669                         }
670                 }
671                 
672                 return 1;
673         }
674         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
675                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
676
677                 for(base=scene->base.first; base; base=base->next) {
678                         if(base->lay & lay) {
679                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
680                                         if(selectable_objects)
681                                                 CTX_data_id_list_add(result, &base->object->id);
682                                         else
683                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
684                                 }
685                         }
686                 }
687                 
688                 return 1;
689         }
690         else if(CTX_data_equals(member, "active_base")) {
691                 if(scene->basact && (scene->basact->lay & lay))
692                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
693                                 CTX_data_pointer_set(result, &scene->id, &RNA_UnknownType, scene->basact);
694                 
695                 return 1;
696         }
697         else if(CTX_data_equals(member, "active_object")) {
698                 if(scene->basact && (scene->basact->lay & lay))
699                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
700                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
701                 
702                 return 1;
703         }
704         else if(CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
705                 Object *obedit= scene->obedit; // XXX get from context?
706                 bArmature *arm= (obedit) ? obedit->data : NULL;
707                 EditBone *ebone, *flipbone=NULL;
708                 int editable_bones= CTX_data_equals(member, "editable_bones");
709                 
710                 if (arm && arm->edbo) {
711                         /* Attention: X-Axis Mirroring is also handled here... */
712                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
713                                 /* first and foremost, bone must be visible and selected */
714                                 if (EBONE_VISIBLE(arm, ebone)) {
715                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
716                                          * so that most users of this data don't need to explicitly check for it themselves.
717                                          * 
718                                          * We need to make sure that these mirrored copies are not selected, otherwise some
719                                          * bones will be operated on twice.
720                                          */
721                                         if (arm->flag & ARM_MIRROR_EDIT)
722                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
723                                         
724                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
725                                         if (editable_bones) {
726                                                 /* only selected + editable */
727                                                 if (EBONE_EDITABLE(ebone)) {
728                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
729                                                 
730                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
731                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
732                                                 }
733                                         }
734                                         else {
735                                                 /* only include bones if visible */
736                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
737                                                 
738                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone)==0)
739                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
740                                         }
741                                 }
742                         }       
743                         
744                         return 1;
745                 }
746         }
747         else if(CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
748                 Object *obedit= scene->obedit; // XXX get from context?
749                 bArmature *arm= (obedit) ? obedit->data : NULL;
750                 EditBone *ebone, *flipbone=NULL;
751                 int selected_editable_bones= CTX_data_equals(member, "selected_editable_bones");
752                 
753                 if (arm && arm->edbo) {
754                         /* Attention: X-Axis Mirroring is also handled here... */
755                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
756                                 /* first and foremost, bone must be visible and selected */
757                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
758                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
759                                          * so that most users of this data don't need to explicitly check for it themselves.
760                                          * 
761                                          * We need to make sure that these mirrored copies are not selected, otherwise some
762                                          * bones will be operated on twice.
763                                          */
764                                         if (arm->flag & ARM_MIRROR_EDIT)
765                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
766                                         
767                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
768                                         if (selected_editable_bones) {
769                                                 /* only selected + editable */
770                                                 if (EBONE_EDITABLE(ebone)) {
771                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
772                                                 
773                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
774                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
775                                                 }
776                                         }
777                                         else {
778                                                 /* only include bones if selected */
779                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
780                                                 
781                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
782                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
783                                         }
784                                 }
785                         }       
786                         
787                         return 1;
788                 }
789         }
790         else if(CTX_data_equals(member, "visible_pchans")) {
791                 Object *obact= OBACT;
792                 bArmature *arm= (obact) ? obact->data : NULL;
793                 bPoseChannel *pchan;
794                 
795                 if (obact && arm) {
796                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
797                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
798                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
799                                         CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
800                                 }
801                         }
802                         
803                         return 1;
804                 }
805         }
806         else if(CTX_data_equals(member, "selected_pchans")) {
807                 Object *obact= OBACT;
808                 bArmature *arm= (obact) ? obact->data : NULL;
809                 bPoseChannel *pchan;
810                 
811                 if (obact && arm) {
812                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
813                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
814                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
815                                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) 
816                                                 CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
817                                 }
818                         }
819                         
820                         return 1;
821                 }
822         }
823         else if(CTX_data_equals(member, "active_bone")) {
824                 Object *obedit= scene->obedit; // XXX get from context?
825                 bArmature *arm= (obedit) ? obedit->data : NULL;
826                 EditBone *ebone;
827                 
828                 if (arm && arm->edbo) {
829                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
830                                 if (EBONE_VISIBLE(arm, ebone)) {
831                                         if (ebone->flag & BONE_ACTIVE) {
832                                                 CTX_data_pointer_set(result, &arm->id, &RNA_UnknownType, ebone);
833                                                 
834                                                 return 1;
835                                         }
836                                 }
837                         }
838                 }
839                 
840         }
841         else if(CTX_data_equals(member, "active_pchan")) {
842                 Object *obact= OBACT;
843                 bPoseChannel *pchan;
844                 
845                 pchan= get_active_posechannel(obact);
846                 if (pchan) {
847                         CTX_data_pointer_set(result, &obact->id, &RNA_PoseChannel, pchan);
848                         return 1;
849                 }
850         }
851
852         return 0;
853 }
854
855 /* only called once, from space/spacetypes.c */
856 void ED_spacetype_view3d(void)
857 {
858         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
859         ARegionType *art;
860         
861         st->spaceid= SPACE_VIEW3D;
862         
863         st->new= view3d_new;
864         st->free= view3d_free;
865         st->init= view3d_init;
866         st->duplicate= view3d_duplicate;
867         st->operatortypes= view3d_operatortypes;
868         st->keymap= view3d_keymap;
869         st->context= view3d_context;
870         
871         /* regions: main window */
872         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
873         art->regionid = RGN_TYPE_WINDOW;
874         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
875         art->draw= view3d_main_area_draw;
876         art->init= view3d_main_area_init;
877         art->free= view3d_main_area_free;
878         art->duplicate= view3d_main_area_duplicate;
879         art->listener= view3d_main_area_listener;
880         art->cursor= view3d_main_area_cursor;
881         BLI_addhead(&st->regiontypes, art);
882         
883         /* regions: listview/buttons */
884         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
885         art->regionid = RGN_TYPE_UI;
886         art->minsizex= 220; // XXX
887         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
888         art->listener= view3d_buttons_area_listener;
889         art->init= view3d_buttons_area_init;
890         art->draw= view3d_buttons_area_draw;
891         BLI_addhead(&st->regiontypes, art);
892
893         view3d_buttons_register(art);
894
895         /* regions: tool(bar) */
896         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
897         art->regionid = RGN_TYPE_TOOLS;
898         art->minsizex= 160; // XXX
899         art->minsizey= 50; // XXX
900         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
901         art->listener= view3d_buttons_area_listener;
902         art->init= view3d_tools_area_init;
903         art->draw= view3d_tools_area_draw;
904         BLI_addhead(&st->regiontypes, art);
905         
906         view3d_toolbar_register(art);
907
908         /* regions: tool properties */
909         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
910         art->regionid = RGN_TYPE_TOOL_PROPS;
911         art->minsizex= 0;
912         art->minsizey= 120;
913         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
914         art->listener= view3d_buttons_area_listener;
915         art->init= view3d_tools_area_init;
916         art->draw= view3d_tools_area_draw;
917         BLI_addhead(&st->regiontypes, art);
918         
919         view3d_tool_props_register(art);
920         
921         
922         /* regions: header */
923         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
924         art->regionid = RGN_TYPE_HEADER;
925         art->minsizey= HEADERY;
926         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
927         art->listener= view3d_header_area_listener;
928         art->init= view3d_header_area_init;
929         art->draw= view3d_header_area_draw;
930         BLI_addhead(&st->regiontypes, art);
931         
932         BKE_spacetype_register(st);
933 }
934