home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Freeware 31 / FreelogHS31.iso / Texte / scribus / scribus-1.3.3.9-win32-install.exe / tcl / tix8.1 / ResizeH.tcl < prev    next >
Text File  |  2001-11-03  |  14KB  |  506 lines

  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. #    $Id: ResizeH.tcl,v 1.2.2.1 2001/11/03 07:20:49 idiscovery Exp $
  4. #
  5. # ResizeH.tcl --
  6. #
  7. #    tixResizeHandle: A general purpose "resizing handle"
  8. #    widget. You can use it to resize pictures, widgets, etc. When
  9. #    using it to resize a widget, you can use the "attachwidget"
  10. #    command to attach it to a widget and it will handle all the
  11. #    events for you.
  12. #
  13. # Copyright (c) 1993-1999 Ioi Kim Lam.
  14. # Copyright (c) 2000-2001 Tix Project Group.
  15. #
  16. # See the file "license.terms" for information on usage and redistribution
  17. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  18. #
  19. #
  20. #
  21.  
  22. tixWidgetClass tixResizeHandle {
  23.     -classname TixResizeHandle
  24.     -superclass tixVResize
  25.  
  26.     -method {
  27.     attachwidget detachwidget hide show
  28.     }
  29.     -flag {
  30.     -command -cursorfg -cursorbg -handlesize -hintcolor -hintwidth -x -y
  31.     }
  32.     -configspec {
  33.     {-command command Command ""}
  34.     {-cursorfg cursorFg CursorColor white}
  35.     {-cursorbg cursorBg CursorColor red}
  36.     {-handlesize handleSize HandleSize 6}
  37.     {-hintcolor hintColor HintColor red}
  38.     {-hintwidth hintWidth HintWidth 1}
  39.     {-x x X 0}
  40.     {-y y Y 0}
  41.     }
  42. }
  43.  
  44. proc tixResizeHandle:InitWidgetRec {w} {
  45.     upvar #0 $w data
  46.  
  47.     tixChainMethod $w InitWidgetRec
  48.  
  49.     set data(shown)  0
  50.     set data(widget) ""
  51. }
  52.  
  53. proc tixResizeHandle:ConstructWidget {w} {
  54.     upvar #0 $w data
  55.  
  56.     tixChainMethod $w ConstructWidget
  57.  
  58.     # Create the hints
  59.     #
  60.     set data(w_ht) $w:tix_priv_ht
  61.     set data(w_hb) $w:tix_priv_hb
  62.     set data(w_hl) $w:tix_priv_hl
  63.     set data(w_hr) $w:tix_priv_hr
  64.  
  65.     frame $data(w_ht) -height $data(-hintwidth) -bg $data(-background)
  66.     frame $data(w_hb) -height $data(-hintwidth) -bg $data(-background)
  67.     frame $data(w_hl) -width  $data(-hintwidth) -bg $data(-background)
  68.     frame $data(w_hr) -width  $data(-hintwidth) -bg $data(-background)
  69.  
  70.     # Create the corner resize handles
  71.     #
  72.     set data(w_r00) $w
  73.  
  74. #   Windows don't like this
  75. #    $data(rootCmd) config\
  76. #    -cursor "top_left_corner $data(-cursorbg) $data(-cursorfg)"
  77.  
  78.     $data(rootCmd) config -cursor top_left_corner
  79.  
  80.     set data(w_r01) $w:tix_priv_01
  81.     set data(w_r10) $w:tix_priv_10
  82.     set data(w_r11) $w:tix_priv_11
  83.  
  84.     frame $data(w_r01) -relief $data(-relief) -bd $data(-borderwidth) \
  85.     -cursor "bottom_left_corner"\
  86.     -bg $data(-background)
  87.     frame $data(w_r10) -relief $data(-relief) -bd $data(-borderwidth) \
  88.     -cursor "top_right_corner"\
  89.     -bg $data(-background)
  90.     frame $data(w_r11) -relief $data(-relief) -bd $data(-borderwidth) \
  91.     -cursor "bottom_right_corner"\
  92.     -bg $data(-background)
  93.  
  94.     # Create the border resize handles
  95.     #
  96.     set data(w_bt)  $w:tix_priv_bt
  97.     set data(w_bb)  $w:tix_priv_bb
  98.     set data(w_bl)  $w:tix_priv_bl
  99.     set data(w_br)  $w:tix_priv_br
  100.  
  101.     frame $data(w_bt) -relief $data(-relief) -bd $data(-borderwidth) \
  102.     -cursor "top_side"\
  103.     -bg $data(-background)
  104.     frame $data(w_bb) -relief $data(-relief) -bd $data(-borderwidth) \
  105.     -cursor "bottom_side"\
  106.     -bg $data(-background)
  107.     frame $data(w_bl) -relief $data(-relief) -bd $data(-borderwidth) \
  108.     -cursor "left_side"\
  109.     -bg $data(-background)
  110.     frame $data(w_br) -relief $data(-relief) -bd $data(-borderwidth) \
  111.     -cursor "right_side"\
  112.     -bg $data(-background)
  113. }
  114.  
  115. proc tixResizeHandle:SetBindings {w} {
  116.     upvar #0 $w data
  117.  
  118.     tixChainMethod $w SetBindings
  119.  
  120.     bind $data(w_r00)   <1> \
  121.     "tixResizeHandle:dragstart $w $data(w_r00)   1 %X %Y  {1  1 -1 -1}"
  122.     bind $data(w_r01)   <1> \
  123.     "tixResizeHandle:dragstart $w $data(w_r01)   1 %X %Y  {1  0 -1  1}"
  124.     bind $data(w_r10)   <1> \
  125.     "tixResizeHandle:dragstart $w $data(w_r10)   1 %X %Y  {0  1  1 -1}"
  126.     bind $data(w_r11)   <1> \
  127.     "tixResizeHandle:dragstart $w $data(w_r11)   1 %X %Y  {0  0  1  1}"
  128.     bind $data(w_bt)    <1> \
  129.     "tixResizeHandle:dragstart $w $data(w_bt)    1 %X %Y  {0  1  0 -1}"
  130.     bind $data(w_bb)    <1> \
  131.     "tixResizeHandle:dragstart $w $data(w_bb)    1 %X %Y  {0  0  0  1}"
  132.     bind $data(w_bl)    <1> \
  133.     "tixResizeHandle:dragstart $w $data(w_bl)    1 %X %Y  {1  0 -1  0}"
  134.     bind $data(w_br)    <1> \
  135.     "tixResizeHandle:dragstart $w $data(w_br)    1 %X %Y  {0  0  1  0}"
  136.  
  137.     foreach win [list \
  138.          $data(w_r00)\
  139.          $data(w_r01)\
  140.          $data(w_r10)\
  141.          $data(w_r11)\
  142.          $data(w_bt)\
  143.          $data(w_bb)\
  144.          $data(w_bl)\
  145.          $data(w_br)\
  146.          ] {
  147.     bind $win <B1-Motion>       "tixVResize:drag    $w %X %Y"
  148.     bind $win <ButtonRelease-1> "tixVResize:dragend $w $win 0 %X %Y"
  149.     bind $win <Any-Escape>      "tixVResize:dragend $w $win 1  0  0"
  150.     }
  151. }
  152.  
  153. #----------------------------------------------------------------------
  154. #         Config Methods
  155. #----------------------------------------------------------------------
  156. proc tixResizeHandle:config-width {w value} {
  157.     tixWidgetDoWhenIdle tixResizeHandle:ComposeWindow $w
  158. }
  159.  
  160. proc tixResizeHandle:config-height {w value} {
  161.     tixWidgetDoWhenIdle tixResizeHandle:ComposeWindow $w
  162. }
  163.  
  164. proc tixResizeHandle:config-x {w value} {
  165.     tixWidgetDoWhenIdle tixResizeHandle:ComposeWindow $w
  166. }
  167.  
  168. proc tixResizeHandle:config-y {w value} {
  169.     tixWidgetDoWhenIdle tixResizeHandle:ComposeWindow $w
  170. }
  171.  
  172.  
  173. #----------------------------------------------------------------------
  174. #         Public Methods
  175. #----------------------------------------------------------------------
  176. proc tixResizeHandle:dragstart {w win depress rootx rooty mrect} {
  177.     upvar #0 $w data
  178.  
  179.     set wx $data(-x)
  180.     set wy $data(-y)
  181.     set ww $data(-width)
  182.     set wh $data(-height)
  183.  
  184.     tixVResize:dragstart $w $win $depress $rootx $rooty \
  185.     [list $wx $wy $ww $wh] $mrect
  186. }
  187.  
  188. # tixDeleteBindTag --
  189. #
  190. #    Delete the bindtag(s) in the args list from the bindtags of the widget
  191. #
  192. proc tixDeleteBindTag {w args} {
  193.     if {![winfo exists $w]} {
  194.     return
  195.     }
  196.     set newtags ""
  197.  
  198.     foreach tag [bindtags $w] {
  199.     if {[lsearch $args $tag] == -1} {
  200.         lappend newtags $tag
  201.     }
  202.     }
  203.     bindtags $w $newtags
  204. }
  205.  
  206. proc tixAddBindTag {w args} {
  207.     bindtags $w [concat [bindtags $w] $args]
  208. }
  209.  
  210. proc tixResizeHandle:attachwidget {w widget args} {
  211.     upvar #0 $w data
  212.  
  213.     set opt(-move) 0
  214.     tixHandleOptions opt {-move} $args
  215.  
  216.     if {$data(widget) != ""} {
  217.     tixDeleteBindTag $data(widget) TixResizeHandleTag:$w
  218.     }
  219.  
  220.     set data(widget) $widget
  221.  
  222.     if {$data(widget) != ""} {
  223.     # Just in case TixResizeHandleTag was already there
  224.     tixDeleteBindTag $data(widget) TixResizeHandleTag:$w
  225.     tixAddBindTag $data(widget) TixResizeHandleTag:$w
  226.  
  227.     
  228.     set data(-x)      [winfo x      $data(widget)]
  229.     set data(-y)      [winfo y      $data(widget)]
  230.     set data(-width)  [winfo width  $data(widget)]
  231.     set data(-height) [winfo height $data(widget)]
  232.  
  233.     tixResizeHandle:show $w
  234.     tixResizeHandle:ComposeWindow $w
  235.  
  236.     # Now set the bindings
  237.     #
  238.     if {$opt(-move)} {
  239.         bind TixResizeHandleTag:$w <1> \
  240.         "tixResizeHandle:Attach $w %X %Y"
  241.         bind TixResizeHandleTag:$w <B1-Motion> \
  242.         "tixResizeHandle:BMotion $w %X %Y"
  243.         bind TixResizeHandleTag:$w <Any-Escape> \
  244.         "tixResizeHandle:BRelease $w 1 %X %Y"
  245.         bind TixResizeHandleTag:$w <ButtonRelease-1>\
  246.         "tixResizeHandle:BRelease $w 0 %X %Y"
  247.     } else {
  248.         # if "move" is false, then the widget won't be moved as a whole -- 
  249.         # ResizeHandle will only move its sides
  250.         bind TixResizeHandleTag:$w <1>          {;}
  251.         bind TixResizeHandleTag:$w <B1-Motion>     {;}
  252.         bind TixResizeHandleTag:$w <Any-Escape>     {;}
  253.         bind TixResizeHandleTag:$w <ButtonRelease-1> {;}
  254.     }
  255.     }
  256. }
  257.  
  258. proc tixResizeHandle:detachwidget {w} {
  259.     upvar #0 $w data
  260.  
  261.     if {$data(widget) != ""} {
  262.     tixDeleteBindTag $data(widget) TixResizeHandleTag:$w
  263.     }
  264.     tixResizeHandle:hide $w
  265. }
  266.  
  267. proc tixResizeHandle:show {w} {
  268.     upvar #0 $w data
  269.  
  270.     set data(shown) 1
  271.  
  272.     raise $data(w_ht)
  273.     raise $data(w_hb)
  274.     raise $data(w_hl)
  275.     raise $data(w_hr)
  276.  
  277.     raise $data(w_r00)
  278.     raise $data(w_r01)
  279.     raise $data(w_r10)
  280.     raise $data(w_r11)
  281.  
  282.     raise $data(w_bt)
  283.     raise $data(w_bb)
  284.     raise $data(w_bl)
  285.     raise $data(w_br)
  286.  
  287. #   tixCancleIdleTask tixResizeHandle:ComposeWindow $w
  288.     tixResizeHandle:ComposeWindow $w
  289. }
  290.  
  291.  
  292. proc tixResizeHandle:hide {w} {
  293.     upvar #0 $w data
  294.  
  295.     if {!$data(shown)} {
  296.     return
  297.     }
  298.  
  299.     set data(shown) 0
  300.  
  301.     place forget $data(w_r00)
  302.     place forget $data(w_r01)
  303.     place forget $data(w_r10)
  304.     place forget $data(w_r11)
  305.  
  306.     place forget $data(w_bt)
  307.     place forget $data(w_bb)
  308.     place forget $data(w_bl)
  309.     place forget $data(w_br)
  310.  
  311.     place forget $data(w_ht)
  312.     place forget $data(w_hb)
  313.     place forget $data(w_hl)
  314.     place forget $data(w_hr)
  315. }
  316.  
  317. proc tixResizeHandle:Destructor {w} {
  318.     upvar #0 $w data
  319.  
  320.     if {$data(widget) != ""} {
  321.     tixDeleteBindTag $data(widget) TixResizeHandleTag:$w
  322.     }
  323.  
  324.     catch {destroy $data(w_r01)}
  325.     catch {destroy $data(w_r10)}
  326.     catch {destroy $data(w_r11)}
  327.  
  328.     catch {destroy $data(w_bt)}
  329.     catch {destroy $data(w_bb)}
  330.     catch {destroy $data(w_bl)}
  331.     catch {destroy $data(w_br)}
  332.  
  333.     catch {destroy $data(w_ht)}
  334.     catch {destroy $data(w_hb)}
  335.     catch {destroy $data(w_hl)}
  336.     catch {destroy $data(w_hr)}
  337.  
  338.     tixChainMethod $w Destructor
  339. }
  340.  
  341. #----------------------------------------------------------------------
  342. #       Private Methods Dealing With Attached Widgets
  343. #----------------------------------------------------------------------
  344. proc tixResizeHandle:Attach {w rx ry} {
  345.     upvar #0 $w data
  346.  
  347.     tixResizeHandle:dragstart $w $data(widget) 0 $rx $ry {1 1 0 0}
  348. }
  349.  
  350. proc tixResizeHandle:BMotion {w rx ry} {
  351.     tixVResize:drag $w $rx $ry
  352. }
  353.  
  354.  
  355. proc tixResizeHandle:BRelease {w isAbort rx ry} {
  356.     upvar #0 $w data
  357.  
  358.     tixVResize:dragend $w $data(widget) $isAbort $rx $ry
  359. }
  360.  
  361. #----------------------------------------------------------------------
  362. #         Private Methods
  363. #----------------------------------------------------------------------
  364. proc tixResizeHandle:DrawTmpLines {w} {
  365.     upvar #0 $w data
  366.  
  367.     # I've seen this error - mike
  368.     if {![info exists data(hf:x1)]} {return}
  369.     set x1 $data(hf:x1)
  370.     if {![info exists data(hf:y1)]} {return}
  371.     set y1 $data(hf:y1)
  372.     if {![info exists data(hf:x2)]} {return}
  373.     set x2 $data(hf:x2)
  374.     if {![info exists data(hf:y2)]} {return}
  375.     set y2 $data(hf:y2)
  376.  
  377.     tixTmpLine $x1 $y1 $x2 $y1 $w
  378.     tixTmpLine $x1 $y2 $x2 $y2 $w
  379.     tixTmpLine $x1 $y1 $x1 $y2 $w
  380.     tixTmpLine $x2 $y1 $x2 $y2 $w
  381. }
  382.  
  383. # Place the hint frame to indicate the changes
  384. #
  385. proc tixResizeHandle:SetHintFrame {w x1 y1 width height} {
  386.     upvar #0 $w data
  387.  
  388.     # The four sides of the window
  389.     #
  390.     set x2 [expr "$x1+$width"]
  391.     set y2 [expr "$y1+$height"]
  392.  
  393.     set rx [winfo rootx [winfo parent $w]]
  394.     set ry [winfo rooty [winfo parent $w]]
  395.  
  396.     incr x1 $rx
  397.     incr y1 $ry
  398.     incr x2 $rx
  399.     incr y2 $ry
  400.  
  401.     if {[info exists data(hf:x1)]} {
  402.     tixResizeHandle:DrawTmpLines $w
  403.     }
  404.  
  405.     set data(hf:x1) $x1
  406.     set data(hf:y1) $y1
  407.     set data(hf:x2) $x2
  408.     set data(hf:y2) $y2
  409.  
  410.     tixResizeHandle:DrawTmpLines $w
  411. }
  412.  
  413. proc tixResizeHandle:ShowHintFrame {w} {
  414.     upvar #0 $w data
  415.  
  416.     place forget $data(w_ht)
  417.     place forget $data(w_hb)
  418.     place forget $data(w_hl)
  419.     place forget $data(w_hr)
  420.  
  421.     update
  422. }
  423.  
  424. proc tixResizeHandle:HideHintFrame {w} {
  425.     upvar #0 $w data
  426.  
  427.     tixResizeHandle:DrawTmpLines $w
  428.     unset data(hf:x1)
  429.     unset data(hf:y1)
  430.     unset data(hf:x2)
  431.     unset data(hf:y2)
  432. }
  433.  
  434. proc tixResizeHandle:UpdateSize {w x y width height} {
  435.     upvar #0 $w data
  436.  
  437.     set data(-x)      $x
  438.     set data(-y)      $y
  439.     set data(-width)  $width
  440.     set data(-height) $height
  441.  
  442.     tixResizeHandle:ComposeWindow $w
  443.  
  444.     if {$data(widget) != ""} {
  445.     place $data(widget) -x $x -y $y -width $width -height $height
  446.     }
  447.  
  448.     if {$data(-command) != ""} {
  449.     eval $data(-command) $x $y $width $height
  450.     }
  451. }
  452.  
  453. proc tixResizeHandle:ComposeWindow {w} {
  454.     upvar #0 $w data
  455.  
  456.     set px $data(-x)
  457.     set py $data(-y)
  458.     set pw $data(-width)
  459.     set ph $data(-height)
  460.  
  461.     # Show the hint frames
  462.     #
  463.     set x1 $px
  464.     set y1 $py
  465.     set x2 [expr "$px+$pw"]
  466.     set y2 [expr "$py+$ph"]
  467.  
  468.     place $data(w_ht) -x $x1 -y $y1 -width  $pw -bordermode outside
  469.     place $data(w_hb) -x $x1 -y $y2 -width  $pw -bordermode outside
  470.     place $data(w_hl) -x $x1 -y $y1 -height $ph -bordermode outside
  471.     place $data(w_hr) -x $x2 -y $y1 -height $ph -bordermode outside
  472.  
  473.     # Set the four corner resize handles
  474.     #
  475.     set sz_2 [expr $data(-handlesize)/2]
  476.  
  477.     set x1 [expr "$px - $sz_2"]
  478.     set y1 [expr "$py - $sz_2"]
  479.     set x2 [expr "$px - $sz_2" + $pw]
  480.     set y2 [expr "$py - $sz_2" + $ph]
  481.  
  482.     place $data(w_r00) -x $x1 -y $y1 \
  483.     -width $data(-handlesize) -height $data(-handlesize)
  484.     place $data(w_r01) -x $x1 -y $y2\
  485.     -width $data(-handlesize) -height $data(-handlesize)
  486.     place $data(w_r10) -x $x2 -y $y1\
  487.     -width $data(-handlesize) -height $data(-handlesize)
  488.     place $data(w_r11) -x $x2 -y $y2\
  489.     -width $data(-handlesize) -height $data(-handlesize)
  490.  
  491.  
  492.     # Set the four border resize handles
  493.     #
  494.     set mx [expr "$px + $pw/2 - $sz_2"]
  495.     set my [expr "$py + $ph/2 - $sz_2"]
  496.  
  497.     place $data(w_bt) -x $mx -y $y1 \
  498.     -width $data(-handlesize) -height $data(-handlesize)
  499.     place $data(w_bb) -x $mx -y $y2 \
  500.     -width $data(-handlesize) -height $data(-handlesize)
  501.     place $data(w_bl) -x $x1 -y $my \
  502.     -width $data(-handlesize) -height $data(-handlesize)
  503.     place $data(w_br) -x $x2 -y $my \
  504.     -width $data(-handlesize) -height $data(-handlesize)
  505. }
  506.