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