unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* SVG/PNG images in toolbars?
@ 2017-10-13 15:07 Clément Pit-Claudel
  2017-10-13 16:11 ` Eli Zaretskii
  2017-10-14  2:15 ` Clément Pit-Claudel
  0 siblings, 2 replies; 7+ messages in thread
From: Clément Pit-Claudel @ 2017-10-13 15:07 UTC (permalink / raw)
  To: Emacs developers

Hi all,

Do we support any form of scalable graphics for toolbar icons, or at least any format that supports anti-aliasing? All I can seem to get working is XPM/XBM/PBM, but I can't find a statement that we don't support anything better.

Additionally, I noticed that emacs -Q on my machine seems to ignore the find-image specs of toolbar-mode-map for the default tool bar (that is, running find-image with the specs found in tool-bar-map yields something like (image :type xpm :file "/usr/local/share/emacs/26.0.50/etc/images/refresh.xpm"), but that image looks nothing like the one actually displayed).  strace-ing Emacs suggests that this is because of `x-gtk-stock-map`.

Is there a way to use custom PNG or SVG icons in the toolbar, at least for GTK builds?  Or is x-gtk-stock-map the only option?

Thanks!
Clément.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SVG/PNG images in toolbars?
  2017-10-13 15:07 SVG/PNG images in toolbars? Clément Pit-Claudel
@ 2017-10-13 16:11 ` Eli Zaretskii
  2017-10-13 22:49   ` Clément Pit-Claudel
  2017-10-14  2:15 ` Clément Pit-Claudel
  1 sibling, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2017-10-13 16:11 UTC (permalink / raw)
  To: Clément Pit-Claudel; +Cc: emacs-devel

> From: Clément Pit-Claudel <cpitclaudel@gmail.com>
> Date: Fri, 13 Oct 2017 17:07:23 +0200
> 
> Additionally, I noticed that emacs -Q on my machine seems to ignore the find-image specs of toolbar-mode-map for the default tool bar (that is, running find-image with the specs found in tool-bar-map yields something like (image :type xpm :file "/usr/local/share/emacs/26.0.50/etc/images/refresh.xpm"), but that image looks nothing like the one actually displayed).  strace-ing Emacs suggests that this is because of `x-gtk-stock-map`.

If your build uses GTK, then by default the toolbar comes from GTK as
well, not from Emacs.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SVG/PNG images in toolbars?
  2017-10-13 16:11 ` Eli Zaretskii
@ 2017-10-13 22:49   ` Clément Pit-Claudel
  2017-10-14  6:51     ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Clément Pit-Claudel @ 2017-10-13 22:49 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

On 2017-10-13 18:11, Eli Zaretskii wrote:
>> From: Clément Pit-Claudel <cpitclaudel@gmail.com>
>> Date: Fri, 13 Oct 2017 17:07:23 +0200
>>
>> Additionally, I noticed that emacs -Q on my machine seems to ignore the find-image specs of toolbar-mode-map for the default tool bar (that is, running find-image with the specs found in tool-bar-map yields something like (image :type xpm :file "/usr/local/share/emacs/26.0.50/etc/images/refresh.xpm"), but that image looks nothing like the one actually displayed).  strace-ing Emacs suggests that this is because of `x-gtk-stock-map`.
> 
> If your build uses GTK, then by default the toolbar comes from GTK as
> well, not from Emacs.

Thanks! But I don't follow. What do you mean?
I can add items to that default toolbar using `tool-bar-add-item`; but I only seem to be able to add low-quality icons to it (XPM images), or icons from my GTK theme — I don't seem to be able to add arbitrary PNGs.

Cheers,
Clément.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SVG/PNG images in toolbars?
  2017-10-13 15:07 SVG/PNG images in toolbars? Clément Pit-Claudel
  2017-10-13 16:11 ` Eli Zaretskii
@ 2017-10-14  2:15 ` Clément Pit-Claudel
  2017-10-14  7:13   ` Eli Zaretskii
  1 sibling, 1 reply; 7+ messages in thread
From: Clément Pit-Claudel @ 2017-10-14  2:15 UTC (permalink / raw)
  To: Emacs developers

