54cfeaecfee80b2fab65831079e97089a49d9e9a
[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 static int preview_render_size(int miplevel);
102
103 /* **************************************************** */
104
105 static void def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs)
106 {
107         Icon *new_icon = NULL;
108         IconImage *iimg = NULL;
109         DrawInfo *di;
110         int y = 0;
111
112         new_icon = MEM_callocN(sizeof(Icon), "texicon");
113
114         new_icon->obj = 0; /* icon is not for library object */
115         new_icon->type = 0;     
116
117         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
118         di->drawFunc = 0;
119         di->w = ICON_DEFAULT_HEIGHT;
120         di->h = ICON_DEFAULT_HEIGHT;
121         di->aspect = 1.0f;
122         
123         iimg = MEM_mallocN(sizeof(IconImage), "icon_img");
124         iimg->rect = MEM_mallocN(ICON_DEFAULT_HEIGHT*ICON_DEFAULT_HEIGHT*sizeof(unsigned int), "icon_rect");
125         iimg->w = ICON_DEFAULT_HEIGHT;
126         iimg->h = ICON_DEFAULT_HEIGHT;
127
128         /* Here we store the rect in the icon - same as before */
129         for (y=0; y<ICON_DEFAULT_HEIGHT; y++) {
130                 memcpy(&iimg->rect[y*ICON_DEFAULT_HEIGHT], &bbuf->rect[(y+yofs)*512+xofs], ICON_DEFAULT_HEIGHT*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 /***/
407
408 static void clear_icon_grid_margins(unsigned char *rect, int w, int h)
409 {
410         int x, y;
411         int xoffs=ICON_GRID_W+ICON_GRID_MARGIN;
412         int yoffs=ICON_GRID_H+ICON_GRID_MARGIN;
413
414         for (y=0; y<h; y++) {
415                 unsigned char *row= &rect[y*w*4];
416
417                 for (x=0; x<w; x++) {
418                         unsigned char *pxl= &row[x*4];
419
420                         if ((x % xoffs < ICON_GRID_MARGIN-2) || (x % xoffs > ICON_GRID_W+2))
421                                 pxl[3] = 0;     //alpha channel == x+3
422                         else if ((y % yoffs < ICON_GRID_MARGIN-2) || (y % yoffs > ICON_GRID_H+2))
423                                 pxl[3] = 0;
424                 }
425         }
426 }
427
428 static void prepare_internal_icons(ImBuf *bbuf)
429 {
430
431         unsigned char *back= (unsigned char *)bbuf->rect;
432         
433         /* this sets the icon grid margin area outside of icon to zero alpha */
434         clear_icon_grid_margins(back, bbuf->x, bbuf->y);
435         
436 }
437
438
439 static void init_internal_icons()
440 {
441         bTheme *btheme= U.themes.first;
442         ImBuf *bbuf;
443         int x, y;
444         char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
445         char filenamestr[FILE_MAXFILE+16];      // 16 == strlen(".blender/icons/")+1
446         
447         if ((btheme!=NULL) && (strlen(btheme->tui.iconfile) > 0)) {
448         
449 #ifdef WIN32
450                 sprintf(filenamestr, "icons/%s", btheme->tui.iconfile);
451 #else
452                 sprintf(filenamestr, ".blender/icons/%s", btheme->tui.iconfile);
453 #endif
454                 
455                 BLI_make_file_string("/", iconfilestr, BLI_gethome(), filenamestr);
456                 
457                 if (BLI_exists(iconfilestr)) {
458                         bbuf = IMB_loadiffname(iconfilestr, IB_rect);
459                 } else {
460                         bbuf = IMB_ibImageFromMemory((int *)datatoc_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
461                 }
462         } else {
463                 bbuf = IMB_ibImageFromMemory((int *)datatoc_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
464         }
465         
466         prepare_internal_icons(bbuf);
467
468         for (y=0; y<ICON_GRID_ROWS; y++) {
469                 for (x=0; x<ICON_GRID_COLS; x++) {
470                         def_internal_icon(bbuf, BIFICONID_FIRST + y*ICON_GRID_COLS + x,
471                                 x*(ICON_GRID_W+ICON_GRID_MARGIN)+3,
472                                 y*(ICON_GRID_H+ICON_GRID_MARGIN)+3);
473                 }
474         }
475
476         def_internal_vicon(VICON_VIEW3D, vicon_view3d_draw);
477         def_internal_vicon(VICON_EDIT, vicon_edit_draw);
478         def_internal_vicon(VICON_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
479         def_internal_vicon(VICON_EDITMODE_HLT, vicon_editmode_hlt_draw);
480         def_internal_vicon(VICON_DISCLOSURE_TRI_RIGHT, vicon_disclosure_tri_right_draw);
481         def_internal_vicon(VICON_DISCLOSURE_TRI_DOWN, vicon_disclosure_tri_down_draw);
482         def_internal_vicon(VICON_MOVE_UP, vicon_move_up_draw);
483         def_internal_vicon(VICON_MOVE_DOWN, vicon_move_down_draw);
484         def_internal_vicon(VICON_X, vicon_x_draw);
485
486         IMB_freeImBuf(bbuf);
487 }
488
489
490 static void init_iconfile_list(struct ListBase *list)
491 {
492         IconFile *ifile;
493         ImBuf *bbuf= NULL;
494         struct direntry *dir;
495         int restoredir = 1; /* restore to current directory */
496         int totfile, i, index=1;
497         int ifilex, ifiley;
498         char icondirstr[FILE_MAX];
499         char iconfilestr[FILE_MAX+16]; /* allow 256 chars for file+dir */
500         char olddir[FILE_MAX];
501         
502         list->first = list->last = NULL;
503
504 #ifdef WIN32
505         BLI_make_file_string("/", icondirstr, BLI_gethome(), "icons");
506 #else
507         BLI_make_file_string("/", icondirstr, BLI_gethome(), ".blender/icons");
508 #endif
509         
510         if(BLI_exists(icondirstr)==0)
511                 return;
512         
513         /* since BLI_getdir changes the current working directory, restore it 
514            back to old value afterwards */
515         if(!BLI_getwdN(olddir)) 
516                 restoredir = 0;
517         totfile = BLI_getdir(icondirstr, &dir);
518         if (restoredir)
519                 chdir(olddir);
520
521         for(i=0; i<totfile; i++) {
522                 if( (dir[i].type & S_IFREG) ) {
523                         char *filename = dir[i].relname;
524                         
525                         if(BLI_testextensie(filename, ".png")) {
526                         
527                                 /* check to see if the image is the right size, continue if not */
528                                 /* copying strings here should go ok, assuming that we never get back
529                                    a complete path to file longer than 256 chars */
530                                 sprintf(iconfilestr, "%s/%s", icondirstr, filename);
531                                 if(BLI_exists(iconfilestr)) bbuf = IMB_loadiffname(iconfilestr, IB_rect);
532                                 
533                                 ifilex = bbuf->x;
534                                 ifiley = bbuf->y;
535                                 IMB_freeImBuf(bbuf);
536                                 
537                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H))
538                                         continue;
539                         
540                                 /* found a potential icon file, so make an entry for it in the cache list */
541                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
542                                 
543                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
544                                 ifile->index = index;
545
546                                 BLI_addtail(list, ifile);
547                                 
548                                 index++;
549                         }
550                 }
551         }
552         
553         /* free temporary direntry structure that's been created by BLI_getdir() */
554         i= totfile-1;
555         
556         for(; i>=0; i--){
557                 MEM_freeN(dir[i].relname);
558                 if (dir[i].string) MEM_freeN(dir[i].string);
559         }
560         free(dir);
561         dir= 0;
562 }
563
564 static void free_iconfile_list(struct ListBase *list)
565 {
566         IconFile *ifile=NULL, *next_ifile=NULL;
567         
568         for(ifile=list->first; ifile; ifile=next_ifile) {
569                 next_ifile = ifile->next;
570                 BLI_freelinkN(list, ifile);
571         }
572 }
573
574 int UI_iconfile_get_index(char *filename)
575 {
576         IconFile *ifile;
577         ListBase *list=&(iconfilelist);
578         
579         for(ifile=list->first; ifile; ifile=ifile->next) {
580                 if ( BLI_streq(filename, ifile->filename)) {
581                         return ifile->index;
582                 }
583         }
584         
585         return 0;
586 }
587
588 ListBase *UI_iconfile_list(void)
589 {
590         ListBase *list=&(iconfilelist);
591         
592         return list;
593 }
594
595
596 void UI_icons_free()
597 {
598         free_iconfile_list(&iconfilelist);
599         BKE_icons_free();
600 }
601
602 void UI_icons_free_drawinfo(void *drawinfo)
603 {
604         DrawInfo *di = drawinfo;
605
606         if (di)
607         {
608                 if (di->icon) {
609                         MEM_freeN(di->icon->rect);
610                         MEM_freeN(di->icon);
611                 }
612                 MEM_freeN(di);
613         }
614 }
615
616 static DrawInfo *icon_create_drawinfo()
617 {
618         DrawInfo *di = NULL;
619
620         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
621         
622         di->drawFunc = 0;
623         di->w = ICON_DEFAULT_HEIGHT;
624         di->h = ICON_DEFAULT_HEIGHT;
625         di->icon = NULL;
626         di->aspect = 1.0f;
627
628         return di;
629 }
630
631 int UI_icon_get_width(int icon_id)
632 {
633         Icon *icon = NULL;
634         DrawInfo *di = NULL;
635
636         icon = BKE_icon_get(icon_id);
637         
638         if (!icon) {
639                 printf("UI_icon_get_width: Internal error, no icon for icon ID: %d\n", icon_id);
640                 return 0;
641         }
642         
643         di = (DrawInfo *)icon->drawinfo;
644         if (!di) {
645                 di = icon_create_drawinfo();
646                 icon->drawinfo = di;
647         }
648
649         if (di)
650                 return di->w;
651
652         return 0;
653 }
654
655 int UI_icon_get_height(int icon_id)
656 {
657         Icon *icon = NULL;
658         DrawInfo *di = NULL;
659
660         icon = BKE_icon_get(icon_id);
661         
662         if (!icon) {
663                 printf("UI_icon_get_height: Internal error, no icon for icon ID: %d\n", icon_id);
664                 return 0;
665         }
666         
667         di = (DrawInfo*)icon->drawinfo;
668
669         if (!di) {
670                 di = icon_create_drawinfo();
671                 icon->drawinfo = di;
672         }
673         
674         if (di)
675                 return di->h;
676
677         return 0;
678 }
679
680 void UI_icons_init(int first_dyn_id)
681 {
682         init_iconfile_list(&iconfilelist);
683         BKE_icons_init(first_dyn_id);
684         init_internal_icons();
685 }
686
687 static void icon_copy_rect(ImBuf *ibuf, unsigned int w, unsigned int h, unsigned int *rect)
688 {
689         struct ImBuf *ima;
690         unsigned int *drect, *srect;
691         float scaledx, scaledy;
692         short ex, ey, dx, dy;
693
694         /* paranoia test */
695         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
696                 return;
697         
698         /* waste of cpu cyles... but the imbuf API has no other way to scale fast (ton) */
699         ima = IMB_dupImBuf(ibuf);
700         
701         if (!ima) 
702                 return;
703         
704         if (ima->x > ima->y) {
705                 scaledx = (float)w;
706                 scaledy =  ( (float)ima->y/(float)ima->x )*(float)w;
707         }
708         else {                  
709                 scaledx =  ( (float)ima->x/(float)ima->y )*(float)h;
710                 scaledy = (float)h;
711         }
712         
713         ex = (short)scaledx;
714         ey = (short)scaledy;
715         
716         dx = (w - ex) / 2;
717         dy = (h - ey) / 2;
718         
719         IMB_scalefastImBuf(ima, ex, ey);
720         
721         /* if needed, convert to 32 bits */
722         if(ima->rect==NULL)
723                 IMB_rect_from_float(ima);
724
725         srect = ima->rect;
726         drect = rect;
727
728         drect+= dy*w+dx;
729         for (;ey > 0; ey--){            
730                 memcpy(drect,srect, ex * sizeof(int));
731                 drect += w;
732                 srect += ima->x;
733         }
734         IMB_freeImBuf(ima);
735 }
736
737 static void icon_create_mipmap(struct PreviewImage* prv_img, int miplevel) 
738 {
739         unsigned int size = preview_render_size(miplevel);
740
741         if (!prv_img) {
742                 printf("Error: requested preview image does not exist");
743         }
744         if (!prv_img->rect[miplevel]) {
745                 prv_img->w[miplevel] = size;
746                 prv_img->h[miplevel] = size;
747                 prv_img->changed[miplevel] = 1;
748                 prv_img->rect[miplevel] = MEM_callocN(size*size*sizeof(unsigned int), "prv_rect"); 
749         }
750 }
751
752 /* create single icon from jpg, png etc. */
753 static void icon_from_image(Image *img, int miplevel)
754 {
755         unsigned int pr_size;
756         short image_loaded = 0;
757         struct ImBuf* ibuf=NULL;
758         PreviewImage* pi;
759
760         /* img->ok is zero when Image cannot load */
761         if (img==NULL || img->ok==0)
762                 return;
763
764         /* elubie: this needs to be changed: here image is always loaded if not
765            already there. Very expensive for large images. Need to find a way to 
766            only get existing ibuf */
767         ibuf = BKE_image_get_ibuf(img, NULL);
768         if(ibuf==NULL || ibuf->rect==NULL) {
769                 return;
770         }
771         
772         pi = BKE_previewimg_get((ID*)img);      
773         
774         if(!pi) {
775                 printf("preview image could'nt be allocated");
776                 return;
777         }
778         /* we can only create the preview rect here, since loading possibly deallocated
779            old preview */
780         icon_create_mipmap(pi, miplevel);
781
782         pr_size = img->preview->w[miplevel]*img->preview->h[miplevel]*sizeof(unsigned int);
783
784         image_loaded = 1;
785         icon_copy_rect(ibuf, img->preview->w[miplevel], img->preview->h[miplevel], img->preview->rect[miplevel]);       
786 }
787
788 static void set_alpha(char* cp, int sizex, int sizey, char alpha) 
789 {
790         int x,y;
791         for(y=0; y<sizey; y++) {
792                 for(x=0; x<sizex; x++, cp+=4) {
793                         cp[3]= alpha;
794                 }
795         }
796 }
797
798 /* only called when icon has changed */
799 /* only call with valid pointer from UI_icon_draw */
800 static void icon_set_image(ID *id, DrawInfo *di, PreviewImage* prv_img, int miplevel)
801 {
802 #if 0 // XXX - preview renders have to be redesigned - possibly low level op (elubie)
803         RenderInfo ri;  
804         unsigned int pr_size = 0;
805         
806         if (!di) return;                                
807         
808         if (!prv_img) {
809                 printf("No preview image for this ID: %s\n", id->name);
810                 return;
811         }       
812
813         /* no drawing (see last parameter doDraw, just calculate preview image 
814                 - hopefully small enough to be fast */
815         if (GS(id->name) == ID_IM)
816                 icon_from_image((struct Image*)id, miplevel);
817         else {  
818                 /* create the preview rect */
819                 icon_create_mipmap(prv_img, miplevel);
820
821                 ri.curtile= 0;
822                 ri.tottile= 0;
823                 ri.rect = NULL;
824                 ri.pr_rectx = prv_img->w[miplevel];
825                 ri.pr_recty = prv_img->h[miplevel];
826
827                 pr_size = ri.pr_rectx*ri.pr_recty*sizeof(unsigned int);
828
829                 BIF_previewrender(id, &ri, NULL, PR_ICON_RENDER);
830
831                 /* world is rendered with alpha=0, so it wasn't displayed 
832                    this could be render option for sky to, for later */
833                 if (GS(id->name) == ID_WO) { 
834                         set_alpha( (char*) ri.rect, ri.pr_rectx, ri.pr_recty, 255);
835                 } 
836                 else if (GS(id->name) == ID_MA) {
837                         Material* mat = (Material*)id;
838                         if (mat->mode & MA_HALO) {
839                                 set_alpha( (char*) ri.rect, ri.pr_rectx, ri.pr_recty, 255);
840                         }
841                 }
842
843                 if (ri.rect) {
844                         memcpy(prv_img->rect[miplevel], ri.rect, pr_size);
845
846                         /* and clean up */
847                         MEM_freeN(ri.rect);
848                         ri.rect = 0;
849                 }
850         }
851 #endif
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         ui_rasterpos_safe(x, y, aspect);
857         
858         if((w<1 || h<1)) {
859                 // XXX - TODO 2.5 verify whether this case can happen
860                 // and only print in debug
861                 printf("what the heck! - icons are %i x %i pixels?\n", w, h);
862         }
863         /* rect contains image in 'rendersize', we only scale if needed */
864         else if(rw!=w && rh!=h) {
865                 ImBuf *ima;
866                 if(w>2000 || h>2000) { /* something has gone wrong! */
867                         printf("insane icon size w=%d h=%d\n",w,h);
868                         return;
869                 }
870                 /* first allocate imbuf for scaling and copy preview into it */
871                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect, 0);
872                 memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));    
873                 
874                 /* scale it */
875                 IMB_scaleImBuf(ima, w, h);
876                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, ima->rect);
877                 
878                 IMB_freeImBuf(ima);
879         }
880         else
881                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
882 }
883
884 /* Render size for preview images at level miplevel */
885 static int preview_render_size(int miplevel)
886 {
887         switch (miplevel) {
888                 case 0: return 32;
889                 case 1: return PREVIEW_DEFAULT_HEIGHT;
890         }
891         return 0;
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         
981         if(shade < 0) {
982                 float r= (128+shade)/128.0f;
983                 glPixelTransferf(GL_ALPHA_SCALE, r);
984         }
985
986         UI_icon_draw_aspect(x, y, icon_id, aspect);
987
988         if(shade < 0)
989                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
990 }