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