s c h e m a t i c s : c o o k b o o k

/ Cookbook.SimpleGridLayout

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Simple Grid Layout

Problem

You want to lay out your widgets in grids.

Solution

This is possible by using a bunch of horizontal-panel% instances. For example:

(module phone-buttons mzscheme
  (require (lib "class.ss")
           (lib "mred.ss" "mred"))

  (define ((make-button parent) label)
    (new button% 
         [label (format "~a" label)]
         [parent parent]))
  
  (define frame (new frame% [label "test"]))
  (define row-1 (make-object horizontal-panel% frame))
  (for-each (make-button row-1) '(1 2 3))
  (define row-2 (make-object horizontal-panel% frame))
  (for-each (make-button row-2) '(4 5 6))
  (define row-3 (make-object horizontal-panel% frame))
  (for-each (make-button row-3) '(7 8 9))
  (define row-4 (make-object horizontal-panel% frame))
  (for-each (make-button row-4) '(* 0 \#))
  (send frame show #t))

Another approach involves writing one's own layout container class.

(module simple-grid mzscheme
  (require (lib "class.ss")
           (lib "list.ss")
           (lib "plt-match.ss")
           (lib "mred.ss" "mred"))

  (provide simple-grid-panel%)
  (define simple-grid-panel%
    (class panel%
      (init [columns-per-row 3])
      (define -columns-per-row columns-per-row)
      (super-new)
      (override container-size place-children)
      
      ;; container-size: (listof (list number number boolean boolean))
      ;;                 -> (values number number)
      ;; Returns our container's minimum size, given the size of
      ;; the children.
      (define (container-size info)
        (let-values ([(child-width child-height)
                      (uniform-child-size info)]
                     [(number-of-rows)
                      (ceiling (/ (length info) -columns-per-row))])
          (values
           (* child-width -columns-per-row)
           (* child-height number-of-rows))))
      
      
      ;; uniform-child-size:
      ;;     (listof (list number number boolean boolean))
      ;;                 -> (values number number)
      ;; returns the minimum width and height that all the children
      ;; can fit into.
      (define (uniform-child-size info)
        (let ([widths (map first info)]
              [heights (map second info)])
          (values (apply max widths)
                  (apply max heights))))
      
      
      ;; place-children: (listof (list integer integer boolean boolean))
      ;;                 integer integer ->
      ;;                 (listof (list integer integer integer integer)
      (define (place-children info width height)
        (let-values ([(child-width child-height)
                      (uniform-child-size info)])
          (let loop ([info info]
                     [i 0])
            (match info
              [(list (list width height horiz-stretch? vert-stretch?)
                     rest ...)
               (cons (list (* child-width (remainder i -columns-per-row))
                           (* child-height (quotient i -columns-per-row))
                           width
                           height)
                     (loop rest (add1 i)))]
              [else empty])))))))

With this, the phone-number example at the beginning can be written more simply:

  (define (phone-numbers)
    (define ((make-button parent) label)
      (new button% 
           [label (format "~a" label)]
           [parent parent]))
    
    (define frame (new frame% [label "test"]))
    (define grid (new simple-grid-panel% [parent frame]))
    (for-each (make-button grid) '(1 2 3
                                   4 5 6
                                   7 8 9
                                   * 0 \#))
    (send frame show #t))

Discussion


Comments about this recipe

Contributors

-- DannyYoo - 23 Apr 2006

CookbookForm
TopicType: Recipe
ParentTopic: GUIRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest