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