/src/wrappers/gtk/examples/gtk-demo/pixbufs.e

http://github.com/tybor/Liberty · Specman e · 298 lines · 21 code · 68 blank · 209 comment · 2 complexity · 3155bb4d0e5953d03a8e316ca116e524 MD5 · raw file

  1. indexing
  2. description: "."
  3. copyright: "[
  4. Copyright (C) 2006 Paolo Redaelli, GTK+ team
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA
  17. ]"
  18. class PIXBUFS
  19. creation make
  20. feature
  21. -- /* Pixbufs
  22. -- *
  23. -- * A GdkPixbuf represents an image, normally in RGB or RGBA format.
  24. -- * Pixbufs are normally used to load files from disk and perform
  25. -- * image scaling.
  26. -- *
  27. -- * This demo is not all that educational, but looks cool. It was written
  28. -- * by Extreme Pixbuf Hacker Federico Mena Quintero. It also shows
  29. -- * off how to use GtkDrawingArea to do a simple animation.
  30. -- *
  31. -- * Look at the Image demo for additional pixbuf usage examples.
  32. -- *
  33. -- */
  34. -- #include <stdlib.h>
  35. -- #include <gtk/gtk.h>
  36. -- #include <math.h>
  37. -- #include "demo-common.h"
  38. -- #define FRAME_DELAY 50
  39. -- #define BACKGROUND_NAME "background.jpg"
  40. -- static const char *image_names[] = {
  41. -- "apple-red.png",
  42. -- "gnome-applets.png",
  43. -- "gnome-calendar.png",
  44. -- "gnome-foot.png",
  45. -- "gnome-gmush.png",
  46. -- "gnome-gimp.png",
  47. -- "gnome-gsame.png",
  48. -- "gnu-keys.png"
  49. -- };
  50. -- #define N_IMAGES G_N_ELEMENTS (image_names)
  51. -- /* demo window */
  52. -- static GtkWidget *window = NULL;
  53. -- /* Current frame */
  54. -- static GdkPixbuf *frame;
  55. -- /* Background image */
  56. -- static GdkPixbuf *background;
  57. -- static gint back_width, back_height;
  58. -- /* Images */
  59. -- static GdkPixbuf *images[N_IMAGES];
  60. -- /* Widgets */
  61. -- static GtkWidget *da;
  62. -- /* Loads the images for the demo and returns whether the operation succeeded */
  63. -- static gboolean
  64. -- load_pixbufs (GError **error)
  65. -- {
  66. -- gint i;
  67. -- char *filename;
  68. -- if (background)
  69. -- return TRUE; /* already loaded earlier */
  70. -- /* demo_find_file() looks in the the current directory first,
  71. -- * so you can run gtk-demo without installing GTK, then looks
  72. -- * in the location where the file is installed.
  73. -- */
  74. -- filename = demo_find_file (BACKGROUND_NAME, error);
  75. -- if (!filename)
  76. -- return FALSE; /* note that "error" was filled in and returned */
  77. -- background = gdk_pixbuf_new_from_file (filename, error);
  78. -- g_free (filename);
  79. -- if (!background)
  80. -- return FALSE; /* Note that "error" was filled with a GError */
  81. -- back_width = gdk_pixbuf_get_width (background);
  82. -- back_height = gdk_pixbuf_get_height (background);
  83. -- for (i = 0; i < N_IMAGES; i++)
  84. -- {
  85. -- filename = demo_find_file (image_names[i], error);
  86. -- if (!filename)
  87. -- return FALSE; /* Note that "error" was filled with a GError */
  88. -- images[i] = gdk_pixbuf_new_from_file (filename, error);
  89. -- g_free (filename);
  90. -- if (!images[i])
  91. -- return FALSE; /* Note that "error" was filled with a GError */
  92. -- }
  93. -- return TRUE;
  94. -- }
  95. -- /* Expose callback for the drawing area */
  96. -- static gint
  97. -- expose_cb (GtkWidget *widget,
  98. -- GdkEventExpose *event,
  99. -- gpointer data)
  100. -- {
  101. -- guchar *pixels;
  102. -- int rowstride;
  103. -- rowstride = gdk_pixbuf_get_rowstride (frame);
  104. -- pixels = gdk_pixbuf_get_pixels (frame) + rowstride * event->area.y + event->area.x * 3;
  105. -- gdk_draw_rgb_image_dithalign (widget->window,
  106. -- widget->style->black_gc,
  107. -- event->area.x, event->area.y,
  108. -- event->area.width, event->area.height,
  109. -- GDK_RGB_DITHER_NORMAL,
  110. -- pixels, rowstride,
  111. -- event->area.x, event->area.y);
  112. -- return TRUE;
  113. -- }
  114. -- #define CYCLE_LEN 60
  115. -- static int frame_num;
  116. -- /* Timeout handler to regenerate the frame */
  117. -- static gint
  118. -- timeout (gpointer data)
  119. -- {
  120. -- double f;
  121. -- int i;
  122. -- double xmid, ymid;
  123. -- double radius;
  124. -- gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
  125. -- frame, 0, 0);
  126. -- f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
  127. -- xmid = back_width / 2.0;
  128. -- ymid = back_height / 2.0;
  129. -- radius = MIN (xmid, ymid) / 2.0;
  130. -- for (i = 0; i < N_IMAGES; i++)
  131. -- {
  132. -- double ang;
  133. -- int xpos, ypos;
  134. -- int iw, ih;
  135. -- double r;
  136. -- GdkRectangle r1, r2, dest;
  137. -- double k;
  138. -- ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
  139. -- iw = gdk_pixbuf_get_width (images[i]);
  140. -- ih = gdk_pixbuf_get_height (images[i]);
  141. -- r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
  142. -- xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
  143. -- ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
  144. -- k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
  145. -- k = 2.0 * k * k;
  146. -- k = MAX (0.25, k);
  147. -- r1.x = xpos;
  148. -- r1.y = ypos;
  149. -- r1.width = iw * k;
  150. -- r1.height = ih * k;
  151. -- r2.x = 0;
  152. -- r2.y = 0;
  153. -- r2.width = back_width;
  154. -- r2.height = back_height;
  155. -- if (gdk_rectangle_intersect (&r1, &r2, &dest))
  156. -- gdk_pixbuf_composite (images[i],
  157. -- frame,
  158. -- dest.x, dest.y,
  159. -- dest.width, dest.height,
  160. -- xpos, ypos,
  161. -- k, k,
  162. -- GDK_INTERP_NEAREST,
  163. -- ((i & 1)
  164. -- ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
  165. -- : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
  166. -- }
  167. -- gtk_widget_queue_draw (da);
  168. -- frame_num++;
  169. -- return TRUE;
  170. -- }
  171. -- static guint timeout_id;
  172. -- static void
  173. -- cleanup_callback (GtkObject *object,
  174. -- gpointer data)
  175. -- {
  176. -- g_source_remove (timeout_id);
  177. -- timeout_id = 0;
  178. -- }
  179. -- GtkWidget *
  180. -- do_pixbufs (GtkWidget *do_widget)
  181. -- {
  182. -- if (!window)
  183. -- {
  184. -- GError *error;
  185. -- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  186. -- gtk_window_set_screen (GTK_WINDOW (window),
  187. -- gtk_widget_get_screen (do_widget));
  188. -- gtk_window_set_title (GTK_WINDOW (window), "Pixbufs");
  189. -- gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  190. -- g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
  191. -- g_signal_connect (window, "destroy", G_CALLBACK (cleanup_callback), NULL);
  192. -- error = NULL;
  193. -- if (!load_pixbufs (&error))
  194. -- {
  195. -- GtkWidget *dialog;
  196. -- dialog = gtk_message_dialog_new (GTK_WINDOW (window),
  197. -- GTK_DIALOG_DESTROY_WITH_PARENT,
  198. -- GTK_MESSAGE_ERROR,
  199. -- GTK_BUTTONS_CLOSE,
  200. -- "Failed to load an image: %s",
  201. -- error->message);
  202. -- g_error_free (error);
  203. -- g_signal_connect (dialog, "response",
  204. -- G_CALLBACK (gtk_widget_destroy), NULL);
  205. -- gtk_widget_show (dialog);
  206. -- }
  207. -- else
  208. -- {
  209. -- gtk_widget_set_size_request (window, back_width, back_height);
  210. -- frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
  211. -- da = gtk_drawing_area_new ();
  212. -- g_signal_connect (da, "expose_event",
  213. -- G_CALLBACK (expose_cb), NULL);
  214. -- gtk_container_add (GTK_CONTAINER (window), da);
  215. -- timeout_id = g_timeout_add (FRAME_DELAY, timeout, NULL);
  216. -- }
  217. -- }
  218. -- if (!GTK_WIDGET_VISIBLE (window))
  219. -- {
  220. -- gtk_widget_show_all (window);
  221. -- }
  222. -- else
  223. -- {
  224. -- gtk_widget_destroy (window);
  225. -- window = NULL;
  226. -- g_object_unref (frame);
  227. -- }
  228. -- return window;
  229. -- }
  230. end