Code Sample |
mb:add("&File/&New File", 0, new_cb) mb:add("&File/&Open File", 0, open_cb) mb:add("&File/&Insert File", 0, insert_cb) mb:add("&File/&Save File", 0, save_cb) mb:add("&File/Save File &As", 0, saveas_cb) mb:add("&File/E&xit", 0, exit_cb) mb:add("&Edit/&Undo", 0, undo_cb) mb:add("&Edit/Cu&t", 0, cut_cb) mb:add("&Edit/&Copy", 0, copy_cb) mb:add("&Edit/&Paste", 0, paste_cb) mb:add("&Edit/&Delete", 0, delete_cb) mb:add("&Search/&Find...", 0, find_cb) mb:add("&Search/Find &Again", 0, findagain_cb) mb:add("&Search/&Replace...", 0, replace_cb) mb:add("&Tools/&Insert Date", 0, insertdate_cb) mb:add("&Help/&About", 0, about_cb) |
Code Sample |
#!/bin/lua version = "0.02" version = "0.03" -- added resizable, get argument for DND, default 800x600 - by RS ww = 800 wh = 600 title = "MURGA LUA FLTK EDITOR - " -- Main window w = fltk:Fl_Double_Window(ww,wh,title) w:resizable(w) -- Text Buffer to hold file to edit tb = fltk:Fl_Text_Buffer(); changed = false filename = "" saved = true newfile = false lastsearch = "" startPos = 1 -- This will be used to hold copied / cutted text clipboard = "" -- Editor widget will work for standard keyboard shortcuts ed = fltk:Fl_Text_Editor(10,30,ww-20,wh-60) ed:buffer(tb) function ask_save(object) if (not saved) then if fltk.fl_choice("There are unsevd Buffers. Do you want to save ?","No",nil,"Yes") >= 1 then save_cb() end end end -- check if we need to save to close function check_save(object) if (not saved) then if fltk.fl_choice("There are unsaved Buffers. Do you Really want to exit ?","No",nil,"Yes") >= 1 then w:hide() end else w:hide() end end -- FILE MENU CALLBACKS function new_cb() ask_save() newfile = true filename = "untitled" saved = false tb:remove(0,tb:length()) w:label(title .. filename) end function open_cb() ask_save() filename = fltk.fl_file_chooser("Select File to open", nil, nil, 0) if (filename ~= nil) then tb:loadfile(filename) w:label(title .. filename) saved = false end end function insert_cb() local fname = fltk.fl_file_chooser("Select File to insert", nil, nil, 0) if (fname ~= nil) then local pos = ed:insert_position() tb:insertfile(fname,pos) end end function save_cb() if (newfile) then saveas_cb() elseif (not saved) then if (tb:savefile(filename)) then saved = true fltk.fl_message("File " .. filename .. " Saved.") else fltk.fl_alert("Error writing file " .. filename) end end end function saveas_cb() local fname = fltk.fl_file_chooser("FILENAME TO SAVE", nil, nil, 0) if (fname ~= nil) then if (tb:savefile(fname)) then filename = fname w:label(title .. filename) saved = true fltk.fl_message("File " .. filename .. " Saved.") else fltk.fl_alert("Error writing file " .. filename) end end end function close_cb() ask_saved() end function exit_cb() check_save() end -- EDIT MENU CALLBACKS function undo_cb() end function cut_cb() if ( tb:selected() ~= 0) then clipboard = tb:selection_text() tb:remove_selection() end end function copy_cb() if (tb:selected() ~= 0) then clipboard = tb:selection_text() end end function paste_cb() local pos = ed:insert_position() tb:insert(pos,clipboard) end function delete_cb() if (tb:selected() ~= 0) then tb:remove_selection() end end function delete_cb() if (tb:selected() ~= 0) then tb:remove_selection() end end -- SEARCH MENU CALLBACKS function find_cb() local str, fnd, fndEnd startPos=1 str = fltk.fl_input("Insert a string to search", lastsearch) if (str ~= nil) then --fnd = tb:search_forward(0,str,fnd,0) fnd,fndEnd = string.find(tb:text(),str,startPos) if (fnd ~= nil) then tb:select(fnd-1, fndEnd) lastsearch = str startPos = fndEnd else fltk.fl_message("String '" .. str .. "' Not found!") end end end function findagain_cb() if (lastsearch == "") then find_cb() else fnd,fndEnd= string.find(tb:text(),lastsearch,startPos) if (fnd ~= nil) then tb:select(fnd-1, fndEnd) startPos = fndEnd else fltk.fl_message("String '" .. lastsearch .. "' Not found!") startPos=1 end end end function replace_cb() replace_dlg:show() end -- TOOLS CALLBACKS function insertdate_cb() local pos = ed:insert_position() local date = os.date() tb:insert(pos,date) end -- HELP CALLBACKS function about_cb() fltk.fl_message("MURGA LUA FLTK EDITOR\n\nMurga Lua Version of FLTK editor example\nBy Mauro Viarizzo (mav_it)\n\nVersion " .. version) end --- Build Up dynamically the menu: mb = fltk:Fl_Menu_Bar(0,0,ww,30) mb:callback(menuItem) mb:add("&File/&New File", 0, new_cb) mb:add("&File/&Open File", 0, open_cb) mb:add("&File/&Insert File", 0, insert_cb) mb:add("&File/&Save File", 0, save_cb) mb:add("&File/Save File &As", 0, saveas_cb) mb:add("&File/E&xit", 0, exit_cb) mb:add("&Edit/&Undo", 0, undo_cb) mb:add("&Edit/Cu&t", 0, cut_cb) mb:add("&Edit/&Copy", 0, copy_cb) mb:add("&Edit/&Paste", 0, paste_cb) mb:add("&Edit/&Delete", 0, delete_cb) mb:add("&Search/&Find...", 0, find_cb) mb:add("&Search/Find &Again", 0, findagain_cb) mb:add("&Search/&Replace...", 0, replace_cb) mb:add("&Tools/&Insert Date", 0, insertdate_cb) mb:add("&Help/&About", 0, about_cb) --This does not seem to be implemented in MurgaLua FLTK --tb:add_modify_callback(changed_cb, w) --tb:call_modify_callbacks() -- REPLACE DIALOG replace_dlg = fltk:Fl_Window (420, 105, "Replace") replace_find = fltk:Fl_Input ( 70, 10, 330, 25, "Find:") replace_with = fltk:Fl_Input ( 70, 40, 330, 25, "Replace:") replace_all = fltk:Fl_Button ( 10, 70, 90, 25, "Replace All") replace_next = fltk:Fl_Return_Button (105, 70, 120, 25, "Replace Next") replace_skip = fltk:Fl_Button (230, 70, 120, 25, "Skip to Next") replace_cancel = fltk:Fl_Button (355, 70, 60, 25, "Cancel") replPos=1 -- CALLBACKS FOR REPLACE DIALOG function replace_next_cb(mesg,doRepl) local findTxt = replace_find:value() local replTxt = replace_with:value() local fnd, fndEnd fnd,fndEnd = string.find(tb:text(),findTxt,replPos) if (fnd ~= nil) then if (doRepl) then tb:replace(fnd-1, fndEnd, replTxt) end replPos = tb:word_end(fnd)+1 return true else if (mesg) then fltk.fl_message("String '" .. findTxt .. "' Not found!") end replPos = 1 return false end end function replace_all_cb() local replaces = 0 while (replace_next_cb(false, true)) do replaces = replaces + 1 end fltk.fl_message("Replaced " .. replaces .. " occurrences") end function replace_skip_cb() replace_next_cb(true,false) end replace_cancel:callback(function () replace_dlg:hide() end ) replace_next:callback(function () replace_next_cb(true, true) end) replace_skip:callback(function () replace_skip_cb() end) replace_all:callback(function () replace_all_cb() end) -- MAIN PROGRAM if #arg == 1 then filename = arg[1] tb:loadfile(filename) w:label(title .. filename) saved=false end w:show() Fl:run() |