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