Cycles: svn merge -r41225:41232 ^/trunk/blender
[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);  \
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= U.themes.first;
510         ImBuf *bbuf= NULL;
511         int x, y, icontype;
512         char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
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_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
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) MEM_freeN(dir[i].string);
669         }
670         free(dir);
671         dir= NULL;
672 }
673
674 static void free_iconfile_list(struct ListBase *list)
675 {
676         IconFile *ifile=NULL, *next_ifile=NULL;
677         
678         for(ifile=list->first; ifile; ifile=next_ifile) {
679                 next_ifile = ifile->next;
680                 BLI_freelinkN(list, ifile);
681         }
682 }
683
684 int UI_iconfile_get_index(const char *filename)
685 {
686         IconFile *ifile;
687         ListBase *list=&(iconfilelist);
688         
689         for(ifile=list->first; ifile; ifile=ifile->next) {
690                 if (BLI_path_cmp(filename, ifile->filename) == 0) {
691                         return ifile->index;
692                 }
693         }
694         
695         return 0;
696 }
697
698 ListBase *UI_iconfile_list(void)
699 {
700         ListBase *list=&(iconfilelist);
701         
702         return list;
703 }
704
705
706 void UI_icons_free(void)
707 {
708 #ifndef WITH_HEADLESS
709         if(icongltex.id) {
710                 glDeleteTextures(1, &icongltex.id);
711                 icongltex.id= 0;
712         }
713
714         free_iconfile_list(&iconfilelist);
715         BKE_icons_free();
716 #endif
717 }
718
719 void UI_icons_free_drawinfo(void *drawinfo)
720 {
721         DrawInfo *di = drawinfo;
722
723         if(di) {
724                 if(di->type == ICON_TYPE_BUFFER) {
725                         if(di->data.buffer.image) {
726                                 MEM_freeN(di->data.buffer.image->rect);
727                                 MEM_freeN(di->data.buffer.image);
728                         }
729                 }
730
731                 MEM_freeN(di);
732         }
733 }
734
735 static DrawInfo *icon_create_drawinfo(void)
736 {
737         DrawInfo *di = NULL;
738
739         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
740         di->type= ICON_TYPE_PREVIEW;
741
742         return di;
743 }
744
745 /* note!, returns unscaled by DPI, may need to multiply result by UI_DPI_ICON_FAC */
746 int UI_icon_get_width(int icon_id)
747 {
748         Icon *icon = NULL;
749         DrawInfo *di = NULL;
750
751         icon = BKE_icon_get(icon_id);
752         
753         if (icon==NULL) {
754                 if (G.f & G_DEBUG)
755                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
756                 return 0;
757         }
758         
759         di = (DrawInfo *)icon->drawinfo;
760         if (!di) {
761                 di = icon_create_drawinfo();
762                 icon->drawinfo = di;
763         }
764
765         if (di)
766                 return ICON_DEFAULT_WIDTH;
767
768         return 0;
769 }
770
771 int UI_icon_get_height(int icon_id)
772 {
773         Icon *icon = NULL;
774         DrawInfo *di = NULL;
775
776         icon = BKE_icon_get(icon_id);
777         
778         if (icon==NULL) {
779                 if (G.f & G_DEBUG)
780                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
781                 return 0;
782         }
783         
784         di = (DrawInfo*)icon->drawinfo;
785
786         if (!di) {
787                 di = icon_create_drawinfo();
788                 icon->drawinfo = di;
789         }
790         
791         if (di)
792                 return ICON_DEFAULT_HEIGHT;
793
794         return 0;
795 }
796
797 void UI_icons_init(int first_dyn_id)
798 {
799 #ifdef WITH_HEADLESS
800         (void)first_dyn_id;
801 #else
802         init_iconfile_list(&iconfilelist);
803         BKE_icons_init(first_dyn_id);
804         init_internal_icons();
805         init_brush_icons();
806 #endif
807 }
808
809 /* Render size for preview images and icons
810  */
811 static int preview_render_size(enum eIconSizes size)
812 {
813         switch (size) {
814                 case ICON_SIZE_ICON:    return 32;
815                 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
816         }
817         return 0;
818 }
819
820 /* Create rect for the icon
821  */
822 static void icon_create_rect(struct PreviewImage* prv_img, enum eIconSizes size) 
823 {
824         unsigned int render_size = preview_render_size(size);
825
826         if (!prv_img) {
827                 if (G.f & G_DEBUG)
828                         printf("%s, error: requested preview image does not exist", __func__);
829         }
830         if (!prv_img->rect[size]) {
831                 prv_img->w[size] = render_size;
832                 prv_img->h[size] = render_size;
833                 prv_img->changed[size] = 1;
834                 prv_img->changed_timestamp[size] = 0;
835                 prv_img->rect[size] = MEM_callocN(render_size*render_size*sizeof(unsigned int), "prv_rect"); 
836         }
837 }
838
839 /* only called when icon has changed */
840 /* only call with valid pointer from UI_icon_draw */
841 static void icon_set_image(bContext *C, ID *id, PreviewImage* prv_img, enum eIconSizes size)
842 {
843         if (!prv_img) {
844                 if (G.f & G_DEBUG)
845                         printf("%s: no preview image for this ID: %s\n", __func__, id->name);
846                 return;
847         }       
848
849         icon_create_rect(prv_img, size);
850
851         ED_preview_icon_job(C, prv_img, id, prv_img->rect[size],
852                 prv_img->w[size], prv_img->h[size]);
853 }
854
855 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)
856 {
857         ImBuf *ima= NULL;
858
859         /* sanity check */
860         if(w<=0 || h<=0 || w>2000 || h>2000) {
861                 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
862                 BLI_assert(!"invalid icon size");
863                 return;
864         }
865
866         /* modulate color */
867         if(alpha != 1.0f)
868                 glPixelTransferf(GL_ALPHA_SCALE, alpha);
869
870         if(rgb) {
871                 glPixelTransferf(GL_RED_SCALE, rgb[0]);
872                 glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
873                 glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
874         }
875
876         /* rect contains image in 'rendersize', we only scale if needed */
877         if(rw!=w && rh!=h) {
878                 /* first allocate imbuf for scaling and copy preview into it */
879                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
880                 memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));    
881                 IMB_scaleImBuf(ima, w, h); /* scale it */
882                 rect= ima->rect;
883         }
884
885         /* draw */
886         if(is_preview) {
887                 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
888         }
889         else {
890                 glRasterPos2f(x, y);
891                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
892         }
893
894         if(ima)
895                 IMB_freeImBuf(ima);
896
897         /* restore color */
898         if(alpha != 0.0f)
899                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
900         
901         if(rgb) {
902                 glPixelTransferf(GL_RED_SCALE, 1.0f);
903                 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
904                 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
905         }
906 }
907
908 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)
909 {
910         float x1, x2, y1, y2;
911
912         if(rgb) glColor4f(rgb[0], rgb[1], rgb[2], alpha);
913         else glColor4f(1.0f, 1.0f, 1.0f, alpha);
914
915         x1= ix*icongltex.invw;
916         x2= (ix + ih)*icongltex.invw;
917         y1= iy*icongltex.invh;
918         y2= (iy + ih)*icongltex.invh;
919
920         glEnable(GL_TEXTURE_2D);
921         glBindTexture(GL_TEXTURE_2D, icongltex.id);
922
923         glBegin(GL_QUADS);
924         glTexCoord2f(x1, y1);
925         glVertex2f(x, y);
926
927         glTexCoord2f(x2, y1);
928         glVertex2f(x+w, y);
929
930         glTexCoord2f(x2, y2);
931         glVertex2f(x+w, y+h);
932
933         glTexCoord2f(x1, y2);
934         glVertex2f(x, y+h);
935         glEnd();
936
937         glBindTexture(GL_TEXTURE_2D, 0);
938         glDisable(GL_TEXTURE_2D);
939 }
940
941 /* Drawing size for preview images */
942 static int get_draw_size(enum eIconSizes size)
943 {
944         switch (size) {
945                 case ICON_SIZE_ICON: return ICON_DEFAULT_HEIGHT;
946                 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
947         }
948         return 0;
949 }
950
951 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)
952 {
953         Icon *icon = NULL;
954         DrawInfo *di = NULL;
955         IconImage *iimg;
956         float fdraw_size= is_preview ? draw_size : (draw_size * UI_DPI_ICON_FAC);
957         int w, h;
958         
959         icon = BKE_icon_get(icon_id);
960         alpha *= 0.75f;
961         
962         if (icon==NULL) {
963                 if (G.f & G_DEBUG)
964                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
965                 return;
966         }
967
968         di = (DrawInfo*)icon->drawinfo;
969         
970         if (!di) {
971                 di = icon_create_drawinfo();
972         
973                 icon->drawinfo = di;            
974                 icon->drawinfo_free = UI_icons_free_drawinfo;           
975         }
976         
977         /* scale width and height according to aspect */
978         w = (int)(fdraw_size/aspect + 0.5f);
979         h = (int)(fdraw_size/aspect + 0.5f);
980         
981         if(di->type == ICON_TYPE_VECTOR) {
982                 /* vector icons use the uiBlock transformation, they are not drawn
983                 with untransformed coordinates like the other icons */
984                 di->data.vector.func((int)x, (int)y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f); 
985         } 
986         else if(di->type == ICON_TYPE_TEXTURE) {
987                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
988                         di->data.texture.w, di->data.texture.h, alpha, rgb);
989         }
990         else if(di->type == ICON_TYPE_BUFFER) {
991                 /* it is a builtin icon */              
992                 iimg= di->data.buffer.image;
993
994                 if(!iimg->rect) return; /* something has gone wrong! */
995
996                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
997         }
998         else if(di->type == ICON_TYPE_PREVIEW) {
999                 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj); 
1000
1001                 if(pi) {                        
1002                         /* no create icon on this level in code */
1003                         if(!pi->rect[size]) return; /* something has gone wrong! */
1004                         
1005                         /* preview images use premul alpha ... */
1006                         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1007                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], 1.0f, NULL, is_preview);
1008                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1009                 }
1010         }
1011 }
1012
1013 static void ui_id_icon_render(bContext *C, ID *id, int big)
1014 {
1015         PreviewImage *pi = BKE_previewimg_get(id); 
1016         
1017         if (pi) {                       
1018                 if ((pi->changed[0] ||!pi->rect[0])) /* changed only ever set by dynamic icons */
1019                 {
1020                         /* create the rect if necessary */                              
1021                         
1022                         icon_set_image(C, id, pi, ICON_SIZE_ICON);              /* icon size */
1023                         if (big)
1024                                 icon_set_image(C, id, pi, ICON_SIZE_PREVIEW);   /* bigger preview size */
1025                         
1026                         pi->changed[0] = 0;
1027                 }
1028         }
1029 }
1030
1031 static void ui_id_brush_render(bContext *C, ID *id)
1032 {
1033         PreviewImage *pi = BKE_previewimg_get(id); 
1034         enum eIconSizes i;
1035         
1036         if(!pi)
1037                 return;
1038         
1039         for(i = 0; i < NUM_ICON_SIZES; i++) {
1040                 /* check if rect needs to be created; changed
1041                  only set by dynamic icons */
1042                 if((pi->changed[i] || !pi->rect[i])) {
1043                         icon_set_image(C, id, pi, i);
1044                         pi->changed[i] = 0;
1045                 }
1046         }
1047 }
1048
1049
1050 static int ui_id_brush_get_icon(bContext *C, ID *id)
1051 {
1052         Brush *br = (Brush*)id;
1053
1054         if(br->flag & BRUSH_CUSTOM_ICON) {
1055                 BKE_icon_getid(id);
1056                 ui_id_brush_render(C, id);
1057         }
1058         else {
1059                 Object *ob = CTX_data_active_object(C);
1060                 SpaceImage *sima;
1061                 EnumPropertyItem *items = NULL;
1062                 int tool, mode = 0;
1063
1064                 /* XXX: this is not nice, should probably make brushes
1065                    be strictly in one paint mode only to avoid
1066                    checking various context stuff here */
1067
1068                 if(CTX_wm_view3d(C) && ob) {
1069                         if(ob->mode & OB_MODE_SCULPT)
1070                                 mode = OB_MODE_SCULPT;
1071                         else if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT))
1072                                 mode = OB_MODE_VERTEX_PAINT;
1073                         else if(ob->mode & OB_MODE_TEXTURE_PAINT)
1074                                 mode = OB_MODE_TEXTURE_PAINT;
1075                 }
1076                 else if((sima = CTX_wm_space_image(C)) &&
1077                         (sima->flag & SI_DRAWTOOL)) {
1078                         mode = OB_MODE_TEXTURE_PAINT;
1079                 }
1080
1081                 /* reset the icon */
1082                 if(mode == OB_MODE_SCULPT) {
1083                         items = brush_sculpt_tool_items;
1084                         tool = br->sculpt_tool;
1085                 }
1086                 else if(mode == OB_MODE_VERTEX_PAINT) {
1087                         items = brush_vertex_tool_items;
1088                         tool = br->vertexpaint_tool;
1089                 }
1090                 else if(mode == OB_MODE_TEXTURE_PAINT) {
1091                         items = brush_image_tool_items;
1092                         tool = br->imagepaint_tool;
1093                 }
1094
1095                 if(!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1096                         id->icon_id = 0;
1097         }
1098
1099         return id->icon_id;
1100 }
1101
1102 int ui_id_icon_get(bContext *C, ID *id, int big)
1103 {
1104         int iconid= 0;
1105         
1106         /* icon */
1107         switch(GS(id->name)) {
1108                 case ID_BR:
1109                         iconid= ui_id_brush_get_icon(C, id);
1110                         break;
1111                 case ID_MA: /* fall through */
1112                 case ID_TE: /* fall through */
1113                 case ID_IM: /* fall through */
1114                 case ID_WO: /* fall through */
1115                 case ID_LA: /* fall through */
1116                         iconid= BKE_icon_getid(id);
1117                         /* checks if not exists, or changed */
1118                         ui_id_icon_render(C, id, big);
1119                         break;
1120                 default:
1121                         break;
1122         }
1123
1124         return iconid;
1125 }
1126
1127 static void icon_draw_at_size(float x, float y, int icon_id, float aspect, float alpha, enum eIconSizes size, int nocreate)
1128 {
1129         int draw_size = get_draw_size(size);
1130         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, FALSE);
1131 }
1132
1133 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1134 {
1135         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
1136 }
1137
1138 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, float *rgb)
1139 {
1140         int draw_size = get_draw_size(ICON_SIZE_ICON);
1141         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, FALSE, FALSE);
1142 }
1143
1144 void UI_icon_draw(float x, float y, int icon_id)
1145 {
1146         UI_icon_draw_aspect(x, y, icon_id, 1.0f, 1.0f);
1147 }
1148
1149 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1150 {
1151         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, TRUE, FALSE);
1152 }
1153
1154 void UI_icon_draw_preview(float x, float y, int icon_id)
1155 {
1156         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
1157 }
1158
1159 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1160 {
1161         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
1162 }
1163
1164 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1165 {
1166         icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, ICON_SIZE_PREVIEW, size, FALSE, TRUE);
1167 }
1168