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