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