7 Defining Interface Classes

7.4 Connecting an interface to an application

Having defined an interface in this way, you can connect it up to your program using callbacks, as described in earlier chapters. Here we define some functions to perform the operations we required for the buttons and menus, and then hook them up to the buttons and menus as callbacks.

The functions to perform the page scrolling operations are given below:

(defun scroll-up (data interface)
 (call-editor (viewer-pane interface) 
              "Scroll Window Up"))

(defun scroll-down (data interface) (call-editor (viewer-pane interface) "Scroll Window Down"))

The functions use the CAPI function call-editor which calls an editor command (given as a string) on an instance of an editor-pane. The editor commands Scroll Window Up and Scroll Window Down perform the necessary operations for Page Up and Page Down respectively.

The function to perform the file-opening operation is given below:

(defun file-choice (data interface)
  (let ((file (prompt-for-file "Select A File:")))
    (when file
      (setf (titled-pane-title (viewer-pane interface)) 
            (format nil "File: ~S" file))
      (setf (editor-pane-text (viewer-pane interface))
            (with-open-file (stream file)
              (let* ((length (file-length stream))
                     (string (make-string length)))
                (dotimes (i length)
                  (setf (schar string i)
                        (read-char stream)))
                   string))))))
This function prompts for a filename and then displays the file in the editor pane.

The function first produces a file prompter through which a file may be selected. Then, the selected file name is shown in the title of the editor pane (using titled-pane-title). Finally, the file name is used to get the contents of the file and display them in the editor pane (usingeditor-pane-text).

The correct callback information for the buttons is specified as shown below:

 (:panes
   (page-up push-button 
            :text "Page Up"
            :selection-callback 'scroll-up)
   (page-down push-button
              :text "Page Down"
              :selection-callback 'scroll-down)
   (open-file push-button
              :text "Open File"
              :selection-callback 'file-choice)
   (viewer editor-pane
           :title "File:"
           :visible-min-height '(:character 8)
           :reader viewer-pane))
All the buttons and menu items operate on the editor paneviewer. A reader is set up to allow access to it.

The correct callback information for the menus is specified as shown below:

 (:menus 
   (file-menu "File"
              (("Open"))
              :selection-callback 'file-choice)
   (page-menu "Page"                       
              (("Page Up" 
                :selection-callback 'scroll-up)
               ("Page Down"
                :selection-callback 'scroll-down)))

In this case, each item in the menu has a different callback. The complete code for the interface is listed below -- try it out.

(define-interface demo ()
  ()
  (:panes
      (page-up push-button
               :text "Page Up"
               :selection-callback 'scroll-up)
      (page-down push-button
                 :text "Page Down"
                 :selection-callback 'scroll-down)
      (open-file push-button
                 :text "Open File"
                 :selection-callback 'file-choice)
      (viewer editor-pane
              :title "File:"
              :visible-min-height '(:character 8)
              :reader viewer-pane))
   (:layouts
     (main-layout column-layout 
                  '(row-of-buttons row-with-editor-pane))
     (row-of-buttons row-layout
                     '(page-up page-down open-file))
     (row-with-editor-pane row-layout
                           '(viewer)))
   (:menus 
     (file-menu "File"
                (("Open"))
                :selection-callback 'file-choice)
     (page-menu "Page"
                (("Page Up" 
                  :selection-callback 'scroll-up)
                 ("Page Down"
                  :selection-callback 'scroll-down))))
   (:menu-bar file-menu page-menu)
   (:default-initargs :title "Demo"))


CAPI User Guide, Liquid Common Lisp Version 5.0 - 2 OCT 1997

Generated with Harlequin WebMaker