]> Pileus Git - ~andy/gtk/blob - demos/testpixbuf.c
Added scaling of pixmaps. Currently doesn't work, however, since I'm
[~andy/gtk] / demos / testpixbuf.c
1
2 /* GTK - The GIMP Toolkit
3  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28
29 /* Note: these #includes differ slightly from the testrgb.c file included
30    in the GdkRgb release. */
31
32 /* For gettimeofday */
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36
37 #include <gtk/gtk.h>
38 #include "gdk-pixbuf.h"
39
40 static void
41 quit_func (GtkWidget *widget, gpointer dummy)
42 {
43   gtk_main_quit ();
44 }
45
46 expose_func (GtkWidget *drawing_area, GdkEventExpose *event, gpointer data)
47 {
48     GdkPixBuf *pixbuf;
49
50     pixbuf = (GdkPixBuf *)gtk_object_get_data(GTK_OBJECT(drawing_area), "pixbuf");
51
52     if (pixbuf->art_pixbuf->has_alpha){
53         gdk_draw_rgb_32_image (drawing_area->window,
54                                drawing_area->style->black_gc,
55                                0, 0, 
56                                pixbuf->art_pixbuf->width, 
57                                pixbuf->art_pixbuf->height,
58                                GDK_RGB_DITHER_NORMAL, 
59                                pixbuf->art_pixbuf->pixels, 
60                                pixbuf->art_pixbuf->rowstride);
61     } else {
62         gdk_draw_rgb_image (drawing_area->window,
63                             drawing_area->style->white_gc,
64                             0, 0, 
65                             pixbuf->art_pixbuf->width, 
66                             pixbuf->art_pixbuf->height,
67                             GDK_RGB_DITHER_NORMAL,
68                             pixbuf->art_pixbuf->pixels,
69                             pixbuf->art_pixbuf->rowstride);
70     }
71 }  
72
73 config_func (GtkWidget *drawing_area, GdkEventConfigure *event, gpointer data)
74 {
75     GdkPixBuf *pixbuf, *spb;
76     
77     pixbuf = (GdkPixBuf *)gtk_object_get_data(GTK_OBJECT(drawing_area), "pixbuf");
78
79     g_print("X:%d Y:%d\n", event->width, event->height);
80
81     if (((event->width) != (pixbuf->art_pixbuf->width)) ||
82         ((event->height) != (pixbuf->art_pixbuf->height))) {
83          spb = gdk_pixbuf_scale(pixbuf, event->width, event->height);
84          gdk_pixbuf_free (pixbuf);
85          gtk_object_set_data (GTK_OBJECT(drawing_area), "pixbuf", spb);
86     }
87 }
88
89 void
90 new_testrgb_window (GdkPixBuf *pixbuf)
91 {
92   GtkWidget *window;
93   GtkWidget *vbox;
94   GtkWidget *button;
95   GtkWidget *drawing_area;
96   gint w, h;
97  
98   w = pixbuf->art_pixbuf->width;
99   h = pixbuf->art_pixbuf->height;
100
101   window = gtk_widget_new (gtk_window_get_type (),
102                            "GtkObject::user_data", NULL,
103                            "GtkWindow::type", GTK_WINDOW_TOPLEVEL,
104                            "GtkWindow::title", "testrgb",
105                            "GtkWindow::allow_shrink", TRUE,
106                            NULL);
107   gtk_signal_connect (GTK_OBJECT (window), "destroy",
108                       (GtkSignalFunc) quit_func, NULL);
109
110   vbox = gtk_vbox_new (FALSE, 0);
111
112   drawing_area = gtk_drawing_area_new ();
113
114   gtk_drawing_area_size (GTK_DRAWING_AREA(drawing_area), w, h);
115   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
116
117   gtk_signal_connect (GTK_OBJECT(drawing_area), "expose_event",
118                       GTK_SIGNAL_FUNC(expose_func), NULL);
119   gtk_signal_connect (GTK_OBJECT(drawing_area), "configure_event",
120                       GTK_SIGNAL_FUNC (config_func), NULL);
121
122   gtk_object_set_data (GTK_OBJECT(drawing_area), "pixbuf", pixbuf);
123
124   gtk_widget_show (drawing_area);
125
126   button = gtk_button_new_with_label ("Quit");
127   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
128   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
129                              (GtkSignalFunc) gtk_widget_destroy,
130                              GTK_OBJECT (window));
131
132   gtk_widget_show (button);
133
134   gtk_container_add (GTK_CONTAINER (window), vbox);
135   gtk_widget_show (vbox);
136
137   gtk_widget_show (window);
138 }
139
140 int
141 main (int argc, char **argv)
142 {
143   int i;
144   int found_valid = FALSE; 
145
146   GdkPixBuf *pixbuf;
147
148   gtk_init (&argc, &argv);
149
150   gdk_rgb_set_verbose (TRUE);
151
152   gdk_rgb_init ();
153
154   gtk_widget_set_default_colormap (gdk_rgb_get_cmap ());
155   gtk_widget_set_default_visual (gdk_rgb_get_visual ());
156
157   i = 1;
158   for (i = 1; i < argc; i++) {
159       if (argv[i]) {
160           pixbuf = gdk_pixbuf_load_image (argv[i]);
161    
162           if (pixbuf) {
163               new_testrgb_window (pixbuf);
164               found_valid = TRUE;
165             }
166         }
167     }
168
169   if (found_valid)
170     gtk_main ();
171
172   return 0;
173 }