comparison src/gtkutil.c @ 64936:d5e998f50c2d

* gtkutil.c (xg_get_pixbuf_from_pix_and_mask): New function. (xg_get_image_for_pixmap): Move some code to xg_get_pixbuf_from_pix_and_mask, and call it.
author Jan Djärv <jan.h.d@swipnet.se>
date Sat, 13 Aug 2005 09:48:17 +0000
parents 0975467abc17
children ec2fa2e1d4ea
comparison
equal deleted inserted replaced
64935:0095a3f61578 64936:d5e998f50c2d
237 { 237 {
238 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy); 238 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
239 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR); 239 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
240 } 240 }
241 241
242 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
243
244 GdkPixbuf *
245 xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap)
246 GdkPixmap *gpix;
247 GdkPixmap *gmask;
248 GdkColormap *cmap;
249 {
250 int x, y, width, height, rowstride, mask_rowstride;
251 GdkPixbuf *icon_buf, *tmp_buf;
252 guchar *pixels;
253 guchar *mask_pixels;
254
255 gdk_drawable_get_size (gpix, &width, &height);
256 tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
257 0, 0, 0, 0, width, height);
258 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
259 g_object_unref (G_OBJECT (tmp_buf));
260
261 if (gmask)
262 {
263 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
264 gmask,
265 NULL,
266 0, 0, 0, 0,
267 width, height);
268 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
269 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
270 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
271 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
272 int y;
273
274 for (y = 0; y < height; ++y)
275 {
276 guchar *iconptr, *maskptr;
277 int x;
278
279 iconptr = pixels + y * rowstride;
280 maskptr = mask_pixels + y * mask_rowstride;
281
282 for (x = 0; x < width; ++x)
283 {
284 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
285 just R is sufficient. */
286 if (maskptr[0] == 0)
287 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
288
289 iconptr += rowstride/width;
290 maskptr += mask_rowstride/width;
291 }
292 }
293
294 g_object_unref (G_OBJECT (mask_buf));
295 }
296
297 return icon_buf;
298 }
299
242 /* For the image defined in IMG, make and return a GtkImage. For displays with 300 /* For the image defined in IMG, make and return a GtkImage. For displays with
243 8 planes or less we must make a GdkPixbuf and apply the mask manually. 301 8 planes or less we must make a GdkPixbuf and apply the mask manually.
244 Otherwise the highlightning and dimming the tool bar code in GTK does 302 Otherwise the highlightning and dimming the tool bar code in GTK does
245 will look bad. For display with more than 8 planes we just use the 303 will look bad. For display with more than 8 planes we just use the
246 pixmap and mask directly. For monochrome displays, GTK doesn't seem 304 pixmap and mask directly. For monochrome displays, GTK doesn't seem
309 else 367 else
310 gtk_image_set_from_pixmap (old_widget, gpix, gmask); 368 gtk_image_set_from_pixmap (old_widget, gpix, gmask);
311 } 369 }
312 else 370 else
313 { 371 {
372
314 /* This is a workaround to make icons look good on pseudo color 373 /* This is a workaround to make icons look good on pseudo color
315 displays. Apparently GTK expects the images to have an alpha 374 displays. Apparently GTK expects the images to have an alpha
316 channel. If they don't, insensitive and activated icons will 375 channel. If they don't, insensitive and activated icons will
317 look bad. This workaround does not work on monochrome displays, 376 look bad. This workaround does not work on monochrome displays,
318 and is not needed on true color/static color displays (i.e. 377 and is not needed on true color/static color displays (i.e.
319 16 bits and higher). */ 378 16 bits and higher). */
320 int x, y, width, height, rowstride, mask_rowstride; 379 GdkColormap *cmap = gtk_widget_get_colormap (widget);
321 GdkPixbuf *icon_buf, *tmp_buf; 380 GdkPixbuf *icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
322 guchar *pixels;
323 guchar *mask_pixels;
324
325 gdk_drawable_get_size (gpix, &width, &height);
326 tmp_buf = gdk_pixbuf_get_from_drawable (NULL,
327 gpix,
328 gtk_widget_get_colormap (widget),
329 0, 0, 0, 0, width, height);
330 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
331 g_object_unref (G_OBJECT (tmp_buf));
332
333 if (gmask)
334 {
335 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
336 gmask,
337 NULL,
338 0, 0, 0, 0,
339 width, height);
340 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
341 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
342 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
343 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
344 int y;
345
346 for (y = 0; y < height; ++y)
347 {
348 guchar *iconptr, *maskptr;
349 int x;
350
351 iconptr = pixels + y * rowstride;
352 maskptr = mask_pixels + y * mask_rowstride;
353
354 for (x = 0; x < width; ++x)
355 {
356 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
357 just R is sufficient. */
358 if (maskptr[0] == 0)
359 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
360
361 iconptr += rowstride/width;
362 maskptr += mask_rowstride/width;
363 }
364 }
365
366 g_object_unref (G_OBJECT (mask_buf));
367 }
368 381
369 if (! old_widget) 382 if (! old_widget)
370 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf)); 383 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
371 else 384 else
372 gtk_image_set_from_pixbuf (old_widget, icon_buf); 385 gtk_image_set_from_pixbuf (old_widget, icon_buf);