On 2017-10-13 17:07, Clément Pit-Claudel wrote:
> Do we support any form of scalable graphics for toolbar icons, or at least any format that supports anti-aliasing? All I can seem to get working is XPM/XBM/PBM, but I can't find a statement that we don't support anything better.

Answering my own question: although the convenience functions in tool-bar.el produce find-image specs refering only to XPM/XBM/PBM images, other images do seem to work.  I used the code below to post-process my tool-bar keymap, replacing XMP images with PNGs and SVGs.

Question: could we extend tool-bar--image-expression to return a form including references to SVGs and PNGs? I'd be happy to provide a patch.

Clément.

(defun cpc--tool-bar-add-item (command icon map)
  "Add an ICON running COMMAND to MAP."
  (tool-bar-local-item-from-menu
   command nil map cpc--mode-map
   :vert-only t :cpc--icon icon))

(defun cpc--tool-bar-cleanup-binding (binding)
  "Recompute :image spec in toolbar entry BINDING."
  (pcase binding
    (`(,key menu-item ,doc ,cmd . ,props)
     (-when-let* ((img (plist-get props :cpc--icon)))
       (let ((specs nil))
         (dolist (type '(xpm png svg))
           (push `(:type ,type :file ,(format "%s.%S" img type)) specs))
         (setq props (plist-put props :image `(find-image '(,@specs))))))
     `(,key menu-item ,doc ,cmd . ,props))
    (_ binding)))

