绘制

许多插件,比如buttons,自己就做了它们所有的绘制工作。比如你仅仅需要告诉它们你想看到的标签、你想它们使用的字体、绘制按钮的轮廓和焦点矩形。有时候,有必要做些自定义的绘制。在这种情况下,一个 GtkDrawingArea控件可能是正确的选择,这个控件提供了一个画布,在这个画布上你可以绘制并且将其连接到”draw“信号。

控件的内容常常需要被部分或者全部重新绘制。比如,当另一个窗口控件被移动并且露出控件的一部分,或者当包含它的窗口重新调整大小时,也会导致控件的部分或者全部被重新绘制。通过调用 gtk_widget_queue_draw()或者它的变体,GTK+提供一个现成的cairo给绘制信号从而实现众多细节。

下面的程序将会展示一个绘制信号句柄。这个例子比之前的略微复杂,因为它也通过button_pressmotion_notify句柄显示出输入活动。

drawingpng

Example 3. Drawing in response to input

新建一个名为 example-3.c 的文件,写入如下内容:

  1. #include <gtk/gtk.h>
  2. /* Surface to store current scribbles */
  3. static cairo_surface_t *surface = NULL;
  4. static void clear_surface (void)
  5. {
  6. cairo_t *cr;
  7. cr = cairo_create (surface);
  8. cairo_set_source_rgb (cr, 1, 1, 1);
  9. cairo_paint (cr);
  10. cairo_destroy (cr);
  11. }
  12. /* Create a new surface of the appropriate size to store our scribbles */
  13. static gboolean
  14. configure_event_cb (GtkWidget *widget,
  15. GdkEventConfigure *event,
  16. gpointer data)
  17. {
  18. if (surface)
  19. cairo_surface_destroy (surface);
  20. surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
  21. CAIRO_CONTENT_COLOR,
  22. gtk_widget_get_allocated_width (widget),
  23. gtk_widget_get_allocated_height (widget));
  24. /* Initialize the surface to white */
  25. clear_surface ();
  26. /* We've handled the configure event, no need for further processing. */
  27. return TRUE;
  28. }
  29. /* Redraw the screen from the surface. Note that the ::draw
  30. * * signal receives a ready-to-be-used cairo_t that is already
  31. * * clipped to only draw the exposed areas of the widget
  32. * */
  33. static gboolean draw_cb (GtkWidget *widget,
  34. cairo_t *cr,
  35. gpointer data)
  36. {
  37. cairo_set_source_surface (cr, surface, 0, 0);
  38. cairo_paint (cr);
  39. return FALSE;
  40. }
  41. /* Draw a rectangle on the surface at the given position */
  42. static void draw_brush (GtkWidget *widget,
  43. gdouble x,
  44. gdouble y)
  45. {
  46. cairo_t *cr;
  47. /* Paint to the surface, where we store our state */
  48. cr = cairo_create (surface);
  49. cairo_rectangle (cr, x - 3, y - 3, 6, 6);
  50. cairo_fill (cr);
  51. cairo_destroy (cr);
  52. /* Now invalidate the affected region of the drawing area. */
  53. gtk_widget_queue_draw_area (widget, x - 3, y - 3, 6, 6);
  54. }
  55. /* Handle button press events by either drawing a rectangle
  56. * or clearing the surface, depending on which button was pressed.
  57. * The ::button-press signal handler receives a GdkEventButton
  58. * struct which contains this information.
  59. */
  60. static gboolean button_press_event_cb (GtkWidget *widget,
  61. GdkEventButton *event,
  62. gpointer data)
  63. {
  64. /* paranoia check, in case we haven't gotten a configure event */
  65. if (surface == NULL)
  66. return FALSE;
  67. if (event->button == GDK_BUTTON_PRIMARY)
  68. {
  69. draw_brush (widget, event->x, event->y);
  70. }
  71. else if (event->button == GDK_BUTTON_SECONDARY)
  72. {
  73. clear_surface ();
  74. gtk_widget_queue_draw (widget);
  75. }
  76. /* We've handled the event, stop processing */
  77. return TRUE;
  78. }
  79. /* Handle motion events by continuing to draw if button 1 is
  80. * still held down. The ::motion-notify signal handler receives
  81. * a GdkEventMotion struct which contains this information.
  82. */
  83. static gboolean
  84. motion_notify_event_cb (GtkWidget *widget,
  85. GdkEventMotion *event,
  86. gpointer data)
  87. {
  88. /* paranoia check, in case we haven't gotten a configure event */
  89. if (surface == NULL)
  90. return FALSE;
  91. if (event->state & GDK_BUTTON1_MASK)
  92. draw_brush (widget, event->x, event->y);
  93. /* We've handled it, stop processing */
  94. return TRUE;
  95. }
  96. static void close_window (void)
  97. {
  98. if (surface)
  99. cairo_surface_destroy (surface);
  100. gtk_main_quit ();
  101. }
  102. int main (int argc, char *argv[])
  103. {
  104. GtkWidget *window;
  105. GtkWidget *frame;
  106. GtkWidget *da;
  107. gtk_init (&argc, &argv);
  108. window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  109. gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
  110. g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
  111. gtk_container_set_border_width (GTK_CONTAINER (window), 8);
  112. frame = gtk_frame_new (NULL);
  113. gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  114. gtk_container_add (GTK_CONTAINER (window), frame);
  115. da = gtk_drawing_area_new ();
  116. /* set a minimum size */
  117. gtk_widget_set_size_request (da, 100, 100);
  118. gtk_container_add (GTK_CONTAINER (frame), da);
  119. /* Signals used to handle the backing surface */
  120. g_signal_connect (da, "draw",
  121. G_CALLBACK (draw_cb), NULL);
  122. g_signal_connect (da,"configure-event",
  123. G_CALLBACK (configure_event_cb), NULL);
  124. /* Event signals */
  125. g_signal_connect (da, "motion-notify-event",
  126. G_CALLBACK (motion_notify_event_cb), NULL);
  127. g_signal_connect (da, "button-press-event",
  128. G_CALLBACK (button_press_event_cb), NULL);
  129. /* Ask to receive events the drawing area doesn't normally
  130. * subscribe to. In particular, we need to ask for the
  131. * button press and motion notify events that want to handle.
  132. */
  133. gtk_widget_set_events (da, gtk_widget_get_events (da)
  134. | GDK_BUTTON_PRESS_MASK
  135. | GDK_POINTER_MOTION_MASK);
  136. gtk_widget_show_all (window);
  137. gtk_main ();
  138. return 0;
  139. }

然后在终端输入以下命令用GCC编译程序:

  1. gcc `pkg-config --cflags gtk+-3.0` -o example-3 example-3.c `pkg-config --libs gtk+-3.0`