NextPrevUpTopContentsIndex

9.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))
		(with-open-file (stream file)
		   (let ((buffer (make-array 1024 :element-type (stream-element-type stream)
					          :adjustable t
					          :fill-pointer 0)))
		     (do ((char (read-char stream nil nil) (read-char stream nil nil)))
			 ((null char))
			 (vector-push-extend char buffer))
		     (subseq buffer 0)))))))

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.

(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:"
              :text "No file selected."
              :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"))

 


LispWorks CAPI User Guide (Windows version) - 14 Jun 2006

NextPrevUpTopContentsIndex