gtk/demos/testpixbuf-save.c

162 lines
5.9 KiB
C
Raw Normal View History

#include "config.h"
#include <stdio.h>
#include <gtk/gtk.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/x11/gdkx.h>
#elif defined (GDK_WINDOWING_WIN32)
#include <gdk/win32/gdkwin32.h>
#elif defined (GDK_WINDOWING_FB)
#include <gdk/linux-fb/gdkfb.h>
#endif
void
keypress_check (GtkWidget *widget, GdkEventKey *evt, gpointer data)
{
GdkPixbuf *pixbuf;
GtkDrawingArea *da = (GtkDrawingArea*)data;
GError *err = NULL;
pixbuf = (GdkPixbuf *) gtk_object_get_data (GTK_OBJECT (da), "pixbuf");
if (evt->keyval == 'q')
gtk_main_quit ();
if (evt->keyval == 's') {
if (pixbuf == NULL) {
fprintf (stderr, "PIXBUF NULL\n");
return;
}
if (!gdk_pixbuf_save (pixbuf, "foo.jpg", "jpeg",
&err,
"quality", "100",
NULL)) {
fprintf (stderr, "%s", err->message);
g_error_free (err);
}
} else if (evt->keyval == 'p') {
if (pixbuf == NULL) {
fprintf (stderr, "PIXBUF NULL\n");
return;
}
if (!gdk_pixbuf_save (pixbuf, "foo.png", "png", &err, NULL)) {
fprintf (stderr, "%s", err->message);
g_error_free (err);
}
}
}
int
close_app (GtkWidget *widget, gpointer data)
{
gtk_main_quit ();
return TRUE;
}
int
expose_cb (GtkWidget *drawing_area, GdkEventExpose *evt, gpointer data)
{
GdkPixbuf *pixbuf;
pixbuf = (GdkPixbuf *) gtk_object_get_data (GTK_OBJECT (drawing_area),
"pixbuf");
if (gdk_pixbuf_get_has_alpha (pixbuf)) {
gdk_draw_rgb_32_image (drawing_area->window,
drawing_area->style->black_gc,
evt->area.x, evt->area.y,
evt->area.width,
evt->area.height,
GDK_RGB_DITHER_MAX,
gdk_pixbuf_get_pixels (pixbuf) +
(evt->area.y * gdk_pixbuf_get_rowstride (pixbuf)) +
(evt->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
gdk_pixbuf_get_rowstride (pixbuf));
} else {
gdk_draw_rgb_image (drawing_area->window,
drawing_area->style->black_gc,
evt->area.x, evt->area.y,
evt->area.width,
evt->area.height,
GDK_RGB_DITHER_NORMAL,
gdk_pixbuf_get_pixels (pixbuf) +
(evt->area.y * gdk_pixbuf_get_rowstride (pixbuf)) +
(evt->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
gdk_pixbuf_get_rowstride (pixbuf));
}
return FALSE;
}
int
configure_cb (GtkWidget *drawing_area, GdkEventConfigure *evt, gpointer data)
{
GdkPixbuf *pixbuf;
pixbuf = (GdkPixbuf *) gtk_object_get_data (GTK_OBJECT (drawing_area),
"pixbuf");
g_print ("X:%d Y:%d\n", evt->width, evt->height);
if (evt->width != gdk_pixbuf_get_width (pixbuf) || evt->height != gdk_pixbuf_get_height (pixbuf)) {
GdkWindow *root;
GdkPixbuf *new_pixbuf;
root = GDK_ROOT_PARENT ();
new_pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
0, 0, 0, 0, evt->width, evt->height);
gtk_object_set_data (GTK_OBJECT (drawing_area), "pixbuf", new_pixbuf);
gdk_pixbuf_unref (pixbuf);
}
return FALSE;
}
int
main (int argc, char **argv)
{
GdkWindow *root;
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *drawing_area;
GdkPixbuf *pixbuf;
gtk_init (&argc, &argv);
gtk_widget_set_default_colormap (gdk_rgb_get_colormap ());
root = GDK_ROOT_PARENT ();
pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
0, 0, 0, 0, 150, 160);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC (close_app), NULL);
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (close_app), NULL);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
drawing_area = gtk_drawing_area_new ();
fix a typo. 2001-08-07 Havoc Pennington <hp@pobox.com> * gtk/gtkfilesel.c (open_ref_dir): fix a typo. * gtk/gtkplug.c (gtk_plug_init): remove setting of auto_shrink; some fixage is needed here, but nothing simple. Owen understands it. ;-) * gtk/gtkwindow.h, gtk/gtkwindow.c: Rework code and API for window sizing and positioning. Also, fix bug in compute_geometry_hints (width/height confusion for setting min size). (gtk_window_move): new function (gtk_window_resize): new function (gtk_window_get_size): new function (gtk_window_get_position): new function (gtk_window_parse_geometry): new function * gtk/gtkwidget.c (gtk_widget_set_size_request): new function (gtk_widget_get_size_request): new function (gtk_widget_get_usize): delete, that was a short-lived function ;-) (gtk_widget_set_usize): deprecate (gtk_widget_set_uposition): deprecate, make it a trivial gtk_window_move() wrapper (gtk_widget_class_init): remove x/y/width/height properties, add width_request height_request * demos/*: update to avoid deprecated functions * gtk/gtklayout.c: add x/y child properties * gtk/gtkfixed.c: add x/y child properties, and get rid of uses of "gint16" * tests/testgtk.c (create_window_sizing): lots of tweaks to window sizing test * gdk/x11/gdkevents-x11.c (gdk_event_translate): Ensure that configure events on toplevel windows are always in root window coordinates, following ICCCM spec that all synthetic events are in root window coords already, while real events are in parent window coords. Previously the code assumed that coords of 0,0 were parent window coords, which was really broken. * gtk/gtkcontainer.c (gtk_container_get_focus_chain): fix warning * gdk/gdkwindow.h (GdkWindowHints): add GDK_HINT_USER_POS and GDK_HINT_USER_SIZE so we can set USSize and USPosition hints in gtk_window_parse_geometry() * gdk/x11/gdkwindow-x11.c (gdk_window_set_geometry_hints): support new USER_POS USER_SIZE hints
2001-08-10 03:46:08 +00:00
gtk_widget_set_size_request (GTK_WIDGET (drawing_area),
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf));
gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
GTK_SIGNAL_FUNC (expose_cb), NULL);
gtk_signal_connect (GTK_OBJECT (drawing_area), "configure_event",
GTK_SIGNAL_FUNC (configure_cb), NULL);
gtk_signal_connect (GTK_OBJECT (window), "key_press_event",
GTK_SIGNAL_FUNC (keypress_check), drawing_area);
gtk_object_set_data (GTK_OBJECT (drawing_area), "pixbuf", pixbuf);
gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}