Posted to tcl by Meliorator at Wed Mar 28 17:12:26 GMT 2012view raw
- package require Tcl 8.5
- package require Tk 8.5
- package require tablelist
- proc ContextMenu {w_table w_menu menu_x menu_y} {
- $w_table finishediting
- set w_window [lindex [wm stackorder .] end]
- set mouse_x [expr {[winfo pointerx .] - [winfo rootx $w_window]}]
- set mouse_y [expr {[winfo pointery .] - [winfo rooty $w_window]}]
- set cell_x [$w_table nearestcolumn $mouse_x]
- set cell_y [$w_table nearest $mouse_y]
- $w_table selection clear 0 end
- $w_table selection set $cell_y $cell_y
- $w_menu delete 1
- $w_menu delete 0
- set browse_command [list [namespace current]::BrowseLogoPath $w_table $cell_y]
- set edit_command [list [namespace current]::EditLogoPath $w_table $cell_y]
- $w_menu add command -label Browse -command $browse_command
- $w_menu add command -label Edit -command $edit_command
- if {$cell_x == 1} {
- tk_popup $w_menu $menu_x $menu_y
- }
- }
- proc UpdateLogoPath {w_table cell_y logo_path} {
- variable logo_database
- $logo_database(command) eval "UPDATE $logo_database(table) SET $logo_database(logo_path) = '$logo_path' WHERE $logo_database(station_number) = '[lindex $logo_database(station_numbers) $cell_y]'"
- if {[file exists $logo_path]} {
- $w_table cellconfigure $cell_y,1 -image [GetLogo $logo_path]
- }
- }
- proc BrowseLogoPath {w_table cell_y} {
- set file_types {
- { {PNG Files} {.png} }
- { {All Files} {*} }
- }
- set logo_path [tk_getOpenFile -filetypes $file_types]
- if {[string length $logo_path]} {
- $w_table cellconfigure $cell_y,1 -text $logo_path
- UpdateLogoPath $w_table $cell_y $logo_path
- }
- }
- proc EditedLogoPath {w_table cell_y cell_x logo_path} {
- $w_table cellconfigure $cell_y,1 -editable false
- UpdateLogoPath $w_table $cell_y $logo_path
- return $logo_path
- }
- proc EditLogoPath {w_table cell_y} {
- $w_table cellconfigure $cell_y,1 -editable true
- $w_table editcell $cell_y,1
- $w_table selection set $cell_y $cell_y
- }
- proc UserInterface {w_root} {
- set w_base [expr {($w_root == ".") ? "" : $w_root}]
- variable ROOT $w_root
- variable BASE $w_base
- variable SCRIPTDIR
- variable logo_database
- variable load_logos_progress 0
- set w_frame ${w_base}.frm
- set w_table ${w_frame}.tbl
- set w_vertical_scroll_bar ${w_frame}.vsb
- set w_horizontal_scroll_bar ${w_frame}.hsb
- set w_menu ${w_base}.mnu
- set w_progress_bar_label ${w_base}.pbl
- set w_progress_bar ${w_base}.pb
- frame $w_frame
- tablelist::tablelist $w_table \
- -columns {0 "Station Name" left
- 0 "Logo" left} \
- -yscrollcommand [list $w_vertical_scroll_bar set] \
- -width 1 \
- -stretch 2 \
- -stripebackground grey97 \
- -selectmode single \
- -editendcommand [namespace current]::EditedLogoPath
- scrollbar $w_vertical_scroll_bar -orient vertical -command [list $w_table yview]
- scrollbar $w_horizontal_scroll_bar -orient horizontal -command [list $w_table xview]
- set stations [$logo_database(command) eval "SELECT $logo_database(station_name), $logo_database(logo_path), $logo_database(station_number) FROM $logo_database(table)"]
- set w_progress_bar_label [label $w_progress_bar_label -text "Loading logos..." -background white]
- set w_progress_bar [ttk::progressbar $w_progress_bar -orient horizontal -variable [namespace current]::load_logos_progress -maximum [expr { [llength $stations] / 3 }]]
- pack $w_progress_bar_label -fill x
- pack $w_progress_bar -fill x
- foreach {station_name logo_path station_number} $stations {
- lappend logo_database(station_numbers) $station_number
- $w_table insert end [list $station_name $logo_path]
- $w_table rowconfigure end -selectbackground grey
- $w_table cellconfigure end,1 -image [GetLogo $logo_path]
- incr load_logos_progress
- update
- }
- unset stations
- pack forget $w_progress_bar_label
- pack forget $w_progress_bar
- menu $w_menu -tearoff 0
- bind $w_root "<Button-3>" [list [namespace current]::ContextMenu $w_table $w_menu %X %Y]
- bind $w_root "<Button-1>" [list $w_table finishediting]
- pack $w_frame -fill both -expand yes
- pack $w_horizontal_scroll_bar -side bottom -fill x -expand no
- pack $w_table -side left -fill both -expand yes
- pack $w_vertical_scroll_bar -side right -fill y -expand no
- }