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