fa337ba7af1a17eec38e7008230f7f0f365ce659
[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_LINKS, 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                 if (tselem->type == 0)
340                         sce = (Scene *)tselem->id;
341         }
342         
343         if (set) {  // make new scene active
344                 if (sce && scene != sce) {
345                         ED_screen_set_scene(C, CTX_wm_screen(C), sce);
346                 }
347         }
348         
349         if (tep == NULL || tselem->id == (ID *)scene) {
350                 if (set) {
351 // XXX                  extern_set_butspace(F8KEY, 0);
352                 }
353                 else {
354                         return 1;
355                 }
356         }
357         return 0;
358 }
359
360 static int tree_element_active_defgroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
361 {
362         Object *ob;
363         
364         /* id in tselem is object */
365         ob = (Object *)tselem->id;
366         if (set) {
367                 BLI_assert(te->index + 1 >= 0);
368                 ob->actdef = te->index + 1;
369
370                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
371                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, ob);
372         }
373         else {
374                 if (ob == OBACT)
375                         if (ob->actdef == te->index + 1) return 1;
376         }
377         return 0;
378 }
379
380 static int tree_element_active_posegroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
381 {
382         Object *ob = (Object *)tselem->id;
383         
384         if (set) {
385                 if (ob->pose) {
386                         ob->pose->active_group = te->index + 1;
387                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
388                 }
389         }
390         else {
391                 if (ob == OBACT && ob->pose) {
392                         if (ob->pose->active_group == te->index + 1) return 1;
393                 }
394         }
395         return 0;
396 }
397
398 static int tree_element_active_posechannel(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
399 {
400         Object *ob = (Object *)tselem->id;
401         bArmature *arm = ob->data;
402         bPoseChannel *pchan = te->directdata;
403         
404         if (set) {
405                 if (!(pchan->bone->flag & BONE_HIDDEN_P)) {
406                         
407                         if (set == 2) ED_pose_deselectall(ob, 2);  // 2 = clear active tag
408                         else ED_pose_deselectall(ob, 0);    // 0 = deselect
409                         
410                         if (set == 2 && (pchan->bone->flag & BONE_SELECTED)) {
411                                 pchan->bone->flag &= ~BONE_SELECTED;
412                         }
413                         else {
414                                 pchan->bone->flag |= BONE_SELECTED;
415                                 arm->act_bone = pchan->bone;
416                         }
417                         
418                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_ACTIVE, ob);
419
420                 }
421         }
422         else {
423                 if (ob == OBACT && ob->pose) {
424                         if (pchan->bone->flag & BONE_SELECTED) return 1;
425                 }
426         }
427         return 0;
428 }
429
430 static int tree_element_active_bone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
431 {
432         bArmature *arm = (bArmature *)tselem->id;
433         Bone *bone = te->directdata;
434         
435         if (set) {
436                 if (!(bone->flag & BONE_HIDDEN_P)) {
437                         Object *ob = OBACT;
438                         if (ob) {
439                                 if (set == 2) ED_pose_deselectall(ob, 2);  // 2 is clear active tag
440                                 else ED_pose_deselectall(ob, 0);
441                         }
442                         
443                         if (set == 2 && (bone->flag & BONE_SELECTED)) {
444                                 bone->flag &= ~BONE_SELECTED;
445                         }
446                         else {
447                                 bone->flag |= BONE_SELECTED;
448                                 arm->act_bone = bone;
449                         }
450                         
451                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_ACTIVE, ob);
452                 }
453         }
454         else {
455                 Object *ob = OBACT;
456                 
457                 if (ob && ob->data == arm) {
458                         if (bone->flag & BONE_SELECTED) return 1;
459                 }
460         }
461         return 0;
462 }
463
464
465 /* ebones only draw in editmode armature */
466 static void tree_element_active_ebone__sel(bContext *C, Scene *scene, bArmature *arm, EditBone *ebone, short sel)
467 {
468         if (sel) {
469                 ebone->flag |= BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL;
470                 arm->act_edbone = ebone;
471                 // flush to parent?
472                 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag |= BONE_TIPSEL;
473         }
474         else {
475                 ebone->flag &= ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
476                 // flush to parent?
477                 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag &= ~BONE_TIPSEL;
478         }
479
480         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_ACTIVE, scene->obedit);
481 }
482 static int tree_element_active_ebone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
483 {
484         bArmature *arm = scene->obedit->data;
485         EditBone *ebone = te->directdata;
486
487         if (set == 1) {
488                 if (!(ebone->flag & BONE_HIDDEN_A)) {
489                         ED_armature_deselect_all(scene->obedit, 0); // deselect
490                         tree_element_active_ebone__sel(C, scene, arm, ebone, TRUE);
491                         return 1;
492                 }
493         }
494         else if (set == 2) {
495                 if (!(ebone->flag & BONE_HIDDEN_A)) {
496                         if (!(ebone->flag & BONE_SELECTED)) {
497                                 tree_element_active_ebone__sel(C, scene, arm, ebone, TRUE);
498                                 return 1;
499                         }
500                         else {
501                                 /* entirely selected, so de-select */
502                                 tree_element_active_ebone__sel(C, scene, arm, ebone, FALSE);
503                                 return 0;
504                         }
505                 }
506         }
507         else if (ebone->flag & BONE_SELECTED) {
508                 return 1;
509         }
510         return 0;
511 }
512
513 static int tree_element_active_modifier(bContext *C, TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
514 {
515         if (set) {
516                 Object *ob = (Object *)tselem->id;
517                 
518                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
519
520 // XXX          extern_set_butspace(F9KEY, 0);
521         }
522         
523         return 0;
524 }
525
526 static int tree_element_active_psys(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
527 {
528         if (set) {
529                 Object *ob = (Object *)tselem->id;
530                 
531                 WM_event_add_notifier(C, NC_OBJECT | ND_PARTICLE | NA_EDITED, ob);
532                 
533 // XXX          extern_set_butspace(F7KEY, 0);
534         }
535         
536         return 0;
537 }
538
539 static int tree_element_active_constraint(bContext *C, TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
540 {
541         if (set) {
542                 Object *ob = (Object *)tselem->id;
543                 
544                 WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT, ob);
545 // XXX          extern_set_butspace(F7KEY, 0);
546         }
547         
548         return 0;
549 }
550
551 static int tree_element_active_text(bContext *UNUSED(C), Scene *UNUSED(scene), SpaceOops *UNUSED(soops), TreeElement *UNUSED(te), int UNUSED(set))
552 {
553         // XXX removed
554         return 0;
555 }
556
557 static int tree_element_active_pose(bContext *C, Scene *scene, TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
558 {
559         Object *ob = (Object *)tselem->id;
560         Base *base = BKE_scene_base_find(scene, ob);
561         
562         if (set) {
563                 if (scene->obedit)
564                         ED_object_exit_editmode(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO);
565                 
566                 if (ob->mode & OB_MODE_POSE)
567                         ED_armature_exit_posemode(C, base);
568                 else 
569                         ED_armature_enter_posemode(C, base);
570         }
571         else {
572                 if (ob->mode & OB_MODE_POSE) return 1;
573         }
574         return 0;
575 }
576
577 static int tree_element_active_sequence(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
578 {
579         Sequence *seq = (Sequence *) te->directdata;
580         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
581
582         if (set) {
583                 /* only check on setting */
584                 if (BLI_findindex(ed->seqbasep, seq) != -1) {
585                         if (set == 2) {
586                                 BKE_sequencer_active_set(scene, NULL);
587                         }
588                         ED_sequencer_deselect_all(scene);
589
590                         if (set == 2 && seq->flag & SELECT) {
591                                 seq->flag &= ~SELECT;
592                         }
593                         else {
594                                 seq->flag |= SELECT;
595                                 BKE_sequencer_active_set(scene, seq);
596                         }
597                 }
598
599                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
600         }
601         else {
602                 if (ed->act_seq == seq && seq->flag & SELECT) {
603                         return 1;
604                 }
605         }
606         return(0);
607 }
608
609 static int tree_element_active_sequence_dup(Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
610 {
611         Sequence *seq, *p;
612         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
613
614         seq = (Sequence *)te->directdata;
615         if (set == 0) {
616                 if (seq->flag & SELECT)
617                         return(1);
618                 return(0);
619         }
620
621 // XXX  select_single_seq(seq, 1);
622         p = ed->seqbasep->first;
623         while (p) {
624                 if ((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
625                         p = p->next;
626                         continue;
627                 }
628
629 //              if (!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
630 // XXX                  select_single_seq(p, 0);
631                 p = p->next;
632         }
633         return(0);
634 }
635
636 static int tree_element_active_keymap_item(bContext *UNUSED(C), TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
637 {
638         wmKeyMapItem *kmi = te->directdata;
639         
640         if (set == 0) {
641                 if (kmi->flag & KMI_INACTIVE) return 0;
642                 return 1;
643         }
644         else {
645                 kmi->flag ^= KMI_INACTIVE;
646         }
647         return 0;
648 }
649
650 /* ---------------------------------------------- */
651
652 /* generic call for ID data check or make/check active in UI */
653 int tree_element_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
654 {
655
656         switch (te->idcode) {
657                 /* Note: no ID_OB: objects are handled specially to allow multiple
658                  * selection. See do_outliner_item_activate. */
659                 case ID_MA:
660                         return tree_element_active_material(C, scene, soops, te, set);
661                 case ID_WO:
662                         return tree_element_active_world(C, scene, soops, te, set);
663                 case ID_LA:
664                         return tree_element_active_lamp(C, scene, soops, te, set);
665                 case ID_TE:
666                         return tree_element_active_texture(C, scene, soops, te, set);
667                 case ID_TXT:
668                         return tree_element_active_text(C, scene, soops, te, set);
669                 case ID_CA:
670                         return tree_element_active_camera(C, scene, soops, te, set);
671         }
672         return 0;
673 }
674
675 /* generic call for non-id data to make/check active in UI */
676 /* Context can be NULL when set==0 */
677 int tree_element_type_active(bContext *C, Scene *scene, SpaceOops *soops,
678                              TreeElement *te, TreeStoreElem *tselem, int set)
679 {
680         switch (tselem->type) {
681                 case TSE_DEFGROUP:
682                         return tree_element_active_defgroup(C, scene, te, tselem, set);
683                 case TSE_BONE:
684                         return tree_element_active_bone(C, scene, te, tselem, set);
685                 case TSE_EBONE:
686                         return tree_element_active_ebone(C, scene, te, tselem, set);
687                 case TSE_MODIFIER:
688                         return tree_element_active_modifier(C, te, tselem, set);
689                 case TSE_LINKED_OB:
690                         if (set) tree_element_set_active_object(C, scene, soops, te, set);
691                         else if (tselem->id == (ID *)OBACT) return 1;
692                         break;
693                 case TSE_LINKED_PSYS:
694                         return tree_element_active_psys(C, scene, te, tselem, set);
695                 case TSE_POSE_BASE:
696                         return tree_element_active_pose(C, scene, te, tselem, set);
697                 case TSE_POSE_CHANNEL:
698                         return tree_element_active_posechannel(C, scene, te, tselem, set);
699                 case TSE_CONSTRAINT:
700                         return tree_element_active_constraint(C, te, tselem, set);
701                 case TSE_R_LAYER:
702                         return tree_element_active_renderlayer(C, te, tselem, set);
703                 case TSE_POSEGRP:
704                         return tree_element_active_posegroup(C, scene, te, tselem, set);
705                 case TSE_SEQUENCE:
706                         return tree_element_active_sequence(C, scene, te, tselem, set);
707                 case TSE_SEQUENCE_DUP:
708                         return tree_element_active_sequence_dup(scene, te, tselem, set);
709                 case TSE_KEYMAP_ITEM:
710                         return tree_element_active_keymap_item(C, te, tselem, set);
711                         
712         }
713         return 0;
714 }
715
716 /* ================================================ */
717
718 static int do_outliner_item_activate(bContext *C, Scene *scene, ARegion *ar, SpaceOops *soops,
719                                      TreeElement *te, int extend, const float mval[2])
720 {
721         
722         if (mval[1] > te->ys && mval[1] < te->ys + UI_UNIT_Y) {
723                 TreeStoreElem *tselem = TREESTORE(te);
724                 int openclose = 0;
725                 
726                 /* open close icon */
727                 if ((te->flag & TE_ICONROW) == 0) {               // hidden icon, no open/close
728                         if (mval[0] > te->xs && mval[0] < te->xs + UI_UNIT_X)
729                                 openclose = 1;
730                 }
731                 
732                 if (openclose) {
733                         /* all below close/open? */
734                         if (extend) {
735                                 tselem->flag &= ~TSE_CLOSED;
736                                 outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
737                         }
738                         else {
739                                 if (tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
740                                 else tselem->flag |= TSE_CLOSED;
741                                 
742                         }
743                         
744                         return 1;
745                 }
746                 /* name and first icon */
747                 else if (mval[0] > te->xs + UI_UNIT_X && mval[0] < te->xend) {
748                         
749                         /* always makes active object */
750                         if (tselem->type != TSE_SEQUENCE && tselem->type != TSE_SEQ_STRIP && tselem->type != TSE_SEQUENCE_DUP)
751                                 tree_element_set_active_object(C, scene, soops, te, 1 + (extend != 0 && tselem->type == 0));
752                         
753                         if (tselem->type == 0) { // the lib blocks
754                                 /* editmode? */
755                                 if (te->idcode == ID_SCE) {
756                                         if (scene != (Scene *)tselem->id) {
757                                                 ED_screen_set_scene(C, CTX_wm_screen(C), (Scene *)tselem->id);
758                                         }
759                                 }
760                                 else if (te->idcode == ID_GR) {
761                                         Group *gr = (Group *)tselem->id;
762                                         GroupObject *gob;
763                                         
764                                         if (extend) {
765                                                 int sel = BA_SELECT;
766                                                 for (gob = gr->gobject.first; gob; gob = gob->next) {
767                                                         if (gob->ob->flag & SELECT) {
768                                                                 sel = BA_DESELECT;
769                                                                 break;
770                                                         }
771                                                 }
772                                                 
773                                                 for (gob = gr->gobject.first; gob; gob = gob->next) {
774                                                         ED_base_object_select(BKE_scene_base_find(scene, gob->ob), sel);
775                                                 }
776                                         }
777                                         else {
778                                                 BKE_scene_base_deselect_all(scene);
779                                                 
780                                                 for (gob = gr->gobject.first; gob; gob = gob->next) {
781                                                         if ((gob->ob->flag & SELECT) == 0)
782                                                                 ED_base_object_select(BKE_scene_base_find(scene, gob->ob), BA_SELECT);
783                                                 }
784                                         }
785                                         
786                                         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
787                                 }
788                                 else if (ELEM5(te->idcode, ID_ME, ID_CU, ID_MB, ID_LT, ID_AR)) {
789                                         WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_INVOKE_REGION_WIN, NULL);
790                                 }
791                                 else {  // rest of types
792                                         tree_element_active(C, scene, soops, te, 1);
793                                 }
794                                 
795                         }
796                         else tree_element_type_active(C, scene, soops, te, tselem, 1 + (extend != 0));
797                         
798                         return 1;
799                 }
800         }
801         
802         for (te = te->subtree.first; te; te = te->next) {
803                 if (do_outliner_item_activate(C, scene, ar, soops, te, extend, mval)) return 1;
804         }
805         return 0;
806 }
807
808 /* event can enterkey, then it opens/closes */
809 static int outliner_item_activate(bContext *C, wmOperator *op, wmEvent *event)
810 {
811         Scene *scene = CTX_data_scene(C);
812         ARegion *ar = CTX_wm_region(C);
813         SpaceOops *soops = CTX_wm_space_outliner(C);
814         TreeElement *te;
815         float fmval[2];
816         int extend = RNA_boolean_get(op->ptr, "extend");
817
818         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], fmval, fmval + 1);
819
820         if (!ELEM3(soops->outlinevis, SO_DATABLOCKS, SO_USERDEF, SO_KEYMAP) &&
821             !(soops->flag & SO_HIDE_RESTRICTCOLS) &&
822             (fmval[0] > ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX))
823         {
824                 return OPERATOR_CANCELLED;
825         }
826
827         for (te = soops->tree.first; te; te = te->next) {
828                 if (do_outliner_item_activate(C, scene, ar, soops, te, extend, fmval)) break;
829         }
830         
831         if (te) {
832                 ED_undo_push(C, "Outliner click event");
833         }
834         else {
835                 short selecting = -1;
836                 int row;
837                 
838                 /* get row number - 100 here is just a dummy value since we don't need the column */
839                 UI_view2d_listview_view_to_cell(&ar->v2d, 1000, UI_UNIT_Y, 0.0f, OL_Y_OFFSET, 
840                                                 fmval[0], fmval[1], NULL, &row);
841                 
842                 /* select relevant row */
843                 if (outliner_select(soops, &soops->tree, &row, &selecting)) {
844                 
845                         soops->storeflag |= SO_TREESTORE_REDRAW;
846                 
847                         /* no need for undo push here, only changing outliner data which is
848                          * scene level - campbell */
849                         /* ED_undo_push(C, "Outliner selection event"); */
850                 }
851         }
852         
853         ED_region_tag_redraw(ar);
854
855         return OPERATOR_FINISHED;
856 }
857
858 void OUTLINER_OT_item_activate(wmOperatorType *ot)
859 {
860         ot->name = "Activate Item";
861         ot->idname = "OUTLINER_OT_item_activate";
862         ot->description = "Handle mouse clicks to activate/select items";
863         
864         ot->invoke = outliner_item_activate;
865         
866         ot->poll = ED_operator_outliner_active;
867         
868         RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection for activation");
869 }
870
871 /* ****************************************************** */
872
873 /* **************** Border Select Tool ****************** */
874 static void outliner_item_border_select(Scene *scene, SpaceOops *soops, rctf *rectf, TreeElement *te, int gesture_mode)
875 {
876         TreeStoreElem *tselem = TREESTORE(te);
877
878         if (te->ys <= rectf->ymax && te->ys + UI_UNIT_Y >= rectf->ymin) {
879                 if (gesture_mode == GESTURE_MODAL_SELECT) {
880                         tselem->flag |= TSE_SELECTED;
881                 }
882                 else {
883                         tselem->flag &= ~TSE_SELECTED;
884                 }
885         }
886
887         /* Look at its children. */
888         if ((tselem->flag & TSE_CLOSED) == 0) {
889                 for (te = te->subtree.first; te; te = te->next) {
890                         outliner_item_border_select(scene, soops, rectf, te, gesture_mode);
891                 }
892         }
893         return;
894 }
895
896 static int outliner_border_select_exec(bContext *C, wmOperator *op)
897 {
898         Scene *scene = CTX_data_scene(C);
899         SpaceOops *soops = CTX_wm_space_outliner(C);
900         ARegion *ar = CTX_wm_region(C);
901         TreeElement *te;
902         rcti rect;
903         rctf rectf;
904         int gesture_mode = RNA_int_get(op->ptr, "gesture_mode");
905
906         WM_operator_properties_border_to_rcti(op, &rect);
907
908         UI_view2d_region_to_view(&ar->v2d, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
909         UI_view2d_region_to_view(&ar->v2d, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
910
911         for (te = soops->tree.first; te; te = te->next) {
912                 outliner_item_border_select(scene, soops, &rectf, te, gesture_mode);
913         }
914
915         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
916         ED_region_tag_redraw(ar);
917
918         return OPERATOR_FINISHED;
919 }
920
921 void OUTLINER_OT_select_border(wmOperatorType *ot)
922 {
923         /* identifiers */
924         ot->name = "Border Select";
925         ot->idname = "OUTLINER_OT_select_border";
926         ot->description = "Use box selection to select tree elements";
927
928         /* api callbacks */
929         ot->invoke = WM_border_select_invoke;
930         ot->exec = outliner_border_select_exec;
931         ot->modal = WM_border_select_modal;
932         ot->cancel = WM_border_select_cancel;
933
934         ot->poll = ED_operator_outliner_active;
935
936         /* flags */
937         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
938
939         /* rna */
940         WM_operator_properties_gesture_border(ot, FALSE);
941 }
942
943 /* ****************************************************** */