Cleanup: wrapped function indentation
[blender.git] / source / blender / editors / interface / interface_icons.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) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributors: Blender Foundation, full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_icons.c
27  *  \ingroup edinterface
28  */
29
30 #include <math.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "GPU_extensions.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_fileops_types.h"
41
42 #include "DNA_brush_types.h"
43 #include "DNA_dynamicpaint_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_space_types.h"
47
48 #include "RNA_access.h"
49 #include "RNA_enum_types.h"
50
51 #include "BKE_context.h"
52 #include "BKE_global.h"
53 #include "BKE_icons.h"
54 #include "BKE_appdir.h"
55
56 #include "IMB_imbuf.h"
57 #include "IMB_imbuf_types.h"
58
59 #include "BIF_gl.h"
60 #include "BIF_glutil.h"
61
62 #include "ED_datafiles.h"
63 #include "ED_render.h"
64
65 #include "UI_interface.h"
66 #include "UI_interface_icons.h"
67
68 #include "interface_intern.h"
69
70 #ifndef WITH_HEADLESS
71 #define ICON_GRID_COLS      26
72 #define ICON_GRID_ROWS      30
73
74 #define ICON_GRID_MARGIN    10
75 #define ICON_GRID_W         32
76 #define ICON_GRID_H         32
77 #endif  /* WITH_HEADLESS */
78
79 typedef struct IconImage {
80         int w;
81         int h;
82         unsigned int *rect;
83         unsigned char *datatoc_rect;
84         int datatoc_size;
85 } IconImage;
86
87 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
88
89 #define ICON_TYPE_PREVIEW   0
90 #define ICON_TYPE_TEXTURE   1
91 #define ICON_TYPE_BUFFER    2
92 #define ICON_TYPE_VECTOR    3
93
94 typedef struct DrawInfo {
95         int type;
96
97         union {
98                 /* type specific data */
99                 struct {
100                         VectorDrawFunc func;
101                 } vector;
102                 struct {
103                         IconImage *image;
104                 } buffer;
105                 struct {
106                         int x, y, w, h;
107                 } texture;
108         } data;
109 } DrawInfo;
110
111 typedef struct IconTexture {
112         GLuint id;
113         int w;
114         int h;
115         float invw;
116         float invh;
117 } IconTexture;
118
119 /* ******************* STATIC LOCAL VARS ******************* */
120 /* static here to cache results of icon directory scan, so it's not 
121  * scanning the filesystem each time the menu is drawn */
122 static struct ListBase iconfilelist = {NULL, NULL};
123 static IconTexture icongltex = {0, 0, 0, 0.0f, 0.0f};
124
125 /* **************************************************** */
126
127 #ifndef WITH_HEADLESS
128
129 static DrawInfo *def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type)
130 {
131         Icon *new_icon = NULL;
132         IconImage *iimg = NULL;
133         DrawInfo *di;
134
135         new_icon = MEM_callocN(sizeof(Icon), "texicon");
136
137         new_icon->obj = NULL; /* icon is not for library object */
138         new_icon->type = 0;
139
140         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
141         di->type = type;
142
143         if (type == ICON_TYPE_TEXTURE) {
144                 di->data.texture.x = xofs;
145                 di->data.texture.y = yofs;
146                 di->data.texture.w = size;
147                 di->data.texture.h = size;
148         }
149         else if (type == ICON_TYPE_BUFFER) {
150                 iimg = MEM_callocN(sizeof(IconImage), "icon_img");
151                 iimg->w = size;
152                 iimg->h = size;
153
154                 /* icon buffers can get initialized runtime now, via datatoc */
155                 if (bbuf) {
156                         int y, imgsize;
157                         
158                         iimg->rect = MEM_mallocN(size * size * sizeof(unsigned int), "icon_rect");
159                         
160                         /* Here we store the rect in the icon - same as before */
161                         if (size == bbuf->x && size == bbuf->y && xofs == 0 && yofs == 0)
162                                 memcpy(iimg->rect, bbuf->rect, size * size * sizeof(int));
163                         else {
164                                 /* this code assumes square images */
165                                 imgsize = bbuf->x;
166                                 for (y = 0; y < size; y++) {
167                                         memcpy(&iimg->rect[y * size], &bbuf->rect[(y + yofs) * imgsize + xofs], size * sizeof(int));
168                                 }
169                         }
170                 }
171                 di->data.buffer.image = iimg;
172         }
173
174         new_icon->drawinfo_free = UI_icons_free_drawinfo;
175         new_icon->drawinfo = di;
176
177         BKE_icon_set(icon_id, new_icon);
178         
179         return di;
180 }
181
182 static void def_internal_vicon(int icon_id, VectorDrawFunc drawFunc)
183 {
184         Icon *new_icon = NULL;
185         DrawInfo *di;
186
187         new_icon = MEM_callocN(sizeof(Icon), "texicon");
188
189         new_icon->obj = NULL; /* icon is not for library object */
190         new_icon->type = 0;
191
192         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
193         di->type = ICON_TYPE_VECTOR;
194         di->data.vector.func = drawFunc;
195
196         new_icon->drawinfo_free = NULL;
197         new_icon->drawinfo = di;
198
199         BKE_icon_set(icon_id, new_icon);
200 }
201
202 /* Vector Icon Drawing Routines */
203
204 /* Utilities */
205
206 static void viconutil_set_point(GLint pt[2], int x, int y)
207 {
208         pt[0] = x;
209         pt[1] = y;
210 }
211
212 static void viconutil_draw_tri(GLint(*pts)[2])
213 {
214         glBegin(GL_TRIANGLES);
215         glVertex2iv(pts[0]);
216         glVertex2iv(pts[1]);
217         glVertex2iv(pts[2]);
218         glEnd();
219 }
220
221 static void viconutil_draw_lineloop(GLint(*pts)[2], int numPoints)
222 {
223         int i;
224
225         glBegin(GL_LINE_LOOP);
226         for (i = 0; i < numPoints; i++) {
227                 glVertex2iv(pts[i]);
228         }
229         glEnd();
230 }
231
232 static void viconutil_draw_lineloop_smooth(GLint(*pts)[2], int numPoints)
233 {
234         glEnable(GL_LINE_SMOOTH);
235         viconutil_draw_lineloop(pts, numPoints);
236         glDisable(GL_LINE_SMOOTH);
237 }
238
239 static void viconutil_draw_points(GLint(*pts)[2], int numPoints, int pointSize)
240 {
241         int i;
242
243         glBegin(GL_QUADS);
244         for (i = 0; i < numPoints; i++) {
245                 int x = pts[i][0], y = pts[i][1];
246
247                 glVertex2i(x - pointSize, y - pointSize);
248                 glVertex2i(x + pointSize, y - pointSize);
249                 glVertex2i(x + pointSize, y + pointSize);
250                 glVertex2i(x - pointSize, y + pointSize);
251         }
252         glEnd();
253 }
254
255 /* Drawing functions */
256
257 static void vicon_x_draw(int x, int y, int w, int h, float alpha)
258 {
259         x += 3;
260         y += 3;
261         w -= 6;
262         h -= 6;
263
264         glEnable(GL_LINE_SMOOTH);
265
266         glLineWidth(2.5);
267         
268         glColor4f(0.0, 0.0, 0.0, alpha);
269         glBegin(GL_LINES);
270         glVertex2i(x, y);
271         glVertex2i(x + w, y + h);
272         glVertex2i(x + w, y);
273         glVertex2i(x, y + h);
274         glEnd();
275
276         glLineWidth(1.0);
277         
278         glDisable(GL_LINE_SMOOTH);
279 }
280
281 static void vicon_view3d_draw(int x, int y, int w, int h, float alpha)
282 {
283         int cx = x + w / 2;
284         int cy = y + h / 2;
285         int d = MAX2(2, h / 3);
286
287         glColor4f(0.5, 0.5, 0.5, alpha);
288         glBegin(GL_LINES);
289         glVertex2i(x, cy - d);
290         glVertex2i(x + w, cy - d);
291         glVertex2i(x, cy + d);
292         glVertex2i(x + w, cy + d);
293
294         glVertex2i(cx - d, y);
295         glVertex2i(cx - d, y + h);
296         glVertex2i(cx + d, y);
297         glVertex2i(cx + d, y + h);
298         glEnd();
299         
300         glColor4f(0.0, 0.0, 0.0, alpha);
301         glBegin(GL_LINES);
302         glVertex2i(x, cy);
303         glVertex2i(x + w, cy);
304         glVertex2i(cx, y);
305         glVertex2i(cx, y + h);
306         glEnd();
307 }
308
309 static void vicon_edit_draw(int x, int y, int w, int h, float alpha)
310 {
311         GLint pts[4][2];
312
313         viconutil_set_point(pts[0], x + 3,     y + 3);
314         viconutil_set_point(pts[1], x + w - 3, y + 3);
315         viconutil_set_point(pts[2], x + w - 3, y + h - 3);
316         viconutil_set_point(pts[3], x + 3,     y + h - 3);
317
318         glColor4f(0.0, 0.0, 0.0, alpha);
319         viconutil_draw_lineloop(pts, 4);
320
321         glColor3f(1, 1, 0.0);
322         viconutil_draw_points(pts, 4, 1);
323 }
324
325 static void vicon_editmode_hlt_draw(int x, int y, int w, int h, float alpha)
326 {
327         GLint pts[3][2];
328
329         viconutil_set_point(pts[0], x + w / 2, y + h - 2);
330         viconutil_set_point(pts[1], x + 3,     y + 4);
331         viconutil_set_point(pts[2], x + w - 3, y + 4);
332
333         glColor4f(0.5, 0.5, 0.5, alpha);
334         viconutil_draw_tri(pts);
335
336         glColor4f(0.0, 0.0, 0.0, 1);
337         viconutil_draw_lineloop_smooth(pts, 3);
338
339         glColor3f(1, 1, 0.0);
340         viconutil_draw_points(pts, 3, 1);
341 }
342
343 static void vicon_editmode_dehlt_draw(int x, int y, int w, int h, float UNUSED(alpha))
344 {
345         GLint pts[3][2];
346
347         viconutil_set_point(pts[0], x + w / 2, y + h - 2);
348         viconutil_set_point(pts[1], x + 3,     y + 4);
349         viconutil_set_point(pts[2], x + w - 3, y + 4);
350
351         glColor4f(0.0f, 0.0f, 0.0f, 1);
352         viconutil_draw_lineloop_smooth(pts, 3);
353
354         glColor3f(0.9f, 0.9f, 0.9f);
355         viconutil_draw_points(pts, 3, 1);
356 }
357
358 static void vicon_disclosure_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
359 {
360         GLint pts[3][2];
361         int cx = x + w / 2;
362         int cy = y + w / 2;
363         int d = w / 3, d2 = w / 5;
364
365         viconutil_set_point(pts[0], cx - d2, cy + d);
366         viconutil_set_point(pts[1], cx - d2, cy - d);
367         viconutil_set_point(pts[2], cx + d2, cy);
368
369         glShadeModel(GL_SMOOTH);
370         glBegin(GL_TRIANGLES);
371         glColor4f(0.8f, 0.8f, 0.8f, alpha);
372         glVertex2iv(pts[0]);
373         glVertex2iv(pts[1]);
374         glColor4f(0.3f, 0.3f, 0.3f, alpha);
375         glVertex2iv(pts[2]);
376         glEnd();
377         glShadeModel(GL_FLAT);
378
379         glColor4f(0.0f, 0.0f, 0.0f, 1);
380         viconutil_draw_lineloop_smooth(pts, 3);
381 }
382
383 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
384 {
385         GLint pts[3][2];
386         int cx = x + w / 2 - 4;
387         int cy = y + w / 2;
388         int d = w / 5, d2 = w / 7;
389
390         viconutil_set_point(pts[0], cx - d2, cy + d);
391         viconutil_set_point(pts[1], cx - d2, cy - d);
392         viconutil_set_point(pts[2], cx + d2, cy);
393
394         glColor4f(0.2f, 0.2f, 0.2f, alpha);
395
396         glShadeModel(GL_SMOOTH);
397         glBegin(GL_TRIANGLES);
398         glVertex2iv(pts[0]);
399         glVertex2iv(pts[1]);
400         glVertex2iv(pts[2]);
401         glEnd();
402         glShadeModel(GL_FLAT);
403 }
404
405 static void vicon_disclosure_tri_down_draw(int x, int y, int w, int UNUSED(h), float alpha)
406 {
407         GLint pts[3][2];
408         int cx = x + w / 2;
409         int cy = y + w / 2;
410         int d = w / 3, d2 = w / 5;
411
412         viconutil_set_point(pts[0], cx + d, cy + d2);
413         viconutil_set_point(pts[1], cx - d, cy + d2);
414         viconutil_set_point(pts[2], cx, cy - d2);
415
416         glShadeModel(GL_SMOOTH);
417         glBegin(GL_TRIANGLES);
418         glColor4f(0.8f, 0.8f, 0.8f, alpha);
419         glVertex2iv(pts[0]);
420         glVertex2iv(pts[1]);
421         glColor4f(0.3f, 0.3f, 0.3f, alpha);
422         glVertex2iv(pts[2]);
423         glEnd();
424         glShadeModel(GL_FLAT);
425
426         glColor4f(0.0f, 0.0f, 0.0f, 1);
427         viconutil_draw_lineloop_smooth(pts, 3);
428 }
429
430 static void vicon_move_up_draw(int x, int y, int w, int h, float UNUSED(alpha))
431 {
432         int d = -2;
433
434         glEnable(GL_LINE_SMOOTH);
435         glLineWidth(1);
436         glColor3f(0.0, 0.0, 0.0);
437
438         glBegin(GL_LINE_STRIP);
439         glVertex2i(x + w / 2 - d * 2, y + h / 2 + d);
440         glVertex2i(x + w / 2, y + h / 2 - d + 1);
441         glVertex2i(x + w / 2 + d * 2, y + h / 2 + d);
442         glEnd();
443
444         glLineWidth(1.0);
445         glDisable(GL_LINE_SMOOTH);
446 }
447
448 static void vicon_move_down_draw(int x, int y, int w, int h, float UNUSED(alpha))
449 {
450         int d = 2;
451
452         glEnable(GL_LINE_SMOOTH);
453         glLineWidth(1);
454         glColor3f(0.0, 0.0, 0.0);
455
456         glBegin(GL_LINE_STRIP);
457         glVertex2i(x + w / 2 - d * 2, y + h / 2 + d);
458         glVertex2i(x + w / 2, y + h / 2 - d - 1);
459         glVertex2i(x + w / 2 + d * 2, y + h / 2 + d);
460         glEnd();
461
462         glLineWidth(1.0);
463         glDisable(GL_LINE_SMOOTH);
464 }
465
466 #ifndef WITH_HEADLESS
467
468 static void init_brush_icons(void)
469 {
470
471 #define INIT_BRUSH_ICON(icon_id, name)                                          \
472         {                                                                           \
473                 unsigned char *rect = (unsigned char *)datatoc_ ##name## _png;          \
474                 int size = datatoc_ ##name## _png_size;                                 \
475                 DrawInfo *di;                                                           \
476                 \
477                 di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_BUFFER);       \
478                 di->data.buffer.image->datatoc_rect = rect;                             \
479                 di->data.buffer.image->datatoc_size = size;                             \
480         }
481         /* end INIT_BRUSH_ICON */
482
483         const int w = 96; /* warning, brush size hardcoded in C, but it gets scaled */
484
485         INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
486         INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
487         INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
488         INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
489         INIT_BRUSH_ICON(ICON_BRUSH_CLAY_STRIPS, claystrips);
490         INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
491         INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
492         INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
493         INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
494         INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
495         INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
496         INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
497         INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
498         INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
499         INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
500         INIT_BRUSH_ICON(ICON_BRUSH_MASK, mask);
501         INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
502         INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
503         INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
504         INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
505         INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
506         INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
507         INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
508         INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
509         INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
510         INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
511         INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
512         INIT_BRUSH_ICON(ICON_BRUSH_TEXFILL, texfill);
513         INIT_BRUSH_ICON(ICON_BRUSH_TEXMASK, texmask);
514         INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
515         INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
516         INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
517
518 #undef INIT_BRUSH_ICON
519 }
520
521 static void icon_verify_datatoc(IconImage *iimg)
522 {
523         /* if it has own rect, things are all OK */
524         if (iimg->rect)
525                 return;
526         
527         if (iimg->datatoc_rect) {
528                 ImBuf *bbuf = IMB_ibImageFromMemory(iimg->datatoc_rect,
529                                                     iimg->datatoc_size, IB_rect, NULL, "<matcap icon>");
530                 /* w and h were set on initialize */
531                 if (bbuf->x != iimg->h && bbuf->y != iimg->w)
532                         IMB_scaleImBuf(bbuf, iimg->w, iimg->h);
533                 
534                 iimg->rect = bbuf->rect;
535                 bbuf->rect = NULL;
536                 IMB_freeImBuf(bbuf);
537         }
538 }
539
540 static void init_matcap_icons(void)
541 {
542         /* dynamic allocation now, tucking datatoc pointers in DrawInfo */
543 #define INIT_MATCAP_ICON(icon_id, name)                                       \
544         {                                                                         \
545                 unsigned char *rect = (unsigned char *)datatoc_ ##name## _jpg;        \
546                 int size = datatoc_ ##name## _jpg_size;                               \
547                 DrawInfo *di;                                                         \
548                                                                                       \
549                 di = def_internal_icon(NULL, icon_id, 0, 0, 96, ICON_TYPE_BUFFER);   \
550                 di->data.buffer.image->datatoc_rect = rect;                           \
551                 di->data.buffer.image->datatoc_size = size;                           \
552         } (void)0
553
554         INIT_MATCAP_ICON(ICON_MATCAP_01, mc01);
555         INIT_MATCAP_ICON(ICON_MATCAP_02, mc02);
556         INIT_MATCAP_ICON(ICON_MATCAP_03, mc03);
557         INIT_MATCAP_ICON(ICON_MATCAP_04, mc04);
558         INIT_MATCAP_ICON(ICON_MATCAP_05, mc05);
559         INIT_MATCAP_ICON(ICON_MATCAP_06, mc06);
560         INIT_MATCAP_ICON(ICON_MATCAP_07, mc07);
561         INIT_MATCAP_ICON(ICON_MATCAP_08, mc08);
562         INIT_MATCAP_ICON(ICON_MATCAP_09, mc09);
563         INIT_MATCAP_ICON(ICON_MATCAP_10, mc10);
564         INIT_MATCAP_ICON(ICON_MATCAP_11, mc11);
565         INIT_MATCAP_ICON(ICON_MATCAP_12, mc12);
566         INIT_MATCAP_ICON(ICON_MATCAP_13, mc13);
567         INIT_MATCAP_ICON(ICON_MATCAP_14, mc14);
568         INIT_MATCAP_ICON(ICON_MATCAP_15, mc15);
569         INIT_MATCAP_ICON(ICON_MATCAP_16, mc16);
570         INIT_MATCAP_ICON(ICON_MATCAP_17, mc17);
571         INIT_MATCAP_ICON(ICON_MATCAP_18, mc18);
572         INIT_MATCAP_ICON(ICON_MATCAP_19, mc19);
573         INIT_MATCAP_ICON(ICON_MATCAP_20, mc20);
574         INIT_MATCAP_ICON(ICON_MATCAP_21, mc21);
575         INIT_MATCAP_ICON(ICON_MATCAP_22, mc22);
576         INIT_MATCAP_ICON(ICON_MATCAP_23, mc23);
577         INIT_MATCAP_ICON(ICON_MATCAP_24, mc24);
578
579 #undef INIT_MATCAP_ICON
580
581 }
582
583 static void init_internal_icons(void)
584 {
585 //      bTheme *btheme = UI_GetTheme();
586         ImBuf *b16buf = NULL, *b32buf = NULL;
587         int x, y, icontype;
588
589 #if 0 // temp disabled
590         if ((btheme != NULL) && btheme->tui.iconfile[0]) {
591                 char *icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
592                 char iconfilestr[FILE_MAX];
593                 
594                 if (icondir) {
595                         BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
596                         bbuf = IMB_loadiffname(iconfilestr, IB_rect, NULL); /* if the image is missing bbuf will just be NULL */
597                         if (bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
598                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
599                                 IMB_freeImBuf(bbuf);
600                                 bbuf = NULL;
601                         }
602                 }
603                 else {
604                         printf("%s: 'icons' data path not found, continuing\n", __func__);
605                 }
606         }
607 #endif
608         if (b16buf == NULL)
609                 b16buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons16_png,
610                                                datatoc_blender_icons16_png_size, IB_rect, NULL, "<blender icons>");
611         if (b16buf)
612                 IMB_premultiply_alpha(b16buf);
613
614         if (b32buf == NULL)
615                 b32buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons32_png,
616                                                datatoc_blender_icons32_png_size, IB_rect, NULL, "<blender icons>");
617         if (b32buf)
618                 IMB_premultiply_alpha(b32buf);
619         
620         if (b16buf && b32buf) {
621                 /* free existing texture if any */
622                 if (icongltex.id) {
623                         glDeleteTextures(1, &icongltex.id);
624                         icongltex.id = 0;
625                 }
626
627                 /* we only use a texture for cards with non-power of two */
628                 if (GPU_non_power_of_two_support()) {
629                         glGenTextures(1, &icongltex.id);
630
631                         if (icongltex.id) {
632                                 int level = 2;
633                                 
634                                 icongltex.w = b32buf->x;
635                                 icongltex.h = b32buf->y;
636                                 icongltex.invw = 1.0f / b32buf->x;
637                                 icongltex.invh = 1.0f / b32buf->y;
638
639                                 glBindTexture(GL_TEXTURE_2D, icongltex.id);
640                                 
641                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, b32buf->x, b32buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b32buf->rect);
642                                 glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, b16buf->x, b16buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b16buf->rect);
643                                 
644                                 while (b16buf->x > 1) {
645                                         ImBuf *nbuf = IMB_onehalf(b16buf);
646                                         glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, nbuf->x, nbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nbuf->rect);
647                                         level++;
648                                         IMB_freeImBuf(b16buf);
649                                         b16buf = nbuf;
650                                 }
651                                 
652                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
653                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
654                                 
655                                 glBindTexture(GL_TEXTURE_2D, 0);
656                                 
657                                 if (glGetError() == GL_OUT_OF_MEMORY) {
658                                         glDeleteTextures(1, &icongltex.id);
659                                         icongltex.id = 0;
660                                 }
661                         }
662                 }
663         }
664
665         if (icongltex.id)
666                 icontype = ICON_TYPE_TEXTURE;
667         else
668                 icontype = ICON_TYPE_BUFFER;
669         
670         if (b32buf) {
671                 for (y = 0; y < ICON_GRID_ROWS; y++) {
672                         for (x = 0; x < ICON_GRID_COLS; x++) {
673                                 def_internal_icon(b32buf, BIFICONID_FIRST + y * ICON_GRID_COLS + x,
674                                                   x * (ICON_GRID_W + ICON_GRID_MARGIN) + ICON_GRID_MARGIN,
675                                                   y * (ICON_GRID_H + ICON_GRID_MARGIN) + ICON_GRID_MARGIN, ICON_GRID_W,
676                                                   icontype);
677                         }
678                 }
679         }
680
681         def_internal_vicon(VICO_VIEW3D_VEC, vicon_view3d_draw);
682         def_internal_vicon(VICO_EDIT_VEC, vicon_edit_draw);
683         def_internal_vicon(VICO_EDITMODE_VEC_DEHLT, vicon_editmode_dehlt_draw);
684         def_internal_vicon(VICO_EDITMODE_VEC_HLT, vicon_editmode_hlt_draw);
685         def_internal_vicon(VICO_DISCLOSURE_TRI_RIGHT_VEC, vicon_disclosure_tri_right_draw);
686         def_internal_vicon(VICO_DISCLOSURE_TRI_DOWN_VEC, vicon_disclosure_tri_down_draw);
687         def_internal_vicon(VICO_MOVE_UP_VEC, vicon_move_up_draw);
688         def_internal_vicon(VICO_MOVE_DOWN_VEC, vicon_move_down_draw);
689         def_internal_vicon(VICO_X_VEC, vicon_x_draw);
690         def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
691
692         IMB_freeImBuf(b16buf);
693         IMB_freeImBuf(b32buf);
694         
695 }
696 #endif  /* WITH_HEADLESS */
697
698 static void init_iconfile_list(struct ListBase *list)
699 {
700         IconFile *ifile;
701         struct direntry *dir;
702         int totfile, i, index = 1;
703         const char *icondir;
704
705         BLI_listbase_clear(list);
706         icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
707
708         if (icondir == NULL)
709                 return;
710         
711         totfile = BLI_filelist_dir_contents(icondir, &dir);
712
713         for (i = 0; i < totfile; i++) {
714                 if ((dir[i].type & S_IFREG)) {
715                         const char *filename = dir[i].relname;
716                         
717                         if (BLI_testextensie(filename, ".png")) {
718                                 /* loading all icons on file start is overkill & slows startup
719                                  * its possible they change size after blender load anyway. */
720 #if 0
721                                 int ifilex, ifiley;
722                                 char iconfilestr[FILE_MAX + 16]; /* allow 256 chars for file+dir */
723                                 ImBuf *bbuf = NULL;
724                                 /* check to see if the image is the right size, continue if not */
725                                 /* copying strings here should go ok, assuming that we never get back
726                                  * a complete path to file longer than 256 chars */
727                                 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
728                                 bbuf = IMB_loadiffname(iconfilestr, IB_rect);
729
730                                 if (bbuf) {
731                                         ifilex = bbuf->x;
732                                         ifiley = bbuf->y;
733                                         IMB_freeImBuf(bbuf);
734                                 }
735                                 else {
736                                         ifilex = ifiley = 0;
737                                 }
738                                 
739                                 /* bad size or failed to load */
740                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
741                                         printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
742                                         continue;
743                                 }
744 #endif          /* removed */
745
746                                 /* found a potential icon file, so make an entry for it in the cache list */
747                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
748                                 
749                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
750                                 ifile->index = index;
751
752                                 BLI_addtail(list, ifile);
753                                 
754                                 index++;
755                         }
756                 }
757         }
758
759         BLI_filelist_free(dir, totfile, NULL);
760         dir = NULL;
761 }
762
763 static void free_iconfile_list(struct ListBase *list)
764 {
765         IconFile *ifile = NULL, *next_ifile = NULL;
766         
767         for (ifile = list->first; ifile; ifile = next_ifile) {
768                 next_ifile = ifile->next;
769                 BLI_freelinkN(list, ifile);
770         }
771 }
772
773 #endif  /* WITH_HEADLESS */
774
775 int UI_iconfile_get_index(const char *filename)
776 {
777         IconFile *ifile;
778         ListBase *list = &(iconfilelist);
779         
780         for (ifile = list->first; ifile; ifile = ifile->next) {
781                 if (BLI_path_cmp(filename, ifile->filename) == 0) {
782                         return ifile->index;
783                 }
784         }
785         
786         return 0;
787 }
788
789 ListBase *UI_iconfile_list(void)
790 {
791         ListBase *list = &(iconfilelist);
792         
793         return list;
794 }
795
796
797 void UI_icons_free(void)
798 {
799 #ifndef WITH_HEADLESS
800         if (icongltex.id) {
801                 glDeleteTextures(1, &icongltex.id);
802                 icongltex.id = 0;
803         }
804
805         free_iconfile_list(&iconfilelist);
806         BKE_icons_free();
807 #endif
808 }
809
810 void UI_icons_free_drawinfo(void *drawinfo)
811 {
812         DrawInfo *di = drawinfo;
813
814         if (di) {
815                 if (di->type == ICON_TYPE_BUFFER) {
816                         if (di->data.buffer.image) {
817                                 if (di->data.buffer.image->rect)
818                                         MEM_freeN(di->data.buffer.image->rect);
819                                 MEM_freeN(di->data.buffer.image);
820                         }
821                 }
822
823                 MEM_freeN(di);
824         }
825 }
826
827 static DrawInfo *icon_create_drawinfo(void)
828 {
829         DrawInfo *di = NULL;
830
831         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
832         di->type = ICON_TYPE_PREVIEW;
833
834         return di;
835 }
836
837 /* note!, returns unscaled by DPI */
838 int UI_icon_get_width(int icon_id)
839 {
840         Icon *icon = NULL;
841         DrawInfo *di = NULL;
842
843         icon = BKE_icon_get(icon_id);
844         
845         if (icon == NULL) {
846                 if (G.debug & G_DEBUG)
847                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
848                 return 0;
849         }
850         
851         di = (DrawInfo *)icon->drawinfo;
852         if (!di) {
853                 di = icon_create_drawinfo();
854                 icon->drawinfo = di;
855         }
856
857         if (di)
858                 return ICON_DEFAULT_WIDTH;
859
860         return 0;
861 }
862
863 int UI_icon_get_height(int icon_id)
864 {
865         Icon *icon = NULL;
866         DrawInfo *di = NULL;
867
868         icon = BKE_icon_get(icon_id);
869         
870         if (icon == NULL) {
871                 if (G.debug & G_DEBUG)
872                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
873                 return 0;
874         }
875         
876         di = (DrawInfo *)icon->drawinfo;
877
878         if (!di) {
879                 di = icon_create_drawinfo();
880                 icon->drawinfo = di;
881         }
882         
883         if (di)
884                 return ICON_DEFAULT_HEIGHT;
885
886         return 0;
887 }
888
889 void UI_icons_init(int first_dyn_id)
890 {
891 #ifdef WITH_HEADLESS
892         (void)first_dyn_id;
893 #else
894         init_iconfile_list(&iconfilelist);
895         BKE_icons_init(first_dyn_id);
896         init_internal_icons();
897         init_brush_icons();
898         init_matcap_icons();
899 #endif
900 }
901
902 /* Render size for preview images and icons
903  */
904 static int preview_render_size(enum eIconSizes size)
905 {
906         switch (size) {
907                 case ICON_SIZE_ICON:    return 32;
908                 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
909         }
910         return 0;
911 }
912
913 /* Create rect for the icon
914  */
915 static void icon_create_rect(struct PreviewImage *prv_img, enum eIconSizes size)
916 {
917         unsigned int render_size = preview_render_size(size);
918
919         if (!prv_img) {
920                 if (G.debug & G_DEBUG)
921                         printf("%s, error: requested preview image does not exist", __func__);
922         }
923         else if (!prv_img->rect[size]) {
924                 prv_img->w[size] = render_size;
925                 prv_img->h[size] = render_size;
926                 prv_img->changed[size] = 1;
927                 prv_img->changed_timestamp[size] = 0;
928                 prv_img->rect[size] = MEM_callocN(render_size * render_size * sizeof(unsigned int), "prv_rect");
929         }
930 }
931
932 /* only called when icon has changed */
933 /* only call with valid pointer from UI_icon_draw */
934 static void icon_set_image(
935         const bContext *C, Scene *scene, ID *id, PreviewImage *prv_img, enum eIconSizes size, const bool use_job)
936 {
937         if (!prv_img) {
938                 if (G.debug & G_DEBUG)
939                         printf("%s: no preview image for this ID: %s\n", __func__, id->name);
940                 return;
941         }
942
943         icon_create_rect(prv_img, size);
944
945         if (use_job) {
946                 /* Job (background) version */
947                 ED_preview_icon_job(C, prv_img, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
948         }
949         else {
950                 if (!scene) {
951                         scene = CTX_data_scene(C);
952                 }
953                 /* Immediate version */
954                 ED_preview_icon_render(scene, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
955         }
956 }
957
958 PreviewImage *UI_icon_to_preview(int icon_id)
959 {
960         Icon *icon = BKE_icon_get(icon_id);
961         
962         if (icon) {
963                 DrawInfo *di = (DrawInfo *)icon->drawinfo;
964                 if (di && di->data.buffer.image) {
965                         ImBuf *bbuf;
966                         
967                         bbuf = IMB_ibImageFromMemory(di->data.buffer.image->datatoc_rect, di->data.buffer.image->datatoc_size, IB_rect, NULL, "<matcap buffer>");
968                         if (bbuf) {
969                                 PreviewImage *prv = BKE_previewimg_create();
970                                 
971                                 prv->rect[0] = bbuf->rect;
972
973                                 prv->w[0] = bbuf->x;
974                                 prv->h[0] = bbuf->y;
975                                 
976                                 bbuf->rect = NULL;
977                                 IMB_freeImBuf(bbuf);
978                                 
979                                 return prv;
980                         }
981                 }
982         }
983         return NULL;
984 }
985
986 static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect), int rw, int rh,
987                            unsigned int *rect, float alpha, const float rgb[3], const bool is_preview)
988 {
989         ImBuf *ima = NULL;
990
991         /* sanity check */
992         if (w <= 0 || h <= 0 || w > 2000 || h > 2000) {
993                 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
994                 BLI_assert(!"invalid icon size");
995                 return;
996         }
997
998         /* modulate color */
999         if (alpha != 1.0f)
1000                 glPixelTransferf(GL_ALPHA_SCALE, alpha);
1001
1002         if (rgb) {
1003                 glPixelTransferf(GL_RED_SCALE, rgb[0]);
1004                 glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
1005                 glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
1006         }
1007
1008         /* rect contains image in 'rendersize', we only scale if needed */
1009         if (rw != w && rh != h) {
1010                 /* first allocate imbuf for scaling and copy preview into it */
1011                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
1012                 memcpy(ima->rect, rect, rw * rh * sizeof(unsigned int));
1013                 IMB_scaleImBuf(ima, w, h); /* scale it */
1014                 rect = ima->rect;
1015         }
1016
1017         /* draw */
1018         if (is_preview) {
1019                 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
1020         }
1021         else {
1022                 glRasterPos2f(x, y);
1023                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
1024         }
1025
1026         if (ima)
1027                 IMB_freeImBuf(ima);
1028
1029         /* restore color */
1030         if (alpha != 0.0f)
1031                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1032         
1033         if (rgb) {
1034                 glPixelTransferf(GL_RED_SCALE, 1.0f);
1035                 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
1036                 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
1037         }
1038 }
1039
1040 static void icon_draw_texture(
1041         float x, float y, float w, float h, int ix, int iy,
1042         int UNUSED(iw), int ih, float alpha, const float rgb[3])
1043 {
1044         float x1, x2, y1, y2;
1045
1046         if (rgb) glColor4f(rgb[0], rgb[1], rgb[2], alpha);
1047         else     glColor4f(alpha, alpha, alpha, alpha);
1048
1049         x1 = ix * icongltex.invw;
1050         x2 = (ix + ih) * icongltex.invw;
1051         y1 = iy * icongltex.invh;
1052         y2 = (iy + ih) * icongltex.invh;
1053
1054         glEnable(GL_TEXTURE_2D);
1055         glBindTexture(GL_TEXTURE_2D, icongltex.id);
1056
1057         /* sharper downscaling, has no effect when scale matches with a mip level */
1058         glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, -0.5f);
1059
1060         glBegin(GL_QUADS);
1061         glTexCoord2f(x1, y1);
1062         glVertex2f(x, y);
1063
1064         glTexCoord2f(x2, y1);
1065         glVertex2f(x + w, y);
1066
1067         glTexCoord2f(x2, y2);
1068         glVertex2f(x + w, y + h);
1069
1070         glTexCoord2f(x1, y2);
1071         glVertex2f(x, y + h);
1072         glEnd();
1073
1074         glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, 0.0f);
1075
1076         glBindTexture(GL_TEXTURE_2D, 0);
1077         glDisable(GL_TEXTURE_2D);
1078 }
1079
1080 /* Drawing size for preview images */
1081 static int get_draw_size(enum eIconSizes size)
1082 {
1083         switch (size) {
1084                 case ICON_SIZE_ICON: return ICON_DEFAULT_HEIGHT;
1085                 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
1086         }
1087         return 0;
1088 }
1089
1090
1091
1092 static void icon_draw_size(
1093         float x, float y, int icon_id, float aspect, float alpha, const float rgb[3],
1094         enum eIconSizes size, int draw_size, const bool UNUSED(nocreate), const bool is_preview)
1095 {
1096         bTheme *btheme = UI_GetTheme();
1097         Icon *icon = NULL;
1098         DrawInfo *di = NULL;
1099         IconImage *iimg;
1100         const float fdraw_size = (float)draw_size;
1101         int w, h;
1102         
1103         icon = BKE_icon_get(icon_id);
1104         alpha *= btheme->tui.icon_alpha;
1105         
1106         if (icon == NULL) {
1107                 if (G.debug & G_DEBUG)
1108                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1109                 return;
1110         }
1111
1112         di = (DrawInfo *)icon->drawinfo;
1113         
1114         if (!di) {
1115                 di = icon_create_drawinfo();
1116         
1117                 icon->drawinfo = di;
1118                 icon->drawinfo_free = UI_icons_free_drawinfo;
1119         }
1120         
1121         /* scale width and height according to aspect */
1122         w = (int)(fdraw_size / aspect + 0.5f);
1123         h = (int)(fdraw_size / aspect + 0.5f);
1124         
1125         if (di->type == ICON_TYPE_VECTOR) {
1126                 /* vector icons use the uiBlock transformation, they are not drawn
1127                  * with untransformed coordinates like the other icons */
1128                 di->data.vector.func((int)x, (int)y, w, h, 1.0f);
1129         }
1130         else if (di->type == ICON_TYPE_TEXTURE) {
1131                 /* texture image use premul alpha for correct scaling */
1132                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1133                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
1134                                   di->data.texture.w, di->data.texture.h, alpha, rgb);
1135                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1136         }
1137         else if (di->type == ICON_TYPE_BUFFER) {
1138                 /* it is a builtin icon */
1139                 iimg = di->data.buffer.image;
1140 #ifndef WITH_HEADLESS
1141                 icon_verify_datatoc(iimg);
1142 #endif
1143                 if (!iimg->rect) return;  /* something has gone wrong! */
1144
1145                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1146                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
1147                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1148         }
1149         else if (di->type == ICON_TYPE_PREVIEW) {
1150                 PreviewImage *pi = BKE_previewimg_get((ID *)icon->obj);
1151
1152                 if (pi) {
1153                         /* no create icon on this level in code */
1154                         if (!pi->rect[size]) return;  /* something has gone wrong! */
1155                         
1156                         /* preview images use premul alpha ... */
1157                         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1158                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], 1.0f, NULL, is_preview);
1159                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1160                 }
1161         }
1162 }
1163
1164 static void ui_id_preview_image_render_size(
1165         const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job)
1166 {
1167         if ((pi->changed[size] || !pi->rect[size])) { /* changed only ever set by dynamic icons */
1168                 /* create the rect if necessary */
1169                 icon_set_image(C, scene, id, pi, size, use_job);
1170
1171                 pi->changed[size] = 0;
1172         }
1173 }
1174
1175 void UI_id_icon_render(const bContext *C, Scene *scene, ID *id, const bool big, const bool use_job)
1176 {
1177         PreviewImage *pi = BKE_previewimg_get(id);
1178
1179         if (pi) {
1180                 if (big)
1181                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_PREVIEW, use_job);  /* bigger preview size */
1182                 else
1183                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_ICON, use_job);     /* icon size */
1184         }
1185 }
1186
1187 static void ui_id_brush_render(const bContext *C, ID *id)
1188 {
1189         PreviewImage *pi = BKE_previewimg_get(id); 
1190         enum eIconSizes i;
1191         
1192         if (!pi)
1193                 return;
1194         
1195         for (i = 0; i < NUM_ICON_SIZES; i++) {
1196                 /* check if rect needs to be created; changed
1197                  * only set by dynamic icons */
1198                 if ((pi->changed[i] || !pi->rect[i])) {
1199                         icon_set_image(C, NULL, id, pi, i, true);
1200                         pi->changed[i] = 0;
1201                 }
1202         }
1203 }
1204
1205
1206 static int ui_id_brush_get_icon(const bContext *C, ID *id)
1207 {
1208         Brush *br = (Brush *)id;
1209
1210         if (br->flag & BRUSH_CUSTOM_ICON) {
1211                 BKE_icon_getid(id);
1212                 ui_id_brush_render(C, id);
1213         }
1214         else {
1215                 Object *ob = CTX_data_active_object(C);
1216                 SpaceImage *sima;
1217                 EnumPropertyItem *items = NULL;
1218                 int tool, mode = 0;
1219
1220                 /* XXX: this is not nice, should probably make brushes
1221                  * be strictly in one paint mode only to avoid
1222                  * checking various context stuff here */
1223
1224                 if (CTX_wm_view3d(C) && ob) {
1225                         if (ob->mode & OB_MODE_SCULPT)
1226                                 mode = OB_MODE_SCULPT;
1227                         else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))
1228                                 mode = OB_MODE_VERTEX_PAINT;
1229                         else if (ob->mode & OB_MODE_TEXTURE_PAINT)
1230                                 mode = OB_MODE_TEXTURE_PAINT;
1231                 }
1232                 else if ((sima = CTX_wm_space_image(C)) &&
1233                          (sima->mode == SI_MODE_PAINT))
1234                 {
1235                         mode = OB_MODE_TEXTURE_PAINT;
1236                 }
1237
1238                 /* reset the icon */
1239                 if (mode == OB_MODE_SCULPT) {
1240                         items = brush_sculpt_tool_items;
1241                         tool = br->sculpt_tool;
1242                 }
1243                 else if (mode == OB_MODE_VERTEX_PAINT) {
1244                         items = brush_vertex_tool_items;
1245                         tool = br->vertexpaint_tool;
1246                 }
1247                 else if (mode == OB_MODE_TEXTURE_PAINT) {
1248                         items = brush_image_tool_items;
1249                         tool = br->imagepaint_tool;
1250                 }
1251
1252                 if (!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1253                         id->icon_id = 0;
1254         }
1255
1256         return id->icon_id;
1257 }
1258
1259 int ui_id_icon_get(const bContext *C, ID *id, const bool big)
1260 {
1261         int iconid = 0;
1262         
1263         /* icon */
1264         switch (GS(id->name)) {
1265                 case ID_BR:
1266                         iconid = ui_id_brush_get_icon(C, id);
1267                         break;
1268                 case ID_MA: /* fall through */
1269                 case ID_TE: /* fall through */
1270                 case ID_IM: /* fall through */
1271                 case ID_WO: /* fall through */
1272                 case ID_LA: /* fall through */
1273                         iconid = BKE_icon_getid(id);
1274                         /* checks if not exists, or changed */
1275                         UI_id_icon_render(C, NULL, id, big, true);
1276                         break;
1277                 default:
1278                         break;
1279         }
1280
1281         return iconid;
1282 }
1283
1284 int UI_rnaptr_icon_get(bContext *C, PointerRNA *ptr, int rnaicon, const bool big)
1285 {
1286         ID *id = NULL;
1287
1288         if (!ptr->data)
1289                 return rnaicon;
1290
1291         /* try ID, material, texture or dynapaint slot */
1292         if (RNA_struct_is_ID(ptr->type)) {
1293                 id = ptr->id.data;
1294         }
1295         else if (RNA_struct_is_a(ptr->type, &RNA_MaterialSlot)) {
1296                 id = RNA_pointer_get(ptr, "material").data;
1297         }
1298         else if (RNA_struct_is_a(ptr->type, &RNA_TextureSlot)) {
1299                 id = RNA_pointer_get(ptr, "texture").data;
1300         }
1301         else if (RNA_struct_is_a(ptr->type, &RNA_DynamicPaintSurface)) {
1302                 DynamicPaintSurface *surface = (DynamicPaintSurface *)ptr->data;
1303
1304                 if (surface->format == MOD_DPAINT_SURFACE_F_PTEX)
1305                         return ICON_TEXTURE_SHADED;
1306                 else if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX)
1307                         return ICON_OUTLINER_DATA_MESH;
1308                 else if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ)
1309                         return ICON_FILE_IMAGE;
1310         }
1311
1312         /* get icon from ID */
1313         if (id) {
1314                 int icon = ui_id_icon_get(C, id, big);
1315
1316                 return icon ? icon : rnaicon;
1317         }
1318
1319         return rnaicon;
1320 }
1321
1322 static void icon_draw_at_size(
1323         float x, float y, int icon_id, float aspect, float alpha,
1324         enum eIconSizes size, const bool nocreate)
1325 {
1326         int draw_size = get_draw_size(size);
1327         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, false);
1328 }
1329
1330 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1331 {
1332         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
1333 }
1334
1335 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, const float rgb[3])
1336 {
1337         int draw_size = get_draw_size(ICON_SIZE_ICON);
1338         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, false, false);
1339 }
1340
1341 /* draws icon with dpi scale factor */
1342 void UI_icon_draw(float x, float y, int icon_id)
1343 {
1344         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, 1.0f);
1345 }
1346
1347 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1348 {
1349         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, true, false);
1350 }
1351
1352 void UI_icon_draw_preview(float x, float y, int icon_id)
1353 {
1354         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
1355 }
1356
1357 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1358 {
1359         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
1360 }
1361
1362 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1363 {
1364         icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, ICON_SIZE_PREVIEW, size, false, true);
1365 }
1366