diff options
Diffstat (limited to 'hardinfo2/callbacks.c')
-rw-r--r-- | hardinfo2/callbacks.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/hardinfo2/callbacks.c b/hardinfo2/callbacks.c index d7982075..526ac1fb 100644 --- a/hardinfo2/callbacks.c +++ b/hardinfo2/callbacks.c @@ -28,6 +28,97 @@ #include <config.h> +void cb_save_graphic() +{ + /* this is ridiculously complicated :/ why in the hell gtk+ makes this kind of thing so difficult? + FIXME: it still don't save the image with the right size; we should do this sometime soon (tm) */ + Shell *shell = shell_get_main_shell(); + GtkWidget *widget = shell->info->view; + GtkWidget *dialog; + + PangoLayout *layout; + PangoContext *context; + PangoRectangle rect; + + GdkPixmap *pm; + GdkPixbuf *pb; + GdkGC *gc; + GdkColor black = { 0, 0, 0, 0 }; + GdkColor white = { 0, 65535, 65535, 65535 }; + + gint w, h; + gchar *filename = NULL; + + /* */ + shell_status_update("Creating image..."); + + /* initialize stuff */ + gc = gdk_gc_new(widget->window); + context = gtk_widget_get_pango_context (widget); + layout = pango_layout_new(context); + + /* draw the title */ + pango_layout_set_markup(layout, shell->selected->name, -1); + pango_layout_set_width(layout, widget->allocation.width * PANGO_SCALE); + pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); + + pango_layout_get_pixel_extents(layout, NULL, &rect); + + w = widget->allocation.width; + h = widget->allocation.height + rect.height; + + pm = gdk_pixmap_new(widget->window, w, h, -1); + gdk_draw_layout_with_colors (GDK_DRAWABLE(pm), gc, 0, 0, layout, &white, &black); + + /* copy the pixmap from the treeview and from the title */ + pb = gdk_pixbuf_get_from_drawable(NULL, + widget->window, + NULL, + 0, 0, + 0, 0, + w, h); + pb = gdk_pixbuf_get_from_drawable(pb, + pm, + NULL, + 0, 0, + 0, widget->allocation.height, + widget->allocation.width, rect.height); + + /* save the pixbuf to a png file */ + dialog = gtk_file_chooser_dialog_new("Save Image", + NULL, + GTK_FILE_CHOOSER_ACTION_SAVE, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, + NULL); + + filename = g_strconcat(shell->selected->name, ".png", NULL); + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename); + g_free(filename); + + if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { + filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + + shell_status_update("Saving image..."); + gdk_pixbuf_save(pb, filename, "png", NULL, + "compression", "9", + "tEXt::hardinfo::version", VERSION, + "tEXt::hardinfo::arch", ARCH, + NULL); + + g_free(filename); + } + + /* unref */ + gtk_widget_destroy (dialog); + g_object_unref(pb); + g_object_unref(layout); + g_object_unref(pm); + g_object_unref(gc); + + shell_status_update("Done."); +} + void cb_open_web_page() { open_url("http://hardinfo.berlios.de"); |