style cleanup, brackets in else/if, some indentation.
[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 #include "MEM_guardedalloc.h"
43
44 #include "GPU_extensions.h"
45
46 #include "BLI_math.h"
47 #include "BLI_blenlib.h"
48 #include "BLI_utildefines.h"
49
50 #include "DNA_brush_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_screen_types.h"
53 #include "DNA_space_types.h"
54
55 #include "RNA_access.h"
56 #include "RNA_enum_types.h"
57
58 #include "BKE_context.h"
59 #include "BKE_global.h"
60 #include "BKE_icons.h"
61 #include "BKE_utildefines.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        5
85 #define ICON_GRID_W             16
86 #define ICON_GRID_H             16
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(.9f, .9f, .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         bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_ ##name## _png,       \
467                                          datatoc_ ##name## _png_size, IB_rect, "<brush icon>");    \
468         def_internal_icon(bbuf, icon_id, 0, 0, w, ICON_TYPE_BUFFER);               \
469         IMB_freeImBuf(bbuf);
470         // end INIT_BRUSH_ICON
471
472         ImBuf *bbuf;
473         const int w = 96;
474
475         INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
476         INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
477         INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
478         INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
479         INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
480         INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
481         INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
482         INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
483         INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
484         INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
485         INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
486         INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
487         INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
488         INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
489         INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
490         INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
491         INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
492         INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
493         INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
494         INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
495         INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
496         INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
497         INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
498         INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
499         INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
500         INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
501         INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
502         INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
503
504 #undef INIT_BRUSH_ICON
505 }
506
507 static void init_internal_icons(void)
508 {
509         bTheme *btheme= UI_GetTheme();
510         ImBuf *bbuf= NULL;
511         int x, y, icontype;
512         char iconfilestr[FILE_MAX];
513         
514         if ((btheme!=NULL) && btheme->tui.iconfile[0]) {
515                 char *icondir= BLI_get_folder(BLENDER_DATAFILES, "icons");
516                 if (icondir) {
517                         BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
518                         bbuf = IMB_loadiffname(iconfilestr, IB_rect); /* if the image is missing bbuf will just be NULL */
519                         if(bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
520                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
521                                 IMB_freeImBuf(bbuf);
522                                 bbuf= NULL;
523                         }
524                 }
525                 else {
526                         printf("%s: 'icons' data path not found, continuing\n", __func__);
527                 }
528         }
529         if(bbuf==NULL)
530                 bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_blender_icons_png, datatoc_blender_icons_png_size, IB_rect, "<blender icons>");
531
532         if(bbuf) {
533                 /* free existing texture if any */
534                 if(icongltex.id) {
535                         glDeleteTextures(1, &icongltex.id);
536                         icongltex.id= 0;
537                 }
538
539                 /* we only use a texture for cards with non-power of two */
540                 if(GPU_non_power_of_two_support()) {
541                         glGenTextures(1, &icongltex.id);
542
543                         if(icongltex.id) {
544                                 icongltex.w = bbuf->x;
545                                 icongltex.h = bbuf->y;
546                                 icongltex.invw = 1.0f/bbuf->x;
547                                 icongltex.invh = 1.0f/bbuf->y;
548
549                                 glBindTexture(GL_TEXTURE_2D, icongltex.id);
550                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bbuf->x, bbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, bbuf->rect);
551                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
552                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
553                                 glBindTexture(GL_TEXTURE_2D, 0);
554
555                                 if(glGetError() == GL_OUT_OF_MEMORY) {
556                                         glDeleteTextures(1, &icongltex.id);
557                                         icongltex.id= 0;
558                                 }
559                         }
560                 }
561         }
562
563         if(icongltex.id)
564                 icontype= ICON_TYPE_TEXTURE;
565         else
566                 icontype= ICON_TYPE_BUFFER;
567         
568         if(bbuf) {
569                 for (y=0; y<ICON_GRID_ROWS; y++) {
570                         for (x=0; x<ICON_GRID_COLS; x++) {
571                                 def_internal_icon(bbuf, BIFICONID_FIRST + y*ICON_GRID_COLS + x,
572                                         x*(ICON_GRID_W+ICON_GRID_MARGIN)+ICON_GRID_MARGIN,
573                                         y*(ICON_GRID_H+ICON_GRID_MARGIN)+ICON_GRID_MARGIN, ICON_GRID_W,
574                                         icontype);
575                         }
576                 }
577         }
578
579         def_internal_vicon(VICO_VIEW3D_VEC, vicon_view3d_draw);
580         def_internal_vicon(VICO_EDIT_VEC, vicon_edit_draw);
581         def_internal_vicon(VICO_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
582         def_internal_vicon(VICO_EDITMODE_HLT, vicon_editmode_hlt_draw);
583         def_internal_vicon(VICO_DISCLOSURE_TRI_RIGHT_VEC, vicon_disclosure_tri_right_draw);
584         def_internal_vicon(VICO_DISCLOSURE_TRI_DOWN_VEC, vicon_disclosure_tri_down_draw);
585         def_internal_vicon(VICO_MOVE_UP_VEC, vicon_move_up_draw);
586         def_internal_vicon(VICO_MOVE_DOWN_VEC, vicon_move_down_draw);
587         def_internal_vicon(VICO_X_VEC, vicon_x_draw);
588         def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
589
590         IMB_freeImBuf(bbuf);
591 }
592 #endif // WITH_HEADLESS
593
594 static void init_iconfile_list(struct ListBase *list)
595 {
596         IconFile *ifile;
597         struct direntry *dir;
598         int restoredir = 1; /* restore to current directory */
599         int totfile, i, index=1;
600         const char *icondir;
601         char olddir[FILE_MAX];
602
603         list->first = list->last = NULL;
604         icondir = BLI_get_folder(BLENDER_DATAFILES, "icons");
605
606         if(icondir==NULL)
607                 return;
608         
609         /* since BLI_dir_contents changes the current working directory, restore it 
610          * back to old value afterwards */
611         if(!BLI_current_working_dir(olddir, sizeof(olddir))) 
612                 restoredir = 0;
613         totfile = BLI_dir_contents(icondir, &dir);
614         if (restoredir && !chdir(olddir)) {} /* fix warning about checking return value */
615
616         for(i=0; i<totfile; i++) {
617                 if( (dir[i].type & S_IFREG) ) {
618                         char *filename = dir[i].relname;
619                         
620                         if(BLI_testextensie(filename, ".png")) {
621                                 /* loading all icons on file start is overkill & slows startup
622                                  * its possible they change size after blender load anyway. */
623 #if 0
624                                 int ifilex, ifiley;
625                                 char iconfilestr[FILE_MAX+16]; /* allow 256 chars for file+dir */
626                                 ImBuf *bbuf= NULL;
627                                 /* check to see if the image is the right size, continue if not */
628                                 /* copying strings here should go ok, assuming that we never get back
629                                  * a complete path to file longer than 256 chars */
630                                 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
631                                 bbuf= IMB_loadiffname(iconfilestr, IB_rect);
632
633                                 if(bbuf) {
634                                         ifilex = bbuf->x;
635                                         ifiley = bbuf->y;
636                                         IMB_freeImBuf(bbuf);
637                                 }
638                                 else {
639                                         ifilex= ifiley= 0;
640                                 }
641                                 
642                                 /* bad size or failed to load */
643                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
644                                         printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
645                                         continue;
646                                 }
647 #endif                  /* removed */
648
649                                 /* found a potential icon file, so make an entry for it in the cache list */
650                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
651                                 
652                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
653                                 ifile->index = index;
654
655                                 BLI_addtail(list, ifile);
656                                 
657                                 index++;
658                         }
659                 }
660         }
661         
662         /* free temporary direntry structure that's been created by BLI_dir_contents() */
663         i= totfile-1;
664         
665         for (; i>=0; i--) {
666                 MEM_freeN(dir[i].relname);
667                 MEM_freeN(dir[i].path);
668                 if (dir[i].string) {
669                         MEM_freeN(dir[i].string);
670                 }
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         bTheme *btheme= UI_GetTheme();
956         Icon *icon = NULL;
957         DrawInfo *di = NULL;
958         IconImage *iimg;
959         float fdraw_size= is_preview ? draw_size : (draw_size * UI_DPI_ICON_FAC);
960         int w, h;
961         
962         icon = BKE_icon_get(icon_id);
963         alpha *= btheme->tui.icon_alpha;
964         
965         if (icon==NULL) {
966                 if (G.f & G_DEBUG)
967                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
968                 return;
969         }
970
971         di = (DrawInfo*)icon->drawinfo;
972         
973         if (!di) {
974                 di = icon_create_drawinfo();
975         
976                 icon->drawinfo = di;            
977                 icon->drawinfo_free = UI_icons_free_drawinfo;           
978         }
979         
980         /* scale width and height according to aspect */
981         w = (int)(fdraw_size/aspect + 0.5f);
982         h = (int)(fdraw_size/aspect + 0.5f);
983         
984         if(di->type == ICON_TYPE_VECTOR) {
985                 /* vector icons use the uiBlock transformation, they are not drawn
986                  * with untransformed coordinates like the other icons */
987                 di->data.vector.func((int)x, (int)y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f); 
988         } 
989         else if(di->type == ICON_TYPE_TEXTURE) {
990                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
991                         di->data.texture.w, di->data.texture.h, alpha, rgb);
992         }
993         else if(di->type == ICON_TYPE_BUFFER) {
994                 /* it is a builtin icon */              
995                 iimg= di->data.buffer.image;
996
997                 if(!iimg->rect) return; /* something has gone wrong! */
998
999                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
1000         }
1001         else if(di->type == ICON_TYPE_PREVIEW) {
1002                 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj); 
1003
1004                 if(pi) {                        
1005                         /* no create icon on this level in code */
1006                         if(!pi->rect[size]) return; /* something has gone wrong! */
1007                         
1008                         /* preview images use premul alpha ... */
1009                         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1010                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], 1.0f, NULL, is_preview);
1011                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1012                 }
1013         }
1014 }
1015
1016 static void ui_id_preview_image_render_size(bContext *C, ID *id, PreviewImage *pi, int size)
1017 {
1018         if ((pi->changed[size] ||!pi->rect[size])) { /* changed only ever set by dynamic icons */
1019                 /* create the rect if necessary */
1020                 icon_set_image(C, id, pi, size);
1021
1022                 pi->changed[size] = 0;
1023         }
1024 }
1025
1026 static void ui_id_icon_render(bContext *C, ID *id, int big)
1027 {
1028         PreviewImage *pi = BKE_previewimg_get(id);
1029
1030         if (pi) {
1031                 if (big)
1032                         ui_id_preview_image_render_size(C, id, pi, ICON_SIZE_PREVIEW);  /* bigger preview size */
1033                 else
1034                         ui_id_preview_image_render_size(C, id, pi, ICON_SIZE_ICON);             /* icon size */
1035         }
1036 }
1037
1038 static void ui_id_brush_render(bContext *C, ID *id)
1039 {
1040         PreviewImage *pi = BKE_previewimg_get(id); 
1041         enum eIconSizes i;
1042         
1043         if(!pi)
1044                 return;
1045         
1046         for(i = 0; i < NUM_ICON_SIZES; i++) {
1047                 /* check if rect needs to be created; changed
1048                  * only set by dynamic icons */
1049                 if((pi->changed[i] || !pi->rect[i])) {
1050                         icon_set_image(C, id, pi, i);
1051                         pi->changed[i] = 0;
1052                 }
1053         }
1054 }
1055
1056
1057 static int ui_id_brush_get_icon(bContext *C, ID *id)
1058 {
1059         Brush *br = (Brush*)id;
1060
1061         if(br->flag & BRUSH_CUSTOM_ICON) {
1062                 BKE_icon_getid(id);
1063                 ui_id_brush_render(C, id);
1064         }
1065         else {
1066                 Object *ob = CTX_data_active_object(C);
1067                 SpaceImage *sima;
1068                 EnumPropertyItem *items = NULL;
1069                 int tool, mode = 0;
1070
1071                 /* XXX: this is not nice, should probably make brushes
1072                  * be strictly in one paint mode only to avoid
1073                  * checking various context stuff here */
1074
1075                 if(CTX_wm_view3d(C) && ob) {
1076                         if(ob->mode & OB_MODE_SCULPT)
1077                                 mode = OB_MODE_SCULPT;
1078                         else if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT))
1079                                 mode = OB_MODE_VERTEX_PAINT;
1080                         else if(ob->mode & OB_MODE_TEXTURE_PAINT)
1081                                 mode = OB_MODE_TEXTURE_PAINT;
1082                 }
1083                 else if((sima = CTX_wm_space_image(C)) &&
1084                         (sima->flag & SI_DRAWTOOL)) {
1085                         mode = OB_MODE_TEXTURE_PAINT;
1086                 }
1087
1088                 /* reset the icon */
1089                 if(mode == OB_MODE_SCULPT) {
1090                         items = brush_sculpt_tool_items;
1091                         tool = br->sculpt_tool;
1092                 }
1093                 else if(mode == OB_MODE_VERTEX_PAINT) {
1094                         items = brush_vertex_tool_items;
1095                         tool = br->vertexpaint_tool;
1096                 }
1097                 else if(mode == OB_MODE_TEXTURE_PAINT) {
1098                         items = brush_image_tool_items;
1099                         tool = br->imagepaint_tool;
1100                 }
1101
1102                 if(!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1103                         id->icon_id = 0;
1104         }
1105
1106         return id->icon_id;
1107 }
1108
1109 int ui_id_icon_get(bContext *C, ID *id, int big)
1110 {
1111         int iconid= 0;
1112         
1113         /* icon */
1114         switch(GS(id->name)) {
1115                 case ID_BR:
1116                         iconid= ui_id_brush_get_icon(C, id);
1117                         break;
1118                 case ID_MA: /* fall through */
1119                 case ID_TE: /* fall through */
1120                 case ID_IM: /* fall through */
1121                 case ID_WO: /* fall through */
1122                 case ID_LA: /* fall through */
1123                         iconid= BKE_icon_getid(id);
1124                         /* checks if not exists, or changed */
1125                         ui_id_icon_render(C, id, big);
1126                         break;
1127                 default:
1128                         break;
1129         }
1130
1131         return iconid;
1132 }
1133
1134 static void icon_draw_at_size(float x, float y, int icon_id, float aspect, float alpha, enum eIconSizes size, int nocreate)
1135 {
1136         int draw_size = get_draw_size(size);
1137         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, FALSE);
1138 }
1139
1140 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1141 {
1142         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
1143 }
1144
1145 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, float *rgb)
1146 {
1147         int draw_size = get_draw_size(ICON_SIZE_ICON);
1148         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, FALSE, FALSE);
1149 }
1150
1151 void UI_icon_draw(float x, float y, int icon_id)
1152 {
1153         UI_icon_draw_aspect(x, y, icon_id, 1.0f, 1.0f);
1154 }
1155
1156 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1157 {
1158         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, TRUE, FALSE);
1159 }
1160
1161 void UI_icon_draw_preview(float x, float y, int icon_id)
1162 {
1163         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
1164 }
1165
1166 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1167 {
1168         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
1169 }
1170
1171 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1172 {
1173         icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, ICON_SIZE_PREVIEW, size, FALSE, TRUE);
1174 }
1175