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