问题描述
我一直在尝试学习 mcclim,鉴于文档的简洁性,这非常困难。阅读 the manual 后,我一直无法弄清楚如何将点击与窗格相关联并运行命令。我知道我可以定义命令,例如:
(define-app-command (com-say :name t) ()
(format t "Hello World!"))
然后在命令框中键入 Say
以使其执行某些操作。我想单击一个窗格并让它在单击时输出这个 hello world 字符串。
我需要设置什么才能在给定窗格上启用点击侦听器?
解决方法
至少有两种方法可以做到这一点。第一个将使用 presentations
和 presentation-to-command-translator
,第二个将使用小工具(又名小部件),如 push-button
。我想您还没有了解 presentations
,所以我会向您展示如何使用小工具来做到这一点。
下面的示例将有一个窗格和一个按钮。当您单击按钮时,您会看到“Hello World!”输出到窗格。
;;;; First Load McCLIM,then save this in a file and load it.
(in-package #:clim-user)
(define-application-frame example ()
()
(:panes
(app :application
:scroll-bars :vertical
:width 400
:height 400)
(button :push-button
:label "Greetings"
:activate-callback
(lambda (pane &rest args)
(declare (ignore pane args))
;; In McCLIM,`t` or *standard-output is bound to the first pane of
;; type :application,in this case `app` pane.
(format t "Hello World!~%" ))))
(:layouts
(default (vertically () app button))))
(defun run ()
(run-frame-top-level (make-application-frame 'example)))
(clim-user::run)
P.S. 在 McCLIM 中学习如何做某事的一种方法是运行并查看 clim-demos
。一旦您发现一些有趣的东西并想知道它是如何完成的,请在 McCLIM 源代码的 Examples
目录中查看其源代码。
如果需要帮助,最好使用 IRC 聊天(libera.chat 上的#clim),多个 McCLIM 开发人员在那里闲逛。
编辑:第二个带有 presentation-to-command-translator
的示例,单击窗格中的任意位置将输出“Hello World!”在窗格中。
(in-package #:clim-user)
(define-application-frame example ()
()
(:pane :application
:width 400
:display-time nil))
;; Note the `:display-time nil` option above,without it default McCLIM command-loop
;; will clear the pane after each time the command is run. It wasn't needed in previous
;; example because there were no commands involved.
(define-example-command (com-say :name t)
()
(format t "Hello World!~%"))
(define-presentation-to-command-translator say-hello
(blank-area com-say example :gesture :select)
(obj)
nil)
(defun run ()
(run-frame-top-level (make-application-frame 'example)))
(clim-user::run)