Merge with 2.5 -r 21003:21788.
[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_small_tri_right_draw(int x, int y, int w, int h, float alpha)
346 {
347         GLint pts[3][2];
348         int cx = x+w/2-4;
349         int cy = y+w/2;
350         int d = w/5, d2 = w/7;
351
352         viconutil_set_point(pts[0], cx-d2, cy+d);
353         viconutil_set_point(pts[1], cx-d2, cy-d);
354         viconutil_set_point(pts[2], cx+d2, cy);
355
356         glColor4f(0.2f, 0.2f, 0.2f, alpha);
357
358         glShadeModel(GL_SMOOTH);
359         glBegin(GL_TRIANGLES);
360         glVertex2iv(pts[0]);
361         glVertex2iv(pts[1]);
362         glVertex2iv(pts[2]);
363         glEnd();
364         glShadeModel(GL_FLAT);
365 }
366
367 static void vicon_disclosure_tri_down_draw(int x, int y, int w, int h, float alpha)
368 {
369         GLint pts[3][2];
370         int cx = x+w/2;
371         int cy = y+w/2;
372         int d = w/3, d2 = w/5;
373
374         viconutil_set_point(pts[0], cx+d, cy+d2);
375         viconutil_set_point(pts[1], cx-d, cy+d2);
376         viconutil_set_point(pts[2], cx, cy-d2);
377
378         glShadeModel(GL_SMOOTH);
379         glBegin(GL_TRIANGLES);
380         glColor4f(0.8f, 0.8f, 0.8f, alpha);
381         glVertex2iv(pts[0]);
382         glVertex2iv(pts[1]);
383         glColor4f(0.3f, 0.3f, 0.3f, alpha);
384         glVertex2iv(pts[2]);
385         glEnd();
386         glShadeModel(GL_FLAT);
387
388         glColor4f(0.0f, 0.0f, 0.0f, 1);
389         viconutil_draw_lineloop_smooth(pts, 3);
390 }
391
392 static void vicon_move_up_draw(int x, int y, int w, int h, float alpha)
393 {
394         int d=-2;
395
396         glEnable(GL_LINE_SMOOTH);
397         glLineWidth(1);
398         glColor3f(0.0, 0.0, 0.0);
399
400         glBegin(GL_LINE_STRIP);
401         glVertex2i(x+w/2-d*2, y+h/2+d);
402         glVertex2i(x+w/2, y+h/2-d + 1);
403         glVertex2i(x+w/2+d*2, y+h/2+d);
404         glEnd();
405
406         glLineWidth(1.0);
407         glDisable(GL_LINE_SMOOTH);
408 }
409
410 static void vicon_move_down_draw(int x, int y, int w, int h, float alpha)
411 {
412         int d=2;
413
414         glEnable(GL_LINE_SMOOTH);
415         glLineWidth(1);
416         glColor3f(0.0, 0.0, 0.0);
417
418         glBegin(GL_LINE_STRIP);
419         glVertex2i(x+w/2-d*2, y+h/2+d);
420         glVertex2i(x+w/2, y+h/2-d - 1);
421         glVertex2i(x+w/2+d*2, y+h/2+d);
422         glEnd();
423
424         glLineWidth(1.0);
425         glDisable(GL_LINE_SMOOTH);
426 }
427
428 static void init_internal_icons()
429 {
430         bTheme *btheme= U.themes.first;
431         ImBuf *bbuf= NULL;
432         int x, y;
433         char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
434         char filenamestr[FILE_MAXFILE+16];      // 16 == strlen(".blender/icons/")+1
435         
436         if ((btheme!=NULL) && (strlen(btheme->tui.iconfile) > 0)) {
437         
438 #ifdef WIN32
439                 sprintf(filenamestr, "icons/%s", btheme->tui.iconfile);
440 #else
441                 sprintf(filenamestr, ".blender/icons/%s", btheme->tui.iconfile);
442 #endif
443                 
444                 BLI_make_file_string("/", iconfilestr, BLI_gethome(), filenamestr);
445                 
446                 if (BLI_exists(iconfilestr)) {
447                         bbuf = IMB_loadiffname(iconfilestr, IB_rect);
448                         if(bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H) {
449                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
450                                 IMB_freeImBuf(bbuf);
451                                 bbuf= NULL;
452                 }
453         }
454         }
455         if(bbuf==NULL)
456                 bbuf = IMB_ibImageFromMemory((int *)datatoc_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
457
458         for (y=0; y<ICON_GRID_ROWS; y++) {
459                 for (x=0; x<ICON_GRID_COLS; x++) {
460                         def_internal_icon(bbuf, BIFICONID_FIRST + y*ICON_GRID_COLS + x,
461                                 x*(ICON_GRID_W+ICON_GRID_MARGIN)+ICON_GRID_MARGIN,
462                                 y*(ICON_GRID_H+ICON_GRID_MARGIN)+ICON_GRID_MARGIN, ICON_GRID_W);
463                 }
464         }
465
466         def_internal_vicon(VICON_VIEW3D, vicon_view3d_draw);
467         def_internal_vicon(VICON_EDIT, vicon_edit_draw);
468         def_internal_vicon(VICON_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
469         def_internal_vicon(VICON_EDITMODE_HLT, vicon_editmode_hlt_draw);
470         def_internal_vicon(VICON_DISCLOSURE_TRI_RIGHT, vicon_disclosure_tri_right_draw);
471         def_internal_vicon(VICON_DISCLOSURE_TRI_DOWN, vicon_disclosure_tri_down_draw);
472         def_internal_vicon(VICON_MOVE_UP, vicon_move_up_draw);
473         def_internal_vicon(VICON_MOVE_DOWN, vicon_move_down_draw);
474         def_internal_vicon(VICON_X, vicon_x_draw);
475         def_internal_vicon(VICON_SMALL_TRI_RIGHT, vicon_small_tri_right_draw);
476
477         IMB_freeImBuf(bbuf);
478 }
479
480
481 static void init_iconfile_list(struct ListBase *list)
482 {
483         IconFile *ifile;
484         ImBuf *bbuf= NULL;
485         struct direntry *dir;
486         int restoredir = 1; /* restore to current directory */
487         int totfile, i, index=1;
488         int ifilex, ifiley;
489         char icondirstr[FILE_MAX];
490         char iconfilestr[FILE_MAX+16]; /* allow 256 chars for file+dir */
491         char olddir[FILE_MAX];
492         
493         list->first = list->last = NULL;
494
495 #ifdef WIN32
496         BLI_make_file_string("/", icondirstr, BLI_gethome(), "icons");
497 #else
498         BLI_make_file_string("/", icondirstr, BLI_gethome(), ".blender/icons");
499 #endif
500         
501         if(BLI_exists(icondirstr)==0)
502                 return;
503         
504         /* since BLI_getdir changes the current working directory, restore it 
505            back to old value afterwards */
506         if(!BLI_getwdN(olddir)) 
507                 restoredir = 0;
508         totfile = BLI_getdir(icondirstr, &dir);
509         if (restoredir)
510                 chdir(olddir);
511
512         for(i=0; i<totfile; i++) {
513                 if( (dir[i].type & S_IFREG) ) {
514                         char *filename = dir[i].relname;
515                         
516                         if(BLI_testextensie(filename, ".png")) {
517                         
518                                 /* check to see if the image is the right size, continue if not */
519                                 /* copying strings here should go ok, assuming that we never get back
520                                    a complete path to file longer than 256 chars */
521                                 sprintf(iconfilestr, "%s/%s", icondirstr, filename);
522                                 if(BLI_exists(iconfilestr)) bbuf = IMB_loadiffname(iconfilestr, IB_rect);
523                                 
524                                 ifilex = bbuf->x;
525                                 ifiley = bbuf->y;
526                                 IMB_freeImBuf(bbuf);
527                                 
528                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H))
529                                         continue;
530                         
531                                 /* found a potential icon file, so make an entry for it in the cache list */
532                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
533                                 
534                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
535                                 ifile->index = index;
536
537                                 BLI_addtail(list, ifile);
538                                 
539                                 index++;
540                         }
541                 }
542         }
543         
544         /* free temporary direntry structure that's been created by BLI_getdir() */
545         i= totfile-1;
546         
547         for(; i>=0; i--){
548                 MEM_freeN(dir[i].relname);
549                 if (dir[i].string) MEM_freeN(dir[i].string);
550         }
551         free(dir);
552         dir= 0;
553 }
554
555 static void free_iconfile_list(struct ListBase *list)
556 {
557         IconFile *ifile=NULL, *next_ifile=NULL;
558         
559         for(ifile=list->first; ifile; ifile=next_ifile) {
560                 next_ifile = ifile->next;
561                 BLI_freelinkN(list, ifile);
562         }
563 }
564
565 int UI_iconfile_get_index(char *filename)
566 {
567         IconFile *ifile;
568         ListBase *list=&(iconfilelist);
569         
570         for(ifile=list->first; ifile; ifile=ifile->next) {
571                 if ( BLI_streq(filename, ifile->filename)) {
572                         return ifile->index;
573                 }
574         }
575         
576         return 0;
577 }
578
579 ListBase *UI_iconfile_list(void)
580 {
581         ListBase *list=&(iconfilelist);
582         
583         return list;
584 }
585
586
587 void UI_icons_free()
588 {
589         free_iconfile_list(&iconfilelist);
590         BKE_icons_free();
591 }
592
593 void UI_icons_free_drawinfo(void *drawinfo)
594 {
595         DrawInfo *di = drawinfo;
596
597         if (di)
598         {
599                 if (di->icon) {
600                         MEM_freeN(di->icon->rect);
601                         MEM_freeN(di->icon);
602                 }
603                 MEM_freeN(di);
604         }
605 }
606
607 static DrawInfo *icon_create_drawinfo()
608 {
609         DrawInfo *di = NULL;
610
611         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
612         
613         di->drawFunc = 0;
614         di->w = ICON_DEFAULT_HEIGHT;
615         di->h = ICON_DEFAULT_HEIGHT;
616         di->icon = NULL;
617         di->aspect = 1.0f;
618
619         return di;
620 }
621
622 int UI_icon_get_width(int icon_id)
623 {
624         Icon *icon = NULL;
625         DrawInfo *di = NULL;
626
627         icon = BKE_icon_get(icon_id);
628         
629         if (!icon) {
630                 printf("UI_icon_get_width: Internal error, no icon for icon ID: %d\n", icon_id);
631                 return 0;
632         }
633         
634         di = (DrawInfo *)icon->drawinfo;
635         if (!di) {
636                 di = icon_create_drawinfo();
637                 icon->drawinfo = di;
638         }
639
640         if (di)
641                 return di->w;
642
643         return 0;
644 }
645
646 int UI_icon_get_height(int icon_id)
647 {
648         Icon *icon = NULL;
649         DrawInfo *di = NULL;
650
651         icon = BKE_icon_get(icon_id);
652         
653         if (!icon) {
654                 printf("UI_icon_get_height: Internal error, no icon for icon ID: %d\n", icon_id);
655                 return 0;
656         }
657         
658         di = (DrawInfo*)icon->drawinfo;
659
660         if (!di) {
661                 di = icon_create_drawinfo();
662                 icon->drawinfo = di;
663         }
664         
665         if (di)
666                 return di->h;
667
668         return 0;
669 }
670
671 void UI_icons_init(int first_dyn_id)
672 {
673         init_iconfile_list(&iconfilelist);
674         BKE_icons_init(first_dyn_id);
675         init_internal_icons();
676 }
677
678 static void icon_copy_rect(ImBuf *ibuf, unsigned int w, unsigned int h, unsigned int *rect)
679 {
680         struct ImBuf *ima;
681         unsigned int *drect, *srect;
682         float scaledx, scaledy;
683         short ex, ey, dx, dy;
684
685         /* paranoia test */
686         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
687                 return;
688         
689         /* waste of cpu cyles... but the imbuf API has no other way to scale fast (ton) */
690         ima = IMB_dupImBuf(ibuf);
691         
692         if (!ima) 
693                 return;
694         
695         if (ima->x > ima->y) {
696                 scaledx = (float)w;
697                 scaledy =  ( (float)ima->y/(float)ima->x )*(float)w;
698         }
699         else {                  
700                 scaledx =  ( (float)ima->x/(float)ima->y )*(float)h;
701                 scaledy = (float)h;
702         }
703         
704         ex = (short)scaledx;
705         ey = (short)scaledy;
706         
707         dx = (w - ex) / 2;
708         dy = (h - ey) / 2;
709         
710         IMB_scalefastImBuf(ima, ex, ey);
711         
712         /* if needed, convert to 32 bits */
713         if(ima->rect==NULL)
714                 IMB_rect_from_float(ima);
715
716         srect = ima->rect;
717         drect = rect;
718
719         drect+= dy*w+dx;
720         for (;ey > 0; ey--){            
721                 memcpy(drect,srect, ex * sizeof(int));
722                 drect += w;
723                 srect += ima->x;
724         }
725         IMB_freeImBuf(ima);
726 }
727
728 /* Render size for preview images at level miplevel */
729 static int preview_render_size(int miplevel)
730 {
731         switch (miplevel) {
732                 case 0: return 32;
733                 case 1: return PREVIEW_DEFAULT_HEIGHT;
734         }
735         return 0;
736 }
737
738 static void icon_create_mipmap(struct PreviewImage* prv_img, int miplevel) 
739 {
740         unsigned int size = preview_render_size(miplevel);
741
742         if (!prv_img) {
743                 printf("Error: requested preview image does not exist");
744         }
745         if (!prv_img->rect[miplevel]) {
746                 prv_img->w[miplevel] = size;
747                 prv_img->h[miplevel] = size;
748                 prv_img->changed[miplevel] = 1;
749                 prv_img->rect[miplevel] = MEM_callocN(size*size*sizeof(unsigned int), "prv_rect"); 
750         }
751 }
752
753 /* create single icon from jpg, png etc. */
754 static void icon_from_image(Scene *scene, Image *img, int miplevel)
755 {
756         ImBuf *ibuf= NULL;
757         ImageUser iuser;
758         PreviewImage *pi;
759         unsigned int pr_size;
760         short image_loaded = 0;
761
762         /* img->ok is zero when Image cannot load */
763         if (img==NULL || img->ok==0)
764                 return;
765
766         /* setup dummy image user */
767         memset(&iuser, 0, sizeof(ImageUser));
768         iuser.ok= iuser.framenr= 1;
769         iuser.scene= scene;
770         
771         /* elubie: this needs to be changed: here image is always loaded if not
772            already there. Very expensive for large images. Need to find a way to 
773            only get existing ibuf */
774         
775         ibuf = BKE_image_get_ibuf(img, &iuser);
776         if(ibuf==NULL || ibuf->rect==NULL) {
777                 return;
778         }
779         
780         pi = BKE_previewimg_get((ID*)img);      
781         
782         if(!pi) {
783                 printf("preview image could'nt be allocated");
784                 return;
785         }
786         /* we can only create the preview rect here, since loading possibly deallocated
787            old preview */
788         icon_create_mipmap(pi, miplevel);
789
790         pr_size = img->preview->w[miplevel]*img->preview->h[miplevel]*sizeof(unsigned int);
791
792         image_loaded = 1;
793         icon_copy_rect(ibuf, img->preview->w[miplevel], img->preview->h[miplevel], img->preview->rect[miplevel]);       
794 }
795
796 static void set_alpha(char* cp, int sizex, int sizey, char alpha) 
797 {
798         int x,y;
799         for(y=0; y<sizey; y++) {
800                 for(x=0; x<sizex; x++, cp+=4) {
801                         cp[3]= alpha;
802                 }
803         }
804 }
805
806 /* only called when icon has changed */
807 /* only call with valid pointer from UI_icon_draw */
808 static void icon_set_image(Scene *scene, ID *id, PreviewImage* prv_img, int miplevel)
809 {
810         RenderInfo ri;  
811         unsigned int pr_size = 0;
812         
813         if (!prv_img) {
814                 printf("No preview image for this ID: %s\n", id->name);
815                 return;
816         }       
817
818         /* no drawing (see last parameter doDraw, just calculate preview image 
819                 - hopefully small enough to be fast */
820         if (GS(id->name) == ID_IM)
821                 icon_from_image(scene, (struct Image*)id, miplevel);
822         else {  
823                 /* create the preview rect */
824                 icon_create_mipmap(prv_img, miplevel);
825
826                 ri.curtile= 0;
827                 ri.tottile= 0;
828                 ri.pr_rectx = prv_img->w[miplevel];
829                 ri.pr_recty = prv_img->h[miplevel];
830                 pr_size = ri.pr_rectx*ri.pr_recty*sizeof(unsigned int);
831                 ri.rect = MEM_callocN(pr_size, "pr icon rect");
832
833                 ED_preview_iconrender(scene, id, ri.rect, ri.pr_rectx, ri.pr_recty);
834
835                 /* world is rendered with alpha=0, so it wasn't displayed 
836                    this could be render option for sky to, for later */
837                 if (GS(id->name) == ID_WO) { 
838                         set_alpha( (char*) ri.rect, ri.pr_rectx, ri.pr_recty, 255);
839                 } 
840                 else if (GS(id->name) == ID_MA) {
841                         Material* mat = (Material*)id;
842                         if (mat->material_type == MA_TYPE_HALO) {
843                                 set_alpha( (char*) ri.rect, ri.pr_rectx, ri.pr_recty, 255);
844                         }
845                 }
846
847                 memcpy(prv_img->rect[miplevel], ri.rect, pr_size);
848
849                 /* and clean up */
850                 MEM_freeN(ri.rect);
851         }
852 }
853
854 static void icon_draw_rect(float x, float y, int w, int h, float aspect, int rw, int rh, unsigned int *rect)
855 {
856         
857         glRasterPos2f(x, y);
858         // XXX ui_rasterpos_safe(x, y, aspect);
859         
860         if((w<1 || h<1)) {
861                 // XXX - TODO 2.5 verify whether this case can happen
862                 // and only print in debug
863                 printf("what the heck! - icons are %i x %i pixels?\n", w, h);
864         }
865         /* rect contains image in 'rendersize', we only scale if needed */
866         else if(rw!=w && rh!=h) {
867                 ImBuf *ima;
868                 if(w>2000 || h>2000) { /* something has gone wrong! */
869                         printf("insane icon size w=%d h=%d\n",w,h);
870                         return;
871                 }
872                 /* first allocate imbuf for scaling and copy preview into it */
873                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect, 0);
874                 memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));    
875                 
876                 /* scale it */
877                 IMB_scaleImBuf(ima, w, h);
878                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, ima->rect);
879                 
880                 IMB_freeImBuf(ima);
881         }
882         else
883                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
884 }
885
886 /* Drawing size for preview images at level miplevel */
887 static int preview_size(int miplevel)
888 {
889         switch (miplevel) {
890                 case 0: return ICON_DEFAULT_HEIGHT;
891                 case 1: return PREVIEW_DEFAULT_HEIGHT;
892         }
893         return 0;
894 }
895
896 static void icon_draw_size(float x, float y, int icon_id, float aspect, int miplevel, int draw_size, int nocreate)
897 {
898         Icon *icon = NULL;
899         DrawInfo *di = NULL;
900         
901         icon = BKE_icon_get(icon_id);
902         
903         if (!icon) {
904                 printf("icon_draw_mipmap: Internal error, no icon for icon ID: %d\n", icon_id);
905                 return;
906         }
907         
908         di = (DrawInfo*)icon->drawinfo;
909         
910         if (!di) {
911                 di = icon_create_drawinfo();
912         
913                 icon->drawinfo = di;            
914                 icon->drawinfo_free = UI_icons_free_drawinfo;           
915         }
916         
917         di->aspect = aspect;
918         /* scale width and height according to aspect */
919         di->w = (int)(draw_size/di->aspect + 0.5f);
920         di->h = (int)(draw_size/di->aspect + 0.5f);
921         
922         if (di->drawFunc) {
923                 /* vector icons use the uiBlock transformation, they are not drawn
924                 with untransformed coordinates like the other icons */
925                 di->drawFunc(x, y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f); 
926         } 
927         else if (di->icon) {
928                 /* it is a builtin icon */              
929                 if (!di->icon->rect) return; /* something has gone wrong! */
930
931                 icon_draw_rect(x,y,di->w, di->h, di->aspect, di->icon->w, di->icon->h, di->icon->rect);
932         }
933         else {
934                 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj); 
935
936                 if (pi) {                       
937                         /* no create icon on this level in code */
938                         
939                         if (!pi->rect[miplevel]) return; /* something has gone wrong! */
940                         
941                         icon_draw_rect(x,y,di->w, di->h, di->aspect, pi->w[miplevel], pi->h[miplevel], pi->rect[miplevel]);             
942                 }
943         }
944 }
945
946 void ui_id_icon_render(Scene *scene, ID *id)
947 {
948         PreviewImage *pi = BKE_previewimg_get(id); 
949                 
950         if (pi) {                       
951                 if ((pi->changed[0] ||!pi->rect[0])) /* changed only ever set by dynamic icons */
952                 {
953                         /* create the preview rect if necessary */                              
954                         icon_set_image(scene, id, pi, 0);
955                         pi->changed[0] = 0;
956                 }
957         }
958 }
959
960 int ui_id_icon_get(Scene *scene, ID *id)
961 {
962         int iconid= 0;
963         
964         /* icon */
965         switch(GS(id->name))
966         {
967                 case ID_MA: /* fall through */
968                 case ID_TE: /* fall through */
969                 case ID_IM: /* fall through */
970                 case ID_WO: /* fall through */
971                 case ID_LA: /* fall through */
972                         iconid= BKE_icon_getid(id);
973                         /* checks if not exists, or changed */
974                         ui_id_icon_render(scene, id);
975                         break;
976                 default:
977                         break;
978         }
979
980         return iconid;
981 }
982
983 static void icon_draw_mipmap(float x, float y, int icon_id, float aspect, int miplevel, int nocreate)
984 {
985         int draw_size = preview_size(miplevel);
986         icon_draw_size(x,y,icon_id, aspect, miplevel, draw_size, nocreate);
987 }
988
989
990 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect)
991 {
992         icon_draw_mipmap(x,y,icon_id, aspect, PREVIEW_MIPMAP_ZERO, 0);
993 }
994
995 void UI_icon_draw(float x, float y, int icon_id)
996 {
997         UI_icon_draw_aspect(x, y, icon_id, 1.0f);
998 }
999
1000 void UI_icon_draw_size_blended(float x, float y, int size, int icon_id, int shade)
1001 {
1002         if(shade < 0) {
1003                 float r= (128+shade)/128.0f;
1004                 glPixelTransferf(GL_ALPHA_SCALE, r);
1005         }
1006
1007         icon_draw_size(x,y,icon_id, 1.0f, 0, size, 1);
1008
1009         if(shade < 0)
1010                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1011 }
1012
1013 void UI_icon_draw_preview(float x, float y, int icon_id, int nocreate)
1014 {
1015         icon_draw_mipmap(x,y,icon_id, 1.0f, PREVIEW_MIPMAP_LARGE, nocreate);
1016 }
1017
1018 void UI_icon_draw_aspect_blended(float x, float y, int icon_id, float aspect, int shade)
1019 {
1020         if(shade < 0) {
1021                 float r= (128+shade)/128.0f;
1022                 glPixelTransferf(GL_ALPHA_SCALE, r);
1023         }
1024
1025         UI_icon_draw_aspect(x, y, icon_id, aspect);
1026
1027         if(shade < 0)
1028                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1029 }