Added upstream version.
[vlp.git] / edit / editor.cpp
1
2
3 #include <qapp.h>
4 #include <qmenubar.h>
5 #include <qpopmenu.h>
6 #include <qlabel.h>
7 #include <qlistbox.h>
8 #include <qfile.h>
9 #include <qfiledlg.h>
10 #include <qfontmet.h>
11 #include <qtooltip.h>
12 #include <qfont.h>
13 #include <qpixmap.h>
14 #include <qmsgbox.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <qscrbar.h>
19 #include <qpainter.h>
20 #include <qcolor.h>
21 #include <qpoint.h>
22 #include <qtstream.h>
23 #include <qchkbox.h>
24 #include <qkeycode.h>
25
26 #include "editor.h"
27
28 #define TYPENUM 5
29
30
31 Editor *e;
32
33
34 char *UnitTypes[5] = {"CLASS","PROCEDURE","FUNCTION","PROCESS","COROUTINE"};
35
36
37 My_Edit::My_Edit(QWidget *parent=0,const char *name=0)
38 : QMultiLineEdit(parent,name)
39 {
40 }
41
42 void My_Edit::keyPressEvent(QKeyEvent *ev)
43 {
44  QMultiLineEdit::keyPressEvent(ev);
45  emit cursorMove();
46 }
47
48
49 Editor::Editor( char *hdir,QWidget * parent , const char * name)
50     : QWidget( parent, name )
51 {
52     QFont f1("Helvetica",12,QFont::Bold);
53
54
55     strcpy(HomeDir,hdir);
56     strcpy(find_text,"");
57     sensitive=FALSE;
58     m = new QMenuBar( this, "menu" );
59     m->setFont(f1);
60     QPopupMenu * file = new QPopupMenu();
61     QPopupMenu * comp = new QPopupMenu();
62     QPopupMenu * loglan = new QPopupMenu();
63     QPopupMenu *medit = new QPopupMenu();
64
65     file->setFont(f1);
66     comp->setFont(f1);
67     loglan->setFont(f1);  
68     medit->setFont(f1);
69     CHECK_PTR( file );
70     CHECK_PTR( comp );
71     m->insertItem( "&File ", file );
72     m->insertItem( "&Edit", medit );
73     m->insertItem( "&Compile", this,SLOT(cmp()) );
74
75 //    m->insertItem( "&LOGLAN ", loglan );
76     m->insertItem( "&Properties", this, SLOT(props()));    
77
78
79
80
81     file->insertItem( "New ",   this, SLOT(create()), CTRL+Key_N);
82     file->insertItem( "Open ",  this, SLOT(load()), CTRL+Key_O);
83     file->insertItem( "Save ",  this, SLOT(save()),CTRL+Key_S);
84     file->insertItem( "Save as",  this, SLOT(save_as()),CTRL+Key_A);
85     file->insertSeparator();
86     file->insertItem( "Quit ", qApp,  SLOT(quit()));
87
88 //    comp->insertItem( "Compile ",   this, SLOT(cmp()), CTRL+Key_C);
89 //    comp->insertItem( "Gen ",  this, SLOT(gen()), CTRL+Key_G);
90 //    comp->insertItem( "Compile & Gen ",  this, SLOT(comp_all()));
91
92 //    loglan->insertItem( "Program structure",  this, SLOT(log_prog()));
93 //    loglan->insertItem( "Unit structure",   this, SLOT(log_unit()));
94
95
96     
97     e = new My_Edit( this, "editor" );
98     connect(e,SIGNAL(cursorMove()),this,SLOT(updateline()));
99     medit->insertItem("Copy ",e,SLOT(copyText()),CTRL+Key_Insert);
100     medit->insertItem("Paste ",e,SLOT(paste()), SHIFT+Key_Insert);
101     medit->insertItem("Cut ",e,SLOT(cut()), CTRL+Key_Delete);
102     medit->insertItem("Clear All ",e,SLOT(clear()));
103     medit->insertSeparator();
104     medit->insertItem("Find ",this,SLOT(findText()), CTRL+Key_F);
105     medit->insertItem("Find Next ",this,SLOT(find_next()),CTRL+Key_L);    
106
107     msg = new QMultiLineEdit( this, "messages" );
108     msg->setReadOnly(TRUE);
109     compiler_path.sprintf("%s/%s",HomeDir,"compile/logcomp");
110     gen_path.sprintf("%s/%s",HomeDir,"compile/gen");
111     file_path.sprintf("%s",HomeDir);
112
113     QFont f2("Times",14,QFont::Bold);
114     e->setFont(f1);
115     QColor col(200,200,200);
116     QColorGroup grp(black,col,col.light(),col.dark(),col.dark(),black,col);
117     
118     msg->setPalette(QPalette(grp,grp,grp));
119     position = new QLabel(this);
120     position->setFont(f2);
121     position->setFrameStyle(QFrame::NoFrame);
122     position->setAutoResize(TRUE);
123     resize(400,300);
124     
125 }
126
127
128 Editor::~Editor()
129 {
130    
131 }
132
133
134
135
136 void Editor::updateline()
137 {
138  char pom[255];
139  int cx,cy;
140  
141  e->getCursorPosition(&cx,&cy);
142  sprintf(pom," %d:%d ",cx,cy);
143  position->setText(pom);
144 }
145
146 void Editor::resizeEvent( QResizeEvent * )
147 {
148     if ( e && m )
149      {
150         e->setGeometry( 0, m->height(), width(), 3*(int)(( height() - m->height() )/4) );
151         msg->setGeometry( 0, m->height()+ e->height(), width(), (int)(( height() - m->height() )/4) );
152         position->setGeometry(width()-80,m->height()+ e->height()-10,
153                               position->width(),position->height());
154      }
155 }
156
157
158 void Editor::load()
159 {
160     QString fn = QFileDialog::getOpenFileName(file_path.data(),"*.log");
161     if ( !fn.isEmpty() ) 
162         load( fn );
163 }
164
165
166 void Editor::load( const char *fileName )
167 {
168    fname.sprintf("%s",fileName);
169
170
171     QFile f( fileName );
172     if ( !f.open( IO_ReadOnly ) )
173         return;
174
175     e->setAutoUpdate( FALSE );
176     e->clear();
177
178     QTextStream t(&f);
179     while ( !t.eof() ) {
180         QString s = t.readLine();
181         e->append( s );
182     }
183     f.close();
184
185     e->setAutoUpdate( TRUE );
186     e->repaint();
187     setCaption( fileName );
188 }
189
190
191 void Editor::save()
192 {
193   if (fname.isEmpty())
194   {
195   QString fn = QFileDialog::getSaveFileName(file_path.data(),"*.log");
196     if ( !fn.isEmpty() )
197        { 
198         fname.sprintf("%s",fn.data());
199         save( fn );
200        }
201   }
202    else
203     save(fname);
204  setCaption(fname);
205 }
206
207 void Editor::save_as()
208 {
209  QString fn = QFileDialog::getSaveFileName(file_path.data(),"*.log");
210     if ( !fn.isEmpty() )
211        { 
212         fname.sprintf("%s",fn.data());
213         save( fn );
214        }
215  setCaption(fname);
216 }
217
218 void Editor::save(const char *fileName)
219 {
220
221   QFile f(fileName);
222   if (!f.open(IO_WriteOnly)) return;
223   f.reset();
224   f.writeBlock( e->text().data(),e->text().size() );
225   f.close();
226   
227    
228 }
229
230
231 void Editor::create()
232 {
233  e->clear();
234  fname.sprintf("%s","");   
235 }
236
237
238 void Editor::print()
239 {
240     
241 }
242
243
244
245 void Editor::cmp()
246 {
247  save();
248  compile(COMP_MODE);
249 }
250
251 void Editor::gen()
252 {
253  compile(GEN_MODE);
254 }
255
256 void Editor::comp_all()
257 {
258  save();
259  compile(ALL_MODE);
260 }
261
262
263 void Editor::compile(int mode)
264 {
265  char cmd[255];
266
267     msg->setAutoUpdate( FALSE );
268     msg->setReadOnly(FALSE);
269     msg->clear();
270     msg->repaint(); 
271
272
273  //i = fname.find('.');
274 // if (i>=0)
275  {
276   QString fn = fname.data();
277 //  fn.truncate(i);
278
279  switch(mode)
280  {
281
282  case COMP_MODE:
283             sprintf(cmd,"%s %s > comp_data!",compiler_path.data(),fname.data());
284             break;
285  case GEN_MODE:
286             sprintf(cmd,"%s %s > comp_data!",gen_path.data(),fn.data());
287             break;
288  case ALL_MODE:
289             sprintf(cmd,"%s %s > comp_data!",compiler_path.data(),fn.data());
290             system(cmd);    
291             sprintf(cmd,"%s %s >> comp_data!",gen_path.data(),fn.data());
292             break;
293
294  } /*switch */
295
296   system(cmd);
297   QFile f( "comp_data!" );
298     if ( !f.open( IO_ReadOnly ) )
299         return;
300
301
302     QTextStream t(&f);
303     while ( !t.eof() ) {
304         QString s = t.readLine();
305         msg->append( s );
306     }
307     f.close();
308     msg->setReadOnly(TRUE);
309     msg->setAutoUpdate( TRUE );
310     msg->repaint(); 
311     unlink("comp_data!");
312  }
313 }
314
315
316
317 void Editor::props()
318 {
319  QDialog dlg(this,"Properties",TRUE);
320
321
322         QLineEdit* files;
323         files = new QLineEdit( &dlg, "f_path" );
324         files->setGeometry( 130, 20, 250, 30 );
325         files->setText( file_path.data() );
326         files->setMaxLength( 32767 );
327         files->setEchoMode( QLineEdit::Normal );
328         files->setFrame( TRUE );
329
330         QLabel* tmpQLabel;
331         tmpQLabel = new QLabel( &dlg, "Label_1" );
332         tmpQLabel->setGeometry( 10, 20, 100, 30 );
333         tmpQLabel->setText( "Path to files:" );
334         tmpQLabel->setAlignment( 289 );
335         tmpQLabel->setMargin( -1 );
336
337         tmpQLabel = new QLabel( &dlg, "Label_2" );
338         tmpQLabel->setGeometry( 10, 60, 100, 30 );
339         tmpQLabel->setText( "Path to compiler:" );
340         tmpQLabel->setAlignment( 289 );
341         tmpQLabel->setMargin( -1 );
342
343 /*      tmpQLabel = new QLabel( &dlg, "Label_3" );
344         tmpQLabel->setGeometry( 10, 100, 100, 30 );
345         tmpQLabel->setText( "Path to gen:" );
346         tmpQLabel->setAlignment( 289 );
347         tmpQLabel->setMargin( -1 );*/
348
349         QLineEdit* compp;
350         compp = new QLineEdit( &dlg, "l_path" );
351         compp->setGeometry( 130, 60, 250, 30 );
352         compp->setText( compiler_path.data() );
353         compp->setMaxLength( 32767 );
354         compp->setEchoMode( QLineEdit::Normal );
355         compp->setFrame( TRUE );
356
357 /*      QLineEdit* genp;
358         genp = new QLineEdit( &dlg, "g_path" );
359         genp->setGeometry( 130, 100, 250, 30 );
360         genp->setText( gen_path.data() );
361         genp->setMaxLength( 32767 );
362         genp->setEchoMode( QLineEdit::Normal );
363         genp->setFrame( TRUE );*/
364
365
366         QPushButton* tmpQPushButton;
367         tmpQPushButton = new QPushButton( &dlg, "OkBtn" );
368         tmpQPushButton->setGeometry( 90, 100, 70, 30 );
369         tmpQPushButton->setText( "Ok" );
370         tmpQPushButton->setAutoRepeat( FALSE );
371         tmpQPushButton->setAutoResize( FALSE );
372         connect(tmpQPushButton,SIGNAL(clicked()),&dlg,SLOT(accept()));
373
374         tmpQPushButton = new QPushButton( &dlg, "CancelBtn" );
375         tmpQPushButton->setGeometry( 180, 100, 70, 30 );
376         tmpQPushButton->setText( "Cancel" );
377         tmpQPushButton->setAutoRepeat( FALSE );
378         tmpQPushButton->setAutoResize( FALSE );
379         connect(tmpQPushButton,SIGNAL(clicked()),&dlg,SLOT(reject()));
380         dlg.resize(400,140);
381
382    if (dlg.exec())
383    {
384     compiler_path.sprintf("%s",compp->text());
385 //    gen_path.sprintf("%s",genp->text());
386     file_path.sprintf("%s",files->text());
387     };
388  }
389
390 /* --------------------------------------- */
391
392
393
394
395
396 void Editor::log_unit()
397 {
398  QString txt;
399  QDialog dlg(this,"unit",TRUE);
400  int cx,cy,i;
401  char uname[255];
402
403
404  QLineEdit* files;
405         files = new QLineEdit( &dlg, "f_path" );
406         files->setGeometry( 130, 20, 250, 30 );
407         files->setText("" );
408         files->setMaxLength( 32767 );
409         files->setEchoMode( QLineEdit::Normal );
410         files->setFrame( TRUE );
411
412         QLabel* tmpQLabel;
413         tmpQLabel = new QLabel( &dlg, "Label_1" );
414         tmpQLabel->setGeometry( 10, 20, 100, 30 );
415         tmpQLabel->setText( "Unit name:" );
416         tmpQLabel->setAlignment( 289 );
417         tmpQLabel->setMargin( -1 );
418
419         QPushButton* tmpQPushButton;
420         tmpQPushButton = new QPushButton( &dlg, "OkBtn" );
421         tmpQPushButton->setGeometry( 40, 170, 70, 30 );
422         tmpQPushButton->setText( "Ok" );
423         tmpQPushButton->setAutoRepeat( FALSE );
424         tmpQPushButton->setAutoResize( FALSE );
425         connect(tmpQPushButton,SIGNAL(clicked()),&dlg,SLOT(accept()));
426
427         tmpQPushButton = new QPushButton( &dlg, "CancelBtn" );
428         tmpQPushButton->setGeometry( 130, 170, 100, 30 );
429         tmpQPushButton->setText( "Cancel" );
430         tmpQPushButton->setAutoRepeat( FALSE );
431         tmpQPushButton->setAutoResize( FALSE );
432         connect(tmpQPushButton,SIGNAL(clicked()),&dlg,SLOT(reject()));
433
434         tmpQLabel = new QLabel( &dlg, "Label_1" );
435         tmpQLabel->setGeometry( 10, 50, 100, 60 );
436         tmpQLabel->setText( "Unit type:" );
437
438
439   QListBox lst(&dlg,"type");
440   for(i=0;i<TYPENUM;i++) lst.insertItem(UnitTypes[i]); 
441   lst.setGeometry(130,60,180,80);
442   lst.setCurrentItem(0);
443
444  if (dlg.exec())
445  {
446   strcpy(uname,files->text());
447   e->getCursorPosition(&cx,&cy);
448   
449   txt.sprintf("UNIT %s : %s( <params> );\nBEGIN\n\nEND %s;",uname,
450               lst.text(lst.currentItem()), uname);
451   e->insertAt(txt,cx,cy);
452  };
453 }
454
455
456 void Editor::log_prog()
457 {
458  QString txt;
459  QDialog dlg(this,"unit",TRUE);
460  int cx,cy;
461  char uname[255];
462
463
464  QLineEdit* files;
465         files = new QLineEdit( &dlg, "f_path" );
466         files->setGeometry( 130, 20, 250, 30 );
467         files->setText("" );
468         files->setMaxLength( 32767 );
469         files->setEchoMode( QLineEdit::Normal );
470         files->setFrame( TRUE );
471
472         QLabel* tmpQLabel;
473         tmpQLabel = new QLabel( &dlg, "Label_1" );
474         tmpQLabel->setGeometry( 10, 20, 100, 30 );
475         tmpQLabel->setText( "Program name:" );
476
477         QPushButton* tmpQPushButton;
478         tmpQPushButton = new QPushButton( &dlg, "OkBtn" );
479         tmpQPushButton->setGeometry( 40, 70, 70, 30 );
480         tmpQPushButton->setText( "Ok" );
481         tmpQPushButton->setAutoRepeat( FALSE );
482         tmpQPushButton->setAutoResize( FALSE );
483         connect(tmpQPushButton,SIGNAL(clicked()),&dlg,SLOT(accept()));
484
485         tmpQPushButton = new QPushButton( &dlg, "CancelBtn" );
486         tmpQPushButton->setGeometry( 130, 70, 100, 30 );
487         tmpQPushButton->setText( "Cancel" );
488         tmpQPushButton->setAutoRepeat( FALSE );
489         tmpQPushButton->setAutoResize( FALSE );
490         connect(tmpQPushButton,SIGNAL(clicked()),&dlg,SLOT(reject()));
491
492
493  if (dlg.exec())
494  {
495   strcpy(uname,files->text());
496   e->getCursorPosition(&cx,&cy);
497   
498   txt.sprintf("PROGRAM %s\n\nBEGIN\n\nEND ",uname);
499   e->insertAt(txt,cx,cy);
500  };
501
502 }
503
504
505 void Editor::findText()
506 {
507  QDialog dlg(this,"",TRUE);
508  QString *txt;
509  int res,line,pom;
510
511
512         QLineEdit* tmpQLineEdit;
513         tmpQLineEdit = new QLineEdit( &dlg, "LineEdit_1" );
514         tmpQLineEdit->setGeometry( 60, 10, 180, 30 );
515         tmpQLineEdit->setText( "" );
516
517         QLabel* tmpQLabel;
518         tmpQLabel = new QLabel( &dlg, "Label_1" );
519         tmpQLabel->setGeometry( 10, 10, 50, 30 );
520         {
521                 QFont font( "helvetica", 12, 75, 0 );
522                 font.setStyleHint( (QFont::StyleHint)0 );
523                 font.setCharSet( (QFont::CharSet)0 );
524                 tmpQLabel->setFont( font );
525         }
526         tmpQLabel->setText( "Text:" );
527
528         QCheckBox* tmpQRadioButton;
529         tmpQRadioButton = new QCheckBox( &dlg, "RadioButton_1" );
530         tmpQRadioButton->setGeometry( 70, 50, 150, 30 );
531         tmpQRadioButton->setText( "Case sensitive" );
532         tmpQRadioButton->setAutoRepeat( FALSE );
533         tmpQRadioButton->setAutoResize( FALSE );
534
535         QPushButton *okbtn, *cbtn;
536         okbtn = new QPushButton( &dlg, "PushButton_1" );
537         okbtn->setGeometry( 260, 10, 100, 30 );
538         okbtn->setText( "Find" );
539         okbtn->setDefault(TRUE);
540         connect(okbtn,SIGNAL(clicked()),&dlg,SLOT(accept()));
541
542         cbtn = new QPushButton( &dlg, "PushButton_2" );
543         cbtn->setGeometry( 260, 50, 100, 30 );
544         cbtn->setText( "Close" );
545         connect(cbtn,SIGNAL(clicked()),&dlg,SLOT(reject()));
546         dlg.resize( 380, 90 );
547
548     if (dlg.exec())
549     {
550      e->getCursorPosition(&pom,&res);
551      sensitive=tmpQRadioButton->isChecked();
552      for(line=pom+1;line<e->numLines();line++)
553      {
554       txt = new QString(e->textLine(line)); 
555       if (tmpQRadioButton->isChecked())
556        res=txt->find(tmpQLineEdit->text(),0,TRUE);
557       else
558        res=txt->find(tmpQLineEdit->text(),0,FALSE);
559       delete txt;
560       if (res>=0) { e->setCursorPosition(line,1);strcpy(find_text,tmpQLineEdit->text());
561                     break;}
562      } //for
563      }  
564
565 }
566
567 void Editor::find_next()
568 {
569  int pom,res,line;
570  QString *txt;
571
572     e->getCursorPosition(&pom,&res);
573     for(line=pom+1;line<e->numLines();line++)
574      {
575       txt = new QString(e->textLine(line)); 
576       if (sensitive)
577        res=txt->find(find_text,0,TRUE);
578       else
579        res=txt->find(find_text,0,FALSE);
580       delete txt; 
581       if (res>=0) { e->setCursorPosition(line,1);
582                     break;}
583      } //for
584  
585 }
586
587 int main( int argc, char **argv )
588 {
589     QApplication a( argc, argv );
590     a.setStyle(WindowsStyle);
591     e = new Editor(argv[1]);
592     e->resize( 600, 400 );
593     e->show();
594     return a.exec();
595 }
596