doxygen: blender/editors tagged.
[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 = {0, 0};
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 = 0; /* 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 = 0; /* 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 = 0;
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 static void init_brush_icons(void)
465 {
466
467 #define INIT_BRUSH_ICON(icon_id, name)                                       \
468         bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_ ##name## _png, \
469                                      datatoc_ ##name## _png_size, IB_rect);  \
470         def_internal_icon(bbuf, icon_id, 0, 0, w, ICON_TYPE_BUFFER);         \
471         IMB_freeImBuf(bbuf);
472         // end INIT_BRUSH_ICON
473
474         ImBuf *bbuf;
475         const int w = 96;
476
477         INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
478         INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
479         INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
480         INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
481         INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
482         INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
483         INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
484         INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
485         INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
486         INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
487         INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
488         INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
489         INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
490         INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
491         INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
492         INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
493         INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
494         INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
495         INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
496         INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
497         INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
498         INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
499         INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
500         INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
501         INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
502         INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
503         INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
504         INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
505
506 #undef INIT_BRUSH_ICON
507 }
508
509 static void init_internal_icons(void)
510 {
511         bTheme *btheme= U.themes.first;
512         ImBuf *bbuf= NULL;
513         int x, y, icontype;
514         char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
515         
516         if ((btheme!=NULL) && btheme->tui.iconfile[0]) {
517                 char *datadir= BLI_get_folder(BLENDER_DATAFILES, NULL);
518                 if (datadir) {
519                         BLI_make_file_string("/", iconfilestr, datadir, btheme->tui.iconfile);
520                         
521                         if (BLI_exists(iconfilestr)) {
522                                 bbuf = IMB_loadiffname(iconfilestr, IB_rect);
523                                 if(bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H) {
524                                         if (G.f & G_DEBUG)
525                                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
526                                         IMB_freeImBuf(bbuf);
527                                         bbuf= NULL;
528                                 }
529                         }
530                 }
531         }
532         if(bbuf==NULL)
533                 bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
534
535         if(bbuf) {
536                 /* free existing texture if any */
537                 if(icongltex.id) {
538                         glDeleteTextures(1, &icongltex.id);
539                         icongltex.id= 0;
540                 }
541
542                 /* we only use a texture for cards with non-power of two */
543                 if(GPU_non_power_of_two_support()) {
544                         glGenTextures(1, &icongltex.id);
545
546                         if(icongltex.id) {
547                                 icongltex.w = bbuf->x;
548                                 icongltex.h = bbuf->y;
549                                 icongltex.invw = 1.0f/bbuf->x;
550                                 icongltex.invh = 1.0f/bbuf->y;
551
552                                 glBindTexture(GL_TEXTURE_2D, icongltex.id);
553                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bbuf->x, bbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, bbuf->rect);
554                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
555                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
556                                 glBindTexture(GL_TEXTURE_2D, 0);
557
558                                 if(glGetError() == GL_OUT_OF_MEMORY) {
559                                         glDeleteTextures(1, &icongltex.id);
560                                         icongltex.id= 0;
561                                 }
562                         }
563                 }
564         }
565
566         if(icongltex.id)
567                 icontype= ICON_TYPE_TEXTURE;
568         else
569                 icontype= ICON_TYPE_BUFFER;
570         
571         if(bbuf) {
572                 for (y=0; y<ICON_GRID_ROWS; y++) {
573                         for (x=0; x<ICON_GRID_COLS; x++) {
574                                 def_internal_icon(bbuf, BIFICONID_FIRST + y*ICON_GRID_COLS + x,
575                                         x*(ICON_GRID_W+ICON_GRID_MARGIN)+ICON_GRID_MARGIN,
576                                         y*(ICON_GRID_H+ICON_GRID_MARGIN)+ICON_GRID_MARGIN, ICON_GRID_W,
577                                         icontype);
578                         }
579                 }
580         }
581
582         def_internal_vicon(VICO_VIEW3D_VEC, vicon_view3d_draw);
583         def_internal_vicon(VICO_EDIT_VEC, vicon_edit_draw);
584         def_internal_vicon(VICO_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
585         def_internal_vicon(VICO_EDITMODE_HLT, vicon_editmode_hlt_draw);
586         def_internal_vicon(VICO_DISCLOSURE_TRI_RIGHT_VEC, vicon_disclosure_tri_right_draw);
587         def_internal_vicon(VICO_DISCLOSURE_TRI_DOWN_VEC, vicon_disclosure_tri_down_draw);
588         def_internal_vicon(VICO_MOVE_UP_VEC, vicon_move_up_draw);
589         def_internal_vicon(VICO_MOVE_DOWN_VEC, vicon_move_down_draw);
590         def_internal_vicon(VICO_X_VEC, vicon_x_draw);
591         def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
592
593         IMB_freeImBuf(bbuf);
594 }
595
596
597 static void init_iconfile_list(struct ListBase *list)
598 {
599         IconFile *ifile;
600         ImBuf *bbuf= NULL;
601         struct direntry *dir;
602         int restoredir = 1; /* restore to current directory */
603         int totfile, i, index=1;
604         int ifilex, ifiley;
605         char icondirstr[FILE_MAX];
606         char iconfilestr[FILE_MAX+16]; /* allow 256 chars for file+dir */
607         char olddir[FILE_MAX];
608         char *datadir= NULL;
609
610         list->first = list->last = NULL;
611         datadir = BLI_get_folder(BLENDER_DATAFILES, NULL);
612
613         if (!datadir) return;
614
615         BLI_make_file_string("/", icondirstr, datadir, "");
616         
617         if(BLI_exists(icondirstr)==0)
618                 return;
619         
620         /* since BLI_getdir changes the current working directory, restore it 
621            back to old value afterwards */
622         if(!BLI_getwdN(olddir, sizeof(olddir))) 
623                 restoredir = 0;
624         totfile = BLI_getdir(icondirstr, &dir);
625         if (restoredir && !chdir(olddir)) {} /* fix warning about checking return value */
626
627         for(i=0; i<totfile; i++) {
628                 if( (dir[i].type & S_IFREG) ) {
629                         char *filename = dir[i].relname;
630                         
631                         if(BLI_testextensie(filename, ".png")) {
632
633                                 /* check to see if the image is the right size, continue if not */
634                                 /* copying strings here should go ok, assuming that we never get back
635                                    a complete path to file longer than 256 chars */
636                                 sprintf(iconfilestr, "%s/%s", icondirstr, filename);
637                                 if(BLI_exists(iconfilestr))
638                                         bbuf= IMB_loadiffname(iconfilestr, IB_rect);
639                                 else
640                                         bbuf= NULL;
641
642
643                                 if(bbuf) {
644                                         ifilex = bbuf->x;
645                                         ifiley = bbuf->y;
646                                         IMB_freeImBuf(bbuf);
647                                 }
648                                 else {
649                                         ifilex= ifiley= 0;
650                                 }
651                                 
652                                 /* bad size or failed to load */
653                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H))
654                                         continue;
655
656                                 /* found a potential icon file, so make an entry for it in the cache list */
657                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
658                                 
659                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
660                                 ifile->index = index;
661
662                                 BLI_addtail(list, ifile);
663                                 
664                                 index++;
665                         }
666                 }
667         }
668         
669         /* free temporary direntry structure that's been created by BLI_getdir() */
670         i= totfile-1;
671         
672         for(; i>=0; i--){
673                 MEM_freeN(dir[i].relname);
674                 MEM_freeN(dir[i].path);
675                 if (dir[i].string) MEM_freeN(dir[i].string);
676         }
677         free(dir);
678         dir= 0;
679 }
680
681 static void free_iconfile_list(struct ListBase *list)
682 {
683         IconFile *ifile=NULL, *next_ifile=NULL;
684         
685         for(ifile=list->first; ifile; ifile=next_ifile) {
686                 next_ifile = ifile->next;
687                 BLI_freelinkN(list, ifile);
688         }
689 }
690
691 int UI_iconfile_get_index(const char *filename)
692 {
693         IconFile *ifile;
694         ListBase *list=&(iconfilelist);
695         
696         for(ifile=list->first; ifile; ifile=ifile->next) {
697                 if ( BLI_streq(filename, ifile->filename)) {
698                         return ifile->index;
699                 }
700         }
701         
702         return 0;
703 }
704
705 ListBase *UI_iconfile_list(void)
706 {
707         ListBase *list=&(iconfilelist);
708         
709         return list;
710 }
711
712
713 void UI_icons_free(void)
714 {
715         if(icongltex.id) {
716                 glDeleteTextures(1, &icongltex.id);
717                 icongltex.id= 0;
718         }
719
720         free_iconfile_list(&iconfilelist);
721         BKE_icons_free();
722 }
723
724 void UI_icons_free_drawinfo(void *drawinfo)
725 {
726         DrawInfo *di = drawinfo;
727
728         if(di) {
729                 if(di->type == ICON_TYPE_BUFFER) {
730                         if(di->data.buffer.image) {
731                                 MEM_freeN(di->data.buffer.image->rect);
732                                 MEM_freeN(di->data.buffer.image);
733                         }
734                 }
735
736                 MEM_freeN(di);
737         }
738 }
739
740 static DrawInfo *icon_create_drawinfo(void)
741 {
742         DrawInfo *di = NULL;
743
744         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
745         di->type= ICON_TYPE_PREVIEW;
746
747         return di;
748 }
749
750 int UI_icon_get_width(int icon_id)
751 {
752         Icon *icon = NULL;
753         DrawInfo *di = NULL;
754
755         icon = BKE_icon_get(icon_id);
756         
757         if (icon==ICON_NONE) {
758                 if (G.f & G_DEBUG)
759                         printf("UI_icon_get_width: Internal error, no icon for icon ID: %d\n", icon_id);
760                 return 0;
761         }
762         
763         di = (DrawInfo *)icon->drawinfo;
764         if (!di) {
765                 di = icon_create_drawinfo();
766                 icon->drawinfo = di;
767         }
768
769         if (di)
770                 return ICON_DEFAULT_WIDTH;
771
772         return 0;
773 }
774
775 int UI_icon_get_height(int icon_id)
776 {
777         Icon *icon = NULL;
778         DrawInfo *di = NULL;
779
780         icon = BKE_icon_get(icon_id);
781         
782         if (icon==ICON_NONE) {
783                 if (G.f & G_DEBUG)
784                         printf("UI_icon_get_height: Internal error, no icon for icon ID: %d\n", icon_id);
785                 return 0;
786         }
787         
788         di = (DrawInfo*)icon->drawinfo;
789
790         if (!di) {
791                 di = icon_create_drawinfo();
792                 icon->drawinfo = di;
793         }
794         
795         if (di)
796                 return ICON_DEFAULT_HEIGHT;
797
798         return 0;
799 }
800
801 void UI_icons_init(int first_dyn_id)
802 {
803         init_iconfile_list(&iconfilelist);
804         BKE_icons_init(first_dyn_id);
805         init_internal_icons();
806         init_brush_icons();
807 }
808
809 /* Render size for preview images at level miplevel */
810 static int preview_render_size(int miplevel)
811 {
812         switch (miplevel) {
813                 case 0: return 32;
814                 case 1: return PREVIEW_DEFAULT_HEIGHT;
815         }
816         return 0;
817 }
818
819 static void icon_create_mipmap(struct PreviewImage* prv_img, int miplevel) 
820 {
821         unsigned int size = preview_render_size(miplevel);
822
823         if (!prv_img) {
824                 if (G.f & G_DEBUG)
825                         printf("Error: requested preview image does not exist");
826         }
827         if (!prv_img->rect[miplevel]) {
828                 prv_img->w[miplevel] = size;
829                 prv_img->h[miplevel] = size;
830                 prv_img->changed[miplevel] = 1;
831                 prv_img->changed_timestamp[miplevel] = 0;
832                 prv_img->rect[miplevel] = MEM_callocN(size*size*sizeof(unsigned int), "prv_rect"); 
833         }
834 }
835
836 /* only called when icon has changed */
837 /* only call with valid pointer from UI_icon_draw */
838 static void icon_set_image(bContext *C, ID *id, PreviewImage* prv_img, int miplevel)
839 {
840         if (!prv_img) {
841                 if (G.f & G_DEBUG)
842                         printf("No preview image for this ID: %s\n", id->name);
843                 return;
844         }       
845
846         /* create the preview rect */
847         icon_create_mipmap(prv_img, miplevel);
848
849         ED_preview_icon_job(C, prv_img, id, prv_img->rect[miplevel],
850                 prv_img->w[miplevel], prv_img->h[miplevel]);
851 }
852
853 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)
854 {
855         ImBuf *ima= NULL;
856
857         /* sanity check */
858         if(w<=0 || h<=0 || w>2000 || h>2000) {
859                 printf("icon_draw_rect: icons are %i x %i pixels?\n", w, h);
860                 BLI_assert(!"invalid icon size");
861                 return;
862         }
863
864         /* modulate color */
865         if(alpha != 1.0f)
866                 glPixelTransferf(GL_ALPHA_SCALE, alpha);
867
868         if(rgb) {
869                 glPixelTransferf(GL_RED_SCALE, rgb[0]);
870                 glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
871                 glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
872         }
873
874         /* rect contains image in 'rendersize', we only scale if needed */
875         if(rw!=w && rh!=h) {
876                 /* first allocate imbuf for scaling and copy preview into it */
877                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
878                 memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));    
879                 IMB_scaleImBuf(ima, w, h); /* scale it */
880                 rect= ima->rect;
881         }
882
883         /* draw */
884         if(is_preview) {
885                 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
886         }
887         else {
888                 glRasterPos2f(x, y);
889                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
890         }
891
892         if(ima)
893                 IMB_freeImBuf(ima);
894
895         /* restore color */
896         if(alpha != 0.0f)
897                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
898         
899         if(rgb) {
900                 glPixelTransferf(GL_RED_SCALE, 1.0f);
901                 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
902                 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
903         }
904 }
905
906 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)
907 {
908         float x1, x2, y1, y2;
909
910         if(rgb) glColor4f(rgb[0], rgb[1], rgb[2], alpha);
911         else glColor4f(1.0f, 1.0f, 1.0f, alpha);
912
913         x1= ix*icongltex.invw;
914         x2= (ix + ih)*icongltex.invw;
915         y1= iy*icongltex.invh;
916         y2= (iy + ih)*icongltex.invh;
917
918         glEnable(GL_TEXTURE_2D);
919         glBindTexture(GL_TEXTURE_2D, icongltex.id);
920
921         glBegin(GL_QUADS);
922         glTexCoord2f(x1, y1);
923         glVertex2f(x, y);
924
925         glTexCoord2f(x2, y1);
926         glVertex2f(x+w, y);
927
928         glTexCoord2f(x2, y2);
929         glVertex2f(x+w, y+h);
930
931         glTexCoord2f(x1, y2);
932         glVertex2f(x, y+h);
933         glEnd();
934
935         glBindTexture(GL_TEXTURE_2D, 0);
936         glDisable(GL_TEXTURE_2D);
937 }
938
939 /* Drawing size for preview images at level miplevel */
940 static int preview_size(int miplevel)
941 {
942         switch (miplevel) {
943                 case 0: return ICON_DEFAULT_HEIGHT;
944                 case 1: return PREVIEW_DEFAULT_HEIGHT;
945         }
946         return 0;
947 }
948
949 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)
950 {
951         Icon *icon = NULL;
952         DrawInfo *di = NULL;
953         IconImage *iimg;
954         int w, h;
955         
956         icon = BKE_icon_get(icon_id);
957         
958         if (icon==ICON_NONE) {
959                 if (G.f & G_DEBUG)
960                         printf("icon_draw_mipmap: Internal error, no icon for icon ID: %d\n", icon_id);
961                 return;
962         }
963
964         di = (DrawInfo*)icon->drawinfo;
965         
966         if (!di) {
967                 di = icon_create_drawinfo();
968         
969                 icon->drawinfo = di;            
970                 icon->drawinfo_free = UI_icons_free_drawinfo;           
971         }
972         
973         /* scale width and height according to aspect */
974         w = (int)(draw_size/aspect + 0.5f);
975         h = (int)(draw_size/aspect + 0.5f);
976         
977         if(di->type == ICON_TYPE_VECTOR) {
978                 /* vector icons use the uiBlock transformation, they are not drawn
979                 with untransformed coordinates like the other icons */
980                 di->data.vector.func((int)x, (int)y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f); 
981         } 
982         else if(di->type == ICON_TYPE_TEXTURE) {
983                 icon_draw_texture(x, y, w, h, di->data.texture.x, di->data.texture.y,
984                         di->data.texture.w, di->data.texture.h, alpha, rgb);
985         }
986         else if(di->type == ICON_TYPE_BUFFER) {
987                 /* it is a builtin icon */              
988                 iimg= di->data.buffer.image;
989
990                 if(!iimg->rect) return; /* something has gone wrong! */
991
992                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
993         }
994         else if(di->type == ICON_TYPE_PREVIEW) {
995                 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj); 
996
997                 if(pi) {                        
998                         /* no create icon on this level in code */
999                         if(!pi->rect[miplevel]) return; /* something has gone wrong! */
1000                         
1001                         /* preview images use premul alpha ... */
1002                         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1003                         icon_draw_rect(x, y, w, h, aspect, pi->w[miplevel], pi->h[miplevel], pi->rect[miplevel], 1.0f, NULL, is_preview);
1004                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1005                 }
1006         }
1007 }
1008
1009 static void ui_id_icon_render(bContext *C, ID *id, int big)
1010 {
1011         PreviewImage *pi = BKE_previewimg_get(id); 
1012         
1013         if (pi) {                       
1014                 if ((pi->changed[0] ||!pi->rect[0])) /* changed only ever set by dynamic icons */
1015                 {
1016                         /* create the rect if necessary */                              
1017                         
1018                         icon_set_image(C, id, pi, 0);           /* icon size */
1019                         if (big)
1020                                 icon_set_image(C, id, pi, 1);   /* bigger preview size */
1021                         
1022                         pi->changed[0] = 0;
1023                 }
1024         }
1025 }
1026
1027 static void ui_id_brush_render(bContext *C, ID *id)
1028 {
1029         PreviewImage *pi = BKE_previewimg_get(id); 
1030         int i;
1031         
1032         if(!pi)
1033                 return;
1034         
1035         for(i = 0; i < PREVIEW_MIPMAPS; i++) {
1036                 /* check if rect needs to be created; changed
1037                  only set by dynamic icons */
1038                 if((pi->changed[i] || !pi->rect[i])) {
1039                         icon_set_image(C, id, pi, i);
1040                         pi->changed[i] = 0;
1041                 }
1042         }
1043 }
1044
1045
1046 static int ui_id_brush_get_icon(bContext *C, ID *id)
1047 {
1048         Brush *br = (Brush*)id;
1049
1050         if(br->flag & BRUSH_CUSTOM_ICON) {
1051                 BKE_icon_getid(id);
1052                 ui_id_brush_render(C, id);
1053         }
1054         else {
1055                 Object *ob = CTX_data_active_object(C);
1056                 SpaceImage *sima;
1057                 EnumPropertyItem *items = NULL;
1058                 int tool, mode = 0;
1059
1060                 /* XXX: this is not nice, should probably make brushes
1061                    be strictly in one paint mode only to avoid
1062                    checking various context stuff here */
1063
1064                 if(CTX_wm_view3d(C) && ob) {
1065                         if(ob->mode & OB_MODE_SCULPT)
1066                                 mode = OB_MODE_SCULPT;
1067                         else if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT))
1068                                 mode = OB_MODE_VERTEX_PAINT;
1069                         else if(ob->mode & OB_MODE_TEXTURE_PAINT)
1070                                 mode = OB_MODE_TEXTURE_PAINT;
1071                 }
1072                 else if((sima = CTX_wm_space_image(C)) &&
1073                         (sima->flag & SI_DRAWTOOL)) {
1074                         mode = OB_MODE_TEXTURE_PAINT;
1075                 }
1076
1077                 /* reset the icon */
1078                 if(mode == OB_MODE_SCULPT) {
1079                         items = brush_sculpt_tool_items;
1080                         tool = br->sculpt_tool;
1081                 }
1082                 else if(mode == OB_MODE_VERTEX_PAINT) {
1083                         items = brush_vertexpaint_tool_items;
1084                         tool = br->vertexpaint_tool;
1085                 }
1086                 else if(mode == OB_MODE_TEXTURE_PAINT) {
1087                         items = brush_imagepaint_tool_items;
1088                         tool = br->imagepaint_tool;
1089                 }
1090
1091                 if(!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1092                         id->icon_id = 0;
1093         }
1094
1095         return id->icon_id;
1096 }
1097
1098 int ui_id_icon_get(bContext *C, ID *id, int big)
1099 {
1100         int iconid= 0;
1101         
1102         /* icon */
1103         switch(GS(id->name))
1104         {
1105                 case ID_BR:
1106                         iconid= ui_id_brush_get_icon(C, id);
1107                         break;
1108                 case ID_MA: /* fall through */
1109                 case ID_TE: /* fall through */
1110                 case ID_IM: /* fall through */
1111                 case ID_WO: /* fall through */
1112                 case ID_LA: /* fall through */
1113                         iconid= BKE_icon_getid(id);
1114                         /* checks if not exists, or changed */
1115                         ui_id_icon_render(C, id, big);
1116                         break;
1117                 default:
1118                         break;
1119         }
1120
1121         return iconid;
1122 }
1123
1124 static void icon_draw_mipmap(float x, float y, int icon_id, float aspect, float alpha, int miplevel, int nocreate)
1125 {
1126         int draw_size = preview_size(miplevel);
1127         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, miplevel, draw_size, nocreate, FALSE);
1128 }
1129
1130 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1131 {
1132         icon_draw_mipmap(x, y, icon_id, aspect, alpha, PREVIEW_MIPMAP_ZERO, 0);
1133 }
1134
1135 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, float *rgb)
1136 {
1137         int draw_size = preview_size(PREVIEW_MIPMAP_ZERO);
1138         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, PREVIEW_MIPMAP_ZERO, draw_size, FALSE, FALSE);
1139 }
1140
1141 void UI_icon_draw(float x, float y, int icon_id)
1142 {
1143         UI_icon_draw_aspect(x, y, icon_id, 1.0f, 1.0f);
1144 }
1145
1146 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1147 {
1148         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, PREVIEW_MIPMAP_ZERO, size, TRUE, FALSE);
1149 }
1150
1151 void UI_icon_draw_preview(float x, float y, int icon_id)
1152 {
1153         icon_draw_mipmap(x, y, icon_id, 1.0f, 1.0f, PREVIEW_MIPMAP_LARGE, 0);
1154 }
1155
1156 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1157 {
1158         icon_draw_mipmap(x, y, icon_id, aspect, 1.0f, PREVIEW_MIPMAP_LARGE, 0);
1159 }
1160
1161 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1162 {
1163         icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, PREVIEW_MIPMAP_LARGE, size, FALSE, TRUE);
1164 }
1165