
--# File Editor, Glade version of test25.ex

include GtkEngine.e
include GtkPrinter.e

constant docs = `Demonstrates 
    menus, 
    file chooser,
    file filters, 
    recent chooser,
    fonts, 
    exec, etc`
 
    add(builder,"~/demos/glade/test25.glade")

object current_font = get("fontchooserdialog1","font")
    set("textview1","font",current_font)
    set("fontchooserdialog1","font",current_font)
    
object current_file = 0

-- Can't find a way to add more than one filter directly in Glade, 
-- nor any way to set the filter's name, which is what shows in the
-- drop down filter selector... so we do it manually:
    
FileFilter f1 = get(builder,"object","filefilter1") 
    set(f1,"name","Euphoria files")
    set("filechooserdialog1","add filter",f1)
    
FileFilter f2 = get(builder,"object","filefilter2") 
    set(f2,"name","All Text Files")
    set("filechooserdialog1","add filter",f2)
 
-- seems that Glade has no way to directly add a drop-down 
-- RecentChooser to main menu so we do this the 'old fashioned' way;

RecentFilter rcf = pointer("recentfilter1")  
RecentChooserMenu rc_menu = create(GtkRecentChooserMenu,{
    {"add filter",rcf},
    {"sort type",GTK_RECENT_SORT_MRU},
    {"show icons",TRUE},
    {"local only",TRUE},
    {"show numbers",TRUE},
    {"show tips",TRUE}})
    set("recent_menu","submenu",rc_menu)
    connect(rc_menu,"selection-done","open_recent",rc_menu)

main()

------------------------------------------------------------------------------
global function menu_open_file(Button self, atom dialog)
------------------------------------------------------------------------------
object fn -- storage for filename
if get(dialog,"run") = 1 -- 'ok' button's Response ID (must set in Glade)
    then fn = get(dialog,"filename") -- actually, gets the full path to file
    if file_exists(fn) then
        LoadText(fn)
    end if
end if
set(dialog,"hide") -- don't destroy this dialog, keep it for later use
return 1
end function

------------------------------------------------------------------------
global function open_recent()
------------------------------------------------------------------------
object fn = get(rc_menu,"current uri") 
    fn = fn[8..$] -- discard the leading FILE:///
    if file_exists(fn) then
        LoadText(fn)
    end if
return 1
end function

---------------------------------
procedure LoadText(sequence fn)
---------------------------------
    set("window1","title",fn)
    set("textbuffer1","text",read_file(fn))
    set("menu_close","sensitive",TRUE)
    set("menu_open","sensitive",FALSE)
    set("menu_exec","sensitive",TRUE)
    set("menu_print","sensitive",TRUE)
    current_file = fn
end procedure

------------------------------------------------------------------------
global function menu_close_file(atom self, atom data)
------------------------------------------------------------------------
    set("textbuffer1","text","") -- clear the text
    set("menu_close","sensitive",FALSE)
    set("menu_open","sensitive",TRUE)
    set("menu_exec","sensitive",FALSE)
    set("menu_print","sensitive",FALSE)
return 1
end function

------------------------------------------------------------------------
global function on_menu_exec_activate()
------------------------------------------------------------------------
    system("eui " & current_file & " & ",0) -- run the current file;
return 1
end function

-----------------------------------------------------------------------
global function on_menu_print_activate() 
-----------------------------------------------------------------------
  printer:PrintFile(current_file)
return 1
end function

------------------------------------------------------------------------
global function on_menu_colors_activate(atom self, atom view)
------------------------------------------------------------------------
-- hack:
-- colors are passed in Glade 'name' field as fgnd,bkgnd (no quotes)
object params = split(get(self,"name"),',') 
    set(view,"foreground",params[1])
    set(view,"background",params[2])
return 1
end function

-------------------------------------------------------------------------------
global function on_menu_font_activate(atom self, atom dialog)
-------------------------------------------------------------------------------
    set(dialog,"filter func",call_back(routine_id("FilterFonts")))
    set(dialog,"font",current_font) 
    if get(dialog,"run") then
        current_font = get(dialog,"font")
        set("textview1","font",current_font)
    set(dialog,"hide")
    end if
return 1
end function

------------------------------------------------------------------------
function FilterFonts(atom family) 
------------------------------------------------------------------------
sequence fontlist = { -- faces to show in font selection dialog;
    "DejaVu Sans Mono","Courier 10 Pitch","Courier New","FreeSerif",
    "Impact","URW Chancery L","Comic","Purisa","Arial","Arial Black",
    "Sawasdee","Balmoral D","Ubuntu Mono"
    }
object fontname = gtk_str_func("pango_font_family_get_name",{P},{family})
-- process each font the dialog tries to load;
return find(fontname,fontlist) 
-- return 1 if it's on our list, otherwise, return 0 to skip this font;
end function

------------------------------------------------------------------------
global function on_menu_normal_activate(atom self, atom view)
------------------------------------------------------------------------
FontDescription fnt = create(PangoFontDescription,current_font)
  set(fnt,"weight",PANGO_WEIGHT_NORMAL)
  set(fnt,"style",PANGO_STYLE_NORMAL)
  current_font = get(fnt,"to string")
  set(view,"font",current_font)
return 1
end function

------------------------------------------------------------------------
global function on_menu_bold_activate(atom self, atom view)
------------------------------------------------------------------------
FontDescription fnt = create(PangoFontDescription,current_font)
  set(fnt,"weight",PANGO_WEIGHT_BOLD)
  current_font = get(fnt,"to string")
  set(view,"font",current_font)
return 1
end function

------------------------------------------------------------------------
global function on_menu_italic_activate(atom self, atom view)
------------------------------------------------------------------------
FontDescription fnt = create(PangoFontDescription,current_font)
  set(fnt,"style",PANGO_STYLE_ITALIC)
  current_font = get(fnt,"to string")
  set(view,"font",current_font)   
return 1
end function

------------------------------------------------------------------------
global function on_menu_about_activate()
------------------------------------------------------------------------
   return Info("window1"," About ",
    "Simple Text\nViewer/Editor",docs,,"accessories-text-editor","gnome-help") 
end function

