# dbquery -- procs for editing a postquel query
#     Ray R. Larson (mar 92)
#

#-------------------------------------------------------
# Database query procedures
#-------------------------------------------------------
# dbquery -- procs for editing a postquel query
#     Ray R. Larson (mar 92)
#
proc dbquery { {w .query}} {
    catch {destroy $w}
    toplevel $w
    wm geometry $w +150+100
    global PGLASTQUERY
    global BIGFONT
    wm title $w "PostQuel Queries"
    wm iconname $w "PostQuel"
    set c $w.c
    set font *times-medium-r-*-140-*

    frame $w.frame1 -relief raised -bd 2
    text $c -relief sunken -width 60 -height 15 -wrap word -font $font -padx 20 -pady 20 -borderwidth 2
    message $w.frame1.msg -font *times-medium-r-normal--*-120* -aspect 500 \
            -justify center \
	    -text "Enter your query in the space below.  You can add characters by pointing, clicking and typing.  You can delete by selecting a segment of text (by clicking and dragging) and typing Control-d.  Backspace, Control-h, and Delete may be typed to erase the character just before the insertion point. Control-v copies selected text (from this window or elsewhere -- such as the list of database attributes) to the insertion point. The Previous and Next buttons move through a historical list of queries in ~/.pgtkqueries.  The Retrieve button submits the query. The Cancel button quits without submitting the query"



    pack append $w $w.frame1 {top fill} $w.c {expand fill}
    pack append $w.frame1 $w.frame1.msg {frame center}

     # First, put default text in the item and give it bindings so it can be 
     # edited more easily.

    $c insert end "retrieve () from "
    bind $c <Right> "$c mark set insert {insert + 1 chars}"
    bind $c <Left> "$c mark set insert {insert - 1 chars}"
    bind $c <Up> "$c mark set insert {insert - 1 lines}"
    bind $c <Down> "$c mark set insert {insert + 1 lines}"



frame $w.bot -borderwidth {2}  -geometry {20x20}  -relief {raised}

button $w.bot.b_cancel   -text {Cancel} \
              -command "destroy $w"

button $w.bot.b_retr   -text {Retrieve}\
             -command "getquery $w"

button $w.bot.b_prev   -text {Previous Query}\
             -command "prevquery $w"

button $w.bot.b_next   -pady 10  -text {Next Query} -state disabled \
             -command "nextquery $w"


pack append $w.bot \
              $w.bot.b_prev {left frame center  expand fillx filly} \
              $w.bot.b_next {left frame center  expand fillx filly} \
              $w.bot.b_retr {left frame center  expand fillx filly} \
              $w.bot.b_cancel {left frame center expand fillx filly}


    pack append $w $w.bot {bottom fill}


}


proc getquery {w} {
    global PGLASTQUERY PGQUERYLIST PGQUERYNUM
    set c $w.c
    set PGLASTQUERY [$c get 1.0 end]
    if {[llength $PGQUERYLIST] == 0} {
        if {[file exists ~/.pgtkquery]} {
		set fileid [open ~/.pgtkquery r]
                while {[gets $fileid line] != -1} {
			lappend PGQUERYLIST [format $line ]
		}
		close $fileid
                set PGQUERYNUM [expr [llength $PGQUERYLIST]-1]
	} else {
		set fileid [open ~/.pgtkquery w]
		puts $fileid $PGLASTQUERY
		lappend PGQUERYLIST $PGLASTQUERY
		set PGQUERYNUM 1
	}
    }
    lappend PGQUERYLIST $PGLASTQUERY
    set PGQUERYNUM [expr [llength $PGQUERYLIST]-1]
    browsedocs $w $PGLASTQUERY "Browse Query Results for:\n $PGLASTQUERY "
}


proc prevquery {w} {
    global PGLASTQUERY PGQUERYLIST PGQUERYNUM

    if {[llength $PGQUERYLIST] == 0} {
	#load the list from a the ~/.pgtkquery file
        if {[file exists ~/.pgtkquery]} {
		set fileid [open ~/.pgtkquery r]
                while {[gets $fileid line] != -1} {
			lappend PGQUERYLIST [format $line ]
		}
		close $fileid
                set PGQUERYNUM [expr [llength $PGQUERYLIST]]
	} else {
		set fileid [open ~/.pgtkquery w]
		puts $fileid $PGLASTQUERY
		lappend PGQUERYLIST $PGLASTQUERY
		set PGQUERYNUM 1
	}
    }
    set c $w.c
    set oldq [$c get 1.0 end]
    $c delete 1.0 end
    if {[incr PGQUERYNUM -1] < 0} {set PGQUERYNUM 0}
    set PGLASTQUERY [lindex $PGQUERYLIST $PGQUERYNUM]
    $c insert end $PGLASTQUERY
    $w.bot.b_next config -state normal
}

proc nextquery {w} {
    global PGLASTQUERY PGQUERYLIST PGQUERYNUM
    set c $w.c
    set oldq [$c get 1.0 end]
    $c delete 1.0 end
    if {[incr PGQUERYNUM] > [llength $PGQUERYLIST]-1} {
        set PGQUERYNUM [expr [llength $PGQUERYLIST]-1]
        $w.bot.b_next config -state disabled
    }
    set PGLASTQUERY [lindex $PGQUERYLIST $PGQUERYNUM]
    $c insert end $PGLASTQUERY

}


proc savequerylist {} {
    global PGQUERYLIST

    if {[llength $PGQUERYLIST] == 0} {return}

    set fileid [open ~/.pgtkquery w]
    foreach query $PGQUERYLIST {
        if {[regsub -all "\n" $query "\\n" temp]} {
	        puts $fileid $temp
	} else {
		puts $fileid $query
	}
    }
    close $fileid
}






