PageRenderTime 1235ms CodeModel.GetById 15ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 1217ms

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

http://github.com/tybor/Liberty
Specman e | 169 lines | 21 code | 33 blank | 115 comment | 2 complexity | 4eabb15f1297db4a1480757c9bb80e1a MD5 | raw file
  1indexing
  2	description: "."
  3	copyright: "[
  4					Copyright (C) 2006 Paolo Redaelli, GTK+ team
  5					
  6					This library is free software; you can redistribute it and/or
  7					modify it under the terms of the GNU Lesser General Public License
  8					as published by the Free Software Foundation; either version 2.1 of
  9					the License, or (at your option) any later version.
 10					
 11					This library is distributed in the hope that it will be useful, but
 12					WITHOUT ANY WARRANTY; without even the implied warranty of
 13					MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14					Lesser General Public License for more details.
 15
 16					You should have received a copy of the GNU Lesser General Public
 17					License along with this library; if not, write to the Free Software
 18					Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 19					02110-1301 USA
 20			]"
 21
 22class ROTATED_TEXT
 23
 24creation make
 25
 26feature
 27-- /* Rotated Text
 28--  *
 29--  * This demo shows how to use GDK and Pango to draw rotated and transformed
 30--  * text. The use of GdkPangoRenderer in this example is a somewhat advanced
 31--  * technique; most applications can simply use gdk_draw_layout(). We use
 32--  * it here mostly because that allows us to work in user coordinates - that is,
 33--  * coordinates prior to the application of the transformation matrix, rather
 34--  * than device coordinates.
 35--  *
 36--  * As of GTK+-2.6, the ability to draw transformed and anti-aliased graphics
 37--  * as shown in this example is only present for text. With GTK+-2.8, a new
 38--  * graphics system called "Cairo" will be introduced that provides these
 39--  * capabilities and many more for all types of graphics.
 40--  */
 41-- #include <math.h>
 42-- #include <gtk/gtk.h>
 43
 44-- static GtkWidget *window = NULL;
 45
 46-- static gboolean
 47-- rotated_text_expose_event (GtkWidget      *widget,
 48-- 			   GdkEventExpose *event,
 49-- 			   gpointer	   data)
 50-- {
 51-- #define RADIUS 150
 52-- #define N_WORDS 10
 53-- #define FONT "Sans Bold 27"
 54  
 55--   PangoRenderer *renderer;
 56--   PangoMatrix matrix = PANGO_MATRIX_INIT;
 57--   PangoContext *context;
 58--   PangoLayout *layout;
 59--   PangoFontDescription *desc;
 60
 61--   int width = widget->allocation.width;
 62--   int height = widget->allocation.height;
 63--   double device_radius;
 64--   int i;
 65
 66--   /* Get the default renderer for the screen, and set it up for drawing  */
 67--   renderer = gdk_pango_renderer_get_default (gtk_widget_get_screen (widget));
 68--   gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), widget->window);
 69--   gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), widget->style->black_gc);
 70
 71--   /* Set up a transformation matrix so that the user space coordinates for
 72--    * the centered square where we draw are [-RADIUS, RADIUS], [-RADIUS, RADIUS]
 73--    * We first center, then change the scale */
 74--   device_radius = MIN (width, height) / 2.;
 75--   pango_matrix_translate (&matrix,
 76-- 			  device_radius + (width - 2 * device_radius) / 2,
 77-- 			  device_radius + (height - 2 * device_radius) / 2);
 78--   pango_matrix_scale (&matrix, device_radius / RADIUS, device_radius / RADIUS);
 79
 80--   /* Create a PangoLayout, set the font and text */
 81--   context = gtk_widget_create_pango_context (widget);
 82--   layout = pango_layout_new (context);
 83--   pango_layout_set_text (layout, "Text", -1);
 84--   desc = pango_font_description_from_string (FONT);
 85--   pango_layout_set_font_description (layout, desc);
 86--   pango_font_description_free (desc);
 87
 88--   /* Draw the layout N_WORDS times in a circle */
 89--   for (i = 0; i < N_WORDS; i++)
 90--     {
 91--       GdkColor color;
 92--       PangoMatrix rotated_matrix = matrix;
 93--       int width, height;
 94--       double angle = (360. * i) / N_WORDS;
 95
 96--       /* Gradient from red at angle == 60 to blue at angle == 300 */
 97--       color.red   = 65535 * (1 + cos ((angle - 60) * G_PI / 180.)) / 2;
 98--       color.green = 0;
 99--       color.blue  = 65535  - color.red;
100	
101--       gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer),
102-- 					     PANGO_RENDER_PART_FOREGROUND, &color);
103															
104--       pango_matrix_rotate (&rotated_matrix, angle);
105
106--       pango_context_set_matrix (context, &rotated_matrix);
107	
108--       /* Inform Pango to re-layout the text with the new transformation matrix */
109--       pango_layout_context_changed (layout);
110	
111--       pango_layout_get_size (layout, &width, &height);
112--       pango_renderer_draw_layout (renderer, layout,
113-- 				  - width / 2, - RADIUS * PANGO_SCALE);
114--     }
115
116--   /* Clean up default renderer, since it is shared */
117--   gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer),
118-- 					 PANGO_RENDER_PART_FOREGROUND, NULL);
119--   gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), NULL);
120--   gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), NULL);
121
122--   /* free the objects we created */
123--   g_object_unref (layout);
124--   g_object_unref (context);
125  
126--   return FALSE;
127-- }
128
129-- GtkWidget *
130-- do_rotated_text (GtkWidget *do_widget)
131-- {
132--   GtkWidget *drawing_area;
133  
134--   if (!window)
135--     {
136--       const GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
137		
138--       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
139--       gtk_window_set_screen (GTK_WINDOW (window),
140-- 			     gtk_widget_get_screen (do_widget));
141--       gtk_window_set_title (GTK_WINDOW (window), "Rotated Text");
142
143--       g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
144
145--       drawing_area = gtk_drawing_area_new ();
146--       gtk_container_add (GTK_CONTAINER (window), drawing_area);
147
148--       /* This overrides the background color from the theme */
149--       gtk_widget_modify_bg (drawing_area, GTK_STATE_NORMAL, &white);
150
151--       g_signal_connect (drawing_area, "expose-event",
152-- 			G_CALLBACK (rotated_text_expose_event), NULL);
153
154--       gtk_window_set_default_size (GTK_WINDOW (window), 2 * RADIUS, 2 * RADIUS);
155--     }
156
157--   if (!GTK_WIDGET_VISIBLE (window))
158--     {
159--       gtk_widget_show_all (window);
160--     }
161--   else
162--     {
163--       gtk_widget_destroy (window);
164--       window = NULL;
165--     }
166
167--   return window;
168-- }
169end