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