wmbiff: Bump to version 0.4.29.
[dockapps.git] / wmmenu / pixmaps.c
blob4ebe54919a16eb9fc251457edf08e64ebf16e9db
1 #include <stdio.h>
2 #include <assert.h>
4 #ifdef WITH_GDKPIXBUF
5 #include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
6 #endif
7 #include <libdockapp/dockapp.h>
9 #include "pixmaps.h"
10 #include "xobjects.h"
11 #include "options.h"
12 #include "utils.h"
13 #include "error.h"
14 #include "menu.h"
16 #include "defaultTile.xpm"
17 #include "defaultIcon.xpm"
19 static char ** CurrentDefault = defaultIcon_xpm ;
21 #ifdef WITH_GDKPIXBUF
23 extern void Pixmaps_FindLoad (const char * name,
24 Pixmap * imageP, Pixmap * maskP, int * wP, int * hP)
26 static int mustInitGdkPixbuf = 1 ;
27 char path [FILENAME_MAX] ;
28 GdkPixbuf * pix ;
29 int width, height ;
30 void (* problem) (const char *, ...) ;
31 GError *gerror = NULL;
33 if (mustInitGdkPixbuf)
35 gdk_pixbuf_xlib_init (DADisplay, DefaultScreen (DADisplay)) ;
36 mustInitGdkPixbuf = 0 ;
39 assert (name != NULL) ;
40 assert (imageP != NULL) ;
41 assert (maskP != NULL) ;
42 problem = (CurrentDefault == NULL ? error : warn) ;
44 #define UseDefault() \
45 if ((pix = gdk_pixbuf_new_from_xpm_data ( \
46 (const char **)CurrentDefault)) == NULL) \
47 error ("can't create internal default pixmap")
49 if (name == NULL || name[0] == EOS)
51 if (CurrentDefault == NULL)
52 error ("missing image name specification") ;
54 UseDefault () ;
56 else
57 if (! File_FindInPath (path, sizeof path, PixmapPath, name))
59 problem ("can't find image \"%s\"", name) ;
60 UseDefault () ;
62 else
63 if ((pix = gdk_pixbuf_new_from_file (path, &gerror)) == NULL)
65 problem ("can't load image \"%s\"", path) ;
66 UseDefault () ;
69 /* get loaded geometry */
70 width = gdk_pixbuf_get_width (pix) ;
71 height = gdk_pixbuf_get_height (pix) ;
73 /* scale if allowed, possible, and useful */
74 if (AutoScale &&
75 TileXSize > 0 && TileYSize > 0 &&
76 (width > TileXSize || height > TileYSize))
78 GdkPixbuf * scaled ;
81 Compare width/TileXSize to height/TileYSize to determine a new
82 size that fits within TileXSize x TileYSize and keeps the same
83 aspect ratio as before. The biggest ratio wins.
85 if (width*TileYSize >= height*TileXSize)
87 /* width directs re-scaling */
88 height = (height * TileXSize) / width ;
89 width = TileXSize ;
91 else
93 /* height directs re-scaling */
94 width = (width * TileYSize) / height ;
95 height = TileYSize ;
98 scaled = gdk_pixbuf_scale_simple (pix,
99 width, height, GDK_INTERP_HYPER) ;
100 g_object_unref (pix) ;
101 pix = scaled ;
104 if (wP != NULL) *wP = width ;
105 if (hP != NULL) *hP = height ;
106 gdk_pixbuf_xlib_render_pixmap_and_mask (pix, imageP, maskP, 128) ;
107 /* don't forget to free now we've done pixmaps */
108 g_object_unref (pix) ;
112 We have to reimplement a few trivial gdk functions here to avoid linking with
113 it !
116 typedef struct {
117 guint32 pixel;
118 guint16 red;
119 guint16 green;
120 guint16 blue;
121 } GdkColor;
123 extern gint gdk_screen_width (void)
125 return DisplayWidth (DADisplay, DefaultScreen (DADisplay)) ;
128 extern gint gdk_screen_height (void)
130 return DisplayHeight (DADisplay, DefaultScreen (DADisplay)) ;
133 extern gboolean gdk_color_parse (const gchar * spec, GdkColor * color)
135 XColor scr ;
136 #if 1
138 /* Lukasz Pankowski suggested this */
139 if (! XParseColor (DADisplay,
140 DefaultColormap (DADisplay, DefaultScreen (DADisplay)),
141 spec, & scr))
142 #else
143 XColor exact ;
145 if (! XAllocNamedColor (DADisplay,
146 DefaultColormap (DADisplay, DefaultScreen (DADisplay)),
147 spec, & scr, & exact))
148 #endif
150 return FALSE ;
152 else
154 color->pixel = scr.pixel ;
155 color->red = scr.red ;
156 color->green = scr.green ;
157 color->blue = scr.blue ;
158 return TRUE ;
162 #else
164 extern void Pixmaps_FindLoad (const char * name,
165 Pixmap * imageP, Pixmap * maskP, int * wP, int * hP)
167 char path [FILENAME_MAX] ;
168 XpmAttributes attr ;
169 void (* problem) (const char *, ...) ;
171 assert (name != NULL) ;
172 assert (imageP != NULL) ;
173 assert (maskP != NULL) ;
174 attr.valuemask = 0 ;
175 problem = (CurrentDefault == NULL ? error : warn) ;
177 #define UseDefault() \
178 if (XpmCreatePixmapFromData (DADisplay, DefaultRootWindow (DADisplay), \
179 CurrentDefault, imageP, maskP, & attr) != XpmSuccess) \
180 error ("can't create internal default pixmap")
182 if (! File_FindInPath (path, sizeof path, PixmapPath, name))
184 problem ("can't find file \"%s\"", name) ;
185 UseDefault () ;
187 else
188 if (XpmReadFileToPixmap (DADisplay, DefaultRootWindow (DADisplay),
189 path, imageP, maskP, & attr) != XpmSuccess)
191 problem ("can't load pixmap \"%s\"", path) ;
192 UseDefault () ;
195 if (wP != NULL) *wP = attr.width ;
196 if (hP != NULL) *hP = attr.height ;
199 #endif
201 extern void Pixmaps_LoadMenu (void)
203 Pixmap image, mask ;
204 bool saveAutoScale ;
205 saveAutoScale = AutoScale ; /* save old value */
206 AutoScale = false ; /* set temporary value */
207 Pixmaps_FindLoad (Menu_GetPixmap (), & image, & mask, NULL, NULL) ;
208 DASetShape (mask) ;
209 DASetPixmap (image) ;
210 AutoScale = saveAutoScale ; /* restore initial value */
213 extern void Pixmaps_LoadTile (void)
215 int x, y ;
217 if (TileImage != 0) XFreePixmap (DADisplay, TileImage) ;
218 if (TileMask != 0) XFreePixmap (DADisplay, TileMask) ;
220 CurrentDefault = defaultTile_xpm ;
221 Pixmaps_FindLoad (TilePath, & TileImage, & TileMask, & x, & y) ;
222 CurrentDefault = defaultIcon_xpm ;
224 if (TileXSize <= 0) TileXSize = x ;
225 if (TileYSize <= 0) TileYSize = y ;
228 extern void Pixmaps_LoadHighlight (void)
230 int x, y ;
232 if (HighlightImage != 0) XFreePixmap (DADisplay, HighlightImage) ;
233 if (HighlightMask != 0) XFreePixmap (DADisplay, HighlightMask) ;
235 if (HighlightPath[0] != EOS)
237 Pixmaps_FindLoad (HighlightPath,
238 & HighlightImage, & HighlightMask, & x, & y) ;
240 else
242 HighlightImage = 0 ;
243 HighlightMask = 0 ;