#! ./vwwish -f

######################################################################
## Procedure: save_spreadsheet {}
##   Purpose: Damnit think
######################################################################

proc save_spreadsheet {} {
    catch {
        set outFile [ open sample w+ ] ;
        puts $outFile [ .matrix.matrix.0.matrix get 0 0 end end ] ;
        close $outFile ;
    }
}

######################################################################
## Procedure: load_spreadsheet {}
##   Purpose: Load a damn spreadsheet
######################################################################

proc load_spreadsheet {} {
    catch {
        set inFile [ open sample r ] ;
        gets $inFile buffer ;
        .matrix.matrix set 0 0 end end $buffer ;
        close $inFile ;
    }
}

######################################################################
## Procedure: change_anchor { wHandle }
##   Purpose: Changes the anchor on TEXT types only, bitmaps
##            are not affected as they need different information
######################################################################

proc change_anchor { wHandle } {
    global _Matrix_ ;

    # No need to parse unknown data

    if ![ info exists _Matrix_(SELECT_X,$wHandle) ] {
	return ;
    }

    set col $_Matrix_(SELECT_X,$wHandle) ;
    set row $_Matrix_(SELECT_Y,$wHandle) ;

    set info   [lindex [$wHandle.0.matrix get $col $row] 0];
    set type   [lindex $info 0] ;
    switch $type {
    "TEXT" -
    "BITMAP" {
                set anchor $_Matrix_(SELECT_ANCHOR,$wHandle) ;
                set data   [lindex $info 2];
                $wHandle set $col $row [list $type $anchor $data] ;
             }
    }
}

######################################################################
## Procedure: local_menu { wHandle }
##   Purpose: Creates the menu used for this application
######################################################################

proc local_menu { wHandle } {
    frame .menu -relief raised -borderwidth 1 ;

    menubutton .menu.files -text "Files" -menu .menu.files.m -underline 0 ;
    menu .menu.files.m ;
    .menu.files.m add command -label "Load..." -command "local_load" ;
    .menu.files.m add command -label "Save..." -command "local_save" ;
    .menu.files.m add command -label "Quit" -command "destroy ." ;
    pack .menu.files -side left ;
}

option add *matrix.background "#9999c6"
option add *matrix.foreground white
option add *matrix.relief raised
option add *matrix.borderWidth 0
option add *matrix.cellrelief raised
option add *matrix.cellborderwidth 1
option add *regionForeground "white"
option add *regionBackground "grey80"
option add *matrix.m.font "-Adobe-Helvetica-Medium-R-Normal--*-120-*"

option add *matrix.vscroll.foreground grey67
option add *matrix.vscroll.borderWidth 1
option add *matrix.hscroll.foreground grey67
option add *matrix.hscroll.borderWidth 1

wm minsize . 0 0 ;
wm geometry . 700x200+0+0 
wm title . "Matrix v0.6" ;

set auto_path [linsert $auto_path 0 .] ;

local_menu .menu ;
vwt_matrix .matrix -cols 100 -rows 30 ;
.matrix bind matrix ;
#.matrix size 2 2 @current 5 ;

pack .menu   -side top -fill x ;
pack .matrix -side top -fill both -expand yes ;

set matrix(2,1) {TEXT w North}
set matrix(2,2) {TEXT w This is the longest line in the group}
set matrix(2,3) {TEXT w South}