editor.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Luigi Negretti Lanner   *
00003  *   luigi.negrettilanner@gmail.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <ctype.h>
00024 #include <errno.h>
00025 
00026 #ifdef __MWERKS__
00027 # define FL_DLL
00028 #endif
00029 
00030 #include <FL/Fl.H>
00031 #include <FL/Fl_Group.H>
00032 #include <FL/Fl_Double_Window.H>
00033 #include <FL/fl_ask.H>
00034 #include <FL/Fl_File_Chooser.H>
00035 #include <FL/Fl_Menu_Bar.H>
00036 #include <FL/Fl_Input.H>
00037 #include <FL/Fl_Button.H>
00038 #include <FL/Fl_Return_Button.H>
00039 #include <FL/Fl_Text_Buffer.H>
00040 #include <FL/Fl_Text_Editor.H>
00041 
00042 void runCs_cb(Fl_Widget*,void*);
00043 void runAudioFile_cb(Fl_Widget*,void*);
00044 void insert_header22050mo_cb(Fl_Widget*,void* v);
00045 void insert_header22050ste_cb(Fl_Widget*,void* v);
00046 void insert_header44100mo_cb(Fl_Widget*,void* v);
00047 void insert_header44100ste_cb(Fl_Widget*,void* v);
00048 void insert_header48000mo_cb(Fl_Widget*,void* v);
00049 void insert_header48000ste_cb(Fl_Widget*,void* v);
00050 void insert_conversions_cb(Fl_Widget*,void* v);
00051 void insert_gen01_cb(Fl_Widget*,void* v);
00052 void insert_gen02_cb(Fl_Widget*,void* v);
00053 void insert_gen03_cb(Fl_Widget*,void* v);
00054 void insert_gen04_cb(Fl_Widget*,void* v);
00055 void insert_gen06_cb(Fl_Widget*,void* v);
00056 void insert_gen07_cb(Fl_Widget*,void* v);
00057 void insert_gen08_cb(Fl_Widget*,void* v);
00058 void insert_gen09_cb(Fl_Widget*,void* v);
00059 void insert_gen10_cb(Fl_Widget*,void* v);
00060 void insert_gen11_cb(Fl_Widget*,void* v);
00061 void insert_gen12_cb(Fl_Widget*,void* v);
00062 void insert_gen13_cb(Fl_Widget*,void* v);
00063 void insert_gen15_cb(Fl_Widget*,void* v);
00064 void insert_gen17_cb(Fl_Widget*,void* v);
00065 void insert_gen19_cb(Fl_Widget*,void* v);
00066 void insert_gen20_cb(Fl_Widget*,void* v);
00067 void insert_gen23_cb(Fl_Widget*,void* v);
00068 void insert_gen25_cb(Fl_Widget*,void* v);
00069 void insert_gen27_cb(Fl_Widget*,void* v);
00070 void insert_gen28_cb(Fl_Widget*,void* v);
00071 void insert_cstags_cb(Fl_Widget*,void* v);
00072 
00073 int                changed = 0;
00074 char               filename[256] = "";
00075 char               title[256];
00076 Fl_Text_Buffer     *textbuf = 0;
00077 
00078 void save_cb();
00079 void saveas_cb();
00080 void find2_cb(Fl_Widget*, void*);
00081 void replall_cb(Fl_Widget*, void*);
00082 void replace2_cb(Fl_Widget*, void*);
00083 void replcan_cb(Fl_Widget*, void*);
00084 
00085 class EditorWindow : public Fl_Double_Window {
00086   public:
00087     EditorWindow(int w, int h, const char* t);
00088     ~EditorWindow();
00089 
00090     Fl_Window          *replace_dlg;
00091     Fl_Input           *replace_find;
00092     Fl_Input           *replace_with;
00093     Fl_Button          *replace_all;
00094     Fl_Return_Button   *replace_next;
00095     Fl_Button          *replace_cancel;
00096 
00097     Fl_Text_Editor     *editor;
00098     char               search[256];
00099 };
00100 
00101 EditorWindow::EditorWindow(int w, int h, const char* t) : Fl_Double_Window(w, h, t) {
00102   replace_dlg = new Fl_Window(300, 105, "Replace");
00103     replace_find = new Fl_Input(80, 10, 210, 25, "Find:");
00104     replace_find->align(FL_ALIGN_LEFT);
00105 
00106     replace_with = new Fl_Input(80, 40, 210, 25, "Replace:");
00107     replace_with->align(FL_ALIGN_LEFT);
00108 
00109     replace_all = new Fl_Button(10, 70, 90, 25, "Replace All");
00110     replace_all->callback((Fl_Callback *)replall_cb, this);
00111 
00112     replace_next = new Fl_Return_Button(105, 70, 120, 25, "Replace Next");
00113     replace_next->callback((Fl_Callback *)replace2_cb, this);
00114 
00115     replace_cancel = new Fl_Button(230, 70, 60, 25, "Cancel");
00116     replace_cancel->callback((Fl_Callback *)replcan_cb, this);
00117   replace_dlg->end();
00118   replace_dlg->set_non_modal();
00119   editor = 0;
00120   *search = (char)0;
00121 }
00122 
00123 EditorWindow::~EditorWindow() {
00124   delete replace_dlg;
00125 }
00126 
00127 /*CHECK SAVE*/
00128 int check_save(void) {
00129 
00130   if (!changed) return 1;
00131 
00132   int r = fl_choice("The current file has not been saved.\n"
00133                     "Would you like to save it now?",
00134                     "Cancel", "Save", "Discard");
00135 
00136   if (r == 1) {
00137     save_cb(); // Save the file...
00138     return !changed;
00139     }
00140 
00141  return (r == 2) ? 1 : 0;
00142  if (r == 2) return !changed;
00143  
00144 }
00145 /*CHECK SAVE*/
00146 
00147 int loading = 0;
00148 void load_file(char *newfile, int ipos) {
00149   loading = 1;
00150   int insert = (ipos != -1);
00151   changed = insert;
00152   if (!insert) strcpy(filename, "");
00153   int r;
00154   if (!insert) r = textbuf->loadfile(newfile);
00155   else r = textbuf->insertfile(newfile, ipos);
00156   if (r)
00157     fl_alert("Error reading from file \'%s\':\n%s.", newfile, strerror(errno));
00158   else
00159     if (!insert) strcpy(filename, newfile);
00160   loading = 0;
00161   textbuf->call_modify_callbacks();
00162 }
00163 
00164 
00165 void save_file(char *newfile) {
00166   if (textbuf->savefile(newfile))
00167     fl_alert("Error writing to file \'%s\':\n%s.", newfile, strerror(errno));
00168   else
00169     strcpy(filename, newfile);
00170   changed = 0;
00171   textbuf->call_modify_callbacks();
00172 }
00173 
00174 void copy_cb(Fl_Widget*, void* v) {
00175   EditorWindow* e = (EditorWindow*)v;
00176   Fl_Text_Editor::kf_copy(0, e->editor);
00177 }
00178 
00179 void cut_cb(Fl_Widget*, void* v) {
00180   EditorWindow* e = (EditorWindow*)v;
00181   Fl_Text_Editor::kf_cut(0, e->editor);
00182 }
00183 
00184 void delete_cb(Fl_Widget*, void*) {
00185   textbuf->remove_selection();
00186 }
00187 
00188 void find_cb(Fl_Widget* w, void* v) {
00189   EditorWindow* e = (EditorWindow*)v;
00190   const char *val;
00191 
00192   val = fl_input("Search String:", e->search);
00193   if (val != NULL) {
00194     // User entered a string - go find it!
00195     strcpy(e->search, val);
00196     find2_cb(w, v);
00197   }
00198 }
00199 
00200 void find2_cb(Fl_Widget* w, void* v) {
00201   EditorWindow* e = (EditorWindow*)v;
00202   if (e->search[0] == '\0') {
00203     // Search string is blank; get a new one...
00204     find_cb(w, v);
00205     return;
00206   }
00207 
00208   int pos = e->editor->insert_position();
00209   int found = textbuf->search_forward(pos, e->search, &pos);
00210   if (found) {
00211     // Found a match; select and update the position...
00212     textbuf->select(pos, pos+strlen(e->search));
00213     e->editor->insert_position(pos+strlen(e->search));
00214     e->editor->show_insert_position();
00215   }
00216   else fl_alert("No occurrences of \'%s\' found!", e->search);
00217 }
00218 
00219 //////TITOLO FINESTRA EDITOR////////
00220 
00221 void set_title(Fl_Window* w) {
00222 
00223   if (filename[0] == '\0') strcpy(title, "Untitled");
00224   else {
00225     char *slash;
00226     slash = strrchr(filename, '/');
00227 #ifdef WIN32
00228     if (slash == NULL) slash = strrchr(filename, '\\');
00229 #endif
00230     if (slash != NULL) strcpy(title, slash + 1);
00231     else strcpy(title, filename);
00232   }
00233 
00234   if (changed) strcat(title, " (modified)");
00235       
00236   w->label(title);
00237 }
00238 //////TITOLO FINESTRA EDITOR////////
00239 
00240 void changed_cb(int, int nInserted, int nDeleted,int, const char*, void* v) {
00241   if ((nInserted || nDeleted) && !loading) changed = 1;
00242   EditorWindow *w = (EditorWindow *)v;
00243   set_title(w);
00244   if (loading) w->editor->show_insert_position();
00245 }
00246 
00247 void new_cb(Fl_Widget*, void*) {
00248   if (!check_save()) return;
00249 
00250   filename[0] = '\0';
00251   textbuf->select(0, textbuf->length());
00252   textbuf->remove_selection();
00253   changed = 0;
00254   textbuf->call_modify_callbacks();
00255 }
00256 
00257 //LNL
00258 void open()
00259 {
00260 //  if (!check_save()) return;
00261 
00262     if (dataScore == 0)
00263     {
00264       char *newfile = fl_file_chooser("Open File?", "*.sco", scoPath->value());
00265       if (newfile != NULL) load_file(newfile, -1);
00266     }
00267     if (dataScore == 1)
00268     {
00269       char *newfile = fl_file_chooser("Open File?", "*.txt", outData->value());
00270       //{ title = outData->value(); }
00271       if (newfile != NULL) load_file(newfile, -1);
00272     }
00273     if (dataScore == 2)
00274     {
00275       char *newfile = fl_file_chooser("Open File?", "*orc|*csd", orcPath->value());
00276       //{ title = orcPath->value(); }
00277       if (newfile != NULL) load_file(newfile, -1);
00278     }
00279 }
00280 
00281 void open_cb(Fl_Widget*, void*)
00282 {
00283     open();
00284 }
00285 
00286 void insert_cb(Fl_Widget*, void *v) {
00287 
00288   if (dataScore == 0) {
00289   char *newfile = fl_file_chooser("Insert File?", "*.sco", filename);
00290   EditorWindow *w = (EditorWindow *)v;
00291   if (newfile != NULL) load_file(newfile, w->editor->insert_position());
00292   }
00293   
00294   else if (dataScore == 1) {
00295   char *newfile = fl_file_chooser("Insert File?", "*.txt", filename);
00296   EditorWindow *w = (EditorWindow *)v;
00297   if (newfile != NULL) load_file(newfile, w->editor->insert_position());
00298   }
00299   else if (dataScore == 2) {
00300   char *newfile = fl_file_chooser("Insert File?", "*.orc", filename);
00301   EditorWindow *w = (EditorWindow *)v;
00302   if (newfile != NULL) load_file(newfile, w->editor->insert_position());
00303   }
00304 }
00305 
00306 void paste_cb(Fl_Widget*, void* v) {
00307   EditorWindow* e = (EditorWindow*)v;
00308   Fl_Text_Editor::kf_paste(0, e->editor);
00309 }
00310 
00311 int num_windows = 0;
00312 
00313 void close_cb(Fl_Widget*, void* v) {
00314 
00315   Fl_Window* w = (Fl_Window*)v;
00316   if (changed && !check_save())
00317 //  if (num_windows == 1 && !check_save())
00318  {
00319   return;
00320   }
00321   textbuf->remove_modify_callback(changed_cb, w);
00322   delete w;
00323   num_windows--;
00324 //  if (!num_windows) exit(0);
00325 }
00326 
00327 
00328 void replace_cb(Fl_Widget*, void* v) {
00329   EditorWindow* e = (EditorWindow*)v;
00330   e->replace_dlg->show();
00331 }
00332 
00333 void replace2_cb(Fl_Widget*, void* v) {
00334   EditorWindow* e = (EditorWindow*)v;
00335   const char *find = e->replace_find->value();
00336   const char *replace = e->replace_with->value();
00337 
00338   if (find[0] == '\0') {
00339     // Search string is blank; get a new one...
00340     e->replace_dlg->show();
00341     return;
00342   }
00343 
00344   e->replace_dlg->hide();
00345 
00346   int pos = e->editor->insert_position();
00347   int found = textbuf->search_forward(pos, find, &pos);
00348 
00349   if (found) {
00350     // Found a match; update the position and replace text...
00351     textbuf->select(pos, pos+strlen(find));
00352     textbuf->remove_selection();
00353     textbuf->insert(pos, replace);
00354     textbuf->select(pos, pos+strlen(replace));
00355     e->editor->insert_position(pos+strlen(replace));
00356     e->editor->show_insert_position();
00357   }
00358   else fl_alert("No occurrences of \'%s\' found!", find);
00359 }
00360 
00361 
00362 void replall_cb(Fl_Widget*, void* v) {
00363   EditorWindow* e = (EditorWindow*)v;
00364   const char *find = e->replace_find->value();
00365   const char *replace = e->replace_with->value();
00366 
00367   find = e->replace_find->value();
00368   if (find[0] == '\0') {
00369     // Search string is blank; get a new one...
00370     e->replace_dlg->show();
00371     return;
00372   }
00373 
00374   e->replace_dlg->hide();
00375 
00376   e->editor->insert_position(0);
00377   int times = 0;
00378 
00379   // Loop through the whole string
00380   for (int found = 1; found;) {
00381     int pos = e->editor->insert_position();
00382     found = textbuf->search_forward(pos, find, &pos);
00383 
00384     if (found) {
00385       // Found a match; update the position and replace text...
00386       textbuf->select(pos, pos+strlen(find));
00387       textbuf->remove_selection();
00388       textbuf->insert(pos, replace);
00389       e->editor->insert_position(pos+strlen(replace));
00390       e->editor->show_insert_position();
00391       times++;
00392     }
00393   }
00394 
00395   if (times) fl_message("Replaced %d occurrences.", times);
00396   else fl_alert("No occurrences of \'%s\' found!", find);
00397 }
00398 
00399 void replcan_cb(Fl_Widget*, void* v) {
00400   EditorWindow* e = (EditorWindow*)v;
00401   e->replace_dlg->hide();
00402 }
00403 
00404 void save_cb() {
00405   if (filename[0] == '\0') {
00406     // No filename - get one!
00407     saveas_cb();
00408     return;
00409   }
00410   else save_file(filename);
00411 }
00412 
00413 void saveas_cb() {
00414   char *newfile;
00415   if ( dataScore == 0 )
00416   {
00417   newfile = fl_file_chooser("Save File As?", "*.sco", filename); 
00418   if (newfile != NULL) save_file(newfile);
00419   }
00420   else if ( dataScore == 1 )
00421   {
00422   newfile = fl_file_chooser("Save File As?", "*.txt", filename); 
00423   if (newfile != NULL) save_file(newfile);
00424   }
00425   else if ( dataScore == 2 )
00426   {
00427   newfile = fl_file_chooser("Save File As?", "*.orc", filename); 
00428   if (newfile != NULL) save_file(newfile);
00429   }
00430 }
00431 
00432 
00433 Fl_Window* new_view();
00434 
00435 void view_cb(Fl_Widget*, void*) {
00436   Fl_Window* w = new_view();
00437   w->show();
00438 }
00439 
00440 Fl_Menu_Item menuitems[] = {
00441   { "&File",              0, 0, 0, FL_SUBMENU },
00442     { "&Insert File...",  FL_CTRL + 'i', (Fl_Callback *)insert_cb, 0, FL_MENU_DIVIDER },
00443     { "&Save File",       FL_CTRL + 's', (Fl_Callback *)save_cb },
00444     { "Save File &As...", FL_CTRL + FL_SHIFT + 's', (Fl_Callback *)saveas_cb, 0, FL_MENU_DIVIDER },
00445     { "Run Csound", FL_ALT + 'x', (Fl_Callback *)runCs_cb, 0, 0 },
00446     { "Play Wav file", FL_ALT + 'w', (Fl_Callback *)runAudioFile_cb, 0, 0 },
00447     { "&Close", FL_CTRL + 'q', (Fl_Callback *)close_cb, 0, FL_MENU_DIVIDER },
00448     { 0 },
00449 
00450   { "&Edit", 0, 0, 0, FL_SUBMENU },
00451     { "Cu&t",        FL_CTRL + 'x', (Fl_Callback *)cut_cb },
00452     { "&Copy",       FL_CTRL + 'c', (Fl_Callback *)copy_cb },
00453     { "&Paste",      FL_CTRL + 'v', (Fl_Callback *)paste_cb },
00454     { "&Delete",     0, (Fl_Callback *)delete_cb },
00455     { 0 },
00456 
00457   { "&Search", 0, 0, 0, FL_SUBMENU },
00458     { "&Find...",       FL_CTRL + 'f', (Fl_Callback *)find_cb },
00459     { "F&ind Again",    FL_CTRL + 'g', find2_cb },
00460     { "&Replace...",    FL_CTRL + 'r', replace_cb },
00461     { "Re&place Again", FL_CTRL + 't', replace2_cb },
00462     { 0 },
00463 
00464     {"&Csound",FL_ALT + 'c',0,0,FL_SUBMENU},
00465     {"Header",0,0,0,FL_SUBMENU},
00466     {"22052 HZ mono",0,insert_header22050mo_cb},
00467     {"22052 HZ stereo",0,insert_header22050ste_cb},
00468     {"44100 HZ mono",0,insert_header44100mo_cb},
00469     {"44100 HZ stereo",0,insert_header44100ste_cb},
00470     {"48000 HZ mono",0,insert_header48000mo_cb},
00471     {"48000 HZ stereo",0,insert_header48000ste_cb},
00472     {0},
00473     {"conversion",0,insert_conversions_cb},
00474     {"Csd tags",0,insert_cstags_cb},
00475     {"Ftables:",0,0,0,FL_SUBMENU},
00476     {"GEN 01",0,insert_gen01_cb},
00477     {"GEN 02",0,insert_gen02_cb},
00478     {"GEN 03",0,insert_gen03_cb},
00479     {"GEN 04",0,insert_gen04_cb},
00480     {"GEN 06",0,insert_gen06_cb},
00481     {"GEN 07",0,insert_gen07_cb},
00482     {"GEN 08",0,insert_gen08_cb},
00483     {"GEN 09",0,insert_gen09_cb},
00484     {"GEN 10",0,insert_gen10_cb},
00485     {"GEN 11",0,insert_gen11_cb},
00486     {"GEN 12",0,insert_gen12_cb},
00487     {"GEN 13",0,insert_gen13_cb},
00488     {"GEN 15",0,insert_gen15_cb},
00489     {"GEN 17",0,insert_gen17_cb},
00490     {"GEN 19",0,insert_gen19_cb},
00491     {"GEN 20",0,insert_gen20_cb},
00492     {"GEN 23",0,insert_gen23_cb},
00493     {"GEN 25",0,insert_gen25_cb},
00494     {"GEN 27",0,insert_gen27_cb},
00495     {"GEN 28",0,insert_gen28_cb},
00496     {0},
00497     {0},
00498   { 0 }
00499 };
00500 
00501 ////////////////////////////////////////////////
00502 
00503 Fl_Window* new_view() {
00504     
00505     EditorWindow* w = new EditorWindow(700, 400, title);
00506     w->begin();
00507     Fl_Menu_Bar* m = new Fl_Menu_Bar(0, 0, 700, 30);
00508     
00509     m->copy(menuitems, w);
00510     
00511     w->editor = new Fl_Text_Editor(0, 30, 700, 370);
00512     w->editor->buffer(textbuf);
00513     w->editor->textfont(FL_COURIER);
00514     w->resizable(w->editor);
00515     //w->set_modal();//LNL
00516     w->end();
00517     w->callback((Fl_Callback *)close_cb, w);
00518 
00519     textbuf->add_modify_callback(changed_cb, w);
00520     textbuf->call_modify_callbacks();
00521     num_windows++;
00522     return w;
00523 }
00524 
00525 void editor_win()
00526 {
00527   textbuf = new Fl_Text_Buffer;
00528   Fl_Window* w_editor = new_view();
00529   w_editor = new_view();
00530   w_editor->show();
00531 }
00532 
00533 //
00534 // End of "$Id: editor.cxx,v 1.2.2.3.2.18 2004/04/11 04:39:01 easysw Exp $".
00535 //MODIFIED BY LUIGI NEGRETTI LANNER UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AT 9 JUNE 05
00536 

Generated on Sun Jul 1 20:29:36 2012 for Csmusgen by  doxygen 1.4.7