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