File: drawingarea.rb

package info (click to toggle)
ruby-gnome2 2.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,360 kB
  • ctags: 14,222
  • sloc: ansic: 85,875; ruby: 38,232; sh: 80; xml: 41; makefile: 22
file content (201 lines) | stat: -rw-r--r-- 6,518 bytes parent folder | download
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
196
197
198
199
200
201
# Copyright (c) 2003-2014 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
# $Id: drawingarea.rb,v 1.5 2005/02/12 23:02:43 kzys Exp $
=begin
= Drawing Area

Gtk::DrawingArea is a blank area where you can draw custom displays
of various kinds.

This demo has two drawing areas. The checkerboard area shows
how you can just draw something; all you have to do is write
a signal handler for expose_event, as shown here.

The 'scribble' area is a bit more advanced, and shows how to handle
events such as button presses and mouse motion. Click the mouse
and drag in the scribble area to draw squiggles. Resize the window
to clear the area.
=end

require "common"

module Demo
  class DrawingArea < BasicWindow
    def initialize
      # Pixmap for scribble area, to store current scribbles
      @cairo_context = nil
      super("Drawing Area")

      self.border_width = 8

      vbox = Gtk::Box.new(:vertical, 8)
      vbox.border_width = 8
      add(vbox)

      ## Create the checkerboard area
      label = Gtk::Label.new
      label.set_markup("<u>Checkerboard pattern</u>")
      vbox.pack_start(label, :expand => false, :fill => false, :padding => 0)

      frame = Gtk::Frame.new
      frame.shadow_type = :in
      vbox.pack_start(frame, :expand => true, :fill => true, :padding => 0)

      da = Gtk::DrawingArea.new
      # set a minimum size
      da.set_size_request(100, 100)

      frame.add(da)

      da.signal_connect("draw") do |widget, event|
        checkerboard_draw(widget, event)
      end

      ## Create the scribble area
      label = Gtk::Label.new
      label.set_markup("<u>Scribble area</u>")
      vbox.pack_start(label, :expand => false, :fill => false, :padding => 0)

      frame = Gtk::Frame.new
      frame.shadow_type = :in
      vbox.pack_start(frame, :expand => true, :fill => true, :padding => 0)

      da = Gtk::DrawingArea.new
      # set a minimum size
      da.set_size_request(100, 100)

      frame.add(da)

      # Signals used to handle backing pixmap
      da.signal_connect("draw") do |*args|
        scribble_draw(*args)
      end
      da.signal_connect("configure-event") do |widget, event|
        scribble_configure_event(widget)
      end

      # Event signals
      da.signal_connect("motion-notify-event") do |*args|
        scribble_motion_notify_event(*args)
      end
      da.signal_connect("button-press-event") do |*args|
        scribble_button_press_event(*args)
      end

      # Ask to receive events the drawing area doesn't normally
      # subscribe to
      da.events |= (Gdk::Event::LEAVE_NOTIFY_MASK |
                    Gdk::Event::BUTTON_PRESS_MASK |
                    Gdk::Event::POINTER_MOTION_MASK |
                    Gdk::Event::POINTER_MOTION_HINT_MASK)
    end

    # Create a new surface of the appropriate size to store our scribbles
    def scribble_configure_event(widget)
      @cairo_context = widget.window.create_cairo_context
      @cairo_context.set_source_rgb(1, 1, 1)
      @cairo_context.paint

      # We've handled the configure event, no need for further processing.
      return true
    end

    CHECK_SIZE = 10
    SPACING = 2
    def checkerboard_draw(da, cairo_context)
      # At the start of a draw handler, a clip region of event.area
      # is set on the window, and event.area has been cleared to the
      # widget's background color. The docs for
      # gdk_window_begin_paint_region give more details on how this
      # works.

      xcount = 0
      width  = da.allocated_width
      height = da.allocated_height
      SPACING.step(da.allocation.width, CHECK_SIZE + SPACING) do |i|
        ycount = xcount % 2; # start with even/odd depending on row
        SPACING.step(da.allocation.height, CHECK_SIZE + SPACING) do |j|
          if ycount % 2 == 1
            cairo_context.set_source_rgb(0.45777, 0, 0.45777)
          else
            cairo_context.set_source_rgb(1, 1, 1)
          end

          # If we're outside the clip, this will do nothing.
          cairo_context.fill do
            cairo_context.rectangle(i, j, CHECK_SIZE, CHECK_SIZE)
          end
          ycount += 1
        end
        xcount += 1
      end
      # return true because we've handled this event, so no
      # further processing is required.
      #
      return true
    end

    # Redraw the screen from the surface
    def scribble_draw(widget, event)
      event.set_source(@cairo_context.target)
      event.paint
      return false
    end

    # Draw a rectangle on the screen
    def draw_brush(widget, x, y)
      update_rect = Gdk::Rectangle.new(x - 3, y - 3, 6, 6)

      # Paint to the surface, where we store our state
      @cairo_context.fill do
        @cairo_context.set_source_rgb(0, 0, 0)
        @cairo_context.rectangle(update_rect.x,
                                 update_rect.y,
                                 update_rect.width,
                                 update_rect.height)
      end

      # Now invalidate the affected region of the drawing area.
      widget.window.invalidate(update_rect, false)
    end

    def scribble_button_press_event(widget, event)
      # paranoia check, in case we haven't gotten a configure event
      return false unless @cairo_context

      if event.button == 1
        draw_brush(widget, event.x, event.y)
      end

      # We've handled the event, stop processing
      return true
    end

    def scribble_motion_notify_event(widget, event)
      # paranoia check, in case we haven't gotten a configure event
      return false unless @cairo_context

      # This call is very important; it requests the next motion event.
      # If you don't call Gdk::Window#pointer you'll only get
      # a single motion event. The reason is that we specified
      # Gdk::POINTER_MOTION_HINT_MASK to Gtk::Widget#set_events.
      # If we hadn't specified that, we could just use event.x, event.y
      # as the pointer location. But we'd also get deluged in events.
      # By requesting the next event as we handle the current one,
      # we avoid getting a huge number of events faster than we
      # can cope.

      # NOTE: Gdk::EventMotion#window will be restored from Ruby/GDK3 2.1.1.
      # win, x, y, state = event.window.get_device_position(event.device)
      win, x, y, state = widget.window.get_device_position(event.device)

      if (state & :button1_mask) != 0
        draw_brush(widget, x, y)
      end

      # We've handled it, stop processing
      return true
    end
  end
end