4d8ec5996beb09729f17184926fcdb1089fdc63d
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #endif   
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_arithb.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_storage_types.h"
43
44 #include "DNA_material_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_userdef_types.h"
48
49 #include "BKE_image.h"
50 #include "BKE_icons.h"
51 #include "BKE_utildefines.h"
52
53 #include "IMB_imbuf.h"
54 #include "IMB_imbuf_types.h"
55
56 #include "BIF_gl.h"
57 #include "BIF_glutil.h"
58
59 #include "ED_datafiles.h"
60 #include "ED_previewrender.h"
61
62 #include "UI_interface.h"
63 #include "UI_interface_icons.h"
64 #include "UI_resources.h" /* elubie: should be removed once the enum for the ICONS is in BIF_preview_icons.h */
65
66 #include "interface_intern.h"
67
68
69 #define ICON_IMAGE_W            600
70 #define ICON_IMAGE_H            640
71
72 #define ICON_GRID_COLS          26
73 #define ICON_GRID_ROWS          30
74
75 #define ICON_GRID_MARGIN        5
76 #define ICON_GRID_W             16
77 #define ICON_GRID_H             16
78
79 typedef struct IconImage {
80         int w;
81         int h;
82         unsigned int *rect; 
83 } IconImage;
84
85 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
86
87 typedef struct DrawInfo {
88         int w;
89         int h;
90         float aspect;
91         VectorDrawFunc drawFunc; /* If drawFunc is defined then it is a vector icon, otherwise use rect */              
92         IconImage* icon;
93 } DrawInfo;
94
95 /* ******************* STATIC LOCAL VARS ******************* */
96 /* static here to cache results of icon directory scan, so it's not 
97  * scanning the filesystem each time the menu is drawn */
98 static struct ListBase iconfilelist = {0, 0};
99
100
101 /* **************************************************** */
102
103 static void def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size)
104 {
105         Icon *new_icon = NULL;
106         IconImage *iimg = NULL;
107         DrawInfo *di;
108         int y = 0;
109         int imgsize = 0;
110
111         new_icon = MEM_callocN(sizeof(Icon), "texicon");
112
113         new_icon->obj = 0; /* icon is not for library object */
114         new_icon->type = 0;     
115
116         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
117         di->drawFunc = 0;
118         di->w = size;
119         di->h = size;
120         di->aspect = 1.0f;
121         
122         iimg = MEM_mallocN(sizeof(IconImage), "icon_img");
123         iimg->rect = MEM_mallocN(size*size*sizeof(unsigned int), "icon_rect");
124         iimg->w = size;
125         iimg->h = size;
126
127         /* Here we store the rect in the icon - same as before */
128         imgsize = bbuf->x;
129         for (y=0; y<size; y++) {
130                 memcpy(&iimg->rect[y*size], &bbuf->rect[(y+yofs)*imgsize+xofs], size*sizeof(int));
131         }
132
133         di->icon = iimg;
134
135         new_icon->drawinfo_free = UI_icons_free_drawinfo;
136         new_icon->drawinfo = di;
137
138         BKE_icon_set(icon_id, new_icon);
139 }
140
141 static void def_internal_vicon( int icon_id, VectorDrawFunc drawFunc)
142 {
143         Icon *new_icon = NULL;
144         DrawInfo* di;
145
146         new_icon = MEM_callocN(sizeof(Icon), "texicon");
147
148         new_icon->obj = 0; /* icon is not for library object */
149         new_icon->type = 0;
150
151         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
152         di->drawFunc =drawFunc;
153         di->w = ICON_DEFAULT_HEIGHT;
154         di->h = ICON_DEFAULT_HEIGHT;
155         di->aspect = 1.0f;
156         di->icon = NULL;
157
158         new_icon->drawinfo_free = 0;
159         new_icon->drawinfo = di;
160
161         BKE_icon_set(icon_id, new_icon);
162 }
163
164 /* Vector Icon Drawing Routines */
165
166         /* Utilities */
167
168 static void viconutil_set_point(GLint pt[2], int x, int y)
169 {
170         pt[0] = x;
171         pt[1] = y;
172 }
173
174 static void viconutil_draw_tri(GLint (*pts)[2])
175 {
176         glBegin(GL_TRIANGLES);
177         glVertex2iv(pts[0]);
178         glVertex2iv(pts[1]);
179         glVertex2iv(pts[2]);
180         glEnd();
181 }
182
183 static void viconutil_draw_lineloop(GLint (*pts)[2], int numPoints)
184 {
185         int i;
186
187         glBegin(GL_LINE_LOOP);
188         for (i=0; i<numPoints; i++) {
189                 glVertex2iv(pts[i]);
190         }
191         glEnd();
192 }
193
194 static void viconutil_draw_lineloop_smooth(GLint (*pts)[2], int numPoints)
195 {
196         glEnable(GL_LINE_SMOOTH);
197         viconutil_draw_lineloop(pts, numPoints);
198         glDisable(GL_LINE_SMOOTH);
199 }
200
201 static void viconutil_draw_points(GLint (*pts)[2], int numPoints, int pointSize)
202 {
203         int i;
204
205         glBegin(GL_QUADS);
206         for (i=0; i<numPoints; i++) {
207                 int x = pts[i][0], y = pts[i][1];
208
209                 glVertex2i(x-pointSize,y-pointSize);
210                 glVertex2i(x+pointSize,y-pointSize);
211                 glVertex2i(x+pointSize,y+pointSize);
212                 glVertex2i(x-pointSize,y+pointSize);
213         }
214         glEnd();
215 }
216
217         /* Drawing functions */
218
219 static void vicon_x_draw(int x, int y, int w, int h, float alpha)
220 {
221         x += 3;
222         y += 3;
223         w -= 6;
224         h -= 6;
225
226         glEnable( GL_LINE_SMOOTH );
227
228         glLineWidth(2.5);
229         
230         glColor4f(0.0, 0.0, 0.0, alpha);
231         glBegin(GL_LINES);
232         glVertex2i(x  ,y  );
233         glVertex2i(x+w,y+h);
234         glVertex2i(x+w,y  );
235         glVertex2i(x  ,y+h);
236         glEnd();
237
238         glLineWidth(1.0);
239         
240         glDisable( GL_LINE_SMOOTH );
241 }
242
243 static void vicon_view3d_draw(int x, int y, int w, int h, float alpha)
244 {
245         int cx = x + w/2;
246         int cy = y + h/2;
247         int d = MAX2(2, h/3);
248
249         glColor4f(0.5, 0.5, 0.5, alpha);
250         glBegin(GL_LINES);
251         glVertex2i(x  , cy-d);
252         glVertex2i(x+w, cy-d);
253         glVertex2i(x  , cy+d);
254         glVertex2i(x+w, cy+d);
255
256         glVertex2i(cx-d, y  );
257         glVertex2i(cx-d, y+h);
258         glVertex2i(cx+d, y  );
259         glVertex2i(cx+d, y+h);
260         glEnd();
261         
262         glColor4f(0.0, 0.0, 0.0, alpha);
263         glBegin(GL_LINES);
264         glVertex2i(x  , cy);
265         glVertex2i(x+w, cy);
266         glVertex2i(cx, y  );
267         glVertex2i(cx, y+h);
268         glEnd();
269 }
270
271 static void vicon_edit_draw(int x, int y, int w, int h, float alpha)
272 {
273         GLint pts[4][2];
274
275         viconutil_set_point(pts[0], x+3  , y+3  );
276         viconutil_set_point(pts[1], x+w-3, y+3  );
277         viconutil_set_point(pts[2], x+w-3, y+h-3);
278         viconutil_set_point(pts[3], x+3  , y+h-3);
279
280         glColor4f(0.0, 0.0, 0.0, alpha);
281         viconutil_draw_lineloop(pts, 4);
282
283         glColor3f(1, 1, 0.0);
284         viconutil_draw_points(pts, 4, 1);
285 }
286
287 static void vicon_editmode_hlt_draw(int x, int y, int w, int h, float alpha)
288 {
289         GLint pts[3][2];
290
291         viconutil_set_point(pts[0], x+w/2, y+h-2);
292         viconutil_set_point(pts[1], x+3, y+4);
293         viconutil_set_point(pts[2], x+w-3, y+4);
294
295         glColor4f(0.5, 0.5, 0.5, alpha);
296         viconutil_draw_tri(pts);
297
298         glColor4f(0.0, 0.0, 0.0, 1);
299         viconutil_draw_lineloop_smooth(pts, 3);
300
301         glColor3f(1, 1, 0.0);
302         viconutil_draw_points(pts, 3, 1);
303 }
304
305 static void vicon_editmode_dehlt_draw(int x, int y, int w, int h, float alpha)
306 {
307         GLint pts[3][2];
308
309         viconutil_set_point(pts[0], x+w/2, y+h-2);
310         viconutil_set_point(pts[1], x+3, y+4);
311         viconutil_set_point(pts[2], x+w-3, y+4);
312
313         glColor4f(0.0f, 0.0f, 0.0f, 1);
314         viconutil_draw_lineloop_smooth(pts, 3);
315
316         glColor3f(.9f, .9f, .9f);
317         viconutil_draw_points(pts, 3, 1);
318 }
319
320 static void vicon_disclosure_tri_right_draw(int x, int y, int w, int h, float alpha)
321 {
322         GLint pts[3][2];
323         int cx = x+w/2;
324         int cy = y+w/2;
325         int d = w/3, d2 = w/5;
326
327         viconutil_set_point(pts[0], cx-d2, cy+d);
328         viconutil_set_point(pts[1], cx-d2, cy-d);
329         viconutil_set_point(pts[2], cx+d2, cy);
330
331         glShadeModel(GL_SMOOTH);
332         glBegin(GL_TRIANGLES);
333         glColor4f(0.8f, 0.8f, 0.8f, alpha);
334         glVertex2iv(pts[0]);
335         glVertex2iv(pts[1]);
336         glColor4f(0.3f, 0.3f, 0.3f, alpha);
337         glVertex2iv(pts[2]);
338         glEnd();
339         glShadeModel(GL_FLAT);
340
341         glColor4f(0.0f, 0.0f, 0.0f, 1);
342         viconutil_draw_lineloop_smooth(pts, 3);
343 }
344
345 static void vicon_disclosure_tri_down_draw(int x, int y, int w, int h, float alpha)
346 {
347         GLint pts[3][2];
348         int cx = x+w/2;
349         int cy = y+w/2;
350         int d = w/3, d2 = w/5;
351
352         viconutil_set_point(pts[0], cx+d, cy+d2);
353         viconutil_set_point(pts[1], cx-d, cy+d2);
354         viconutil_set_point(pts[2], cx, cy-d2);
355
356         glShadeModel(GL_SMOOTH);
357         glBegin(GL_TRIANGLES);
358         glColor4f(0.8f, 0.8f, 0.8f, alpha);
359         glVertex2iv(pts[0]);
360         glVertex2iv(pts[1]);
361         glColor4f(0.3f, 0.3f, 0.3f, alpha);
362         glVertex2iv(pts[2]);
363         glEnd();
364         glShadeModel(GL_FLAT);
365
366         glColor4f(0.0f, 0.0f, 0.0f, 1);
367         viconutil_draw_lineloop_smooth(pts, 3);
368 }
369
370 static void vicon_move_up_draw(int x, int y, int w, int h, float alpha)
371 {
372         int d=-2;
373
374         glEnable(GL_LINE_SMOOTH);
375         glLineWidth(1);
376         glColor3f(0.0, 0.0, 0.0);
377
378         glBegin(GL_LINE_STRIP);
379         glVertex2i(x+w/2-d*2, y+h/2+d);
380         glVertex2i(x+w/2, y+h/2-d + 1);
381         glVertex2i(x+w/2+d*2, y+h/2+d);
382         glEnd();
383
384         glLineWidth(1.0);
385         glDisable(GL_LINE_SMOOTH);
386 }
387
388 static void vicon_move_down_draw(int x, int y, int w, int h, float alpha)
389 {
390         int d=2;
391
392         glEnable(GL_LINE_SMOOTH);
393         glLineWidth(1);
394         glColor3f(0.0, 0.0, 0.0);
395
396         glBegin(GL_LINE_STRIP);
397         glVertex2i(x+w/2-d*2, y+h/2+d);
398         glVertex2i(x+w/2, y+h/2-d - 1);
399         glVertex2i(x+w/2+d*2, y+h/2+d);
400         glEnd();
401
402         glLineWidth(1.0);
403         glDisable(GL_LINE_SMOOTH);
404 }
405
406 static void init_internal_icons()
407 {
408         bTheme *btheme= U.themes.first;
409         ImBuf *bbuf= NULL;
410         int x, y;
411         char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
412         char filenamestr[FILE_MAXFILE+16];      // 16 == strlen(".blender/icons/")+1
413         
414         if ((btheme!=NULL) && (strlen(btheme->tui.iconfile) > 0)) {
415         
416 #ifdef WIN32
417                 sprintf(filenamestr, "icons/%s", btheme->tui.iconfile);
418 #else
419                 sprintf(filenamestr, ".blender/icons/%s", btheme->tui.iconfile);
420 #endif
421                 
422                 BLI_make_file_string("/", iconfilestr, BLI_gethome(), filenamestr);
423                 
424                 if (BLI_exists(iconfilestr)) {
425                         bbuf = IMB_loadiffname(iconfilestr, IB_rect);
426                         if(bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H) {
427                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
428                                 IMB_freeImBuf(bbuf);
429                                 bbuf= NULL;
430                 }
431         }
432         }
433         if(bbuf==NULL)
434                 bbuf = IMB_ibImageFromMemory((int *)datatoc_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
435
436         for (y=0; y<ICON_GRID_ROWS; y++) {
437                 for (x=0; x<ICON_GRID_COLS; x++) {
438                         def_internal_icon(bbuf, BIFICONID_FIRST + y*ICON_GRID_COLS + x,
439                                 x*(ICON_GRID_W+ICON_GRID_MARGIN)+ICON_GRID_MARGIN,
440                                 y*(ICON_GRID_H+ICON_GRID_MARGIN)+ICON_GRID_MARGIN, ICON_GRID_W);
441                 }
442         }
443
444         def_internal_vicon(VICON_VIEW3D, vicon_view3d_draw);
445         def_internal_vicon(VICON_EDIT, vicon_edit_draw);
446         def_internal_vicon(VICON_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
447         def_internal_vicon(VICON_EDITMODE_HLT, vicon_editmode_hlt_draw);
448         def_internal_vicon(VICON_DISCLOSURE_TRI_RIGHT, vicon_disclosure_tri_right_draw);
449         def_internal_vicon(VICON_DISCLOSURE_TRI_DOWN, vicon_disclosure_tri_down_draw);
450         def_internal_vicon(VICON_MOVE_UP, vicon_move_up_draw);
451         def_internal_vicon(VICON_MOVE_DOWN, vicon_move_down_draw);
452         def_internal_vicon(VICON_X, vicon_x_draw);
453
454         IMB_freeImBuf(bbuf);
455 }
456
457
458 static void init_iconfile_list(struct ListBase *list)
459 {
460         IconFile *ifile;
461         ImBuf *bbuf= NULL;
462         struct direntry *dir;
463         int restoredir = 1; /* restore to current directory */
464         int totfile, i, index=1;
465         int ifilex, ifiley;
466         char icondirstr[FILE_MAX];
467         char iconfilestr[FILE_MAX+16]; /* allow 256 chars for file+dir */
468         char olddir[FILE_MAX];
469         
470         list->first = list->last = NULL;
471
472 #ifdef WIN32
473         BLI_make_file_string("/", icondirstr, BLI_gethome(), "icons");
474 #else
475         BLI_make_file_string("/", icondirstr, BLI_gethome(), ".blender/icons");
476 #endif
477         
478         if(BLI_exists(icondirstr)==0)
479                 return;
480         
481         /* since BLI_getdir changes the current working directory, restore it 
482            back to old value afterwards */
483         if(!BLI_getwdN(olddir)) 
484                 restoredir = 0;
485         totfile = BLI_getdir(icondirstr, &dir);
486         if (restoredir)
487                 chdir(olddir);
488
489         for(i=0; i<totfile; i++) {
490                 if( (dir[i].type & S_IFREG) ) {
491                         char *filename = dir[i].relname;
492                         
493                         if(BLI_testextensie(filename, ".png")) {
494                         
495                                 /* check to see if the image is the right size, continue if not */
496                                 /* copying strings here should go ok, assuming that we never get back
497                                    a complete path to file longer than 256 chars */
498                                 sprintf(iconfilestr, "%s/%s", icondirstr, filename);
499                                 if(BLI_exists(iconfilestr)) bbuf = IMB_loadiffname(iconfilestr, IB_rect);
500                                 
501                                 ifilex = bbuf->x;
502                                 ifiley = bbuf->y;
503                                 IMB_freeImBuf(bbuf);
504                                 
505                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H))
506                                         continue;
507                         
508                                 /* found a potential icon file, so make an entry for it in the cache list */
509                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
510                                 
511                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
512                                 ifile->index = index;
513
514                                 BLI_addtail(list, ifile);
515                                 
516                                 index++;
517                         }
518                 }
519         }
520         
521         /* free temporary direntry structure that's been created by BLI_getdir() */
522         i= totfile-1;
523         
524         for(; i>=0; i--){
525                 MEM_freeN(dir[i].relname);
526                 if (dir[i].string) MEM_freeN(dir[i].string);
527         }
528         free(dir);
529         dir= 0;
530 }
531
532 static void free_iconfile_list(struct ListBase *list)
533 {
534         IconFile *ifile=NULL, *next_ifile=NULL;
535         
536         for(ifile=list->first; ifile; ifile=next_ifile) {
537                 next_ifile = ifile->next;
538                 BLI_freelinkN(list, ifile);
539         }
540 }
541
542 int UI_iconfile_get_index(char *filename)
543 {
544         IconFile *ifile;
545         ListBase *list=&(iconfilelist);
546         
547         for(ifile=list->first; ifile; ifile=ifile->next) {
548                 if ( BLI_streq(filename, ifile->filename)) {
549                         return ifile->index;
550                 }
551         }
552         
553         return 0;
554 }
555
556 ListBase *UI_iconfile_list(void)
557 {
558         ListBase *list=&(iconfilelist);
559         
560         return list;
561 }
562
563
564 void UI_icons_free()
565 {
566         free_iconfile_list(&iconfilelist);
567         BKE_icons_free();
568 }
569
570 void UI_icons_free_drawinfo(void *drawinfo)
571 {
572         DrawInfo *di = drawinfo;
573
574         if (di)
575         {
576                 if (di->icon) {
577                         MEM_freeN(di->icon->rect);
578                         MEM_freeN(di->icon);
579                 }
580                 MEM_freeN(di);
581         }
582 }
583
584 static DrawInfo *icon_create_drawinfo()
585 {
586         DrawInfo *di = NULL;
587
588         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
589         
590         di->drawFunc = 0;
591         di->w = ICON_DEFAULT_HEIGHT;
592         di->h = ICON_DEFAULT_HEIGHT;
593         di->icon = NULL;
594         di->aspect = 1.0f;
595
596         return di;
597 }
598
599 int UI_icon_get_width(int icon_id)
600 {
601         Icon *icon = NULL;
602         DrawInfo *di = NULL;
603
604         icon = BKE_icon_get(icon_id);
605         
606         if (!icon) {
607                 printf("UI_icon_get_width: Internal error, no icon for icon ID: %d\n", icon_id);
608                 return 0;
609         }
610         
611         di = (DrawInfo *)icon->drawinfo;
612         if (!di) {
613                 di = icon_create_drawinfo();
614                 icon->drawinfo = di;
615         }
616
617         if (di)
618                 return di->w;
619
620         return 0;
621 }
622
623 int UI_icon_get_height(int icon_id)
624 {
625         Icon *icon = NULL;
626         DrawInfo *di = NULL;
627
628         icon = BKE_icon_get(icon_id);
629         
630         if (!icon) {
631                 printf("UI_icon_get_height: Internal error, no icon for icon ID: %d\n", icon_id);
632                 return 0;
633         }
634         
635         di = (DrawInfo*)icon->drawinfo;
636
637         if (!di) {
638                 di = icon_create_drawinfo();
639                 icon->drawinfo = di;
640         }
641         
642         if (di)
643                 return di->h;
644
645         return 0;
646 }
647
648 void UI_icons_init(int first_dyn_id)
649 {
650         init_iconfile_list(&iconfilelist);
651         BKE_icons_init(first_dyn_id);
652         init_internal_icons();
653 }
654
655 static void icon_copy_rect(ImBuf *ibuf, unsigned int w, unsigned int h, unsigned int *rect)
656 {
657         struct ImBuf *ima;
658         unsigned int *drect, *srect;
659         float scaledx, scaledy;
660         short ex, ey, dx, dy;
661
662         /* paranoia test */
663         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
664                 return;
665         
666         /* waste of cpu cyles... but the imbuf API has no other way to scale fast (ton) */
667         ima = IMB_dupImBuf(ibuf);
668         
669         if (!ima) 
670                 return;
671         
672         if (ima->x > ima->y) {
673                 scaledx = (float)w;
674                 scaledy =  ( (float)ima->y/(float)ima->x )*(float)w;
675         }
676         else {                  
677                 scaledx =  ( (float)ima->x/(float)ima->y )*(float)h;
678                 scaledy = (float)h;
679         }
680         
681         ex = (short)scaledx;
682         ey = (short)scaledy;
683         
684         dx = (w - ex) / 2;
685         dy = (h - ey) / 2;
686         
687         IMB_scalefastImBuf(ima, ex, ey);
688         
689         /* if needed, convert to 32 bits */
690         if(ima->rect==NULL)
691                 IMB_rect_from_float(ima);
692
693         srect = ima->rect;
694         drect = rect;
695
696         drect+= dy*w+dx;
697         for (;ey > 0; ey--){            
698                 memcpy(drect,srect, ex * sizeof(int));
699                 drect += w;
700                 srect += ima->x;
701         }
702         IMB_freeImBuf(ima);
703 }
704
705 /* Render size for preview images at level miplevel */
706 static int preview_render_size(int miplevel)
707 {
708         switch (miplevel) {
709                 case 0: return 32;
710                 case 1: return PREVIEW_DEFAULT_HEIGHT;
711         }
712         return 0;
713 }
714
715 static void icon_create_mipmap(struct PreviewImage* prv_img, int miplevel) 
716 {
717         unsigned int size = preview_render_size(miplevel);
718
719         if (!prv_img) {
720                 printf("Error: requested preview image does not exist");
721         }
722         if (!prv_img->rect[miplevel]) {
723                 prv_img->w[miplevel] = size;
724                 prv_img->h[miplevel] = size;
725                 prv_img->changed[miplevel] = 1;
726                 prv_img->rect[miplevel] = MEM_callocN(size*size*sizeof(unsigned int), "prv_rect"); 
727         }
728 }
729
730 /* create single icon from jpg, png etc. */
731 static void icon_from_image(Image *img, int miplevel)
732 {
733         unsigned int pr_size;
734         short image_loaded = 0;
735         struct ImBuf* ibuf=NULL;
736         PreviewImage* pi;
737
738         /* img->ok is zero when Image cannot load */
739         if (img==NULL || img->ok==0)
740                 return;
741
742         /* elubie: this needs to be changed: here image is always loaded if not
743            already there. Very expensive for large images. Need to find a way to 
744            only get existing ibuf */
745         ibuf = BKE_image_get_ibuf(img, NULL);
746         if(ibuf==NULL || ibuf->rect==NULL) {
747                 return;
748         }
749         
750         pi = BKE_previewimg_get((ID*)img);      
751         
752         if(!pi) {
753                 printf("preview image could'nt be allocated");
754                 return;
755         }
756         /* we can only create the preview rect here, since loading possibly deallocated
757            old preview */
758         icon_create_mipmap(pi, miplevel);
759
760         pr_size = img->preview->w[miplevel]*img->preview->h[miplevel]*sizeof(unsigned int);
761
762         image_loaded = 1;
763         icon_copy_rect(ibuf, img->preview->w[miplevel], img->preview->h[miplevel], img->preview->rect[miplevel]);       
764 }
765
766 static void set_alpha(char* cp, int sizex, int sizey, char alpha) 
767 {
768         int x,y;
769         for(y=0; y<sizey; y++) {
770                 for(x=0; x<sizex; x++, cp+=4) {
771                         cp[3]= alpha;
772                 }
773         }
774 }
775
776 /* only called when icon has changed */
777 /* only call with valid pointer from UI_icon_draw */
778 static void icon_set_image(Scene *scene, ID *id, PreviewImage* prv_img, int miplevel)
779 {
780         RenderInfo ri;  
781         unsigned int pr_size = 0;
782         
783         if (!prv_img) {
784                 printf("No preview image for this ID: %s\n", id->name);
785                 return;
786         }       
787
788         /* no drawing (see last parameter doDraw, just calculate preview image 
789                 - hopefully small enough to be fast */
790         if (GS(id->name) == ID_IM)
791                 icon_from_image((struct Image*)id, miplevel);
792         else {  
793                 /* create the preview rect */
794                 icon_create_mipmap(prv_img, miplevel);
795
796                 ri.curtile= 0;
797                 ri.tottile= 0;
798                 ri.pr_rectx = prv_img->w[miplevel];
799                 ri.pr_recty = prv_img->h[miplevel];
800                 pr_size = ri.pr_rectx*ri.pr_recty*sizeof(unsigned int);
801                 ri.rect = MEM_callocN(pr_size, "pr icon rect");
802
803                 ED_preview_iconrender(scene, id, ri.rect, ri.pr_rectx, ri.pr_recty);
804
805                 /* world is rendered with alpha=0, so it wasn't displayed 
806                    this could be render option for sky to, for later */
807                 if (GS(id->name) == ID_WO) { 
808                         set_alpha( (char*) ri.rect, ri.pr_rectx, ri.pr_recty, 255);
809                 } 
810                 else if (GS(id->name) == ID_MA) {
811                         Material* mat = (Material*)id;
812                         if (mat->material_type == MA_TYPE_HALO) {
813                                 set_alpha( (char*) ri.rect, ri.pr_rectx, ri.pr_recty, 255);
814                         }
815                 }
816
817                 memcpy(prv_img->rect[miplevel], ri.rect, pr_size);
818
819                 /* and clean up */
820                 MEM_freeN(ri.rect);
821         }
822 }
823
824 static void icon_draw_rect(float x, float y, int w, int h, float aspect, int rw, int rh, unsigned int *rect)
825 {
826         
827         glRasterPos2f(x, y);
828         // XXX ui_rasterpos_safe(x, y, aspect);
829         
830         if((w<1 || h<1)) {
831                 // XXX - TODO 2.5 verify whether this case can happen
832                 // and only print in debug
833                 printf("what the heck! - icons are %i x %i pixels?\n", w, h);
834         }
835         /* rect contains image in 'rendersize', we only scale if needed */
836         else if(rw!=w && rh!=h) {
837                 ImBuf *ima;
838                 if(w>2000 || h>2000) { /* something has gone wrong! */
839                         printf("insane icon size w=%d h=%d\n",w,h);
840                         return;
841                 }
842                 /* first allocate imbuf for scaling and copy preview into it */
843                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect, 0);
844                 memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));    
845                 
846                 /* scale it */
847                 IMB_scaleImBuf(ima, w, h);
848                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, ima->rect);
849                 
850                 IMB_freeImBuf(ima);
851         }
852         else
853                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
854 }
855
856 /* Drawing size for preview images at level miplevel */
857 static int preview_size(int miplevel)
858 {
859         switch (miplevel) {
860                 case 0: return ICON_DEFAULT_HEIGHT;
861                 case 1: return PREVIEW_DEFAULT_HEIGHT;
862         }
863         return 0;
864 }
865
866 static void icon_draw_size(float x, float y, int icon_id, float aspect, int miplevel, int draw_size, int nocreate)
867 {
868         Icon *icon = NULL;
869         DrawInfo *di = NULL;
870         
871         icon = BKE_icon_get(icon_id);
872         
873         if (!icon) {
874                 printf("icon_draw_mipmap: Internal error, no icon for icon ID: %d\n", icon_id);
875                 return;
876         }
877         
878         di = (DrawInfo*)icon->drawinfo;
879         
880         if (!di) {
881                 di = icon_create_drawinfo();
882         
883                 icon->drawinfo = di;            
884                 icon->drawinfo_free = UI_icons_free_drawinfo;           
885         }
886         
887         di->aspect = aspect;
888         /* scale width and height according to aspect */
889         di->w = (int)(draw_size/di->aspect + 0.5f);
890         di->h = (int)(draw_size/di->aspect + 0.5f);
891         
892         if (di->drawFunc) {
893                 /* vector icons use the uiBlock transformation, they are not drawn
894                 with untransformed coordinates like the other icons */
895                 di->drawFunc(x, y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f); 
896         } 
897         else if (di->icon) {
898                 /* it is a builtin icon */              
899                 if (!di->icon->rect) return; /* something has gone wrong! */
900
901                 icon_draw_rect(x,y,di->w, di->h, di->aspect, di->icon->w, di->icon->h, di->icon->rect);
902         }
903         else {
904                 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj); 
905
906                 if (pi) {                       
907                         /* no create icon on this level in code */
908                         
909                         if (!pi->rect[miplevel]) return; /* something has gone wrong! */
910                         
911                         icon_draw_rect(x,y,di->w, di->h, di->aspect, pi->w[miplevel], pi->h[miplevel], pi->rect[miplevel]);             
912                 }
913         }
914 }
915
916 void ui_id_icon_render(Scene *scene, ID *id)
917 {
918         PreviewImage *pi = BKE_previewimg_get(id); 
919                 
920         if (pi) {                       
921                 if ((pi->changed[0] ||!pi->rect[0])) /* changed only ever set by dynamic icons */
922                 {
923                         /* create the preview rect if necessary */                              
924                         icon_set_image(scene, id, pi, 0);
925                         pi->changed[0] = 0;
926                 }
927         }
928 }
929
930 int ui_id_icon_get(Scene *scene, ID *id)
931 {
932         int iconid= 0;
933         
934         /* icon */
935         switch(GS(id->name))
936         {
937                 case ID_MA: /* fall through */
938                 case ID_TE: /* fall through */
939                 case ID_IM: /* fall through */
940                 case ID_WO: /* fall through */
941                 case ID_LA: /* fall through */
942                         iconid= BKE_icon_getid(id);
943                         /* checks if not exists, or changed */
944                         ui_id_icon_render(scene, id);
945                         break;
946                 default:
947                         break;
948         }
949
950         return iconid;
951 }
952
953 static void icon_draw_mipmap(float x, float y, int icon_id, float aspect, int miplevel, int nocreate)
954 {
955         int draw_size = preview_size(miplevel);
956         icon_draw_size(x,y,icon_id, aspect, miplevel, draw_size, nocreate);
957 }
958
959
960 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect)
961 {
962         icon_draw_mipmap(x,y,icon_id, aspect, PREVIEW_MIPMAP_ZERO, 0);
963 }
964
965 void UI_icon_draw(float x, float y, int icon_id)
966 {
967         UI_icon_draw_aspect(x, y, icon_id, 1.0f);
968 }
969
970 void UI_icon_draw_size_blended(float x, float y, int size, int icon_id, int shade)
971 {
972         if(shade < 0) {
973                 float r= (128+shade)/128.0f;
974                 glPixelTransferf(GL_ALPHA_SCALE, r);
975         }
976
977         icon_draw_size(x,y,icon_id, 1.0f, 0, size, 1);
978
979         if(shade < 0)
980                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
981 }
982
983 void UI_icon_draw_preview(float x, float y, int icon_id, int nocreate)
984 {
985         icon_draw_mipmap(x,y,icon_id, 1.0f, PREVIEW_MIPMAP_LARGE, nocreate);
986 }
987
988 void UI_icon_draw_aspect_blended(float x, float y, int icon_id, float aspect, int shade)
989 {
990         if(shade < 0) {
991                 float r= (128+shade)/128.0f;
992                 glPixelTransferf(GL_ALPHA_SCALE, r);
993         }
994
995         UI_icon_draw_aspect(x, y, icon_id, aspect);
996
997         if(shade < 0)
998                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
999 }