
-----------------------------------------------------------------------------
--# GtkSearchBar -- type a word which appears in the text;
-----------------------------------------------------------------------------

include GtkEngine.e
include GtkEvents.e

requires("3.10","GtkSearchBar")

integer vis = TRUE

constant hdr = "<span font='bold 16'><u>GtkSearchBar</u></span>\n"

constant txt = 
"""
 A container made to have a search entry 
 (possibly with additional widgets, 
 such as drop-down menus, or buttons) 
 built-in. 
 
 The search bar would appear when a search 
 is started through typing on the keyboard, 
 or the application's search mode is toggled on.

For keyboard presses to start a search, 
events will need to be forwarded from the 
top-level window that contains the search bar. 
See gtk_search_bar_handle_event() for example code. 
Common shortcuts such as Ctrl+F should be handled 
as an application action, or through the menu items.

Type a word which appears in the text above, or hit the esc key
to clear.

"""

constant win = create(GtkWindow,
	"border=10,size=500x100,position=1,$key-press-event=onClick,$destroy=Quit")

constant panel = create(GtkBox,VERTICAL)
	add(win,panel)

constant sb = create(GtkSearchBar)
	set(sb,"show close button",TRUE)
	add(panel,sb)

constant srch = create(GtkSearchEntry)
	set(sb,"connect entry",srch)
	connect(srch,"search-changed",_("ShowText"))
	add(sb,srch)
	
constant cap = create(GtkLabel)
	set(cap,"markup",hdr)
	add(panel,cap)
	
constant lbl = create(GtkLabel)
	set(lbl,"markup",txt)
	add(panel,lbl)

constant box = pack_end(panel,create(GtkButtonBox,HORIZONTAL))

constant btn1 =	create(GtkButton,"gtk-quit","Quit")
	add(box,btn1)
	
show_all(win)
main()

---------------------------------------
global function onClick(atom ctl, atom event) -- detect keypresses in window;
---------------------------------------
return get(sb,"handle event",event)
end function

---------------------------
function ShowText(atom ctl) -- simple way to highlight found text;
---------------------------

object st = get(ctl,"text") -- search term;

object t = get(lbl,"text") -- current text;

if length(st) > 1 then
    t = split(t,st)
    set(lbl,"markup",flatten(t,sprintf("<b><span underline='double' color='blue'>%s</span></b>",{st}))) -- bold it;
else
    set(lbl,"markup",txt) -- restore original;
end if

return 1
end function
