// filename: Editor.cpp // author: John Flores // date: May, 2003. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #include #include "Editor.h" Editor::Editor(): cmdlist(16, 28, (LINES-16)/3, (COLS-28)/2, this), textbox(LINES - 1, COLS, 0, 0, this) { keypad(stdscr, true); cmdlist.additem(SAVE_EXIT, "Save & Exit Ctrl X"); cmdlist.additem(SAVE, "Save Ctrl S"); cmdlist.additem(EXIT, "Exit (Quit) Ctrl Q"); cmdlist.addseparator( "----------------------"); cmdlist.additem(CUT, "Cut (Kut) Ctrl K"); cmdlist.additem(COPY, "Copy (seLect) Ctrl L"); cmdlist.additem(PASTE, "Paste Ctrl P"); cmdlist.addseparator( "----------------------"); cmdlist.additem(FIND, "Find Ctrl F"); cmdlist.additem(REPLACE, "Replace Ctrl R"); cmdlist.additem(GOTOLINE, "Go to line Ctrl G"); cmdlist.additem(TOGGLENUMS, "Toggle numbers Ctrl N"); cmdlist.addseparator( "----------------------"); cmdlist.additem(ABOUT, "About ezed F1"); strcpy(filename, ""); strcpy(txtfind, ""); strcpy(txtreplace, ""); } bool Editor::edit(const char *fname) { bool done = false; int keypress; int command; if (fname != NULL) { strncpy(filename, fname, FILENAME_LEN); filename[FILENAME_LEN] = '\0'; textbox.load(fname); } else { strcpy(filename, ""); } dspmsg("File \"", filename, "\". Press escape key for command menu."); do { keypress = textbox.edit(); if (keypress == key::ESCAPE) keypress = cmdlist.getitem(command); else command = keypress; switch (command) { case SAVE_EXIT: save(); done = true; break; case SAVE: save(); break; case EXIT: done = true; break; case CUT: textbox.cut(); break; case COPY: textbox.copy(); break; case PASTE: textbox.paste(); break; case FIND: find(); break; case GOTOLINE: gotoline(); break; case REPLACE: replace(); break; case TOGGLENUMS: textbox.togglenumbers(); break; case ABOUT: dspmsg("EZED - The EZ EDitor version 2.0, john.flores@senecac.on.ca, May 2003."); break; default: command = NONE; break; } } while (!done); return true; } void Editor::find() { if (getstring("Enter text to find:", txtfind, TXTFIND_LEN) != key::ESCAPE) { if (!textbox.find(txtfind)) dspmsg("Cannot find text \"", txtfind, "\"."); } } void Editor::replace() { if (getstring("Enter text to find:", txtfind, TXTFIND_LEN) != key::ESCAPE) { if (textbox.find(txtfind)) { if (getstring("Replace text with:", txtreplace, TXTREPLACE_LEN) != key::ESCAPE) { if (!textbox.replace(txtreplace)) dspmsg("Cannot replace text"); } } else { dspmsg("Cannot find text \"", txtfind, "\"."); } } } void Editor::save() { int keypress; if (strcmp(filename, "") == 0) keypress = getstring("No filename. Enter new filename:", filename, sizeof(filename) - 1); if (keypress != key::ESCAPE) { if (textbox.save(filename)) dspmsg("File \"", filename, "\" saved."); else dspmsg("File \"", filename, "\" not saved."); } } inline void Editor::gotoline() { char buffer[7] = ""; if (getstring("Enter '$' for last line. Go to line number:", buffer, sizeof(buffer) - 1) != key::ESCAPE) { int line = 1; if (strcmp(buffer, "$") == 0) { line = INT_MAX; textbox.gotoline(line); } else if (sscanf(buffer, " %d ", &line) == 1) { textbox.gotoline(line); } } } int Editor::getstring(const char *prompt, char *str, int len) { bool done = false; int curpos = strlen(str); int keypress; int base = strlen(prompt) + 3; while (!done) { mvwhline(stdscr, LINES - 1, 0, '-', COLS); mvwprintw(stdscr, LINES - 1, 2, "%s %s ", prompt, str); keypress = mvwgetch(stdscr, LINES - 1, base + curpos); switch (keypress) { case key::LEFT: if (curpos > 0) --curpos; break; case key::RIGHT: if (curpos < int(strlen(str))) curpos++; break; case key::BACKSPACE: case key::DELETE_ASCII: if (curpos > 0) { memmove(str + curpos - 1, str + curpos, strlen(str) - curpos + 1); curpos--; } break; case key::DELETE: if (strlen(str) > 0) { memmove(str + curpos, str + curpos + 1, strlen(str) - curpos); } break; case key::ENTER: case key::ESCAPE: done = true; break; default: if (isprint(keypress)) { if (int(strlen(str)) < len) { memmove(str + curpos + 1, str + curpos, strlen(str) - curpos + 1); str[curpos++] = keypress; } } break; } } return keypress; } inline void Editor::dspmsg(const char *s1, const char *s2, const char *s3) { mvwhline(stdscr, LINES - 1, 0, '-', COLS); mvwprintw(stdscr, LINES - 1, 2, "%s%s%s", s1?s1:"", s2?s2:"", s3?s3:""); wrefresh(stdscr); } void Editor::notify(Component *notifier) { dspmsg("File \"", filename, "\". Press escape key for command menu."); }