# mupdf::widget demo
catch {console show}

set THIS_DIR [file dirname [file normalize [info script]]]
lappend auto_path $THIS_DIR/..
lappend auto_path $THIS_DIR

set res [catch {package require tkMuPDF 2.0}]
if { $res } {
	tk_messageBox -message {
package mupdf not loaded.
		
Check if package tkMuPDF is installed under a directory listed in "auto_path"
	}
	exit
}
package require mupdf::widget

namespace eval GUI {
	variable textSearch {}

	 # page up/down
	bind MuPDFWidget <Key-Next>  {%W nextpage}
	bind MuPDFWidget <Key-Prior> {%W prevpage}
	 # Button-1 and drag, scroll the page
	bind MuPDFWidget <ButtonPress-1> { focus %W ; %W scan mark %x %y }
	bind MuPDFWidget <B1-Motion> { %W scan dragto %x %y 1 }

	 # zoom +/-
	bind MuPDFWidget <Key-plus>  { %W rzoom +1 }
	bind MuPDFWidget <Key-minus> { %W rzoom -1 }
	bind MuPDFWidget <Key-x> { %W zoomfit x }
	bind MuPDFWidget <Key-y> { %W zoomfit y }
	bind MuPDFWidget <Key-z> { %W zoomfit xy }
		 
	bind MuPDFWidget <Control-Key-f> { GUI::openSearchPanel %W }
}

proc GUI::help {} {
	if { [winfo exists .tophelp] } return
	
	toplevel .tophelp -padx 10 -pady 10
	text .tophelp.descr
	pack .tophelp.descr -expand 1 -fill both
		
	.tophelp.descr insert end { \
mupdf::widget -- DEMO keyboards controls
-----------------------------------------

<PageUp>       --  previous page
<PageDown>     --  next page

< + >          -- increment zoom
< - >          -- decrement zoom

<up>/<down>    -- scroll page up/down
<right>/<left> -- scroll page right/left

<X>            -- fit page width
<Y>            -- fit page height
<Z>            -- fit page (best fit)

<ctrl-F>       -- search text ...

mupdf::widget -- DEMO Mouse controls:
-------------------------------------
click and drag for scrolling the page
}
		.tophelp.descr  configure -state disabled
		wm attributes .tophelp -topmost true
}

proc GUI::doSearch { pdfW } {
	variable textSearch
	$pdfW search $textSearch			
}


proc GUI::openSearchPanel { pdfW } {
	variable textSearch
	
	set textSearch {}
	
	set panelW [toplevel $pdfW.searchPanel -padx 20 -pady 20]
	wm title $panelW "Search ..."
	wm attributes $panelW -topmost true
	 
	 # don't use ttk::entry, since it cannot change the text color !
	entry $panelW.search -textvariable GUI::textSearch  
	button $panelW.ok -text "Search" 
	pack $panelW.search -fill x
	pack $panelW.ok

	$panelW.ok configure -command [list GUI::doSearch $pdfW]
	bind $panelW.search <Return> [list GUI::doSearch $pdfW] 

   	 # place the new panel close to the pdfW widget
	set x0 [winfo rootx $pdfW]
	set y0 [winfo rooty $pdfW]
	wm geometry $panelW +[expr {$x0-10}]+[expr {$y0-10}] 

	 # when this panel is closed, reset the search
	bind $panelW <Destroy> [list apply { 
		{W panelW pdfW} {
		     # NOTE: since <Destroy> is propagated to all children,
			 #  the following "if", ensure that this core script is executed
			 #  just once.
			if {  $W != $panelW } return
			$pdfW search ""
		}} %W $panelW $pdfW]

	focus $panelW.search		
}


proc formatCoords {L} {
    lassign $L x y
    format "(%.1f, %.1lf)" $x $y
}

 # SHOW PDF coords  (in Points)
bind MuPDFWidget <Motion> { 
    set coordsPdfStr  [formatCoords [.c win2PDFcoords %x %y]] 
}

proc GUI::main {filename} {
	wm title . [file tail $filename]
	set pdf [mupdf::open $filename]
	mupdf::widget .c $pdf
	pack .c -side left -expand 1 -fill both

	pack [label .coordspdf_lbl -text "  PDF coords (points)  "]
    pack [label .coordspdf -textvariable coordsPdfStr]
	pack [ttk::button .help -text "Help..." -command { GUI::help }]    

}

	GUI::main [file join $THIS_DIR demo.pdf]

	update
	.c zoomfit xy
	focus .c