Merge branch 'temp_remove_particles' into blender2.8
[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 #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 viconutil_draw_tri(GLint(*pts)[2])
217 {
218         glBegin(GL_TRIANGLES);
219         glVertex2iv(pts[0]);
220         glVertex2iv(pts[1]);
221         glVertex2iv(pts[2]);
222         glEnd();
223 }
224
225 static void viconutil_draw_lineloop(GLint(*pts)[2], int numPoints)
226 {
227         int i;
228
229         glBegin(GL_LINE_LOOP);
230         for (i = 0; i < numPoints; i++) {
231                 glVertex2iv(pts[i]);
232         }
233         glEnd();
234 }
235
236 static void viconutil_draw_lineloop_smooth(GLint(*pts)[2], int numPoints)
237 {
238         glEnable(GL_LINE_SMOOTH);
239         viconutil_draw_lineloop(pts, numPoints);
240         glDisable(GL_LINE_SMOOTH);
241 }
242
243 static void viconutil_draw_points(GLint(*pts)[2], int numPoints, int pointSize)
244 {
245         int i;
246
247         glBegin(GL_QUADS);
248         for (i = 0; i < numPoints; i++) {
249                 int x = pts[i][0], y = pts[i][1];
250
251                 glVertex2i(x - pointSize, y - pointSize);
252                 glVertex2i(x + pointSize, y - pointSize);
253                 glVertex2i(x + pointSize, y + pointSize);
254                 glVertex2i(x - pointSize, y + pointSize);
255         }
256         glEnd();
257 }
258
259 /* Drawing functions */
260
261 static void vicon_x_draw(int x, int y, int w, int h, float alpha)
262 {
263         x += 3;
264         y += 3;
265         w -= 6;
266         h -= 6;
267
268         glEnable(GL_LINE_SMOOTH);
269
270         glLineWidth(2.5);
271         
272         glColor4f(0.0, 0.0, 0.0, alpha);
273         glBegin(GL_LINES);
274         glVertex2i(x, y);
275         glVertex2i(x + w, y + h);
276         glVertex2i(x + w, y);
277         glVertex2i(x, y + h);
278         glEnd();
279         
280         glDisable(GL_LINE_SMOOTH);
281 }
282
283 static void vicon_view3d_draw(int x, int y, int w, int h, float alpha)
284 {
285         int cx = x + w / 2;
286         int cy = y + h / 2;
287         int d = MAX2(2, h / 3);
288
289         glColor4f(0.5, 0.5, 0.5, alpha);
290         glBegin(GL_LINES);
291         glVertex2i(x, cy - d);
292         glVertex2i(x + w, cy - d);
293         glVertex2i(x, cy + d);
294         glVertex2i(x + w, cy + d);
295
296         glVertex2i(cx - d, y);
297         glVertex2i(cx - d, y + h);
298         glVertex2i(cx + d, y);
299         glVertex2i(cx + d, y + h);
300         glEnd();
301         
302         glColor4f(0.0, 0.0, 0.0, alpha);
303         glBegin(GL_LINES);
304         glVertex2i(x, cy);
305         glVertex2i(x + w, cy);
306         glVertex2i(cx, y);
307         glVertex2i(cx, y + h);
308         glEnd();
309 }
310
311 static void vicon_edit_draw(int x, int y, int w, int h, float alpha)
312 {
313         GLint pts[4][2];
314
315         viconutil_set_point(pts[0], x + 3,     y + 3);
316         viconutil_set_point(pts[1], x + w - 3, y + 3);
317         viconutil_set_point(pts[2], x + w - 3, y + h - 3);
318         viconutil_set_point(pts[3], x + 3,     y + h - 3);
319
320         glColor4f(0.0, 0.0, 0.0, alpha);
321         viconutil_draw_lineloop(pts, 4);
322
323         glColor3f(1, 1, 0.0);
324         viconutil_draw_points(pts, 4, 1);
325 }
326
327 static void vicon_editmode_hlt_draw(int x, int y, int w, int h, float alpha)
328 {
329         GLint pts[3][2];
330
331         viconutil_set_point(pts[0], x + w / 2, y + h - 2);
332         viconutil_set_point(pts[1], x + 3,     y + 4);
333         viconutil_set_point(pts[2], x + w - 3, y + 4);
334
335         glColor4f(0.5, 0.5, 0.5, alpha);
336         viconutil_draw_tri(pts);
337
338         glColor4f(0.0, 0.0, 0.0, 1);
339         viconutil_draw_lineloop_smooth(pts, 3);
340
341         glColor3f(1, 1, 0.0);
342         viconutil_draw_points(pts, 3, 1);
343 }
344
345 static void vicon_editmode_dehlt_draw(int x, int y, int w, int h, float UNUSED(alpha))
346 {
347         GLint pts[3][2];
348
349         viconutil_set_point(pts[0], x + w / 2, y + h - 2);
350         viconutil_set_point(pts[1], x + 3,     y + 4);
351         viconutil_set_point(pts[2], x + w - 3, y + 4);
352
353         glColor4f(0.0f, 0.0f, 0.0f, 1);
354         viconutil_draw_lineloop_smooth(pts, 3);
355
356         glColor3f(0.9f, 0.9f, 0.9f);
357         viconutil_draw_points(pts, 3, 1);
358 }
359
360 static void vicon_disclosure_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
361 {
362         GLint pts[3][2];
363         int cx = x + w / 2;
364         int cy = y + w / 2;
365         int d = w / 3, d2 = w / 5;
366
367         viconutil_set_point(pts[0], cx - d2, cy + d);
368         viconutil_set_point(pts[1], cx - d2, cy - d);
369         viconutil_set_point(pts[2], cx + d2, cy);
370
371         glShadeModel(GL_SMOOTH);
372         glBegin(GL_TRIANGLES);
373         glColor4f(0.8f, 0.8f, 0.8f, alpha);
374         glVertex2iv(pts[0]);
375         glVertex2iv(pts[1]);
376         glColor4f(0.3f, 0.3f, 0.3f, alpha);
377         glVertex2iv(pts[2]);
378         glEnd();
379         glShadeModel(GL_FLAT);
380
381         glColor4f(0.0f, 0.0f, 0.0f, 1);
382         viconutil_draw_lineloop_smooth(pts, 3);
383 }
384
385 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
386 {
387         GLint pts[3][2];
388         int cx = x + w / 2 - 4;
389         int cy = y + w / 2;
390         int d = w / 5, d2 = w / 7;
391
392         viconutil_set_point(pts[0], cx - d2, cy + d);
393         viconutil_set_point(pts[1], cx - d2, cy - d);
394         viconutil_set_point(pts[2], cx + d2, cy);
395
396         glColor4f(0.2f, 0.2f, 0.2f, alpha);
397
398         glShadeModel(GL_SMOOTH);
399         glBegin(GL_TRIANGLES);
400         glVertex2iv(pts[0]);
401         glVertex2iv(pts[1]);
402         glVertex2iv(pts[2]);
403         glEnd();
404         glShadeModel(GL_FLAT);
405 }
406
407 static void vicon_disclosure_tri_down_draw(int x, int y, int w, int UNUSED(h), float alpha)
408 {
409         GLint pts[3][2];
410         int cx = x + w / 2;
411         int cy = y + w / 2;
412         int d = w / 3, d2 = w / 5;
413
414         viconutil_set_point(pts[0], cx + d, cy + d2);
415         viconutil_set_point(pts[1], cx - d, cy + d2);
416         viconutil_set_point(pts[2], cx, cy - d2);
417
418         glShadeModel(GL_SMOOTH);
419         glBegin(GL_TRIANGLES);
420         glColor4f(0.8f, 0.8f, 0.8f, alpha);
421         glVertex2iv(pts[0]);
422         glVertex2iv(pts[1]);
423         glColor4f(0.3f, 0.3f, 0.3f, alpha);
424         glVertex2iv(pts[2]);
425         glEnd();
426         glShadeModel(GL_FLAT);
427
428         glColor4f(0.0f, 0.0f, 0.0f, 1);
429         viconutil_draw_lineloop_smooth(pts, 3);
430 }
431
432 static void vicon_move_up_draw(int x, int y, int w, int h, float UNUSED(alpha))
433 {
434         int d = -2;
435
436         glEnable(GL_LINE_SMOOTH);
437         glLineWidth(1);
438         glColor3f(0.0, 0.0, 0.0);
439
440         glBegin(GL_LINE_STRIP);
441         glVertex2i(x + w / 2 - d * 2, y + h / 2 + d);
442         glVertex2i(x + w / 2, y + h / 2 - d + 1);
443         glVertex2i(x + w / 2 + d * 2, y + h / 2 + d);
444         glEnd();
445
446         glDisable(GL_LINE_SMOOTH);
447 }
448
449 static void vicon_move_down_draw(int x, int y, int w, int h, float UNUSED(alpha))
450 {
451         int d = 2;
452
453         glEnable(GL_LINE_SMOOTH);
454         glLineWidth(1);
455         glColor3f(0.0, 0.0, 0.0);
456
457         glBegin(GL_LINE_STRIP);
458         glVertex2i(x + w / 2 - d * 2, y + h / 2 + d);
459         glVertex2i(x + w / 2, y + h / 2 - d - 1);
460         glVertex2i(x + w / 2 + d * 2, y + h / 2 + d);
461         glEnd();
462
463         glDisable(GL_LINE_SMOOTH);
464 }
465
466 static void vicon_keytype_draw_wrapper(int x, int y, int w, int h, float alpha, short key_type)
467 {
468         /* init dummy theme state for Action Editor - where these colors are defined
469          * (since we're doing this offscreen, free from any particular space_id)
470          */
471         struct bThemeState theme_state;
472         int xco, yco;
473         
474         UI_Theme_Store(&theme_state);
475         UI_SetTheme(SPACE_ACTION, RGN_TYPE_WINDOW);
476         
477         /* the "x" and "y" given are the bottom-left coordinates of the icon,
478          * while the draw_keyframe_shape() function needs the midpoint for
479          * the keyframe
480          */
481         xco = x + w / 2;
482         yco = y + h / 2;
483         
484         /* draw keyframe
485          * - xscale: 1.0 (since there's no timeline scaling to compensate for)
486          * - yscale: 0.3 * h (found out experimentally... dunno why!)
487          * - sel: true (so that "keyframe" state shows the iconic yellow icon)
488          */
489         draw_keyframe_shape(xco, yco, 1.0f, 0.3f * h, true, key_type, KEYFRAME_SHAPE_BOTH, alpha);
490         
491         UI_Theme_Restore(&theme_state);
492 }
493
494 static void vicon_keytype_keyframe_draw(int x, int y, int w, int h, float alpha)
495 {
496         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME);
497 }
498
499 static void vicon_keytype_breakdown_draw(int x, int y, int w, int h, float alpha)
500 {
501         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_BREAKDOWN);
502 }
503
504 static void vicon_keytype_extreme_draw(int x, int y, int w, int h, float alpha)
505 {
506         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_EXTREME);
507 }
508
509 static void vicon_keytype_jitter_draw(int x, int y, int w, int h, float alpha)
510 {
511         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_JITTER);
512 }
513
514 #ifndef WITH_HEADLESS
515
516 static void init_brush_icons(void)
517 {
518
519 #define INIT_BRUSH_ICON(icon_id, name)                                          \
520         {                                                                           \
521                 unsigned char *rect = (unsigned char *)datatoc_ ##name## _png;          \
522                 int size = datatoc_ ##name## _png_size;                                 \
523                 DrawInfo *di;                                                           \
524                 \
525                 di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_BUFFER);       \
526                 di->data.buffer.image->datatoc_rect = rect;                             \
527                 di->data.buffer.image->datatoc_size = size;                             \
528         }
529         /* end INIT_BRUSH_ICON */
530
531         const int w = 96; /* warning, brush size hardcoded in C, but it gets scaled */
532
533         INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
534         INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
535         INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
536         INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
537         INIT_BRUSH_ICON(ICON_BRUSH_CLAY_STRIPS, claystrips);
538         INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
539         INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
540         INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
541         INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
542         INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
543         INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
544         INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
545         INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
546         INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
547         INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
548         INIT_BRUSH_ICON(ICON_BRUSH_MASK, mask);
549         INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
550         INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
551         INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
552         INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
553         INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
554         INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
555         INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
556         INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
557         INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
558         INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
559         INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
560         INIT_BRUSH_ICON(ICON_BRUSH_TEXFILL, texfill);
561         INIT_BRUSH_ICON(ICON_BRUSH_TEXMASK, texmask);
562         INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
563         INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
564         INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
565
566 #undef INIT_BRUSH_ICON
567 }
568
569 static void icon_verify_datatoc(IconImage *iimg)
570 {
571         /* if it has own rect, things are all OK */
572         if (iimg->rect)
573                 return;
574         
575         if (iimg->datatoc_rect) {
576                 ImBuf *bbuf = IMB_ibImageFromMemory(iimg->datatoc_rect,
577                                                     iimg->datatoc_size, IB_rect, NULL, "<matcap icon>");
578                 /* w and h were set on initialize */
579                 if (bbuf->x != iimg->h && bbuf->y != iimg->w)
580                         IMB_scaleImBuf(bbuf, iimg->w, iimg->h);
581                 
582                 iimg->rect = bbuf->rect;
583                 bbuf->rect = NULL;
584                 IMB_freeImBuf(bbuf);
585         }
586 }
587
588 static void init_matcap_icons(void)
589 {
590         /* dynamic allocation now, tucking datatoc pointers in DrawInfo */
591 #define INIT_MATCAP_ICON(icon_id, name)                                       \
592         {                                                                         \
593                 unsigned char *rect = (unsigned char *)datatoc_ ##name## _jpg;        \
594                 int size = datatoc_ ##name## _jpg_size;                               \
595                 DrawInfo *di;                                                         \
596                                                                                       \
597                 di = def_internal_icon(NULL, icon_id, 0, 0, 96, ICON_TYPE_BUFFER);   \
598                 di->data.buffer.image->datatoc_rect = rect;                           \
599                 di->data.buffer.image->datatoc_size = size;                           \
600         } (void)0
601
602         INIT_MATCAP_ICON(ICON_MATCAP_01, mc01);
603         INIT_MATCAP_ICON(ICON_MATCAP_02, mc02);
604         INIT_MATCAP_ICON(ICON_MATCAP_03, mc03);
605         INIT_MATCAP_ICON(ICON_MATCAP_04, mc04);
606         INIT_MATCAP_ICON(ICON_MATCAP_05, mc05);
607         INIT_MATCAP_ICON(ICON_MATCAP_06, mc06);
608         INIT_MATCAP_ICON(ICON_MATCAP_07, mc07);
609         INIT_MATCAP_ICON(ICON_MATCAP_08, mc08);
610         INIT_MATCAP_ICON(ICON_MATCAP_09, mc09);
611         INIT_MATCAP_ICON(ICON_MATCAP_10, mc10);
612         INIT_MATCAP_ICON(ICON_MATCAP_11, mc11);
613         INIT_MATCAP_ICON(ICON_MATCAP_12, mc12);
614         INIT_MATCAP_ICON(ICON_MATCAP_13, mc13);
615         INIT_MATCAP_ICON(ICON_MATCAP_14, mc14);
616         INIT_MATCAP_ICON(ICON_MATCAP_15, mc15);
617         INIT_MATCAP_ICON(ICON_MATCAP_16, mc16);
618         INIT_MATCAP_ICON(ICON_MATCAP_17, mc17);
619         INIT_MATCAP_ICON(ICON_MATCAP_18, mc18);
620         INIT_MATCAP_ICON(ICON_MATCAP_19, mc19);
621         INIT_MATCAP_ICON(ICON_MATCAP_20, mc20);
622         INIT_MATCAP_ICON(ICON_MATCAP_21, mc21);
623         INIT_MATCAP_ICON(ICON_MATCAP_22, mc22);
624         INIT_MATCAP_ICON(ICON_MATCAP_23, mc23);
625         INIT_MATCAP_ICON(ICON_MATCAP_24, mc24);
626
627 #undef INIT_MATCAP_ICON
628
629 }
630
631 static void init_internal_icons(void)
632 {
633 //      bTheme *btheme = UI_GetTheme();
634         ImBuf *b16buf = NULL, *b32buf = NULL;
635         int x, y, icontype;
636
637 #if 0 // temp disabled
638         if ((btheme != NULL) && btheme->tui.iconfile[0]) {
639                 char *icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
640                 char iconfilestr[FILE_MAX];
641                 
642                 if (icondir) {
643                         BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
644                         bbuf = IMB_loadiffname(iconfilestr, IB_rect, NULL); /* if the image is missing bbuf will just be NULL */
645                         if (bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
646                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
647                                 IMB_freeImBuf(bbuf);
648                                 bbuf = NULL;
649                         }
650                 }
651                 else {
652                         printf("%s: 'icons' data path not found, continuing\n", __func__);
653                 }
654         }
655 #endif
656         if (b16buf == NULL)
657                 b16buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons16_png,
658                                                datatoc_blender_icons16_png_size, IB_rect, NULL, "<blender icons>");
659         if (b16buf)
660                 IMB_premultiply_alpha(b16buf);
661
662         if (b32buf == NULL)
663                 b32buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons32_png,
664                                                datatoc_blender_icons32_png_size, IB_rect, NULL, "<blender icons>");
665         if (b32buf)
666                 IMB_premultiply_alpha(b32buf);
667         
668         if (b16buf && b32buf) {
669                 /* free existing texture if any */
670                 if (icongltex.id) {
671                         glDeleteTextures(1, &icongltex.id);
672                         icongltex.id = 0;
673                 }
674
675                 /* we only use a texture for cards with non-power of two */
676                 if (GPU_full_non_power_of_two_support()) {
677                         glGenTextures(1, &icongltex.id);
678
679                         if (icongltex.id) {
680                                 int level = 2;
681                                 
682                                 icongltex.w = b32buf->x;
683                                 icongltex.h = b32buf->y;
684                                 icongltex.invw = 1.0f / b32buf->x;
685                                 icongltex.invh = 1.0f / b32buf->y;
686
687                                 glBindTexture(GL_TEXTURE_2D, icongltex.id);
688                                 
689                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, b32buf->x, b32buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b32buf->rect);
690                                 glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, b16buf->x, b16buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b16buf->rect);
691                                 
692                                 while (b16buf->x > 1) {
693                                         ImBuf *nbuf = IMB_onehalf(b16buf);
694                                         glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, nbuf->x, nbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nbuf->rect);
695                                         level++;
696                                         IMB_freeImBuf(b16buf);
697                                         b16buf = nbuf;
698                                 }
699                                 
700                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
701                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
702                                 
703                                 glBindTexture(GL_TEXTURE_2D, 0);
704                                 
705                                 if (glGetError() == GL_OUT_OF_MEMORY) {
706                                         glDeleteTextures(1, &icongltex.id);
707                                         icongltex.id = 0;
708                                 }
709                         }
710                 }
711         }
712
713         if (icongltex.id)
714                 icontype = ICON_TYPE_TEXTURE;
715         else
716                 icontype = ICON_TYPE_BUFFER;
717         
718         if (b32buf) {
719                 for (y = 0; y < ICON_GRID_ROWS; y++) {
720                         for (x = 0; x < ICON_GRID_COLS; x++) {
721                                 def_internal_icon(b32buf, BIFICONID_FIRST + y * ICON_GRID_COLS + x,
722                                                   x * (ICON_GRID_W + ICON_GRID_MARGIN) + ICON_GRID_MARGIN,
723                                                   y * (ICON_GRID_H + ICON_GRID_MARGIN) + ICON_GRID_MARGIN, ICON_GRID_W,
724                                                   icontype);
725                         }
726                 }
727         }
728
729         def_internal_vicon(VICO_VIEW3D_VEC, vicon_view3d_draw);
730         def_internal_vicon(VICO_EDIT_VEC, vicon_edit_draw);
731         def_internal_vicon(VICO_EDITMODE_VEC_DEHLT, vicon_editmode_dehlt_draw);
732         def_internal_vicon(VICO_EDITMODE_VEC_HLT, vicon_editmode_hlt_draw);
733         def_internal_vicon(VICO_DISCLOSURE_TRI_RIGHT_VEC, vicon_disclosure_tri_right_draw);
734         def_internal_vicon(VICO_DISCLOSURE_TRI_DOWN_VEC, vicon_disclosure_tri_down_draw);
735         def_internal_vicon(VICO_MOVE_UP_VEC, vicon_move_up_draw);
736         def_internal_vicon(VICO_MOVE_DOWN_VEC, vicon_move_down_draw);
737         def_internal_vicon(VICO_X_VEC, vicon_x_draw);
738         def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
739         
740         def_internal_vicon(VICO_KEYTYPE_KEYFRAME_VEC, vicon_keytype_keyframe_draw);
741         def_internal_vicon(VICO_KEYTYPE_BREAKDOWN_VEC, vicon_keytype_breakdown_draw);
742         def_internal_vicon(VICO_KEYTYPE_EXTREME_VEC, vicon_keytype_extreme_draw);
743         def_internal_vicon(VICO_KEYTYPE_JITTER_VEC, vicon_keytype_jitter_draw);
744
745         IMB_freeImBuf(b16buf);
746         IMB_freeImBuf(b32buf);
747         
748 }
749 #endif  /* WITH_HEADLESS */
750
751 static void init_iconfile_list(struct ListBase *list)
752 {
753         IconFile *ifile;
754         struct direntry *dir;
755         int totfile, i, index = 1;
756         const char *icondir;
757
758         BLI_listbase_clear(list);
759         icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
760
761         if (icondir == NULL)
762                 return;
763         
764         totfile = BLI_filelist_dir_contents(icondir, &dir);
765
766         for (i = 0; i < totfile; i++) {
767                 if ((dir[i].type & S_IFREG)) {
768                         const char *filename = dir[i].relname;
769                         
770                         if (BLI_testextensie(filename, ".png")) {
771                                 /* loading all icons on file start is overkill & slows startup
772                                  * its possible they change size after blender load anyway. */
773 #if 0
774                                 int ifilex, ifiley;
775                                 char iconfilestr[FILE_MAX + 16]; /* allow 256 chars for file+dir */
776                                 ImBuf *bbuf = NULL;
777                                 /* check to see if the image is the right size, continue if not */
778                                 /* copying strings here should go ok, assuming that we never get back
779                                  * a complete path to file longer than 256 chars */
780                                 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
781                                 bbuf = IMB_loadiffname(iconfilestr, IB_rect);
782
783                                 if (bbuf) {
784                                         ifilex = bbuf->x;
785                                         ifiley = bbuf->y;
786                                         IMB_freeImBuf(bbuf);
787                                 }
788                                 else {
789                                         ifilex = ifiley = 0;
790                                 }
791                                 
792                                 /* bad size or failed to load */
793                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
794                                         printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
795                                         continue;
796                                 }
797 #endif          /* removed */
798
799                                 /* found a potential icon file, so make an entry for it in the cache list */
800                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
801                                 
802                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
803                                 ifile->index = index;
804
805                                 BLI_addtail(list, ifile);
806                                 
807                                 index++;
808                         }
809                 }
810         }
811
812         BLI_filelist_free(dir, totfile);
813         dir = NULL;
814 }
815
816 static void free_iconfile_list(struct ListBase *list)
817 {
818         IconFile *ifile = NULL, *next_ifile = NULL;
819         
820         for (ifile = list->first; ifile; ifile = next_ifile) {
821                 next_ifile = ifile->next;
822                 BLI_freelinkN(list, ifile);
823         }
824 }
825
826 #endif  /* WITH_HEADLESS */
827
828 int UI_iconfile_get_index(const char *filename)
829 {
830         IconFile *ifile;
831         ListBase *list = &(iconfilelist);
832         
833         for (ifile = list->first; ifile; ifile = ifile->next) {
834                 if (BLI_path_cmp(filename, ifile->filename) == 0) {
835                         return ifile->index;
836                 }
837         }
838         
839         return 0;
840 }
841
842 ListBase *UI_iconfile_list(void)
843 {
844         ListBase *list = &(iconfilelist);
845         
846         return list;
847 }
848
849
850 void UI_icons_free(void)
851 {
852 #ifndef WITH_HEADLESS
853         if (icongltex.id) {
854                 glDeleteTextures(1, &icongltex.id);
855                 icongltex.id = 0;
856         }
857
858         free_iconfile_list(&iconfilelist);
859         BKE_icons_free();
860 #endif
861 }
862
863 void UI_icons_free_drawinfo(void *drawinfo)
864 {
865         DrawInfo *di = drawinfo;
866
867         if (di) {
868                 if (di->type == ICON_TYPE_BUFFER) {
869                         if (di->data.buffer.image) {
870                                 if (di->data.buffer.image->rect)
871                                         MEM_freeN(di->data.buffer.image->rect);
872                                 MEM_freeN(di->data.buffer.image);
873                         }
874                 }
875
876                 MEM_freeN(di);
877         }
878 }
879
880 static DrawInfo *icon_create_drawinfo(void)
881 {
882         DrawInfo *di = NULL;
883
884         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
885         di->type = ICON_TYPE_PREVIEW;
886
887         return di;
888 }
889
890 /* note!, returns unscaled by DPI */
891 int UI_icon_get_width(int icon_id)
892 {
893         Icon *icon = NULL;
894         DrawInfo *di = NULL;
895
896         icon = BKE_icon_get(icon_id);
897         
898         if (icon == NULL) {
899                 if (G.debug & G_DEBUG)
900                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
901                 return 0;
902         }
903         
904         di = (DrawInfo *)icon->drawinfo;
905         if (!di) {
906                 di = icon_create_drawinfo();
907                 icon->drawinfo = di;
908         }
909
910         if (di)
911                 return ICON_DEFAULT_WIDTH;
912
913         return 0;
914 }
915
916 int UI_icon_get_height(int icon_id)
917 {
918         Icon *icon = NULL;
919         DrawInfo *di = NULL;
920
921         icon = BKE_icon_get(icon_id);
922         
923         if (icon == NULL) {
924                 if (G.debug & G_DEBUG)
925                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
926                 return 0;
927         }
928         
929         di = (DrawInfo *)icon->drawinfo;
930
931         if (!di) {
932                 di = icon_create_drawinfo();
933                 icon->drawinfo = di;
934         }
935         
936         if (di)
937                 return ICON_DEFAULT_HEIGHT;
938
939         return 0;
940 }
941
942 void UI_icons_init(int first_dyn_id)
943 {
944         BKE_icons_init(first_dyn_id);
945 #ifndef WITH_HEADLESS
946         init_iconfile_list(&iconfilelist);
947         init_internal_icons();
948         init_brush_icons();
949         init_matcap_icons();
950 #endif
951 }
952
953 /* Render size for preview images and icons
954  */
955 int UI_preview_render_size(enum eIconSizes size)
956 {
957         switch (size) {
958                 case ICON_SIZE_ICON:
959                         return ICON_RENDER_DEFAULT_HEIGHT;
960                 case ICON_SIZE_PREVIEW:
961                         return PREVIEW_RENDER_DEFAULT_HEIGHT;
962                 default:
963                         return 0;
964         }
965 }
966
967 /* Create rect for the icon
968  */
969 static void icon_create_rect(struct PreviewImage *prv_img, enum eIconSizes size)
970 {
971         unsigned int render_size = UI_preview_render_size(size);
972
973         if (!prv_img) {
974                 if (G.debug & G_DEBUG)
975                         printf("%s, error: requested preview image does not exist", __func__);
976         }
977         else if (!prv_img->rect[size]) {
978                 prv_img->w[size] = render_size;
979                 prv_img->h[size] = render_size;
980                 prv_img->flag[size] |= PRV_CHANGED;
981                 prv_img->changed_timestamp[size] = 0;
982                 prv_img->rect[size] = MEM_callocN(render_size * render_size * sizeof(unsigned int), "prv_rect");
983         }
984 }
985
986 void ui_icon_ensure_deferred(const bContext *C, const int icon_id, const bool big)
987 {
988         Icon *icon = BKE_icon_get(icon_id);
989
990         if (icon) {
991                 DrawInfo *di = (DrawInfo *)icon->drawinfo;
992
993                 if (!di) {
994                         di = icon_create_drawinfo();
995
996                         icon->drawinfo = di;
997                         icon->drawinfo_free = UI_icons_free_drawinfo;
998                 }
999
1000                 if (di) {
1001                         if (di->type == ICON_TYPE_PREVIEW) {
1002                                 PreviewImage *prv = (icon->type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
1003
1004                                 if (prv) {
1005                                         const int size = big ? ICON_SIZE_PREVIEW : ICON_SIZE_ICON;
1006
1007                                         if (!prv->use_deferred || prv->rect[size] || (prv->flag[size] & PRV_USER_EDITED)) {
1008                                                 return;
1009                                         }
1010
1011                                         icon_create_rect(prv, size);
1012
1013                                         /* Always using job (background) version. */
1014                                         ED_preview_icon_job(C, prv, NULL, prv->rect[size], prv->w[size], prv->h[size]);
1015
1016                                         prv->flag[size] &= ~PRV_CHANGED;
1017                                 }
1018                         }
1019                 }
1020         }
1021 }
1022
1023 /* only called when icon has changed */
1024 /* only call with valid pointer from UI_icon_draw */
1025 static void icon_set_image(
1026         const bContext *C, Scene *scene, ID *id, PreviewImage *prv_img, enum eIconSizes size, const bool use_job)
1027 {
1028         if (!prv_img) {
1029                 if (G.debug & G_DEBUG)
1030                         printf("%s: no preview image for this ID: %s\n", __func__, id->name);
1031                 return;
1032         }
1033
1034         if (prv_img->flag[size] & PRV_USER_EDITED) {
1035                 /* user-edited preview, do not auto-update! */
1036                 return;
1037         }
1038
1039         icon_create_rect(prv_img, size);
1040
1041         if (use_job) {
1042                 /* Job (background) version */
1043                 ED_preview_icon_job(C, prv_img, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
1044         }
1045         else {
1046                 if (!scene) {
1047                         scene = CTX_data_scene(C);
1048                 }
1049                 /* Immediate version */
1050                 ED_preview_icon_render(CTX_data_main(C), scene, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
1051         }
1052 }
1053
1054 PreviewImage *UI_icon_to_preview(int icon_id)
1055 {
1056         Icon *icon = BKE_icon_get(icon_id);
1057         
1058         if (icon) {
1059                 DrawInfo *di = (DrawInfo *)icon->drawinfo;
1060                 if (di) {
1061                         if (di->type == ICON_TYPE_PREVIEW) {
1062                                 PreviewImage *prv = (icon->type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
1063
1064                                 if (prv) {
1065                                         return BKE_previewimg_copy(prv);
1066                                 }
1067                         }
1068                         else if (di->data.buffer.image) {
1069                                 ImBuf *bbuf;
1070
1071                                 bbuf = IMB_ibImageFromMemory(di->data.buffer.image->datatoc_rect, di->data.buffer.image->datatoc_size,
1072                                                              IB_rect, NULL, __func__);
1073                                 if (bbuf) {
1074                                         PreviewImage *prv = BKE_previewimg_create();
1075
1076                                         prv->rect[0] = bbuf->rect;
1077
1078                                         prv->w[0] = bbuf->x;
1079                                         prv->h[0] = bbuf->y;
1080
1081                                         bbuf->rect = NULL;
1082                                         IMB_freeImBuf(bbuf);
1083
1084                                         return prv;
1085                                 }
1086                         }
1087                 }
1088         }
1089         return NULL;
1090 }
1091
1092 static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect), int rw, int rh,
1093                            unsigned int *rect, float alpha, const float rgb[3], const bool is_preview)
1094 {
1095         ImBuf *ima = NULL;
1096         int draw_w = w;
1097         int draw_h = h;
1098         int draw_x = x;
1099         int draw_y = y;
1100
1101         /* sanity check */
1102         if (w <= 0 || h <= 0 || w > 2000 || h > 2000) {
1103                 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
1104                 BLI_assert(!"invalid icon size");
1105                 return;
1106         }
1107
1108         /* modulate color */
1109         if (alpha != 1.0f)
1110                 glPixelTransferf(GL_ALPHA_SCALE, alpha);
1111
1112         if (rgb) {
1113                 glPixelTransferf(GL_RED_SCALE, rgb[0]);
1114                 glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
1115                 glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
1116         }
1117
1118         /* rect contains image in 'rendersize', we only scale if needed */
1119         if (rw != w || rh != h) {
1120                 /* preserve aspect ratio and center */
1121                 if (rw > rh) {
1122                         draw_w = w;
1123                         draw_h = (int)(((float)rh / (float)rw) * (float)w);
1124                         draw_y += (h - draw_h) / 2;
1125                 }
1126                 else if (rw < rh) {
1127                         draw_w = (int)(((float)rw / (float)rh) * (float)h);
1128                         draw_h = h;
1129                         draw_x += (w - draw_w) / 2;
1130                 }
1131                 /* if the image is squared, the draw_ initialization values are good */
1132
1133                 /* first allocate imbuf for scaling and copy preview into it */
1134                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
1135                 memcpy(ima->rect, rect, rw * rh * sizeof(unsigned int));
1136                 IMB_scaleImBuf(ima, draw_w, draw_h); /* scale it */
1137                 rect = ima->rect;
1138         }
1139
1140         /* draw */
1141         if (is_preview) {
1142                 glaDrawPixelsSafe(draw_x, draw_y, draw_w, draw_h, draw_w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
1143         }
1144         else {
1145                 glRasterPos2f(draw_x, draw_y);
1146                 glDrawPixels(draw_w, draw_h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
1147         }
1148
1149         if (ima)
1150                 IMB_freeImBuf(ima);
1151
1152         /* restore color */
1153         if (alpha != 0.0f)
1154                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1155         
1156         if (rgb) {
1157                 glPixelTransferf(GL_RED_SCALE, 1.0f);
1158                 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
1159                 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
1160         }
1161 }
1162
1163 static void icon_draw_texture(
1164         float x, float y, float w, float h, int ix, int iy,
1165         int UNUSED(iw), int ih, float alpha, const float rgb[3])
1166 {
1167         float x1, x2, y1, y2;
1168
1169         if (rgb) glColor4f(rgb[0], rgb[1], rgb[2], alpha);
1170         else     glColor4f(alpha, alpha, alpha, alpha);
1171
1172         x1 = ix * icongltex.invw;
1173         x2 = (ix + ih) * icongltex.invw;
1174         y1 = iy * icongltex.invh;
1175         y2 = (iy + ih) * icongltex.invh;
1176
1177         GPU_basic_shader_bind(GPU_SHADER_TEXTURE_2D | GPU_SHADER_USE_COLOR);
1178         glBindTexture(GL_TEXTURE_2D, icongltex.id);
1179
1180         /* sharper downscaling, has no effect when scale matches with a mip level */
1181         glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, -0.5f);
1182
1183         glBegin(GL_QUADS);
1184         glTexCoord2f(x1, y1);
1185         glVertex2f(x, y);
1186
1187         glTexCoord2f(x2, y1);
1188         glVertex2f(x + w, y);
1189
1190         glTexCoord2f(x2, y2);
1191         glVertex2f(x + w, y + h);
1192
1193         glTexCoord2f(x1, y2);
1194         glVertex2f(x, y + h);
1195         glEnd();
1196
1197         glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, 0.0f);
1198
1199         glBindTexture(GL_TEXTURE_2D, 0);
1200         GPU_basic_shader_bind(GPU_SHADER_USE_COLOR);
1201 }
1202
1203 /* Drawing size for preview images */
1204 static int get_draw_size(enum eIconSizes size)
1205 {
1206         switch (size) {
1207                 case ICON_SIZE_ICON:
1208                         return ICON_DEFAULT_HEIGHT;
1209                 case ICON_SIZE_PREVIEW:
1210                         return PREVIEW_DEFAULT_HEIGHT;
1211                 default:
1212                         return 0;
1213         }
1214 }
1215
1216
1217
1218 static void icon_draw_size(
1219         float x, float y, int icon_id, float aspect, float alpha, const float rgb[3],
1220         enum eIconSizes size, int draw_size, const bool UNUSED(nocreate), const bool is_preview)
1221 {
1222         bTheme *btheme = UI_GetTheme();
1223         Icon *icon = NULL;
1224         DrawInfo *di = NULL;
1225         IconImage *iimg;
1226         const float fdraw_size = (float)draw_size;
1227         int w, h;
1228         
1229         icon = BKE_icon_get(icon_id);
1230         alpha *= btheme->tui.icon_alpha;
1231         
1232         if (icon == NULL) {
1233                 if (G.debug & G_DEBUG)
1234                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1235                 return;
1236         }
1237
1238         di = (DrawInfo *)icon->drawinfo;
1239         
1240         if (!di) {
1241                 di = icon_create_drawinfo();
1242         
1243                 icon->drawinfo = di;
1244                 icon->drawinfo_free = UI_icons_free_drawinfo;
1245         }
1246         
1247         /* scale width and height according to aspect */
1248         w = (int)(fdraw_size / aspect + 0.5f);
1249         h = (int)(fdraw_size / aspect + 0.5f);
1250         
1251         if (di->type == ICON_TYPE_VECTOR) {
1252                 /* vector icons use the uiBlock transformation, they are not drawn
1253                  * with untransformed coordinates like the other icons */
1254                 di->data.vector.func((int)x, (int)y, w, h, 1.0f);
1255         }
1256         else if (di->type == ICON_TYPE_TEXTURE) {
1257                 /* texture image use premul alpha for correct scaling */
1258                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1259                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
1260                                   di->data.texture.w, di->data.texture.h, alpha, rgb);
1261                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1262         }
1263         else if (di->type == ICON_TYPE_BUFFER) {
1264                 /* it is a builtin icon */
1265                 iimg = di->data.buffer.image;
1266 #ifndef WITH_HEADLESS
1267                 icon_verify_datatoc(iimg);
1268 #endif
1269                 if (!iimg->rect) return;  /* something has gone wrong! */
1270
1271                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1272                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
1273                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1274         }
1275         else if (di->type == ICON_TYPE_PREVIEW) {
1276                 PreviewImage *pi = (icon->type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
1277
1278                 if (pi) {
1279                         /* no create icon on this level in code */
1280                         if (!pi->rect[size]) return;  /* something has gone wrong! */
1281                         
1282                         /* preview images use premul alpha ... */
1283                         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1284
1285                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], alpha, rgb, is_preview);
1286                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1287                 }
1288         }
1289 }
1290
1291 static void ui_id_preview_image_render_size(
1292         const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job)
1293 {
1294         if (((pi->flag[size] & PRV_CHANGED) || !pi->rect[size])) { /* changed only ever set by dynamic icons */
1295                 /* create the rect if necessary */
1296                 icon_set_image(C, scene, id, pi, size, use_job);
1297
1298                 pi->flag[size] &= ~PRV_CHANGED;
1299         }
1300 }
1301
1302 void UI_id_icon_render(const bContext *C, Scene *scene, ID *id, const bool big, const bool use_job)
1303 {
1304         PreviewImage *pi = BKE_previewimg_id_ensure(id);
1305
1306         if (pi) {
1307                 if (big)
1308                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_PREVIEW, use_job);  /* bigger preview size */
1309                 else
1310                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_ICON, use_job);     /* icon size */
1311         }
1312 }
1313
1314 static void ui_id_brush_render(const bContext *C, ID *id)
1315 {
1316         PreviewImage *pi = BKE_previewimg_id_ensure(id);
1317         enum eIconSizes i;
1318         
1319         if (!pi)
1320                 return;
1321         
1322         for (i = 0; i < NUM_ICON_SIZES; i++) {
1323                 /* check if rect needs to be created; changed
1324                  * only set by dynamic icons */
1325                 if (((pi->flag[i] & PRV_CHANGED) || !pi->rect[i])) {
1326                         icon_set_image(C, NULL, id, pi, i, true);
1327                         pi->flag[i] &= ~PRV_CHANGED;
1328                 }
1329         }
1330 }
1331
1332
1333 static int ui_id_brush_get_icon(const bContext *C, ID *id)
1334 {
1335         Brush *br = (Brush *)id;
1336
1337         if (br->flag & BRUSH_CUSTOM_ICON) {
1338                 BKE_icon_id_ensure(id);
1339                 ui_id_brush_render(C, id);
1340         }
1341         else {
1342                 Object *ob = CTX_data_active_object(C);
1343                 SpaceImage *sima;
1344                 EnumPropertyItem *items = NULL;
1345                 int tool = PAINT_TOOL_DRAW, mode = 0;
1346
1347                 /* XXX: this is not nice, should probably make brushes
1348                  * be strictly in one paint mode only to avoid
1349                  * checking various context stuff here */
1350
1351                 if (CTX_wm_view3d(C) && ob) {
1352                         if (ob->mode & OB_MODE_SCULPT)
1353                                 mode = OB_MODE_SCULPT;
1354                         else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))
1355                                 mode = OB_MODE_VERTEX_PAINT;
1356                         else if (ob->mode & OB_MODE_TEXTURE_PAINT)
1357                                 mode = OB_MODE_TEXTURE_PAINT;
1358                 }
1359                 else if ((sima = CTX_wm_space_image(C)) &&
1360                          (sima->mode == SI_MODE_PAINT))
1361                 {
1362                         mode = OB_MODE_TEXTURE_PAINT;
1363                 }
1364
1365                 /* reset the icon */
1366                 if (mode == OB_MODE_SCULPT) {
1367                         items = rna_enum_brush_sculpt_tool_items;
1368                         tool = br->sculpt_tool;
1369                 }
1370                 else if (mode == OB_MODE_VERTEX_PAINT) {
1371                         items = rna_enum_brush_vertex_tool_items;
1372                         tool = br->vertexpaint_tool;
1373                 }
1374                 else if (mode == OB_MODE_TEXTURE_PAINT) {
1375                         items = rna_enum_brush_image_tool_items;
1376                         tool = br->imagepaint_tool;
1377                 }
1378
1379                 if (!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1380                         id->icon_id = 0;
1381         }
1382
1383         return id->icon_id;
1384 }
1385
1386 int ui_id_icon_get(const bContext *C, ID *id, const bool big)
1387 {
1388         int iconid = 0;
1389         
1390         /* icon */
1391         switch (GS(id->name)) {
1392                 case ID_BR:
1393                         iconid = ui_id_brush_get_icon(C, id);
1394                         break;
1395                 case ID_MA: /* fall through */
1396                 case ID_TE: /* fall through */
1397                 case ID_IM: /* fall through */
1398                 case ID_WO: /* fall through */
1399                 case ID_LA: /* fall through */
1400                         iconid = BKE_icon_id_ensure(id);
1401                         /* checks if not exists, or changed */
1402                         UI_id_icon_render(C, NULL, id, big, true);
1403                         break;
1404                 default:
1405                         break;
1406         }
1407
1408         return iconid;
1409 }
1410
1411 int UI_rnaptr_icon_get(bContext *C, PointerRNA *ptr, int rnaicon, const bool big)
1412 {
1413         ID *id = NULL;
1414
1415         if (!ptr->data)
1416                 return rnaicon;
1417
1418         /* try ID, material, texture or dynapaint slot */
1419         if (RNA_struct_is_ID(ptr->type)) {
1420                 id = ptr->id.data;
1421         }
1422         else if (RNA_struct_is_a(ptr->type, &RNA_MaterialSlot)) {
1423                 id = RNA_pointer_get(ptr, "material").data;
1424         }
1425         else if (RNA_struct_is_a(ptr->type, &RNA_TextureSlot)) {
1426                 id = RNA_pointer_get(ptr, "texture").data;
1427         }
1428         else if (RNA_struct_is_a(ptr->type, &RNA_DynamicPaintSurface)) {
1429                 DynamicPaintSurface *surface = (DynamicPaintSurface *)ptr->data;
1430
1431                 if (surface->format == MOD_DPAINT_SURFACE_F_PTEX)
1432                         return ICON_TEXTURE_SHADED;
1433                 else if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX)
1434                         return ICON_OUTLINER_DATA_MESH;
1435                 else if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ)
1436                         return ICON_FILE_IMAGE;
1437         }
1438
1439         /* get icon from ID */
1440         if (id) {
1441                 int icon = ui_id_icon_get(C, id, big);
1442
1443                 return icon ? icon : rnaicon;
1444         }
1445
1446         return rnaicon;
1447 }
1448
1449 int UI_idcode_icon_get(const int idcode)
1450 {
1451         switch (idcode) {
1452                 case ID_AC:
1453                         return ICON_ANIM_DATA;
1454                 case ID_AR:
1455                         return ICON_ARMATURE_DATA;
1456                 case ID_BR:
1457                         return ICON_BRUSH_DATA;
1458                 case ID_CA:
1459                         return ICON_CAMERA_DATA;
1460                 case ID_CU:
1461                         return ICON_CURVE_DATA;
1462                 case ID_GD:
1463                         return ICON_GREASEPENCIL;
1464                 case ID_GR:
1465                         return ICON_GROUP;
1466                 case ID_IM:
1467                         return ICON_IMAGE_DATA;
1468                 case ID_LA:
1469                         return ICON_LAMP_DATA;
1470                 case ID_LS:
1471                         return ICON_LINE_DATA;
1472                 case ID_LT:
1473                         return ICON_LATTICE_DATA;
1474                 case ID_MA:
1475                         return ICON_MATERIAL_DATA;
1476                 case ID_MB:
1477                         return ICON_META_DATA;
1478                 case ID_MC:
1479                         return ICON_CLIP;
1480                 case ID_ME:
1481                         return ICON_MESH_DATA;
1482                 case ID_MSK:
1483                         return ICON_MOD_MASK;  /* TODO! this would need its own icon! */
1484                 case ID_NT:
1485                         return ICON_NODETREE;
1486                 case ID_OB:
1487                         return ICON_OBJECT_DATA;
1488                 case ID_PAL:
1489                         return ICON_COLOR;  /* TODO! this would need its own icon! */
1490                 case ID_PC:
1491                         return ICON_CURVE_BEZCURVE;  /* TODO! this would need its own icon! */
1492                 case ID_SCE:
1493                         return ICON_SCENE_DATA;
1494                 case ID_SPK:
1495                         return ICON_SPEAKER;
1496                 case ID_SO:
1497                         return ICON_SOUND;
1498                 case ID_TE:
1499                         return ICON_TEXTURE_DATA;
1500                 case ID_TXT:
1501                         return ICON_TEXT;
1502                 case ID_VF:
1503                         return ICON_FONT_DATA;
1504                 case ID_WO:
1505                         return ICON_WORLD_DATA;
1506                 default:
1507                         return ICON_NONE;
1508         }
1509 }
1510
1511 static void icon_draw_at_size(
1512         float x, float y, int icon_id, float aspect, float alpha,
1513         enum eIconSizes size, const bool nocreate)
1514 {
1515         int draw_size = get_draw_size(size);
1516         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, false);
1517 }
1518
1519 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1520 {
1521         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
1522 }
1523
1524 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, const float rgb[3])
1525 {
1526         int draw_size = get_draw_size(ICON_SIZE_ICON);
1527         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, false, false);
1528 }
1529
1530 /* draws icon with dpi scale factor */
1531 void UI_icon_draw(float x, float y, int icon_id)
1532 {
1533         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, 1.0f);
1534 }
1535
1536 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1537 {
1538         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, true, false);
1539 }
1540
1541 void UI_icon_draw_preview(float x, float y, int icon_id)
1542 {
1543         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
1544 }
1545
1546 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1547 {
1548         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
1549 }
1550
1551 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, float alpha, int size)
1552 {
1553         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, ICON_SIZE_PREVIEW, size, false, true);
1554 }
1555