/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

  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 ROTATED_TEXT
  19. creation make
  20. feature
  21. -- /* Rotated Text
  22. -- *
  23. -- * This demo shows how to use GDK and Pango to draw rotated and transformed
  24. -- * text. The use of GdkPangoRenderer in this example is a somewhat advanced
  25. -- * technique; most applications can simply use gdk_draw_layout(). We use
  26. -- * it here mostly because that allows us to work in user coordinates - that is,
  27. -- * coordinates prior to the application of the transformation matrix, rather
  28. -- * than device coordinates.
  29. -- *
  30. -- * As of GTK+-2.6, the ability to draw transformed and anti-aliased graphics
  31. -- * as shown in this example is only present for text. With GTK+-2.8, a new
  32. -- * graphics system called "Cairo" will be introduced that provides these
  33. -- * capabilities and many more for all types of graphics.
  34. -- */
  35. -- #include <math.h>
  36. -- #include <gtk/gtk.h>
  37. -- static GtkWidget *window = NULL;
  38. -- static gboolean
  39. -- rotated_text_expose_event (GtkWidget *widget,
  40. -- GdkEventExpose *event,
  41. -- gpointer data)
  42. -- {
  43. -- #define RADIUS 150
  44. -- #define N_WORDS 10
  45. -- #define FONT "Sans Bold 27"
  46. -- PangoRenderer *renderer;
  47. -- PangoMatrix matrix = PANGO_MATRIX_INIT;
  48. -- PangoContext *context;
  49. -- PangoLayout *layout;
  50. -- PangoFontDescription *desc;
  51. -- int width = widget->allocation.width;
  52. -- int height = widget->allocation.height;
  53. -- double device_radius;
  54. -- int i;
  55. -- /* Get the default renderer for the screen, and set it up for drawing */
  56. -- renderer = gdk_pango_renderer_get_default (gtk_widget_get_screen (widget));
  57. -- gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), widget->window);
  58. -- gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), widget->style->black_gc);
  59. -- /* Set up a transformation matrix so that the user space coordinates for
  60. -- * the centered square where we draw are [-RADIUS, RADIUS], [-RADIUS, RADIUS]
  61. -- * We first center, then change the scale */
  62. -- device_radius = MIN (width, height) / 2.;
  63. -- pango_matrix_translate (&matrix,
  64. -- device_radius + (width - 2 * device_radius) / 2,
  65. -- device_radius + (height - 2 * device_radius) / 2);
  66. -- pango_matrix_scale (&matrix, device_radius / RADIUS, device_radius / RADIUS);
  67. -- /* Create a PangoLayout, set the font and text */
  68. -- context = gtk_widget_create_pango_context (widget);
  69. -- layout = pango_layout_new (context);
  70. -- pango_layout_set_text (layout, "Text", -1);
  71. -- desc = pango_font_description_from_string (FONT);
  72. -- pango_layout_set_font_description (layout, desc);
  73. -- pango_font_description_free (desc);
  74. -- /* Draw the layout N_WORDS times in a circle */
  75. -- for (i = 0; i < N_WORDS; i++)
  76. -- {
  77. -- GdkColor color;
  78. -- PangoMatrix rotated_matrix = matrix;
  79. -- int width, height;
  80. -- double angle = (360. * i) / N_WORDS;
  81. -- /* Gradient from red at angle == 60 to blue at angle == 300 */
  82. -- color.red = 65535 * (1 + cos ((angle - 60) * G_PI / 180.)) / 2;
  83. -- color.green = 0;
  84. -- color.blue = 65535 - color.red;
  85. -- gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer),
  86. -- PANGO_RENDER_PART_FOREGROUND, &color);
  87. -- pango_matrix_rotate (&rotated_matrix, angle);
  88. -- pango_context_set_matrix (context, &rotated_matrix);
  89. -- /* Inform Pango to re-layout the text with the new transformation matrix */
  90. -- pango_layout_context_changed (layout);
  91. -- pango_layout_get_size (layout, &width, &height);
  92. -- pango_renderer_draw_layout (renderer, layout,
  93. -- - width / 2, - RADIUS * PANGO_SCALE);
  94. -- }
  95. -- /* Clean up default renderer, since it is shared */
  96. -- gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer),
  97. -- PANGO_RENDER_PART_FOREGROUND, NULL);
  98. -- gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), NULL);
  99. -- gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), NULL);
  100. -- /* free the objects we created */
  101. -- g_object_unref (layout);
  102. -- g_object_unref (context);
  103. -- return FALSE;
  104. -- }
  105. -- GtkWidget *
  106. -- do_rotated_text (GtkWidget *do_widget)
  107. -- {
  108. -- GtkWidget *drawing_area;
  109. -- if (!window)
  110. -- {
  111. -- const GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
  112. -- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  113. -- gtk_window_set_screen (GTK_WINDOW (window),
  114. -- gtk_widget_get_screen (do_widget));
  115. -- gtk_window_set_title (GTK_WINDOW (window), "Rotated Text");
  116. -- g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
  117. -- drawing_area = gtk_drawing_area_new ();
  118. -- gtk_container_add (GTK_CONTAINER (window), drawing_area);
  119. -- /* This overrides the background color from the theme */
  120. -- gtk_widget_modify_bg (drawing_area, GTK_STATE_NORMAL, &white);
  121. -- g_signal_connect (drawing_area, "expose-event",
  122. -- G_CALLBACK (rotated_text_expose_event), NULL);
  123. -- gtk_window_set_default_size (GTK_WINDOW (window), 2 * RADIUS, 2 * RADIUS);
  124. -- }
  125. -- if (!GTK_WIDGET_VISIBLE (window))
  126. -- {
  127. -- gtk_widget_show_all (window);
  128. -- }
  129. -- else
  130. -- {
  131. -- gtk_widget_destroy (window);
  132. -- window = NULL;
  133. -- }
  134. -- return window;
  135. -- }
  136. end