Merging r58701 through r58746 from trunk into soc-2013-depsgraph_mt
[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                                         break;
468                                 }
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                                         break;
486                                 }
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                                         break;
503                                 }
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, CTX_DATA_(BLF_I18NCONTEXT_ID_ACTION, "Group"), '.',
510                                                        offsetof(bActionGroup, name), sizeof(grp->name));
511                                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
512                                         break;
513                                 }
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), 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                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
547                                 
548                                 bt = uiDefIconButR(block, ICONTOG, 0, ICON_RESTRICT_SELECT_OFF,
549                                                    (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X, UI_UNIT_Y,
550                                                    &ptr, "hide_select", -1, 0, 0, -1, -1,
551                                                    TIP_("Restrict viewport selection (Ctrl - Recursive)"));
552                                 uiButSetFunc(bt, restrictbutton_sel_cb, scene, ob);
553                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
554                                 
555                                 bt = uiDefIconButR(block, ICONTOG, 0, ICON_RESTRICT_RENDER_OFF,
556                                                    (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX), te->ys, UI_UNIT_X, UI_UNIT_Y,
557                                                    &ptr, "hide_render", -1, 0, 0, -1, -1,
558                                                    TIP_("Restrict rendering (Ctrl - Recursive)"));
559                                 uiButSetFunc(bt, restrictbutton_rend_cb, scene, ob);
560                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
561                                 
562                                 uiBlockSetEmboss(block, UI_EMBOSS);
563                                 
564                         }
565                         if (tselem->type == 0 && te->idcode == ID_GR) {
566                                 int restrict_bool;
567                                 gr = (Group *)tselem->id;
568                                 
569                                 uiBlockSetEmboss(block, UI_EMBOSSN);
570
571                                 restrict_bool = group_restrict_flag(gr, OB_RESTRICT_VIEW);
572                                 bt = uiDefIconBut(block, ICONTOG, 0, restrict_bool ? ICON_RESTRICT_VIEW_ON : ICON_RESTRICT_VIEW_OFF,
573                                                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X, UI_UNIT_Y,
574                                                   NULL, 0, 0, 0, 0, TIP_("Restrict/Allow visibility in the 3D View"));
575                                 uiButSetFunc(bt, restrictbutton_gr_restrict_view, scene, gr);
576                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
577
578                                 restrict_bool = group_restrict_flag(gr, OB_RESTRICT_SELECT);
579                                 bt = uiDefIconBut(block, ICONTOG, 0, restrict_bool ? ICON_RESTRICT_SELECT_ON : ICON_RESTRICT_SELECT_OFF,
580                                                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X, UI_UNIT_Y,
581                                                   NULL, 0, 0, 0, 0, TIP_("Restrict/Allow selection in the 3D View"));
582                                 uiButSetFunc(bt, restrictbutton_gr_restrict_select, scene, gr);
583                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
584
585                                 restrict_bool = group_restrict_flag(gr, OB_RESTRICT_RENDER);
586                                 bt = uiDefIconBut(block, ICONTOG, 0, restrict_bool ? ICON_RESTRICT_RENDER_ON : ICON_RESTRICT_RENDER_OFF,
587                                                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX), te->ys, UI_UNIT_X, UI_UNIT_Y,
588                                                   NULL, 0, 0, 0, 0, TIP_("Restrict/Allow renderability"));
589                                 uiButSetFunc(bt, restrictbutton_gr_restrict_render, scene, gr);
590                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
591
592                                 uiBlockSetEmboss(block, UI_EMBOSS);
593                         }
594                         /* scene render layers and passes have toggle-able flags too! */
595                         else if (tselem->type == TSE_R_LAYER) {
596                                 uiBlockSetEmboss(block, UI_EMBOSSN);
597                                 
598                                 bt = uiDefIconButBitI(block, ICONTOGN, SCE_LAY_DISABLE, 0, ICON_CHECKBOX_HLT - 1,
599                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
600                                                       UI_UNIT_Y, te->directdata, 0, 0, 0, 0, TIP_("Render this RenderLayer"));
601                                 uiButSetFunc(bt, restrictbutton_r_lay_cb, tselem->id, NULL);
602                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
603                                 
604                                 uiBlockSetEmboss(block, UI_EMBOSS);
605                         }
606                         else if (tselem->type == TSE_R_PASS) {
607                                 int *layflag = te->directdata;
608                                 int passflag = 1 << tselem->nr;
609                                 
610                                 uiBlockSetEmboss(block, UI_EMBOSSN);
611                                 
612                                 
613                                 bt = uiDefIconButBitI(block, ICONTOG, passflag, 0, ICON_CHECKBOX_HLT - 1,
614                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
615                                                       UI_UNIT_Y, layflag, 0, 0, 0, 0, TIP_("Render this Pass"));
616                                 uiButSetFunc(bt, restrictbutton_r_lay_cb, tselem->id, NULL);
617                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
618                                 
619                                 layflag++;  /* is lay_xor */
620                                 if (ELEM8(passflag, SCE_PASS_SPEC, SCE_PASS_SHADOW, SCE_PASS_AO, SCE_PASS_REFLECT, SCE_PASS_REFRACT,
621                                           SCE_PASS_INDIRECT, SCE_PASS_EMIT, SCE_PASS_ENVIRONMENT))
622                                 {
623                                         bt = uiDefIconButBitI(block, TOG, passflag, 0, (*layflag & passflag) ? ICON_DOT : ICON_BLANK1,
624                                                               (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X,
625                                                               UI_UNIT_Y, layflag, 0, 0, 0, 0, TIP_("Exclude this Pass from Combined"));
626                                         uiButSetFunc(bt, restrictbutton_r_lay_cb, tselem->id, NULL);
627                                         uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
628                                 }
629                                 
630                                 uiBlockSetEmboss(block, UI_EMBOSS);
631                         }
632                         else if (tselem->type == TSE_MODIFIER) {
633                                 ModifierData *md = (ModifierData *)te->directdata;
634                                 ob = (Object *)tselem->id;
635                                 
636                                 uiBlockSetEmboss(block, UI_EMBOSSN);
637                                 bt = uiDefIconButBitI(block, ICONTOGN, eModifierMode_Realtime, 0, ICON_RESTRICT_VIEW_OFF,
638                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
639                                                       UI_UNIT_Y, &(md->mode), 0, 0, 0, 0,
640                                                       TIP_("Restrict/Allow visibility in the 3D View"));
641                                 uiButSetFunc(bt, restrictbutton_modifier_cb, scene, ob);
642                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
643                                 
644                                 bt = uiDefIconButBitI(block, ICONTOGN, eModifierMode_Render, 0, ICON_RESTRICT_RENDER_OFF,
645                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX), te->ys, UI_UNIT_X,
646                                                       UI_UNIT_Y, &(md->mode), 0, 0, 0, 0, TIP_("Restrict/Allow renderability"));
647                                 uiButSetFunc(bt, restrictbutton_modifier_cb, scene, ob);
648                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
649
650                                 uiBlockSetEmboss(block, UI_EMBOSS);
651                         }
652                         else if (tselem->type == TSE_POSE_CHANNEL) {
653                                 bPoseChannel *pchan = (bPoseChannel *)te->directdata;
654                                 Bone *bone = pchan->bone;
655                                 ob = (Object *)tselem->id;
656                                 
657                                 uiBlockSetEmboss(block, UI_EMBOSSN);
658                                 bt = uiDefIconButBitI(block, ICONTOG, BONE_HIDDEN_P, 0, ICON_RESTRICT_VIEW_OFF,
659                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
660                                                       UI_UNIT_Y, &(bone->flag), 0, 0, 0, 0,
661                                                       TIP_("Restrict/Allow visibility in the 3D View"));
662                                 uiButSetFunc(bt, restrictbutton_bone_visibility_cb, ob->data, bone);
663                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
664                                 
665                                 bt = uiDefIconButBitI(block, ICONTOG, BONE_UNSELECTABLE, 0, ICON_RESTRICT_SELECT_OFF,
666                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X,
667                                                       UI_UNIT_Y, &(bone->flag), 0, 0, 0, 0,
668                                                       TIP_("Restrict/Allow selection in the 3D View"));
669                                 uiButSetFunc(bt, restrictbutton_bone_select_cb, ob->data, bone);
670                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
671
672                                 uiBlockSetEmboss(block, UI_EMBOSS);
673                         }
674                         else if (tselem->type == TSE_EBONE) {
675                                 EditBone *ebone = (EditBone *)te->directdata;
676                                 
677                                 uiBlockSetEmboss(block, UI_EMBOSSN);
678                                 bt = uiDefIconButBitI(block, ICONTOG, BONE_HIDDEN_A, 0, ICON_RESTRICT_VIEW_OFF,
679                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX), te->ys, UI_UNIT_X,
680                                                       UI_UNIT_Y, &(ebone->flag), 0, 0, 0, 0,
681                                                       TIP_("Restrict/Allow visibility in the 3D View"));
682                                 uiButSetFunc(bt, restrictbutton_ebone_visibility_cb, NULL, ebone);
683                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
684                                 
685                                 bt = uiDefIconButBitI(block, ICONTOG, BONE_UNSELECTABLE, 0, ICON_RESTRICT_SELECT_OFF,
686                                                       (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX), te->ys, UI_UNIT_X,
687                                                       UI_UNIT_Y, &(ebone->flag), 0, 0, 0, 0,
688                                                       TIP_("Restrict/Allow selection in the 3D View"));
689                                 uiButSetFunc(bt, restrictbutton_ebone_select_cb, NULL, ebone);
690                                 uiButSetFlag(bt, UI_BUT_DRAG_LOCK);
691
692                                 uiBlockSetEmboss(block, UI_EMBOSS);
693                         }
694                 }
695                 
696                 if (TSELEM_OPEN(tselem, soops)) outliner_draw_restrictbuts(block, scene, ar, soops, &te->subtree);
697         }
698 }
699
700 static void outliner_draw_rnacols(ARegion *ar, int sizex)
701 {
702         View2D *v2d = &ar->v2d;
703
704         float miny = v2d->cur.ymin;
705         if (miny < v2d->tot.ymin) miny = v2d->tot.ymin;
706
707         UI_ThemeColorShadeAlpha(TH_BACK, -15, -200);
708
709         /* draw column separator lines */
710         fdrawline((float)sizex,
711                   v2d->cur.ymax,
712                   (float)sizex,
713                   miny);
714
715         fdrawline((float)sizex + OL_RNA_COL_SIZEX,
716                   v2d->cur.ymax,
717                   (float)sizex + OL_RNA_COL_SIZEX,
718                   miny);
719 }
720
721 static void outliner_draw_rnabuts(uiBlock *block, Scene *scene, ARegion *ar, SpaceOops *soops, int sizex, ListBase *lb)
722 {
723         TreeElement *te;
724         TreeStoreElem *tselem;
725         PointerRNA *ptr;
726         PropertyRNA *prop;
727         
728         uiBlockSetEmboss(block, UI_EMBOSST);
729
730         for (te = lb->first; te; te = te->next) {
731                 tselem = TREESTORE(te);
732                 if (te->ys + 2 * UI_UNIT_Y >= ar->v2d.cur.ymin && te->ys <= ar->v2d.cur.ymax) {
733                         if (tselem->type == TSE_RNA_PROPERTY) {
734                                 ptr = &te->rnaptr;
735                                 prop = te->directdata;
736                                 
737                                 if (!(RNA_property_type(prop) == PROP_POINTER && (TSELEM_OPEN(tselem, soops)))) {
738                                         uiDefAutoButR(block, ptr, prop, -1, "", ICON_NONE, sizex, te->ys, OL_RNA_COL_SIZEX,
739                                                       UI_UNIT_Y - 1);
740                                 }
741                         }
742                         else if (tselem->type == TSE_RNA_ARRAY_ELEM) {
743                                 ptr = &te->rnaptr;
744                                 prop = te->directdata;
745                                 
746                                 uiDefAutoButR(block, ptr, prop, te->index, "", ICON_NONE, sizex, te->ys, OL_RNA_COL_SIZEX,
747                                               UI_UNIT_Y - 1);
748                         }
749                 }
750                 
751                 if (TSELEM_OPEN(tselem, soops)) outliner_draw_rnabuts(block, scene, ar, soops, sizex, &te->subtree);
752         }
753
754         uiBlockSetEmboss(block, UI_EMBOSS);
755 }
756
757 static void operator_call_cb(struct bContext *UNUSED(C), void *arg_kmi, void *arg2)
758 {
759         wmOperatorType *ot = arg2;
760         wmKeyMapItem *kmi = arg_kmi;
761         
762         if (ot)
763                 BLI_strncpy(kmi->idname, ot->idname, OP_MAX_TYPENAME);
764 }
765
766 static void operator_search_cb(const struct bContext *UNUSED(C), void *UNUSED(arg_kmi),
767                                const char *str, uiSearchItems *items)
768 {
769         GHashIterator *iter = WM_operatortype_iter();
770
771         for (; !BLI_ghashIterator_done(iter); BLI_ghashIterator_step(iter)) {
772                 wmOperatorType *ot = BLI_ghashIterator_getValue(iter);
773                 
774                 if (BLI_strcasestr(ot->idname, str)) {
775                         char name[OP_MAX_TYPENAME];
776                         
777                         /* display name for menu */
778                         WM_operator_py_idname(name, ot->idname);
779                         
780                         if (false == uiSearchItemAdd(items, name, ot, 0))
781                                 break;
782                 }
783         }
784         BLI_ghashIterator_free(iter);
785 }
786
787 /* operator Search browse menu, open */
788 static uiBlock *operator_search_menu(bContext *C, ARegion *ar, void *arg_kmi)
789 {
790         static char search[OP_MAX_TYPENAME];
791         wmEvent event;
792         wmWindow *win = CTX_wm_window(C);
793         wmKeyMapItem *kmi = arg_kmi;
794         wmOperatorType *ot = WM_operatortype_find(kmi->idname, 0);
795         uiBlock *block;
796         uiBut *but;
797         
798         /* clear initial search string, then all items show */
799         search[0] = 0;
800         
801         block = uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
802         uiBlockSetFlag(block, UI_BLOCK_LOOP | UI_BLOCK_REDRAW | UI_BLOCK_SEARCH_MENU);
803         
804         /* fake button, it holds space for search items */
805         uiDefBut(block, LABEL, 0, "", 10, 15, uiSearchBoxWidth(), uiSearchBoxHeight(), NULL, 0, 0, 0, 0, NULL);
806         
807         but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 0, 150, UI_UNIT_Y, 0, 0, "");
808         uiButSetSearchFunc(but, operator_search_cb, arg_kmi, operator_call_cb, ot);
809         
810         uiBoundsBlock(block, 6);
811         uiBlockSetDirection(block, UI_DOWN);
812         uiEndBlock(C, block);
813         
814         wm_event_init_from_window(win, &event);
815         event.type = EVT_BUT_OPEN;
816         event.val = KM_PRESS;
817         event.customdata = but;
818         event.customdatafree = FALSE;
819         wm_event_add(win, &event);
820         
821         return block;
822 }
823
824 #define OL_KM_KEYBOARD      0
825 #define OL_KM_MOUSE         1
826 #define OL_KM_TWEAK         2
827 #define OL_KM_SPECIALS      3
828
829 static short keymap_menu_type(short type)
830 {
831         if (ISKEYBOARD(type)) return OL_KM_KEYBOARD;
832         if (ISTWEAK(type)) return OL_KM_TWEAK;
833         if (ISMOUSE(type)) return OL_KM_MOUSE;
834 //      return OL_KM_SPECIALS;
835         return 0;
836 }
837
838 static const char *keymap_type_menu(void)
839 {
840         static const char string[] =
841             "Event Type%t"
842             "|Keyboard%x" STRINGIFY(OL_KM_KEYBOARD)
843             "|Mouse%x" STRINGIFY(OL_KM_MOUSE)
844             "|Tweak%x" STRINGIFY(OL_KM_TWEAK)
845 //      "|Specials%x" STRINGIFY(OL_KM_SPECIALS)
846         ;
847
848         return string;
849 }
850
851 static const char *keymap_mouse_menu(void)
852 {
853         static const char string[] =
854             "Mouse Event%t"
855             "|Left Mouse%x" STRINGIFY(LEFTMOUSE)
856             "|Middle Mouse%x" STRINGIFY(MIDDLEMOUSE)
857             "|Right Mouse%x" STRINGIFY(RIGHTMOUSE)
858             "|Middle Mouse%x" STRINGIFY(MIDDLEMOUSE)
859             "|Right Mouse%x" STRINGIFY(RIGHTMOUSE)
860             "|Button4 Mouse%x" STRINGIFY(BUTTON4MOUSE)
861             "|Button5 Mouse%x" STRINGIFY(BUTTON5MOUSE)
862             "|Action Mouse%x" STRINGIFY(ACTIONMOUSE)
863             "|Select Mouse%x" STRINGIFY(SELECTMOUSE)
864             "|Mouse Move%x" STRINGIFY(MOUSEMOVE)
865             "|Wheel Up%x" STRINGIFY(WHEELUPMOUSE)
866             "|Wheel Down%x" STRINGIFY(WHEELDOWNMOUSE)
867             "|Wheel In%x" STRINGIFY(WHEELINMOUSE)
868             "|Wheel Out%x" STRINGIFY(WHEELOUTMOUSE)
869             "|Mouse/Trackpad Pan%x" STRINGIFY(MOUSEPAN)
870             "|Mouse/Trackpad Zoom%x" STRINGIFY(MOUSEZOOM)
871             "|Mouse/Trackpad Rotate%x" STRINGIFY(MOUSEROTATE)
872         ;
873
874         return string;
875 }
876
877 static const char *keymap_tweak_menu(void)
878 {
879         static const char string[] =
880             "Tweak Event%t"
881             "|Left Mouse%x" STRINGIFY(EVT_TWEAK_L)
882             "|Middle Mouse%x" STRINGIFY(EVT_TWEAK_M)
883             "|Right Mouse%x" STRINGIFY(EVT_TWEAK_R)
884             "|Action Mouse%x" STRINGIFY(EVT_TWEAK_A)
885             "|Select Mouse%x" STRINGIFY(EVT_TWEAK_S)
886         ;
887
888         return string;
889 }
890
891 static const char *keymap_tweak_dir_menu(void)
892 {
893         static const char string[] =
894             "Tweak Direction%t"
895             "|Any%x" STRINGIFY(KM_ANY)
896             "|North%x" STRINGIFY(EVT_GESTURE_N)
897             "|North-East%x" STRINGIFY(EVT_GESTURE_NE)
898             "|East%x" STRINGIFY(EVT_GESTURE_E)
899             "|Sout-East%x" STRINGIFY(EVT_GESTURE_SE)
900             "|South%x" STRINGIFY(EVT_GESTURE_S)
901             "|South-West%x" STRINGIFY(EVT_GESTURE_SW)
902             "|West%x" STRINGIFY(EVT_GESTURE_W)
903             "|North-West%x" STRINGIFY(EVT_GESTURE_NW)
904         ;
905
906         return string;
907 }
908
909
910 static void keymap_type_cb(bContext *C, void *kmi_v, void *UNUSED(arg_v))
911 {
912         wmKeyMapItem *kmi = kmi_v;
913         short maptype = keymap_menu_type(kmi->type);
914         
915         if (maptype != kmi->maptype) {
916                 switch (kmi->maptype) {
917                         case OL_KM_KEYBOARD:
918                                 kmi->type = AKEY;
919                                 kmi->val = KM_PRESS;
920                                 break;
921                         case OL_KM_MOUSE:
922                                 kmi->type = LEFTMOUSE;
923                                 kmi->val = KM_PRESS;
924                                 break;
925                         case OL_KM_TWEAK:
926                                 kmi->type = EVT_TWEAK_L;
927                                 kmi->val = KM_ANY;
928                                 break;
929                         case OL_KM_SPECIALS:
930                                 kmi->type = AKEY;
931                                 kmi->val = KM_PRESS;
932                                 break;
933                 }
934                 ED_region_tag_redraw(CTX_wm_region(C));
935         }
936 }
937
938 static void outliner_draw_keymapbuts(uiBlock *block, ARegion *ar, SpaceOops *soops, ListBase *lb)
939 {
940         TreeElement *te;
941         TreeStoreElem *tselem;
942         
943         uiBlockSetEmboss(block, UI_EMBOSST);
944         
945         for (te = lb->first; te; te = te->next) {
946                 tselem = TREESTORE(te);
947                 if (te->ys + 2 * UI_UNIT_Y >= ar->v2d.cur.ymin && te->ys <= ar->v2d.cur.ymax) {
948                         uiBut *but;
949                         const char *str;
950                         int xstart = 240;
951                         int butw1 = UI_UNIT_X; /* operator */
952                         int butw2 = 90; /* event type, menus */
953                         int butw3 = 43; /* modifiers */
954
955                         if (tselem->type == TSE_KEYMAP_ITEM) {
956                                 wmKeyMapItem *kmi = te->directdata;
957                                 
958                                 /* modal map? */
959                                 if (kmi->propvalue) {
960                                         /* pass */
961                                 }
962                                 else {
963                                         uiDefBlockBut(block, operator_search_menu, kmi, "", xstart, te->ys + 1, butw1, UI_UNIT_Y - 1,
964                                                       TIP_("Assign new Operator"));
965                                 }
966                                 xstart += butw1 + 10;
967                                 
968                                 /* map type button */
969                                 kmi->maptype = keymap_menu_type(kmi->type);
970                                 
971                                 str = keymap_type_menu();
972                                 but = uiDefButS(block, MENU, 0, str,    xstart, te->ys + 1, butw2, UI_UNIT_Y - 1, &kmi->maptype,
973                                                 0, 0, 0, 0, TIP_("Event type"));
974                                 uiButSetFunc(but, keymap_type_cb, kmi, NULL);
975                                 xstart += butw2 + 5;
976                                 
977                                 /* edit actual event */
978                                 switch (kmi->maptype) {
979                                         case OL_KM_KEYBOARD:
980                                                 uiDefKeyevtButS(block, 0, "", xstart, te->ys + 1, butw2, UI_UNIT_Y - 1, &kmi->type,
981                                                                 TIP_("Key code"));
982                                                 xstart += butw2 + 5;
983                                                 break;
984                                         case OL_KM_MOUSE:
985                                                 str = keymap_mouse_menu();
986                                                 uiDefButS(block, MENU, 0, str, xstart, te->ys + 1, butw2, UI_UNIT_Y - 1, &kmi->type,
987                                                           0, 0, 0, 0, TIP_("Mouse button"));
988                                                 xstart += butw2 + 5;
989                                                 break;
990                                         case OL_KM_TWEAK:
991                                                 str = keymap_tweak_menu();
992                                                 uiDefButS(block, MENU, 0, str, xstart, te->ys + 1, butw2, UI_UNIT_Y - 1, &kmi->type,
993                                                           0, 0, 0, 0, TIP_("Tweak gesture"));
994                                                 xstart += butw2 + 5;
995                                                 str = keymap_tweak_dir_menu();
996                                                 uiDefButS(block, MENU, 0, str, xstart, te->ys + 1, butw2, UI_UNIT_Y - 1, &kmi->val,
997                                                           0, 0, 0, 0, TIP_("Tweak gesture direction"));
998                                                 xstart += butw2 + 5;
999                                                 break;
1000                                 }
1001                                 
1002                                 /* modifiers */
1003                                 uiDefButS(block, OPTION, 0, IFACE_("Shift"), xstart, te->ys + 1, butw3 + 5, UI_UNIT_Y - 1,
1004                                           &kmi->shift, 0, 0, 0, 0, TIP_("Modifier"));
1005                                 xstart += butw3 + 5;
1006                                 uiDefButS(block, OPTION, 0, IFACE_("Ctrl"), xstart, te->ys + 1, butw3, UI_UNIT_Y - 1, &kmi->ctrl,
1007                                           0, 0, 0, 0, TIP_("Modifier"));
1008                                 xstart += butw3;
1009                                 uiDefButS(block, OPTION, 0, IFACE_("Alt"), xstart, te->ys + 1, butw3, UI_UNIT_Y - 1, &kmi->alt,
1010                                           0, 0, 0, 0, TIP_("Modifier"));
1011                                 xstart += butw3;
1012                                 uiDefButS(block, OPTION, 0, IFACE_("OS"), xstart, te->ys + 1, butw3, UI_UNIT_Y - 1, &kmi->oskey,
1013                                           0, 0, 0, 0, TIP_("Modifier"));
1014                                 xstart += butw3 + 5;
1015                                 uiDefKeyevtButS(block, 0, "", xstart, te->ys + 1, butw3, UI_UNIT_Y - 1, &kmi->keymodifier,
1016                                                 TIP_("Key Modifier code"));
1017                                 xstart += butw3 + 5;
1018                                 
1019                                 /* rna property */
1020                                 if (kmi->ptr && kmi->ptr->data) {
1021                                         uiDefBut(block, LABEL, 0, IFACE_("(RNA property)"), xstart, te->ys + 1, butw2, UI_UNIT_Y - 1,
1022                                                  NULL, 0, 0, 0, 0, "");
1023                                         xstart += butw2;
1024                                 }
1025
1026                                 (void)xstart;
1027                         }
1028                 }
1029                 
1030                 if (TSELEM_OPEN(tselem, soops)) outliner_draw_keymapbuts(block, ar, soops, &te->subtree);
1031         }
1032
1033         uiBlockSetEmboss(block, UI_EMBOSS);
1034 }
1035
1036
1037 static void outliner_buttons(const bContext *C, uiBlock *block, ARegion *ar, SpaceOops *soops, ListBase *lb)
1038 {
1039         uiBut *bt;
1040         TreeElement *te;
1041         TreeStoreElem *tselem;
1042         int spx, dx, len;
1043         
1044         for (te = lb->first; te; te = te->next) {
1045                 tselem = TREESTORE(te);
1046                 if (te->ys + 2 * UI_UNIT_Y >= ar->v2d.cur.ymin && te->ys <= ar->v2d.cur.ymax) {
1047                         
1048                         if (tselem->flag & TSE_TEXTBUT) {
1049                                 
1050                                 /* If we add support to rename Sequence.
1051                                  * need change this.
1052                                  */
1053                                 // prevent crash when trying to rename 'pose' entry of armature
1054                                 if (tselem->type == TSE_POSE_BASE) continue;  
1055                                 
1056                                 if (tselem->type == TSE_EBONE) len = sizeof(((EditBone *) 0)->name);
1057                                 else if (tselem->type == TSE_MODIFIER) len = sizeof(((ModifierData *) 0)->name);
1058                                 else if (tselem->id && GS(tselem->id->name) == ID_LI) len = sizeof(((Library *) 0)->name);
1059                                 else len = MAX_ID_NAME - 2;
1060                                 
1061
1062                                 dx = (int)UI_GetStringWidth(te->name);
1063                                 if (dx < 5 * UI_UNIT_X) dx = 5 * UI_UNIT_X;
1064                                 spx = te->xs + 1.8f * UI_UNIT_X;
1065                                 if (spx + dx + 0.5f * UI_UNIT_X > ar->v2d.cur.xmax) dx = ar->v2d.cur.xmax - spx - 0.5f * UI_UNIT_X;
1066
1067                                 bt = uiDefBut(block, TEX, OL_NAMEBUTTON, "", spx, te->ys, dx + UI_UNIT_X, UI_UNIT_Y - 1, (void *)te->name,
1068                                               1.0, (float)len, 0, 0, "");
1069                                 uiButSetRenameFunc(bt, namebutton_cb, tselem);
1070                                 
1071                                 /* returns false if button got removed */
1072                                 if (false == uiButActiveOnly(C, ar, block, bt)) {
1073                                         tselem->flag &= ~TSE_TEXTBUT;
1074                                 }
1075                         }
1076                 }
1077                 
1078                 if (TSELEM_OPEN(tselem, soops)) outliner_buttons(C, block, ar, soops, &te->subtree);
1079         }
1080 }
1081
1082 /* ****************************************************** */
1083 /* Normal Drawing... */
1084
1085 /* make function calls a bit compacter */
1086 struct DrawIconArg {
1087         uiBlock *block;
1088         ID *id;
1089         float xmax, x, y, xb, yb;
1090         float alpha;
1091 };
1092
1093 static void tselem_draw_icon_uibut(struct DrawIconArg *arg, int icon)
1094 {
1095         /* restrict column clip... it has been coded by simply overdrawing, doesnt work for buttons */
1096         if (arg->x >= arg->xmax) {
1097                 glEnable(GL_BLEND);
1098                 UI_icon_draw_aspect(arg->x, arg->y, icon, 1.0f / UI_DPI_FAC, arg->alpha);
1099                 glDisable(GL_BLEND);
1100         }
1101         else {
1102                 uiBut *but = uiDefIconBut(arg->block, LABEL, 0, icon, arg->xb, arg->yb, UI_UNIT_X, UI_UNIT_Y, NULL,
1103                                           0.0, 0.0, 1.0, arg->alpha, (arg->id && arg->id->lib) ? arg->id->lib->name : "");
1104                 
1105                 if (arg->id)
1106                         uiButSetDragID(but, arg->id);
1107         }
1108
1109 }
1110
1111 static void tselem_draw_icon(uiBlock *block, int xmax, float x, float y, TreeStoreElem *tselem, TreeElement *te,
1112                              float alpha)
1113 {
1114         struct DrawIconArg arg;
1115         float aspect;
1116         
1117         /* icons tiny bit away from text */
1118         x -= 0.15f * UI_UNIT_Y;
1119         
1120         /* make function calls a bit compacter */
1121         arg.block = block;
1122         arg.id = tselem->id;
1123         arg.xmax = xmax;
1124         arg.xb = x;     /* for ui buttons */
1125         arg.yb = y;
1126         arg.alpha = alpha;
1127         
1128         /* placement of icons, copied from interface_widgets.c */
1129         aspect = (0.8f * UI_UNIT_Y) / ICON_DEFAULT_HEIGHT;
1130         arg.x = x = x + 4.0f * aspect;
1131         arg.y = y = y + 0.1f * UI_UNIT_Y;
1132
1133         if (tselem->type) {
1134                 switch (tselem->type) {
1135                         case TSE_ANIM_DATA:
1136                                 UI_icon_draw(x, y, ICON_ANIM_DATA); break; // xxx
1137                         case TSE_NLA:
1138                                 UI_icon_draw(x, y, ICON_NLA); break;
1139                         case TSE_NLA_TRACK:
1140                                 UI_icon_draw(x, y, ICON_NLA); break; // XXX
1141                         case TSE_NLA_ACTION:
1142                                 UI_icon_draw(x, y, ICON_ACTION); break;
1143                         case TSE_DRIVER_BASE:
1144                                 UI_icon_draw(x, y, ICON_DRIVER); break;
1145                         case TSE_DEFGROUP_BASE:
1146                                 UI_icon_draw(x, y, ICON_GROUP_VERTEX); break;
1147                         case TSE_BONE:
1148                         case TSE_EBONE:
1149                                 UI_icon_draw(x, y, ICON_BONE_DATA); break;
1150                         case TSE_CONSTRAINT_BASE:
1151                                 UI_icon_draw(x, y, ICON_CONSTRAINT); break;
1152                         case TSE_MODIFIER_BASE:
1153                                 UI_icon_draw(x, y, ICON_MODIFIER); break;
1154                         case TSE_LINKED_OB:
1155                                 UI_icon_draw(x, y, ICON_OBJECT_DATA); break;
1156                         case TSE_LINKED_PSYS:
1157                                 UI_icon_draw(x, y, ICON_PARTICLES); break;
1158                         case TSE_MODIFIER:
1159                         {
1160                                 Object *ob = (Object *)tselem->id;
1161                                 ModifierData *md = BLI_findlink(&ob->modifiers, tselem->nr);
1162                                 switch ((ModifierType)md->type) {
1163                                         case eModifierType_Subsurf: 
1164                                                 UI_icon_draw(x, y, ICON_MOD_SUBSURF); break;
1165                                         case eModifierType_Armature: 
1166                                                 UI_icon_draw(x, y, ICON_MOD_ARMATURE); break;
1167                                         case eModifierType_Lattice: 
1168                                                 UI_icon_draw(x, y, ICON_MOD_LATTICE); break;
1169                                         case eModifierType_Curve: 
1170                                                 UI_icon_draw(x, y, ICON_MOD_CURVE); break;
1171                                         case eModifierType_Build: 
1172                                                 UI_icon_draw(x, y, ICON_MOD_BUILD); break;
1173                                         case eModifierType_Mirror: 
1174                                                 UI_icon_draw(x, y, ICON_MOD_MIRROR); break;
1175                                         case eModifierType_Decimate: 
1176                                                 UI_icon_draw(x, y, ICON_MOD_DECIM); break;
1177                                         case eModifierType_Wave: 
1178                                                 UI_icon_draw(x, y, ICON_MOD_WAVE); break;
1179                                         case eModifierType_Hook: 
1180                                                 UI_icon_draw(x, y, ICON_HOOK); break;
1181                                         case eModifierType_Softbody: 
1182                                                 UI_icon_draw(x, y, ICON_MOD_SOFT); break;
1183                                         case eModifierType_Boolean: 
1184                                                 UI_icon_draw(x, y, ICON_MOD_BOOLEAN); break;
1185                                         case eModifierType_ParticleSystem: 
1186                                                 UI_icon_draw(x, y, ICON_MOD_PARTICLES); break;
1187                                         case eModifierType_ParticleInstance:
1188                                                 UI_icon_draw(x, y, ICON_MOD_PARTICLES); break;
1189                                         case eModifierType_EdgeSplit:
1190                                                 UI_icon_draw(x, y, ICON_MOD_EDGESPLIT); break;
1191                                         case eModifierType_Array:
1192                                                 UI_icon_draw(x, y, ICON_MOD_ARRAY); break;
1193                                         case eModifierType_UVProject:
1194                                         case eModifierType_UVWarp:  /* TODO, get own icon */
1195                                                 UI_icon_draw(x, y, ICON_MOD_UVPROJECT); break;
1196                                         case eModifierType_Displace:
1197                                                 UI_icon_draw(x, y, ICON_MOD_DISPLACE); break;
1198                                         case eModifierType_Shrinkwrap:
1199                                                 UI_icon_draw(x, y, ICON_MOD_SHRINKWRAP); break;
1200                                         case eModifierType_Cast:
1201                                                 UI_icon_draw(x, y, ICON_MOD_CAST); break;
1202                                         case eModifierType_MeshDeform:
1203                                                 UI_icon_draw(x, y, ICON_MOD_MESHDEFORM); break;
1204                                         case eModifierType_Bevel:
1205                                                 UI_icon_draw(x, y, ICON_MOD_BEVEL); break;
1206                                         case eModifierType_Smooth:
1207                                         case eModifierType_LaplacianSmooth:
1208                                                 UI_icon_draw(x, y, ICON_MOD_SMOOTH); break;
1209                                         case eModifierType_SimpleDeform:
1210                                                 UI_icon_draw(x, y, ICON_MOD_SIMPLEDEFORM); break;
1211                                         case eModifierType_Mask:
1212                                                 UI_icon_draw(x, y, ICON_MOD_MASK); break;
1213                                         case eModifierType_Cloth:
1214                                                 UI_icon_draw(x, y, ICON_MOD_CLOTH); break;
1215                                         case eModifierType_Explode:
1216                                                 UI_icon_draw(x, y, ICON_MOD_EXPLODE); break;
1217                                         case eModifierType_Collision:
1218                                         case eModifierType_Surface:
1219                                                 UI_icon_draw(x, y, ICON_MOD_PHYSICS); break;
1220                                         case eModifierType_Fluidsim:
1221                                                 UI_icon_draw(x, y, ICON_MOD_FLUIDSIM); break;
1222                                         case eModifierType_Multires:
1223                                                 UI_icon_draw(x, y, ICON_MOD_MULTIRES); break;
1224                                         case eModifierType_Smoke:
1225                                                 UI_icon_draw(x, y, ICON_MOD_SMOKE); break;
1226                                         case eModifierType_Solidify:
1227                                                 UI_icon_draw(x, y, ICON_MOD_SOLIDIFY); break;
1228                                         case eModifierType_Screw:
1229                                                 UI_icon_draw(x, y, ICON_MOD_SCREW); break;
1230                                         case eModifierType_Remesh:
1231                                                 UI_icon_draw(x, y, ICON_MOD_REMESH); break;
1232                                         case eModifierType_WeightVGEdit:
1233                                         case eModifierType_WeightVGMix:
1234                                         case eModifierType_WeightVGProximity:
1235                                                 UI_icon_draw(x, y, ICON_MOD_VERTEX_WEIGHT); break;
1236                                         case eModifierType_DynamicPaint:
1237                                                 UI_icon_draw(x, y, ICON_MOD_DYNAMICPAINT); break;
1238                                         case eModifierType_Ocean:
1239                                                 UI_icon_draw(x, y, ICON_MOD_OCEAN); break;
1240                                         case eModifierType_Warp:
1241                                                 UI_icon_draw(x, y, ICON_MOD_WARP); break;
1242                                         case eModifierType_Skin:
1243                                                 UI_icon_draw(x, y, ICON_MOD_SKIN); break;
1244                                         case eModifierType_Triangulate:
1245                                                 UI_icon_draw(x, y, ICON_MOD_TRIANGULATE); break;
1246                                         case eModifierType_MeshCache:
1247                                                 UI_icon_draw(x, y, ICON_MOD_MESHDEFORM); break;  /* XXX, needs own icon */
1248                                         /* Default */
1249                                         case eModifierType_None:
1250                                         case eModifierType_ShapeKey:
1251                                         case NUM_MODIFIER_TYPES:
1252                                                 UI_icon_draw(x, y, ICON_DOT); break;
1253                                 }
1254                                 break;
1255                         }
1256                         case TSE_POSE_BASE:
1257                                 UI_icon_draw(x, y, ICON_ARMATURE_DATA); break;
1258                         case TSE_POSE_CHANNEL:
1259                                 UI_icon_draw(x, y, ICON_BONE_DATA); break;
1260                         case TSE_PROXY:
1261                                 UI_icon_draw(x, y, ICON_GHOST); break;
1262                         case TSE_R_LAYER_BASE:
1263                                 UI_icon_draw(x, y, ICON_RENDERLAYERS); break;
1264                         case TSE_R_LAYER:
1265                                 UI_icon_draw(x, y, ICON_RENDERLAYERS); break;
1266                         case TSE_LINKED_LAMP:
1267                                 UI_icon_draw(x, y, ICON_LAMP_DATA); break;
1268                         case TSE_LINKED_MAT:
1269                                 UI_icon_draw(x, y, ICON_MATERIAL_DATA); break;
1270                         case TSE_POSEGRP_BASE:
1271                                 UI_icon_draw(x, y, ICON_GROUP_BONE); break;
1272                         case TSE_SEQUENCE:
1273                                 if (te->idcode == SEQ_TYPE_MOVIE)
1274                                         UI_icon_draw(x, y, ICON_SEQUENCE);
1275                                 else if (te->idcode == SEQ_TYPE_META)
1276                                         UI_icon_draw(x, y, ICON_DOT);
1277                                 else if (te->idcode == SEQ_TYPE_SCENE)
1278                                         UI_icon_draw(x, y, ICON_SCENE);
1279                                 else if (te->idcode == SEQ_TYPE_SOUND_RAM)
1280                                         UI_icon_draw(x, y, ICON_SOUND);
1281                                 else if (te->idcode == SEQ_TYPE_IMAGE)
1282                                         UI_icon_draw(x, y, ICON_IMAGE_COL);
1283                                 else
1284                                         UI_icon_draw(x, y, ICON_PARTICLES);
1285                                 break;
1286                         case TSE_SEQ_STRIP:
1287                                 UI_icon_draw(x, y, ICON_LIBRARY_DATA_DIRECT);
1288                                 break;
1289                         case TSE_SEQUENCE_DUP:
1290                                 UI_icon_draw(x, y, ICON_OBJECT_DATA);
1291                                 break;
1292                         case TSE_RNA_STRUCT:
1293                                 if (RNA_struct_is_ID(te->rnaptr.type)) {
1294                                         arg.id = (ID *)te->rnaptr.data;
1295                                         tselem_draw_icon_uibut(&arg, RNA_struct_ui_icon(te->rnaptr.type));
1296                                 }
1297                                 else
1298                                         UI_icon_draw(x, y, RNA_struct_ui_icon(te->rnaptr.type));
1299                                 break;
1300                         default:
1301                                 UI_icon_draw(x, y, ICON_DOT); break;
1302                 }
1303         }
1304         else if (GS(tselem->id->name) == ID_OB) {
1305                 Object *ob = (Object *)tselem->id;
1306                 switch (ob->type) {
1307                         case OB_LAMP:
1308                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_LAMP); break;
1309                         case OB_MESH: 
1310                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_MESH); break;
1311                         case OB_CAMERA: 
1312                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_CAMERA); break;
1313                         case OB_CURVE: 
1314                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_CURVE); break;
1315                         case OB_MBALL: 
1316                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_META); break;
1317                         case OB_LATTICE: 
1318                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_LATTICE); break;
1319                         case OB_ARMATURE: 
1320                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_ARMATURE); break;
1321                         case OB_FONT: 
1322                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_FONT); break;
1323                         case OB_SURF: 
1324                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_SURFACE); break;
1325                         case OB_SPEAKER:
1326                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_SPEAKER); break;
1327                         case OB_EMPTY: 
1328                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_OB_EMPTY); break;
1329                 
1330                 }
1331         }
1332         else {
1333                 switch (GS(tselem->id->name)) {
1334                         case ID_SCE:
1335                                 tselem_draw_icon_uibut(&arg, ICON_SCENE_DATA); break;
1336                         case ID_ME:
1337                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_MESH); break;
1338                         case ID_CU:
1339                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_CURVE); break;
1340                         case ID_MB:
1341                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_META); break;
1342                         case ID_LT:
1343                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_LATTICE); break;
1344                         case ID_LA:
1345                         {
1346                                 Lamp *la = (Lamp *)tselem->id;
1347                                 
1348                                 switch (la->type) {
1349                                         case LA_LOCAL:
1350                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_POINT); break;
1351                                         case LA_SUN:
1352                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_SUN); break;
1353                                         case LA_SPOT:
1354                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_SPOT); break;
1355                                         case LA_HEMI:
1356                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_HEMI); break;
1357                                         case LA_AREA:
1358                                                 tselem_draw_icon_uibut(&arg, ICON_LAMP_AREA); break;
1359                                         default:
1360                                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_LAMP); break;
1361                                 }
1362                                 break;
1363                         }
1364                         case ID_MA:
1365                                 tselem_draw_icon_uibut(&arg, ICON_MATERIAL_DATA); break;
1366                         case ID_TE:
1367                                 tselem_draw_icon_uibut(&arg, ICON_TEXTURE_DATA); break;
1368                         case ID_IM:
1369                                 tselem_draw_icon_uibut(&arg, ICON_IMAGE_DATA); break;
1370                         case ID_SPK:
1371                         case ID_SO:
1372                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_SPEAKER); break;
1373                         case ID_AR:
1374                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_ARMATURE); break;
1375                         case ID_CA:
1376                                 tselem_draw_icon_uibut(&arg, ICON_OUTLINER_DATA_CAMERA); break;
1377                         case ID_KE:
1378                                 tselem_draw_icon_uibut(&arg, ICON_SHAPEKEY_DATA); break;
1379                         case ID_WO:
1380                                 tselem_draw_icon_uibut(&arg, ICON_WORLD_DATA); break;
1381                         case ID_AC:
1382                                 tselem_draw_icon_uibut(&arg, ICON_ACTION); break;
1383                         case ID_NLA:
1384                                 tselem_draw_icon_uibut(&arg, ICON_NLA); break;
1385                         case ID_TXT:
1386                                 tselem_draw_icon_uibut(&arg, ICON_SCRIPT); break;
1387                         case ID_GR:
1388                                 tselem_draw_icon_uibut(&arg, ICON_GROUP); break;
1389                         case ID_LI:
1390                                 tselem_draw_icon_uibut(&arg, ICON_LIBRARY_DATA_DIRECT); break;
1391                 }
1392         }
1393 }
1394
1395 static void outliner_draw_iconrow(bContext *C, uiBlock *block, Scene *scene, SpaceOops *soops, ListBase *lb, int level,
1396                                   int xmax, int *offsx, int ys)
1397 {
1398         TreeElement *te;
1399         TreeStoreElem *tselem;
1400         int active;
1401
1402         for (te = lb->first; te; te = te->next) {
1403                 
1404                 /* exit drawing early */
1405                 if ((*offsx) - UI_UNIT_X > xmax)
1406                         break;
1407
1408                 tselem = TREESTORE(te);
1409                 
1410                 /* object hierarchy always, further constrained on level */
1411                 if (level < 1 || (tselem->type == 0 && te->idcode == ID_OB)) {
1412
1413                         /* active blocks get white circle */
1414                         if (tselem->type == 0) {
1415                                 if (te->idcode == ID_OB) {
1416                                         active = (OBACT == (Object *)tselem->id);
1417                                 }
1418                                 else if (scene->obedit && scene->obedit->data == tselem->id) {
1419                                         active = 1;  // XXX use context?
1420                                 }
1421                                 else {
1422                                         active = tree_element_active(C, scene, soops, te, 0);
1423                                 }
1424                         }
1425                         else {
1426                                 active = tree_element_type_active(NULL, scene, soops, te, tselem, 0, false);
1427                         }
1428
1429                         if (active) {
1430                                 float ufac = UI_UNIT_X / 20.0f;
1431
1432                                 uiSetRoundBox(UI_CNR_ALL);
1433                                 glColor4ub(255, 255, 255, 100);
1434                                 uiRoundBox((float) *offsx - 1.0f * ufac,
1435                                            (float)ys + 1.0f * ufac,
1436                                            (float)*offsx + UI_UNIT_X - 2.0f * ufac,
1437                                            (float)ys + UI_UNIT_Y - ufac,
1438                                            (float)UI_UNIT_Y / 2.0f - ufac);
1439                                 glEnable(GL_BLEND); /* roundbox disables */
1440                         }
1441                         
1442                         tselem_draw_icon(block, xmax, (float)*offsx, (float)ys, tselem, te, 0.5f);
1443                         te->xs = *offsx;
1444                         te->ys = ys;
1445                         te->xend = (short)*offsx + UI_UNIT_X;
1446                         te->flag |= TE_ICONROW; // for click
1447                         
1448                         (*offsx) += UI_UNIT_X;
1449                 }
1450                 
1451                 /* this tree element always has same amount of branches, so don't draw */
1452                 if (tselem->type != TSE_R_LAYER)
1453                         outliner_draw_iconrow(C, block, scene, soops, &te->subtree, level + 1, xmax, offsx, ys);
1454         }
1455         
1456 }
1457
1458 /* closed tree element */
1459 static void outliner_set_coord_tree_element(SpaceOops *soops, TreeElement *te, int startx, int starty)
1460 {
1461         TreeElement *ten;
1462         
1463         /* store coord and continue, we need coordinates for elements outside view too */
1464         te->xs = startx;
1465         te->ys = starty;
1466         
1467         for (ten = te->subtree.first; ten; ten = ten->next) {
1468                 outliner_set_coord_tree_element(soops, ten, startx + UI_UNIT_X, starty);
1469         }
1470 }
1471
1472
1473 static void outliner_draw_tree_element(bContext *C, uiBlock *block, Scene *scene, ARegion *ar, SpaceOops *soops,
1474                                        TreeElement *te, int startx, int *starty)
1475 {
1476         TreeElement *ten;
1477         TreeStoreElem *tselem;
1478         float ufac = UI_UNIT_X / 20.0f;
1479         int offsx = 0, active = 0; // active=1 active obj, else active data
1480         
1481         tselem = TREESTORE(te);
1482
1483         if (*starty + 2 * UI_UNIT_Y >= ar->v2d.cur.ymin && *starty <= ar->v2d.cur.ymax) {
1484                 int xmax = ar->v2d.cur.xmax;
1485                 unsigned char alpha = 128;
1486                 
1487                 /* icons can be ui buts, we don't want it to overlap with restrict */
1488                 if ((soops->flag & SO_HIDE_RESTRICTCOLS) == 0)
1489                         xmax -= OL_TOGW + UI_UNIT_X;
1490                 
1491                 glEnable(GL_BLEND);
1492
1493                 /* start by highlighting search matches 
1494                  *      we don't expand items when searching in the datablocks but we 
1495                  *      still want to highlight any filter matches. 
1496                  */
1497                 if ((SEARCHING_OUTLINER(soops) || (soops->outlinevis == SO_DATABLOCKS && soops->search_string[0] != 0)) &&
1498                     (tselem->flag & TSE_SEARCHMATCH))
1499                 {
1500                         char col[4];
1501                         UI_GetThemeColorType4ubv(TH_MATCH, SPACE_OUTLINER, col);
1502                         col[3] = alpha;
1503                         glColor4ubv((GLubyte *)col);
1504                         glRecti(startx, *starty + 1, ar->v2d.cur.xmax, *starty + UI_UNIT_Y - 1);
1505                 }
1506
1507                 /* colors for active/selected data */
1508                 if (tselem->type == 0) {
1509                         
1510                         if (te->idcode == ID_SCE) {
1511                                 if (tselem->id == (ID *)scene) {
1512                                         glColor4ub(255, 255, 255, alpha);
1513                                         active = 2;
1514                                 }
1515                         }
1516                         else if (te->idcode == ID_GR) {
1517                                 Group *gr = (Group *)tselem->id;
1518                                 if (group_select_flag(gr)) {
1519                                         char col[4];
1520                                         UI_GetThemeColorType4ubv(TH_SELECT, SPACE_VIEW3D, col);
1521                                         col[3] = alpha;
1522                                         glColor4ubv((GLubyte *)col);
1523                                         
1524                                         active = 2;
1525                                 }
1526                         }
1527                         else if (te->idcode == ID_OB) {
1528                                 Object *ob = (Object *)tselem->id;
1529                                 
1530                                 if (ob == OBACT || (ob->flag & SELECT)) {
1531                                         char col[4] = {0, 0, 0, 0};
1532                                         
1533                                         /* outliner active ob: always white text, circle color now similar to view3d */
1534                                         
1535                                         active = 2; /* means it draws a color circle */
1536                                         if (ob == OBACT) {
1537                                                 if (ob->flag & SELECT) {
1538                                                         UI_GetThemeColorType4ubv(TH_ACTIVE, SPACE_VIEW3D, col);
1539                                                         col[3] = alpha;
1540                                                 }
1541                                                 
1542                                                 active = 1; /* means it draws white text */
1543                                         }
1544                                         else if (ob->flag & SELECT) {
1545                                                 UI_GetThemeColorType4ubv(TH_SELECT, SPACE_VIEW3D, col);
1546                                                 col[3] = alpha;
1547                                         }
1548                                         
1549                                         glColor4ubv((GLubyte *)col);
1550                                 }
1551                         
1552                         }
1553                         else if (scene->obedit && scene->obedit->data == tselem->id) {
1554                                 glColor4ub(255, 255, 255, alpha);
1555                                 active = 2;
1556                         }
1557                         else {
1558                                 if (tree_element_active(C, scene, soops, te, 0)) {
1559                                         glColor4ub(220, 220, 255, alpha);
1560                                         active = 2;
1561                                 }
1562                         }
1563                 }
1564                 else {
1565                         if (tree_element_type_active(NULL, scene, soops, te, tselem, 0, false) ) active = 2;
1566                         glColor4ub(220, 220, 255, alpha);
1567                 }
1568                 
1569                 /* active circle */
1570                 if (active) {
1571                         uiSetRoundBox(UI_CNR_ALL);
1572                         uiRoundBox((float)startx + UI_UNIT_X,
1573                                    (float)*starty + 1.0f * ufac,
1574                                    (float)startx + 2.0f * UI_UNIT_X - 2.0f * ufac,
1575                                    (float)*starty + UI_UNIT_Y - 1.0f * ufac,
1576                                    UI_UNIT_Y / 2.0f - 1.0f * ufac);
1577                         glEnable(GL_BLEND); /* roundbox disables it */
1578                         
1579                         te->flag |= TE_ACTIVE; // for lookup in display hierarchies
1580                 }
1581                 
1582                 /* open/close icon, only when sublevels, except for scene */
1583                 if (te->subtree.first || (tselem->type == 0 && te->idcode == ID_SCE) || (te->flag & TE_LAZY_CLOSED)) {
1584                         int icon_x;
1585                         if (tselem->type == 0 && ELEM(te->idcode, ID_OB, ID_SCE))
1586                                 icon_x = startx;
1587                         else
1588                                 icon_x = startx + 5 * ufac;
1589                         
1590                         // icons a bit higher
1591                         if (TSELEM_OPEN(tselem, soops))
1592                                 UI_icon_draw((float)icon_x, (float)*starty + 2 * ufac, ICON_DISCLOSURE_TRI_DOWN);
1593                         else
1594                                 UI_icon_draw((float)icon_x, (float)*starty + 2 * ufac, ICON_DISCLOSURE_TRI_RIGHT);
1595                 }
1596                 offsx += UI_UNIT_X;
1597                 
1598                 /* datatype icon */
1599                 
1600                 if (!(ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM))) {
1601                         
1602                         tselem_draw_icon(block, xmax, (float)startx + offsx, (float)*starty, tselem, te, 1.0f);
1603                         
1604                         offsx += UI_UNIT_X;
1605                 }
1606                 else
1607                         offsx += 2 * ufac;
1608                 
1609                 if (tselem->type == 0 && tselem->id->lib) {
1610                         glPixelTransferf(GL_ALPHA_SCALE, 0.5f);
1611                         if (tselem->id->flag & LIB_INDIRECT)
1612                                 UI_icon_draw((float)startx + offsx, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_INDIRECT);
1613                         else
1614                                 UI_icon_draw((float)startx + offsx, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_DIRECT);
1615                         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1616                         offsx += UI_UNIT_X;
1617                 }
1618                 glDisable(GL_BLEND);
1619                 
1620                 /* name */
1621                 if (active == 1) UI_ThemeColor(TH_TEXT_HI);
1622                 else if (ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) UI_ThemeColorBlend(TH_BACK, TH_TEXT, 0.75f);
1623                 else UI_ThemeColor(TH_TEXT);
1624                 
1625                 UI_DrawString(startx + offsx, *starty + 5 * ufac, te->name);
1626                 
1627                 offsx += (int)(UI_UNIT_X + UI_GetStringWidth(te->name));
1628                 
1629                 /* closed item, we draw the icons, not when it's a scene, or master-server list though */
1630                 if (!TSELEM_OPEN(tselem, soops)) {
1631                         if (te->subtree.first) {
1632                                 if (tselem->type == 0 && te->idcode == ID_SCE) {
1633                                         /* pass */
1634                                 }
1635                                 else if (tselem->type != TSE_R_LAYER) {
1636                                         /* this tree element always has same amount of branches, so don't draw */
1637
1638                                         int tempx = startx + offsx;
1639                                         
1640                                         /* divider */
1641                                         UI_ThemeColorShade(TH_BACK, -40);
1642                                         glRecti(tempx   - 10.0f * ufac,
1643                                                 *starty +  4.0f * ufac,
1644                                                 tempx   -  8.0f * ufac,
1645                                                 *starty + UI_UNIT_Y - 4.0f * ufac);
1646                                         
1647                                         glEnable(GL_BLEND);
1648                                         glPixelTransferf(GL_ALPHA_SCALE, 0.5);
1649                                         
1650                                         outliner_draw_iconrow(C, block, scene, soops, &te->subtree, 0, xmax, &tempx, *starty);
1651                                         
1652                                         glPixelTransferf(GL_ALPHA_SCALE, 1.0);
1653                                         glDisable(GL_BLEND);
1654                                 }
1655                         }
1656                 }
1657         }
1658         /* store coord and continue, we need coordinates for elements outside view too */
1659         te->xs = startx;
1660         te->ys = *starty;
1661         te->xend = startx + offsx;
1662                 
1663         if (TSELEM_OPEN(tselem, soops)) {
1664                 *starty -= UI_UNIT_Y;
1665                 
1666                 for (ten = te->subtree.first; ten; ten = ten->next)
1667                         outliner_draw_tree_element(C, block, scene, ar, soops, ten, startx + UI_UNIT_X, starty);
1668         }
1669         else {
1670                 for (ten = te->subtree.first; ten; ten = ten->next)
1671                         outliner_set_coord_tree_element(soops, ten, startx, *starty);
1672                 
1673                 *starty -= UI_UNIT_Y;
1674         }
1675 }
1676
1677 static void outliner_draw_hierarchy(SpaceOops *soops, ListBase *lb, int startx, int *starty)
1678 {
1679         TreeElement *te;
1680         TreeStoreElem *tselem;
1681         int y1, y2;
1682         
1683         if (lb->first == NULL) return;
1684         
1685         y1 = y2 = *starty; /* for vertical lines between objects */
1686         for (te = lb->first; te; te = te->next) {
1687                 y2 = *starty;
1688                 tselem = TREESTORE(te);
1689                 
1690                 /* horizontal line? */
1691                 if (tselem->type == 0 && (te->idcode == ID_OB || te->idcode == ID_SCE))
1692                         glRecti(startx, *starty, startx + UI_UNIT_X, *starty - 1);
1693                         
1694                 *starty -= UI_UNIT_Y;
1695                 
1696                 if (TSELEM_OPEN(tselem, soops))
1697                         outliner_draw_hierarchy(soops, &te->subtree, startx + UI_UNIT_X, starty);
1698         }
1699         
1700         /* vertical line */
1701         te = lb->last;
1702         if (te->parent || lb->first != lb->last) {
1703                 tselem = TREESTORE(te);
1704                 if (tselem->type == 0 && te->idcode == ID_OB) {
1705                         
1706                         glRecti(startx, y1 + UI_UNIT_Y, startx + 1, y2);
1707                 }
1708         }
1709 }
1710
1711 static void outliner_draw_struct_marks(ARegion *ar, SpaceOops *soops, ListBase *lb, int *starty) 
1712 {
1713         TreeElement *te;
1714         TreeStoreElem *tselem;
1715         
1716         for (te = lb->first; te; te = te->next) {
1717                 tselem = TREESTORE(te);
1718                 
1719                 /* selection status */
1720                 if (TSELEM_OPEN(tselem, soops))
1721                         if (tselem->type == TSE_RNA_STRUCT)
1722                                 glRecti(0, *starty + 1, (int)ar->v2d.cur.xmax, *starty + UI_UNIT_Y - 1);
1723
1724                 *starty -= UI_UNIT_Y;
1725                 if (TSELEM_OPEN(tselem, soops)) {
1726                         outliner_draw_struct_marks(ar, soops, &te->subtree, starty);
1727                         if (tselem->type == TSE_RNA_STRUCT)
1728                                 fdrawline(0, (float)*starty + UI_UNIT_Y, ar->v2d.cur.xmax, (float)*starty + UI_UNIT_Y);
1729                 }
1730         }
1731 }
1732
1733 static void outliner_draw_selection(ARegion *ar, SpaceOops *soops, ListBase *lb, int *starty) 
1734 {
1735         TreeElement *te;
1736         TreeStoreElem *tselem;
1737         
1738         for (te = lb->first; te; te = te->next) {
1739                 tselem = TREESTORE(te);
1740                 
1741                 /* selection status */
1742                 if (tselem->flag & TSE_SELECTED) {
1743                         glRecti(0, *starty + 1, (int)ar->v2d.cur.xmax, *starty + UI_UNIT_Y - 1);
1744                 }
1745                 *starty -= UI_UNIT_Y;
1746                 if (TSELEM_OPEN(tselem, soops)) outliner_draw_selection(ar, soops, &te->subtree, starty);
1747         }
1748 }
1749
1750
1751 static void outliner_draw_tree(bContext *C, uiBlock *block, Scene *scene, ARegion *ar, SpaceOops *soops)
1752 {
1753         TreeElement *te;
1754         int starty, startx;
1755         float col[3];
1756                 
1757         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); // only once
1758         
1759         if (ELEM(soops->outlinevis, SO_DATABLOCKS, SO_USERDEF)) {
1760                 /* struct marks */
1761                 UI_ThemeColorShadeAlpha(TH_BACK, -15, -200);
1762                 //UI_ThemeColorShade(TH_BACK, -20);
1763                 starty = (int)ar->v2d.tot.ymax - UI_UNIT_Y - OL_Y_OFFSET;
1764                 outliner_draw_struct_marks(ar, soops, &soops->tree, &starty);
1765         }
1766         
1767         /* always draw selection fill before hierarchy */
1768         UI_GetThemeColor3fv(TH_SELECT_HIGHLIGHT, col);
1769         glColor3fv(col);
1770         starty = (int)ar->v2d.tot.ymax - UI_UNIT_Y - OL_Y_OFFSET;
1771         outliner_draw_selection(ar, soops, &soops->tree, &starty);
1772         
1773         // gray hierarchy lines
1774         UI_ThemeColorBlend(TH_BACK, TH_TEXT, 0.4f);
1775         starty = (int)ar->v2d.tot.ymax - UI_UNIT_Y / 2 - OL_Y_OFFSET;
1776         startx = 6;
1777         outliner_draw_hierarchy(soops, &soops->tree, startx, &starty);
1778         
1779         // items themselves
1780         starty = (int)ar->v2d.tot.ymax - UI_UNIT_Y - OL_Y_OFFSET;
1781         startx = 0;
1782         for (te = soops->tree.first; te; te = te->next) {
1783                 outliner_draw_tree_element(C, block, scene, ar, soops, te, startx, &starty);
1784         }
1785 }
1786
1787
1788 static void outliner_back(ARegion *ar)
1789 {
1790         int ystart;
1791         
1792         UI_ThemeColorShade(TH_BACK, 6);
1793         ystart = (int)ar->v2d.tot.ymax;
1794         ystart = UI_UNIT_Y * (ystart / (UI_UNIT_Y)) - OL_Y_OFFSET;
1795         
1796         while (ystart + 2 * UI_UNIT_Y > ar->v2d.cur.ymin) {
1797                 glRecti(0, ystart, (int)ar->v2d.cur.xmax, ystart + UI_UNIT_Y);
1798                 ystart -= 2 * UI_UNIT_Y;
1799         }
1800 }
1801
1802 static void outliner_draw_restrictcols(ARegion *ar)
1803 {
1804         int ystart;
1805         
1806         /* background underneath */
1807         UI_ThemeColor(TH_BACK);
1808         glRecti((int)(ar->v2d.cur.xmax - OL_TOGW),
1809                 (int)(ar->v2d.cur.ymin - 1), (int)ar->v2d.cur.xmax, (int)ar->v2d.cur.ymax);
1810         
1811         UI_ThemeColorShade(TH_BACK, 6);
1812         ystart = (int)ar->v2d.tot.ymax;
1813         ystart = UI_UNIT_Y * (ystart / (UI_UNIT_Y)) - OL_Y_OFFSET;
1814         
1815         while (ystart + 2 * UI_UNIT_Y > ar->v2d.cur.ymin) {
1816                 glRecti((int)ar->v2d.cur.xmax - OL_TOGW, ystart, (int)ar->v2d.cur.xmax, ystart + UI_UNIT_Y);
1817                 ystart -= 2 * UI_UNIT_Y;
1818         }
1819         
1820         UI_ThemeColorShadeAlpha(TH_BACK, -15, -200);
1821
1822         /* view */
1823         sdrawline((int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX),
1824                   (int)ar->v2d.cur.ymax,
1825                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX),
1826                   (int)ar->v2d.cur.ymin);
1827
1828         /* render */
1829         sdrawline((int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX),
1830                   (int)ar->v2d.cur.ymax,
1831                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_SELECTX),
1832                   (int)ar->v2d.cur.ymin);
1833
1834         /* render */
1835         sdrawline((int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX),
1836                   (int)ar->v2d.cur.ymax,
1837                   (int)(ar->v2d.cur.xmax - OL_TOG_RESTRICT_RENDERX),
1838                   (int)ar->v2d.cur.ymin);
1839 }
1840
1841 /* ****************************************************** */
1842 /* Main Entrypoint - Draw contents of Outliner editor */
1843
1844 void draw_outliner(const bContext *C)
1845 {
1846         Main *mainvar = CTX_data_main(C); 
1847         Scene *scene = CTX_data_scene(C);
1848         ARegion *ar = CTX_wm_region(C);
1849         View2D *v2d = &ar->v2d;
1850         SpaceOops *soops = CTX_wm_space_outliner(C);
1851         uiBlock *block;
1852         int sizey = 0, sizex = 0, sizex_rna = 0;
1853
1854         outliner_build_tree(mainvar, scene, soops); // always
1855         
1856         /* get extents of data */
1857         outliner_height(soops, &soops->tree, &sizey);
1858
1859         if (ELEM3(soops->outlinevis, SO_DATABLOCKS, SO_USERDEF, SO_KEYMAP)) {
1860                 /* RNA has two columns:
1861                  *  - column 1 is (max_width + OL_RNA_COL_SPACEX) or
1862                  *                               (OL_RNA_COL_X), whichever is wider...
1863                  *      - column 2 is fixed at OL_RNA_COL_SIZEX
1864                  *
1865                  *  (*) XXX max width for now is a fixed factor of (UI_UNIT_X * (max_indention + 100))
1866                  */
1867                  
1868                 /* get actual width of column 1 */
1869                 outliner_rna_width(soops, &soops->tree, &sizex_rna, 0);
1870                 sizex_rna = max_ii(OL_RNA_COLX, sizex_rna + OL_RNA_COL_SPACEX);
1871                 
1872                 /* get width of data (for setting 'tot' rect, this is column 1 + column 2 + a bit extra) */
1873                 if (soops->outlinevis == SO_KEYMAP)
1874                         // XXX this is only really a quick hack to make this wide enough...
1875                         sizex = sizex_rna + OL_RNA_COL_SIZEX * 3 + 50;
1876                 else
1877                         sizex = sizex_rna + OL_RNA_COL_SIZEX + 50;
1878         }
1879         else {
1880                 /* width must take into account restriction columns (if visible) so that entries will still be visible */
1881                 //outliner_width(soops, &soops->tree, &sizex);
1882                 // XXX should use outliner_width instead when te->xend will be set correctly...
1883                 outliner_rna_width(soops, &soops->tree, &sizex, 0);
1884                 
1885                 /* constant offset for restriction columns */
1886                 // XXX this isn't that great yet...
1887                 if ((soops->flag & SO_HIDE_RESTRICTCOLS) == 0)
1888                         sizex += OL_TOGW * 3;
1889                 
1890         }
1891         
1892         /* adds vertical offset */
1893         sizey += OL_Y_OFFSET;
1894
1895         /* update size of tot-rect (extents of data/viewable area) */
1896         UI_view2d_totRect_set(v2d, sizex, sizey);
1897
1898         /* force display to pixel coords */
1899         v2d->flag |= (V2D_PIXELOFS_X | V2D_PIXELOFS_Y);
1900         /* set matrix for 2d-view controls */
1901         UI_view2d_view_ortho(v2d);
1902
1903         /* draw outliner stuff (background, hierachy lines and names) */
1904         outliner_back(ar);
1905         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1906         outliner_draw_tree((bContext *)C, block, scene, ar, soops);
1907         
1908         if (ELEM(soops->outlinevis, SO_DATABLOCKS, SO_USERDEF)) {
1909                 /* draw rna buttons */
1910                 outliner_draw_rnacols(ar, sizex_rna);
1911                 outliner_draw_rnabuts(block, scene, ar, soops, sizex_rna, &soops->tree);
1912         }
1913         else if (soops->outlinevis == SO_KEYMAP) {
1914                 outliner_draw_keymapbuts(block, ar, soops, &soops->tree);
1915         }
1916         else if (!(soops->flag & SO_HIDE_RESTRICTCOLS)) {
1917                 /* draw restriction columns */
1918                 outliner_draw_restrictcols(ar);
1919                 outliner_draw_restrictbuts(block, scene, ar, soops, &soops->tree);
1920         }
1921
1922         /* draw edit buttons if nessecery */
1923         outliner_buttons(C, block, ar, soops, &soops->tree);
1924
1925         uiEndBlock(C, block);
1926         uiDrawBlock(C, block);
1927
1928         /* clear flag that allows quick redraws */
1929         soops->storeflag &= ~SO_TREESTORE_REDRAW;
1930