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