All Manuals > CAPI User Guide and Reference Manual > 10 Defining Interface Classes - top level windows

NextPrevUpTopContentsIndex

10.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 generic 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-object-title (viewer-pane interface)) 
            (format nil "File: ~S" file))
      (setf (editor-pane-text (viewer-pane interface))
            (file-string file)))))

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-object-title). Finally, the file name is used to get the contents of the file and display them in the editor pane (using editor-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:"
           :text "No file selected."
           :visible-min-height '(:character 8)
           :reader viewer-pane))

All the buttons and menu items operate on the editor pane viewer. 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.

(capi:define-interface demo ()
  ()
  (:panes
      (page-up capi:push-button
               :text "Page Up"
               :selection-callback 'scroll-up)
      (page-down capi:push-button
                 :text "Page Down"
                 :selection-callback 'scroll-down)
      (open-file capi:push-button
                 :text "Open File"
                 :selection-callback 'file-choice)
      (viewer capi:editor-pane
              :title "File:"
              :text "No file selected."
              :visible-min-height '(:character 8)
              :reader viewer-pane))
   (:layouts
     (main-layout capi:column-layout 
                  '(row-of-buttons viewer))
     (row-of-buttons capi:row-layout
                     '(page-up page-down open-file)))
   (: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 and Reference Manual (Macintosh version) - 25 Feb 2015

NextPrevUpTopContentsIndex