package require msgcat

namespace eval themes_editor {
variable SCRIPT_DIR
variable THEMES_DIR
variable COLORS
variable SHOW_MENU
variable CURRENT_COLOR

	set SCRIPT_DIR [file dirname [info script]]
	
	msgcat::mcload [file join $SCRIPT_DIR msgs]
	
	hook::add finload_hook [namespace current]::handle 100
}

proc themes_editor::handle { } {
variable SCRIPT_DIR
variable THEMES_DIR
variable COLORS

	if { ! [namespace exist ::plugins::themes] } {
		return NO
	}
	
	set THEMES_DIR $::plugins::themes::script_dir
	
	set pm [.mainframe getmenu plugins]
	$pm add command -label [::msgcat::mc "Edit theme"]  \
		-command [list [namespace current]::edit_theme]
}

proc themes_editor::edit_theme { } {
	create_window
	insert_themes
}


proc themes_editor::create_window { } {
	set w ._theme_
	
	if { [winfo exist $w] } {
		destroy $w
	}
	
	toplevel $w
	
	wm title $w [msgcat::mc "Themes editor"]
	
	frame $w.frame -relief groove -bd 2 
	frame $w.buttonbar -relief groove -bd 2 
	pack $w.frame -fill both -expand 1 -pady 3 -padx 3
	pack $w.buttonbar -fill x
	
	text $w.frame.main -wrap word -cursor arrow -cursor arrow \
		-font {Tahoma 8} -yscrollcommand "$w.frame.scroll set" \
		-undo 1 -maxundo 0
		
	scrollbar $w.frame.scroll -command "$w.frame.main yview"
	pack $w.frame.scroll -side right -fill y
	pack $w.frame.main -fill both -expand 1
	
	set text $w.frame.main
	
	button $w.buttonbar.save -text [msgcat::mc "Save"] -relief groove \
		-command [list [namespace current]::save_theme]
		
	button $w.buttonbar.back -text [msgcat::mc "Back"] -relief groove \
		-command [list [namespace current]::redraw_edit_theme]
		
	button $w.buttonbar.cancel -text [msgcat::mc "Cancel"] -relief groove \
		-command [list destroy $w]
		
	pack $w.buttonbar.save $w.buttonbar.back $w.buttonbar.cancel \
		-padx 2 -pady 2 -side left -anchor center
		
	bind $w <3> [list [namespace current]::create_menu %W %X %Y]
}


proc themes_editor::insert_themes {} {
variable THEMES_DIR
variable THEMES
variable SHOW_MENU
	
	set text ._theme_.frame.main
	
	$text configure -state normal
	
	set theme_dir [file join $THEMES_DIR xrdb]
	foreach theme [glob -nocomplain -directory $theme_dir *xrdb] {
		set file [open $theme r]
		set THEMES($theme) [read $file]
		close $file
	}

	foreach theme [array names THEMES] {
		set name [file tail [file rootname $theme]]
		$text insert end $name\n $name
		$text tag bind $name <Any-Enter> [list $text tag configure $name -background #43ce80 -borderwidth 1]
		$text tag bind $name <Any-Leave> [list $text tag configure $name -background {} -borderwidth 0]
		$text tag bind $name <1> [list [namespace current]::edit_next_theme $theme]
	}
	
	._theme_.buttonbar.save configure -state disabled
	._theme_.buttonbar.back configure -state disabled
	$text configure -state disabled
	
	set SHOW_MENU 0
}


proc themes_editor::edit_next_theme { theme } {
variable THEMES
variable SHOW_MENU
	
	._theme_.buttonbar.save configure -state normal
	._theme_.buttonbar.back configure -state normal
	._theme_.frame.main configure -state normal
	
	._theme_.frame.main delete 0.0 end
	
	._theme_.frame.main insert end $THEMES($theme)
	
	set SHOW_MENU 1
}


proc themes_editor::create_menu { w x y } {
variable SHOW_MENU
	
	set m $w.menu
	
	if { $SHOW_MENU == 0 } {
		return
	}
	
	 if { [winfo exists $m] } {
		destroy $m
    }

    menu $m -tearoff 0
	
	$m add command -label [::msgcat::mc "Color set"] \
		-command  [list [namespace current]::new_color_code]
	
	set wpaths "balloon rulecondmenu ruleactmenu \
	h1popmenu h2popmenu tray mainframe menubar \
	presence roster nb pages tabsmenu avatar_menu \
	roster_filter sound_menu session_menu customize"
	
	$m add cascade -label [::msgcat::mc "Widget's path"] \
		-menu [menu $m.paths -tearoff 0]	
	
	foreach path $wpaths {
		$m.paths add command -label $path \
			-command  [list [namespace current]::insert $path]
	}
	
	set wclasses "Button Canvas Checkbutton Entry \
	Frame Label Labelframe Listbox Menu Menubutton \
	Message Panedwindow Radiobutton Scale Scrollbar \
	Spinbox Text"
	
	$m add cascade -label [::msgcat::mc "Widget's class"] \
		-menu [menu $m.classes -tearoff 0]	
	
	foreach cl $wclasses {
		$m.classes add command -label $cl \
			-command  [list [namespace current]::insert $cl]
	}
	
	set woptions "activeBackground activeBorderWidth \
	activeForeground anchor background bitmap \
	borderWidth cursor compound disabledForeground \
	font foreground highlightBackground highlightColor \
	highlightThickness image insertBackground \
	insertBorderWidth insertOffTime insertOnTime \
	insertWidth justify padX padY relief \
	selectBackground selectBorderWidth \
	selectForeground troughColor"
	
	$m add cascade -label [::msgcat::mc "Widget's options"] \
		-menu [menu $m.option -tearoff 0]	
	
	foreach opt $woptions {
		$m.option add command -label $opt \
			-command  [list [namespace current]::insert $opt]
	}
	
	tk_popup $m $x $y
}


proc themes_editor::save_theme { } {
variable THEMES_DIR
	
	set filename [tk_getSaveFile \
			-filetypes  {{"xrdb Files" { .xrdb .XRDB}}} \
			-initialdir [file join $THEMES_DIR themes]]
	
	if { $filename eq {} } { 
		focus ._theme_.frame.main
		return 
	}
	
	set data [._theme_.frame.main get 0.0 end]
   
	set fileid [open $filename w]

	puts $fileid $data

	close $fileid
	
	tk_messageBox -message [::msgcat::mc "Saved"]
	
}

proc themes_editor::redraw_edit_theme {} {
	clear_window
	insert_themes
}

proc themes_editor::clear_window {} {
	._theme_.frame.main configure -state normal
	._theme_.frame.main delete 0.0 end
	._theme_.frame.main configure -state disabled
}

proc themes_editor::new_color_code { } {
	set color [tk_chooseColor -parent ._theme_]
	if { $color ne {} } {
		catch {._theme_.frame.main delete sel.first sel.last}
		._theme_.frame.main insert insert $color
	}
}

proc themes_editor::insert { who } {
	if { $who ne {} } {
		catch {._theme_.frame.main delete sel.first sel.last}
		._theme_.frame.main insert insert $who
	}
}

proc themes_editor::undo {} {
	catch {._theme_.frame.main edit undo}
}

proc themes_editor::redo {} {
	catch {._theme_.frame.main edit redo}
}

proc themes_editor::clear {} {
	._theme_.frame.main delete 0.0 end
}