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