File: example_images.cc

package info (click to toggle)
gtkmm2.4 1%3A2.20.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 73,860 kB
  • ctags: 20,553
  • sloc: xml: 79,575; sh: 10,120; cpp: 8,347; makefile: 290
file content (334 lines) | stat: -rw-r--r-- 9,187 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* Images
 *
 * GtkImage is used to display an image; the image can be in a number of formats.
 * Typically, you load an image into a GdkPixbuf, then display the pixbuf.
 *
 * This demo code shows some of the more obscure cases, in the simple
 * case a call to gtk_image_new_from_file() is all you need.
 *
 * If you want to put image data in your program as a C variable,
 * use the make-inline-pixbuf program that comes with GTK+.
 * This way you won't need to depend on loading external files, your
 * application binary can be self-contained.
 */

#include <gtkmm.h>
#include "demo-common.h"

class Example_Images : public Gtk::Window
{
public:
  Example_Images();
  virtual ~Example_Images();

protected:
  virtual void start_progressive_loading();

  //signal handler:
  virtual bool on_timeout();
  virtual void on_loader_area_prepared();
  virtual void on_loader_area_updated(int x, int y, int width, int height);

  //Member widgets:
  Gtk::VBox m_VBox;
  Gtk::Label m_Label_Image, m_Label_Animation, m_Label_Progressive;
  Gtk::Frame m_Frame_Image, m_Frame_Animation, m_Frame_Progressive;
  Gtk::Alignment m_Alignment_Image, m_Alignment_Animation, m_Alignment_Progressive;
  Gtk::Image m_Image_Progressive;
  Glib::RefPtr<Gdk::PixbufLoader> m_refPixbufLoader;

  Glib::RefPtr<Glib::IOChannel> m_image_stream;
};

//Called by DemoWindow;
Gtk::Window* do_images()
{
  return new Example_Images();
}

Example_Images::Example_Images()
:
  m_VBox                (false, 8),
  m_Alignment_Image     (0.5, 0.5, 0, 0),
  m_Alignment_Animation (0.5, 0.5, 0, 0),
  m_image_stream        (0)
{
  set_title("Images");
  set_border_width(8);

  m_VBox.set_border_width(8);
  add(m_VBox);

  /* Image */

  m_Label_Image.set_markup("<u>Image loaded from a file</u>");
  m_VBox.pack_start(m_Label_Image, Gtk::PACK_SHRINK);

  m_Frame_Image.set_shadow_type(Gtk::SHADOW_IN);

  /* The alignment keeps the frame from growing when users resize
   * the window
   */
  m_Alignment_Image.add(m_Frame_Image);
  m_VBox.pack_start(m_Alignment_Image, Gtk::PACK_SHRINK);

  Gtk::Image* pImage = Gtk::manage(new Gtk::Image(demo_find_file("gtk-logo-rgb.gif")));
  m_Frame_Image.add(*pImage);

  /* Animation */

  m_Label_Animation.set_markup("<u>Animation loaded from a file</u>");
  m_VBox.pack_start(m_Label_Animation, Gtk::PACK_SHRINK);

  m_Frame_Animation.set_shadow_type(Gtk::SHADOW_IN);

  /* The alignment keeps the frame from growing when users resize
   * the window
   */
  m_Alignment_Animation.add(m_Frame_Animation);
  m_VBox.pack_start(m_Alignment_Animation, Gtk::PACK_SHRINK);

  pImage = Gtk::manage(new Gtk::Image(demo_find_file("floppybuddy.gif")));
  m_Frame_Animation.add(*pImage);

  /* Progressive */

  m_Label_Progressive.set_markup("<u>Progressive image loading</u>");
  m_VBox.pack_start(m_Label_Progressive, Gtk::PACK_SHRINK);

  m_Frame_Progressive.set_shadow_type(Gtk::SHADOW_IN);

  /* The alignment keeps the frame from growing when users resize
  * the window
  */
  m_Alignment_Progressive.add(m_Frame_Progressive);
  m_VBox.pack_start(m_Alignment_Progressive, Gtk::PACK_SHRINK);

  /* Create an empty image for now; the progressive loader
   * will create the pixbuf and fill it in.
   */
  m_Frame_Progressive.add(m_Image_Progressive);

  start_progressive_loading();

  show_all();
}

Example_Images::~Example_Images()
{
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
    if(m_refPixbufLoader)
      m_refPixbufLoader->close();
  }
  catch(const Gdk::PixbufError&)
  {
    // ignore errors
  }
  #else
  std::auto_ptr<Glib::Error> error;
  if(m_refPixbufLoader)
      m_refPixbufLoader->close(error);
  #endif //GLIBMM_EXCEPTIONS_ENABLED
}

void Example_Images::start_progressive_loading()
{
  Glib::signal_timeout().connect(sigc::mem_fun(*this, &Example_Images::on_timeout), 150);
}

