#
# Copyright (c) 1993 Eric Schenk.
# All rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in
# all copies of this software.
# 
# IN NO EVENT SHALL ERIC SCHENK BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIC
# SCHENK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ERIC SCHENK SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND ERIC SCHENK HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

# The failure of anything in this file should never cause
# anything that uses these functions to fail. Therefore all calls to these
# functions should be protected be a catch.

# Maintainer and version information
set tkwm(maintainer) schenk@cs.toronto.edu
set tkwm(version) 0.1a9

# FRAMES

proc frame_bindings {frame} {
    tree-bind $frame <ButtonPress-2> "move $frame <Any-ButtonRelease-2>"
    tree-bind $frame <ButtonPress-3> "resize $frame <Any-ButtonRelease-3>"

    tkwm grabevent $frame <Meta-F7>
    bind $frame <Meta-F7> "move $frame <Any-ButtonRelease-1>"
    tkwm grabevent $frame <Meta-F8>
    bind $frame <Meta-F8> "resize $frame <Any-ButtonRelease-1>"
    tkwm grabevent $frame <Meta-F9>
    bind $frame <Meta-F9> "iconify $frame"
    tkwm grabevent $frame <Meta-F10>
    bind $frame <Meta-F10> "zoom $frame"
    tkwm grabevent $frame <Meta-F1>
    bind $frame <Meta-F1> "raise $frame"
    tkwm grabevent $frame <Meta-F3>
    bind $frame <Meta-F3> "lower $frame"
    tkwm grabevent $frame <Meta-F11>
    bind $frame <Meta-F11> "vzoom $frame"
    tkwm grabevent $frame <Meta-F12>
    bind $frame <Meta-F12> "hzoom $frame"

    tkwm grabevent $frame <L7>
    bind $frame <L7> "iconify $frame"
    tkwm grabevent $frame <L5>
    bind $frame <L5> "raise_lower $frame"
    tkwm grabevent $frame <Shift-L5>
    bind $frame <Shift-L5> "lower $frame"
    tkwm grabevent $frame <L9>
    bind $frame <L9> "wrefresh $frame"
    tkwm grabevent $frame <Shift-Escape>
    bind $frame <Shift-Escape> {
	if [lindex [%W.decoration config -titlebar] 4] {
	    %W.decoration invoke
	} else {
    	    global current_client
            set current_client [winfo toplevel %W]
	    set w $current_client.plug
            tkwm_postmenu .client_menu [winfo rootx $w] [winfo rooty $w]
        }
    }
}
# ROOMS

add_room Xterms

proc choose_room {plug} {
    if {[$plug info name] == "xterm"} {
	return Xterms
    }
    return global
}

# ICONS

proc icon_bindings {win iconwin} {
    # bind some keys to the icon
    catch {
	tkwm grabevent $iconwin.plug.w <Button-1>
    	tkwm grabevent $iconwin.plug.w <Button-2>
    }
    catch {
    	tkwm grabevent $iconwin.plug.w <L7>
    	tkwm grabevent $iconwin.plug.w <L5>
    }
    tree-bind $iconwin <ButtonPress-2> "move $iconwin <Any-ButtonRelease-2>"
    tree-bind $iconwin <ButtonRelease-1> "deiconify $win"
    tree-bind $iconwin <L7> "deiconify $win"
    tree-bind $iconwin <L5> "raise_lower $iconwin"
}

# define the menus
tkwm_menu .windows_menu {
    {title  "Windows Menu"}
    {separator}
    {command  "Tiny XTerm"  "exec xterm &"}
    {command  "XTerm"  "exec xterm -ut -geometry 80x40 &"}
}

tkwm_menu .root_menu {
    {title  "Root Menu"}
    {separator}
    {command  "Blank"  {exec xset s 1; exec sleep 2; exec xset s 600}}
    {command  "Refresh"  {refresh}}
    {separator}
    {command  "Restart TKWM"  {source $tkwm_resource}}
    {separator}
    {command  "Quit TKWM"  {exit}}
}

tkwm_menu .client_menu {
    {command "Move" {move $current_client <Any-ButtonRelease-1>} "    Alt+F7"}
    {command "Size" {resize $current_client <Any-ButtonRelease-1>} "    Alt+F8"}
    {command "Iconify" {iconify $current_client} "    Alt+F9"}
    {command "Zoom" {zoom $current_client} "   Alt+F10"}
    {command "Vert. Zoom" {vzoom $current_client} "   Alt+F11"}
    {command "Hor. Zoom" {hzoom $current_client} "   Alt+F12"}
    {command "Raise" {raise $current_client} "    Alt+F1"}
    {command "Lower" {lower $current_client} "    Alt+F3"}
    {command "Refresh" {wrefresh $current_client} "        L9"}
    {command "Preferences" {client_preferences $current_client}}
    {command "Destroy" {handleError {$current_client.plug delete} {destroy $current_client}}}
}

# bind the menus to keys over the root window

bind . <ButtonPress-1> {tkwm_postmenu .windows_menu %X %Y}
bind . <ButtonPress-2> {tkwm_postmenu .room_menu %X %Y}
bind . <ButtonPress-3> {tkwm_postmenu .root_menu %X %Y}

# set up geometry managers

# Set up a simple manager that just maps windows where they ask.
simpleManager tkwmDefaultSimpleManager

# set up an icon region in the lower right corner of size 200x600
# with a padding of 5 between windows, and filled starting from the
# south to the north, and then from the east to the west as each
# column is filled.

packingManager tkwmDefaultIconManager 200x600-0-0 5 5 south east
