Drag and Drop a Window

MEd 2006-02-02: I needed this feature for a MetPad plugin but did not find anything matching in the existing wiki-pages - so I did it on my own :)

Purpose: The wm overrideredirect command allows to hide the window decoration, but when doing so it is not possible to move the window any more. This tiny script allows to move the window by just dragging-and-dropping it.

    ##########################################################
    # Name:  dndwin.tcl
    # Author: Martin Eder, [email protected]
    # Description: A toplevel window without window decoration
    #         that can be moved by drag-and-drop
    ##########################################################
    
    package require Tk
    
    variable xoff
    variable yoff
    
    proc move {x y} {
        set xpos [expr $x - $::xoff]
        set ypos [expr $y - $::yoff]
        wm geometry . "+$xpos+$ypos"
    }
    
    proc gui {} {
        ### Create some GUI    
        . configure -padx 5 -pady 5 -relief ridge -borderwidth 4
        label .llab -text "Use drag-and-drop\nto move this window"
        button .bexit -text "Exit" -command exit
        button .babout -text "About"  -command about
        pack .llab -padx 10 -pady 10
        pack .bexit .babout -padx 10 -pady 10 -side left -expand 1
    }
    
    proc init {} {
        ### Hide the Window Decoration
        wm overrideredirect . true
        
        ### DnD Binding
        bind . <ButtonPress-1> {
            set ::xoff [expr %X - [winfo rootx .]]
            set ::yoff [expr %Y - [winfo rooty .]]
        }
        bind . <B1-Motion> [list move %X %Y]    
    }
    
    proc about {} {
        wm withdraw .
        tk_messageBox -title "About" -message "DnD Toplevel\n2006 by Martin Eder\n([email protected])"
        wm deiconify .
    }
    
    init
    gui
    ### End of Script