Skip to content

Commit

Permalink
Gui, Add, Picture: Fix bad clipping when x/y were specified
Browse files Browse the repository at this point in the history
  • Loading branch information
phil294 committed Jul 5, 2023
1 parent 17f5abe commit 0a0704d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
18 changes: 11 additions & 7 deletions src/cmd/gtk/gui/gui-add.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class Cmd::Gtk::Gui::GuiAdd < Cmd::Base
widget.active = ((opt["choose"][:n] || 1_i64) - 1).to_i if opt["choose"]?
widget.changed_signal.connect run_g_label
when "picture", "pic"
# Shortest, non-canonical way: Doesn't work because when setting x/y the image is clipped (why????)
# widget.has_window = true
# widget.events = ::Gdk::EventMask::ButtonPressMask.to_i
# widget.button_press_event_signal.connect run_g_label.unsafe_as(Proc(Gdk::EventButton, Bool))
widget = ::Gtk::EventBox.new
widget.button_press_event_signal.connect run_g_label.unsafe_as(Proc(Gdk::EventButton, Bool))
if text.starts_with?("icon:")
size = case w > 1 ? w : h > 1 ? h : 48
when 16 then ::Gtk::IconSize::Menu
Expand All @@ -71,23 +77,21 @@ class Cmd::Gtk::Gui::GuiAdd < Cmd::Base
else
raise Run::RuntimeException.new "For Gui pictures with icon paths, the height/width needs to be either 16, 24, 32, 48 or unspecified."
end
widget = ::Gtk::Image.new_from_icon_name text[5..], size.value.to_i
img = ::Gtk::Image.new_from_icon_name text[5..], size.value.to_i
else
widget = ::Gtk::Image.new_from_file text
img = ::Gtk::Image.new_from_file text
end
widget.has_window = true
widget.events = ::Gdk::EventMask::ButtonPressMask.to_i
widget.button_press_event_signal.connect run_g_label.unsafe_as(Proc(Gdk::EventButton, Bool))
# Icon pictures have no pixbuf, so this applies only to external files
if (pixbuf = widget.pixbuf) && (w > -1 || h > -1)
if (pixbuf = img.pixbuf) && (w > -1 || h > -1)
if w == -1 || w == 1
w = (h * pixbuf.width / pixbuf.height).to_i
elsif h == -1 || h == 1
h = (w * pixbuf.height / pixbuf.width).to_i
end
pixbuf_scaled = pixbuf.scale_simple w, h, GdkPixbuf::InterpType::Bilinear
widget.pixbuf = pixbuf_scaled if pixbuf_scaled
img.pixbuf = pixbuf_scaled if pixbuf_scaled
end
widget.add img
else
widget = ::Gtk::Label.new text
widget.has_window = true
Expand Down
13 changes: 7 additions & 6 deletions src/cmd/gtk/gui/gui-control.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ class Cmd::Gtk::Gui::GuiControl < Cmd::Base
ctrl.label = value
when ::Gtk::Entry
ctrl.text = value
when ::Gtk::Image
pixbuf_before = ctrl.pixbuf
LibGtk.gtk_widget_get_size_request(ctrl.to_unsafe, out w, out h)
ctrl.from_file = value
if (pixbuf_after = ctrl.pixbuf) && (w > -1 || h > -1)
when ::Gtk::EventBox
img = ctrl.children[0].unsafe_as(::Gtk::Image)
pixbuf_before = img.pixbuf
LibGtk.gtk_widget_get_size_request(img.to_unsafe, out w, out h)
img.from_file = value
if (pixbuf_after = img.pixbuf) && (w > -1 || h > -1)
if w == -1
w = (h * pixbuf_after.width / pixbuf_after.height).to_i
elsif h == -1
h = (w * pixbuf_after.height / pixbuf_after.width).to_i
end
pixbuf_after_scaled = pixbuf_after.scale_simple w, h, GdkPixbuf::InterpType::Bilinear
ctrl.pixbuf = pixbuf_after_scaled if pixbuf_after_scaled
img.pixbuf = pixbuf_after_scaled if pixbuf_after_scaled
end
else
raise Run::RuntimeException.new "GuiControl not yet supported for element of type #{ctrl.class}, sorry."
Expand Down

0 comments on commit 0a0704d

Please sign in to comment.