;; toolbar-settings.el --- Tool bar settings  -*- mode: emacs-lisp; lexical-binding: t -*-

;;; Commentary:
;; icon name and item name must be the same, e.g. pepper
;; "/home/daniel/.config/emacs/icon/pepper.xpm"
;; defining toolbar items
;; emacs look into the data-directory to find files.
;; either copy global etc folder to local and overwrite the path to local
;; (setq data-directory "/home/daniel/.config/emacs/icon/")  ;; default "/usr/share/emacs/28.0.50/etc/"
;; or copy local files to global etc folder
;; then add item with name of icon name, e. g. pepper because of pepper.xpm
;; but before changing data-directory, better add local icon folder to the image-load-path

;; Requirements:
;; dialog.el https://www.emacswiki.org/emacs/Dialog https://www.emacswiki.org/emacs/dialog.el https://www.emacswiki.org/emacs/download/dialog.el

;;; Code:

;;(setq tool-bar-style "image")  ;; uses GTK+ toolkit / does not work

(use-package x-win
  :defer t
  :config
  (when (boundp 'x-gtk-stock-map)
    ;; icon-map-list (x-gtk-stock-map)
    (delete '("etc/images/diropen" . "n:system-file-manager") x-gtk-stock-map)
    (delete '("etc/images/close" . ("window-close" "gtk-close")) x-gtk-stock-map)
    (delete '("etc/images/cancel" . "gtk-cancel") x-gtk-stock-map)
    (mapc (lambda (item) (add-to-list 'x-gtk-stock-map item))
          '(
            ;; key . value
            ;; key: file names without extension and with two directory components
            ;; value: either "stock-icon", "n:named-icon" or ("named-icon" "stock-icon") (or is it like: Let stock be a list of icon names to try.)
            ("etc/images/diropen" . ("n:folder-stash" "n:system-file-manager"))
            ("etc/images/close" . ("n:document-close" "window-close" "gtk-close"))
            ("etc/images/cancel" . ("n:dialog-close" "gtk-cancel"))
            
            ("images/newsticker/narrow" . "format-justify-fill")
            ("etc/images/next-node" . "document-export")
            ("etc/images/refresh" . ("document-revert" "refresh" "gtk-convert"))
            ("etc/images/exit" . ("n:application-exit" "n:system-shutdown" "exit" "gtk-quit"))
            ))))

(use-package dialog
  :defer t
  :config
  (defun my-org-export-dialog-html-fun ()
    "Ask."
    (interactive)
    ;;(my-org-export-dialog)
    ;;(previous-buffer)
    ;;(get-buffer "*Messages*")
    ;;(switch-to-buffer "*Messages*")
    ;;(message "ja")
    ;;(my-org-export-html
    (when t
      (message "ja")
      (previous-buffer)
      (my-org-export-html)
      (next-buffer)))
  (setq my-org-export-dialog-html-var 'my-org-export-dialog-html-fun)
  (defun my-org-export-dialog-pdf-fun ()
    "Ask."
    (interactive)
    (when t
      (message "ja")
      (previous-buffer)
      (my-org-export-pdf)
      (next-buffer)))
  (setq my-org-export-dialog-pdf-var 'my-org-export-dialog-pdf-fun)

  (dialog-define my-org-export-dialog
    '(:style window
             [navigation 1 1 :tag "Navigation"]
             [text 3 1 "Export org document:"]
             ;;[text 4 1 "HTML"]
             ;; :notify needs a variable which points to a function
             [button-quit 4 1 :tag "HTML" :notify my-org-export-dialog-html-var]
             ;;[text 5 1 "PDF (LaTeX)"]
             [button-quit 5 1 :tag "PDF (LaTeX)" :notify my-org-export-dialog-pdf-var]
             [button-quit 7 1]
             [text 8 1 ""])
    "This is my-org-export dialog.")
  ;; (my-org-export-dialog)
  )


(defun my-org-export-ivy ()
  ""
  (setq my-org-export-list
        '(("HTML" . my-org-export-html)
          ("HTML async" . my-org-export-html-async)
          ("PDF (LaTeX)" . my-org-export-pdf)
          ("PDF (LaTeX) async" . my-org-export-pdf-async)))
  (ivy-read
   "Org export: "
   ;;'("HTML" "PDF (LaTeX)")
   ;;'(my-org-export-html "PDF (LaTeX)")
   my-org-export-list
   :action '(1  ;; index (1 based) of the default action
             ("s" (lambda (x)
                    ;;(message x)
                    ;;(command-execute 'x)
                    ;;(call-interactively x)
                    ;; get the value from the key value pair and call value (fun)
                    (funcall (cdr x))
                    )
              "switch"))))

(defun my-org-export ()
  ""
  (interactive)
  ;;(my-org-export-dialog)
  ;;(dialog-run 'my-org-export-dialog)
  (my-org-export-ivy))
;;(my-org-export)
;;(makunbound 'my-org-export)

;; (describe-key [tool-bar])
;; see tool-bar.el - tool-bar-setup
;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Tool-Bar.html
;; auto-resize-tool-bars
(use-package tool-bar
  :defer t
  :config
  ;; add local icon folder to the image-load-path
  (add-to-list 'image-load-path (concat config-dir "icon/"))
  
  ;; TODO: revert after close?
  ;; TODO: add redo?
  ;; TODO: add toggle-window-split with icon transform-rotate? or view-multiple-objects
  ;; TODO: add swap windows left with right with icon view-pages-facing? or better view-split-left-right? text-frame-link
  ;; TODO: export to html; icon viewhtml?
  ;; TODO: export to pdf: icon viewpdf?
  ;;(define-key-after (default-value 'tool-bar-map) [separator-6] menu-bar-separator 'kill-buffer)

  ;; ("etc/images/spell" "tools-check-spelling" "gtk-spell-check")
  ;;(tool-bar-add-item-from-menu 'ispell-buffer "spell")
  (tool-bar-add-item "spell"
                     #'flyspell-buffer
                     'flyspell-buffer
                     :help "Run function `flyspell-buffer'")

  (tool-bar-add-item "languagetool.org"
                     #'langtool-check-buffer
                     'langtool-check-buffer
                     :help "Run function `langtool-check-buffer'")

  ;; fill, using the defined logic of Menu - Edit - Fill using function fill-region
  (tool-bar-add-item "newsticker/narrow"
                     #'fill-region
                     'fill-region
                     :help "Fill text in region to fit between left and right margin `fill-region'")
  ;;(tool-bar-add-item-from-menu (lookup-key menu-bar-edit-menu [fill])
  ;;			                       "images/newsticker/narrow" nil :vert-only t)

  ;; add separator
  (define-key-after (default-value 'tool-bar-map) [separator-4] menu-bar-separator)

  ;; ("etc/images/refresh" "view-refresh" "gtk-refresh")
  (tool-bar-add-item "refresh"
                     #'revert-buffer
                     'revert-buffer
                     :help "Re-read current buffer from its file `revert-buffer'")
  ;;(tool-bar-add-item-from-menu (lookup-key menu-bar-file-menu [revert-buffer])
  ;;			                       "refresh" nil :vert-only t)

  ;; text-html
  ;;(tool-bar-add-item "export_html"
  ;;                   #'my-org-export-html
  ;;                   'my-org-export-html
  ;;                   :help "Run function `my-org-export-html'")
  ;; x-office-document
  ;;(tool-bar-add-item "export_pdf"
  ;;                   #'my-org-export-pdf
  ;;                   'my-org-export-pdf
  ;;                   :help "Run function `my-org-export-pdf'")
  (tool-bar-add-item "next-node"
                     #'my-org-export
                     'my-org-export
                     :enable '(eq major-mode 'org-mode)
                     :help "Run function `my-org-export'")

  ;; add separator
  (define-key-after (default-value 'tool-bar-map) [separator-5] menu-bar-separator)

  (tool-bar-add-item "cancel"
                     #'keyboard-escape-quit
                     'keyboard-escape-quit
                     :help "Run function `keyboard-escape-quit'")

  (tool-bar-add-item "exit"
                     #'kill-emacs
                     'kill-emacs
                     :help "Exit the Emacs job and kill it `kill-emacs'")
  ;;(tool-bar-add-item-from-menu (lookup-key menu-bar-file-menu [Kill\ Emacs])
  ;;			                       "exit" nil :vert-only t)
  )

(provide 'toolbar-settings)
;;; toolbar-settings.el ends here