bool Example_Images::on_timeout()
{
  /* This shows off fully-paranoid error handling, so looks scary.
   * You could factor out the error handling code into a nice separate
   * function to make things nicer.
   */

  if(m_image_stream)
  {
    guint8 buf[256];
    gsize bytes_read = 0;
    Glib::IOStatus status = Glib::IO_STATUS_NORMAL;

    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    try
    {
      status = m_image_stream->read(reinterpret_cast<char*>(&buf[0]), sizeof(buf), bytes_read);
    }
    catch(const Glib::Error& error)
    {
    #else
    std::auto_ptr<Glib::Error> error;
    status = m_image_stream->read(reinterpret_cast<char*>(&buf[0]), sizeof(buf), bytes_read, error);
    if(error.get())
    {
    #endif //GLIBMM_EXCEPTIONS_ENABLED
     
      Glib::ustring strMsg = "Failure reading image file 'alphatest.png': ";
      #ifdef GLIBMM_EXCEPTIONS_ENABLED
      strMsg += error.what();
      #else
      strMsg += error->what();
      #endif //GLIBMM_EXCEPTIONS_ENABLED

      Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
      dialog.run();

      m_image_stream.clear();

      return false; // uninstall the timeout
    }

    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    try
    {
      m_refPixbufLoader->write(buf, bytes_read);
    }
    catch(const Glib::Error& error)
    {
    #else
    //std::auto_ptr<Glib::Error> error;
    m_refPixbufLoader->write(buf, bytes_read, error);
    if(error.get())
    {
    #endif //GLIBMM_EXCEPTIONS_ENABLED
      Glib::ustring strMsg = "Failed to load image: ";

      #ifdef GLIBMM_EXCEPTIONS_ENABLED
      strMsg += error.what();
      #else
      strMsg += error->what();
      #endif //GLIBMM_EXCEPTIONS_ENABLED

      Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
      dialog.run();

      m_image_stream.clear();

      return false; // uninstall the timeout
    }

    if(status == Glib::IO_STATUS_EOF)
    {
      m_image_stream.clear();

      /* Errors can happen on close, e.g. if the image
       * file was truncated we'll know on close that
       * it was incomplete.
       */

      #ifdef GLIBMM_EXCEPTIONS_ENABLED
      try
      {
        m_refPixbufLoader->close();
      }
      catch(const Glib::Error& error)
      {
      #else
      std::auto_ptr<Glib::Error> error;
      m_refPixbufLoader->close(error);
      if(error.get())
      {
      #endif //GLIBMM_EXCEPTIONS_ENABLED
        Glib::ustring strMsg = "Failed to load image: ";

        #ifdef GLIBMM_EXCEPTIONS_ENABLED
        strMsg += error.what();
        #else
        strMsg += error->what();
        #endif //GLIBMM_EXCEPTIONS_ENABLED

        Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
        dialog.run();

        m_refPixbufLoader.clear();

        return false; // uninstall the timeout
      }

      m_refPixbufLoader.clear();
    }
  }
  else
  {
    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    try
    {
      m_image_stream = Glib::IOChannel::create_from_file(demo_find_file("alphatest.png"), "r");
    }
    catch(const Glib::Error& error)
    {
    #else
    std::auto_ptr<Glib::Error> error;
    m_image_stream = Glib::IOChannel::create_from_file(demo_find_file("alphatest.png"), "r", error);
    if(error.get())
    {
    #endif //GLIBMM_EXCEPTIONS_ENABLED

      Glib::ustring strMsg = "Unable to open image file 'alphatest.png': ";
      #ifdef GLIBMM_EXCEPTIONS_ENABLED
      strMsg += error.what();
      #else
      strMsg += error->what();
      #endif //GLIBMM_EXCEPTIONS_ENABLED

      Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
      dialog.run();

      return false; // uninstall the timeout
    }

    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    m_image_stream->set_encoding(); // no encoding == binary
    #else
    m_image_stream->set_encoding("", error); // no encoding == binary
    #endif //GLIBMM_EXCEPTIONS_ENABLED

    if(m_refPixbufLoader)
    {
      #ifdef GLIBMM_EXCEPTIONS_ENABLED
      m_refPixbufLoader->close();
      #else
      std::auto_ptr<Glib::Error> error;
      m_refPixbufLoader->close(error);
      #endif //GLIBMM_EXCEPTIONS_ENABLED

      m_refPixbufLoader.clear();
    }

    m_refPixbufLoader = Gdk::PixbufLoader::create();

    m_refPixbufLoader->signal_area_prepared().connect(
        sigc::mem_fun(*this, &Example_Images::on_loader_area_prepared));

    m_refPixbufLoader->signal_area_updated().connect(
        sigc::mem_fun(*this, &Example_Images::on_loader_area_updated));
  }

  return true; // leave timeout installed
}

void Example_Images::on_loader_area_prepared()
{
  const Glib::RefPtr<Gdk::Pixbuf> refPixbuf = m_refPixbufLoader->get_pixbuf();

  /* Avoid displaying random memory contents, since the pixbuf
   * isn't filled in yet.
   */
  refPixbuf->fill(0xaaaaaaff);
  m_Image_Progressive.set(refPixbuf);
}

void Example_Images::on_loader_area_updated(int/*x*/, int/*y*/, int/*width*/, int/*height*/)
{
  /* We know the pixbuf inside the Gtk::Image has changed, but the image
   * itself doesn't know this; so queue a redraw.  If we wanted to be
   * really efficient, we could use a drawing area or something
   * instead of a GtkImage, so we could control the exact position of
   * the pixbuf on the display, then we could queue a draw for only
   * the updated area of the image.
   */
  m_Image_Progressive.queue_draw();
}