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