All Manuals > CAPI User Guide > 16 Drag and Drop > 16.3 Dropping > 16.3.2 Dropping in a choice

NextPrevUpTopContentsIndex

16.3.2.1 Example: dropping in a list

This drop-callback simply appends the dropped string at the end of the list:

(defun list-drop-callback (pane drop-object stage)
  (format t "list drop callback ~S ~S ~S" pane  drop-object stage)
  (case stage
    (:formats 
     (set-drop-object-supported-formats drop-object
                                        (list :string)))
    (:drag
     (when (and (drop-object-provides-format drop-object
                                             :string)
                (drop-object-allows-drop-effect-p drop-object
                                                  :copy))
       (setf (drop-object-drop-effect drop-object) :copy)))
    (:drop
     (when (and (drop-object-provides-format drop-object
                                             :string)
                (drop-object-allows-drop-effect-p drop-object
                                                  :copy))
       (setf (drop-object-drop-effect drop-object) :copy)
       (add-list-item pane drop-object)))))
 
(defun add-list-item (pane drop-object)
  (append-items
        pane 
        (list (string-capitalize 
               (drop-object-get-object drop-object 
                                       pane :string)))))
 
(contain
 (make-instance 'list-panel
                :title "Shopping list"
                :items (list "Tea" "Bread")
                :drop-callback 'list-drop-callback))

Try dragging an item from the tree-view created in Example: dragging from a tree.

Below is a more sophisticated version of add-list-item which inserts the item at the expected position within the list. This position is obtained using drop-object-collection-index :

(defun add-list-item (pane drop-object)
  (multiple-value-bind (index placement)
      (drop-object-collection-index drop-object)
    (list-panel-add-item pane 
                         (string-capitalize 
                          (drop-object-get-object
                           drop-object pane :string))
                         index placement))))))
 
(defun list-panel-add-item (pane item index placement)
  (let ((item-count (count-collection-items pane)))
    (let ((adjusted-index (if (eq placement :above)
                              index
                            (1+ index)))
          (current-items (collection-items pane)))
      (setf (collection-items pane)
            (concatenate 'simple-vector
                         (subseq current-items 0 adjusted-index)
                         (vector item)
                         (subseq current-items adjusted-index 
                                 item-count))))))

CAPI User Guide (Unix version) - 30 Aug 2011

NextPrevUpTopContentsIndex