(defun cpc--tool-bar-cleanup-map (map)
  "Replace image paths in MAP.
This is a hacky way to work around the fact that
`tool-bar-local-item-from-menu' doesn't include `png' files in
its `find-image' forms."
  (pcase map
    (`(keymap . ,bindings)
     `(keymap . ,(mapcar #'cpc--tool-bar-cleanup-binding bindings)))
    (_ map)))




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SVG/PNG images in toolbars?
  2017-10-13 22:49   ` Clément Pit-Claudel
@ 2017-10-14  6:51     ` Eli Zaretskii
  0 siblings, 0 replies; 7+ messages in thread
From: Eli Zaretskii @ 2017-10-14  6:51 UTC (permalink / raw)
  To: Clément Pit-Claudel; +Cc: emacs-devel

> Cc: emacs-devel@gnu.org
> From: Clément Pit-Claudel <cpitclaudel@gmail.com>
> Date: Sat, 14 Oct 2017 00:49:23 +0200
> 
> > If your build uses GTK, then by default the toolbar comes from GTK as
> > well, not from Emacs.
> 
> Thanks! But I don't follow. What do you mean?

I mean that AFAIU the toolbar icons are looked up using GTK functions,
see gtkutil.c.  I don't know enough about GTK, but it could well be
that the icons are found in places different from what you'd expect by
looking in the Emacs tree.

Once again, I'm hardly an expert on how GTK toolbar works in Emacs, so
apologies if the above is incorrect or misleading.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SVG/PNG images in toolbars?
  2017-10-14  2:15 ` Clément Pit-Claudel
@ 2017-10-14  7:13   ` Eli Zaretskii
  2017-11-19  2:31     ` Clément Pit-Claudel
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2017-10-14  7:13 UTC (permalink / raw)
  To: Clément Pit-Claudel; +Cc: emacs-devel

> From: Clément Pit-Claudel <cpitclaudel@gmail.com>
> Date: Sat, 14 Oct 2017 04:15:15 +0200
> 
> Question: could we extend tool-bar--image-expression to return a form including references to SVGs and PNGs? I'd be happy to provide a patch.

Please show the patch.  Though I admit I don't really understand the
purpose of such a patch: you can already provide an image spec for a
tool-bar button when you write the corresponding define-key
expression, and you can secify any images you like in that spec.
tool-bar--image-expression is for constructing tool-bar items from
menus, AFAIR.

Thanks.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SVG/PNG images in toolbars?
  2017-10-14  7:13   ` Eli Zaretskii
@ 2017-11-19  2:31     ` Clément Pit-Claudel
  0 siblings, 0 replies; 7+ messages in thread
From: Clément Pit-Claudel @ 2017-11-19  2:31 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

On 2017-10-14 03:13, Eli Zaretskii wrote:
>> From: Clément Pit-Claudel <cpitclaudel@gmail.com>
>> Date: Sat, 14 Oct 2017 04:15:15 +0200
>>
>> Question: could we extend tool-bar--image-expression to return a form including references to SVGs and PNGs? I'd be happy to provide a patch.
> 
> Please show the patch.

Something along these lines, roughly:

diff --git a/lisp/tool-bar.el b/lisp/tool-bar.el
index ee01a69..4bc7354 100644
--- a/lisp/tool-bar.el
+++ b/lisp/tool-bar.el
@@ -152,13 +152,17 @@ tool-bar--image-expression
 	 (pbm-spec (append (list :type 'pbm :file
                                  (concat icon ".pbm")) colors))
 	 (xbm-spec (append (list :type 'xbm :file
-                                 (concat icon ".xbm")) colors)))
+                                 (concat icon ".xbm")) colors))
+         (png-spec (append (list :type 'png :file
+                                 (concat icon ".png")) colors))
+         (svg-spec (append (list :type 'svg :file
+                                 (concat icon ".svg")) colors)))
     `(find-image (cond ((not (display-color-p))
 			',(list pbm-spec xbm-spec xpm-lo-spec xpm-spec))
 		       ((< (display-color-cells) 256)
 			',(list xpm-lo-spec xpm-spec pbm-spec xbm-spec))
 		       (t
-			',(list xpm-spec pbm-spec xbm-spec))))))
+			',(list svg-spec png-spec xpm-spec pbm-spec xbm-spec))))))
 
 ;;;###autoload
 (defun tool-bar-local-item (icon def key map &rest props)


> Though I admit I don't really understand the
> purpose of such a patch: you can already provide an image spec for a
> tool-bar button when you write the corresponding define-key
> expression, and you can secify any images you like in that spec.
> tool-bar--image-expression is for constructing tool-bar items from
> menus, AFAIR.

Yes, exactly. Sorry I was unclear.
Let me try to explain better :)

I already have a menu for my major mode, and I'm making a toolbar for it. tool-bar-local-item-from-menu is super convenient for that:

(defvar mymode-tool-bar-map
  (let ((map (make-sparse-keymap)))
    (tool-bar-local-item-from-menu 'mymode-copy "mymode-copy" map mymode-map)
    (tool-bar-local-item-from-menu 'mymode-paste "mymode-paste" map mymode-map)
    (define-key-after map [mymode-sep] '(menu-item "--"))
    (tool-bar-local-item-from-menu 'mymode-reload "mymode-reload" map mymode-map)
    …))

… except this code only works if my icons (mymode-copy, mymode-paste, mymode-reload etc.) are in XPM, PBM, or XBM format.  That's because tool-bar-local-item-from-menu calls tool-bar--image-expression to build the image spec of the new toolbar item, and unfortunately the resulting spec only references XPM, PBM, and XBM files (this is documented in tool-bar-add-item):

> ICON is the base name of a file containing the image to use.  The
> function will first try to use low-color/ICON.xpm if `display-color-cells'
> is less or equal to 256, then ICON.xpm, then ICON.pbm, and finally
> ICON.xbm, using `find-image'
Now suppose I want to use PNG or SVG images in my toolbar.  I can either rebind tool-bar--image-expression to return an image spec including PNGs and SVGs, or duplicate tool-bar-item-from-menu and replace the call to tool-bar--image-expression in the copy, or post-process the keymap after calling tool-bar-item-from-menu.

Neither of these are great, right?

(I'm not super happy with the patch either; maybe it'd be better to introduce a variant of tool-bar-local-item-from-menu that takes an image expression instead of a string for the icon, and use that to implement tool-bar-local-item-from-menu; or maybe it would be better to make a dynamic variable that specifies which image formats to use; or maybe something else?).

Thanks for your time!
Clément.



^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-11-19  2:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-13 15:07 SVG/PNG images in toolbars? Clément Pit-Claudel
2017-10-13 16:11 ` Eli Zaretskii
2017-10-13 22:49   ` Clément Pit-Claudel
2017-10-14  6:51     ` Eli Zaretskii
2017-10-14  2:15 ` Clément Pit-Claudel
2017-10-14  7:13   ` Eli Zaretskii
2017-11-19  2:31     ` Clément Pit-Claudel

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).