unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Felix Dietrich <felix.dietrich@sperrhaken.name>
To: Jean Louis <bugs@gnu.support>
Cc: Help GNU Emacs <help-gnu-emacs@gnu.org>
Subject: Re: How to create SVG image as clickable button?
Date: Sun, 25 Sep 2022 11:01:23 +0200	[thread overview]
Message-ID: <87leq7bzfg.fsf@sperrhaken.name> (raw)
In-Reply-To: <Yy96vZ5QRWgNDxF2@protected.localdomain> (Jean Louis's message of "Sun, 25 Sep 2022 00:46:37 +0300")

Hello Jean Louis,

Jean Louis <bugs@gnu.support> writes:

> I am now here, trying to make image as clickable button, need help
> please.
>
> (let ((image (create-image "/home/data1/protected/Programming/git/haiku-icons/png/64x64/Alert_Info.png"
> 			   nil nil :action 'my-function)))
>   (insert-image image))

in the documentation for Emacs 28.1, I cannot find an image property
:action.  For buttons, though, I can find a property ‘action’.  Note
that there is no colon in front of ‘action’: it is not a keyword symbol
but a proper one.

With the following snippet, I managed to insert a clickable image button
at the end of the current buffer:

#+begin_src emacs-lisp
  (save-excursion
    (goto-char (point-max))
    (let* ((path "path/to/image")
           (image (create-image path)))
      ;; (insert-button (propertize " " 'display image)
      ;;                'action …
      (insert-button " "
                     'display image
                     'action (lambda (button)
                               (message "Button clicked")))))
#+end_src


You could also try to insert an image with custom keybindings:

#+begin_src emacs-lisp
  (save-excursion
    (let* ((start (point))
           (keymap (let ((map (make-sparse-keymap)))
                     ;; The default keymap for images inserted with
                     ;; ‘insert-image’ is ‘image-map’.  It is a global
                     ;; variable.
                     (set-keymap-parent map image-map)
                     (define-key map [mouse-1]
                       (lambda () (interactive)
                         (message "Hello World from mouse click!")))
                     (define-key map [?h]
                       (lambda () (interactive)
                         (message "Hello World from key press!")))
                     map))
           (path "path/to/image")
           (image (create-image path)))
  
      (goto-char (point-max))
      (insert-image image)
      (add-text-properties start (point) `(keymap ,keymap))))
#+end_src


-- 
Felix Dietrich



  reply	other threads:[~2022-09-25  9:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-24  5:26 How to create SVG image as clickable button? Jean Louis
2022-09-24 21:46 ` Jean Louis
2022-09-25  9:01   ` Felix Dietrich [this message]
2022-09-25 11:07     ` Jean Louis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87leq7bzfg.fsf@sperrhaken.name \
    --to=felix.dietrich@sperrhaken.name \
    --cc=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).