fix for uninitialized value in BLI_path_cwd() if PWD wasn't defined and the CWD was...
[blender.git] / source / blender / editors / interface / interface_icons.c
index 2da54b4..32b2787 100644 (file)
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_storage_types.h"
+#include "BLI_utildefines.h"
 
 #include "DNA_brush_types.h"
 #include "DNA_object_types.h"
 #include "DNA_screen_types.h"
+#include "DNA_space_types.h"
 
 #include "RNA_access.h"
 #include "RNA_enum_types.h"
@@ -454,7 +456,7 @@ static void vicon_move_down_draw(int x, int y, int w, int h, float UNUSED(alpha)
        glDisable(GL_LINE_SMOOTH);
 }
 
-static void init_brush_icons()
+static void init_brush_icons(void)
 {
 
 #define INIT_BRUSH_ICON(icon_id, name)                                      \
@@ -499,14 +501,14 @@ static void init_brush_icons()
 #undef INIT_BRUSH_ICON
 }
 
-static void init_internal_icons()
+static void init_internal_icons(void)
 {
        bTheme *btheme= U.themes.first;
        ImBuf *bbuf= NULL;
        int x, y, icontype;
        char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
        
-       if ((btheme!=NULL) && (strlen(btheme->tui.iconfile) > 0)) {
+       if ((btheme!=NULL) && btheme->tui.iconfile[0]) {
                char *datadir= BLI_get_folder(BLENDER_DATAFILES, NULL);
                if (datadir) {
                        BLI_make_file_string("/", iconfilestr, datadir, btheme->tui.iconfile);
@@ -612,7 +614,7 @@ static void init_iconfile_list(struct ListBase *list)
        
        /* since BLI_getdir changes the current working directory, restore it 
           back to old value afterwards */
-       if(!BLI_getwdN(olddir)) 
+       if(!BLI_getwdN(olddir, sizeof(olddir))) 
                restoredir = 0;
        totfile = BLI_getdir(icondirstr, &dir);
        if (restoredir && !chdir(olddir)) {} /* fix warning about checking return value */
@@ -681,7 +683,7 @@ static void free_iconfile_list(struct ListBase *list)
        }
 }
 
-int UI_iconfile_get_index(char *filename)
+int UI_iconfile_get_index(const char *filename)
 {
        IconFile *ifile;
        ListBase *list=&(iconfilelist);
@@ -730,7 +732,7 @@ void UI_icons_free_drawinfo(void *drawinfo)
        }
 }
 
-static DrawInfo *icon_create_drawinfo()
+static DrawInfo *icon_create_drawinfo(void)
 {
        DrawInfo *di = NULL;
 
@@ -747,7 +749,7 @@ int UI_icon_get_width(int icon_id)
 
        icon = BKE_icon_get(icon_id);
        
-       if (!icon) {
+       if (icon==ICON_NULL) {
                if (G.f & G_DEBUG)
                        printf("UI_icon_get_width: Internal error, no icon for icon ID: %d\n", icon_id);
                return 0;
@@ -772,7 +774,7 @@ int UI_icon_get_height(int icon_id)
 
        icon = BKE_icon_get(icon_id);
        
-       if (!icon) {
+       if (icon==ICON_NULL) {
                if (G.f & G_DEBUG)
                        printf("UI_icon_get_height: Internal error, no icon for icon ID: %d\n", icon_id);
                return 0;
@@ -845,6 +847,15 @@ static void icon_set_image(bContext *C, ID *id, PreviewImage* prv_img, int miple
 
 static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect), int rw, int rh, unsigned int *rect, float alpha, float *rgb, short is_preview)
 {
+       ImBuf *ima= NULL;
+
+       /* sanity check */
+       if(w<=0 || h<=0 || w>2000 || h>2000) {
+               printf("icon_draw_rect: icons are %i x %i pixels?\n", w, h);
+               BLI_assert(!"invalid icon size");
+               return;
+       }
+
        /* modulate color */
        if(alpha != 1.0f)
                glPixelTransferf(GL_ALPHA_SCALE, alpha);
@@ -854,53 +865,28 @@ static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect),
                glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
                glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
        }
-       
-       if(is_preview == 0) {
-               /* position */
-               glRasterPos2f(x,y);
-       }
 
-       /* draw */
-       if((w<1 || h<1)) {
-               // XXX - TODO 2.5 verify whether this case can happen
-               if (G.f & G_DEBUG)
-                       printf("what the heck! - icons are %i x %i pixels?\n", w, h);
-       }
        /* rect contains image in 'rendersize', we only scale if needed */
-       else if(rw!=w && rh!=h) {
-               if(w>2000 || h>2000) { /* something has gone wrong! */
-                       if (G.f & G_DEBUG)
-                               printf("insane icon size w=%d h=%d\n",w,h);
-               }
-               else {
-                       ImBuf *ima;
-
-                       /* first allocate imbuf for scaling and copy preview into it */
-                       ima = IMB_allocImBuf(rw, rh, 32, IB_rect, 0);
-                       memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));    
-                       
-                       /* scale it */
-                       IMB_scaleImBuf(ima, w, h);
-
-                       if(is_preview) {
-                               glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, ima->rect);
-                       }
-                       else {
-                               glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, ima->rect);
-                       }
+       if(rw!=w && rh!=h) {
+               /* first allocate imbuf for scaling and copy preview into it */
+               ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
+               memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));    
+               IMB_scaleImBuf(ima, w, h); /* scale it */
+               rect= ima->rect;
+       }
 
