home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / tcl / 1305 < prev    next >
Encoding:
Text File  |  1992-09-04  |  3.0 KB  |  104 lines

  1. Newsgroups: comp.lang.tcl
  2. Path: sparky!uunet!decwrl!parc!welch
  3. From: welch@parc.xerox.com (Brent Welch)
  4. Subject: Placing diaglog boxes
  5. Message-ID: <welch.715131551@corvina>
  6. Summary: Here's how I place dialog boxes
  7. Keywords: TK
  8. Sender: news@parc.xerox.com
  9. Organization: Xerox PARC
  10. Date: 29 Aug 92 23:39:11 GMT
  11. Lines: 91
  12.  
  13. In response to someones comment about not being able to
  14. put a dialog box up in the middle of a top-level,
  15. here's how I do it for my editor, mxedit.
  16.  
  17.  
  18. #
  19. # placePopUp -
  20. #    Place a popup relative to its parent window
  21. #
  22. proc placePopUp { widget {where center} } {
  23.     # mxedit is the main editing widget.  It exports a gridsize
  24.     # command that gives the bounding box size of characters and
  25.     # is the basis for using the "wm grid" command
  26.     global mxedit
  27.  
  28.     # The screenwidth command is just a call to
  29.     # winfo screenwidth $mxedit
  30.     # The error checking is historical
  31.     if {[string compare [screenwidth] unknown] == 0} {
  32.     set screenWidth [lindex [exec xwininfo -root | egrep Width:] 2]
  33.     set screenHeight [lindex [exec xwininfo -root | egrep Height:] 2]
  34.     }
  35.  
  36.     # Figure out where we are
  37.     scan [wm geometry .] "%dx%d+%d+%d" charsWide linesHigh xoff yoff
  38. #    puts stderr "Geometry $charsWide $linesHigh $xoff $yoff"
  39.  
  40.     # Use gridding parameters to get our width and height
  41.     set gridWidth [lindex [$mxedit gridsize] 0]
  42.     set gridHeight [lindex [$mxedit gridsize] 1]
  43.     set mainWidth [expr {$charsWide * $gridWidth}]
  44.     set mainHeight [expr {$linesHigh * $gridHeight}]
  45.  
  46.     # Hide the diaglog box
  47.     wm withdraw $widget
  48.     update
  49.     scan [wm geometry $widget] "%dx%d" itsWidth itsHeight    
  50. #    puts stderr "Its $itsWidth $itsHeight"
  51.  
  52.     # This is for putting the dialog off to the side
  53.     set leftRoom $xoff 
  54.     set rightRoom [expr {[screenwidth] - $xoff - $mainWidth}]
  55.  
  56.     set topRoom $yoff 
  57.     set bottomRoom [expr {[screenheight] - $yoff - $mainHeight}]
  58.  
  59.     case $where in {
  60.     # The "off" placement is still a bit lame
  61.     "off" {
  62. #        puts stderr "placePopUp " nonewline
  63.         if {$leftRoom > $rightRoom} {
  64.         set itsXoff [expr {$xoff - $itsWidth}]
  65.         if {$itsXoff < 0} {
  66.             set itsXoff 0
  67.         }
  68. #        puts stderr "left $itsXoff " nonewline
  69.         } else {
  70.         set itsXoff [expr {$xoff + $mainWidth}]
  71.         if {[expr {$itsXoff + $itsWidth}] > [screenwidth]} {
  72.             set itsXoff [expr {[screenwidth] - $itsWidth}]
  73.         }
  74. #        puts stderr "right $itsXoff " nonewline
  75.         }
  76.         if {$topRoom > $bottomRoom} {
  77.         set itsYoff [expr {$yoff - $itsHeight}]
  78.         if {$itsYoff < 0} {
  79.             set itsYoff 0
  80.         }
  81. #        puts stderr "top $itsYoff " nonewline
  82.         } else {
  83.         set itsYoff [expr {$yoff + $mainHeight}]
  84.         if {[expr {$itsYoff + $itsHeight}] > [screenheight]} {
  85.             set itsYoff [expr {[screenheight] - $itsHeight}]
  86.         }
  87. #        puts stderr "bottom $itsYoff " nonewline
  88.         }
  89.     }
  90.     { default center } {
  91.         if {[string compare $where center] == 0} {
  92.         set itsXoff [expr {$xoff + ($mainWidth - $itsWidth) / 2}]
  93.         set itsYoff [expr {$yoff + ($mainHeight - $itsHeight) / 2}]
  94.         }
  95.     }
  96.     }
  97.     wm geometry $widget +${itsXoff}+${itsYoff}
  98.     wm deiconify $widget
  99. }
  100.  
  101. --
  102.  
  103.     Brent Welch
  104.