Skip to content

Commit

Permalink
Gui, Add, Picture: Allow specifying a common icon name instead of path
Browse files Browse the repository at this point in the history
  • Loading branch information
phil294 committed Jul 5, 2023
1 parent 8069c94 commit 17f5abe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6090,7 +6090,7 @@ <h2 class="calibre9"><span class="calibre23">The "Last Found" Window </span></h2
<p class="calibre8">The Hotkey control has limited capabilities. For example, it does not support mouse/joystick hotkeys or the Windows key (LWin and RWin). One way to work around this is to provide one or more <a href="#Gui.htm__CheckBox" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">checkboxes</a> as a means for the user to enable extra modifiers such as the Windows key.</p>
</div>
<p class="calibre8"><br class="calibre12" />
<strong class="calibre14"><span class="calibre30"><a id="Gui.htm__Picture" href="#Gui.htm__Picture" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">#</a> Picture</span></strong> (or <strong class="calibre14"><span class="calibre30">Pic</span></strong>): An area containing an image (see last paragraph for supported file types). The <em class="calibre21">Text</em> parameter is the filename of the image, which is assumed to be in <a href="#Variables.htm__WorkingDir" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">%A_WorkingDir%</a> if an absolute path isn't specified. <span class="tbd">To retain the image's actual width and/or height, omit the W and/or H options. Otherwise, the image is scaled to the specified width and/or height. To shrink or enlarge the image while preserving its aspect ratio, specify -1 for one of the dimensions and a positive number for the other. For example, specifying "w200 h-1" would make the image 200 pixels wide and cause its height to be set automatically. If the picture cannot be loaded or displayed (e.g. file not found), the control is left empty and its height and width are set to zero.</span>
<strong class="calibre14"><span class="calibre30"><a id="Gui.htm__Picture" href="#Gui.htm__Picture" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">#</a> Picture</span></strong> (or <strong class="calibre14"><span class="calibre30">Pic</span></strong>): An area containing an image (see last paragraph for supported file types). The <em class="calibre21">Text</em> parameter is the filename of the image, which is assumed to be in <a href="#Variables.htm__WorkingDir" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">%A_WorkingDir%</a> if an absolute path isn't specified. <span class="x11">You can also specify a <a href="https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html">standard icon name</a> by prefixing it with "icon:". Example: Gui, Add, Picture, , icon:appointment-new.</span> To retain the image's actual width and/or height, omit the W and/or H options. Otherwise, the image is scaled to the specified width and/or height. To shrink or enlarge the image while preserving its aspect ratio, specify -1 for one of the dimensions and a positive number for the other. For example, specifying "w200 h-1" would make the image 200 pixels wide and cause its height to be set automatically. If the picture cannot be loaded or displayed (e.g. file not found), the control is left empty and its height and width are set to zero.
</p>
<p class="calibre8">To use a picture as a background for other controls, the picture should normally be added prior to those controls. However, if those controls are input-capable and the picture has a <a href="#Gui.htm__label" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">g-label</a>, create the picture after the other controls and include +0x4000000 (which is WS_CLIPSIBLINGS) in the picture's <em class="calibre21">Options</em>. This trick also allows a picture to be the background behind a Tab control.</p>
<div class="tbd">
Expand Down Expand Up @@ -6465,7 +6465,7 @@ <h2 class="calibre9"><span class="calibre23">The "Last Found" Window </span></h2
<p class="calibre8"> </p>
<p class="calibre8"><strong class="calibre14">Sub-commands</strong></p>
<p class="calibre8"><strong class="calibre14">(Blank)</strong>: Leave <em class="calibre21">Sub-command</em> blank to put new contents into the control via <em class="calibre21">Param3</em>. All control types are self-explanatory except the following:</p>
<p class="calibre8 tbd"><a href="#Gui.htm__Picture" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Picture</a>: <em class="calibre21">Param3</em> should be the filename of the new image to load (see <a href="#Gui.htm__Picture" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Gui</a> for supported file types). In v1.0.24+, one or more of the following options may be specified immediately in front of the filename: *wN (width N), *hN (height N), and *IconN (icon number N). In the following example, the second icon is loaded with a width of 100 and an automatic height to "keep aspect ratio": <em class="calibre21">GuiControl,, MyPic, *icon2 *w100 *h-1 C:\My Application.exe</em>. Specify <em class="calibre21">*w0 *h0</em> to use the image's actual width and height. If *w and *h are omitted, the image will be scaled to fit the current size of the control. Note: Use only one space or tab between the final option and the filename itself; any other spaces and tabs are treated as part of the filename.</p>
<p class="calibre8"><a href="#Gui.htm__Picture" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Picture</a>: <em class="calibre21">Param3</em> should be the filename of the new image to load (see <a href="#Gui.htm__Picture" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Gui</a> for supported file types). <span class="tbd">In v1.0.24+, one or more of the following options may be specified immediately in front of the filename: *wN (width N), *hN (height N), and *IconN (icon number N). In the following example, the second icon is loaded with a width of 100 and an automatic height to "keep aspect ratio": <em class="calibre21">GuiControl,, MyPic, *icon2 *w100 *h-1 C:\My Application.exe</em>. Specify <em class="calibre21">*w0 *h0</em> to use the image's actual width and height. If *w and *h are omitted, the image will be scaled to fit the current size of the control. Note: Use only one space or tab between the final option and the filename itself; any other spaces and tabs are treated as part of the filename.</span></p>
<p class="calibre8"><a href="#Gui.htm__Edit" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Edit</a>: <span class="rm">Any linefeeds (`n) in <em class="calibre21">Param3</em> that lack a preceding carriage return (`r) are automatically translated to CR+LF (`r`n) to make them display properly. However, this is usually transparent because the "Gui Submit" and "GuiControlGet OutputVar" commands will automatically undo this translation by replacing CR+LF with LF (`n).</span></p>
<p class="calibre8 tbd"><a href="#Gui.htm__Hotkey" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Hotkey</a>: <em class="calibre21">Param3</em> can be blank to clear the control, or a set of modifiers with a key name. Examples: ^!c, ^Numpad1, +Home. The only modifiers supported are ^ (Control), ! (Alt), and + (Shift). See the <a href="#KeyList.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">key list</a> for available key names.</p>
<p class="calibre8"><a href="#Gui.htm__Checkbox" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Checkbox</a>: <em class="calibre21">Param3</em> can be 0 to uncheck the button, 1 to check it, or -1 to give it a gray checkmark. Otherwise, <em class="calibre21">Param3</em> is assumed to be the control's new caption/text. See <strong class="calibre14">Text</strong> below for how to override this behavior.</p>
Expand Down
19 changes: 16 additions & 3 deletions src/cmd/gtk/gui/gui-add.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,27 @@ 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"
widget = ::Gtk::Image.new_from_file text
if text.starts_with?("icon:")
size = case w > 1 ? w : h > 1 ? h : 48
when 16 then ::Gtk::IconSize::Menu
when 24 then ::Gtk::IconSize::LargeToolbar
when 32 then ::Gtk::IconSize::Dnd
when 48 then ::Gtk::IconSize::Dialog
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
else
widget = ::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 w == -1
if w == -1 || w == 1
w = (h * pixbuf.width / pixbuf.height).to_i
elsif h == -1
elsif h == -1 || h == 1
h = (w * pixbuf.height / pixbuf.width).to_i
end
pixbuf_scaled = pixbuf.scale_simple w, h, GdkPixbuf::InterpType::Bilinear
Expand Down

0 comments on commit 17f5abe

Please sign in to comment.