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