Ouliner: Added recursive select of (visible) bones (Edit- and Object-mode)
[blender.git] / source / blender / editors / space_outliner / outliner_select.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2004 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_outliner/outliner_select.c
29  *  \ingroup spoutliner
30  */
31
32 #include <stdlib.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_armature_types.h"
37 #include "DNA_group_types.h"
38 #include "DNA_lamp_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_sequence_types.h"
43 #include "DNA_world_types.h"
44
45 #include "BLI_utildefines.h"
46 #include "BLI_listbase.h"
47
48 #include "BKE_context.h"
49 #include "BKE_depsgraph.h"
50 #include "BKE_main.h"
51 #include "BKE_object.h"
52 #include "BKE_scene.h"
53 #include "BKE_sequencer.h"
54 #include "BKE_armature.h"
55
56 #include "ED_armature.h"
57 #include "ED_object.h"
58 #include "ED_screen.h"
59 #include "ED_sequencer.h"
60 #include "ED_util.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65
66 #include "UI_interface.h"
67 #include "UI_view2d.h"
68
69 #include "RNA_access.h"
70 #include "RNA_define.h"
71
72 #include "outliner_intern.h"
73
74 /* ****************************************************** */
75 /* Outliner Selection (gray-blue highlight for rows) */
76
77 static int outliner_select(SpaceOops *soops, ListBase *lb, int *index, short *selecting)
78 {
79         TreeElement *te;
80         TreeStoreElem *tselem;
81         int change = 0;
82         
83         for (te = lb->first; te && *index >= 0; te = te->next, (*index)--) {
84                 tselem = TREESTORE(te);
85                 
86                 /* if we've encountered the right item, set its 'Outliner' selection status */
87                 if (*index == 0) {
88                         /* this should be the last one, so no need to do anything with index */
89                         if ((te->flag & TE_ICONROW) == 0) {
90                                 /* -1 value means toggle testing for now... */
91                                 if (*selecting == -1) {
92                                         if (tselem->flag & TSE_SELECTED) 
93                                                 *selecting = 0;
94                                         else 
95                                                 *selecting = 1;
96                                 }
97                                 
98                                 /* set selection */
99                                 if (*selecting) 
100                                         tselem->flag |= TSE_SELECTED;
101                                 else 
102                                         tselem->flag &= ~TSE_SELECTED;
103
104                                 change |= 1;
105                         }
106                 }
107                 else if (TSELEM_OPEN(tselem, soops)) {
108                         /* Only try selecting sub-elements if we haven't hit the right element yet
109                          *
110                          * Hack warning:
111                          *  Index must be reduced before supplying it to the sub-tree to try to do
112                          *  selection, however, we need to increment it again for the next loop to
113                          *  function correctly
114                          */
115                         (*index)--;
116                         change |= outliner_select(soops, &te->subtree, index, selecting);
117                         (*index)++;
118                 }
119         }
120
121         return change;
122 }
123
124 /* ****************************************************** */
125 /* Outliner Element Selection/Activation on Click */
126
127 static int tree_element_active_renderlayer(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
128 {
129         Scene *sce;
130         
131         /* paranoia check */
132         if (te->idcode != ID_SCE)
133                 return 0;
134         sce = (Scene *)tselem->id;
135         
136         if (set) {
137                 sce->r.actlay = tselem->nr;
138                 WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, sce);
139         }
140         else {
141                 return sce->r.actlay == tselem->nr;
142         }
143         return 0;
144 }
145
146 /**
147  * Select object tree:
148  * CTRL+LMB: Select/Deselect object and all cildren
149  * CTRL+SHIFT+LMB: Add/Remove object and all children
150  */
151 static void do_outliner_object_select_recursive(Scene *scene, Object *ob_parent, bool select)
152 {
153         Base *base;
154
155         for (base = FIRSTBASE; base; base = base->next) {
156                 Object *ob = base->object;
157                 if ((((ob->restrictflag & OB_RESTRICT_VIEW) == 0) && BKE_object_is_child_recursive(ob_parent, ob))) {
158                         ED_base_object_select(base, select ? BA_SELECT : BA_DESELECT);
159                 }
160         }
161 }
162
163 static void do_outliner_bone_select_recursive(Scene *scene, bArmature *arm, Bone *bone_parent, bool select)
164 {
165         Bone *bone;
166         for (bone = bone_parent->childbase.first; bone; bone = bone->next) {
167                 if(select && PBONE_VISIBLE(arm, bone))
168                         bone->flag |= BONE_SELECTED;
169                 else
170                         bone->flag &= ~BONE_SELECTED;
171                 do_outliner_bone_select_recursive(scene, arm, bone, select);
172         }
173 }
174
175 static bool is_child_of(EditBone *ebone, EditBone *ebone_parent) {
176         for (ebone = ebone->parent; ebone; ebone=ebone->parent) {
177                 if (ebone == ebone_parent)
178                         return true;
179         }
180         return false;
181 }
182
183 static void do_outliner_ebone_select_recursive(Scene *scene, bArmature *arm, EditBone *ebone_parent, bool select)
184 {
185         EditBone *ebone;
186         for (ebone = ebone_parent->next; ebone; ebone=ebone->next) {
187                 if (is_child_of(ebone, ebone_parent)) {
188                         if(select && EBONE_VISIBLE(arm, ebone))
189                                 ebone->flag |= BONE_SELECTED;
190                         else
191                                 ebone->flag &= ~BONE_SELECTED;
192                 }
193         }
194 }
195
196 static int  tree_element_set_active_object(bContext *C, Scene *scene, SpaceOops *soops,
197                                            TreeElement *te, int set, bool recursive)
198 {
199         TreeStoreElem *tselem = TREESTORE(te);
200         Scene *sce;
201         Base *base;
202         Object *ob = NULL;
203         
204         /* if id is not object, we search back */
205         if (te->idcode == ID_OB) {
206                 ob = (Object *)tselem->id;
207         }
208         else {
209                 ob = (Object *)outliner_search_back(soops, te, ID_OB);
210                 if (ob == OBACT) return 0;
211         }
212         if (ob == NULL) return 0;
213         
214         sce = (Scene *)outliner_search_back(soops, te, ID_SCE);
215         if (sce && scene != sce) {
216                 ED_screen_set_scene(C, CTX_wm_screen(C), sce);
217         }
218         
219         /* find associated base in current scene */
220         base = BKE_scene_base_find(scene, ob);
221
222         if (base) {
223                 if (set == 2) {
224                         /* swap select */
225                         if (base->flag & SELECT)
226                                 ED_base_object_select(base, BA_DESELECT);
227                         else 
228                                 ED_base_object_select(base, BA_SELECT);
229                 }
230                 else {
231                         /* deleselect all */
232                         BKE_scene_base_deselect_all(scene);
233                         ED_base_object_select(base, BA_SELECT);
234                 }
235
236                 if (recursive) {
237                         /* Recursive select/deselect */
238                         do_outliner_object_select_recursive(scene, ob, (ob->flag & SELECT) != 0);
239                 }
240
241                 if (C) {
242                         ED_base_object_activate(C, base); /* adds notifier */
243                         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
244                 }
245         }
246         
247         if (ob != scene->obedit)
248                 ED_object_exit_editmode(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO);
249                 
250         return 1;
251 }
252
253 static int tree_element_active_material(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
254 {
255         TreeElement *tes;
256         Object *ob;
257         
258         /* we search for the object parent */
259         ob = (Object *)outliner_search_back(soops, te, ID_OB);
260         // note: ob->matbits can be NULL when a local object points to a library mesh.
261         if (ob == NULL || ob != OBACT || ob->matbits == NULL) return 0;  // just paranoia
262         
263         /* searching in ob mat array? */
264         tes = te->parent;
265         if (tes->idcode == ID_OB) {
266                 if (set) {
267                         ob->actcol = te->index + 1;
268                         ob->matbits[te->index] = 1;  // make ob material active too
269                 }
270                 else {
271                         if (ob->actcol == te->index + 1)
272                                 if (ob->matbits[te->index]) return 1;
273                 }
274         }
275         /* or we search for obdata material */
276         else {
277                 if (set) {
278                         ob->actcol = te->index + 1;
279                         ob->matbits[te->index] = 0;  // make obdata material active too
280                 }
281                 else {
282                         if (ob->actcol == te->index + 1)
283                                 if (ob->matbits[te->index] == 0) return 1;
284                 }
285         }
286         if (set) {
287                 WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, NULL);
288         }
289         return 0;
290 }
291
292 static int tree_element_active_texture(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
293 {
294         TreeElement *tep;
295         TreeStoreElem /* *tselem,*/ *tselemp;
296         Object *ob = OBACT;
297         SpaceButs *sbuts = NULL;
298         
299         if (ob == NULL) return 0;  // no active object
300         
301         /*tselem = TREESTORE(te);*/ /*UNUSED*/
302         
303         /* find buttons area (note, this is undefined really still, needs recode in blender) */
304         /* XXX removed finding sbuts */
305         
306         /* where is texture linked to? */
307         tep = te->parent;
308         tselemp = TREESTORE(tep);
309         
310         if (tep->idcode == ID_WO) {
311                 World *wrld = (World *)tselemp->id;
312
313                 if (set) {
314                         if (sbuts) {
315                                 // XXX sbuts->tabo = TAB_SHADING_TEX;   // hack from header_buttonswin.c
316                                 // XXX sbuts->texfrom = 1;
317                         }
318 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
319                         wrld->texact = te->index;
320                 }
321                 else if (tselemp->id == (ID *)(scene->world)) {
322                         if (wrld->texact == te->index) return 1;
323                 }
324         }
325         else if (tep->idcode == ID_LA) {
326                 Lamp *la = (Lamp *)tselemp->id;
327                 if (set) {
328                         if (sbuts) {
329                                 // XXX sbuts->tabo = TAB_SHADING_TEX;   // hack from header_buttonswin.c
330                                 // XXX sbuts->texfrom = 2;
331                         }
332 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
333                         la->texact = te->index;
334                 }
335                 else {
336                         if (tselemp->id == ob->data) {
337                                 if (la->texact == te->index) return 1;
338                         }
339                 }
340         }
341         else if (tep->idcode == ID_MA) {
342                 Material *ma = (Material *)tselemp->id;
343                 if (set) {
344                         if (sbuts) {
345                                 //sbuts->tabo = TAB_SHADING_TEX;        // hack from header_buttonswin.c
346                                 // XXX sbuts->texfrom = 0;
347                         }
348 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
349                         ma->texact = (char)te->index;
350                         
351                         /* also set active material */
352                         ob->actcol = tep->index + 1;
353                 }
354                 else if (tep->flag & TE_ACTIVE) {   // this is active material
355                         if (ma->texact == te->index) return 1;
356                 }
357         }
358         
359         if (set)
360                 WM_event_add_notifier(C, NC_TEXTURE, NULL);
361
362         return 0;
363 }
364
365
366 static int tree_element_active_lamp(bContext *UNUSED(C), Scene *scene, SpaceOops *soops, TreeElement *te, int set)
367 {
368         Object *ob;
369         
370         /* we search for the object parent */
371         ob = (Object *)outliner_search_back(soops, te, ID_OB);
372         if (ob == NULL || ob != OBACT) return 0;  // just paranoia
373         
374         if (set) {
375 // XXX          extern_set_butspace(F5KEY, 0);
376         }
377         else {
378                 return 1;
379         }
380         
381         return 0;
382 }
383
384 static int tree_element_active_camera(bContext *UNUSED(C), Scene *scene, SpaceOops *soops, TreeElement *te, int set)
385 {
386         Object *ob = (Object *)outliner_search_back(soops, te, ID_OB);
387
388         if (set)
389                 return 0;
390
391         return scene->camera == ob;
392 }
393
394 static int tree_element_active_world(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
395 {
396         TreeElement *tep;
397         TreeStoreElem *tselem = NULL;
398         Scene *sce = NULL;
399         
400         tep = te->parent;
401         if (tep) {
402                 tselem = TREESTORE(tep);
403                 if (tselem->type == 0)
404                         sce = (Scene *)tselem->id;
405         }
406         
407         if (set) {  // make new scene active
408                 if (sce && scene != sce) {
409                         ED_screen_set_scene(C, CTX_wm_screen(C), sce);
410                 }
411         }
412         
413         if (tep == NULL || tselem->id == (ID *)scene) {
414                 if (set) {
415 // XXX                  extern_set_butspace(F8KEY, 0);
416                 }
417                 else {
418                         return 1;
419                 }
420         }
421         return 0;
422 }
423
424 static int tree_element_active_defgroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
425 {
426         Object *ob;
427         
428         /* id in tselem is object */
429         ob = (Object *)tselem->id;
430         if (set) {
431                 BLI_assert(te->index + 1 >= 0);
432                 ob->actdef = te->index + 1;
433
434                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
435                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, ob);
436         }
437         else {
438                 if (ob == OBACT)
439                         if (ob->actdef == te->index + 1) return 1;
440         }
441         return 0;
442 }
443
444 static int tree_element_active_posegroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
445 {
446         Object *ob = (Object *)tselem->id;
447         
448         if (set) {
449                 if (ob->pose) {
450                         ob->pose->active_group = te->index + 1;
451                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
452                 }
453         }
454         else {
455                 if (ob == OBACT && ob->pose) {
456                         if (ob->pose->active_group == te->index + 1) return 1;
457                 }
458         }
459         return 0;
460 }
461
462 static int tree_element_active_posechannel(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
463 {
464         Object *ob = (Object *)tselem->id;
465         bArmature *arm = ob->data;
466         bPoseChannel *pchan = te->directdata;
467         
468         if (set) {
469                 if (!(pchan->bone->flag & BONE_HIDDEN_P)) {
470                         
471                         if (set == 2) ED_pose_deselectall(ob, 2);  // 2 = clear active tag
472                         else ED_pose_deselectall(ob, 0);    // 0 = deselect
473                         
474                         if (set == 2 && (pchan->bone->flag & BONE_SELECTED)) {
475                                 pchan->bone->flag &= ~BONE_SELECTED;
476                         }
477                         else {
478                                 pchan->bone->flag |= BONE_SELECTED;
479                                 arm->act_bone = pchan->bone;
480                         }
481                         
482                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_ACTIVE, ob);
483
484                 }
485         }
486         else {
487                 if (ob == OBACT && ob->pose) {
488                         if (pchan->bone->flag & BONE_SELECTED) return 1;
489                 }
490         }
491         return 0;
492 }
493
494 static int tree_element_active_bone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set, bool recursive)
495 {
496         bArmature *arm = (bArmature *)tselem->id;
497         Bone *bone = te->directdata;
498         
499         if (set) {
500                 if (!(bone->flag & BONE_HIDDEN_P)) {
501                         Object *ob = OBACT;
502                         if (ob) {
503                                 if (set == 2) ED_pose_deselectall(ob, 2);  // 2 is clear active tag
504                                 else ED_pose_deselectall(ob, 0);
505                         }
506                         
507                         if (set == 2 && (bone->flag & BONE_SELECTED)) {
508                                 bone->flag &= ~BONE_SELECTED;
509                         }
510                         else {
511                                 bone->flag |= BONE_SELECTED;
512                                 arm->act_bone = bone;
513                         }
514
515                         if (recursive) {
516                                 /* Recursive select/deselect */
517                                 do_outliner_bone_select_recursive(scene, arm, bone, (bone->flag & BONE_SELECTED) != 0);
518                         }
519
520                         
521                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_ACTIVE, ob);
522                 }
523         }
524         else {
525                 Object *ob = OBACT;
526                 
527                 if (ob && ob->data == arm) {
528                         if (bone->flag & BONE_SELECTED) return 1;
529                 }
530         }
531         return 0;
532 }
533
534
535 /* ebones only draw in editmode armature */
536 static void tree_element_active_ebone__sel(bContext *C, Scene *scene, bArmature *arm, EditBone *ebone, short sel)
537 {
538         if (sel) {
539                 ebone->flag |= BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL;
540                 arm->act_edbone = ebone;
541                 // flush to parent?
542                 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag |= BONE_TIPSEL;
543         }
544         else {
545                 ebone->flag &= ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
546                 // flush to parent?
547                 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag &= ~BONE_TIPSEL;
548         }
549
550         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_ACTIVE, scene->obedit);
551 }
552 static int tree_element_active_ebone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tselem), int set, bool recursive)
553 {
554         bArmature *arm = scene->obedit->data;
555         EditBone *ebone = te->directdata;
556         int status = 0;
557         if (set) {
558                 if (set == 1) {
559                         if (!(ebone->flag & BONE_HIDDEN_A)) {
560                                 ED_armature_deselect_all(scene->obedit, 0); // deselect
561                                 tree_element_active_ebone__sel(C, scene, arm, ebone, TRUE);
562                                 status = 1;
563                         }
564                 }
565                 else if (set == 2) {
566                         if (!(ebone->flag & BONE_HIDDEN_A)) {
567                                 if (!(ebone->flag & BONE_SELECTED)) {
568                                         tree_element_active_ebone__sel(C, scene, arm, ebone, TRUE);
569                                         status = 1;
570                                 }
571                                 else {
572                                         /* entirely selected, so de-select */
573                                         tree_element_active_ebone__sel(C, scene, arm, ebone, FALSE);
574                                         status = 0;
575                                 }
576                         }
577                 }
578
579                 if (recursive) {
580                         /* Recursive select/deselect */
581                         do_outliner_ebone_select_recursive(scene, arm, ebone, (ebone->flag & BONE_SELECTED) != 0);
582                 }
583         }
584         else if (ebone->flag & BONE_SELECTED) {
585                 status = 1;
586         }
587         return status;
588 }
589
590 static int tree_element_active_modifier(bContext *C, TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
591 {
592         if (set) {
593                 Object *ob = (Object *)tselem->id;
594                 
595                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
596
597 // XXX          extern_set_butspace(F9KEY, 0);
598         }
599         
600         return 0;
601 }
602
603 static int tree_element_active_psys(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
604 {
605         if (set) {
606                 Object *ob = (Object *)tselem->id;
607                 
608                 WM_event_add_notifier(C, NC_OBJECT | ND_PARTICLE | NA_EDITED, ob);
609                 
610 // XXX          extern_set_butspace(F7KEY, 0);
611         }
612         
613         return 0;
614 }
615
616 static int tree_element_active_constraint(bContext *C, TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
617 {
618         if (set) {
619                 Object *ob = (Object *)tselem->id;
620                 
621                 WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT, ob);
622 // XXX          extern_set_butspace(F7KEY, 0);
623         }
624         
625         return 0;
626 }
627
628 static int tree_element_active_text(bContext *UNUSED(C), Scene *UNUSED(scene), SpaceOops *UNUSED(soops), TreeElement *UNUSED(te), int UNUSED(set))
629 {
630         // XXX removed
631         return 0;
632 }
633
634 static int tree_element_active_pose(bContext *C, Scene *scene, TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
635 {
636         Object *ob = (Object *)tselem->id;
637         Base *base = BKE_scene_base_find(scene, ob);
638         
639         if (set) {
640                 if (scene->obedit)
641                         ED_object_exit_editmode(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO);
642                 
643                 if (ob->mode & OB_MODE_POSE)
644                         ED_armature_exit_posemode(C, base);
645                 else 
646                         ED_armature_enter_posemode(C, base);
647         }
648         else {
649                 if (ob->mode & OB_MODE_POSE) return 1;
650         }
651         return 0;
652 }
653
654 static int tree_element_active_sequence(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
655 {
656         Sequence *seq = (Sequence *) te->directdata;
657         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
658
659         if (set) {
660                 /* only check on setting */
661                 if (BLI_findindex(ed->seqbasep, seq) != -1) {
662                         if (set == 2) {
663                                 BKE_sequencer_active_set(scene, NULL);
664                         }
665                         ED_sequencer_deselect_all(scene);
666
667                         if (set == 2 && seq->flag & SELECT) {
668                                 seq->flag &= ~SELECT;
669                         }
670                         else {
671                                 seq->flag |= SELECT;
672                                 BKE_sequencer_active_set(scene, seq);
673                         }
674                 }
675
676                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
677         }
678         else {
679                 if (ed->act_seq == seq && seq->flag & SELECT) {
680                         return 1;
681                 }
682         }
683         return(0);
684 }
685
686 static int tree_element_active_sequence_dup(Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
687 {
688         Sequence *seq, *p;
689         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
690
691         seq = (Sequence *)te->directdata;
692         if (set == 0) {
693                 if (seq->flag & SELECT)
694                         return(1);
695                 return(0);
696         }
697
698 // XXX  select_single_seq(seq, 1);
699         p = ed->seqbasep->first;
700         while (p) {
701                 if ((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
702                         p = p->next;
703                         continue;
704                 }
705
706 //              if (!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
707 // XXX                  select_single_seq(p, 0);
708                 p = p->next;
709         }
710         return(0);
711 }
712
713 static int tree_element_active_keymap_item(bContext *UNUSED(C), TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
714 {
715         wmKeyMapItem *kmi = te->directdata;
716         
717         if (set == 0) {
718                 if (kmi->flag & KMI_INACTIVE) return 0;
719                 return 1;
720         }
721         else {
722                 kmi->flag ^= KMI_INACTIVE;
723         }
724         return 0;
725 }
726
727 /* ---------------------------------------------- */
728
729 /* generic call for ID data check or make/check active in UI */
730 int tree_element_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
731 {
732
733         switch (te->idcode) {
734                 /* Note: no ID_OB: objects are handled specially to allow multiple
735                  * selection. See do_outliner_item_activate. */
736                 case ID_MA:
737                         return tree_element_active_material(C, scene, soops, te, set);
738                 case ID_WO:
739                         return tree_element_active_world(C, scene, soops, te, set);
740                 case ID_LA:
741                         return tree_element_active_lamp(C, scene, soops, te, set);
742                 case ID_TE:
743                         return tree_element_active_texture(C, scene, soops, te, set);
744                 case ID_TXT:
745                         return tree_element_active_text(C, scene, soops, te, set);
746                 case ID_CA:
747                         return tree_element_active_camera(C, scene, soops, te, set);
748         }
749         return 0;
750 }
751
752 /* generic call for non-id data to make/check active in UI */
753 /* Context can be NULL when set==0 */
754 int tree_element_type_active(bContext *C, Scene *scene, SpaceOops *soops,
755                              TreeElement *te, TreeStoreElem *tselem, int set, bool recursive)
756 {
757         switch (tselem->type) {
758                 case TSE_DEFGROUP:
759                         return tree_element_active_defgroup(C, scene, te, tselem, set);
760                 case TSE_BONE:
761                         return tree_element_active_bone(C, scene, te, tselem, set, recursive);
762                 case TSE_EBONE:
763                         return tree_element_active_ebone(C, scene, te, tselem, set, recursive);
764                 case TSE_MODIFIER:
765                         return tree_element_active_modifier(C, te, tselem, set);
766                 case TSE_LINKED_OB:
767                         if (set) tree_element_set_active_object(C, scene, soops, te, set, FALSE);
768                         else if (tselem->id == (ID *)OBACT) return 1;
769                         break;
770                 case TSE_LINKED_PSYS:
771                         return tree_element_active_psys(C, scene, te, tselem, set);
772                 case TSE_POSE_BASE:
773                         return tree_element_active_pose(C, scene, te, tselem, set);
774                 case TSE_POSE_CHANNEL:
775                         return tree_element_active_posechannel(C, scene, te, tselem, set);
776                 case TSE_CONSTRAINT:
777                         return tree_element_active_constraint(C, te, tselem, set);
778                 case TSE_R_LAYER:
779                         return tree_element_active_renderlayer(C, te, tselem, set);
780                 case TSE_POSEGRP:
781                         return tree_element_active_posegroup(C, scene, te, tselem, set);
782                 case TSE_SEQUENCE:
783                         return tree_element_active_sequence(C, scene, te, tselem, set);
784                 case TSE_SEQUENCE_DUP:
785                         return tree_element_active_sequence_dup(scene, te, tselem, set);
786                 case TSE_KEYMAP_ITEM:
787                         return tree_element_active_keymap_item(C, te, tselem, set);
788                         
789         }
790         return 0;
791 }
792
793 /* ================================================ */
794
795 static int do_outliner_item_activate(bContext *C, Scene *scene, ARegion *ar, SpaceOops *soops,
796                                      TreeElement *te, bool extend, bool recursive, const float mval[2])
797 {
798         
799         if (mval[1] > te->ys && mval[1] < te->ys + UI_UNIT_Y) {
800                 TreeStoreElem *tselem = TREESTORE(te);
801                 int openclose = 0;
802                 
803                 /* open close icon */
804                 if ((te->flag & TE_ICONROW) == 0) {               // hidden icon, no open/close
805                         if (mval[0] > te->xs && mval[0] < te->xs + UI_UNIT_X)
806                                 openclose = 1;
807                 }
808                 
809                 if (openclose) {
810                         /* all below close/open? */
811                         if (extend) {
812                                 tselem->flag &= ~TSE_CLOSED;
813                                 outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
814                         }
815                         else {
816                                 if (tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
817                                 else tselem->flag |= TSE_CLOSED;
818                                 
819                         }
820                         
821                         return 1;
822                 }
823                 /* name and first icon */
824                 else if (mval[0] > te->xs + UI_UNIT_X && mval[0] < te->xend) {
825                         
826                         /* always makes active object */
827                         if (tselem->type != TSE_SEQUENCE && tselem->type != TSE_SEQ_STRIP && tselem->type != TSE_SEQUENCE_DUP)
828                                 tree_element_set_active_object(C, scene, soops, te, 1 + (extend != 0 && tselem->type == 0), recursive);
829                         
830                         if (tselem->type == 0) { // the lib blocks
831                                 /* editmode? */
832                                 if (te->idcode == ID_SCE) {
833                                         if (scene != (Scene *)tselem->id) {
834                                                 ED_screen_set_scene(C, CTX_wm_screen(C), (Scene *)tselem->id);
835                                         }
836                                 }
837                                 else if (te->idcode == ID_GR) {
838                                         Group *gr = (Group *)tselem->id;
839                                         GroupObject *gob;
840                                         
841                                         if (extend) {
842                                                 int sel = BA_SELECT;
843                                                 for (gob = gr->gobject.first; gob; gob = gob->next) {
844                                                         if (gob->ob->flag & SELECT) {
845                                                                 sel = BA_DESELECT;
846                                                                 break;
847                                                         }
848                                                 }
849                                                 
850                                                 for (gob = gr->gobject.first; gob; gob = gob->next) {
851                                                         ED_base_object_select(BKE_scene_base_find(scene, gob->ob), sel);
852                                                 }
853                                         }
854                                         else {
855                                                 BKE_scene_base_deselect_all(scene);
856                                                 
857                                                 for (gob = gr->gobject.first; gob; gob = gob->next) {
858                                                         if ((gob->ob->flag & SELECT) == 0)
859                                                                 ED_base_object_select(BKE_scene_base_find(scene, gob->ob), BA_SELECT);
860                                                 }
861                                         }
862                                         
863                                         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
864                                 }
865                                 else if (ELEM5(te->idcode, ID_ME, ID_CU, ID_MB, ID_LT, ID_AR)) {
866                                         WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_INVOKE_REGION_WIN, NULL);
867                                 }
868                                 else {  // rest of types
869                                         tree_element_active(C, scene, soops, te, 1);
870                                 }
871
872                         }
873                         else {
874                                 tree_element_type_active(C, scene, soops, te, tselem, 1 + (extend != 0), recursive);
875                         }
876                         
877                         return 1;
878                 }
879         }
880         
881         for (te = te->subtree.first; te; te = te->next) {
882                 if (do_outliner_item_activate(C, scene, ar, soops, te, extend, recursive, mval)) return 1;
883         }
884         return 0;
885 }
886
887 int outliner_item_do_activate(bContext *C, int x, int y, bool extend, bool recursive)
888 {
889         Scene *scene = CTX_data_scene(C);
890         ARegion *ar = CTX_wm_region(C);
891         SpaceOops *soops = CTX_wm_space_outliner(C);
892         TreeElement *te;
893         float fmval[2];
894
895         UI_view2d_region_to_view(&ar->v2d, x, y, fmval, fmval + 1);
896
897         if (!ELEM3(soops->outlinevis, SO_DATABLOCKS, SO_USERDEF, SO_KEYMAP) &&
898             !(soops->flag & SO_HIDE_RESTRICTCOLS) &&
899             (fmval[0] > ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX))
900         {
901                 return OPERATOR_CANCELLED;
902         }
903
904         for (te = soops->tree.first; te; te = te->next) {
905                 if (do_outliner_item_activate(C, scene, ar, soops, te, extend, recursive, fmval)) break;
906         }
907         
908         if (te) {
909                 ED_undo_push(C, "Outliner click event");
910         }
911         else {
912                 short selecting = -1;
913                 int row;
914                 
915                 /* get row number - 100 here is just a dummy value since we don't need the column */
916                 UI_view2d_listview_view_to_cell(&ar->v2d, 1000, UI_UNIT_Y, 0.0f, OL_Y_OFFSET, 
917                                                 fmval[0], fmval[1], NULL, &row);
918                 
919                 /* select relevant row */
920                 if (outliner_select(soops, &soops->tree, &row, &selecting)) {
921                 
922                         soops->storeflag |= SO_TREESTORE_REDRAW;
923                 
924                         /* no need for undo push here, only changing outliner data which is
925                          * scene level - campbell */
926                         /* ED_undo_push(C, "Outliner selection event"); */
927                 }
928         }
929         
930         ED_region_tag_redraw(ar);
931
932         return OPERATOR_FINISHED;
933 }
934
935 /* event can enterkey, then it opens/closes */
936 static int outliner_item_activate(bContext *C, wmOperator *op, wmEvent *event)
937 {
938         bool extend    = RNA_boolean_get(op->ptr, "extend");
939         bool recursive = RNA_boolean_get(op->ptr, "recursive");
940         int x = event->mval[0];
941         int y = event->mval[1];
942         return outliner_item_do_activate(C, x, y, extend, recursive);
943 }
944
945 void OUTLINER_OT_item_activate(wmOperatorType *ot)
946 {
947         ot->name = "Activate Item";
948         ot->idname = "OUTLINER_OT_item_activate";
949         ot->description = "Handle mouse clicks to activate/select items";
950         
951         ot->invoke = outliner_item_activate;
952         
953         ot->poll = ED_operator_outliner_active;
954         
955         RNA_def_boolean(ot->srna, "extend", true, "Extend", "Extend selection for activation");
956         RNA_def_boolean(ot->srna, "recursive", false, "Recursive", "Select Objects and their children");
957 }
958
959 /* ****************************************************** */
960
961 /* **************** Border Select Tool ****************** */
962 static void outliner_item_border_select(Scene *scene, SpaceOops *soops, rctf *rectf, TreeElement *te, int gesture_mode)
963 {
964         TreeStoreElem *tselem = TREESTORE(te);
965
966         if (te->ys <= rectf->ymax && te->ys + UI_UNIT_Y >= rectf->ymin) {
967                 if (gesture_mode == GESTURE_MODAL_SELECT) {
968                         tselem->flag |= TSE_SELECTED;
969                 }
970                 else {
971                         tselem->flag &= ~TSE_SELECTED;
972                 }
973         }
974
975         /* Look at its children. */
976         if ((tselem->flag & TSE_CLOSED) == 0) {
977                 for (te = te->subtree.first; te; te = te->next) {
978                         outliner_item_border_select(scene, soops, rectf, te, gesture_mode);
979                 }
980         }
981         return;
982 }
983
984 static int outliner_border_select_exec(bContext *C, wmOperator *op)
985 {
986         Scene *scene = CTX_data_scene(C);
987         SpaceOops *soops = CTX_wm_space_outliner(C);
988         ARegion *ar = CTX_wm_region(C);
989         TreeElement *te;
990         rcti rect;
991         rctf rectf;
992         int gesture_mode = RNA_int_get(op->ptr, "gesture_mode");
993
994         WM_operator_properties_border_to_rcti(op, &rect);
995
996         UI_view2d_region_to_view(&ar->v2d, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
997         UI_view2d_region_to_view(&ar->v2d, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
998
999         for (te = soops->tree.first; te; te = te->next) {
1000                 outliner_item_border_select(scene, soops, &rectf, te, gesture_mode);
1001         }
1002
1003         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1004         ED_region_tag_redraw(ar);
1005
1006         return OPERATOR_FINISHED;
1007 }
1008
1009 void OUTLINER_OT_select_border(wmOperatorType *ot)
1010 {
1011         /* identifiers */
1012         ot->name = "Border Select";
1013         ot->idname = "OUTLINER_OT_select_border";
1014         ot->description = "Use box selection to select tree elements";
1015
1016         /* api callbacks */
1017         ot->invoke = WM_border_select_invoke;
1018         ot->exec = outliner_border_select_exec;
1019         ot->modal = WM_border_select_modal;
1020         ot->cancel = WM_border_select_cancel;
1021
1022         ot->poll = ED_operator_outliner_active;
1023
1024         /* flags */
1025         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1026
1027         /* rna */
1028         WM_operator_properties_gesture_border(ot, FALSE);
1029 }
1030
1031 /* ****************************************************** */