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