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