-                       IMB_freeImBuf(ima);
-               }
+       /* draw */
+       if(is_preview) {
+               glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
        }
        else {
-               if(is_preview) {
-                       glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
-               }
-               else {
-                       glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
-               }
+               glRasterPos2f(x, y);
+               glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
        }
 
+       if(ima)
+               IMB_freeImBuf(ima);
+
        /* restore color */
        if(alpha != 0.0f)
                glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
@@ -964,7 +950,7 @@ static void icon_draw_size(float x, float y, int icon_id, float aspect, float al
        
        icon = BKE_icon_get(icon_id);
        
-       if (!icon) {
+       if (icon==ICON_NULL) {
                if (G.f & G_DEBUG)
                        printf("icon_draw_mipmap: Internal error, no icon for icon ID: %d\n", icon_id);
                return;
@@ -1015,62 +1001,96 @@ static void icon_draw_size(float x, float y, int icon_id, float aspect, float al
        }
 }
 
-void ui_id_icon_render(bContext *C, ID *id, int preview)
+static void ui_id_icon_render(bContext *C, ID *id, int big)
 {
        PreviewImage *pi = BKE_previewimg_get(id); 
-               
+       
        if (pi) {                       
                if ((pi->changed[0] ||!pi->rect[0])) /* changed only ever set by dynamic icons */
                {
-                       /* create the preview rect if necessary */                              
+                       /* create the rect if necessary */                              
                        
                        icon_set_image(C, id, pi, 0);           /* icon size */
-                       if (preview)
-                               icon_set_image(C, id, pi, 1);   /* preview size */
+                       if (big)
+                               icon_set_image(C, id, pi, 1);   /* bigger preview size */
                        
                        pi->changed[0] = 0;
                }
        }
 }
 
-static int ui_id_brush_get_icon(bContext *C, ID *id, int preview)
+static void ui_id_brush_render(bContext *C, ID *id)
+{
+       PreviewImage *pi = BKE_previewimg_get(id); 
+       int i;
+       
+       if(!pi)
+               return;
+       
+       for(i = 0; i < PREVIEW_MIPMAPS; i++) {
+               /* check if rect needs to be created; changed
+                only set by dynamic icons */
+               if((pi->changed[i] || !pi->rect[i])) {
+                       icon_set_image(C, id, pi, i);
+                       pi->changed[i] = 0;
+               }
+       }
+}
+
+
+static int ui_id_brush_get_icon(bContext *C, ID *id)
 {
        Brush *br = (Brush*)id;
 
        if(br->flag & BRUSH_CUSTOM_ICON) {
                BKE_icon_getid(id);
-               ui_id_icon_render(C, id, preview);
+               ui_id_brush_render(C, id);
        }
-       else if(!id->icon_id) {
-               /* no icon found, reset it */
-               
-               /* this is not nice, should probably make
-                  brushes be strictly in one paint mode only
-                  to avoid this kind of thing */
+       else {
                Object *ob = CTX_data_active_object(C);
-               EnumPropertyItem *items;
-               int tool;
-               
-               if(ob && (ob->mode & OB_MODE_SCULPT)) {
+               SpaceImage *sima;
+               EnumPropertyItem *items = NULL;
+               int tool, mode = 0;
+
+               /* XXX: this is not nice, should probably make brushes
+                  be strictly in one paint mode only to avoid
+                  checking various context stuff here */
+
+               if(CTX_wm_view3d(C) && ob) {
+                       if(ob->mode & OB_MODE_SCULPT)
+                               mode = OB_MODE_SCULPT;
+                       else if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT))
+                               mode = OB_MODE_VERTEX_PAINT;
+                       else if(ob->mode & OB_MODE_TEXTURE_PAINT)
+                               mode = OB_MODE_TEXTURE_PAINT;
+               }
+               else if((sima = CTX_wm_space_image(C)) &&
+                       (sima->flag & SI_DRAWTOOL)) {
+                       mode = OB_MODE_TEXTURE_PAINT;
+               }
+
+               /* reset the icon */
+               if(mode == OB_MODE_SCULPT) {
                        items = brush_sculpt_tool_items;
                        tool = br->sculpt_tool;
                }
-               else if(ob && (ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT))) {
+               else if(mode == OB_MODE_VERTEX_PAINT) {
                        items = brush_vertexpaint_tool_items;
                        tool = br->vertexpaint_tool;
                }
-               else {
+               else if(mode == OB_MODE_TEXTURE_PAINT) {
                        items = brush_imagepaint_tool_items;
                        tool = br->imagepaint_tool;
                }
 
-               RNA_enum_icon_from_value(items, tool, &id->icon_id);
+               if(!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
+                       id->icon_id = 0;
        }
 
        return id->icon_id;
 }
 
-int ui_id_icon_get(bContext *C, ID *id, int preview)
+int ui_id_icon_get(bContext *C, ID *id, int big)
 {
        int iconid= 0;
        
@@ -1078,7 +1098,7 @@ int ui_id_icon_get(bContext *C, ID *id, int preview)
        switch(GS(id->name))
        {
                case ID_BR:
-                       iconid= ui_id_brush_get_icon(C, id, preview);
+                       iconid= ui_id_brush_get_icon(C, id);
                        break;
                case ID_MA: /* fall through */
                case ID_TE: /* fall through */
@@ -1087,7 +1107,7 @@ int ui_id_icon_get(bContext *C, ID *id, int preview)
                case ID_LA: /* fall through */
                        iconid= BKE_icon_getid(id);
                        /* checks if not exists, or changed */
-                       ui_id_icon_render(C, id, preview);
+                       ui_id_icon_render(C, id, big);
                        break;
                default:
                        break;