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