fix [#28752] Brush Icons do not scale correctly with DPI
[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_storage_types.h"
51 #include "BLI_utildefines.h"
52
53 #include "DNA_brush_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_screen_types.h"
56 #include "DNA_space_types.h"
57
58 #include "RNA_access.h"
59 #include "RNA_enum_types.h"
60
61 #include "BKE_context.h"
62 #include "BKE_global.h"
63 #include "BKE_icons.h"
64 #include "BKE_utildefines.h"
65
66 #include "IMB_imbuf.h"
67 #include "IMB_imbuf_types.h"
68
69 #include "BIF_gl.h"
70 #include "BIF_glutil.h"
71
72 #include "ED_datafiles.h"
73 #include "ED_render.h"
74
75 #include "UI_interface.h"
76 #include "UI_interface_icons.h"
77
78 #include "interface_intern.h"
79
80
81 #define ICON_IMAGE_W            600
82 #define ICON_IMAGE_H            640
83
84 #define ICON_GRID_COLS          26
85 #define ICON_GRID_ROWS          30
86
87 #define ICON_GRID_MARGIN        5
88 #define ICON_GRID_W             16
89 #define ICON_GRID_H             16
90
91 typedef struct IconImage {
92         int w;
93         int h;
94         unsigned int *rect; 
95 } IconImage;
96
97 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
98
99 #define ICON_TYPE_PREVIEW       0
100 #define ICON_TYPE_TEXTURE       1
101 #define ICON_TYPE_BUFFER        2
102 #define ICON_TYPE_VECTOR        3
103
104 typedef struct DrawInfo {
105         int type;
106
107         union {
108                 /* type specific data */
109                 struct {
110                         VectorDrawFunc func;
111                 } vector;
112                 struct {
113                         IconImage* image;
114                 } buffer;
115                 struct {
116                         int x, y, w, h;
117                 } texture;
118         } data;
119 } DrawInfo;
120
121 typedef struct IconTexture {
122         GLuint id;
123         int w;
124         int h;
125         float invw;
126         float invh;
127 } IconTexture;
128
129 /* ******************* STATIC LOCAL VARS ******************* */
130 /* static here to cache results of icon directory scan, so it's not 
131  * scanning the filesystem each time the menu is drawn */
132 static struct ListBase iconfilelist = {NULL, NULL};
133 static IconTexture icongltex = {0, 0, 0, 0.0f, 0.0f};
134
135 /* **************************************************** */
136
137 static void def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type)
138 {
139         Icon *new_icon = NULL;
140         IconImage *iimg = NULL;
141         DrawInfo *di;
142         int y = 0;
143         int imgsize = 0;
144
145         new_icon = MEM_callocN(sizeof(Icon), "texicon");
146
147         new_icon->obj = NULL; /* icon is not for library object */
148         new_icon->type = 0;     
149
150         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
151         di->type= type;
152
153         if(type == ICON_TYPE_TEXTURE) {
154                 di->data.texture.x= xofs;
155                 di->data.texture.y= yofs;
156                 di->data.texture.w= size;
157                 di->data.texture.h= size;
158         }
159         else if(type == ICON_TYPE_BUFFER) {
160                 iimg = MEM_mallocN(sizeof(IconImage), "icon_img");
161                 iimg->rect = MEM_mallocN(size*size*sizeof(unsigned int), "icon_rect");
162                 iimg->w = size;
163                 iimg->h = size;
164
165                 /* Here we store the rect in the icon - same as before */
166                 imgsize = bbuf->x;
167                 for (y=0; y<size; y++) {
168                         memcpy(&iimg->rect[y*size], &bbuf->rect[(y+yofs)*imgsize+xofs], size*sizeof(int));
169                 }
170
171                 di->data.buffer.image = iimg;
172         }
173
174         new_icon->drawinfo_free = UI_icons_free_drawinfo;
175         new_icon->drawinfo = di;
176
177         BKE_icon_set(icon_id, new_icon);
178 }
179
180 static void def_internal_vicon( int icon_id, VectorDrawFunc drawFunc)
181 {
182         Icon *new_icon = NULL;
183         DrawInfo* di;
184
185         new_icon = MEM_callocN(sizeof(Icon), "texicon");
186
187         new_icon->obj = NULL; /* icon is not for library object */
188         new_icon->type = 0;
189
190         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
191         di->type= ICON_TYPE_VECTOR;
192         di->data.vector.func =drawFunc;
193
194         new_icon->drawinfo_free = NULL;
195         new_icon->drawinfo = di;
196
197         BKE_icon_set(icon_id, new_icon);
198 }
199
200 /* Vector Icon Drawing Routines */
201
202         /* Utilities */
203
204 static void viconutil_set_point(GLint pt[2], int x, int y)
205 {
206         pt[0] = x;
207         pt[1] = y;
208 }
209
210 static void viconutil_draw_tri(GLint (*pts)[2])
211 {
212         glBegin(GL_TRIANGLES);
213         glVertex2iv(pts[0]);
214         glVertex2iv(pts[1]);
215         glVertex2iv(pts[2]);
216         glEnd();
217 }
218
219 static void viconutil_draw_lineloop(GLint (*pts)[2], int numPoints)
220 {
221         int i;
222
223         glBegin(GL_LINE_LOOP);
224         for (i=0; i<numPoints; i++) {
225                 glVertex2iv(pts[i]);
226         }
227         glEnd();
228 }
229
230 static void viconutil_draw_lineloop_smooth(GLint (*pts)[2], int numPoints)
231 {
232         glEnable(GL_LINE_SMOOTH);
233         viconutil_draw_lineloop(pts, numPoints);
234         glDisable(GL_LINE_SMOOTH);
235 }
236
237 static void viconutil_draw_points(GLint (*pts)[2], int numPoints, int pointSize)
238 {
239         int i;
240
241         glBegin(GL_QUADS);
242         for (i=0; i<numPoints; i++) {
243                 int x = pts[i][0], y = pts[i][1];
244
245                 glVertex2i(x-pointSize,y-pointSize);
246                 glVertex2i(x+pointSize,y-pointSize);
247                 glVertex2i(x+pointSize,y+pointSize);
248                 glVertex2i(x-pointSize,y+pointSize);
249         }
250         glEnd();
251 }
252
253         /* Drawing functions */
254
255 static void vicon_x_draw(int x, int y, int w, int h, float alpha)
256 {
257         x += 3;
258         y += 3;
259         w -= 6;
260         h -= 6;
261
262         glEnable( GL_LINE_SMOOTH );
263
264         glLineWidth(2.5);
265         
266         glColor4f(0.0, 0.0, 0.0, alpha);
267         glBegin(GL_LINES);
268         glVertex2i(x  ,y  );
269         glVertex2i(x+w,y+h);
270         glVertex2i(x+w,y  );
271         glVertex2i(x  ,y+h);
272         glEnd();
273
274         glLineWidth(1.0);
275         
276         glDisable( GL_LINE_SMOOTH );
277 }
278
279 static void vicon_view3d_draw(int x, int y, int w, int h, float alpha)
280 {
281         int cx = x + w/2;
282         int cy = y + h/2;
283         int d = MAX2(2, h/3);
284
285         glColor4f(0.5, 0.5, 0.5, alpha);
286         glBegin(GL_LINES);
287         glVertex2i(x  , cy-d);
288         glVertex2i(x+w, cy-d);
289         glVertex2i(x  , cy+d);
290         glVertex2i(x+w, cy+d);
291
292         glVertex2i(cx-d, y  );
293         glVertex2i(cx-d, y+h);
294         glVertex2i(cx+d, y  );
295         glVertex2i(cx+d, y+h);
296         glEnd();
297         
298         glColor4f(0.0, 0.0, 0.0, alpha);
299         glBegin(GL_LINES);
300         glVertex2i(x  , cy);
301         glVertex2i(x+w, cy);
302         glVertex2i(cx, y  );
303         glVertex2i(cx, y+h);
304         glEnd();
305 }
306
307 static void vicon_edit_draw(int x, int y, int w, int h, float alpha)
308 {
309         GLint pts[4][2];
310
311         viconutil_set_point(pts[0], x+3  , y+3  );
312         viconutil_set_point(pts[1], x+w-3, y+3  );
313         viconutil_set_point(pts[2], x+w-3, y+h-3);
314         viconutil_set_point(pts[3], x+3  , y+h-3);
315
316         glColor4f(0.0, 0.0, 0.0, alpha);
317         viconutil_draw_lineloop(pts, 4);
318
319         glColor3f(1, 1, 0.0);
320         viconutil_draw_points(pts, 4, 1);
321 }
322
323 static void vicon_editmode_hlt_draw(int x, int y, int w, int h, float alpha)
324 {
325         GLint pts[3][2];
326
327         viconutil_set_point(pts[0], x+w/2, y+h-2);
328         viconutil_set_point(pts[1], x+3, y+4);
329         viconutil_set_point(pts[2], x+w-3, y+4);
330
331         glColor4f(0.5, 0.5, 0.5, alpha);
332         viconutil_draw_tri(pts);
333
334         glColor4f(0.0, 0.0, 0.0, 1);
335         viconutil_draw_lineloop_smooth(pts, 3);
336
337         glColor3f(1, 1, 0.0);
338         viconutil_draw_points(pts, 3, 1);
339 }
340
341 static void vicon_editmode_dehlt_draw(int x, int y, int w, int h, float UNUSED(alpha))
342 {
343         GLint pts[3][2];
344
345         viconutil_set_point(pts[0], x+w/2, y+h-2);
346         viconutil_set_point(pts[1], x+3, y+4);
347         viconutil_set_point(pts[2], x+w-3, y+4);
348
349         glColor4f(0.0f, 0.0f, 0.0f, 1);
350         viconutil_draw_lineloop_smooth(pts, 3);
351
352         glColor3f(.9f, .9f, .9f);
353         viconutil_draw_points(pts, 3, 1);
354 }
355
356 static void vicon_disclosure_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
357 {
358         GLint pts[3][2];
359         int cx = x+w/2;
360         int cy = y+w/2;
361         int d = w/3, d2 = w/5;
362
363         viconutil_set_point(pts[0], cx-d2, cy+d);
364         viconutil_set_point(pts[1], cx-d2, cy-d);
365         viconutil_set_point(pts[2], cx+d2, cy);
366
367         glShadeModel(GL_SMOOTH);
368         glBegin(GL_TRIANGLES);
369         glColor4f(0.8f, 0.8f, 0.8f, alpha);
370         glVertex2iv(pts[0]);
371         glVertex2iv(pts[1]);
372         glColor4f(0.3f, 0.3f, 0.3f, alpha);
373         glVertex2iv(pts[2]);
374         glEnd();
375         glShadeModel(GL_FLAT);
376
377         glColor4f(0.0f, 0.0f, 0.0f, 1);
378         viconutil_draw_lineloop_smooth(pts, 3);
379 }
380
381 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
382 {
383         GLint pts[3][2];
384         int cx = x+w/2-4;
385         int cy = y+w/2;
386         int d = w/5, d2 = w/7;
387
388         viconutil_set_point(pts[0], cx-d2, cy+d);
389         viconutil_set_point(pts[1], cx-d2, cy-d);
390         viconutil_set_point(pts[2], cx+d2, cy);
391
392         glColor4f(0.2f, 0.2f, 0.2f, alpha);
393
394         glShadeModel(GL_SMOOTH);
395         glBegin(GL_TRIANGLES);
396         glVertex2iv(pts[0]);
397         glVertex2iv(pts[1]);
398         glVertex2iv(pts[2]);
399         glEnd();
400         glShadeModel(GL_FLAT);
401 }
402
403 static void vicon_disclosure_tri_down_draw(int x, int y, int w, int UNUSED(h), float alpha)
404 {
405         GLint pts[3][2];
406         int cx = x+w/2;
407         int cy = y+w/2;
408         int d = w/3, d2 = w/5;
409
410         viconutil_set_point(pts[0], cx+d, cy+d2);
411         viconutil_set_point(pts[1], cx-d, cy+d2);
412         viconutil_set_point(pts[2], cx, cy-d2);
413
414         glShadeModel(GL_SMOOTH);
415         glBegin(GL_TRIANGLES);
416         glColor4f(0.8f, 0.8f, 0.8f, alpha);
417         glVertex2iv(pts[0]);
418         glVertex2iv(pts[1]);
419         glColor4f(0.3f, 0.3f, 0.3f, alpha);
420         glVertex2iv(pts[2]);
421         glEnd();
422         glShadeModel(GL_FLAT);
423
424         glColor4f(0.0f, 0.0f, 0.0f, 1);
425         viconutil_draw_lineloop_smooth(pts, 3);
426 }
427
428 static void vicon_move_up_draw(int x, int y, int w, int h, float UNUSED(alpha))
429 {
430         int d=-2;
431
432         glEnable(GL_LINE_SMOOTH);
433         glLineWidth(1);
434         glColor3f(0.0, 0.0, 0.0);
435
436         glBegin(GL_LINE_STRIP);
437         glVertex2i(x+w/2-d*2, y+h/2+d);
438         glVertex2i(x+w/2, y+h/2-d + 1);
439         glVertex2i(x+w/2+d*2, y+h/2+d);
440         glEnd();
441
442         glLineWidth(1.0);
443         glDisable(GL_LINE_SMOOTH);
444 }
445
446 static void vicon_move_down_draw(int x, int y, int w, int h, float UNUSED(alpha))
447 {
448         int d=2;
449
450         glEnable(GL_LINE_SMOOTH);
451         glLineWidth(1);
452         glColor3f(0.0, 0.0, 0.0);
453
454         glBegin(GL_LINE_STRIP);
455         glVertex2i(x+w/2-d*2, y+h/2+d);
456         glVertex2i(x+w/2, y+h/2-d - 1);
457         glVertex2i(x+w/2+d*2, y+h/2+d);
458         glEnd();
459
460         glLineWidth(1.0);
461         glDisable(GL_LINE_SMOOTH);
462 }
463
464 #ifndef WITH_HEADLESS
465 static void init_brush_icons(void)
466 {
467
468 #define INIT_BRUSH_ICON(icon_id, name)                                       \
469         bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_ ##name## _png, \
470                                      datatoc_ ##name## _png_size, IB_rect);  \
471         def_internal_icon(bbuf, icon_id, 0, 0, w, ICON_TYPE_BUFFER);         \
472         IMB_freeImBuf(bbuf);
473         // end INIT_BRUSH_ICON
474
475         ImBuf *bbuf;
476         const int w = 96;
477
478         INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
479         INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
480         INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
481         INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
482         INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
483         INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
484         INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
485         INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
486         INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
487         INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
488         INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
489         INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
490         INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
491         INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
492         INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
493         INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
494         INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
495         INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
496         INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
497         INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
498         INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
499         INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
500         INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
501         INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
502         INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
503         INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
504         INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
505         INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
506
507 #undef INIT_BRUSH_ICON
508 }
509
510 static void init_internal_icons(void)
511 {
512         bTheme *btheme= U.themes.first;
513         ImBuf *bbuf= NULL;
514         int x, y, icontype;
515         char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
516         
517         if ((btheme!=NULL) && btheme->tui.iconfile[0]) {
518                 char *icondir= BLI_get_folder(BLENDER_DATAFILES, "icons");
519                 if (icondir) {
520                         BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
521                         bbuf = IMB_loadiffname(iconfilestr, IB_rect); /* if the image is missing bbuf will just be NULL */
522                         if(bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
523                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
524                                 IMB_freeImBuf(bbuf);
525                                 bbuf= NULL;
526                         }
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_getdir changes the current working directory, restore it 
610            back to old value afterwards */
611         if(!BLI_getwdN(olddir, sizeof(olddir))) 
612                 restoredir = 0;
613         totfile = BLI_getdir(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_getdir() */
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("UI_icon_get_width: Internal error, no icon for icon ID: %d\n", 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("UI_icon_get_height: Internal error, no icon for icon ID: %d\n", 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("Error: requested preview image does not exist");
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("No preview image for this ID: %s\n", 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("icon_draw_rect: icons are %i x %i pixels?\n", 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), int 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         
961         if (icon==NULL) {
962                 if (G.f & G_DEBUG)
963                         printf("icon_draw_mipmap: Internal error, no icon for icon ID: %d\n", icon_id);
964                 return;
965         }
966
967         di = (DrawInfo*)icon->drawinfo;
968         
969         if (!di) {
970                 di = icon_create_drawinfo();
971         
972                 icon->drawinfo = di;            
973                 icon->drawinfo_free = UI_icons_free_drawinfo;           
974         }
975         
976         /* scale width and height according to aspect */
977         w = (int)(fdraw_size/aspect + 0.5f);
978         h = (int)(fdraw_size/aspect + 0.5f);
979         
980         if(di->type == ICON_TYPE_VECTOR) {
981                 /* vector icons use the uiBlock transformation, they are not drawn
982                 with untransformed coordinates like the other icons */
983                 di->data.vector.func((int)x, (int)y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f); 
984         } 
985         else if(di->type == ICON_TYPE_TEXTURE) {
986                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
987                         di->data.texture.w, di->data.texture.h, alpha, rgb);
988         }
989         else if(di->type == ICON_TYPE_BUFFER) {
990                 /* it is a builtin icon */              
991                 iimg= di->data.buffer.image;
992
993                 if(!iimg->rect) return; /* something has gone wrong! */
994
995                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
996         }
997         else if(di->type == ICON_TYPE_PREVIEW) {
998                 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj); 
999
1000                 if(pi) {                        
1001                         /* no create icon on this level in code */
1002                         if(!pi->rect[size]) return; /* something has gone wrong! */
1003                         
1004                         /* preview images use premul alpha ... */
1005                         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1006                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], 1.0f, NULL, is_preview);
1007                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1008                 }
1009         }
1010 }
1011
1012 static void ui_id_icon_render(bContext *C, ID *id, int big)
1013 {
1014         PreviewImage *pi = BKE_previewimg_get(id); 
1015         
1016         if (pi) {                       
1017                 if ((pi->changed[0] ||!pi->rect[0])) /* changed only ever set by dynamic icons */
1018                 {
1019                         /* create the rect if necessary */                              
1020                         
1021                         icon_set_image(C, id, pi, 0);           /* icon size */
1022                         if (big)
1023                                 icon_set_image(C, id, pi, 1);   /* bigger preview size */
1024                         
1025                         pi->changed[0] = 0;
1026                 }
1027         }
1028 }
1029
1030 static void ui_id_brush_render(bContext *C, ID *id)
1031 {
1032         PreviewImage *pi = BKE_previewimg_get(id); 
1033         int i;
1034         
1035         if(!pi)
1036                 return;
1037         
1038         for(i = 0; i < NUM_ICON_SIZES; i++) {
1039                 /* check if rect needs to be created; changed
1040                  only set by dynamic icons */
1041                 if((pi->changed[i] || !pi->rect[i])) {
1042                         icon_set_image(C, id, pi, i);
1043                         pi->changed[i] = 0;
1044                 }
1045         }
1046 }
1047
1048
1049 static int ui_id_brush_get_icon(bContext *C, ID *id)
1050 {
1051         Brush *br = (Brush*)id;
1052
1053         if(br->flag & BRUSH_CUSTOM_ICON) {
1054                 BKE_icon_getid(id);
1055                 ui_id_brush_render(C, id);
1056         }
1057         else {
1058                 Object *ob = CTX_data_active_object(C);
1059                 SpaceImage *sima;
1060                 EnumPropertyItem *items = NULL;
1061                 int tool, mode = 0;
1062
1063                 /* XXX: this is not nice, should probably make brushes
1064                    be strictly in one paint mode only to avoid
1065                    checking various context stuff here */
1066
1067                 if(CTX_wm_view3d(C) && ob) {
1068                         if(ob->mode & OB_MODE_SCULPT)
1069                                 mode = OB_MODE_SCULPT;
1070                         else if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT))
1071                                 mode = OB_MODE_VERTEX_PAINT;
1072                         else if(ob->mode & OB_MODE_TEXTURE_PAINT)
1073                                 mode = OB_MODE_TEXTURE_PAINT;
1074                 }
1075                 else if((sima = CTX_wm_space_image(C)) &&
1076                         (sima->flag & SI_DRAWTOOL)) {
1077                         mode = OB_MODE_TEXTURE_PAINT;
1078                 }
1079
1080                 /* reset the icon */
1081                 if(mode == OB_MODE_SCULPT) {
1082                         items = brush_sculpt_tool_items;
1083                         tool = br->sculpt_tool;
1084                 }
1085                 else if(mode == OB_MODE_VERTEX_PAINT) {
1086                         items = brush_vertex_tool_items;
1087                         tool = br->vertexpaint_tool;
1088                 }
1089                 else if(mode == OB_MODE_TEXTURE_PAINT) {
1090                         items = brush_image_tool_items;
1091                         tool = br->imagepaint_tool;
1092                 }
1093
1094                 if(!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1095                         id->icon_id = 0;
1096         }
1097
1098         return id->icon_id;
1099 }
1100
1101 int ui_id_icon_get(bContext *C, ID *id, int big)
1102 {
1103         int iconid= 0;
1104         
1105         /* icon */
1106         switch(GS(id->name)) {
1107                 case ID_BR:
1108                         iconid= ui_id_brush_get_icon(C, id);
1109                         break;
1110                 case ID_MA: /* fall through */
1111                 case ID_TE: /* fall through */
1112                 case ID_IM: /* fall through */
1113                 case ID_WO: /* fall through */
1114                 case ID_LA: /* fall through */
1115                         iconid= BKE_icon_getid(id);
1116                         /* checks if not exists, or changed */
1117                         ui_id_icon_render(C, id, big);
1118                         break;
1119                 default:
1120                         break;
1121         }
1122
1123         return iconid;
1124 }
1125
1126 static void icon_draw_at_size(float x, float y, int icon_id, float aspect, float alpha, enum eIconSizes size, int nocreate)
1127 {
1128         int draw_size = get_draw_size(size);
1129         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, FALSE);
1130 }
1131
1132 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1133 {
1134         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
1135 }
1136
1137 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, float *rgb)
1138 {
1139         int draw_size = get_draw_size(ICON_SIZE_ICON);
1140         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, FALSE, FALSE);
1141 }
1142
1143 void UI_icon_draw(float x, float y, int icon_id)
1144 {
1145         UI_icon_draw_aspect(x, y, icon_id, 1.0f, 1.0f);
1146 }
1147
1148 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1149 {
1150         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, TRUE, FALSE);
1151 }
1152
1153 void UI_icon_draw_preview(float x, float y, int icon_id)
1154 {
1155         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
1156 }
1157
1158 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1159 {
1160         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
1161 }
1162
1163 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1164 {
1165         icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, ICON_SIZE_PREVIEW, size, FALSE, TRUE);
1166 }
1167