517b8b61ac1f08d3e7ea48147decda6d8fb82eb0
[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_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_free_filelist(dir, totfile);
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(bContext *C, ID *id, PreviewImage *prv_img, enum eIconSizes size)
935 {
936         if (!prv_img) {
937                 if (G.debug & G_DEBUG)
938                         printf("%s: no preview image for this ID: %s\n", __func__, id->name);
939                 return;
940         }
941
942         icon_create_rect(prv_img, size);
943
944         ED_preview_icon_job(C, prv_img, id, prv_img->rect[size],
945                             prv_img->w[size], prv_img->h[size]);
946 }
947
948 PreviewImage *UI_icon_to_preview(int icon_id)
949 {
950         Icon *icon = BKE_icon_get(icon_id);
951         
952         if (icon) {
953                 DrawInfo *di = (DrawInfo *)icon->drawinfo;
954                 if (di && di->data.buffer.image) {
955                         ImBuf *bbuf;
956                         
957                         bbuf = IMB_ibImageFromMemory(di->data.buffer.image->datatoc_rect, di->data.buffer.image->datatoc_size, IB_rect, NULL, "<matcap buffer>");
958                         if (bbuf) {
959                                 PreviewImage *prv = BKE_previewimg_create();
960                                 
961                                 prv->rect[0] = bbuf->rect;
962
963                                 prv->w[0] = bbuf->x;
964                                 prv->h[0] = bbuf->y;
965                                 
966                                 bbuf->rect = NULL;
967                                 IMB_freeImBuf(bbuf);
968                                 
969                                 return prv;
970                         }
971                 }
972         }
973         return NULL;
974 }
975
976 static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect), int rw, int rh,
977                            unsigned int *rect, float alpha, const float rgb[3], const bool is_preview)
978 {
979         ImBuf *ima = NULL;
980
981         /* sanity check */
982         if (w <= 0 || h <= 0 || w > 2000 || h > 2000) {
983                 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
984                 BLI_assert(!"invalid icon size");
985                 return;
986         }
987
988         /* modulate color */
989         if (alpha != 1.0f)
990                 glPixelTransferf(GL_ALPHA_SCALE, alpha);
991
992         if (rgb) {
993                 glPixelTransferf(GL_RED_SCALE, rgb[0]);
994                 glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
995                 glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
996         }
997
998         /* rect contains image in 'rendersize', we only scale if needed */
999         if (rw != w && rh != h) {
1000                 /* first allocate imbuf for scaling and copy preview into it */
1001                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
1002                 memcpy(ima->rect, rect, rw * rh * sizeof(unsigned int));
1003                 IMB_scaleImBuf(ima, w, h); /* scale it */
1004                 rect = ima->rect;
1005         }
1006
1007         /* draw */
1008         if (is_preview) {
1009                 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
1010         }
1011         else {
1012                 glRasterPos2f(x, y);
1013                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
1014         }
1015
1016         if (ima)
1017                 IMB_freeImBuf(ima);
1018
1019         /* restore color */
1020         if (alpha != 0.0f)
1021                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1022         
1023         if (rgb) {
1024                 glPixelTransferf(GL_RED_SCALE, 1.0f);
1025                 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
1026                 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
1027         }
1028 }
1029
1030 static void icon_draw_texture(float x, float y, float w, float h, int ix, int iy,
1031                               int UNUSED(iw), int ih, float alpha, const float rgb[3])
1032 {
1033         float x1, x2, y1, y2;
1034
1035         if (rgb) glColor4f(rgb[0], rgb[1], rgb[2], alpha);
1036         else     glColor4f(alpha, alpha, alpha, alpha);
1037
1038         x1 = ix * icongltex.invw;
1039         x2 = (ix + ih) * icongltex.invw;
1040         y1 = iy * icongltex.invh;
1041         y2 = (iy + ih) * icongltex.invh;
1042
1043         glEnable(GL_TEXTURE_2D);
1044         glBindTexture(GL_TEXTURE_2D, icongltex.id);
1045
1046         /* sharper downscaling, has no effect when scale matches with a mip level */
1047         glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, -0.5f);
1048
1049         glBegin(GL_QUADS);
1050         glTexCoord2f(x1, y1);
1051         glVertex2f(x, y);
1052
1053         glTexCoord2f(x2, y1);
1054         glVertex2f(x + w, y);
1055
1056         glTexCoord2f(x2, y2);
1057         glVertex2f(x + w, y + h);
1058
1059         glTexCoord2f(x1, y2);
1060         glVertex2f(x, y + h);
1061         glEnd();
1062
1063         glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, 0.0f);
1064
1065         glBindTexture(GL_TEXTURE_2D, 0);
1066         glDisable(GL_TEXTURE_2D);
1067 }
1068
1069 /* Drawing size for preview images */
1070 static int get_draw_size(enum eIconSizes size)
1071 {
1072         switch (size) {
1073                 case ICON_SIZE_ICON: return ICON_DEFAULT_HEIGHT;
1074                 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
1075         }
1076         return 0;
1077 }
1078
1079
1080
1081 static void icon_draw_size(float x, float y, int icon_id, float aspect, float alpha, const float rgb[3],
1082                            enum eIconSizes size, int draw_size, const bool UNUSED(nocreate), const bool is_preview)
1083 {
1084         bTheme *btheme = UI_GetTheme();
1085         Icon *icon = NULL;
1086         DrawInfo *di = NULL;
1087         IconImage *iimg;
1088         const float fdraw_size = (float)draw_size;
1089         int w, h;
1090         
1091         icon = BKE_icon_get(icon_id);
1092         alpha *= btheme->tui.icon_alpha;
1093         
1094         if (icon == NULL) {
1095                 if (G.debug & G_DEBUG)
1096                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1097                 return;
1098         }
1099
1100         di = (DrawInfo *)icon->drawinfo;
1101         
1102         if (!di) {
1103                 di = icon_create_drawinfo();
1104         
1105                 icon->drawinfo = di;
1106                 icon->drawinfo_free = UI_icons_free_drawinfo;
1107         }
1108         
1109         /* scale width and height according to aspect */
1110         w = (int)(fdraw_size / aspect + 0.5f);
1111         h = (int)(fdraw_size / aspect + 0.5f);
1112         
1113         if (di->type == ICON_TYPE_VECTOR) {
1114                 /* vector icons use the uiBlock transformation, they are not drawn
1115                  * with untransformed coordinates like the other icons */
1116                 di->data.vector.func((int)x, (int)y, w, h, 1.0f);
1117         }
1118         else if (di->type == ICON_TYPE_TEXTURE) {
1119                 /* texture image use premul alpha for correct scaling */
1120                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1121                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
1122                                   di->data.texture.w, di->data.texture.h, alpha, rgb);
1123                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1124         }
1125         else if (di->type == ICON_TYPE_BUFFER) {
1126                 /* it is a builtin icon */
1127                 iimg = di->data.buffer.image;
1128 #ifndef WITH_HEADLESS
1129                 icon_verify_datatoc(iimg);
1130 #endif
1131                 if (!iimg->rect) return;  /* something has gone wrong! */
1132
1133                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1134                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
1135                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1136         }
1137         else if (di->type == ICON_TYPE_PREVIEW) {
1138                 PreviewImage *pi = BKE_previewimg_get((ID *)icon->obj);
1139
1140                 if (pi) {
1141                         /* no create icon on this level in code */
1142                         if (!pi->rect[size]) return;  /* something has gone wrong! */
1143                         
1144                         /* preview images use premul alpha ... */
1145                         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1146                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], 1.0f, NULL, is_preview);
1147                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1148                 }
1149         }
1150 }
1151
1152 static void ui_id_preview_image_render_size(bContext *C, ID *id, PreviewImage *pi, int size)
1153 {
1154         if ((pi->changed[size] || !pi->rect[size])) { /* changed only ever set by dynamic icons */
1155                 /* create the rect if necessary */
1156                 icon_set_image(C, id, pi, size);
1157
1158                 pi->changed[size] = 0;
1159         }
1160 }
1161
1162 static void ui_id_icon_render(bContext *C, ID *id, const bool big)
1163 {
1164         PreviewImage *pi = BKE_previewimg_get(id);
1165
1166         if (pi) {
1167                 if (big)
1168                         ui_id_preview_image_render_size(C, id, pi, ICON_SIZE_PREVIEW);  /* bigger preview size */
1169                 else
1170                         ui_id_preview_image_render_size(C, id, pi, ICON_SIZE_ICON);     /* icon size */
1171         }
1172 }
1173
1174 static void ui_id_brush_render(bContext *C, ID *id)
1175 {
1176         PreviewImage *pi = BKE_previewimg_get(id); 
1177         enum eIconSizes i;
1178         
1179         if (!pi)
1180                 return;
1181         
1182         for (i = 0; i < NUM_ICON_SIZES; i++) {
1183                 /* check if rect needs to be created; changed
1184                  * only set by dynamic icons */
1185                 if ((pi->changed[i] || !pi->rect[i])) {
1186                         icon_set_image(C, id, pi, i);
1187                         pi->changed[i] = 0;
1188                 }
1189         }
1190 }
1191
1192
1193 static int ui_id_brush_get_icon(bContext *C, ID *id)
1194 {
1195         Brush *br = (Brush *)id;
1196
1197         if (br->flag & BRUSH_CUSTOM_ICON) {
1198                 BKE_icon_getid(id);
1199                 ui_id_brush_render(C, id);
1200         }
1201         else {
1202                 Object *ob = CTX_data_active_object(C);
1203                 SpaceImage *sima;
1204                 EnumPropertyItem *items = NULL;
1205                 int tool, mode = 0;
1206
1207                 /* XXX: this is not nice, should probably make brushes
1208                  * be strictly in one paint mode only to avoid
1209                  * checking various context stuff here */
1210
1211                 if (CTX_wm_view3d(C) && ob) {
1212                         if (ob->mode & OB_MODE_SCULPT)
1213                                 mode = OB_MODE_SCULPT;
1214                         else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))
1215                                 mode = OB_MODE_VERTEX_PAINT;
1216                         else if (ob->mode & OB_MODE_TEXTURE_PAINT)
1217                                 mode = OB_MODE_TEXTURE_PAINT;
1218                 }
1219                 else if ((sima = CTX_wm_space_image(C)) &&
1220                          (sima->mode == SI_MODE_PAINT))
1221                 {
1222                         mode = OB_MODE_TEXTURE_PAINT;
1223                 }
1224
1225                 /* reset the icon */
1226                 if (mode == OB_MODE_SCULPT) {
1227                         items = brush_sculpt_tool_items;
1228                         tool = br->sculpt_tool;
1229                 }
1230                 else if (mode == OB_MODE_VERTEX_PAINT) {
1231                         items = brush_vertex_tool_items;
1232                         tool = br->vertexpaint_tool;
1233                 }
1234                 else if (mode == OB_MODE_TEXTURE_PAINT) {
1235                         items = brush_image_tool_items;
1236                         tool = br->imagepaint_tool;
1237                 }
1238
1239                 if (!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1240                         id->icon_id = 0;
1241         }
1242
1243         return id->icon_id;
1244 }
1245
1246 int ui_id_icon_get(bContext *C, ID *id, const bool big)
1247 {
1248         int iconid = 0;
1249         
1250         /* icon */
1251         switch (GS(id->name)) {
1252                 case ID_BR:
1253                         iconid = ui_id_brush_get_icon(C, id);
1254                         break;
1255                 case ID_MA: /* fall through */
1256                 case ID_TE: /* fall through */
1257                 case ID_IM: /* fall through */
1258                 case ID_WO: /* fall through */
1259                 case ID_LA: /* fall through */
1260                         iconid = BKE_icon_getid(id);
1261                         /* checks if not exists, or changed */
1262                         ui_id_icon_render(C, id, big);
1263                         break;
1264                 default:
1265                         break;
1266         }
1267
1268         return iconid;
1269 }
1270
1271 int UI_rnaptr_icon_get(bContext *C, PointerRNA *ptr, int rnaicon, const bool big)
1272 {
1273         ID *id = NULL;
1274
1275         if (!ptr->data)
1276                 return rnaicon;
1277
1278         /* try ID, material, texture or dynapaint slot */
1279         if (RNA_struct_is_ID(ptr->type)) {
1280                 id = ptr->id.data;
1281         }
1282         else if (RNA_struct_is_a(ptr->type, &RNA_MaterialSlot)) {
1283                 id = RNA_pointer_get(ptr, "material").data;
1284         }
1285         else if (RNA_struct_is_a(ptr->type, &RNA_TextureSlot)) {
1286                 id = RNA_pointer_get(ptr, "texture").data;
1287         }
1288         else if (RNA_struct_is_a(ptr->type, &RNA_DynamicPaintSurface)) {
1289                 DynamicPaintSurface *surface = (DynamicPaintSurface *)ptr->data;
1290
1291                 if (surface->format == MOD_DPAINT_SURFACE_F_PTEX)
1292                         return ICON_TEXTURE_SHADED;
1293                 else if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX)
1294                         return ICON_OUTLINER_DATA_MESH;
1295                 else if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ)
1296                         return ICON_FILE_IMAGE;
1297         }
1298
1299         /* get icon from ID */
1300         if (id) {
1301                 int icon = ui_id_icon_get(C, id, big);
1302
1303                 return icon ? icon : rnaicon;
1304         }
1305
1306         return rnaicon;
1307 }
1308
1309 static void icon_draw_at_size(float x, float y, int icon_id, float aspect, float alpha,
1310                               enum eIconSizes size, const bool nocreate)
1311 {
1312         int draw_size = get_draw_size(size);
1313         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, false);
1314 }
1315
1316 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1317 {
1318         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
1319 }
1320
1321 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, const float rgb[3])
1322 {
1323         int draw_size = get_draw_size(ICON_SIZE_ICON);
1324         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, false, false);
1325 }
1326
1327 /* draws icon with dpi scale factor */
1328 void UI_icon_draw(float x, float y, int icon_id)
1329 {
1330         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, 1.0f);
1331 }
1332
1333 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1334 {
1335         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, true, false);
1336 }
1337
1338 void UI_icon_draw_preview(float x, float y, int icon_id)
1339 {
1340         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
1341 }
1342
1343 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1344 {
1345         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
1346 }
1347
1348 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1349 {
1350         icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, ICON_SIZE_PREVIEW, size, false, true);
1351 }
1352