/* * Copyright (C) 2007 Daniel Bessler * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Author : Daniel Bessler */ #include #include #define ICON_SURFACE_WIDTH 50.0 #define ICON_SURFACE_HEIGHT 50.0 #define FALLBACK_ICON "kiba-dock" G_DEFINE_TYPE (KibaIcon, kiba_icon, KIBA_TYPE_OBJECT) void kiba_icon_set_pixbuf (KibaIcon *icon, GdkPixbuf *pixbuf) { g_return_if_fail (KIBA_IS_ICON (icon)); g_return_if_fail (GDK_IS_PIXBUF (pixbuf)); if (icon->file) { g_free (icon->file); icon->file = NULL; } if (icon->pixbuf) { g_object_unref (icon->pixbuf); icon->pixbuf = NULL; } icon->pixbuf = pixbuf; KIBA_OBJECT(icon)->surface_buffer->needs_update = TRUE; gtk_widget_queue_draw (GTK_WIDGET(icon)->parent->parent); } GdkPixbuf* kiba_icon_get_pixbuf (KibaIcon *icon) { g_return_if_fail (KIBA_IS_ICON (icon)); return icon->pixbuf; } void kiba_icon_set_file (KibaIcon *icon, const char *file) { g_return_if_fail (KIBA_IS_ICON (icon)); g_return_if_fail (file != NULL); if (icon->file && strcmp (file, icon->file) == 0) return; if (!kiba_load_icon (file, KIBA_OBJECT(icon)->allocation.width, &icon->pixbuf)) { if (icon->file) kiba_load_icon (icon->file, KIBA_OBJECT(icon)->allocation.width, &icon->pixbuf); return; } if (icon->file) { g_free (icon->file); icon->file = NULL; } icon->file = g_strdup(file); KIBA_OBJECT(icon)->surface_buffer->needs_update = TRUE; gtk_widget_queue_draw (GTK_WIDGET(icon)->parent->parent); } const char* kiba_icon_get_file (KibaIcon *icon) { g_return_if_fail (KIBA_IS_ICON (icon)); return icon->file; } void kiba_icon_set_size (KibaIcon *icon, gint size) { // kiba_object_set_size (KIBA_OBJECT(icon), size, size); } gint kiba_icon_get_size (KibaIcon *icon) { return GTK_WIDGET(icon)->allocation.height; } static void kiba_icon_paint (KibaObject *object, cairo_t *cr) { g_return_if_fail (KIBA_IS_OBJECT (object)); g_return_if_fail (object->surface_buffer != NULL); cairo_set_source_surface (cr, object->surface_buffer->surface, 0.0, 0.0); cairo_paint (cr); } static void kiba_icon_surface (KibaObject *object, cairo_t *cr) { g_return_if_fail (KIBA_IS_ICON(object)); g_return_if_fail (GDK_IS_PIXBUF(KIBA_ICON(object)->pixbuf)); gdk_cairo_set_source_pixbuf (cr, KIBA_ICON(object)->pixbuf, 0.0, 0.0); cairo_paint (cr); } static void kiba_icon_unrealize (GtkWidget *widget) { if (GDK_IS_PIXBUF (KIBA_ICON(widget)->pixbuf)) g_object_unref (KIBA_ICON(widget)->pixbuf); if (KIBA_ICON(widget)->file) g_free (KIBA_ICON(widget)->file); (* GTK_WIDGET_CLASS (kiba_icon_parent_class)->unrealize) (widget); } static void kiba_icon_realize (GtkWidget *widget) { (* GTK_WIDGET_CLASS (kiba_icon_parent_class)->realize) (widget); kiba_surface_init (&KIBA_OBJECT(widget)->surface_buffer, KIBA_OBJECT_SURFACE, KIBA_OBJECT(widget)->allocation.height, KIBA_OBJECT(widget)->allocation.height); if (KIBA_ICON(widget)->file) kiba_load_icon (KIBA_ICON(widget)->file, KIBA_OBJECT(widget)->allocation.width, &KIBA_ICON(widget)->pixbuf); if (!KIBA_ICON(widget)->pixbuf) kiba_load_icon (FALLBACK_ICON, KIBA_OBJECT(widget)->allocation.width, &KIBA_ICON(widget)->pixbuf); } static void kiba_icon_class_init (KibaIconClass *class) { GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); widget_class->realize = kiba_icon_realize; widget_class->unrealize = kiba_icon_unrealize; } static void kiba_icon_init (KibaIcon *icon) { KIBA_OBJECT(icon)->allocation.width = 48; KIBA_OBJECT(icon)->allocation.height = 48; KIBA_OBJECT(icon)->paint = kiba_icon_paint; KIBA_OBJECT(icon)->surface = kiba_icon_surface; SET_FLAG (KIBA_OBJECT(icon)->props, KIBA_OBJECT_VISIBLE); SET_FLAG (KIBA_OBJECT(icon)->props, KIBA_OBJECT_MOVEABLE); SET_FLAG (KIBA_OBJECT(icon)->props, KIBA_OBJECT_PRELIGHTABLE); SET_FLAG (KIBA_OBJECT(icon)->props, KIBA_OBJECT_ZOOMABLE); SET_FLAG (KIBA_OBJECT(icon)->props, KIBA_OBJECT_BOUNCEABLE); SET_FLAG (KIBA_OBJECT(icon)->props, KIBA_OBJECT_PULSEABLE); SET_FLAG (KIBA_OBJECT(icon)->props, KIBA_OBJECT_ROTATEABLE); } GtkWidget* kiba_icon_new_from_file (const char *file) { KibaIcon *icon; icon = g_object_new (KIBA_TYPE_ICON, NULL); icon->file = g_strdup (file); return GTK_WIDGET(icon); } GtkWidget* kiba_icon_new () { KibaIcon *icon; icon = g_object_new (KIBA_TYPE_ICON, NULL); icon->file = NULL; return GTK_WIDGET(icon); }