1369f399c898433836e5cf7da59b606966067fc3
[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 *bbuf = NULL;
516         int x, y, icontype;
517         char iconfilestr[FILE_MAX];
518         
519         if ((btheme != NULL) && btheme->tui.iconfile[0]) {
520                 char *icondir = BLI_get_folder(BLENDER_DATAFILES, "icons");
521                 if (icondir) {
522                         BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
523                         bbuf = IMB_loadiffname(iconfilestr, IB_rect, NULL); /* if the image is missing bbuf will just be NULL */
524                         if (bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
525                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
526                                 IMB_freeImBuf(bbuf);
527                                 bbuf = NULL;
528                         }
529                 }
530                 else {
531                         printf("%s: 'icons' data path not found, continuing\n", __func__);
532                 }
533         }
534         if (bbuf == NULL)
535                 bbuf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons_png,
536                                              datatoc_blender_icons_png_size, IB_rect, NULL, "<blender icons>");
537
538         if (bbuf) {
539                 /* free existing texture if any */
540                 if (icongltex.id) {
541                         glDeleteTextures(1, &icongltex.id);
542                         icongltex.id = 0;
543                 }
544
545                 /* we only use a texture for cards with non-power of two */
546                 if (GPU_non_power_of_two_support()) {
547                         glGenTextures(1, &icongltex.id);
548
549                         if (icongltex.id) {
550                                 icongltex.w = bbuf->x;
551                                 icongltex.h = bbuf->y;
552                                 icongltex.invw = 1.0f / bbuf->x;
553                                 icongltex.invh = 1.0f / bbuf->y;
554
555                                 glBindTexture(GL_TEXTURE_2D, icongltex.id);
556                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bbuf->x, bbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, bbuf->rect);
557                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
558                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
559                                 glBindTexture(GL_TEXTURE_2D, 0);
560
561                                 if (glGetError() == GL_OUT_OF_MEMORY) {
562                                         glDeleteTextures(1, &icongltex.id);
563                                         icongltex.id = 0;
564                                 }
565                         }
566                 }
567         }
568
569         if (icongltex.id)
570                 icontype = ICON_TYPE_TEXTURE;
571         else
572                 icontype = ICON_TYPE_BUFFER;
573         
574         if (bbuf) {
575                 for (y = 0; y < ICON_GRID_ROWS; y++) {
576                         for (x = 0; x < ICON_GRID_COLS; x++) {
577                                 def_internal_icon(bbuf, BIFICONID_FIRST + y * ICON_GRID_COLS + x,
578                                                   x * (ICON_GRID_W + ICON_GRID_MARGIN) + ICON_GRID_MARGIN,
579                                                   y * (ICON_GRID_H + ICON_GRID_MARGIN) + ICON_GRID_MARGIN, ICON_GRID_W,
580                                                   icontype);
581                         }
582                 }
583         }
584
585         def_internal_vicon(VICO_VIEW3D_VEC, vicon_view3d_draw);
586         def_internal_vicon(VICO_EDIT_VEC, vicon_edit_draw);
587         def_internal_vicon(VICO_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
588         def_internal_vicon(VICO_EDITMODE_HLT, vicon_editmode_hlt_draw);
589         def_internal_vicon(VICO_DISCLOSURE_TRI_RIGHT_VEC, vicon_disclosure_tri_right_draw);
590         def_internal_vicon(VICO_DISCLOSURE_TRI_DOWN_VEC, vicon_disclosure_tri_down_draw);
591         def_internal_vicon(VICO_MOVE_UP_VEC, vicon_move_up_draw);
592         def_internal_vicon(VICO_MOVE_DOWN_VEC, vicon_move_down_draw);
593         def_internal_vicon(VICO_X_VEC, vicon_x_draw);
594         def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
595
596         IMB_freeImBuf(bbuf);
597 }
598 #endif  /* WITH_HEADLESS */
599
600 static void init_iconfile_list(struct ListBase *list)
601 {
602         IconFile *ifile;
603         struct direntry *dir;
604         int restoredir = 1; /* restore to current directory */
605         int totfile, i, index = 1;
606         const char *icondir;
607         char olddir[FILE_MAX];
608
609         list->first = list->last = NULL;
610         icondir = BLI_get_folder(BLENDER_DATAFILES, "icons");
611
612         if (icondir == NULL)
613                 return;
614         
615         /* since BLI_dir_contents changes the current working directory, restore it 
616          * back to old value afterwards */
617         if (!BLI_current_working_dir(olddir, sizeof(olddir))) 
618                 restoredir = 0;
619         totfile = BLI_dir_contents(icondir, &dir);
620         if (restoredir && !chdir(olddir)) {} /* fix warning about checking return value */
621
622         for (i = 0; i < totfile; i++) {
623                 if ((dir[i].type & S_IFREG)) {
624                         char *filename = dir[i].relname;
625                         
626                         if (BLI_testextensie(filename, ".png")) {
627                                 /* loading all icons on file start is overkill & slows startup
628                                  * its possible they change size after blender load anyway. */
629 #if 0
630                                 int ifilex, ifiley;
631                                 char iconfilestr[FILE_MAX + 16]; /* allow 256 chars for file+dir */
632                                 ImBuf *bbuf = NULL;
633                                 /* check to see if the image is the right size, continue if not */
634                                 /* copying strings here should go ok, assuming that we never get back
635                                  * a complete path to file longer than 256 chars */
636                                 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
637                                 bbuf = IMB_loadiffname(iconfilestr, IB_rect);
638
639                                 if (bbuf) {
640                                         ifilex = bbuf->x;
641                                         ifiley = bbuf->y;
642                                         IMB_freeImBuf(bbuf);
643                                 }
644                                 else {
645                                         ifilex = ifiley = 0;
646                                 }
647                                 
648                                 /* bad size or failed to load */
649                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
650                                         printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
651                                         continue;
652                                 }
653 #endif          /* removed */
654
655                                 /* found a potential icon file, so make an entry for it in the cache list */
656                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
657                                 
658                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
659                                 ifile->index = index;
660
661                                 BLI_addtail(list, ifile);
662                                 
663                                 index++;
664                         }
665                 }
666         }
667         
668         /* free temporary direntry structure that's been created by BLI_dir_contents() */
669         i = totfile - 1;
670         
671         for (; i >= 0; i--) {
672                 MEM_freeN(dir[i].relname);
673                 MEM_freeN(dir[i].path);
674                 if (dir[i].string) {
675                         MEM_freeN(dir[i].string);
676                 }
677         }
678         free(dir);
679         dir = NULL;
680 }
681
682 static void free_iconfile_list(struct ListBase *list)
683 {
684         IconFile *ifile = NULL, *next_ifile = NULL;
685         
686         for (ifile = list->first; ifile; ifile = next_ifile) {
687                 next_ifile = ifile->next;
688                 BLI_freelinkN(list, ifile);
689         }
690 }
691
692 int UI_iconfile_get_index(const char *filename)
693 {
694         IconFile *ifile;
695         ListBase *list = &(iconfilelist);
696         
697         for (ifile = list->first; ifile; ifile = ifile->next) {
698                 if (BLI_path_cmp(filename, ifile->filename) == 0) {
699                         return ifile->index;
700                 }
701         }
702         
703         return 0;
704 }
705
706 ListBase *UI_iconfile_list(void)
707 {
708         ListBase *list = &(iconfilelist);
709         
710         return list;
711 }
712
713
714 void UI_icons_free(void)
715 {
716 #ifndef WITH_HEADLESS
717         if (icongltex.id) {
718                 glDeleteTextures(1, &icongltex.id);
719                 icongltex.id = 0;
720         }
721
722         free_iconfile_list(&iconfilelist);
723         BKE_icons_free();
724 #endif
725 }
726
727 void UI_icons_free_drawinfo(void *drawinfo)
728 {
729         DrawInfo *di = drawinfo;
730
731         if (di) {
732                 if (di->type == ICON_TYPE_BUFFER) {
733                         if (di->data.buffer.image) {
734                                 MEM_freeN(di->data.buffer.image->rect);
735                                 MEM_freeN(di->data.buffer.image);
736                         }
737                 }
738
739                 MEM_freeN(di);
740         }
741 }
742
743 static DrawInfo *icon_create_drawinfo(void)
744 {
745         DrawInfo *di = NULL;
746
747         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
748         di->type = ICON_TYPE_PREVIEW;
749
750         return di;
751 }
752
753 /* note!, returns unscaled by DPI */
754 int UI_icon_get_width(int icon_id)
755 {
756         Icon *icon = NULL;
757         DrawInfo *di = NULL;
758
759         icon = BKE_icon_get(icon_id);
760         
761         if (icon == NULL) {
762                 if (G.debug & G_DEBUG)
763                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
764                 return 0;
765         }
766         
767         di = (DrawInfo *)icon->drawinfo;
768         if (!di) {
769                 di = icon_create_drawinfo();
770                 icon->drawinfo = di;
771         }
772
773         if (di)
774                 return ICON_DEFAULT_WIDTH;
775
776         return 0;
777 }
778
779 int UI_icon_get_height(int icon_id)
780 {
781         Icon *icon = NULL;
782         DrawInfo *di = NULL;
783
784         icon = BKE_icon_get(icon_id);
785         
786         if (icon == NULL) {
787                 if (G.debug & G_DEBUG)
788                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
789                 return 0;
790         }
791         
792         di = (DrawInfo *)icon->drawinfo;
793
794         if (!di) {
795                 di = icon_create_drawinfo();
796                 icon->drawinfo = di;
797         }
798         
799         if (di)
800                 return ICON_DEFAULT_HEIGHT;
801
802         return 0;
803 }
804
805 void UI_icons_init(int first_dyn_id)
806 {
807 #ifdef WITH_HEADLESS
808         (void)first_dyn_id;
809 #else
810         init_iconfile_list(&iconfilelist);
811         BKE_icons_init(first_dyn_id);
812         init_internal_icons();
813         init_brush_icons();
814 #endif
815 }
816
817 /* Render size for preview images and icons
818  */
819 static int preview_render_size(enum eIconSizes size)
820 {
821         switch (size) {
822                 case ICON_SIZE_ICON:    return 32;
823                 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
824         }
825         return 0;
826 }
827
828 /* Create rect for the icon
829  */
830 static void icon_create_rect(struct PreviewImage *prv_img, enum eIconSizes size)
831 {
832         unsigned int render_size = preview_render_size(size);
833
834         if (!prv_img) {
835                 if (G.debug & G_DEBUG)
836                         printf("%s, error: requested preview image does not exist", __func__);
837         }
838         else if (!prv_img->rect[size]) {
839                 prv_img->w[size] = render_size;
840                 prv_img->h[size] = render_size;
841                 prv_img->changed[size] = 1;
842                 prv_img->changed_timestamp[size] = 0;
843                 prv_img->rect[size] = MEM_callocN(render_size * render_size * sizeof(unsigned int), "prv_rect");
844         }
845 }
846
847 /* only called when icon has changed */
848 /* only call with valid pointer from UI_icon_draw */
849 static void icon_set_image(bContext *C, ID *id, PreviewImage *prv_img, enum eIconSizes size)
850 {
851         if (!prv_img) {
852                 if (G.debug & G_DEBUG)
853                         printf("%s: no preview image for this ID: %s\n", __func__, id->name);
854                 return;
855         }
856
857         icon_create_rect(prv_img, size);
858
859         ED_preview_icon_job(C, prv_img, id, prv_img->rect[size],
860                             prv_img->w[size], prv_img->h[size]);
861 }
862
863 static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect), int rw, int rh,
864                            unsigned int *rect, float alpha, const float rgb[3], short is_preview)
865 {
866         ImBuf *ima = NULL;
867
868         /* sanity check */
869         if (w <= 0 || h <= 0 || w > 2000 || h > 2000) {
870                 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
871                 BLI_assert(!"invalid icon size");
872                 return;
873         }
874
875         /* modulate color */
876         if (alpha != 1.0f)
877                 glPixelTransferf(GL_ALPHA_SCALE, alpha);
878
879         if (rgb) {
880                 glPixelTransferf(GL_RED_SCALE, rgb[0]);
881                 glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
882                 glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
883         }
884
885         /* rect contains image in 'rendersize', we only scale if needed */
886         if (rw != w && rh != h) {
887                 /* first allocate imbuf for scaling and copy preview into it */
888                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
889                 memcpy(ima->rect, rect, rw * rh * sizeof(unsigned int));
890                 IMB_scalefastImBuf(ima, w, h); /* scale it */
891                 rect = ima->rect;
892         }
893
894         /* draw */
895         if (is_preview) {
896                 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
897         }
898         else {
899                 glRasterPos2f(x, y);
900                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
901         }
902
903         if (ima)
904                 IMB_freeImBuf(ima);
905
906         /* restore color */
907         if (alpha != 0.0f)
908                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
909         
910         if (rgb) {
911                 glPixelTransferf(GL_RED_SCALE, 1.0f);
912                 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
913                 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
914         }
915 }
916
917 static void icon_draw_texture(float x, float y, float w, float h, int ix, int iy,
918                               int UNUSED(iw), int ih, float alpha, const float rgb[3])
919 {
920         float x1, x2, y1, y2;
921
922         if (rgb) glColor4f(rgb[0], rgb[1], rgb[2], alpha);
923         else glColor4f(1.0f, 1.0f, 1.0f, alpha);
924
925         x1 = ix * icongltex.invw;
926         x2 = (ix + ih) * icongltex.invw;
927         y1 = iy * icongltex.invh;
928         y2 = (iy + ih) * icongltex.invh;
929
930         glEnable(GL_TEXTURE_2D);
931         glBindTexture(GL_TEXTURE_2D, icongltex.id);
932
933         glBegin(GL_QUADS);
934         glTexCoord2f(x1, y1);
935         glVertex2f(x, y);
936
937         glTexCoord2f(x2, y1);
938         glVertex2f(x + w, y);
939
940         glTexCoord2f(x2, y2);
941         glVertex2f(x + w, y + h);
942
943         glTexCoord2f(x1, y2);
944         glVertex2f(x, y + h);
945         glEnd();
946
947         glBindTexture(GL_TEXTURE_2D, 0);
948         glDisable(GL_TEXTURE_2D);
949 }
950
951 /* Drawing size for preview images */
952 static int get_draw_size(enum eIconSizes size)
953 {
954         switch (size) {
955                 case ICON_SIZE_ICON: return ICON_DEFAULT_HEIGHT;
956                 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
957         }
958         return 0;
959 }
960
961 static void icon_draw_size(float x, float y, int icon_id, float aspect, float alpha, const float rgb[3],
962                            enum eIconSizes size, int draw_size, int UNUSED(nocreate), short is_preview)
963 {
964         bTheme *btheme = UI_GetTheme();
965         Icon *icon = NULL;
966         DrawInfo *di = NULL;
967         IconImage *iimg;
968         float fdraw_size = draw_size;
969         int w, h;
970         
971         icon = BKE_icon_get(icon_id);
972         alpha *= btheme->tui.icon_alpha;
973         
974         if (icon == NULL) {
975                 if (G.debug & G_DEBUG)
976                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
977                 return;
978         }
979
980         di = (DrawInfo *)icon->drawinfo;
981         
982         if (!di) {
983                 di = icon_create_drawinfo();
984         
985                 icon->drawinfo = di;
986                 icon->drawinfo_free = UI_icons_free_drawinfo;
987         }
988         
989         /* scale width and height according to aspect */
990         w = (int)(fdraw_size / aspect + 0.5f);
991         h = (int)(fdraw_size / aspect + 0.5f);
992         
993         if (di->type == ICON_TYPE_VECTOR) {
994                 /* vector icons use the uiBlock transformation, they are not drawn
995                  * with untransformed coordinates like the other icons */
996                 di->data.vector.func((int)x, (int)y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f); 
997         }
998         else if (di->type == ICON_TYPE_TEXTURE) {
999                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
1000                                   di->data.texture.w, di->data.texture.h, alpha, rgb);
1001         }
1002         else if (di->type == ICON_TYPE_BUFFER) {
1003                 /* it is a builtin icon */
1004                 iimg = di->data.buffer.image;
1005
1006                 if (!iimg->rect) return;  /* something has gone wrong! */
1007
1008                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
1009         }
1010         else if (di->type == ICON_TYPE_PREVIEW) {
1011                 PreviewImage *pi = BKE_previewimg_get((ID *)icon->obj);
1012
1013                 if (pi) {
1014                         /* no create icon on this level in code */
1015                         if (!pi->rect[size]) return;  /* something has gone wrong! */
1016                         
1017                         /* preview images use premul alpha ... */
1018                         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1019                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], 1.0f, NULL, is_preview);
1020                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1021                 }
1022         }
1023 }
1024
1025 static void ui_id_preview_image_render_size(bContext *C, ID *id, PreviewImage *pi, int size)
1026 {
1027         if ((pi->changed[size] || !pi->rect[size])) { /* changed only ever set by dynamic icons */
1028                 /* create the rect if necessary */
1029                 icon_set_image(C, id, pi, size);
1030
1031                 pi->changed[size] = 0;
1032         }
1033 }
1034
1035 static void ui_id_icon_render(bContext *C, ID *id, int big)
1036 {
1037         PreviewImage *pi = BKE_previewimg_get(id);
1038
1039         if (pi) {
1040                 if (big)
1041                         ui_id_preview_image_render_size(C, id, pi, ICON_SIZE_PREVIEW);  /* bigger preview size */
1042                 else
1043                         ui_id_preview_image_render_size(C, id, pi, ICON_SIZE_ICON);     /* icon size */
1044         }
1045 }
1046
1047 static void ui_id_brush_render(bContext *C, ID *id)
1048 {
1049         PreviewImage *pi = BKE_previewimg_get(id); 
1050         enum eIconSizes i;
1051         
1052         if (!pi)
1053                 return;
1054         
1055         for (i = 0; i < NUM_ICON_SIZES; i++) {
1056                 /* check if rect needs to be created; changed
1057                  * only set by dynamic icons */
1058                 if ((pi->changed[i] || !pi->rect[i])) {
1059                         icon_set_image(C, id, pi, i);
1060                         pi->changed[i] = 0;
1061                 }
1062         }
1063 }
1064
1065
1066 static int ui_id_brush_get_icon(bContext *C, ID *id)
1067 {
1068         Brush *br = (Brush *)id;
1069
1070         if (br->flag & BRUSH_CUSTOM_ICON) {
1071                 BKE_icon_getid(id);
1072                 ui_id_brush_render(C, id);
1073         }
1074         else {
1075                 Object *ob = CTX_data_active_object(C);
1076                 SpaceImage *sima;
1077                 EnumPropertyItem *items = NULL;
1078                 int tool, mode = 0;
1079
1080                 /* XXX: this is not nice, should probably make brushes
1081                  * be strictly in one paint mode only to avoid
1082                  * checking various context stuff here */
1083
1084                 if (CTX_wm_view3d(C) && ob) {
1085                         if (ob->mode & OB_MODE_SCULPT)
1086                                 mode = OB_MODE_SCULPT;
1087                         else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))
1088                                 mode = OB_MODE_VERTEX_PAINT;
1089                         else if (ob->mode & OB_MODE_TEXTURE_PAINT)
1090                                 mode = OB_MODE_TEXTURE_PAINT;
1091                 }
1092                 else if ((sima = CTX_wm_space_image(C)) &&
1093                          (sima->mode == SI_MODE_PAINT))
1094                 {
1095                         mode = OB_MODE_TEXTURE_PAINT;
1096                 }
1097
1098                 /* reset the icon */
1099                 if (mode == OB_MODE_SCULPT) {
1100                         items = brush_sculpt_tool_items;
1101                         tool = br->sculpt_tool;
1102                 }
1103                 else if (mode == OB_MODE_VERTEX_PAINT) {
1104                         items = brush_vertex_tool_items;
1105                         tool = br->vertexpaint_tool;
1106                 }
1107                 else if (mode == OB_MODE_TEXTURE_PAINT) {
1108                         items = brush_image_tool_items;
1109                         tool = br->imagepaint_tool;
1110                 }
1111
1112                 if (!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1113                         id->icon_id = 0;
1114         }
1115
1116         return id->icon_id;
1117 }
1118
1119 int ui_id_icon_get(bContext *C, ID *id, int big)
1120 {
1121         int iconid = 0;
1122         
1123         /* icon */
1124         switch (GS(id->name)) {
1125                 case ID_BR:
1126                         iconid = ui_id_brush_get_icon(C, id);
1127                         break;
1128                 case ID_MA: /* fall through */
1129                 case ID_TE: /* fall through */
1130                 case ID_IM: /* fall through */
1131                 case ID_WO: /* fall through */
1132                 case ID_LA: /* fall through */
1133                         iconid = BKE_icon_getid(id);
1134                         /* checks if not exists, or changed */
1135                         ui_id_icon_render(C, id, big);
1136                         break;
1137                 default:
1138                         break;
1139         }
1140
1141         return iconid;
1142 }
1143
1144 static void icon_draw_at_size(float x, float y, int icon_id, float aspect, float alpha, enum eIconSizes size, int nocreate)
1145 {
1146         int draw_size = get_draw_size(size);
1147         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, FALSE);
1148 }
1149
1150 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1151 {
1152         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
1153 }
1154
1155 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, const float rgb[3])
1156 {
1157         int draw_size = get_draw_size(ICON_SIZE_ICON);
1158         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, FALSE, FALSE);
1159 }
1160
1161 /* draws icon with dpi scale factor */
1162 void UI_icon_draw(float x, float y, int icon_id)
1163 {
1164         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, 1.0f);
1165 }
1166
1167 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1168 {
1169         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, TRUE, FALSE);
1170 }
1171
1172 void UI_icon_draw_preview(float x, float y, int icon_id)
1173 {
1174         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
1175 }
1176
1177 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1178 {
1179         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
1180 }
1181
1182 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1183 {
1184         icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, ICON_SIZE_PREVIEW, size, FALSE, TRUE);
1185 }
1186