sample/examples/drawing.rb in gtk4-4.0.3 vs sample/examples/drawing.rb in gtk4-4.0.4

- old
+ new

@@ -32,21 +32,17 @@ # Draw a rectangle on the surface at the given position def draw_brush(widget, surface, x, y) cr = Cairo::Context.new(surface) cr.rectangle(x - 3, y - 3, 6, 6) cr.fill - widget.queue_draw_area(x - 3, y - 3, 6, 6) + widget.queue_draw end def generate_surface_from_widget(widget) - surface = nil - if widget.window - surface = widget.window.create_similar_surface(Cairo::CONTENT_COLOR, - widget.allocated_width, - widget.allocated_height) - end - surface + widget.native.surface.create_similar_surface(Cairo::CONTENT_COLOR, + widget.allocated_width, + widget.allocated_height) end myapp = Gtk::Application.new("org.gtk.example", :flags_none) myapp.signal_connect "activate" do |app| @@ -58,18 +54,14 @@ win.signal_connect "destroy" do surface.destroy if surface win.destroy end - frame = Gtk::Frame.new - frame.shadow_type = Gtk::ShadowType::IN - win.add(frame) - drawing_area = Gtk::DrawingArea.new # Set a minimum size drawing_area.set_size_request(100, 100) - frame.add(drawing_area) + win.set_child(drawing_area) drawing_area.set_draw_func do |da, cr| if surface.nil? surface = generate_surface_from_widget(da) ## Initialize the surface to white @@ -80,19 +72,20 @@ # clipped to only draw the exposed areas of the widget cr.set_source(surface, 0, 0) cr.paint end - drawing_area.signal_connect_after "size-allocate" do |da, alloc| + drawing_area.signal_connect_after "resize" do |da, alloc| surface.destroy if surface surface = generate_surface_from_widget(da) # Initialize the surface to white clear_surface(surface) if surface end - drag = Gtk::GestureDrag.new(drawing_area) + drag = Gtk::GestureDrag.new drag.button = Gdk::BUTTON_PRIMARY + drawing_area.add_controller(drag) start_x = 0.0 start_y = 0.0 drag.signal_connect "drag-begin" do |gesture, x, y| @@ -107,11 +100,12 @@ drag.signal_connect "drag-end" do |gesture, x, y| draw_brush(drawing_area, surface, start_x + x, start_y + y) end - press = Gtk::GestureMultiPress.new(drawing_area) + press = Gtk::GestureClick.new press.button = Gdk::BUTTON_SECONDARY + drawing_area.add_controller(press) press.signal_connect "pressed" do |gesture, n_press, x, y| clear_surface(surface) drawing_area.queue_draw end win.show