File: example_pixbufs.cc

package info (click to toggle)
gtkmm3.0 3.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 159,024 kB
  • ctags: 22,555
  • sloc: xml: 107,819; sh: 11,425; cpp: 7,074; makefile: 241; perl: 235
file content (195 lines) | stat: -rw-r--r-- 4,673 bytes parent folder | download | duplicates (2)
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
/* Pixbufs
 *
 * A GdkPixbuf represents an image, normally in RGB or RGBA format.
 * Pixbufs are normally used to load files from disk and perform
 * image scaling.
 *
 * This demo is not all that educational, but looks cool. It was written
 * by Extreme Pixbuf Hacker Federico Mena Quintero. It also shows
 * off how to use GtkDrawingArea to do a simple animation.
 *
 * Look at the Image demo for additional pixbuf usage examples.
 *
 */

//#include <config.h>

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

#define FRAME_DELAY 50

#define BACKGROUND_NAME "background.jpg"

#ifndef M_PI
#define M_PI 3.1415927
#endif

static const char* image_names[] = {
  "apple-red.png",
  "gnome-applets.png",
  "gnome-calendar.png",
  "gnome-foot.png",
  "gnome-gmush.png",
  "gnome-gimp.png",
  "gnome-gsame.png",
  "gnu-keys.png"
};


enum { N_IMAGES = G_N_ELEMENTS(image_names) };




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

protected:
  virtual void load_pixbufs();

  //signal handlers:
  virtual bool on_drawingarea_draw(const Cairo::RefPtr<Cairo::Context>& cr);
  virtual bool on_timeout();

  //Member widgets:
  Glib::RefPtr<Gdk::Pixbuf> m_refPixbuf;
  Glib::RefPtr<Gdk::Pixbuf> m_refPixbuf_Background;
  Glib::RefPtr<Gdk::Pixbuf> m_images[N_IMAGES];
  Gtk::DrawingArea m_DrawingArea;

  sigc::connection m_TimeoutConnection;
  guint m_back_width, m_back_height;
  gint m_frame_num;
};

//Called by DemoWindow;
Gtk::Window* do_pixbufs()
{
  return new Example_Pixbufs();
}

Example_Pixbufs::Example_Pixbufs()
{
  m_back_width = 0;
  m_back_height = 0;


  set_title("Pixbufs");
  set_resizable(false);

  load_pixbufs();

  set_size_request(m_back_width, m_back_height);
  m_refPixbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, FALSE, 8, m_back_width, m_back_height);
  m_DrawingArea.signal_draw().connect(sigc::mem_fun(*this, &Example_Pixbufs::on_drawingarea_draw));
  add(m_DrawingArea);

  m_TimeoutConnection = Glib::signal_timeout().connect(
      sigc::mem_fun(*this, &Example_Pixbufs::on_timeout), FRAME_DELAY);

  show_all();
}

Example_Pixbufs::~Example_Pixbufs()
{
  m_TimeoutConnection.disconnect(); //Will probably happen anyway, in the destrctor.
}

/* Loads the m_images for the demo and returns whether the operation succeeded */
void Example_Pixbufs::load_pixbufs()
{
  if(m_refPixbuf_Background)
    return; /* already loaded earlier */

  std::string filename_background = BACKGROUND_NAME;

  m_refPixbuf_Background = Gdk::Pixbuf::create_from_file(demo_find_file(filename_background));

  m_back_width = m_refPixbuf_Background->get_width();
  m_back_height = m_refPixbuf_Background->get_height();

  for(unsigned i = 0; i < N_IMAGES; ++i)
  {
    std::string filename = image_names[i];

    Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_file(demo_find_file(filename));

    m_images[i] = pixbuf;
  }
}

/* Draw callback for the drawing area */
bool Example_Pixbufs::on_drawingarea_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
  Gdk::Cairo::set_source_pixbuf(cr, m_refPixbuf_Background);
  cr->paint();

  return true;
}

#define CYCLE_LEN 60


/* Timeout handler to regenerate the frame */
bool Example_Pixbufs::on_timeout()
{
  m_refPixbuf_Background->copy_area( 0, 0, m_back_width, m_back_height, m_refPixbuf, 0, 0);

  double f = (double) (m_frame_num % CYCLE_LEN) / CYCLE_LEN;

  double xmid = m_back_width / 2.0;
  double ymid = m_back_height / 2.0;

  double radius = MIN (xmid, ymid) / 2.0;

  for (int i = 0; i < N_IMAGES; i++)
  {
    double ang = 2.0 * M_PI * (double) i / N_IMAGES - f * 2.0 * M_PI;

    int iw = m_images[i]->get_width();
    int ih = m_images[i]->get_height();

    double r = radius + (radius / 3.0) * sin (f * 2.0 * M_PI);

    double xpos = floor (xmid + r * cos(ang) - iw / 2.0 + 0.5);
    double ypos = floor (ymid + r * sin(ang) - ih / 2.0 + 0.5);

    double k = (i & 1) ? sin (f * 2.0 * M_PI) : cos (f * 2.0 * M_PI);
    k = 2.0 * k * k;
    k = MAX (0.25, k);

    GdkRectangle r1, r2, dest;
    r1.x = (int)xpos;
    r1.y = (int)ypos;
    r1.width = (int)(iw * k);
    r1.height = (int)(ih * k);

    r2.x = 0;
    r2.y = 0;
    r2.width = m_back_width;
    r2.height = m_back_height;

    if (gdk_rectangle_intersect (&r1, &r2, &dest))
    {
      m_images[i]->composite(m_refPixbuf,
		      dest.x, dest.y,
		      dest.width, dest.height,
		      xpos, ypos,
		      k, k,
		      Gdk::INTERP_NEAREST,
		      (int)((i & 1)
		       ? MAX (127, fabs (255 * sin (f * 2.0 * M_PI)))
		       : MAX (127, fabs (255 * cos (f * 2.0 * M_PI)))));
    }
  }

  m_DrawingArea.queue_draw();

  m_frame_num++;
  return true;
}