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