Skip to content

Conversation

@jjsimpso
Copy link
Contributor

Calling set-string with a column argument other than 0 overwrites content, which doesn't appear to store any data other than the string to display in column 0. This corrupts data returned by get-string.

So,
(send lb set-string "foo" 1)
changes the value returned by
(send lb get-string)

  - calling set-string with a column argument other than 0 overwrites
  content, which doesn't appear to store any data other than the
  string to display in column 0. this breaks future calls to
  get-string.
@jjsimpso
Copy link
Contributor Author

Below is some test code to reproduce or test the fix with. The Up/Down buttons swap items in the list. Each swap corrupts the data in the list.

#lang racket/gui

(define item-list '(("1" . "one") ("2" . "two") ("3" . "three") ("4" . "four") ("5" . "five")))

(define (swap-items distance)
  (define selections (send lb get-selections))
  (when (not (empty? selections))
    (define selected-idx (car selections))
    (define swap-idx (+ selected-idx distance))
    (define max-idx (sub1 (send lb get-number)))
    (when (and (>= swap-idx 0) (<= swap-idx max-idx))
      (define selected-c1 (send lb get-string selected-idx))
      (define selected-c2 (send lb get-data selected-idx))
      (define swap-c1 (send lb get-string swap-idx))
      (define swap-c2 (send lb get-data swap-idx))
      (printf "swapping ~a and ~a~n" selected-c1 swap-c1)
      (send lb set-string swap-idx selected-c1 0)
      (send lb set-string swap-idx selected-c2 1)
      (send lb set-data swap-idx selected-c2)
      (send lb set-string selected-idx swap-c1 0)
      (send lb set-string selected-idx swap-c2 1)
      (send lb set-data selected-idx swap-c2)
      (send lb select swap-idx)
      (printf "selected index was ~a now ~a~n" selected-idx (car (send lb get-selections))))))

(define frame (new frame%
                   [label "list box bug"]
                   [height 512]
                   [width 512]))

(define p (new vertical-pane% [parent frame]))

(define lb
  (new list-box%
       [label ""]
       [parent p]
       [choices '()]
       [style '(single column-headers)]
       [columns '("Column 1" "Column 2")]
       [callback
        (lambda (item event)
          (define selections (send item get-selections))
          (when (not (empty? selections))
            (define selected-idx (car selections))
            (when (eq? (send event get-event-type) 'list-box)
              (printf "selected get-string: ~a~n" (send lb get-string selected-idx)))))]))

(define button-pane
  (new horizontal-pane%
       [parent p]
       [alignment '(center center)]
       [stretchable-height #f]))

(define up-button
  (new button%
       (label "Up")
       (parent button-pane)
       (stretchable-width #f)
       (callback
        (lambda (item event)
          (swap-items -1)))))

(define down-button
  (new button%
       (label "Down")
       (parent button-pane)
       (stretchable-width #f)
       (callback
        (lambda (item event)
          (swap-items 1)))))

(send lb set (map car item-list) (map cdr item-list))

;; save column 1's data since it isn't accessible with get-string
(for ([item (in-list (map cdr item-list))]
      [i (in-naturals 0)])
    (send lb set-data i item))

(send lb set-column-width 0 (exact-truncate (* (send frame get-width) 0.5)) 100 2000)

(send frame show #t)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant