#!/usr/local/bin/wish8.0jp
kanji defaultInputCode EUC

#########################################
## ƥɥ.ˤδ
#########################################
wm title . "Tk_Address"

# եؤΥѥ
set icon_path "/usr/local/lib/icons"
set ADS_LIST "~/ads_list"
set SEARCH_file  "/tmp/ads_list.search"

# Ͽ뤫Υå(->base_ads)
exec base_ads filecheck

#########################################
## Ͽ
#########################################
image create photo ads_all -file $icon_path/group.gif
image create photo ads_person -file $icon_path/personal.gif
image create photo ads_find -file $icon_path/personwho.gif
image create photo ads_new -file $icon_path/personnew.gif
image create photo ads_close -file $icon_path/close.gif

#########################################
## ƥɥ.ѤΥ٥κ
#########################################
label .l0 -text "Show All" -font { helvetica 9 }
label .l1 -text "Select" -font { helvetica 9 }
label .l2 -text "Add List" -font { helvetica 9 }
label .l3 -text "Quit" -font { helvetica 9 }

#########################################
## ƥɥ.ѤΥܥκ
#########################################
#########################################
## 1.Show Allܥ
#########################################
button .b0 -image ads_all -width 38 -height 38 -command {

	## ҥɥ.w0ˤδ
	toplevel .w0
	wm title .w0 "Tk_Adress Show All"
	.w0 configure -width 630 -height 270

	## ҥɥ.w0ˤΥ٥κ
	label .w0.l0 -fg White -bg Black -textvariable Date_field -anchor w

	## ϿѤΥեɥ٥ɤ߹(->base_ads)
	set Date_field [ exec base_ads data_field ]

	## ҥɥ.w0ˤΥܥκ
	button .w0.b0 -text "Ĥ" -command "destroy .w0"

	## ҥɥ.w0ˤΥƥȤκ
	text .w0.t0 -xscrollcommand ".w0.s0 set" -yscrollcommand ".w0.s1 set"
	scrollbar .w0.s0 -orient horizontal -command ".w0.t0 xview"
	scrollbar .w0.s1 -orient vertical -command ".w0.t0 yview"

	## awkꥹȤ(->base_ads)
	.w0.t0 insert end [ exec base_ads alllist ]

	## ҥɥ.w0ˤʤ
	place .w0.l0 -x 0 -y 0 -width 630 -height 20
	place .w0.t0 -x 0 -y 20 -width 610 -height 200
	place .w0.s0 -x 0 -y 220 -width 610
	place .w0.s1 -x 610 -y 20 -height 200
	place .w0.b0 -relx 0.5 -rely 1 -anchor s
}


#########################################
## 2.Selectܥ
#########################################
button .b1 -image ads_find -width 38 -height 38 -command {

	## ҥɥ.w1ˤδ
	toplevel .w1
	wm title .w1 "Tk_Adress Select"
	.w1 configure -width 300 -height 210

	## ҥɥ.w1ˤΥ٥κ
	label .w1.l0 -image ads_find
	label .w1.l1 -text "ܤǲ"
	label .w1.l2 -text ""
	label .w1.l3 -text ""
	label .w1.line0 -relief sunken
	label .w1.line1 -relief sunken
	label .w1.line2 -relief sunken

	## ҥɥ.w1ˤΥȥ꡼κ
	entry .w1.e0 -textvariable Search_key

	## ҥɥ.w1ˤΥ饸ܥκ
	radiobutton .w1.rb0 -text "" -value key_alpha -variable Search_mode -anchor w
	radiobutton .w1.rb1 -text "" -value key_1name -variable Search_mode -anchor w
	radiobutton .w1.rb2 -text "̾" -value key_2name -variable Search_mode -anchor w
	radiobutton .w1.rb3 -text "" -value key_ads -variable Search_mode -anchor w
	radiobutton .w1.rb4 -text "" -value key_tel -variable Search_mode -anchor w
	radiobutton .w1.rb5 -text "" -value key_sex1 -variable Search_mode -anchor w -command {
		set Search_key ""
	}
	radiobutton .w1.rb6 -text "" -value key_sex2 -variable Search_mode -anchor w -command {
		set Search_key ""
	}
 

	## ҥɥ.w1ˤΥܥκ
	button .w1.b0 -text "" -command {

		### ɤϤ줿γǧ
		if { $Search_key == "" } then {
			tk_messageBox -type ok -icon warning -default ok -message "ɤϤƲ"
		} else {
			#### (->base_ads)
			set Total_value [ exec base_ads $Search_mode  $Search_key ]

			#### η̤ɤξʬ
			if { $Total_value == 0 } then {
				tk_messageBox -type ok -icon warning -default ok -message "ԤϤޤ"
			} else {
				##### ¹ɥ.ww0ˤδ
				toplevel .ww0
				wm title .ww0 "Tk_Adress Search Result"
				.ww0 configure -width 250 -height 170
				set Now_value 1

				##### ¹ɥ.ww0ˤΥ٥κ
				label .ww0.l0 -image ads_person
				label .ww0.l1 -text ""
				label .ww0.l2 -text ""
				label .ww0.l3 -text ""
				label .ww0.l4 -text ""
				label .ww0.line0 -relief sunken
				label .ww0.line1 -relief sunken

				##### ¹ɥ.ww0ˤΥȥ꡼κ
				entry .ww0.e0 -textvariable Search_1name
				entry .ww0.e1 -textvariable Search_2name
				entry .ww0.e2 -textvariable Search_ads
				entry .ww0.e3 -textvariable Search_tel
				entry .ww0.e4 -textvariable Search_alpha

				##### ¹ɥ.ww0ˤΥ饸ܥκ
				radiobutton .ww0.rb0 -text "" -value "" -variable Search_sex -anchor w
				radiobutton .ww0.rb1 -text "" -value "" -variable Search_sex -anchor w

				##### ¹ɥ.ww0ˤʤ(->base_ads)
				set Search_alpha [ exec base_ads get_pram 1 $Now_value ]
				set Search_1name [ exec base_ads get_pram 2 $Now_value ]
				set Search_2name [ exec base_ads get_pram 3 $Now_value ]
				set Search_ads [ exec base_ads get_pram 4 $Now_value ]
				set Search_tel [ exec base_ads get_pram 5 $Now_value ]
				set Search_sex [ exec base_ads get_pram 6 $Now_value ]

				##### ¹ɥ.ww0ˤΥܥκ
				button .ww0.b0 -text "" -command {
					###### Υǡ뤫ʬƤޤ
					if { $Now_value == 1 } then {
						tk_messageBox -type ok -default ok -icon warning -message "ΥǡϤޤ"
					} else {
						####### ̤餽줾ιܤФޤ(->base_ads)
						set Now_value [ expr $Now_value - 1 ]
						set Search_alpha [ exec base_ads get_pram 1 $Now_value ]
						set Search_1name [ exec base_ads get_pram 2 $Now_value ]
						set Search_2name [ exec base_ads get_pram 3 $Now_value ]
						set Search_ads [ exec base_ads get_pram 4 $Now_value ]
						set Search_tel [ exec base_ads get_pram 5 $Now_value ]
						set Search_sex [ exec base_ads get_pram 6 $Now_value ]
					}
				}

				button .ww0.b1 -text "" -command {
					###### Υǡ뤫ʬƤޤ
					if { $Now_value == $Total_value } then {
						tk_messageBox -type ok -default ok -icon warning -message "ΥǡϤޤ"
					} else {
						####### ̤餽줾ιܤФޤ(->base_ads)
						set Now_value [ expr $Now_value + 1 ]
						set Search_alpha [ exec base_ads get_pram 1 $Now_value ]
						set Search_1name [ exec base_ads get_pram 2 $Now_value ]
						set Search_2name [ exec base_ads get_pram 3 $Now_value ]
						set Search_ads [ exec base_ads get_pram 4 $Now_value ]
						set Search_tel [ exec base_ads get_pram 5 $Now_value ]
						set Search_sex [ exec base_ads get_pram 6 $Now_value ]
					}
				}

				button .ww0.b2 -text "" -command {
					###### õǡޤ(->base_ads)
					exec base_ads del_list $Search_alpha $Search_1name $Search_2name $Search_ads $Search_tel $Search_sex $Now_value
					###### õǧΥɽޤ
					tk_messageBox -type ok -default ok -message "ޤ"
					###### äˤ⸽ɽƤܤäޤ
					set Search_1name ""
					set Search_2name ""
					set Search_ads ""
					set Search_tel ""
					set Search_alpha ""
					set Search_sex ""
				}

				button .ww0.b3 -text "ѹ" -command {
					###### ѹǡޤ(->base_ads)
					exec base_ads chg_list $Search_alpha $Search_1name $Search_2name $Search_ads $Search_tel $Search_sex $Now_value
					###### ѹǧΥɽޤ
					tk_messageBox -type ok -default ok -message "ѹޤ"
				}

				button .ww0.b4 -text "Ĥ" -command {
					destroy .ww0
					exec rm $SEARCH_file
				}

				##### ¹ɥ.ww0ˤʤ
				place .ww0.l0 -x 5 -y 2
				place .ww0.e0 -x 45 -y 5 -width 98
				place .ww0.e1 -x 145 -y 5 -width 100

				place .ww0.line0 -x 5 -y 40 -width 240 -height 4
				place .ww0.l1 -x 5 -y 50
				place .ww0.e2 -x 45 -y 50 -width 200
				place .ww0.l2 -x 5 -y 75
				place .ww0.e3 -x 45 -y 75 -width 200
				place .ww0.l3 -x 5 -y 100
				place .ww0.e4 -x 45 -y 100 -width 25
				place .ww0.rb0 -x 120 -y 100
				place .ww0.rb1 -x 170 -y 100

				place .ww0.line1 -x 5 -y 130 -width 240 -height 4
				if { $Total_value > 1 } then {
					place .ww0.b0 -x 5 -y 140 -width 44
				}
				if { $Total_value > 1 } then {
					place .ww0.b1 -x 54 -y 140 -width 44
				}
				place .ww0.b2 -x 103 -y 140 -width 44
				place .ww0.b3 -x 152 -y 140 -width 44
				place .ww0.b4 -x 201 -y 140 -width 44
			}
		}
	}

	button .w1.b1 -text "Ĥ" -command "destroy .w1"

	## ҥɥ.w1ˤʤ
	place .w1.l0 -x 5 -y 2
	place .w1.l1 -x 45 -y 5

	place .w1.line0 -x 5 -y 40 -width 290 -height 4
	place .w1.rb0 -x 5 -y 50
	place .w1.rb1 -x 5 -y 75
	place .w1.rb2 -x 5 -y 100
	place .w1.rb3 -x 150 -y 50
	place .w1.rb4 -x 150 -y 75
	place .w1.l2 -x 150 -y 100
	place .w1.rb5 -x 180 -y 100
	place .w1.rb6 -x 230 -y 100

	place .w1.line1 -x 5 -y 130 -width 290 -height 4
	place .w1.l3 -x 5 -y 140
	place .w1.e0 -x 100 -y 140 -width 195

	place .w1.line2 -x 5 -y 170 -width 290 -height 4
	place .w1.b0 -relx 0.333 -y 180 -anchor n
	place .w1.b1 -relx 0.666 -y 180 -anchor n
}

#########################################
## 3.Add Listܥ
#########################################
button .b2 -image ads_new -width 38 -height 38 -command {

	## ҥɥ.w2ˤδ
	toplevel .w2
	wm title .w2 "Tk_Adress Add List"
	.w2 configure -width 250 -height 170

	## ҥɥ.w2ˤΥ٥κ
	label .w2.l0 -image ads_new
	label .w2.l1 -text ""
	label .w2.l2 -text ""
	label .w2.l3 -text ""
	label .w2.l4 -text ""
	label .w2.line0 -relief sunken
	label .w2.line1 -relief sunken

	## ҥɥ.w2ˤΥȥ꡼κ
	entry .w2.e0 -textvariable New_1name
	entry .w2.e1 -textvariable New_2name
	entry .w2.e2 -textvariable New_ads
	entry .w2.e3 -textvariable New_tel
	entry .w2.e4 -textvariable New_alpha

	## ҥɥ.w2ˤΥ饸ܥκ
	radiobutton .w2.rb0 -text "" -value "" -variable New_sex -anchor w
	radiobutton .w2.rb1 -text "" -value "" -variable New_sex -anchor w

	## ҥɥ.w2ˤΥܥκ
	button .w2.b0 -text "ɲ" -command {
		### ǡɲäޤ
		exec echo $New_alpha $New_1name $New_2name $New_ads $New_tel $New_sex >> $ADS_LIST
		### ɲóǧΥɽޤ
		tk_messageBox -type ok -default ok -message "Ͽޤ"
	}

	button .w2.b1 -text "Ĥ" -command "destroy .w2"

	## ҥɥ.w2ˤʤ
	place .w2.l0 -x 5 -y 2
	place .w2.e0 -x 45 -y 5 -width 98
	place .w2.e1 -x 145 -y 5 -width 100

	place .w2.line0 -x 5 -y 40 -width 245 -height 4
	place .w2.l1 -x 5 -y 50
	place .w2.e2 -x 45 -y 50 -width 200
	place .w2.l2 -x 5 -y 75
	place .w2.e3 -x 45 -y 75 -width 200
	place .w2.l3 -x 5 -y 100
	place .w2.e4 -x 45 -y 100 -width 25
	place .w2.rb0 -x 120 -y 100
	place .w2.rb1 -x 170 -y 100

	place .w2.line1 -x 5 -y 130 -width 245 -height 4
	place .w2.b0 -relx 0.333 -y 140 -anchor n
	place .w2.b1 -relx 0.666 -y 140 -anchor n
}

#########################################
## 4.Quitܥ
#########################################
button .b3 -image ads_close -width 38 -height 38 -command exit

#########################################
## ƥɥ.Ѥʤ
#########################################
grid .b0 -column 0 -row 0
grid .b1 -column 1 -row 0
grid .b2 -column 2 -row 0
grid .b3 -column 3 -row 0
grid .l0 -column 0 -row 1
grid .l1 -column 1 -row 1
grid .l2 -column 2 -row 1
grid .l3 -column 3 -row 1
