Rename any instance of scene layer or render layer in code with view layer
[blender.git] / source / blender / editors / space_buttons / buttons_texture.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) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_buttons/buttons_texture.c
27  *  \ingroup spbuttons
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_listbase.h"
37 #include "BLI_string.h"
38 #include "BLI_utildefines.h"
39
40 #include "BLT_translation.h"
41
42 #include "DNA_brush_types.h"
43 #include "DNA_ID.h"
44 #include "DNA_lamp_types.h"
45 #include "DNA_material_types.h"
46 #include "DNA_node_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_object_force.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_world_types.h"
54 #include "DNA_linestyle_types.h"
55
56 #include "BKE_context.h"
57 #include "BKE_layer.h"
58 #include "BKE_linestyle.h"
59 #include "BKE_material.h"
60 #include "BKE_modifier.h"
61 #include "BKE_node.h"
62 #include "BKE_paint.h"
63 #include "BKE_particle.h"
64 #include "BKE_scene.h"
65 #ifdef WITH_FREESTYLE
66 #  include "BKE_freestyle.h"
67 #endif
68
69 #include "RNA_access.h"
70
71 #include "UI_interface.h"
72 #include "UI_resources.h"
73
74 #include "ED_buttons.h"
75 #include "ED_node.h"
76 #include "ED_screen.h"
77
78 #include "../interface/interface_intern.h"
79
80 #include "buttons_intern.h" // own include
81
82 /****************** "Old Shading" Texture Context ****************/
83
84 bool ED_texture_context_check_world(const bContext *C)
85 {
86         Scene *scene = CTX_data_scene(C);
87         return (scene && scene->world);
88 }
89
90 bool ED_texture_context_check_material(const bContext *C)
91 {
92         Object *ob = CTX_data_active_object(C);
93         return (ob && (ob->totcol != 0));
94 }
95
96 bool ED_texture_context_check_lamp(const bContext *C)
97 {
98         Object *ob = CTX_data_active_object(C);
99         return (ob && (ob->type == OB_LAMP));
100 }
101
102 bool ED_texture_context_check_particles(const bContext *C)
103 {
104         Object *ob = CTX_data_active_object(C);
105         return (ob && ob->particlesystem.first);
106 }
107
108 bool ED_texture_context_check_linestyle(const bContext *C)
109 {
110 #ifdef WITH_FREESTYLE
111         Scene *scene = CTX_data_scene(C);
112         ViewLayer *active_view_layer;
113         FreestyleConfig *config;
114         FreestyleLineSet *lineset;
115         FreestyleLineStyle *linestyle;
116
117         if (scene && (scene->r.mode & R_EDGE_FRS)) {
118                 active_view_layer = BLI_findlink(&scene->view_layers, scene->active_view_layer);
119                 config = &active_view_layer->freestyle_config;
120                 if (config->mode == FREESTYLE_CONTROL_EDITOR_MODE) {
121                         lineset = BKE_freestyle_lineset_get_active(config);
122                         if (lineset) {
123                                 linestyle = lineset->linestyle;
124                                 return linestyle && (linestyle->flag & LS_TEXTURE);
125                         }
126                 }
127         }
128 #else
129         (void)C;
130 #endif
131         return false;
132 }
133
134 static void texture_context_check_modifier_foreach(void *userData, Object *UNUSED(ob), ModifierData *UNUSED(md),
135                                                    const char *UNUSED(propname))
136 {
137         *((bool *)userData) = true;
138 }
139
140 bool ED_texture_context_check_others(const bContext *C)
141 {
142         /* We cannot rely on sbuts->texuser here, as it is NULL when in "old" tex handling, non-OTHERS tex context. */
143         Object *ob = CTX_data_active_object(C);
144
145         /* object */
146         if (ob) {
147                 /* Tex force field. */
148                 if (ob->pd && ob->pd->forcefield == PFIELD_TEXTURE) {
149                         return true;
150                 }
151
152                 /* modifiers */
153                 {
154                         bool check = false;
155                         modifiers_foreachTexLink(ob, texture_context_check_modifier_foreach, &check);
156                         if (check) {
157                                 return true;
158                         }
159                 }
160         }
161
162         /* brush */
163         if (BKE_paint_brush(BKE_paint_get_active_from_context(C))) {
164                 return true;
165         }
166
167         return false;
168 }
169
170 static void set_texture_context(const bContext *C, SpaceButs *sbuts)
171 {
172         Scene *scene = CTX_data_scene(C);
173
174         if (BKE_scene_use_new_shading_nodes(scene)) {
175                 return;  /* No texture context in new shading mode */
176         }
177
178         {
179                 bool valid_world = ED_texture_context_check_world(C);
180                 bool valid_material = ED_texture_context_check_material(C);
181                 bool valid_lamp = ED_texture_context_check_lamp(C);
182                 bool valid_particles = ED_texture_context_check_particles(C);
183                 bool valid_linestyle = ED_texture_context_check_linestyle(C);
184                 bool valid_others = ED_texture_context_check_others(C);
185
186                 /* this is similar to direct user action, no need to keep "better" ctxt in _prev */
187                 if ((sbuts->mainb == BCONTEXT_WORLD) && valid_world) {
188                         sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_WORLD;
189                 }
190                 else if ((sbuts->mainb == BCONTEXT_MATERIAL) && valid_material) {
191                         sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_MATERIAL;
192                 }
193                 else if ((sbuts->mainb == BCONTEXT_DATA) && valid_lamp) {
194                         sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_LAMP;
195                 }
196                 else if ((sbuts->mainb == BCONTEXT_PARTICLE) && valid_particles) {
197                         sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_PARTICLES;
198                 }
199                 else if ((sbuts->mainb == BCONTEXT_VIEW_LAYER) && valid_linestyle) {
200                         sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_LINESTYLE;
201                 }
202                 else if ((ELEM(sbuts->mainb, BCONTEXT_MODIFIER, BCONTEXT_PHYSICS)) && valid_others) {
203                         sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_OTHER;
204                 }
205                 /* Else, try to revive a previous "better" ctxt... */
206                 else if ((sbuts->texture_context_prev != sbuts->texture_context) &&
207                          (((sbuts->texture_context_prev == SB_TEXC_WORLD) && valid_world) ||
208                           ((sbuts->texture_context_prev == SB_TEXC_MATERIAL) && valid_material) ||
209                           ((sbuts->texture_context_prev == SB_TEXC_LAMP) && valid_lamp) ||
210                           ((sbuts->texture_context_prev == SB_TEXC_PARTICLES) && valid_particles) ||
211                           ((sbuts->texture_context_prev == SB_TEXC_LINESTYLE) && valid_linestyle) ||
212                           ((sbuts->texture_context_prev == SB_TEXC_OTHER) && valid_others)))
213                 {
214                         sbuts->texture_context = sbuts->texture_context_prev;
215                 }
216                 /* Else, just be sure that current context is valid! */
217                 else if (((sbuts->texture_context == SB_TEXC_WORLD) && !valid_world) ||
218                          ((sbuts->texture_context == SB_TEXC_MATERIAL) && !valid_material) ||
219                          ((sbuts->texture_context == SB_TEXC_LAMP) && !valid_lamp) ||
220                          ((sbuts->texture_context == SB_TEXC_PARTICLES) && !valid_particles) ||
221                          ((sbuts->texture_context == SB_TEXC_LINESTYLE) && !valid_linestyle) ||
222                          ((sbuts->texture_context == SB_TEXC_OTHER) && !valid_others))
223                 {
224                         /* this is default fallback, do keep "better" ctxt in _prev */
225                         sbuts->texture_context_prev = sbuts->texture_context;
226                         if (valid_material) {
227                                 sbuts->texture_context = SB_TEXC_MATERIAL;
228                         }
229                         else if (valid_lamp) {
230                                 sbuts->texture_context = SB_TEXC_LAMP;
231                         }
232                         else if (valid_particles) {
233                                 sbuts->texture_context = SB_TEXC_PARTICLES;
234                         }
235                         else if (valid_linestyle) {
236                                 sbuts->texture_context = SB_TEXC_LINESTYLE;
237                         }
238                         else if (valid_world) {
239                                 sbuts->texture_context = SB_TEXC_WORLD;
240                         }
241                         else if (valid_others) {
242                                 sbuts->texture_context = SB_TEXC_OTHER;
243                         }
244                 }
245         }
246 }
247
248 /************************* Texture User **************************/
249
250 static void buttons_texture_user_property_add(ListBase *users, ID *id, 
251                                               PointerRNA ptr, PropertyRNA *prop,
252                                               const char *category, int icon, const char *name)
253 {
254         ButsTextureUser *user = MEM_callocN(sizeof(ButsTextureUser), "ButsTextureUser");
255
256         user->id = id;
257         user->ptr = ptr;
258         user->prop = prop;
259         user->category = category;
260         user->icon = icon;
261         user->name = name;
262         user->index = BLI_listbase_count(users);
263
264         BLI_addtail(users, user);
265 }
266
267 static void buttons_texture_user_node_add(ListBase *users, ID *id, 
268                                           bNodeTree *ntree, bNode *node,
269                                           const char *category, int icon, const char *name)
270 {
271         ButsTextureUser *user = MEM_callocN(sizeof(ButsTextureUser), "ButsTextureUser");
272
273         user->id = id;
274         user->ntree = ntree;
275         user->node = node;
276         user->category = category;
277         user->icon = icon;
278         user->name = name;
279         user->index = BLI_listbase_count(users);
280
281         BLI_addtail(users, user);
282 }
283
284 static void buttons_texture_users_find_nodetree(ListBase *users, ID *id,
285                                                 bNodeTree *ntree, const char *category)
286 {
287         bNode *node;
288
289         if (ntree) {
290                 for (node = ntree->nodes.first; node; node = node->next) {
291                         if (node->typeinfo->nclass == NODE_CLASS_TEXTURE) {
292                                 PointerRNA ptr;
293                                 /* PropertyRNA *prop; */ /* UNUSED */
294                                 
295                                 RNA_pointer_create(&ntree->id, &RNA_Node, node, &ptr);
296                                 /* prop = RNA_struct_find_property(&ptr, "texture"); */ /* UNUSED */
297                                 
298                                 buttons_texture_user_node_add(users, id, ntree, node,
299                                                               category, RNA_struct_ui_icon(ptr.type), node->name);
300                         }
301                         else if (node->type == NODE_GROUP && node->id) {
302                                 buttons_texture_users_find_nodetree(users, id, (bNodeTree *)node->id, category);
303                         }
304                 }
305         }
306 }
307
308 static void buttons_texture_modifier_foreach(void *userData, Object *ob, ModifierData *md, const char *propname)
309 {
310         PointerRNA ptr;
311         PropertyRNA *prop;
312         ListBase *users = userData;
313
314         RNA_pointer_create(&ob->id, &RNA_Modifier, md, &ptr);
315         prop = RNA_struct_find_property(&ptr, propname);
316
317         buttons_texture_user_property_add(users, &ob->id, ptr, prop,
318                                           N_("Modifiers"), RNA_struct_ui_icon(ptr.type), md->name);
319 }
320
321 static void buttons_texture_users_from_context(ListBase *users, const bContext *C, SpaceButs *sbuts)
322 {
323         Scene *scene = NULL;
324         Object *ob = NULL;
325         Material *ma = NULL;
326         Lamp *la = NULL;
327         World *wrld = NULL;
328         WorkSpace *workspace = NULL;
329         FreestyleLineStyle *linestyle = NULL;
330         Brush *brush = NULL;
331         ID *pinid = sbuts->pinid;
332         bool limited_mode = (sbuts->flag & SB_TEX_USER_LIMITED) != 0;
333
334         /* get data from context */
335         if (pinid) {
336                 if (GS(pinid->name) == ID_SCE)
337                         scene = (Scene *)pinid;
338                 else if (GS(pinid->name) == ID_OB)
339                         ob = (Object *)pinid;
340                 else if (GS(pinid->name) == ID_LA)
341                         la = (Lamp *)pinid;
342                 else if (GS(pinid->name) == ID_WO)
343                         wrld = (World *)pinid;
344                 else if (GS(pinid->name) == ID_MA)
345                         ma = (Material *)pinid;
346                 else if (GS(pinid->name) == ID_BR)
347                         brush = (Brush *)pinid;
348                 else if (GS(pinid->name) == ID_LS)
349                         linestyle = (FreestyleLineStyle *)pinid;
350                 else if (GS(pinid->name) == ID_WS)
351                         workspace = (WorkSpace *)workspace;
352         }
353
354         if (!scene)
355                 scene = CTX_data_scene(C);
356
357         if (!pinid || GS(pinid->name) == ID_SCE) {
358                 wrld = scene->world;
359                 brush = BKE_paint_brush(BKE_paint_get_active_from_context(C));
360                 linestyle = BKE_linestyle_active_from_scene(scene);
361         }
362         else if (!pinid || GS(pinid->name) == ID_WS) {
363                 if (!workspace) {
364                         workspace = CTX_wm_workspace(C);
365                 }
366                 ViewLayer *view_layer = BKE_view_layer_from_workspace_get(scene, workspace);
367                 ob = OBACT(view_layer);
368         }
369
370         if (ob && ob->type == OB_LAMP && !la)
371                 la = ob->data;
372         if (ob && !ma)
373                 ma = give_current_material(ob, ob->actcol);
374
375         /* fill users */
376         BLI_listbase_clear(users);
377
378         if (ma && !limited_mode)
379                 buttons_texture_users_find_nodetree(users, &ma->id, ma->nodetree, N_("Material"));
380         if (la && !limited_mode)
381                 buttons_texture_users_find_nodetree(users, &la->id, la->nodetree, N_("Lamp"));
382         if (wrld && !limited_mode)
383                 buttons_texture_users_find_nodetree(users, &wrld->id, wrld->nodetree, N_("World"));
384         if (linestyle && !limited_mode)
385                 buttons_texture_users_find_nodetree(users, &linestyle->id, linestyle->nodetree, N_("Line Style"));
386
387         if (ob) {
388                 ParticleSystem *psys = psys_get_current(ob);
389                 MTex *mtex;
390                 int a;
391
392                 /* modifiers */
393                 modifiers_foreachTexLink(ob, buttons_texture_modifier_foreach, users);
394
395                 /* particle systems */
396                 if (psys && !limited_mode) {
397                         for (a = 0; a < MAX_MTEX; a++) {
398                                 mtex = psys->part->mtex[a];
399
400                                 if (mtex) {
401                                         PointerRNA ptr;
402                                         PropertyRNA *prop;
403
404                                         RNA_pointer_create(&psys->part->id, &RNA_ParticleSettingsTextureSlot, mtex, &ptr);
405                                         prop = RNA_struct_find_property(&ptr, "texture");
406
407                                         buttons_texture_user_property_add(users, &psys->part->id, ptr, prop, N_("Particles"),
408                                                                           RNA_struct_ui_icon(&RNA_ParticleSettings), psys->name);
409                                 }
410                         }
411                 }
412
413                 /* field */
414                 if (ob->pd && ob->pd->forcefield == PFIELD_TEXTURE) {
415                         PointerRNA ptr;
416                         PropertyRNA *prop;
417
418                         RNA_pointer_create(&ob->id, &RNA_FieldSettings, ob->pd, &ptr);
419                         prop = RNA_struct_find_property(&ptr, "texture");
420
421                         buttons_texture_user_property_add(users, &ob->id, ptr, prop,
422                                                           N_("Fields"), ICON_FORCE_TEXTURE, IFACE_("Texture Field"));
423                 }
424         }
425
426         /* brush */
427         if (brush) {
428                 PointerRNA ptr;
429                 PropertyRNA *prop;
430
431                 /* texture */
432                 RNA_pointer_create(&brush->id, &RNA_BrushTextureSlot, &brush->mtex, &ptr);
433                 prop = RNA_struct_find_property(&ptr, "texture");
434
435                 buttons_texture_user_property_add(users, &brush->id, ptr, prop,
436                                                   N_("Brush"), ICON_BRUSH_DATA, IFACE_("Brush"));
437
438                 /* mask texture */
439                 RNA_pointer_create(&brush->id, &RNA_BrushTextureSlot, &brush->mask_mtex, &ptr);
440                 prop = RNA_struct_find_property(&ptr, "texture");
441
442                 buttons_texture_user_property_add(users, &brush->id, ptr, prop,
443                                                   N_("Brush"), ICON_BRUSH_DATA, IFACE_("Brush Mask"));
444         }
445 }
446
447 void buttons_texture_context_compute(const bContext *C, SpaceButs *sbuts)
448 {
449         /* gather available texture users in context. runs on every draw of
450          * properties editor, before the buttons are created. */
451         ButsContextTexture *ct = sbuts->texuser;
452         Scene *scene = CTX_data_scene(C);
453         ID *pinid = sbuts->pinid;
454
455         set_texture_context(C, sbuts);
456
457         if (!((sbuts->texture_context == SB_TEXC_OTHER) || BKE_scene_use_new_shading_nodes(scene))) {
458                 if (ct) {
459                         BLI_freelistN(&ct->users);
460                         MEM_freeN(ct);
461                         sbuts->texuser = NULL;
462                 }
463
464                 return;
465         }
466
467         if (!ct) {
468                 ct = MEM_callocN(sizeof(ButsContextTexture), "ButsContextTexture");
469                 sbuts->texuser = ct;
470         }
471         else {
472                 BLI_freelistN(&ct->users);
473         }
474
475         buttons_texture_users_from_context(&ct->users, C, sbuts);
476
477         if (pinid && GS(pinid->name) == ID_TE) {
478                 ct->user = NULL;
479                 ct->texture = (Tex *)pinid;
480         }
481         else {
482                 /* set one user as active based on active index */
483                 if (ct->index >= BLI_listbase_count_ex(&ct->users, ct->index + 1))
484                         ct->index = 0;
485
486                 ct->user = BLI_findlink(&ct->users, ct->index);
487                 ct->texture = NULL;
488
489                 if (ct->user) {
490                         if (ct->user->ptr.data) {
491                                 PointerRNA texptr;
492                                 Tex *tex;
493
494                                 /* get texture datablock pointer if it's a property */
495                                 texptr = RNA_property_pointer_get(&ct->user->ptr, ct->user->prop);
496                                 tex = (RNA_struct_is_a(texptr.type, &RNA_Texture)) ? texptr.data : NULL;
497
498                                 ct->texture = tex;
499                         }
500                         else if (ct->user->node && !(ct->user->node->flag & NODE_ACTIVE_TEXTURE)) {
501                                 ButsTextureUser *user;
502
503                                 /* detect change of active texture node in same node tree, in that
504                                  * case we also automatically switch to the other node */
505                                 for (user = ct->users.first; user; user = user->next) {
506                                         if (user->ntree == ct->user->ntree && user->node != ct->user->node) {
507                                                 if (user->node->flag & NODE_ACTIVE_TEXTURE) {
508                                                         ct->user = user;
509                                                         ct->index = BLI_findindex(&ct->users, user);
510                                                         break;
511                                                 }
512                                         }
513                                 }
514                         }
515                 }
516         }
517 }
518
519 static void template_texture_select(bContext *C, void *user_p, void *UNUSED(arg))
520 {
521         /* callback when selecting a texture user in the menu */
522         SpaceButs *sbuts = CTX_wm_space_buts(C);
523         ButsContextTexture *ct = (sbuts) ? sbuts->texuser : NULL;
524         ButsTextureUser *user = (ButsTextureUser *)user_p;
525         PointerRNA texptr;
526         Tex *tex;
527
528         if (!ct)
529                 return;
530
531         /* set user as active */
532         if (user->node) {
533                 ED_node_set_active(CTX_data_main(C), user->ntree, user->node);
534                 ct->texture = NULL;
535         }
536         else {
537                 texptr = RNA_property_pointer_get(&user->ptr, user->prop);
538                 tex = (RNA_struct_is_a(texptr.type, &RNA_Texture)) ? texptr.data : NULL;
539
540                 ct->texture = tex;
541
542                 if (user->ptr.type == &RNA_ParticleSettingsTextureSlot) {
543                         /* stupid exception for particle systems which still uses influence
544                          * from the old texture system, set the active texture slots as well */
545                         ParticleSettings *part = user->ptr.id.data;
546                         int a;
547
548                         for (a = 0; a < MAX_MTEX; a++)
549                                 if (user->ptr.data == part->mtex[a])
550                                         part->texact = a;
551                 }
552
553                 if (sbuts && tex)
554                         sbuts->preview = 1;
555         }
556
557         ct->user = user;
558         ct->index = user->index;
559 }
560
561 static void template_texture_user_menu(bContext *C, uiLayout *layout, void *UNUSED(arg))
562 {
563         /* callback when opening texture user selection menu, to create buttons. */
564         SpaceButs *sbuts = CTX_wm_space_buts(C);
565         ButsContextTexture *ct = sbuts->texuser;
566         ButsTextureUser *user;
567         uiBlock *block = uiLayoutGetBlock(layout);
568         const char *last_category = NULL;
569
570         for (user = ct->users.first; user; user = user->next) {
571                 uiBut *but;
572                 char name[UI_MAX_NAME_STR];
573
574                 /* add label per category */
575                 if (!last_category || !STREQ(last_category, user->category)) {
576                         uiItemL(layout, IFACE_(user->category), ICON_NONE);
577                         but = block->buttons.last;
578                         but->drawflag = UI_BUT_TEXT_LEFT;
579                 }
580
581                 /* create button */
582                 if (user->prop) {
583                         PointerRNA texptr = RNA_property_pointer_get(&user->ptr, user->prop);
584                         Tex *tex = texptr.data;
585
586                         if (tex)
587                                 BLI_snprintf(name, UI_MAX_NAME_STR, "  %s - %s", user->name, tex->id.name + 2);
588                         else
589                                 BLI_snprintf(name, UI_MAX_NAME_STR, "  %s", user->name);
590                 }
591                 else
592                         BLI_snprintf(name, UI_MAX_NAME_STR, "  %s", user->name);
593
594                 but = uiDefIconTextBut(block, UI_BTYPE_BUT, 0, user->icon, name, 0, 0, UI_UNIT_X * 4, UI_UNIT_Y,
595                                        NULL, 0.0, 0.0, 0.0, 0.0, "");
596                 UI_but_funcN_set(but, template_texture_select, MEM_dupallocN(user), NULL);
597
598                 last_category = user->category;
599         }
600
601         UI_block_flag_enable(block, UI_BLOCK_NO_FLIP);
602 }
603
604 void uiTemplateTextureUser(uiLayout *layout, bContext *C)
605 {
606         /* texture user selection dropdown menu. the available users have been
607          * gathered before drawing in ButsContextTexture, we merely need to
608          * display the current item. */
609         SpaceButs *sbuts = CTX_wm_space_buts(C);
610         ButsContextTexture *ct = (sbuts) ? sbuts->texuser : NULL;
611         uiBlock *block = uiLayoutGetBlock(layout);
612         uiBut *but;
613         ButsTextureUser *user;
614         char name[UI_MAX_NAME_STR];
615
616         if (!ct)
617                 return;
618
619         /* get current user */
620         user = ct->user;
621
622         if (!user) {
623                 uiItemL(layout, IFACE_("No textures in context"), ICON_NONE);
624                 return;
625         }
626
627         /* create button */
628         BLI_strncpy(name, user->name, UI_MAX_NAME_STR);
629
630         if (user->icon) {
631                 but = uiDefIconTextMenuBut(block, template_texture_user_menu, NULL,
632                                            user->icon, name, 0, 0, UI_UNIT_X * 4, UI_UNIT_Y, "");
633         }
634         else {
635                 but = uiDefMenuBut(block, template_texture_user_menu, NULL,
636                                    name, 0, 0, UI_UNIT_X * 4, UI_UNIT_Y, "");
637         }
638
639         /* some cosmetic tweaks */
640         UI_but_type_set_menu_from_pulldown(but);
641
642         but->flag &= ~UI_BUT_ICON_SUBMENU;
643 }
644
645 /************************* Texture Show **************************/
646
647 static void template_texture_show(bContext *C, void *data_p, void *prop_p)
648 {
649         SpaceButs *sbuts = CTX_wm_space_buts(C);
650         ButsContextTexture *ct = (sbuts) ? sbuts->texuser : NULL;
651         ButsTextureUser *user;
652
653         if (!ct)
654                 return;
655
656         for (user = ct->users.first; user; user = user->next)
657                 if (user->ptr.data == data_p && user->prop == prop_p)
658                         break;
659         
660         if (user) {
661                 /* select texture */
662                 template_texture_select(C, user, NULL);
663
664                 /* change context */
665                 sbuts->mainb = BCONTEXT_TEXTURE;
666                 sbuts->mainbuser = sbuts->mainb;
667                 sbuts->preview = 1;
668
669                 /* redraw editor */
670                 ED_area_tag_redraw(CTX_wm_area(C));
671         }
672 }
673
674 void uiTemplateTextureShow(uiLayout *layout, bContext *C, PointerRNA *ptr, PropertyRNA *prop)
675 {
676         /* button to quickly show texture in texture tab */
677         SpaceButs *sbuts = CTX_wm_space_buts(C);
678         ButsContextTexture *ct = (sbuts) ? sbuts->texuser : NULL;
679         ButsTextureUser *user;
680
681         /* only show button in other tabs in properties editor */
682         if (!ct || sbuts->mainb == BCONTEXT_TEXTURE)
683                 return;
684
685         /* find corresponding texture user */
686         for (user = ct->users.first; user; user = user->next)
687                 if (user->ptr.data == ptr->data && user->prop == prop)
688                         break;
689         
690         /* draw button */
691         if (user) {
692                 uiBlock *block = uiLayoutGetBlock(layout);
693                 uiBut *but;
694                 
695                 but = uiDefIconBut(block, UI_BTYPE_BUT, 0, ICON_BUTS, 0, 0, UI_UNIT_X, UI_UNIT_Y,
696                                    NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Show texture in texture tab"));
697                 UI_but_func_set(but, template_texture_show, user->ptr.data, user->prop);
698         }
699 }
700