mav_it
Group: Members
Posts: 7
Joined: Mar. 2007 |
|
Posted: Nov. 12 2007,12:15 |
|
Thank to Mikshaw and Roberts, now the example editor works, more or less.
It uses "standard" keybindings for cut/copy/paste and delete text (ctlr+x, ctlr+C, ctrl+V and Del respctively) and the menus are working.
Posted, should someone find useful, as I have found, to learn some murgaLua basics.)
Thank you again for your help, Mauro
Code Sample | #!/bin/lua
version = "0.02"
ww = 1024 wh = 768
title = "MURGA LUA FLTK EDITOR - "
-- Main window w = fltk:Fl_Double_Window(ww,wh,title)
-- 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
w:callback(check_save)
-- Callback for menu bar function menuItem(object) --print(mb:text() .. ' --> ' .. mb:value()) if (mb:text()=="&New File") then new_cb()
-- FILE MENU elseif (mb:text()=="&Open File") then open_cb() elseif (mb:text()=="&Insert File") then insert_cb() elseif (mb:text()=="&Save File") then save_cb() elseif (mb:text()=="Save File &As") then saveas_cb() elseif (mb:text()=="E&xit") then exit_cb()
-- EDIT MENU elseif (mb:text()=="&Undo") then undo_cb() elseif (mb:text()=="Cu&t") then cut_cb() elseif (mb:text()=="&Copy") then copy_cb() elseif (mb:text()=="&Paste") then paste_cb() elseif (mb:text()=="&Delete") then delete_cb()
-- SEARCH MENU elseif (mb:text()=="&Find...") then find_cb() elseif (mb:text()=="Find &Again") then findagain_cb() elseif (mb:text()=="&Replace...") then replace_cb()
-- TOOLS MENU elseif (mb:text()=="&Insert Date") then insertdate_cb()
-- HELP MENU elseif (mb:text()=="&About") then about_cb() 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, 0) mb:add("&File/&Open File", 0, 0) mb:add("&File/&Insert File", 0, 0) mb:add("&File/&Save File", 0, 0) mb:add("&File/Save File &As", 0, 0) mb:add("&File/E&xit", 0, 0)
mb:add("&Edit/&Undo", 0, 0) mb:add("&Edit/Cu&t", 0, 0)
mb:add("&Edit/&Copy", 0, 0) mb:add("&Edit/&Paste", 0, 0) mb:add("&Edit/&Delete", 0, 0)
mb:add("&Search/&Find...", 0, 0) mb:add("&Search/Find &Again", 0, 0) mb:add("&Search/&Replace...", 0, 0)
mb:add("&Tools/&Insert Date", 0, 0)
mb:add("&Help/&About", 0, 0)
--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 w:show() Fl:run()
|
|