Debian Codegraph @

demos/gtk-demo/example_images.cc

raw
1 /* Images
2 *
3 * GtkImage is used to display an image; the image can be in a number of formats.
4 * Typically, you load an image into a GdkPixbuf, then display the pixbuf.
5 *
6 * This demo code shows some of the more obscure cases, in the simple
7 * case a call to gtk_image_new_from_file() is all you need.
8 *
9 * If you want to put image data in your program as a C variable,
10 * use the make-inline-pixbuf program that comes with GTK+.
11 * This way you won't need to depend on loading external files, your
12 * application binary can be self-contained.
13 */
14
15 #include <gtkmm.h>
16
17 class Example_Images : public Gtk::Window
18 {
19 public:
20 Example_Images();
21 ~Example_Images() override;
22
23 protected:
24 virtual void start_progressive_loading();
25
26 //signal handler:
27 virtual bool on_timeout();
28 virtual void on_loader_area_prepared();
29 virtual void on_loader_area_updated(int x, int y, int width, int height);
30
31 //Member widgets:
32 Gtk::Box m_VBox;
33 Gtk::Label m_Label_Image, m_Label_Animation, m_Label_Progressive;
34 Gtk::Frame m_Frame_Image, m_Frame_Animation, m_Frame_Progressive;
35 Gtk::Image m_Image_Progressive;
36 Glib::RefPtr<Gdk::PixbufLoader> m_refPixbufLoader;
37
38 Glib::RefPtr<Gio::InputStream> m_image_stream;
39 };
40
41 //Called by DemoWindow;
42 Gtk::Window* do_images()
43 {
44 return new Example_Images();
45 }
46
47 Example_Images::Example_Images()
48 :
49 m_VBox (Gtk::ORIENTATION_VERTICAL, 8),
50 m_image_stream ()
51 {
52 set_title("Images");
53 set_border_width(8);
54
55 m_VBox.set_border_width(8);
56 add(m_VBox);
57
58 /* Image */
59
60 m_Label_Image.set_markup("<u>Image loaded from a file</u>");
61 m_VBox.pack_start(m_Label_Image, Gtk::PACK_SHRINK);
62
63 m_Frame_Image.set_shadow_type(Gtk::SHADOW_IN);
64
65 m_Frame_Image.set_halign(Gtk::ALIGN_CENTER);
66 m_Frame_Image.set_valign(Gtk::ALIGN_CENTER);
67 m_VBox.pack_start(m_Frame_Image, Gtk::PACK_SHRINK);
68
69 Gtk::Image* pImage = Gtk::make_managed<Gtk::Image>();
70 pImage->set_from_resource("/images/gtk-logo-rgb.gif");
71 m_Frame_Image.add(*pImage);
72
73 /* Animation */
74
75 m_Label_Animation.set_markup("<u>Animation loaded from a file</u>");
76 m_VBox.pack_start(m_Label_Animation, Gtk::PACK_SHRINK);
77
78 m_Frame_Animation.set_shadow_type(Gtk::SHADOW_IN);
79
80 m_Frame_Animation.set_halign(Gtk::ALIGN_CENTER);
81 m_Frame_Animation.set_valign(Gtk::ALIGN_CENTER);
82 m_VBox.pack_start(m_Frame_Animation, Gtk::PACK_SHRINK);
83
84 pImage = Gtk::make_managed<Gtk::Image>();
85 pImage->set_from_resource("/images/floppybuddy.gif");
86 m_Frame_Animation.add(*pImage);
87
88 /* Progressive */
89
90 m_Label_Progressive.set_markup("<u>Progressive image loading</u>");
91 m_VBox.pack_start(m_Label_Progressive, Gtk::PACK_SHRINK);
92
93 m_Frame_Progressive.set_shadow_type(Gtk::SHADOW_IN);
94
95 m_VBox.pack_start(m_Frame_Progressive, Gtk::PACK_SHRINK);
96
97 /* Create an empty image for now; the progressive loader
98 * will create the pixbuf and fill it in.
99 */
100 m_Frame_Progressive.add(m_Image_Progressive);
101
102 start_progressive_loading();
103
104 show_all();
105 }
106
107 Example_Images::~Example_Images()
108 {
109 try
110 {
111 if(m_refPixbufLoader)
112 m_refPixbufLoader->close();
113 }
114 catch(const Gdk::PixbufError&)
115 {
116 // ignore errors
117 }
118 }
119
120 void Example_Images::start_progressive_loading()
121 {
122 Glib::signal_timeout().connect(sigc::mem_fun(*this, &Example_Images::on_timeout), 150);
123 }
124
125 bool Example_Images::on_timeout()
126 {
127 /* This shows off fully-paranoid error handling, so looks scary.
128 * You could factor out the error handling code into a nice separate
129 * function to make things nicer.
130 */
131
132 if(m_image_stream)
133 {
134 guint8 buf[256];
135 gsize bytes_read = 0;
136
137 try
138 {
139 bytes_read = m_image_stream->read(buf, sizeof(buf));
140 }
141 catch(const Glib::Error& error)
142 {
143
144 Glib::ustring strMsg = "Failure reading image 'alphatest.png': ";
145 strMsg += error.what();
146
147 Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
148 dialog.run();
149
150 m_image_stream.reset();
151
152 return false; // uninstall the timeout
153 }
154
155 try
156 {
157 m_refPixbufLoader->write(buf, bytes_read);
158 }
159 catch(const Glib::Error& error)
160 {
161 Glib::ustring strMsg = "Failed to load image: ";
162 strMsg += error.what();
163
164 Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
165 dialog.run();
166
167 m_image_stream.reset();
168
169 return false; // uninstall the timeout
170 }
171
172 if(bytes_read == 0)
173 {
174 m_image_stream.reset();
175
176 /* Errors can happen on close, e.g. if the image
177 * file was truncated we'll know on close that
178 * it was incomplete.
179 */
180
181 try
182 {
183 m_refPixbufLoader->close();
184 }
185 catch(const Glib::Error& error)
186 {
187 Glib::ustring strMsg = "Failed to close image: ";
188 strMsg += error.what();
189
190 Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
191 dialog.run();
192
193 m_refPixbufLoader.reset();
194
195 return false; // uninstall the timeout
196 }
197
198 m_refPixbufLoader.reset();
199 }
200 }
201 else
202 {
203 try
204 {
205 m_image_stream = Gio::Resource::open_stream_global("/images/alphatest.png");
206 }
207 catch(const Glib::Error& error)
208 {
209 Glib::ustring strMsg = "Unable to open image 'alphatest.png': ";
210 strMsg += error.what();
211
212 Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
213 dialog.run();
214
215 return false; // uninstall the timeout
216 }
217
218 if(m_refPixbufLoader)
219 {
220 m_refPixbufLoader->close();
221
222 m_refPixbufLoader.reset();
223 }
224
225 m_refPixbufLoader = Gdk::PixbufLoader::create();
226
227 m_refPixbufLoader->signal_area_prepared().connect(
228 sigc::mem_fun(*this, &Example_Images::on_loader_area_prepared));
229
230 m_refPixbufLoader->signal_area_updated().connect(
231 sigc::mem_fun(*this, &Example_Images::on_loader_area_updated));
232 }
233
234 return true; // leave timeout installed
235 }
236
237 void Example_Images::on_loader_area_prepared()
238 {
239 const Glib::RefPtr<Gdk::Pixbuf> refPixbuf = m_refPixbufLoader->get_pixbuf();
240
241 /* Avoid displaying random memory contents, since the pixbuf
242 * isn't filled in yet.
243 */
244 refPixbuf->fill(0xaaaaaaff);
245 m_Image_Progressive.set(refPixbuf);
246 }
247
248 void Example_Images::on_loader_area_updated(int/*x*/, int/*y*/, int/*width*/, int/*height*/)
249 {
250 /* We know the pixbuf inside the Gtk::Image has changed, but the image
251 * itself doesn't know this; so give it a hint by setting the pixbuf
252 * again. Queuing a redraw used to be sufficient, but nowadays Gtk::Image
253 * uses GtkIconHelper which caches the pixbuf state and will just redraw
254 * from the cache.
255 * If we wanted to be really efficient, we could use a drawing area or
256 * something instead of a Gtk::Image, so we could control the exact
257 * position of the pixbuf on the display, then we could queue a draw
258 * for only the updated area of the image.
259 */
260 Glib::RefPtr<Gdk::Pixbuf> refPixbuf = m_Image_Progressive.get_pixbuf();
261 m_Image_Progressive.set(refPixbuf);
262 }
263