
----------------------------------------------------------------------------
--# Demo of a simple highlighting text editor using GtkSourceView
-- This seems to be able to detect over 100 different languages.
----------------------------------------------------------------------------

include GtkEngine.e
include GtkCairo.e
include GtkSourceView.plugin -- 'plugins' allow extending EuGTK on the fly

include GtkSettings.e -- for saving editor last file and window size, pos.
include GtkFileSelector.e

constant eddie = create(GdkPixbuf,"thumbnails/text-editor.png",100,100)

object working_file = 0
boolean dirty = FALSE

constant inifile = canonical_path("~/.test201.ini") -- in home directory

constant win = create(GtkWindow,{
	{"name","Main Window"}, -- important so ini can access!
	{"default size",900,600}, -- may be overridden by ini.
	{"border width",10},
	{"icon",eddie},
	{"connect","delete-event",_("Bail")}}) -- on Bail, save ini and quit.

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

constant scroller = create(GtkScrolledWindow)
	pack(panel,scroller,TRUE,TRUE)

constant sv = create(GtkSourceView,{
	{"show line numbers",TRUE},
	{"tab width",4},
	{"indent width",4},
	{"indent on tab",TRUE},
	{"auto indent",TRUE},
	{"font","Ubuntu mono bold 12"}, -- overridden by ini
	{"show right margin",TRUE},
	{"show line marks",TRUE},
	{"draw spaces",FALSE},
	{"insert spaces instead of tabs",FALSE},    
	{"right margin position",90},
	{"highlight current line",TRUE},
	{"wrap mode",GTK_WRAP_WORD_CHAR},
	$})
	add(scroller,sv)

constant buf = get(sv,"buffer")

constant mgr = create(GtkSourceStyleSchemeManager) 
constant ids = get(mgr,"scheme ids")

constant lm = create(GtkSourceLanguageManager) 
	-- display(get(lm,"language ids")) -- a long list!
	
constant 
	btn1 = create(GtkButton,"gtk-quit",_("Bail")),
	btn2 = create(GtkButton,"gtk-open",_("OpenFile")),
	btn3 = create(GtkButton,"gtk-save",_("SaveFile")),
	btn4 = create(GtkButton,"gtk-execute",_("RunFile")),
	btn5 = create(GtkFontButton,,_("SetFont")),
	btn6 = create(GtkComboBoxText)
	set(btn6,"append text","Styles")
	set(btn6,"active",1)
	for i = 1 to length(ids) do
		set(btn6,"append text",ids[i])
	end for
	connect(btn6,"changed",_("ChangeStyleScheme"))
	
	set(btn5,"name","Font Button")
	set(btn5,"filter func",_("FontFilter"))

	set(btn2,"tooltip markup","Select any sourcecode file to edit")
	set(btn4,"tooltip markup","Run a Euphoria program")

constant box = create(GtkButtonBox,{
	{"orientation",HORIZONTAL},
	{"layout",GTK_BUTTONBOX_SPREAD},
	{"margin top",10}})

	add(box,{btn1,btn2,btn3,btn4,btn5,btn6})
	pack_end(panel,box)

settings:Load(inifile,1)

working_file = get("Main Window","title")

show_all(win)

if file_exists(working_file) then
	OpenFile(working_file)
else
	Warn(win,,"Cannot open file",working_file)
end if

connect(buf,"changed",_("SetDirty"))

main()

-------------------------------------
function ChangeStyleScheme(atom ctl)
-------------------------------------
atom scheme  = get(mgr,"scheme",get(ctl,"active text")) 
set(buf,"style scheme",scheme)
set(mgr,"force rescan")
return 1
end function

--------------------------------
function OpenFile(object name=0)
--------------------------------

	if atom(name) then
		fileselector:filters= {"euphoria","html","text"}
		name = fileselector:Open()
		if not string(name) then return 0
		end if
	end if

	set(sv,"font",get("Font Button","font name"))

	set(buf,"text",read_file(name))
	set(win,"title",name)

	object lang = get(lm,"guess language",filename(name)) 
	sequence mimetypes = {}

	if lang > 0 then
	set(buf,"language",lang) 
	mimetypes = get(lang,"mime types")
	Info(win,"File Properties",
		text:format("</b>File:<b> []",{filename(name)}),
		text:format("Language: <b>[]</b>\nSection: <b>[]</b>\nMime types: <b>[]</b>",
		{get(lang,"name"),get(lang,"section"),mimetypes}),,
		eddie)
	end if

	working_file = name
	dirty = FALSE

set(btn4,"sensitive", equal("ex",fileext(working_file)))

return 1
end function

-----------------------------
function SetFont(atom ctl) -- set view font as selected by font button;
-----------------------------
	set(sv,"font",get(ctl,"font"))
return 1
end function

-------------------
function SetDirty()
-------------------
	dirty = TRUE
return 1
end function

----------------------
function SaveFile() -- save the [updated] text;
----------------------
	if write_file(working_file,get(buf,"text")) then
		dirty=FALSE
	else
		Warn(win,"Write Error",sprintf("Cannot save %s",{working_file}))
	end if
return 1
end function

---------------------
function RunFile() -- execute current file;
---------------------
	if dirty then
		if Question(win,"Dirty","Save changes?") = MB_YES then
			SaveFile()
			dirty = FALSE
		end if
	end if
	if string(working_file) then
		show_uri(working_file)
	end if
return 1
end function

------------------
function Bail() -- save current settings and quit;
------------------
	settings:Save(inifile,{win,btn5},1)
	settings:Add(inifile,"Main Window","title", working_file)
	Quit()
return 1
end function

--------------------------------------------------------
function FontFilter(FontFamily family, atom face) -- show only mono
--------------------------------------------------------
-- filter to include mono only;
	return get(family,"is monospace")
end function

-- here's how to filter "out" unwanted fonts;
	--object name = get(family,"name")
	-- if match(name,"Japanese") then return 0 end if 
	-- if match(name,"Webdings") then return 0 end if

-- here's how to filter "in" some font;
	--if equal(name,"Georgia") then return 1 end if





