Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_outliner / outliner_draw.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_draw.c
29  *  \ingroup spoutliner
30  */
31
32 #include "DNA_anim_types.h"
33 #include "DNA_armature_types.h"
34 #include "DNA_gpencil_types.h"
35 #include "DNA_group_types.h"
36 #include "DNA_lamp_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_sequence_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_mempool.h"
45
46 #include "BLT_translation.h"
47
48 #include "BKE_context.h"
49 #include "BKE_deform.h"
50 #include "BKE_depsgraph.h"
51 #include "BKE_fcurve.h"
52 #include "BKE_global.h"
53 #include "BKE_library.h"
54 #include "BKE_main.h"
55 #include "BKE_modifier.h"
56 #include "BKE_report.h"
57 #include "BKE_scene.h"
58 #include "BKE_object.h"
59
60 #include "ED_armature.h"
61 #include "ED_keyframing.h"
62 #include "ED_object.h"
63 #include "ED_screen.h"
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 #include "BIF_gl.h"
69 #include "BIF_glutil.h"
70
71 #include "UI_interface.h"
72 #include "UI_interface_icons.h"
73 #include "UI_resources.h"
74 #include "UI_view2d.h"
75
76 #include "RNA_access.h"
77
78 #include "outliner_intern.h"
79
80 /* disable - this is far too slow - campbell */
81 // #define USE_GROUP_SELECT
82
83 /* ****************************************************** */
84 /* Tree Size Functions */
85
86 static void outliner_height(SpaceOops *soops, ListBase *lb, int *h)
87 {
88         TreeElement *te = lb->first;
89         while (te) {
90                 TreeStoreElem *tselem = TREESTORE(te);
91                 if (TSELEM_OPEN(tselem, soops))
92                         outliner_height(soops, &te->subtree, h);
93                 (*h) += UI_UNIT_Y;
94                 te = te->next;
95         }
96 }
97
98 #if 0  // XXX this is currently disabled until te->xend is set correctly
99 static void outliner_width(SpaceOops *soops, ListBase *lb, int *w)
100 {
101         TreeElement *te = lb->first;
102         while (te) {
103 //              TreeStoreElem *tselem = TREESTORE(te);
104                 
105                 // XXX fixme... te->xend is not set yet
106                 if (!TSELEM_OPEN(tselem, soops)) {
107                         if (te->xend > *w)
108                                 *w = te->xend;
109                 }
110                 outliner_width(soops, &te->subtree, w);
111                 te = te->next;
112         }
113 }
114 #endif
115
116 static void outliner_rna_width(SpaceOops *soops, ListBase *lb, int *w, int startx)
117 {
118         TreeElement *te = lb->first;
119         while (te) {
120                 TreeStoreElem *tselem = TREESTORE(te);
121                 // XXX fixme... (currently, we're using a fixed length of 100)!
122 #if 0
123                 if (te->xend) {
124                         if (te->xend > *w)
125                                 *w = te->xend;
126                 }
127 #endif
128                 if (startx + 100 > *w)
129                         *w = startx + 100;
130
131                 if (TSELEM_OPEN(tselem, soops))
132                         outliner_rna_width(soops, &te->subtree, w, startx + UI_UNIT_X);
133                 te = te->next;
134         }
135 }
136
137 /* ****************************************************** */
138
139 static void restrictbutton_recursive_ebone(bContext *C, EditBone *ebone_parent, int flag, bool set_flag)
140 {
141         Object *obedit = CTX_data_edit_object(C);
142         bArmature *arm = obedit->data;
143         EditBone *ebone;
144         
145         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
146                 if (ED_armature_ebone_is_child_recursive(ebone_parent, ebone)) {
147                         if (set_flag) {
148                                 ebone->flag &= ~(BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL);
149                                 ebone->flag |= flag;
150                         }
151                         else {
152                                 ebone->flag &= ~flag;
153                         }
154                 }
155         }
156 }
157
158 static void restrictbutton_recursive_bone(bContext *C, bArmature *arm, Bone *bone_parent, int flag, bool set_flag)
159 {
160         Bone *bone;
161         for (bone = bone_parent->childbase.first; bone; bone = bone->next) {
162                 if (set_flag) {
163                         bone->flag &= ~(BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL);
164                         bone->flag |= flag;
165                 }
166                 else {
167                         bone->flag &= ~flag;
168                 }
169                 restrictbutton_recursive_bone(C, arm, bone, flag, set_flag);
170         }
171
172 }
173
174 static void restrictbutton_recursive_child(bContext *C, Scene *scene, Object *ob_parent, char flag,
175                                            bool state, bool deselect, const char *rnapropname)
176 {
177         Main *bmain = CTX_data_main(C);
178         Object *ob;
179
180         for (ob = bmain->object.first; ob; ob = ob->id.next) {
181                 if (BKE_object_is_child_recursive(ob_parent, ob)) {
182                         /* only do if child object is selectable */
183                         if ((flag == OB_RESTRICT_SELECT) || (ob->restrictflag & OB_RESTRICT_SELECT) == 0) {
184                                 if (state) {
185                                         ob->restrictflag |= flag;
186                                         if (deselect) {
187                                                 ED_base_object_select(BKE_scene_base_find(scene, ob), BA_DESELECT);
188                                         }
189                                 }
190                                 else {
191                                         ob->restrictflag &= ~flag;
192                                 }
193                         }
194
195                         if (rnapropname) {
196                                 PointerRNA ptr;
197                                 PropertyRNA *prop;
198                                 ID *id;
199                                 bAction *action;
200                                 FCurve *fcu;
201                                 bool driven, special;
202
203                                 RNA_id_pointer_create(&ob->id, &ptr);
204                                 prop = RNA_struct_find_property(&ptr, rnapropname);
205                                 fcu = rna_get_fcurve_context_ui(C, &ptr, prop, 0, NULL, &action, &driven, &special);
206
207                                 if (fcu && !driven) {
208                                         id = ptr.id.data;
209                                         if (autokeyframe_cfra_can_key(scene, id)) {
210                                                 ReportList *reports = CTX_wm_reports(C);
211                                                 ToolSettings *ts = scene->toolsettings;
212                                                 eInsertKeyFlags key_flag = ANIM_get_keyframing_flags(scene, 1);
213
214                                                 fcu->flag &= ~FCURVE_SELECTED;
215                                                 insert_keyframe(reports, id, action, ((fcu->grp) ? (fcu->grp->name) : (NULL)),
216                                                                 fcu->rna_path, fcu->array_index, CFRA, ts->keyframe_type, key_flag);
217                                                 /* Assuming this is not necessary here, since 'ancestor' object button will do it anyway. */
218                                                 /* WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL); */
219                                         }
220                                 }
221                         }
222                 }
223         }
224 }
225
226 static void restrictbutton_view_cb(bContext *C, void *poin, void *poin2)
227 {
228         Scene *scene = (Scene *)poin;
229         Object *ob = (Object *)poin2;
230         
231         if (!common_restrict_check(C, ob)) return;
232         
233         /* deselect objects that are invisible */
234         if (ob->restrictflag & OB_RESTRICT_VIEW) {
235                 /* Ouch! There is no backwards pointer from Object to Base, 
236                  * so have to do loop to find it. */
237                 ED_base_object_select(BKE_scene_base_find(scene, ob), BA_DESELECT);
238         }
239
240         if (CTX_wm_window(C)->eventstate->ctrl) {
241                 restrictbutton_recursive_child(C, scene, ob, OB_RESTRICT_VIEW,
242                                                (ob->restrictflag & OB_RESTRICT_VIEW) != 0, true, "hide");
243         }
244
245         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
246
247 }
248
249 static void restrictbutton_sel_cb(bContext *C, void *poin, void *poin2)
250 {
251         Scene *scene = (Scene *)poin;
252         Object *ob = (Object *)poin2;
253         
254         if (!common_restrict_check(C, ob)) return;
255         
256         /* if select restriction has just been turned on */
257         if (ob->restrictflag & OB_RESTRICT_SELECT) {
258                 /* Ouch! There is no backwards pointer from Object to Base, 
259                  * so have to do loop to find it. */
260                 ED_base_object_select(BKE_scene_base_find(scene, ob), BA_DESELECT);
261         }
262
263         if (CTX_wm_window(C)->eventstate->ctrl) {
264                 restrictbutton_recursive_child(C, scene, ob, OB_RESTRICT_SELECT,
265                                                (ob->restrictflag & OB_RESTRICT_SELECT) != 0, true, NULL);
266         }
267
268         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
269
270 }
271
272 static void restrictbutton_rend_cb(bContext *C, void *poin, void *poin2)
273 {
274         Object *ob = (Object *)poin2;
275
276         if (CTX_wm_window(C)->eventstate->ctrl) {
277                 restrictbutton_recursive_child(C, (Scene *)poin, ob, OB_RESTRICT_RENDER,
278                                                (ob->restrictflag & OB_RESTRICT_RENDER) != 0, false, "hide_render");
279         }
280
281         WM_event_add_notifier(C, NC_SCENE | ND_OB_RENDER, poin);
282 }
283
284 static void restrictbutton_r_lay_cb(bContext *C, void *poin, void *UNUSED(poin2))
285 {
286         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, poin);
287 }
288
289 static void restrictbutton_modifier_cb(bContext *C, void *UNUSED(poin), void *poin2)
290 {
291         Object *ob = (Object *)poin2;
292         
293         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
294         WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
295 }
296
297 static void restrictbutton_bone_visibility_cb(bContext *C, void *poin, void *poin2)
298 {
299         bArmature *arm = (bArmature *)poin;
300         Bone *bone = (Bone *)poin2;
301         if (bone->flag & BONE_HIDDEN_P)
302                 bone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
303
304         if (CTX_wm_window(C)->eventstate->ctrl) {
305                 restrictbutton_recursive_bone(C, arm, bone, BONE_HIDDEN_P, (bone->flag & BONE_HIDDEN_P) != 0);
306         }
307
308         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
309 }
310
311 static void restrictbutton_bone_select_cb(bContext *C, void *poin, void *poin2)
312 {
313         bArmature *arm = (bArmature *)poin;
314         Bone *bone = (Bone *)poin2;
315         if (bone->flag & BONE_UNSELECTABLE)
316                 bone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
317
318         if (CTX_wm_window(C)->eventstate->ctrl) {
319                 restrictbutton_recursive_bone(C, arm, bone, BONE_UNSELECTABLE, (bone->flag & BONE_UNSELECTABLE) != 0);
320         }
321
322         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
323 }
324
325 static void restrictbutton_ebone_select_cb(bContext *C, void *UNUSED(poin), void *poin2)
326 {
327         EditBone *ebone = (EditBone *)poin2;
328
329         if (ebone->flag & BONE_UNSELECTABLE) {
330                 ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
331         }
332
333         if (CTX_wm_window(C)->eventstate->ctrl) {
334                 restrictbutton_recursive_ebone(C, ebone, BONE_UNSELECTABLE, (ebone->flag & BONE_UNSELECTABLE) != 0);
335         }
336
337         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
338 }
339
340 static void restrictbutton_ebone_visibility_cb(bContext *C, void *UNUSED(poin), void *poin2)
341 {
342         EditBone *ebone = (EditBone *)poin2;
343         if (ebone->flag & BONE_HIDDEN_A) {
344                 ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
345         }
346
347         if (CTX_wm_window(C)->eventstate->ctrl) {
348                 restrictbutton_recursive_ebone(C, ebone, BONE_HIDDEN_A, (ebone->flag & BONE_HIDDEN_A) != 0);
349         }
350
351         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
352 }
353
354 static void restrictbutton_gp_layer_flag_cb(bContext *C, void *UNUSED(poin), void *UNUSED(poin2))
355 {
356         WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
357 }
358
359 static int group_restrict_flag(Group *gr, int flag)
360 {
361         GroupObject *gob;
362
363 #ifdef USE_GROUP_SELECT
364         for (gob = gr->gobject.first; gob; gob = gob->next) {
365                 if ((gob->ob->restrictflag & flag) == 0)
366                         return 0;
367         }
368         return 1;
369 #else
370         /* weak but fast */
371         if ((gob = gr->gobject.first))
372                 if ((gob->ob->restrictflag & flag) == 0)
373                         return 0;
374         return 1;
375 #endif
376 }
377
378 static int group_select_flag(Group *gr)
379 {
380         GroupObject *gob;
381
382 #ifdef USE_GROUP_SELECT
383         for (gob = gr->gobject.first; gob; gob = gob->next)
384                 if ((gob->ob->flag & SELECT))
385                         return 1;
386
387         return 0;
388 #else
389         /* weak but fast */
390         if ((gob = gr->gobject.first))
391                 if (gob->ob->flag & SELECT)
392                         return 1;
393         return 0;
394 #endif
395 }
396
397 void restrictbutton_gr_restrict_flag(void *poin, void *poin2, int flag)
398 {
399         Scene *scene = (Scene *)poin;
400         GroupObject *gob;
401         Group *gr = (Group *)poin2;
402
403         if (group_restrict_flag(gr, flag)) {
404                 for (gob = gr->gobject.first; gob; gob = gob->next) {
405                         if (ID_IS_LINKED_DATABLOCK(gob->ob))
406                                 continue;
407
408                         gob->ob->restrictflag &= ~flag;
409                         
410                         if (flag == OB_RESTRICT_VIEW)
411                                 if (gob->ob->flag & SELECT)
412                                         ED_base_object_select(BKE_scene_base_find(scene, gob->ob), BA_DESELECT);
413                 }
414         }
415         else {
416                 for (gob = gr->gobject.first; gob; gob = gob->next) {
417                         if (ID_IS_LINKED_DATABLOCK(gob->ob))
418                                 continue;
419
420                         /* not in editmode */
421                         if (scene->obedit != gob->ob) {
422                                 gob->ob->restrictflag |= flag;
423
424                                 if (ELEM(flag, OB_RESTRICT_SELECT, OB_RESTRICT_VIEW)) {
425                                         if ((gob->ob->flag & SELECT)) {
426                                                 ED_base_object_select(BKE_scene_base_find(scene, gob->ob), BA_DESELECT);
427                                         }
428                                 }
429                         }
430                 }
431         }
432
433
434 static void restrictbutton_gr_restrict_view(bContext *C, void *poin, void *poin2)
435 {
436         restrictbutton_gr_restrict_flag(poin, poin2, OB_RESTRICT_VIEW);
437         WM_event_add_notifier(C, NC_GROUP, NULL);
438         DAG_id_type_tag(CTX_data_main(C), ID_OB);
439 }
440 static void restrictbutton_gr_restrict_select(bContext *C, void *poin, void *poin2)
441 {
442         restrictbutton_gr_restrict_flag(poin, poin2, OB_RESTRICT_SELECT);
443         WM_event_add_notifier(C, NC_GROUP, NULL);
444 }
445 static void restrictbutton_gr_restrict_render(bContext *C, void *poin, void *poin2)
446 {
447         restrictbutton_gr_restrict_flag(poin, poin2, OB_RESTRICT_RENDER);
448         WM_event_add_notifier(C, NC_GROUP, NULL);
449 }
450
451 static void restrictbutton_id_user_toggle(bContext *UNUSED(C), void *poin, void *UNUSED(poin2))
452 {
453         ID *id = (ID *)poin;
454         
455         BLI_assert(id != NULL);
456         
457         if (id->flag & LIB_FAKEUSER) {
458                 id_us_plus(id);
459         }
460         else {
461                 id_us_min(id);
462         }
463 }
464
465
466 static void namebutton_cb(bContext *C, void *tsep, char *oldname)
467 {
468         SpaceOops *soops = CTX_wm_space_outliner(C);
469         Scene *scene = CTX_data_scene(C);
470         Object *obedit = CTX_data_edit_object(C);
471         BLI_mempool *ts = soops->treestore;
472         TreeStoreElem *tselem = tsep;
473         
474         if (ts && tselem) {
475                 TreeElement *te = outliner_find_tree_element(&soops->tree, tselem);
476                 
477                 if (tselem->type == 0) {
478                         BLI_libblock_ensure_unique_name(G.main, tselem->id->name);
479                         
480                         switch (GS(tselem->id->name)) {
481                                 case ID_MA:
482                                         WM_event_add_notifier(C, NC_MATERIAL, NULL); break;
483                                 case ID_TE:
484                                         WM_event_add_notifier(C, NC_TEXTURE, NULL); break;
485                                 case ID_IM:
486                                         WM_event_add_notifier(C, NC_IMAGE, NULL); break;
487                                 case ID_SCE:
488                                         WM_event_add_notifier(C, NC_SCENE, NULL); break;
489                                 default:
490                                         WM_event_add_notifier(C, NC_ID | NA_RENAME, NULL); break;
491                         }
492                         /* Check the library target exists */
493                         if (te->idcode == ID_LI) {
494                                 Library *lib = (Library *)tselem->id;
495                                 char expanded[FILE_MAX];
496
497                                 BKE_library_filepath_set(lib, lib->name);
498
499                                 BLI_strncpy(expanded, lib->name, sizeof(expanded));
500                                 BLI_path_abs(expanded, G.main->name);
501                                 if (!BLI_exists(expanded)) {
502                                         BKE_reportf(CTX_wm_reports(C), RPT_ERROR,
503                                                     "Library path '%s' does not exist, correct this before saving", expanded);
504                                 }
505                                 else if (lib->id.tag & LIB_TAG_MISSING) {
506                                         BKE_reportf(CTX_wm_reports(C), RPT_INFO,
507                                                     "Library path '%s' is now valid, please reload the library", expanded);
508                                         lib->id.tag &= ~LIB_TAG_MISSING;
509                                 }
510                         }
511                 }
512                 else {
513                         switch (tselem->type) {
514                                 case TSE_DEFGROUP:
515                                         defgroup_unique_name(te->directdata, (Object *)tselem->id); //  id = object
516                                         break;
517                                 case TSE_NLA_ACTION:
518                                         BLI_libblock_ensure_unique_name(G.main, tselem->id->name);
519                                         break;
520                                 case TSE_EBONE:
521                                 {
522                                         bArmature *arm = (bArmature *)tselem->id;
523                                         if (arm->edbo) {
524                                                 EditBone *ebone = te->directdata;
525                                                 char newname[sizeof(ebone->name)];
526
527                                                 /* restore bone name */
528                                                 BLI_strncpy(newname, ebone->name, sizeof(ebone->name));
529                                                 BLI_strncpy(ebone->name, oldname, sizeof(ebone->name));
530                                                 ED_armature_bone_rename(obedit->data, oldname, newname);
531                                                 WM_event_add_notifier(C, NC_OBJECT | ND_POSE, OBACT);
532                                         }
533                                         break;
534                                 }
535
536                                 case TSE_BONE:
537                                 {
538                                         Bone *bone = te->directdata;
539                                         Object *ob;
540                                         char newname[sizeof(bone->name)];
541                                         
542                                         /* always make current object active */
543                                         tree_element_active(C, scene, soops, te, OL_SETSEL_NORMAL, true);
544                                         ob = OBACT;
545                                         
546                                         /* restore bone name */
547                                         BLI_strncpy(newname, bone->name, sizeof(bone->name));
548                                         BLI_strncpy(bone->name, oldname, sizeof(bone->name));
549                                         ED_armature_bone_rename(ob->data, oldname, newname);
550                                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
551                                         break;
552                                 }
553                                 case TSE_POSE_CHANNEL:
554                                 {
555                                         bPoseChannel *pchan = te->directdata;
556                                         Object *ob;
557                                         char newname[sizeof(pchan->name)];
558                                         
559                                         /* always make current pose-bone active */
560                                         tree_element_active(C, scene, soops, te, OL_SETSEL_NORMAL, true);
561                                         ob = OBACT;
562
563                                         BLI_assert(ob->type == OB_ARMATURE);
564                                         
565                                         /* restore bone name */
566                                         BLI_strncpy(newname, pchan->name, sizeof(pchan->name));
567                                         BLI_strncpy(pchan->name, oldname, sizeof(pchan->name));
568                                         ED_armature_bone_rename(ob->data, oldname, newname);
569                                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
570                                         break;
571                                 }
572                                 case TSE_POSEGRP:
573                                 {
574                                         Object *ob = (Object *)tselem->id; // id = object
575                                         bActionGroup *grp = te->directdata;
576                                         
577                                         BLI_uniquename(&ob->pose->agroups, grp, CTX_DATA_(BLT_I18NCONTEXT_ID_ACTION, "Group"), '.',
578                                                        offsetof(bActionGroup, name), sizeof(grp->name));
579                                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
580                                         break;
581                                 }
582                                 case TSE_GP_LAYER:
583                                 {
584                                         bGPdata *gpd = (bGPdata *)tselem->id; // id = GP Datablock
585                                         bGPDlayer *gpl = te->directdata;
586                                         
587                                         // XXX: name needs translation stuff
588                                         BLI_uniquename(&gpd->layers, gpl, "GP Layer", '.',
589                                                        offsetof(bGPDlayer, info), sizeof(gpl->info));
590                                         WM_event_add_notifier(C, NC_GPENCIL | ND_DATA, gpd);
591                                         break;
592                                 }
593                                 case TSE_R_LAYER:
594                                         break;
595                         }
596                 }
597                 tselem->flag &= ~TSE_TEXTBUT;
598         }
599 }
600
601 static void outliner_draw_restrictbuts(uiBlock *block, Scene *scene, ARegion *ar, SpaceOops *soops, ListBase *lb)
602 {       
603         uiBut *bt;
604         TreeElement *te;
605         TreeStoreElem *tselem;
606         Object *ob = NULL;
607         Group  *gr = NULL;
608
609         PropertyRNA *object_prop_hide, *object_prop_hide_select, *object_prop_hide_render;
610
611         /* get RNA properties (once) */
612         object_prop_hide = RNA_struct_type_find_property(&RNA_Object, "hide");
613         object_prop_hide_select = RNA_struct_type_find_property(&RNA_Object, "hide_select");
614         object_prop_hide_render = RNA_struct_type_find_property(&RNA_Object, "hide_render");
615         BLI_assert(object_prop_hide && object_prop_hide_select  && object_prop_hide_render);
616
617
618         for (te = lb->first; te; te = te->next) {
619                 tselem = TREESTORE(te);
620                 if (te->ys + 2 * UI_UNIT_Y >= ar->v2d.cur.ymin && te->ys <= ar->v2d.cur.ymax) {
621                         /* objects have toggle-able restriction flags */
622                         if (tselem->type == 0 && te->idcode == ID_OB) {
623                                 PointerRNA ptr;
624
625                                 ob = (Object *)tselem->id;
626                                 RNA_pointer_create((ID *)ob, &RNA_Object, ob, &ptr);
627
628                                 UI_block_emboss_set(block, UI_EMBOSS_NONE);
629                                 bt = uiDefIconButR_prop(block, UI_BTYPE_ICON_TOGGLE, 0, ICON_RESTRICT_VIEW_OFF,
630                                                         (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X, UI_UNIT_Y,
631                                                         &ptr, object_prop_hide, -1, 0, 0, -1, -1,
632                                                         TIP_("Restrict viewport visibility (Ctrl - Recursive)"));
633                                 UI_but_func_set(bt, restrictbutton_view_cb, scene, ob);
634                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
635                                 
636                                 bt = uiDefIconButR_prop(block, UI_BTYPE_ICON_TOGGLE, 0, ICON_RESTRICT_SELECT_OFF,
637                                                         (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X, UI_UNIT_Y,
638                                                         &ptr, object_prop_hide_select, -1, 0, 0, -1, -1,
639                                                         TIP_("Restrict viewport selection (Ctrl - Recursive)"));
640                                 UI_but_func_set(bt, restrictbutton_sel_cb, scene, ob);
641                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
642                                 
643                                 bt = uiDefIconButR_prop(block, UI_BTYPE_ICON_TOGGLE, 0, ICON_RESTRICT_RENDER_OFF,
644                                                         (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX), te->ys, UI_UNIT_X, UI_UNIT_Y,
645                                                         &ptr, object_prop_hide_render, -1, 0, 0, -1, -1,
646                                                         TIP_("Restrict rendering (Ctrl - Recursive)"));
647                                 UI_but_func_set(bt, restrictbutton_rend_cb, scene, ob);
648                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
649                                 
650                                 UI_block_emboss_set(block, UI_EMBOSS);
651                                 
652                         }
653                         if (tselem->type == 0 && te->idcode == ID_GR) {
654                                 int restrict_bool;
655                                 int but_flag = UI_BUT_DRAG_LOCK;
656                                 gr = (Group *)tselem->id;
657
658                                 if (ID_IS_LINKED_DATABLOCK(gr))
659                                         but_flag |= UI_BUT_DISABLED;
660                                 
661                                 UI_block_emboss_set(block, UI_EMBOSS_NONE);
662
663                                 restrict_bool = group_restrict_flag(gr, OB_RESTRICT_VIEW);
664                                 bt = uiDefIconBut(block, UI_BTYPE_ICON_TOGGLE, 0, restrict_bool ? ICON_RESTRICT_VIEW_ON : ICON_RESTRICT_VIEW_OFF,
665                                                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X, UI_UNIT_Y,
666                                                   NULL, 0, 0, 0, 0, TIP_("Restrict/Allow visibility in the 3D View"));
667                                 UI_but_func_set(bt, restrictbutton_gr_restrict_view, scene, gr);
668                                 UI_but_flag_enable(bt, but_flag);
669
670                                 restrict_bool = group_restrict_flag(gr, OB_RESTRICT_SELECT);
671                                 bt = uiDefIconBut(block, UI_BTYPE_ICON_TOGGLE, 0, restrict_bool ? ICON_RESTRICT_SELECT_ON : ICON_RESTRICT_SELECT_OFF,
672                                                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X, UI_UNIT_Y,
673                                                   NULL, 0, 0, 0, 0, TIP_("Restrict/Allow selection in the 3D View"));
674                                 UI_but_func_set(bt, restrictbutton_gr_restrict_select, scene, gr);
675                                 UI_but_flag_enable(bt, but_flag);
676
677                                 restrict_bool = group_restrict_flag(gr, OB_RESTRICT_RENDER);
678                                 bt = uiDefIconBut(block, UI_BTYPE_ICON_TOGGLE, 0, restrict_bool ? ICON_RESTRICT_RENDER_ON : ICON_RESTRICT_RENDER_OFF,
679                                                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX), te->ys, UI_UNIT_X, UI_UNIT_Y,
680                                                   NULL, 0, 0, 0, 0, TIP_("Restrict/Allow renderability"));
681                                 UI_but_func_set(bt, restrictbutton_gr_restrict_render, scene, gr);
682                                 UI_but_flag_enable(bt, but_flag);
683
684                                 UI_block_emboss_set(block, UI_EMBOSS);
685                         }
686                         /* scene render layers and passes have toggle-able flags too! */
687                         else if (tselem->type == TSE_R_LAYER) {
688                                 UI_block_emboss_set(block, UI_EMBOSS_NONE);
689                                 
690                                 bt = uiDefIconButBitI(block, UI_BTYPE_ICON_TOGGLE_N, SCE_LAY_DISABLE, 0, ICON_CHECKBOX_HLT - 1,
691                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
692                                                       UI_UNIT_Y, te->directdata, 0, 0, 0, 0, TIP_("Render this RenderLayer"));
693                                 UI_but_func_set(bt, restrictbutton_r_lay_cb, tselem->id, NULL);
694                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
695                                 
696                                 UI_block_emboss_set(block, UI_EMBOSS);
697                         }
698                         else if (tselem->type == TSE_R_PASS) {
699                                 int *layflag = te->directdata;
700                                 int passflag = 1 << tselem->nr;
701                                 
702                                 UI_block_emboss_set(block, UI_EMBOSS_NONE);
703                                 
704                                 
705                                 bt = uiDefIconButBitI(block, UI_BTYPE_ICON_TOGGLE, passflag, 0, ICON_CHECKBOX_HLT - 1,
706                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
707                                                       UI_UNIT_Y, layflag, 0, 0, 0, 0, TIP_("Render this Pass"));
708                                 UI_but_func_set(bt, restrictbutton_r_lay_cb, tselem->id, NULL);
709                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
710                                 
711                                 layflag++;  /* is lay_xor */
712                                 if (ELEM(passflag, SCE_PASS_SPEC, SCE_PASS_SHADOW, SCE_PASS_AO, SCE_PASS_REFLECT, SCE_PASS_REFRACT,
713                                           SCE_PASS_INDIRECT, SCE_PASS_EMIT, SCE_PASS_ENVIRONMENT))
714                                 {
715                                         bt = uiDefIconButBitI(block, UI_BTYPE_TOGGLE, passflag, 0, (*layflag & passflag) ? ICON_DOT : ICON_BLANK1,
716                                                               (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X,
717                                                               UI_UNIT_Y, layflag, 0, 0, 0, 0, TIP_("Exclude this Pass from Combined"));
718                                         UI_but_func_set(bt, restrictbutton_r_lay_cb, tselem->id, NULL);
719                                         UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
720                                 }
721                                 
722                                 UI_block_emboss_set(block, UI_EMBOSS);
723                         }
724                         else if (tselem->type == TSE_MODIFIER) {
725                                 ModifierData *md = (ModifierData *)te->directdata;
726                                 ob = (Object *)tselem->id;
727                                 
728                                 UI_block_emboss_set(block, UI_EMBOSS_NONE);
729                                 bt = uiDefIconButBitI(block, UI_BTYPE_ICON_TOGGLE_N, eModifierMode_Realtime, 0, ICON_RESTRICT_VIEW_OFF,
730                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
731                                                       UI_UNIT_Y, &(md->mode), 0, 0, 0, 0,
732                                                       TIP_("Restrict/Allow visibility in the 3D View"));
733                                 UI_but_func_set(bt, restrictbutton_modifier_cb, scene, ob);
734                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
735                                 
736                                 bt = uiDefIconButBitI(block, UI_BTYPE_ICON_TOGGLE_N, eModifierMode_Render, 0, ICON_RESTRICT_RENDER_OFF,
737                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX), te->ys, UI_UNIT_X,
738                                                       UI_UNIT_Y, &(md->mode), 0, 0, 0, 0, TIP_("Restrict/Allow renderability"));
739                                 UI_but_func_set(bt, restrictbutton_modifier_cb, scene, ob);
740                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
741
742                                 UI_block_emboss_set(block, UI_EMBOSS);
743                         }
744                         else if (tselem->type == TSE_POSE_CHANNEL) {
745                                 bPoseChannel *pchan = (bPoseChannel *)te->directdata;
746                                 Bone *bone = pchan->bone;
747                                 ob = (Object *)tselem->id;
748                                 
749                                 UI_block_emboss_set(block, UI_EMBOSS_NONE);
750                                 bt = uiDefIconButBitI(block, UI_BTYPE_ICON_TOGGLE, BONE_HIDDEN_P, 0, ICON_RESTRICT_VIEW_OFF,
751                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
752                                                       UI_UNIT_Y, &(bone->flag), 0, 0, 0, 0,
753                                                       TIP_("Restrict/Allow visibility in the 3D View"));
754                                 UI_but_func_set(bt, restrictbutton_bone_visibility_cb, ob->data, bone);
755                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
756                                 
757                                 bt = uiDefIconButBitI(block, UI_BTYPE_ICON_TOGGLE, BONE_UNSELECTABLE, 0, ICON_RESTRICT_SELECT_OFF,
758                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X,
759                                                       UI_UNIT_Y, &(bone->flag), 0, 0, 0, 0,
760                                                       TIP_("Restrict/Allow selection in the 3D View"));
761                                 UI_but_func_set(bt, restrictbutton_bone_select_cb, ob->data, bone);
762                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
763
764                                 UI_block_emboss_set(block, UI_EMBOSS);
765                         }
766                         else if (tselem->type == TSE_EBONE) {
767                                 EditBone *ebone = (EditBone *)te->directdata;
768                                 
769                                 UI_block_emboss_set(block, UI_EMBOSS_NONE);
770                                 bt = uiDefIconButBitI(block, UI_BTYPE_ICON_TOGGLE, BONE_HIDDEN_A, 0, ICON_RESTRICT_VIEW_OFF,
771                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
772                                                       UI_UNIT_Y, &(ebone->flag), 0, 0, 0, 0,
773                                                       TIP_("Restrict/Allow visibility in the 3D View"));
774                                 UI_but_func_set(bt, restrictbutton_ebone_visibility_cb, NULL, ebone);
775                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
776                                 
777                                 bt = uiDefIconButBitI(block, UI_BTYPE_ICON_TOGGLE, BONE_UNSELECTABLE, 0, ICON_RESTRICT_SELECT_OFF,
778                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X,
779                                                       UI_UNIT_Y, &(ebone->flag), 0, 0, 0, 0,
780                                                       TIP_("Restrict/Allow selection in the 3D View"));
781                                 UI_but_func_set(bt, restrictbutton_ebone_select_cb, NULL, ebone);
782                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
783
784                                 UI_block_emboss_set(block, UI_EMBOSS);
785                         }
786                         else if (tselem->type == TSE_GP_LAYER) {
787                                 bGPDlayer *gpl = (bGPDlayer *)te->directdata;
788                                 
789                                 UI_block_emboss_set(block, UI_EMBOSS_NONE);
790                                 
791                                 bt = uiDefIconButBitS(block, UI_BTYPE_ICON_TOGGLE, GP_LAYER_HIDE, 0, ICON_RESTRICT_VIEW_OFF,
792                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
793                                                       UI_UNIT_Y, &gpl->flag, 0, 0, 0, 0,
794                                                       TIP_("Restrict/Allow visibility in the 3D View"));
795                                 UI_but_func_set(bt, restrictbutton_gp_layer_flag_cb, NULL, gpl);
796                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
797                                 
798                                 bt = uiDefIconButBitS(block, UI_BTYPE_ICON_TOGGLE, GP_LAYER_LOCKED, 0, ICON_UNLOCKED,
799                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X,
800                                                       UI_UNIT_Y, &gpl->flag, 0, 0, 0, 0,
801                                                       TIP_("Restrict/Allow editing of strokes and keyframes in this layer"));
802                                 UI_but_func_set(bt, restrictbutton_gp_layer_flag_cb, NULL, gpl);
803                                 UI_but_flag_enable(bt, UI_BUT_DRAG_LOCK);
804                                 
805                                 /* TODO: visibility in renders */
806                                 
807                                 UI_block_emboss_set(block, UI_EMBOSS);
808                         }
809                 }
810                 
811                 if (TSELEM_OPEN(tselem, soops)) outliner_draw_restrictbuts(block, scene, ar, soops, &te->subtree);
812         }
813 }
814
815 static void outliner_draw_userbuts(uiBlock *block, ARegion *ar, SpaceOops *soops, ListBase *lb)
816 {
817         uiBut *bt;
818         TreeElement *te;
819         TreeStoreElem *tselem;
820
821         for (te = lb->first; te; te = te->next) {
822                 tselem = TREESTORE(te);
823                 if (te->ys + 2 * UI_UNIT_Y >= ar->v2d.cur.ymin && te->ys <= ar->v2d.cur.ymax) {
824                         if (tselem->type == 0) {
825                                 ID *id = tselem->id;
826                                 const char *tip = NULL;
827                                 int icon = ICON_NONE;
828                                 char buf[16] = "";
829                                 int but_flag = UI_BUT_DRAG_LOCK;
830
831                                 if (ID_IS_LINKED_DATABLOCK(id))
832                                         but_flag |= UI_BUT_DISABLED;
833
834                                 UI_block_emboss_set(block, UI_EMBOSS_NONE);
835
836                                 if (id->flag & LIB_FAKEUSER) {
837                                         icon = ICON_FILE_TICK;
838                                         tip  = TIP_("Datablock will be retained using a fake user");
839                                 }
840                                 else {
841                                         icon = ICON_X;
842                                         tip  = TIP_("Datablock has no users and will be deleted");
843                                 }
844                                 bt = uiDefIconButBitS(block, UI_BTYPE_TOGGLE, LIB_FAKEUSER, 1, icon,
845                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X, UI_UNIT_Y,
846                                                       &id->flag, 0, 0, 0, 0, tip);
847                                 UI_but_func_set(bt, restrictbutton_id_user_toggle, id, NULL);
848                                 UI_but_flag_enable(bt, but_flag);
849                                 
850                                 
851                                 BLI_str_format_int_grouped(buf, id->us);
852                                 bt = uiDefBut(block, UI_BTYPE_BUT, 1, buf, 
853                                               (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, 
854                                               UI_UNIT_X, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0,
855                                               TIP_("Number of users of this datablock"));
856                                 UI_but_flag_enable(bt, but_flag);
857                                 
858                                 
859                                 bt = uiDefButBitS(block, UI_BTYPE_TOGGLE, LIB_FAKEUSER, 1, (id->flag & LIB_FAKEUSER) ? "F" : " ",
860                                                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX), te->ys, UI_UNIT_X, UI_UNIT_Y,
861                                                   &id->flag, 0, 0, 0, 0,
862                                                   TIP_("Datablock has a 'fake' user which will keep it in the file "
863                                                        "even if nothing else uses it"));
864                                 UI_but_func_set(bt, restrictbutton_id_user_toggle, id, NULL);
865                                 UI_but_flag_enable(bt, but_flag);
866                                 
867                                 UI_block_emboss_set(block, UI_EMBOSS);
868                         }
869                 }
870                 
871                 if (TSELEM_OPEN(tselem, soops)) outliner_draw_userbuts(block, ar, soops, &te->subtree);
872         }
873 }
874
875 static void outliner_draw_rnacols(ARegion *ar, int sizex)
876 {
877         View2D *v2d = &ar->v2d;
878
879         float miny = v2d->cur.ymin;
880         if (miny < v2d->tot.ymin) miny = v2d->tot.ymin;
881
882         UI_ThemeColorShadeAlpha(TH_BACK, -15, -200);
883
884         /* draw column separator lines */
885         fdrawline((float)sizex,
886                   v2d->cur.ymax,
887                   (float)sizex,
888                   miny);
889
890         fdrawline((float)sizex + OL_RNA_COL_SIZEX,
891                   v2d->cur.ymax,
892                   (float)sizex + OL_RNA_COL_SIZEX,
893                   miny);
894 }
895
896 static void outliner_draw_rnabuts(uiBlock *block, Scene *scene, ARegion *ar, SpaceOops *soops, int sizex, ListBase *lb)
897 {
898         TreeElement *te;
899         TreeStoreElem *tselem;
900         PointerRNA *ptr;
901         PropertyRNA *prop;
902
903         for (te = lb->first; te; te = te->next) {
904                 tselem = TREESTORE(te);
905                 if (te->ys + 2 * UI_UNIT_Y >= ar->v2d.cur.ymin && te->ys <= ar->v2d.cur.ymax) {
906                         if (tselem->type == TSE_RNA_PROPERTY) {
907                                 ptr = &te->rnaptr;
908                                 prop = te->directdata;
909
910                                 if (!TSELEM_OPEN(tselem, soops)) {
911                                         if (RNA_property_type(prop) == PROP_POINTER) {
912                                                 uiBut *but = uiDefAutoButR(block, ptr, prop, -1, "", ICON_NONE, sizex, te->ys,
913                                                                            OL_RNA_COL_SIZEX, UI_UNIT_Y - 1);
914                                                 UI_but_flag_enable(but, UI_BUT_DISABLED);
915                                         }
916                                         else if (RNA_property_type(prop) == PROP_ENUM) {
917                                                 uiDefAutoButR(block, ptr, prop, -1, NULL, ICON_NONE, sizex, te->ys, OL_RNA_COL_SIZEX,
918                                                               UI_UNIT_Y - 1);
919                                         }
920                                         else {
921                                                 uiDefAutoButR(block, ptr, prop, -1, "", ICON_NONE, sizex, te->ys, OL_RNA_COL_SIZEX,
922                                                               UI_UNIT_Y - 1);
923                                         }
924                                 }
925                         }
926                         else if (tselem->type == TSE_RNA_ARRAY_ELEM) {
927                                 ptr = &te->rnaptr;
928                                 prop = te->directdata;
929                                 
930                                 uiDefAutoButR(block, ptr, prop, te->index, "", ICON_NONE, sizex, te->ys, OL_RNA_COL_SIZEX,
931                                               UI_UNIT_Y - 1);
932                         }
933                 }
934                 
935                 if (TSELEM_OPEN(tselem, soops)) outliner_draw_rnabuts(block, scene, ar, soops, sizex, &te->subtree);
936         }
937
938         UI_block_emboss_set(block, UI_EMBOSS);
939 }
940
941 static void outliner_buttons(const bContext *C, uiBlock *block, ARegion *ar, TreeElement *te)
942 {
943         uiBut *bt;
944         TreeStoreElem *tselem;
945         int spx, dx, len;
946
947         tselem = TREESTORE(te);
948
949         BLI_assert(tselem->flag & TSE_TEXTBUT);
950         /* If we add support to rename Sequence.
951          * need change this.
952          */
953
954         if (tselem->type == TSE_EBONE) len = sizeof(((EditBone *) 0)->name);
955         else if (tselem->type == TSE_MODIFIER) len = sizeof(((ModifierData *) 0)->name);
956         else if (tselem->id && GS(tselem->id->name) == ID_LI) len = sizeof(((Library *) 0)->name);
957         else len = MAX_ID_NAME - 2;
958
959         spx = te->xs + 1.8f * UI_UNIT_X;
960         dx = ar->v2d.cur.xmax - (spx + 3.2f * UI_UNIT_X);
961
962         bt = uiDefBut(block, UI_BTYPE_TEXT, OL_NAMEBUTTON, "", spx, te->ys, dx, UI_UNIT_Y - 1, (void *)te->name,
963                       1.0, (float)len, 0, 0, "");
964         UI_but_func_rename_set(bt, namebutton_cb, tselem);
965
966         /* returns false if button got removed */
967         if (false == UI_but_active_only(C, ar, block, bt)) {
968                 tselem->flag &= ~TSE_TEXTBUT;
969
970                 /* bad! (notifier within draw) without this, we don't get a refesh */
971                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
972         }
973 }
974
975 /* ****************************************************** */
976 /* Normal Drawing... */
977
978 /* make function calls a bit compacter */
979 struct DrawIconArg {
980         uiBlock *block;
981         ID *id;
982         float xmax, x, y, xb, yb;
983         float alpha;
984 };
985
986 static void tselem_draw_icon_uibut(struct DrawIconArg *arg, int icon)
987 {
988         /* restrict column clip... it has been coded by simply overdrawing, doesnt work for buttons */
989         if (arg->x >= arg->xmax) {
990                 glEnable(GL_BLEND);
991                 UI_icon_draw_aspect(arg->x, arg->y, icon, 1.0f / UI_DPI_FAC, arg->alpha);
992                 glDisable(GL_BLEND);
993         }
994         else {
995                 uiBut *but = uiDefIconBut(arg->block, UI_BTYPE_LABEL, 0, icon, arg->xb, arg->yb, UI_UNIT_X, UI_UNIT_Y, NULL,
996                                           0.0, 0.0, 1.0, arg->alpha,
997                                           (arg->id && ID_IS_LINKED_DATABLOCK(arg->id)) ? arg->id->lib->name : "");
998                 
999                 if (arg->id)
1000                         UI_but_drag_set_id(but, arg->id);
1001         }
1002
1003 }
1004
1005 static void tselem_draw_gp_icon_uibut(struct DrawIconArg *arg, ID *id, bGPDlayer *gpl)
1006 {
1007         /* restrict column clip - skip it for now... */
1008         if (arg->x >= arg->xmax) {
1009                 /* pass */
1010         }
1011         else {
1012                 PointerRNA ptr;
1013                 const float eps = 0.001f;
1014                 const bool is_stroke_visible = (gpl->color[3] > eps);
1015                 const bool is_fill_visible = (gpl->fill[3] > eps);
1016                 float w = 0.5f  * UI_UNIT_X;
1017                 float h = 0.85f * UI_UNIT_Y;
1018
1019                 RNA_pointer_create(id, &RNA_GPencilLayer, gpl, &ptr);
1020
1021                 UI_block_align_begin(arg->block);
1022                 
1023                 UI_block_emboss_set(arg->block, is_stroke_visible ? UI_EMBOSS : UI_EMBOSS_NONE);
1024                 uiDefButR(arg->block, UI_BTYPE_COLOR, 1, "", arg->xb, arg->yb, w, h,
1025                           &ptr, "color", -1,
1026                           0, 0, 0, 0, NULL);
1027                 
1028                 UI_block_emboss_set(arg->block, is_fill_visible ? UI_EMBOSS : UI_EMBOSS_NONE);
1029                 uiDefButR(arg->block, UI_BTYPE_COLOR, 1, "", arg->xb + w, arg->yb, w, h,
1030                           &ptr, "fill_color", -1,
1031                           0, 0, 0, 0, NULL);
1032                 
1033                 UI_block_emboss_set(arg->block, UI_EMBOSS_NONE);
1034                 UI_block_align_end(arg->block);
1035         }
1036 }
1037
1038 static void tselem_draw_icon(uiBlock *block, int xmax, float x, float y, TreeStoreElem *tselem, TreeElement *te,
1039                              float alpha)
1040 {
1041         struct DrawIconArg arg;
1042         float aspect;
1043         
1044         /* icons tiny bit away from text */
1045         x -= 0.15f * UI_UNIT_Y;
1046         
1047         /* make function calls a bit compacter */
1048         arg.block = block;
1049         arg.id = tselem->id;
1050         arg.xmax = xmax;
1051         arg.xb = x;     /* for ui buttons */
1052         arg.yb = y;
1053         arg.alpha = alpha;
1054         
1055         /* placement of icons, copied from interface_widgets.c */
1056         aspect = (0.8f * UI_UNIT_Y) / ICON_DEFAULT_HEIGHT;
1057         arg.x = x = x + 4.0f * aspect;
1058         arg.y = y = y + 0.1f * UI_UNIT_Y;
1059
1060         if (tselem->type) {
1061                 switch (tselem->type) {
1062                         case TSE_ANIM_DATA:
1063                                 UI_icon_draw(x, y, ICON_ANIM_DATA); break; // xxx
1064                         case TSE_NLA:
1065                                 UI_icon_draw(x, y, ICON_NLA); break;
1066                         case TSE_NLA_TRACK:
1067                                 UI_icon_draw(x, y, ICON_NLA); break; // XXX
1068                         case TSE_NLA_ACTION:
1069                                 UI_icon_draw(x, y, ICON_ACTION); break;
1070                         case TSE_DRIVER_BASE:
1071                                 UI_icon_draw(x, y, ICON_DRIVER); break;
1072                         case TSE_DEFGROUP_BASE:
1073                                 UI_icon_draw(x, y, ICON_GROUP_VERTEX); break;
1074                         case TSE_BONE:
1075                         case TSE_EBONE:
1076                                 UI_icon_draw(x, y, ICON_BONE_DATA); break;
1077                         case TSE_CONSTRAINT_BASE:
1078                                 UI_icon_draw(x, y, ICON_CONSTRAINT); break;
1079                         case TSE_MODIFIER_BASE:
1080                                 UI_icon_draw(x, y, ICON_MODIFIER); break;
1081                         case TSE_LINKED_OB:
1082                                 UI_icon_draw(x, y, ICON_OBJECT_DATA); break;
1083                         case TSE_MODIFIER:
1084                         {
1085                                 Object *ob = (Object *)tselem->id;
1086                                 ModifierData *md = BLI_findlink(&ob->modifiers, tselem->nr);
1087                                 switch ((ModifierType)md->type) {
1088                                         case eModifierType_Subsurf: 
1089                                                 UI_icon_draw(x, y, ICON_MOD_SUBSURF); break;
1090                                         case eModifierType_Armature: 
1091                                                 UI_icon_draw(x, y, ICON_MOD_ARMATURE); break;
1092                                         case eModifierType_Lattice: 
1093                                                 UI_icon_draw(x, y, ICON_MOD_LATTICE); break;
1094                                         case eModifierType_Curve: 
1095                                                 UI_icon_draw(x, y, ICON_MOD_CURVE); break;
1096                                         case eModifierType_Build: 
1097                                                 UI_icon_draw(x, y, ICON_MOD_BUILD); break;
1098                                         case eModifierType_Mirror: 
1099                                                 UI_icon_draw(x, y, ICON_MOD_MIRROR); break;
1100                                         case eModifierType_Decimate: 
1101                                                 UI_icon_draw(x, y, ICON_MOD_DECIM); break;
1102                                         case eModifierType_Wave: 
1103                                                 UI_icon_draw(x, y, ICON_MOD_WAVE); break;
1104                                         case eModifierType_Hook: 
1105                                                 UI_icon_draw(x, y, ICON_HOOK); break;
1106                                         case eModifierType_Softbody: 
1107                                                 UI_icon_draw(x, y, ICON_MOD_SOFT); break;
1108                                         case eModifierType_Boolean: 
1109                                                 UI_icon_draw(x, y, ICON_MOD_BOOLEAN); break;
1110                                         case eModifierType_EdgeSplit:
1111                                                 UI_icon_draw(x, y, ICON_MOD_EDGESPLIT); break;
1112                                         case eModifierType_Array:
1113                                                 UI_icon_draw(x, y, ICON_MOD_ARRAY); break;
1114                                         case eModifierType_UVProject:
1115                                         case eModifierType_UVWarp:  /* TODO, get own icon */
1116                                                 UI_icon_draw(x, y, ICON_MOD_UVPROJECT); break;
1117                                         case eModifierType_Displace:
1118                                                 UI_icon_draw(x, y, ICON_MOD_DISPLACE); break;
1119                                         case eModifierType_Shrinkwrap:
1120                                                 UI_icon_draw(x, y, ICON_MOD_SHRINKWRAP); break;
1121                                         case eModifierType_Cast:
1122                                                 UI_icon_draw(x, y, ICON_MOD_CAST); break;
1123                                         case eModifierType_MeshDeform:
1124                                                 UI_icon_draw(x, y, ICON_MOD_MESHDEFORM); break;
1125                                         case eModifierType_Bevel:
1126                                                 UI_icon_draw(x, y, ICON_MOD_BEVEL); break;
1127                                         case eModifierType_Smooth:
1128                                         case eModifierType_LaplacianSmooth:
1129                                         case eModifierType_CorrectiveSmooth:
1130                                                 UI_icon_draw(x, y, ICON_MOD_SMOOTH); break;
1131                                         case eModifierType_SimpleDeform:
1132                                                 UI_icon_draw(x, y, ICON_MOD_SIMPLEDEFORM); break;
1133                                         case eModifierType_Mask:
1134                                                 UI_icon_draw(x, y, ICON_MOD_MASK); break;
1135                                         case eModifierType_Cloth:
1136                                                 UI_icon_draw(x, y, ICON_MOD_CLOTH); break;
1137                                         case eModifierType_Explode:
1138                                                 UI_icon_draw(x, y, ICON_MOD_EXPLODE); break;
1139                                         case eModifierType_Collision:
1140                                         case eModifierType_Surface:
1141                                                 UI_icon_draw(x, y, ICON_MOD_PHYSICS); break;
1142                                         case eModifierType_Fluidsim:
1143                                                 UI_icon_draw(x, y, ICON_MOD_FLUIDSIM); break;
1144                                         case eModifierType_Multires:
1145                                                 UI_icon_draw(x, y, ICON_MOD_MULTIRES); break;
1146                                         case eModifierType_Smoke:
1147                                                 UI_icon_draw(x, y, ICON_MOD_SMOKE); break;
1148                                         case eModifierType_Solidify:
1149                                                 UI_icon_draw(x, y, ICON_MOD_SOLIDIFY); break;
1150                                         case eModifierType_Screw:
1151                                                 UI_icon_draw(x, y, ICON_MOD_SCREW); break;
1152                                         case eModifierType_Remesh:
1153                                                 UI_icon_draw(x, y, ICON_MOD_REMESH); break;
1154                                         case eModifierType_WeightVGEdit:
1155                                         case eModifierType_WeightVGMix:
1156                                         case eModifierType_WeightVGProximity:
1157                                                 UI_icon_draw(x, y, ICON_MOD_VERTEX_WEIGHT); break;
1158                                         case eModifierType_DynamicPaint:
1159                                                 UI_icon_draw(x, y, ICON_MOD_DYNAMICPAINT); break;
1160                                         case eModifierType_Ocean:
1161                                                 UI_icon_draw(x, y, ICON_MOD_OCEAN); break;
1162                                         case eModifierType_Warp:
1163                                                 UI_icon_draw(x, y, ICON_MOD_WARP); break;
1164                                         case eModifierType_Skin:
1165                                                 UI_icon_draw(x, y, ICON_MOD_SKIN); break;
1166                                         case eModifierType_Triangulate:
1167                                                 UI_icon_draw(x, y, ICON_MOD_TRIANGULATE); break;
1168                                         case eModifierType_MeshCache:
1169                                                 UI_icon_draw(x, y, ICON_MOD_MESHDEFORM); break;  /* XXX, needs own icon */
1170                                         case eModifierType_Wireframe:
1171                                                 UI_icon_draw(x, y, ICON_MOD_WIREFRAME); break;
1172                                         case eModifierType_LaplacianDeform:
1173                                                 UI_icon_draw(x, y, ICON_MOD_MESHDEFORM); break;  /* XXX, needs own icon */
1174                                         case eModifierType_DataTransfer:
1175                                                 UI_icon_draw(x, y, ICON_MOD_DATA_TRANSFER); break;
1176                                         case eModifierType_NormalEdit:
1177                                                 UI_icon_draw(x, y, ICON_MOD_NORMALEDIT); break;
1178                                         /* Default */
1179                                         case eModifierType_None:
1180                                         case eModifierType_ShapeKey:
1181                                         case NUM_MODIFIER_TYPES:
1182                                                 UI_icon_draw(x, y, ICON_DOT); break;
1183                                 }
1184                                 break;
1185                         }
1186                         case TSE_POSE_BASE:
1187                                 UI_icon_draw(x, y, ICON_ARMATURE_DATA); break;
1188                         case TSE_POSE_CHANNEL:
1189                                 UI_icon_draw(x, y, ICON_BONE_DATA); break;
1190                         case TSE_PROXY:
1191                                 UI_icon_draw(x, y, ICON_GHOST); break;
1192                         case TSE_R_LAYER_BASE:
1193                                 UI_icon_draw(x, y, ICON_RENDERLAYERS); break;
1194                         case TSE_R_LAYER:
1195                                 UI_icon_draw(x, y, ICON_RENDERLAYERS); break;
1196                         case TSE_LINKED_LAMP:
1197                                 UI_icon_draw(x, y, ICON_LAMP_DATA); break;
1198                         case TSE_LINKED_MAT:
1199                                 UI_icon_draw(x, y, ICON_MATERIAL_DATA); break;
1200                         case TSE_POSEGRP_BASE:
1201                                 UI_icon_draw(x, y, ICON_GROUP_BONE); break;
1202                         case TSE_SEQUENCE:
1203                                 if (te->idcode == SEQ_TYPE_MOVIE)
1204                                         UI_icon_draw(x, y, ICON_SEQUENCE);
1205                                 else if (te->idcode == SEQ_TYPE_META)
1206                                         UI_icon_draw(x, y, ICON_DOT);
1207                                 else if (te->idcode == SEQ_TYPE_SCENE)
1208                                         UI_icon_draw(x, y, ICON_SCENE);
1209                                 else if (te->idcode == SEQ_TYPE_SOUND_RAM)
1210                                         UI_icon_draw(x, y, ICON_SOUND);
1211                                 else if (te->idcode == SEQ_TYPE_IMAGE)
1212                                         UI_icon_draw(x, y, ICON_IMAGE_COL);
1213                                 else
1214                                         UI_icon_draw(x, y, ICON_PARTICLES);
1215                                 break;
1216                         case TSE_SEQ_STRIP:
1217                                 UI_icon_draw(x, y, ICON_LIBRARY_DATA_DIRECT);
1218                                 break;
1219                         case TSE_SEQUENCE_DUP:
1220                                 UI_icon_draw(x, y, ICON_OBJECT_DATA);
1221                                 break;
1222                         case TSE_RNA_STRUCT:
1223                                 if (RNA_struct_is_ID(te->rnaptr.type)) {
1224                                         arg.id = (ID *)te->rnaptr.data;
1225                                         tselem_draw_icon_uibut(&arg, RNA_struct_ui_icon(te->rnaptr.type));
1226                                 }
1227                                 else
1228                                         UI_icon_draw(x, y, RNA_struct_ui_icon(te->rnaptr.type));
1229                                 break;
1230                         case TSE_GP_LAYER:
1231                                 tselem_draw_gp_icon_uibut(&arg, tselem->id, te->directdata);
1232                                 break;
1233                         default:
1234                                 UI_icon_draw(x, y, ICON_DOT); break;
1235                 }
1236         }
1237         else if (GS(tselem->id->name) == ID_OB) {
1238                 Object *ob = (Object *)tselem->id;
1239                 switch (ob->type) {
1240                         case OB_LAMP:
1241                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_LAMP); break;
1242                         case OB_MESH: 
1243                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_MESH); break;
1244                         case OB_CAMERA: 
1245                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_CAMERA); break;
1246                         case OB_CURVE: 
1247                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_CURVE); break;
1248                         case OB_MBALL: 
1249                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_META); break;
1250                         case OB_LATTICE: 
1251                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_LATTICE); break;
1252                         case OB_ARMATURE: 
1253                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_ARMATURE); break;
1254                         case OB_FONT: 
1255                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_FONT); break;
1256                         case OB_SURF: 
1257                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_SURFACE); break;
1258                         case OB_SPEAKER:
1259                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_SPEAKER); break;
1260                         case OB_EMPTY: 
1261                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_EMPTY); break;
1262                 
1263                 }
1264         }
1265         else {
1266                 switch (GS(tselem->id->name)) {
1267                         case ID_SCE:
1268                                 tselem_draw_icon_uibut(&arg, ICON_SCENE_DATA); break;
1269                         case ID_ME:
1270                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_MESH); break;
1271                         case ID_CU:
1272                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_CURVE); break;
1273                         case ID_MB:
1274                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_META); break;
1275                         case ID_LT:
1276                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_LATTICE); break;
1277                         case ID_LA:
1278                         {
1279                                 Lamp *la = (Lamp *)tselem->id;
1280                                 
1281                                 switch (la->type) {
1282                                         case LA_LOCAL:
1283                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_POINT); break;
1284                                         case LA_SUN:
1285                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_SUN); break;
1286                                         case LA_SPOT:
1287                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_SPOT); break;
1288                                         case LA_HEMI:
1289                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_HEMI); break;
1290                                         case LA_AREA:
1291                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_AREA); break;
1292                                         default:
1293                                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_LAMP); break;
1294                                 }
1295                                 break;
1296                         }
1297                         case ID_MA:
1298                                 tselem_draw_icon_uibut(&arg, ICON_MATERIAL_DATA); break;
1299                         case ID_TE:
1300                                 tselem_draw_icon_uibut(&arg, ICON_TEXTURE_DATA); break;
1301                         case ID_IM:
1302                                 tselem_draw_icon_uibut(&arg, ICON_IMAGE_DATA); break;
1303                         case ID_SPK:
1304                         case ID_SO:
1305                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_SPEAKER); break;
1306                         case ID_AR:
1307                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_ARMATURE); break;
1308                         case ID_CA:
1309                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_CAMERA); break;
1310                         case ID_KE:
1311                                 tselem_draw_icon_uibut(&arg, ICON_SHAPEKEY_DATA); break;
1312                         case ID_WO:
1313                                 tselem_draw_icon_uibut(&arg, ICON_WORLD_DATA); break;
1314                         case ID_AC:
1315                                 tselem_draw_icon_uibut(&arg, ICON_ACTION); break;
1316                         case ID_NLA:
1317                                 tselem_draw_icon_uibut(&arg, ICON_NLA); break;
1318                         case ID_TXT:
1319                                 tselem_draw_icon_uibut(&arg, ICON_SCRIPT); break;
1320                         case ID_GR:
1321                                 tselem_draw_icon_uibut(&arg, ICON_GROUP); break;
1322                         case ID_LI:
1323                                 if (tselem->id->tag & LIB_TAG_MISSING) {
1324                                         tselem_draw_icon_uibut(&arg, ICON_LIBRARY_DATA_BROKEN);
1325                                 }
1326                                 else if (((Library *)tselem->id)->parent) {
1327                                         tselem_draw_icon_uibut(&arg, ICON_LIBRARY_DATA_INDIRECT);
1328                                 }
1329                                 else {
1330                                         tselem_draw_icon_uibut(&arg, ICON_LIBRARY_DATA_DIRECT);
1331                                 }
1332                                 break;
1333                         case ID_LS:
1334                                 tselem_draw_icon_uibut(&arg, ICON_LINE_DATA); break;
1335                         case ID_GD:
1336                                 tselem_draw_icon_uibut(&arg, ICON_GREASEPENCIL); break;
1337                 }
1338         }
1339 }
1340
1341 static void outliner_draw_iconrow(bContext *C, uiBlock *block, Scene *scene, SpaceOops *soops, ListBase *lb, int level,
1342                                   int xmax, int *offsx, int ys)
1343 {
1344         TreeElement *te;
1345         TreeStoreElem *tselem;
1346         eOLDrawState active;
1347
1348         for (te = lb->first; te; te = te->next) {
1349                 
1350                 /* exit drawing early */
1351                 if ((*offsx) - UI_UNIT_X > xmax)
1352                         break;
1353
1354                 tselem = TREESTORE(te);
1355                 
1356                 /* object hierarchy always, further constrained on level */
1357                 if (level < 1 || (tselem->type == 0 && te->idcode == ID_OB)) {
1358
1359                         /* active blocks get white circle */
1360                         if (tselem->type == 0) {
1361                                 if (te->idcode == ID_OB) {
1362                                         active = (OBACT == (Object *)tselem->id) ? OL_DRAWSEL_NORMAL : OL_DRAWSEL_NONE;
1363                                 }
1364                                 else if (scene->obedit && scene->obedit->data == tselem->id) {
1365                                         active = OL_DRAWSEL_NORMAL;
1366                                 }
1367                                 else {
1368                                         active = tree_element_active(C, scene, soops, te, OL_SETSEL_NONE, false);
1369                                 }
1370                         }
1371                         else {
1372                                 active = tree_element_type_active(NULL, scene, soops, te, tselem, OL_SETSEL_NONE, false);
1373                         }
1374
1375                         if (active != OL_DRAWSEL_NONE) {
1376                                 float ufac = UI_UNIT_X / 20.0f;
1377
1378                                 UI_draw_roundbox_corner_set(UI_CNR_ALL);
1379                                 glColor4ub(255, 255, 255, 100);
1380                                 UI_draw_roundbox(
1381                                         (float) *offsx - 1.0f * ufac,
1382                                         (float)ys + 1.0f * ufac,
1383                                         (float)*offsx + UI_UNIT_X - 2.0f * ufac,
1384                                         (float)ys + UI_UNIT_Y - ufac,
1385                                         (float)UI_UNIT_Y / 2.0f - ufac);
1386                                 glEnable(GL_BLEND); /* roundbox disables */
1387                         }
1388                         
1389                         tselem_draw_icon(block, xmax, (float)*offsx, (float)ys, tselem, te, 0.5f);
1390                         te->xs = *offsx;
1391                         te->ys = ys;
1392                         te->xend = (short)*offsx + UI_UNIT_X;
1393                         te->flag |= TE_ICONROW; // for click
1394                         
1395                         (*offsx) += UI_UNIT_X;
1396                 }
1397                 
1398                 /* this tree element always has same amount of branches, so don't draw */
1399                 if (tselem->type != TSE_R_LAYER)
1400                         outliner_draw_iconrow(C, block, scene, soops, &te->subtree, level + 1, xmax, offsx, ys);
1401         }
1402         
1403 }
1404
1405 /* closed tree element */
1406 static void outliner_set_coord_tree_element(SpaceOops *soops, TreeElement *te, int startx, int starty)
1407 {
1408         TreeElement *ten;
1409         
1410         /* store coord and continue, we need coordinates for elements outside view too */
1411         te->xs = startx;
1412         te->ys = starty;
1413         
1414         for (ten = te->subtree.first; ten; ten = ten->next) {
1415                 outliner_set_coord_tree_element(soops, ten, startx + UI_UNIT_X, starty);
1416         }
1417 }
1418
1419
1420 static void outliner_draw_tree_element(
1421         bContext *C, uiBlock *block, const uiFontStyle *fstyle, Scene *scene, ARegion *ar, SpaceOops *soops,
1422         TreeElement *te, int startx, int *starty, TreeElement **te_edit)
1423 {
1424         TreeElement *ten;
1425         TreeStoreElem *tselem;
1426         float ufac = UI_UNIT_X / 20.0f;
1427         int offsx = 0;
1428         eOLDrawState active = OL_DRAWSEL_NONE;
1429         
1430         tselem = TREESTORE(te);
1431
1432         if (*starty + 2 * UI_UNIT_Y >= ar->v2d.cur.ymin && *starty <= ar->v2d.cur.ymax) {
1433                 int xmax = ar->v2d.cur.xmax;
1434                 unsigned char alpha = 128;
1435                 
1436                 if ((tselem->flag & TSE_TEXTBUT) && (*te_edit == NULL)) {
1437                         *te_edit = te;
1438                 }
1439
1440                 /* icons can be ui buts, we don't want it to overlap with restrict */
1441                 if ((soops->flag & SO_HIDE_RESTRICTCOLS) == 0)
1442                         xmax -= OL_TOGW + UI_UNIT_X;
1443                 
1444                 glEnable(GL_BLEND);
1445
1446                 /* start by highlighting search matches 
1447                  *      we don't expand items when searching in the datablocks but we 
1448                  *      still want to highlight any filter matches. 
1449                  */
1450                 if ((SEARCHING_OUTLINER(soops) || (soops->outlinevis == SO_DATABLOCKS && soops->search_string[0] != 0)) &&
1451                     (tselem->flag & TSE_SEARCHMATCH))
1452                 {
1453                         char col[4];
1454                         UI_GetThemeColorType4ubv(TH_MATCH, SPACE_OUTLINER, col);
1455                         col[3] = alpha;
1456                         glColor4ubv((GLubyte *)col);
1457                         glRecti(startx, *starty + 1, ar->v2d.cur.xmax, *starty + UI_UNIT_Y - 1);
1458                 }
1459
1460                 /* colors for active/selected data */
1461                 if (tselem->type == 0) {
1462                         
1463                         if (te->idcode == ID_SCE) {
1464                                 if (tselem->id == (ID *)scene) {
1465                                         glColor4ub(255, 255, 255, alpha);
1466                                         active = OL_DRAWSEL_ACTIVE;
1467                                 }
1468                         }
1469                         else if (te->idcode == ID_GR) {
1470                                 Group *gr = (Group *)tselem->id;
1471                                 if (group_select_flag(gr)) {
1472                                         char col[4];
1473                                         UI_GetThemeColorType4ubv(TH_SELECT, SPACE_VIEW3D, col);
1474                                         col[3] = alpha;
1475                                         glColor4ubv((GLubyte *)col);
1476                                         
1477                                         active = OL_DRAWSEL_ACTIVE;
1478                                 }
1479                         }
1480                         else if (te->idcode == ID_OB) {
1481                                 Object *ob = (Object *)tselem->id;
1482                                 
1483                                 if (ob == OBACT || (ob->flag & SELECT)) {
1484                                         char col[4] = {0, 0, 0, 0};
1485                                         
1486                                         /* outliner active ob: always white text, circle color now similar to view3d */
1487                                         
1488                                         active = OL_DRAWSEL_ACTIVE;
1489                                         if (ob == OBACT) {
1490                                                 if (ob->flag & SELECT) {
1491                                                         UI_GetThemeColorType4ubv(TH_ACTIVE, SPACE_VIEW3D, col);
1492                                                         col[3] = alpha;
1493                                                 }
1494                                                 
1495                                                 active = OL_DRAWSEL_NORMAL;
1496                                         }
1497                                         else if (ob->flag & SELECT) {
1498                                                 UI_GetThemeColorType4ubv(TH_SELECT, SPACE_VIEW3D, col);
1499                                                 col[3] = alpha;
1500                                         }
1501                                         
1502                                         glColor4ubv((GLubyte *)col);
1503                                 }
1504                         
1505                         }
1506                         else if (scene->obedit && scene->obedit->data == tselem->id) {
1507                                 glColor4ub(255, 255, 255, alpha);
1508                                 active = OL_DRAWSEL_ACTIVE;
1509                         }
1510                         else {
1511                                 if (tree_element_active(C, scene, soops, te, OL_SETSEL_NONE, false)) {
1512                                         glColor4ub(220, 220, 255, alpha);
1513                                         active = OL_DRAWSEL_ACTIVE;
1514                                 }
1515                         }
1516                 }
1517                 else {
1518                         if (tree_element_type_active(NULL, scene, soops, te, tselem, OL_SETSEL_NONE, false) != OL_DRAWSEL_NONE) {
1519                                 active = OL_DRAWSEL_ACTIVE;
1520                         }
1521                         glColor4ub(220, 220, 255, alpha);
1522                 }
1523                 
1524                 /* active circle */
1525                 if (active != OL_DRAWSEL_NONE) {
1526                         UI_draw_roundbox_corner_set(UI_CNR_ALL);
1527                         UI_draw_roundbox(
1528                                 (float)startx + UI_UNIT_X,
1529                                 (float)*starty + 1.0f * ufac,
1530                                 (float)startx + 2.0f * UI_UNIT_X - 2.0f * ufac,
1531                                 (float)*starty + UI_UNIT_Y - 1.0f * ufac,
1532                                 UI_UNIT_Y / 2.0f - 1.0f * ufac);
1533                         glEnable(GL_BLEND); /* roundbox disables it */
1534                         
1535                         te->flag |= TE_ACTIVE; // for lookup in display hierarchies
1536                 }
1537                 
1538                 /* open/close icon, only when sublevels, except for scene */
1539                 if (te->subtree.first || (tselem->type == 0 && te->idcode == ID_SCE) || (te->flag & TE_LAZY_CLOSED)) {
1540                         int icon_x;
1541                         if (tselem->type == 0 && ELEM(te->idcode, ID_OB, ID_SCE))
1542                                 icon_x = startx;
1543                         else
1544                                 icon_x = startx + 5 * ufac;
1545                         
1546                         // icons a bit higher
1547                         if (TSELEM_OPEN(tselem, soops))
1548                                 UI_icon_draw((float)icon_x, (float)*starty + 2 * ufac, ICON_DISCLOSURE_TRI_DOWN);
1549                         else
1550                                 UI_icon_draw((float)icon_x, (float)*starty + 2 * ufac, ICON_DISCLOSURE_TRI_RIGHT);
1551                 }
1552                 offsx += UI_UNIT_X;
1553                 
1554                 /* datatype icon */
1555                 
1556                 if (!(ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM))) {
1557                         
1558                         tselem_draw_icon(block, xmax, (float)startx + offsx, (float)*starty, tselem, te, 1.0f);
1559                         
1560                         offsx += UI_UNIT_X;
1561                 }
1562                 else
1563                         offsx += 2 * ufac;
1564                 
1565                 if (tselem->type == 0 && ID_IS_LINKED_DATABLOCK(tselem->id)) {
1566                         glPixelTransferf(GL_ALPHA_SCALE, 0.5f);
1567                         if (tselem->id->tag & LIB_TAG_MISSING) {
1568                                 UI_icon_draw((float)startx + offsx, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_BROKEN);
1569                         }
1570                         else if (tselem->id->tag & LIB_TAG_INDIRECT) {
1571                                 UI_icon_draw((float)startx + offsx, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_INDIRECT);
1572                         }
1573                         else {
1574                                 UI_icon_draw((float)startx + offsx, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_DIRECT);
1575                         }
1576                         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1577                         offsx += UI_UNIT_X;
1578                 }
1579                 glDisable(GL_BLEND);
1580                 
1581                 /* name */
1582                 if ((tselem->flag & TSE_TEXTBUT) == 0) {
1583                         if (active == OL_DRAWSEL_NORMAL) {
1584                                 UI_ThemeColor(TH_TEXT_HI);
1585                         }
1586                         else if (ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
1587                                 UI_ThemeColorBlend(TH_BACK, TH_TEXT, 0.75f);
1588                         }
1589                         else {
1590                                 UI_ThemeColor(TH_TEXT);
1591                         }
1592
1593                         UI_fontstyle_draw_simple(fstyle, startx + offsx, *starty + 5 * ufac, te->name);
1594                 }
1595                 
1596                 offsx += (int)(UI_UNIT_X + UI_fontstyle_string_width(fstyle, te->name));
1597                 
1598                 /* closed item, we draw the icons, not when it's a scene, or master-server list though */
1599                 if (!TSELEM_OPEN(tselem, soops)) {
1600                         if (te->subtree.first) {
1601                                 if (tselem->type == 0 && te->idcode == ID_SCE) {
1602                                         /* pass */
1603                                 }
1604                                 else if (tselem->type != TSE_R_LAYER) {
1605                                         /* this tree element always has same amount of branches, so don't draw */
1606
1607                                         int tempx = startx + offsx;
1608                                         
1609                                         /* divider */
1610                                         UI_ThemeColorShade(TH_BACK, -40);
1611                                         glRecti(tempx   - 10.0f * ufac,
1612                                                 *starty +  4.0f * ufac,
1613                                                 tempx   -  8.0f * ufac,
1614                                                 *starty + UI_UNIT_Y - 4.0f * ufac);
1615                                         
1616                                         glEnable(GL_BLEND);
1617                                         glPixelTransferf(GL_ALPHA_SCALE, 0.5);
1618                                         
1619                                         outliner_draw_iconrow(C, block, scene, soops, &te->subtree, 0, xmax, &tempx, *starty);
1620                                         
1621                                         glPixelTransferf(GL_ALPHA_SCALE, 1.0);
1622                                         glDisable(GL_BLEND);
1623                                 }
1624                         }
1625                 }
1626         }
1627         /* store coord and continue, we need coordinates for elements outside view too */
1628         te->xs = startx;
1629         te->ys = *starty;
1630         te->xend = startx + offsx;
1631                 
1632         if (TSELEM_OPEN(tselem, soops)) {
1633                 *starty -= UI_UNIT_Y;
1634
1635                 for (ten = te->subtree.first; ten; ten = ten->next) {
1636                         outliner_draw_tree_element(C, block, fstyle, scene, ar, soops, ten, startx + UI_UNIT_X, starty, te_edit);
1637                 }
1638         }
1639         else {
1640                 for (ten = te->subtree.first; ten; ten = ten->next) {
1641                         outliner_set_coord_tree_element(soops, ten, startx, *starty);
1642                 }
1643                 
1644                 *starty -= UI_UNIT_Y;
1645         }
1646 }
1647
1648 static void outliner_draw_hierarchy(SpaceOops *soops, ListBase *lb, int startx, int *starty)
1649 {
1650         TreeElement *te;
1651         TreeStoreElem *tselem;
1652         int y1, y2;
1653         
1654         if (BLI_listbase_is_empty(lb)) return;
1655         
1656         y1 = y2 = *starty; /* for vertical lines between objects */
1657         for (te = lb->first; te; te = te->next) {
1658                 y2 = *starty;
1659                 tselem = TREESTORE(te);
1660                 
1661                 /* horizontal line? */
1662                 if (tselem->type == 0 && (te->idcode == ID_OB || te->idcode == ID_SCE))
1663                         glRecti(startx, *starty, startx + UI_UNIT_X, *starty - 1);
1664                         
1665                 *starty -= UI_UNIT_Y;
1666                 
1667                 if (TSELEM_OPEN(tselem, soops))
1668                         outliner_draw_hierarchy(soops, &te->subtree, startx + UI_UNIT_X, starty);
1669         }
1670         
1671         /* vertical line */
1672         te = lb->last;
1673         if (te->parent || lb->first != lb->last) {
1674                 tselem = TREESTORE(te);
1675                 if (tselem->type == 0 && te->idcode == ID_OB) {
1676                         
1677                         glRecti(startx, y1 + UI_UNIT_Y, startx + 1, y2);
1678                 }
1679         }
1680 }
1681
1682 static void outliner_draw_struct_marks(ARegion *ar, SpaceOops *soops, ListBase *lb, int *starty) 
1683 {
1684         TreeElement *te;
1685         TreeStoreElem *tselem;
1686         
1687         for (te = lb->first; te; te = te->next) {
1688                 tselem = TREESTORE(te);
1689                 
1690                 /* selection status */
1691                 if (TSELEM_OPEN(tselem, soops))
1692                         if (tselem->type == TSE_RNA_STRUCT)
1693                                 glRecti(0, *starty + 1, (int)ar->v2d.cur.xmax, *starty + UI_UNIT_Y - 1);
1694
1695                 *starty -= UI_UNIT_Y;
1696                 if (TSELEM_OPEN(tselem, soops)) {
1697                         outliner_draw_struct_marks(ar, soops, &te->subtree, starty);
1698                         if (tselem->type == TSE_RNA_STRUCT)
1699                                 fdrawline(0, (float)*starty + UI_UNIT_Y, ar->v2d.cur.xmax, (float)*starty + UI_UNIT_Y);
1700                 }
1701         }
1702 }
1703
1704 static void outliner_draw_selection(ARegion *ar, SpaceOops *soops, ListBase *lb, int *starty) 
1705 {
1706         TreeElement *te;
1707         TreeStoreElem *tselem;
1708         
1709         for (te = lb->first; te; te = te->next) {
1710                 tselem = TREESTORE(te);
1711                 
1712                 /* selection status */
1713                 if (tselem->flag & TSE_SELECTED) {
1714                         glRecti(0, *starty + 1, (int)ar->v2d.cur.xmax, *starty + UI_UNIT_Y - 1);
1715                 }
1716                 *starty -= UI_UNIT_Y;
1717                 if (TSELEM_OPEN(tselem, soops)) outliner_draw_selection(ar, soops, &te->subtree, starty);
1718         }
1719 }
1720
1721
1722 static void outliner_draw_tree(bContext *C, uiBlock *block, Scene *scene, ARegion *ar,
1723                                SpaceOops *soops, TreeElement **te_edit)
1724 {
1725         const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
1726         TreeElement *te;
1727         int starty, startx;
1728         float col[3];
1729                 
1730         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); // only once
1731         
1732         if (ELEM(soops->outlinevis, SO_DATABLOCKS, SO_USERDEF)) {
1733                 /* struct marks */
1734                 UI_ThemeColorShadeAlpha(TH_BACK, -15, -200);
1735                 //UI_ThemeColorShade(TH_BACK, -20);
1736                 starty = (int)ar->v2d.tot.ymax - UI_UNIT_Y - OL_Y_OFFSET;
1737                 outliner_draw_struct_marks(ar, soops, &soops->tree, &starty);
1738         }
1739         
1740         /* always draw selection fill before hierarchy */
1741         UI_GetThemeColor3fv(TH_SELECT_HIGHLIGHT, col);
1742         glColor3fv(col);
1743         starty = (int)ar->v2d.tot.ymax - UI_UNIT_Y - OL_Y_OFFSET;
1744         outliner_draw_selection(ar, soops, &soops->tree, &starty);
1745         
1746         // gray hierarchy lines
1747         UI_ThemeColorBlend(TH_BACK, TH_TEXT, 0.4f);
1748         starty = (int)ar->v2d.tot.ymax - UI_UNIT_Y / 2 - OL_Y_OFFSET;
1749         startx = 6;
1750         outliner_draw_hierarchy(soops, &soops->tree, startx, &starty);
1751         
1752         // items themselves
1753         starty = (int)ar->v2d.tot.ymax - UI_UNIT_Y - OL_Y_OFFSET;
1754         startx = 0;
1755         for (te = soops->tree.first; te; te = te->next) {
1756                 outliner_draw_tree_element(C, block, fstyle, scene, ar, soops, te, startx, &starty, te_edit);
1757         }
1758 }
1759
1760
1761 static void outliner_back(ARegion *ar)
1762 {
1763         int ystart;
1764         
1765         UI_ThemeColorShade(TH_BACK, 6);
1766         ystart = (int)ar->v2d.tot.ymax;
1767         ystart = UI_UNIT_Y * (ystart / (UI_UNIT_Y)) - OL_Y_OFFSET;
1768         
1769         while (ystart + 2 * UI_UNIT_Y > ar->v2d.cur.ymin) {
1770                 glRecti(0, ystart, (int)ar->v2d.cur.xmax, ystart + UI_UNIT_Y);
1771                 ystart -= 2 * UI_UNIT_Y;
1772         }
1773 }
1774
1775 static void outliner_draw_restrictcols(ARegion *ar)
1776 {
1777         int ystart;
1778         
1779         /* background underneath */
1780         UI_ThemeColor(TH_BACK);
1781         glRecti((int)(ar->v2d.cur.xmax - OL_TOGW),
1782                 (int)(ar->v2d.cur.ymin - 1), (int)ar->v2d.cur.xmax, (int)ar->v2d.cur.ymax);
1783         
1784         UI_ThemeColorShade(TH_BACK, 6);
1785         ystart = (int)ar->v2d.tot.ymax;
1786         ystart = UI_UNIT_Y * (ystart / (UI_UNIT_Y)) - OL_Y_OFFSET;
1787         
1788         while (ystart + 2 * UI_UNIT_Y > ar->v2d.cur.ymin) {
1789                 glRecti((int)ar->v2d.cur.xmax - OL_TOGW, ystart, (int)ar->v2d.cur.xmax, ystart + UI_UNIT_Y);
1790                 ystart -= 2 * UI_UNIT_Y;
1791         }
1792         
1793         UI_ThemeColorShadeAlpha(TH_BACK, -15, -200);
1794
1795         /* view */
1796         sdrawline((int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX),
1797                   (int)ar->v2d.cur.ymax,
1798                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX),
1799                   (int)ar->v2d.cur.ymin);
1800
1801         /* render */
1802         sdrawline((int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX),
1803                   (int)ar->v2d.cur.ymax,
1804                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX),
1805                   (int)ar->v2d.cur.ymin);
1806
1807         /* render */
1808         sdrawline((int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX),
1809                   (int)ar->v2d.cur.ymax,
1810                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX),
1811                   (int)ar->v2d.cur.ymin);
1812 }
1813
1814 /* ****************************************************** */
1815 /* Main Entrypoint - Draw contents of Outliner editor */
1816
1817 void draw_outliner(const bContext *C)
1818 {
1819         Main *mainvar = CTX_data_main(C); 
1820         Scene *scene = CTX_data_scene(C);
1821         ARegion *ar = CTX_wm_region(C);
1822         View2D *v2d = &ar->v2d;
1823         SpaceOops *soops = CTX_wm_space_outliner(C);
1824         uiBlock *block;
1825         int sizey = 0, sizex = 0, sizex_rna = 0;
1826         TreeElement *te_edit = NULL;
1827
1828         outliner_build_tree(mainvar, scene, soops); // always
1829         
1830         /* get extents of data */
1831         outliner_height(soops, &soops->tree, &sizey);
1832
1833         if (ELEM(soops->outlinevis, SO_DATABLOCKS, SO_USERDEF)) {
1834                 /* RNA has two columns:
1835                  *  - column 1 is (max_width + OL_RNA_COL_SPACEX) or
1836                  *                               (OL_RNA_COL_X), whichever is wider...
1837                  *      - column 2 is fixed at OL_RNA_COL_SIZEX
1838                  *
1839                  *  (*) XXX max width for now is a fixed factor of (UI_UNIT_X * (max_indention + 100))
1840                  */
1841                  
1842                 /* get actual width of column 1 */
1843                 outliner_rna_width(soops, &soops->tree, &sizex_rna, 0);
1844                 sizex_rna = max_ii(OL_RNA_COLX, sizex_rna + OL_RNA_COL_SPACEX);
1845                 
1846                 /* get width of data (for setting 'tot' rect, this is column 1 + column 2 + a bit extra) */
1847                 sizex = sizex_rna + OL_RNA_COL_SIZEX + 50;
1848         }
1849         else {
1850                 /* width must take into account restriction columns (if visible) so that entries will still be visible */
1851                 //outliner_width(soops, &soops->tree, &sizex);
1852                 // XXX should use outliner_width instead when te->xend will be set correctly...
1853                 outliner_rna_width(soops, &soops->tree, &sizex, 0);
1854                 
1855                 /* constant offset for restriction columns */
1856                 // XXX this isn't that great yet...
1857                 if ((soops->flag & SO_HIDE_RESTRICTCOLS) == 0)
1858                         sizex += OL_TOGW * 3;
1859                 
1860         }
1861         
1862         /* adds vertical offset */
1863         sizey += OL_Y_OFFSET;
1864
1865         /* update size of tot-rect (extents of data/viewable area) */
1866         UI_view2d_totRect_set(v2d, sizex, sizey);
1867
1868         /* force display to pixel coords */
1869         v2d->flag |= (V2D_PIXELOFS_X | V2D_PIXELOFS_Y);
1870         /* set matrix for 2d-view controls */
1871         UI_view2d_view_ortho(v2d);
1872
1873         /* draw outliner stuff (background, hierarchy lines and names) */
1874         outliner_back(ar);
1875         block = UI_block_begin(C, ar, __func__, UI_EMBOSS);
1876         outliner_draw_tree((bContext *)C, block, scene, ar, soops, &te_edit);
1877         
1878         if (ELEM(soops->outlinevis, SO_DATABLOCKS, SO_USERDEF)) {
1879                 /* draw rna buttons */
1880                 outliner_draw_rnacols(ar, sizex_rna);
1881                 outliner_draw_rnabuts(block, scene, ar, soops, sizex_rna, &soops->tree);
1882         }
1883         else if ((soops->outlinevis == SO_ID_ORPHANS) && !(soops->flag & SO_HIDE_RESTRICTCOLS)) {
1884                 /* draw user toggle columns */
1885                 outliner_draw_restrictcols(ar);
1886                 outliner_draw_userbuts(block, ar, soops, &soops->tree);
1887         }
1888         else if (!(soops->flag & SO_HIDE_RESTRICTCOLS)) {
1889                 /* draw restriction columns */
1890                 outliner_draw_restrictcols(ar);
1891                 outliner_draw_restrictbuts(block, scene, ar, soops, &soops->tree);
1892         }
1893
1894         /* draw edit buttons if nessecery */
1895         if (te_edit) {
1896                 outliner_buttons(C, block, ar, te_edit);
1897         }
1898
1899         UI_block_end(C, block);
1900         UI_block_draw(C, block);
1901
1902         /* clear flag that allows quick redraws */
1903         soops->storeflag &= ~SO_TREESTORE_REDRAW;
1904