vlp-10 Using coding style in loggraph.
[vlp.git] / src / graph / loggraph.cpp
1 /*
2 //
3 // Qt Example Application: drawdemo
4 //
5 // Demonstrates the painter and the printer.
6 //
7
8 //#include <qwindow.h>
9 */
10 #include <qpainter.h>
11 #include <qprinter.h>
12 #include <qpushbt.h>
13 #include <qradiobt.h>
14 #include <qbttngrp.h>
15 #include <qapp.h>
16 #include <math.h>
17 #include <qpixmap.h>
18 #include <qscrbar.h>
19 #include <qcolor.h>
20 #include <stdio.h>
21 #include <qfont.h>
22 #include <qfontmet.h>
23 #include <qlist.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <netinet/in.h>
27 #include "genint1.h"
28 #include "comm.h"
29 #include "socu.h"
30 #include <unistd.h>
31 #include <qsocknot.h>
32 #include <qobject.h>
33 #include <errno.h>
34 #include <qqueue.h>
35 #include <qmsgbox.h>
36 #include <qmenubar.h>
37 #include <qpopmenu.h>
38 #include <qkeycode.h>
39
40 #define MAXWIDTH        640
41 #define MAXHEIGHT       480
42 #define TEXT_LINES      50
43 #define TEXT_COLS       100
44 #define REQUEST_NAME    "gr.req"
45 #define PERM            0666
46
47 class VGRMap {
48 public:
49         QPixmap *map;
50         int number;
51
52         VGRMap(int n, QPixmap *m) {
53                 number = n;
54                 map = m;
55         };
56 };
57
58 class VGR : public QFrame {
59         Q_OBJECT
60 public:
61         VGR(char*);
62         ~VGR();
63
64         int resp_sock;
65         int fcol;
66         int bcol;
67         int gfcol;
68         int gbcol;
69         int curx;
70         int cury;
71         int tx;
72         int ty;
73         QQueue<int> CharBuffer;
74
75         bool string_wait;
76         bool char_wait;
77         bool line_wait;
78         bool mouse_wait;
79         bool inkey_wait;
80         bool was_line;
81         QColor *lcolors[256];
82         QFont *prv_font;
83         QFont *italic_font;
84         QFont *bold_font;
85         QFont *normal_font;
86
87         void MakeColors();
88
89         void SetForeground(int);
90         void SetBackground(int);
91         void ClearAll();
92         void ClearArea(int, int, int, int);
93
94         void Line(int, int, int, int);
95         void Ellipse(int x, int y, int a, int b, int alfa, int beta, int fill);
96         void Rect(int x1, int y1, int x2, int y2, int col, int fill);
97         void Point(int x, int y);
98         void TextXY(int x, int y, char *s);
99         void CharXY(int x, int y, char a);
100         void IntXY(int x, int y, int val);
101
102         void WriteText(char *s);
103         void WriteChar(char a);
104         void WriteLine();
105         
106         /* Write Char w/o changing position */
107         void PutChar(char a);
108         void DelChar();
109
110         void Outstring(int x, int y, char *s, int b, int f);
111         void writeintxy(int x, int y, int val, int c);
112         void CurPos();
113
114         int Getmap(int w, int h);
115         void Putmap(int map); 
116
117         void MagicGraph(G_MESSAGE*);
118         bool GetInput(int);
119
120 public slots:
121         void vscrolled(int);
122         void hscrolled(int);
123         void CloseMe();
124         void RequestMessage();
125
126 protected:
127         virtual void resizeEvent(QResizeEvent *);
128         virtual void closeEvent(QCloseEvent *);
129         virtual void keyPressEvent(QKeyEvent *);
130         virtual void mousePressEvent(QMouseEvent *);
131         virtual void paintEvent(QPaintEvent *);
132         virtual void focusInEvent(QFocusEvent *);
133
134 private:
135         QPixmap *canvas;
136         QScrollBar *hscroll;
137         QScrollBar *vscroll;
138         QPushButton *close_btn;
139         int ox,oy,lstep,pstep,gx,gy;
140         QList<VGRMap> maps;
141         int firstfreemap;
142         bool Closed;
143         bool MustRepaint;
144         bool GraphRead;
145         QSocketNotifier *request;
146         char internal_buffer[256];
147         int strcnt;
148 };
149
150 void VGR::MakeColors()
151 {
152         /* black  */
153         lcolors[0] = new QColor(0, 0, 0);
154         /* blue dark */
155         lcolors[1] = new QColor(0, 0, 139);
156         /* green dark  */
157         lcolors[2] = new QColor(0, 100, 0);
158         /* turquoise dark   */
159         lcolors[3] = new QColor(0, 197, 205);
160         /* red dark */
161         lcolors[4] = new QColor(205, 0, 0);
162         /* violet */
163         lcolors[5] = new QColor(238, 130, 238);
164         /* brown */
165         lcolors[6] = new QColor(139,35, 35);
166         /* grey light */
167         lcolors[7] = new QColor(190, 190, 190);
168         /* grey dark */
169         lcolors[8] = new QColor(97, 97, 97);
170         /* blue */
171         lcolors[9] = new QColor(0, 0, 255);
172         /* green */
173         lcolors[10] = new QColor(0, 255, 0);
174         /* turquoise */
175         lcolors[11] = new QColor(0, 229, 238);
176         /* red light */
177         lcolors[12] = new QColor(255, 0, 0);
178         /* rose */
179         lcolors[13] = new QColor(255, 110, 180);
180         /* yellow */
181         lcolors[14] = new QColor(255, 255, 0);
182         /* white */
183         lcolors[15] = new QColor(255, 255, 255);
184 }
185
186 /*
187  * Construct the DrawView with buttons.
188  */
189 VGR::VGR(char *sockname)
190         :QFrame()
191 {
192         QPixmap mp;
193         struct sockaddr_un svr;
194         int len, i, optval, on;
195
196         normal_font = new QFont("lucidatypewriter", 10, QFont::Normal);
197         normal_font->setFixedPitch(TRUE);
198         bold_font = new QFont("lucidatypewriter", 10, QFont::Bold);
199         bold_font->setFixedPitch(TRUE);
200         italic_font = new QFont("lucidatypewriter", 10, QFont::Normal,TRUE);
201         italic_font->setFixedPitch(TRUE);
202
203         prv_font = normal_font;
204
205         MakeColors();
206         setCaption("graphic resource");
207         setBackgroundColor(white);
208         canvas = new QPixmap(640, 480);
209         canvas->fill(backgroundColor());
210
211         ox = oy = 0;
212         curx = cury = 0;
213         tx = ty = 0;
214         gx = gy = 0;
215         
216         maps.setAutoDelete(TRUE);
217         firstfreemap = 1;
218         Closed = FALSE;
219         GraphRead = FALSE;
220         lstep = 10;
221         pstep = 250;
222         hscroll = new QScrollBar(0, MAXWIDTH, lstep, pstep, 0, QScrollBar::Horizontal, this);
223         vscroll = new QScrollBar(0, MAXHEIGHT, lstep, pstep, 0, QScrollBar::Vertical, this);
224         hscroll->setTracking(TRUE);
225         vscroll->setTracking(TRUE);
226         resize(640, 300);
227
228         hscroll->setGeometry(0, height() - 16, width() - 16, 16);
229         vscroll->setGeometry(width() - 16, 0, 16, height() - 16);
230
231         connect(hscroll, SIGNAL(valueChanged(int)), this, SLOT(hscrolled(int)));
232         connect(vscroll, SIGNAL(valueChanged(int)), this, SLOT(vscrolled(int)));
233
234         close_btn = new QPushButton(this, "close");
235
236         if (mp.load("pics/close.bmp"))
237                 close_btn->setPixmap(mp);
238         else
239                 close_btn->setText("C");
240         
241         close_btn->setGeometry(width()-16,height()-16,16,16);
242         close_btn->setEnabled(FALSE);
243         connect(close_btn, SIGNAL(clicked()), this, SLOT(CloseMe()));
244
245
246         gfcol = fcol = 0;
247         gbcol = bcol = 15;
248         resp_sock = socket(AF_UNIX, SOCK_STREAM, 0);
249         bzero(&svr, sizeof(svr));
250         svr.sun_family = AF_UNIX;
251         strcpy(svr.sun_path, sockname);
252         len = strlen(svr.sun_path) + sizeof(svr.sun_family);
253         i = ::connect(resp_sock, (struct sockaddr*)&svr, len);
254         if (i == 0)
255                 fcntl(resp_sock, F_SETFL, O_NONBLOCK | 
256                                                 fcntl(resp_sock , F_GETFL, 0));
257         on = 1;
258         setsockopt(resp_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on));
259         request = new QSocketNotifier(resp_sock, QSocketNotifier::Read);
260         connect(request, SIGNAL(activated(int)), this, SLOT(RequestMessage()));   
261         optval=TCP_BUFFER_SIZE;
262         /*
263         setsockopt(resp_sock,SOL_SOCKET,SO_SNDBUF,&optval,sizeof(optval));
264         setsockopt(resp_sock,SOL_SOCKET,SO_RCVBUF,&optval,sizeof(optval));
265         */
266
267         inkey_wait = string_wait = FALSE;
268         char_wait = line_wait = mouse_wait = FALSE;
269         was_line = FALSE;
270         CharBuffer.clear(); 
271         CharBuffer.setAutoDelete(TRUE);
272
273         setFocusPolicy(QWidget::StrongFocus);
274         hscroll->setRange(0, MAXWIDTH - width() + 20);
275         vscroll->setRange(0, MAXHEIGHT - height() + 20);
276         hscroll->setSteps((int)(hscroll->width() / hscroll->maxValue()),
277                         (int)((hscroll->width() / hscroll->maxValue()) * 4));
278         vscroll->setSteps((int)(vscroll->height() / vscroll->maxValue()),
279                         (int)((vscroll->height() / vscroll->maxValue()) * 4));
280
281         setMaximumSize(MAXWIDTH + 16, MAXHEIGHT + 16);
282         setUpdatesEnabled(FALSE);
283 };
284
285 VGR::~VGR()
286 {
287         if (request != NULL)
288                 delete request;
289 }
290
291 void VGR::paintEvent(QPaintEvent *p)
292 {
293         /*if (isUpdatesEnabled()) {*/
294         bitBlt(this, 0, 0, canvas, ox, oy, width() - 16, height() - 16);
295         /*}*/
296         MustRepaint = FALSE;
297 }
298
299 void VGR::focusInEvent(QFocusEvent *)
300 {
301         setUpdatesEnabled(TRUE);
302         repaint();
303         setUpdatesEnabled(FALSE);
304 }
305
306
307 void VGR::resizeEvent( QResizeEvent *ev )
308 {
309         hscroll->setGeometry(0, height() - 16, width() - 16, 16);
310         vscroll->setGeometry(width() - 16, 0, 16, height() - 16);
311         close_btn->setGeometry(width() - 16, height() - 16, 16, 16);
312         hscroll->setRange(0, MAXWIDTH - width() + 20);
313         vscroll->setRange(0, MAXHEIGHT - height() + 20); 
314         pstep = height() - 32;
315         hscroll->setSteps(lstep, pstep);
316         vscroll->setSteps(lstep, pstep);
317 }
318
319 void VGR::closeEvent(QCloseEvent *ev)
320 {
321         if (Closed)
322                 QFrame::closeEvent(ev);
323 }
324
325 void VGR::mousePressEvent(QMouseEvent *ev)
326 {
327         G_MESSAGE msg;
328
329         if (mouse_wait) {
330                 msg.msg_type = MSG_GRAPH;
331                 msg.param.pword[0] = GRAPH_MGETPRESS_RESPONSE;
332                 msg.param.pword[2] = ev->pos().x() - ox;
333                 msg.param.pword[3] = ev->pos().y() - oy;
334                 switch(ev->button()) {
335                 case LeftButton:
336                         msg.param.pword[7] = 1;
337                         break;
338                 case RightButton:
339                         msg.param.pword[7] = 3;
340                         break;
341                 case MidButton:
342                         msg.param.pword[7] = 2;
343                         break;
344                 default:
345                         msg.param.pword[7] = 0;
346                         break;
347                 }
348                 write(resp_sock, &msg, sizeof(G_MESSAGE));
349
350                 mouse_wait = FALSE;
351         }
352         QFrame::mousePressEvent(ev);
353 }
354
355
356 bool VGR::GetInput(int t)
357 {
358         G_MESSAGE msg;
359
360         if (!CharBuffer.isEmpty()) { 
361                 switch(t) {
362                 case 2: {
363                         msg.msg_type = MSG_GRAPH;
364                         msg.param.pword[0] = GRAPH_INKEY_RESPONSE;
365                         msg.param.pword[3] = *(CharBuffer.dequeue());
366                         write(resp_sock,&msg,sizeof(G_MESSAGE));
367                         inkey_wait = FALSE;
368                         return TRUE;
369                 };
370                 break;
371
372                 case 0:
373                         if (GraphRead) {
374                                 msg.msg_type = MSG_GRAPH;
375                                 msg.param.pword[0] = GRAPH_MAGIC_RESPONSE;
376                                 msg.param.pword[1] = 0;
377                                 msg.param.pchar = 
378                                                 (char)(*(CharBuffer.dequeue())); 
379                                 WriteChar(msg.param.pchar);
380                                 GraphRead = FALSE;
381                         } else {
382                                 msg.msg_type = MSG_GRAPH;
383                                 msg.param.pword[0] = GRAPH_READCHAR_RESPONSE;
384                                 msg.param.pchar = 
385                                                 (char)(*(CharBuffer.dequeue()));
386                         }
387
388                         write(resp_sock,&msg,sizeof(G_MESSAGE));
389                         char_wait = FALSE;
390                         return TRUE;
391
392                 case 1:
393                         while ( ((!CharBuffer.isEmpty()) &&
394                                         (!(*(CharBuffer.dequeue()) = 13))));
395                 
396                         if (!CharBuffer.isEmpty()) {
397                                 msg.msg_type = MSG_GRAPH;
398                                 msg.param.pword[0] = GRAPH_READLN_RESPONSE;
399                                 write(resp_sock, &msg, sizeof(G_MESSAGE));
400                                 line_wait = FALSE;
401                                 return TRUE;
402                         }
403                         /* line_wait */
404                         break;
405                 } /* switch */
406         } /* buffer empty */
407
408         if (CharBuffer.isEmpty() && (t == 2)) {
409                 msg.msg_type = MSG_GRAPH;
410                 msg.param.pword[0] = GRAPH_INKEY_RESPONSE;
411                 msg.param.pword[3] = 0;
412                 write(resp_sock, &msg, sizeof(G_MESSAGE));
413                 inkey_wait = FALSE;
414                 return TRUE;
415         }
416
417         return FALSE;   
418 }
419
420 void VGR::keyPressEvent(QKeyEvent *ev)
421 {
422         G_MESSAGE msg;
423         int *pom;
424
425         pom = new int[1];
426         *pom=ev->ascii();
427         if (*pom==0) {
428                 switch(ev->key()) {
429                 case Key_F1:
430                         *pom = -10;
431                         break;
432                 case Key_F2:
433                         *pom = -11;
434                         break;
435                 case Key_F3:
436                         *pom = -12;
437                         break;
438                 case Key_F4:
439                         *pom = -13;
440                         break;
441                 case Key_F5:
442                         *pom = -14;
443                         break;
444                 case Key_F6:
445                         *pom = -15;
446                         break;
447                 case Key_F7:
448                         *pom = -16;
449                         break;
450                 case Key_F8:
451                         *pom = -17;
452                         break;
453                 case Key_F9:
454                         *pom = -18;
455                         break;
456                 case Key_F10:
457                         *pom = -19;
458                         break;
459                 case Key_Insert:
460                         *pom = -20;
461                         break;
462                 case Key_Home:
463                         *pom = -21;
464                         break;
465                 case Key_End:
466                         *pom = -22;
467                         break;
468                 case Key_PageUp:
469                         *pom = -23;
470                         break;
471                 case Key_PageDown:
472                         *pom = -24;
473                         break;
474                 case Key_Left:
475                         *pom = -25;
476                         break;
477                 case Key_Right:
478                         *pom = -26;
479                         break;
480                 case Key_Up:
481                         *pom = -27;
482                         break;
483                 case Key_Down:
484                         *pom = -28;
485                         break;
486                 }/* switch */
487         }
488         if ((!string_wait) && (!inkey_wait) && (!char_wait) && (!line_wait))
489                 CharBuffer.enqueue(pom);
490
491
492         if (inkey_wait) {
493                 msg.msg_type = MSG_GRAPH;
494                 msg.param.pword[0] = GRAPH_INKEY_RESPONSE;
495                 msg.param.pword[3] = *pom;
496                 write(resp_sock, &msg, sizeof(G_MESSAGE));
497                 inkey_wait = FALSE;
498         };
499
500         if (char_wait) {
501                 if (GraphRead) {
502                         msg.msg_type = MSG_GRAPH;
503                         msg.param.pword[0] = GRAPH_MAGIC_RESPONSE;
504                         msg.param.pword[1] = 0;
505                         WriteChar((char)ev->ascii());
506                         GraphRead = FALSE;
507                 } else {
508                         msg.msg_type = MSG_GRAPH;
509                         msg.param.pword[0] = GRAPH_READCHAR_RESPONSE;
510                 }
511                 msg.param.pchar = (char)ev->ascii();
512                 write(resp_sock, &msg, sizeof(G_MESSAGE));
513                 char_wait = FALSE;
514         }
515
516         if ((line_wait) && (ev->ascii() == 13)) {
517                 msg.msg_type = MSG_GRAPH;
518                 msg.param.pword[0] = GRAPH_READLN_RESPONSE;
519                 write(resp_sock, &msg, sizeof(G_MESSAGE));
520                 line_wait = FALSE;
521         }
522
523         if (string_wait) {
524                 if (ev->ascii() == 13) {
525                         internal_buffer[strcnt] = '\0';
526                         DelChar();
527                         if (GraphRead) {
528                                 GraphRead = FALSE;
529                                 msg.msg_type = MSG_GRAPH;
530                                 msg.param.pword[0] = GRAPH_MAGIC_RESPONSE;
531                                 msg.param.pword[1] = 0;
532                         } else {
533                                 msg.msg_type = MSG_GRAPH;
534                                 msg.param.pword[0] = GRAPH_READSTR_RESPONSE;
535                         }
536                         strcpy(msg.param.pstr, internal_buffer);
537                         write(resp_sock, &msg, sizeof(G_MESSAGE));   
538                         string_wait = FALSE;
539                         was_line = TRUE;
540                         CharBuffer.clear();
541                 }
542                 else if (ev->ascii() == 8) {
543                         strcnt--;
544                         DelChar();
545                         DelChar();
546                         WriteChar('_');
547                 } else {
548                         internal_buffer[strcnt] = (char)ev->ascii();
549                         strcnt++;
550                         DelChar();
551                         WriteChar((char)ev->ascii());
552                         WriteChar('_');
553                 }
554         }
555 }
556
557
558 void VGR::CloseMe()
559 {
560         qApp->quit();
561 }
562
563
564
565 void VGR::hscrolled(int val)
566 {
567         ox = val;
568         setUpdatesEnabled(TRUE);
569         repaint();
570         setUpdatesEnabled(FALSE);
571 }
572
573 void VGR::vscrolled(int val)
574 {
575         oy = val;
576         setUpdatesEnabled(TRUE);
577         repaint();
578         setUpdatesEnabled(FALSE);
579 }
580
581
582 /****************************************/
583
584 void VGR::SetForeground(int col)
585 {
586         fcol = col;
587 }
588
589 void VGR::SetBackground(int col)
590 {
591         bcol = col;
592 }
593
594 void VGR::Line(int x1, int y1, int x2, int y2)
595 {
596         QPainter p;
597         p.begin(canvas);
598         p.setPen(*lcolors[fcol]);
599         p.drawLine(x1, y1, x2, y2);
600         p.end();
601
602         p.begin(this);
603         p.setPen(*lcolors[fcol]);
604         p.drawLine(x1 - ox, y1 - oy, x2 - ox, y2 - oy);
605         p.end();
606 }
607
608 void VGR::Ellipse(int x, int y, int a, int b, int alfa, int beta, int fill)
609 {
610         QPainter p;
611
612         p.begin(canvas);
613         p.setPen(*lcolors[fcol]);
614         p.setBrush(*lcolors[fcol]);
615         if (fill > 0)
616                 p.drawPie(x, y, a, b, alfa * 16, (beta - alfa) * 16);
617         else
618                 p.drawArc(x, y, a, b, alfa * 16, (beta - alfa) * 16); 
619
620         p.end();
621
622         p.begin(this);
623         p.setPen(*lcolors[fcol]);
624         p.setBrush(*lcolors[fcol]);
625         if (fill > 0)
626                 p.drawPie(x - ox, y - oy, a, b, alfa * 16, (beta - alfa) * 16);
627         else
628                 p.drawArc(x - ox, y - oy, a, b, alfa * 16, (beta - alfa) * 16); 
629         
630         p.end();
631 }
632
633 void VGR::Rect(int x1, int y1, int x2, int y2,int col, int fill)
634 {
635         QPainter p;
636         QBrush b(*lcolors[col]);
637         p.begin(canvas);
638         p.setPen(*lcolors[col]);
639         if (fill > 0)
640                 p.fillRect(x1 , y1 , x2 - x1, y2 - y1, b);
641         else
642                 p.drawRect(x1,y1,x2-x1,y2-y1);
643         
644         p.end();
645
646         p.begin(this);
647         p.setPen(*lcolors[col]);
648         if (fill > 0)
649                 p.fillRect(x1 - ox, y1 - oy, x2 - x1, y2 - y1, b);
650         else
651                 p.drawRect(x1 - ox, y1 - oy, x2 - x1, y2 - y1);
652         p.end();
653 }
654
655 void VGR::TextXY(int x, int y, char *s)
656 {
657         QPainter p;
658         QFontMetrics fm(*prv_font);
659
660         p.begin(canvas);
661         p.setPen(*lcolors[fcol]);
662         p.setBackgroundColor(*lcolors[bcol]);
663         p.setBackgroundMode(OpaqueMode);
664         p.setFont(*prv_font);
665         p.drawText(x, y + fm.height(), s, strlen(s));
666         p.end();
667
668
669         p.begin(this);
670         p.setPen(*lcolors[fcol]);
671         p.setBackgroundColor(*lcolors[bcol]);
672         p.setBackgroundMode(OpaqueMode);
673         p.setFont(*prv_font);
674         p.drawText(x - ox, y - oy + fm.height(), s, strlen(s));
675         p.end();
676 }
677
678
679 void VGR::CharXY(int x, int y, char a)
680 {
681         QPainter p;
682         char s[2];
683         QFontMetrics fm(*prv_font);
684
685         s[0] = a;
686         s[1] = '\0';
687         p.begin(canvas);
688         p.setPen(*lcolors[fcol]);
689         p.setBackgroundColor(*lcolors[bcol]);
690         p.setBackgroundMode(OpaqueMode);
691         p.setFont(*prv_font);
692         p.drawText(x , y + fm.height(), s, strlen(s));
693         p.end();
694
695         p.begin(this);
696         p.setPen(*lcolors[fcol]);
697         p.setBackgroundColor(*lcolors[bcol]);
698         p.setBackgroundMode(OpaqueMode);
699         p.setFont(*prv_font);
700         p.drawText(x - ox, y - oy + fm.height(), s, strlen(s));
701         p.end();
702 }
703
704
705 void VGR::IntXY(int x, int y, int val)
706 {
707         QPainter p;
708         char s[80];
709         QFontMetrics fm(*prv_font);
710
711         sprintf(s, "%d", val);
712         p.begin(canvas);
713         p.setPen(*lcolors[fcol]);
714         p.setBackgroundColor(*lcolors[bcol]);
715         p.setBackgroundMode(OpaqueMode);
716         p.setFont(*prv_font);
717         p.drawText(x, y + fm.height(), s, strlen(s));
718         p.end();
719
720         p.begin(this);
721         p.setPen(*lcolors[fcol]);
722         p.setBackgroundColor(*lcolors[bcol]);
723         p.setBackgroundMode(OpaqueMode);
724         p.setFont(*prv_font);
725         p.drawText(x - ox, y - oy + fm.height(), s, strlen(s));
726         p.end();
727 }
728
729 void VGR::WriteText(char *s)
730 {
731         QPainter p;
732         QFontMetrics fm(*prv_font);
733
734         p.begin(canvas);
735         p.setPen(*lcolors[fcol]);
736         p.setBackgroundColor(*lcolors[bcol]);
737         p.setBackgroundMode(OpaqueMode);
738         p.setFont(*prv_font);
739         p.drawText(tx * fm.maxWidth(), (ty + 1) * fm.height(), s, strlen(s));
740         p.end();
741
742         p.begin(this);
743         p.setPen(*lcolors[fcol]);
744         p.setBackgroundColor(*lcolors[bcol]);
745         p.setBackgroundMode(OpaqueMode);
746         p.setFont(*prv_font);
747         p.drawText(tx * fm.maxWidth() - ox, (ty + 1) * fm.height() - oy, s,
748                                                                 strlen(s));
749         p.end();
750
751         tx = tx + strlen(s);
752         if (tx > TEXT_COLS) {
753                 tx = 0;
754                 ty++;
755         }
756         if (ty > TEXT_LINES) {
757                 ClearAll();
758         }
759 }
760
761 void VGR::WriteChar(char a)
762 {
763         QPainter p;
764         QFontMetrics fm(*prv_font);
765         char s[2];
766
767         s[0] = a;
768         s[1] = '\0';
769
770         if (GraphRead) {
771                 p.begin(canvas);
772                 p.setPen(*lcolors[gfcol]);
773                 p.setBackgroundColor(*lcolors[gbcol]);
774                 p.setBackgroundMode(OpaqueMode);
775                 p.setFont(*prv_font);
776                 p.drawText(gx, gy + fm.height(), s, strlen(s));
777                 p.end();
778
779                 p.begin(this);
780                 p.setPen(*lcolors[gfcol]);
781                 p.setBackgroundColor(*lcolors[gbcol]);
782                 p.setBackgroundMode(OpaqueMode);
783                 p.setFont(*prv_font);
784                 p.drawText(gx - ox, gy - oy + fm.height(), s, strlen(s));
785                 p.end();
786
787                 gx = gx + strlen(s) * fm.maxWidth();
788                 if (gx > MAXWIDTH) {
789                         gy = gy + fm.height();
790                         gx=0;
791                 }
792                 if (gy > MAXHEIGHT) {
793                         ClearAll();
794                 }
795         } else {
796                 p.begin(canvas);
797                 p.setPen(*lcolors[fcol]);
798                 p.setBackgroundColor(*lcolors[bcol]);
799                 p.setBackgroundMode(OpaqueMode);
800                 p.setFont(*prv_font);
801                 p.drawText(tx * fm.maxWidth(), (ty + 1) * fm.height(), s, 
802                                                                 strlen(s));
803                 p.end();
804
805                 p.begin(this);
806                 p.setPen(*lcolors[fcol]);
807                 p.setBackgroundColor(*lcolors[bcol]);
808                 p.setBackgroundMode(OpaqueMode);
809                 p.setFont(*prv_font);
810                 p.drawText(tx * fm.maxWidth() - ox, (ty + 1) * fm.height() - oy,
811                                                                 s, strlen(s));
812                 p.end();
813
814                 tx = tx + strlen(s);
815                 if (tx > TEXT_COLS) {
816                         tx = 0;
817                         ty++;
818                 }
819                 if (ty > TEXT_LINES) {
820                         ClearAll();
821                 }
822         }
823 }
824
825 void VGR::DelChar()
826 {
827         QPainter p;
828         QFontMetrics fm(*prv_font);
829
830         if (GraphRead) {
831                 p.begin(canvas);
832                 p.setPen(*lcolors[fcol]);
833                 p.fillRect(gx - fm.maxWidth(), gy, fm.maxWidth(),
834                                 fm.height() + fm.descent() + 1, *lcolors[bcol]);
835                 p.end();
836
837                 p.begin(this);
838                 p.setPen(*lcolors[fcol]);
839
840                 p.fillRect(gx - ox - fm.maxWidth(), gy - oy, fm.maxWidth(),
841                                 fm.height() + fm.descent() + 1, *lcolors[bcol]);
842                 p.end();
843                 gx = gx - fm.maxWidth();
844         } else {
845                 /* Text read */
846                 p.begin(canvas);
847                 p.setPen(*lcolors[fcol]);
848                 p.fillRect((tx - 1) * fm.maxWidth(), (ty) * fm.height(), 
849                                                         tx * fm.maxWidth(),
850                                 (ty + 1) * fm.height() + fm.descent() + 1, 
851                                                                 *lcolors[bcol]);
852                 p.end();
853
854                 p.begin(this);
855                 p.setPen(*lcolors[fcol]);
856                 p.fillRect((tx - 1) * fm.maxWidth() - ox, (ty) * fm.height() - oy,
857                                                         tx * fm.maxWidth(),
858                                 (ty + 1) * fm.height() + fm.descent() + 1,
859                                                                 *lcolors[bcol]);
860                 p.end();
861
862                 tx--;
863                 if (tx < 0)
864                         tx = 0;
865         }
866 }
867
868 void VGR::PutChar(char a)
869 {
870         QPainter p;
871         QFontMetrics fm(*prv_font);
872         char s[2];
873
874         s[0] = a;
875         s[1] = '\0';
876         p.begin(canvas);
877         p.setPen(*lcolors[fcol]);
878         p.setBackgroundColor(*lcolors[bcol]);
879         p.setBackgroundMode(OpaqueMode);
880         p.setFont(*prv_font);
881         p.drawText(tx * fm.maxWidth(), (ty + 1) * fm.height(), s, strlen(s));
882         p.end();
883
884         p.begin(this);
885         p.setPen(*lcolors[fcol]);
886         p.setBackgroundColor(*lcolors[bcol]);
887         p.setBackgroundMode(OpaqueMode);
888         p.setFont(*prv_font);
889         p.drawText(tx * fm.maxWidth() - ox, (ty + 1) * fm.height() - oy, s,
890                                                                 strlen(s));
891         p.end();
892 }
893
894 void VGR::WriteLine()
895 {
896         tx = 0;
897         ty++;
898         if (ty > TEXT_LINES) {
899                 ClearAll();
900         }
901 }
902
903
904 void VGR::Point(int x, int y)
905 {
906         QPainter p;
907
908         p.begin(canvas);
909         p.setPen(*lcolors[fcol]);
910         p.drawPoint(x, y);
911         p.end();
912
913         p.begin(this);
914         p.setPen(*lcolors[fcol]);
915         p.drawPoint(x - ox, y - oy);
916         p.end();
917 }
918
919 void VGR::ClearArea(int x, int y, int w, int h)
920 {
921         QPainter p;
922         p.begin(canvas);
923         p.eraseRect(x, y, w, h);
924         p.end();
925
926         p.begin(this);
927         p.eraseRect(x - ox, y - oy, w, h);
928         p.end();
929 }
930
931 void VGR::ClearAll()
932 {
933         QPainter p;
934         QBrush b(QColor(white));
935
936         tx = ty = 0;
937         curx = cury = 0;
938         canvas->fill(backgroundColor());
939
940         p.begin(this);
941         p.fillRect(0, 0, width() - 16, height() - 16, backgroundColor());
942         p.end();
943 }
944
945 int VGR::Getmap(int w, int h)
946 {
947         QPixmap *m;
948
949         m = new QPixmap(w, h);
950         bitBlt(m, 0, 0, canvas, curx, cury, w, h);
951         maps.append(new VGRMap(firstfreemap, m));
952         firstfreemap++;
953         return(firstfreemap - 1);
954 }
955
956
957 void VGR::Putmap(int map)
958 {
959         VGRMap *m;
960         m = maps.first();
961         while (m != NULL) {
962                 if (m->number == map) {
963                         bitBlt(canvas, curx, cury, m->map, 0, 0,
964                                         m->map->width(), m->map->height());
965                         bitBlt(this, curx - ox, cury - oy, m->map, 0, 0,
966                                         m->map->width(), m->map->height());
967                         break;
968                 }
969                 m = maps.next();
970         }
971 }
972
973 void VGR::Outstring(int x, int y, char *s, int b, int f)
974 {
975         QPainter p;
976         QFontMetrics fm(*prv_font);
977
978         p.begin(canvas);
979         p.setPen(*lcolors[f]);
980         p.setBackgroundColor(*lcolors[b]);
981         p.setBackgroundMode(OpaqueMode);
982         p.setFont(*prv_font);
983         p.drawText(x, y + fm.height(), s, strlen(s));
984         p.end();
985
986         p.begin(this);
987         p.setPen(*lcolors[f]);
988         p.setFont(*prv_font);
989         p.setBackgroundColor(*lcolors[b]);
990         p.setBackgroundMode(OpaqueMode);
991         p.drawText(x - ox, y - oy + fm.height(), s, strlen(s));
992         p.end();
993
994 }
995
996 void VGR::writeintxy(int x, int y, int val,int c)
997 {
998         QPainter p;
999         char s[80];
1000         QFontMetrics fm(*prv_font);
1001
1002         sprintf(s, "%d", val);
1003         p.begin(canvas);
1004         p.setPen(*lcolors[c]);
1005         p.setFont(*prv_font);
1006         p.drawText(x, y, s, strlen(s));
1007         p.end();
1008
1009         p.begin(this);
1010         p.setPen(*lcolors[c]);
1011         p.setFont(*prv_font);
1012         p.drawText(x - ox, y - oy + fm.height(), s, strlen(s));
1013         p.end();
1014 }
1015
1016
1017 void VGR::CurPos()
1018 {
1019         G_MESSAGE msg;
1020         msg.msg_type = MSG_GRAPH;
1021         msg.param.pword[0] = GRAPH_CURPOS_RESPONSE;
1022         msg.param.pword[3] = curx;
1023         msg.param.pword[4] = cury;
1024         write(resp_sock, &msg, sizeof(G_MESSAGE));
1025 }
1026
1027
1028 void VGR::RequestMessage()
1029 {
1030         G_MESSAGE m,mm;
1031         int stat;
1032
1033         bzero(&m, sizeof(G_MESSAGE));
1034         stat = read(resp_sock, &m, sizeof(G_MESSAGE));
1035         if (stat > 0) {
1036                 if (m.msg_type == MSG_GRAPH) {
1037                         switch(m.param.pword[0]) {
1038                         case GRAPH_FREE: 
1039                                 close_btn->setEnabled(TRUE);
1040                                 delete request;
1041                                 request = NULL;
1042                                 break;
1043                         case GRAPH_SET_TITLE:
1044                                 setCaption(m.param.pstr);
1045                                 break;
1046                         case GRAPH_WRITE:
1047                                 if (strcmp(m.param.pstr,"\n") == 0)
1048                                         WriteLine();
1049                                 else
1050                                         WriteText(m.param.pstr);
1051
1052                                 break;
1053                         case GRAPH_WRITEXY:
1054                                 TextXY(m.param.pword[3], m.param.pword[4], 
1055                                                                 m.param.pstr);
1056                                 break;
1057                         case GRAPH_READCHAR:
1058                                 if (!GetInput(0))
1059                                         char_wait = TRUE;
1060
1061                                 break;
1062                         case GRAPH_READSTR:
1063                                 strcpy(internal_buffer, "");
1064                                 strcnt = 0;
1065                                 string_wait = TRUE;
1066                                 WriteChar('_');
1067                                 break;
1068                         case GRAPH_READLN:
1069                                 if (was_line) {
1070                                         m.msg_type = MSG_GRAPH;
1071                                         m.param.pword[0] = GRAPH_READLN_RESPONSE;
1072                                         write(resp_sock, &m, sizeof(G_MESSAGE));
1073                                         was_line = FALSE;
1074                                 } else if (!GetInput(1))
1075                                         line_wait = TRUE;
1076                                 break;
1077                         case GRAPH_PUTCHAR:
1078                                 WriteChar(m.param.pchar);
1079                                 break;
1080                         case GRAPH_LINETO:
1081                                 Line(curx,cury, m.param.pword[3],
1082                                                         m.param.pword[4]);
1083                                 break;
1084                         case GRAPH_ELLIPSE:
1085                                 Ellipse(m.param.pword[3], m.param.pword[4],
1086                                         m.param.pword[5], m.param.pword[6], 
1087                                         m.param.pword[7], m.param.pword[8],
1088                                                         m.param.pword[9]);
1089                                 break;
1090                         case GRAPH_RECT:
1091                                 Rect(m.param.pword[3], m.param.pword[4], 
1092                                         m.param.pword[5], m.param.pword[6],
1093                                         m.param.pword[7], m.param.pword[8]);
1094                                 break;  
1095                         case GRAPH_FOREGROUND:
1096                                 SetForeground(m.param.pword[3]);
1097                                 break;
1098                         case GRAPH_BACKGROUND:
1099                                 SetBackground(m.param.pword[3]);
1100                                 break;
1101                         case GRAPH_POINT:
1102                                 Point(m.param.pword[3], m.param.pword[4]);
1103                                 break;
1104                         case GRAPH_CLEAR:
1105                                 ClearAll();
1106                                 break;
1107                         case GRAPH_INKEY:
1108                                 if (!GetInput(2))
1109                                         inkey_wait = TRUE;
1110
1111                                 break; 
1112                         case GRAPH_CURPOS:
1113                                 CurPos();
1114                                 break;
1115                         case GRAPH_OUTSTRING:
1116                                 Outstring(m.param.pword[2], m.param.pword[3],
1117                                                 m.param.pstr, m.param.pword[4],
1118                                                 m.param.pword[5]);
1119                                 break;
1120                         case GRAPH_WRITEINTXY:
1121                                 writeintxy(m.param.pword[2], m.param.pword[3],
1122                                         m.param.pword[4], m.param.pword[5]);
1123                                 break;
1124                         case GRAPH_GETMAP:
1125                                 bzero(&mm, sizeof(G_MESSAGE));
1126                                 mm.msg_type = MSG_GRAPH;
1127                                 mm.param.pword[0] = GRAPH_GETMAP_RESPONSE;
1128                                 mm.param.pword[2] = Getmap(m.param.pword[2] - curx, m.param.pword[3] - cury);
1129                                 mm.param.pword[3] = m.param.pword[2] - curx;
1130                                 mm.param.pword[4] = m.param.pword[3] - cury;
1131                                 write(resp_sock, &mm, sizeof(G_MESSAGE));
1132                                 break;
1133                         case GRAPH_PUTMAP:
1134                                 Putmap(m.param.pword[2]);
1135                                 break;
1136                         case GRAPH_MOVE:
1137                                 curx = m.param.pword[2];
1138                                 cury = m.param.pword[3];
1139                                 break;
1140                         case GRAPH_MGETPRESS:
1141                                 mouse_wait = TRUE;
1142                                 break;
1143                         case GRAPH_MAGIC:
1144                                 MagicGraph(&m);
1145                                 break;
1146                         case GRAPH_HASCII:
1147                                 if (m.param.pword[1] != 0)
1148                                         CharXY(curx, cury, (char)m.param.pword[1]);
1149                                 else
1150                                         CharXY(curx, cury, ' ');
1151                                 break;
1152                         } /* end switch */
1153                 } /* stat > 0 */
1154         }
1155 }
1156
1157
1158
1159
1160 void VGR::MagicGraph(G_MESSAGE *msg)
1161 {
1162         int f,b;
1163         char ss[255];
1164         QPixmap p;
1165         VGRMap *pmap;
1166
1167         switch(msg->param.pword[1]) {
1168         /* gotoxy */
1169         case 10:
1170                 tx = msg->param.pword[2];
1171                 ty = msg->param.pword[3];
1172                 break;
1173         /* forecolor */
1174         case 11:
1175                 fcol = msg->param.pword[2];
1176                 break;
1177         /* bkcolor */
1178         case 12:
1179                 bcol = msg->param.pword[2];
1180                 break;
1181         case 13:
1182                 prv_font = bold_font;
1183                 break;
1184         case 14:
1185                 prv_font = italic_font;
1186                 break;
1187         case 15:
1188                 prv_font = normal_font;
1189                 break; 
1190         case 16:
1191                 ClearAll();
1192                 break;
1193         /* Draw array_of char (x,y,fcol,bcol) */
1194         case 303:
1195                 f = fcol;
1196                 b = bcol;
1197                 fcol = msg->param.pword[4];
1198                 bcol = msg->param.pword[5];
1199                 TextXY(msg->param.pword[2], msg->param.pword[3], msg->param.pstr);
1200                 fcol = f;
1201                 bcol = b;
1202                 break;
1203         /* Draw int (x,y,int,fcol,bcol) */
1204         case 300:
1205                 f = fcol;
1206                 b = bcol;
1207                 fcol = msg->param.pword[5];
1208                 bcol = msg->param.pword[6];
1209                 IntXY(msg->param.pword[2], msg->param.pword[3], msg->param.pword[4]);
1210                 fcol = f;
1211                 bcol = b;
1212                 break;
1213         /* Draw char (x,y,char,fcol,bcol) */
1214         case 301:
1215                 f = fcol;
1216                 b = bcol;
1217                 fcol = msg->param.pword[5];
1218                 bcol = msg->param.pword[6];
1219                 CharXY(msg->param.pword[2], msg->param.pword[3], 
1220                                                 (char)(msg->param.pword[4]));
1221                 fcol = f;
1222                 bcol = b;
1223                 break;
1224         /* Draw real (x,y,int,frac,fcol,bcol) */
1225         case 302:
1226                 f = fcol;
1227                 b = bcol;
1228                 fcol = msg->param.pword[6];
1229                 bcol = msg->param.pword[7];
1230                 sprintf(ss, "%d.%d", msg->param.pword[4], msg->param.pword[5]);
1231                 TextXY(msg->param.pword[2], msg->param.pword[3], ss);
1232                 fcol = f;
1233                 bcol = b;
1234                 break;
1235         /* ReadText (x,y,fcol,bcol) */
1236         case -304:
1237         case -305:
1238         case -307:
1239                 gfcol = msg->param.pword[4];
1240                 gbcol = msg->param.pword[5];
1241                 strcpy(internal_buffer, "");
1242                 strcnt = 0;
1243                 GraphRead = TRUE;
1244                 gx = msg->param.pword[2];
1245                 gy = msg->param.pword[3]; 
1246                 string_wait = TRUE;
1247                 WriteChar('_');
1248                 break;
1249         case -306:
1250                 if (!GetInput(0)) {
1251                         gfcol = msg->param.pword[4];
1252                         gbcol = msg->param.pword[5];
1253                         strcpy(internal_buffer, "");
1254                         strcnt = 0;
1255                         GraphRead = TRUE;
1256                         gx = msg->param.pword[2];
1257                         gy = msg->param.pword[3]; 
1258                         char_wait = TRUE;
1259                 }
1260                 break;
1261         /* Put image from file (x,y,fname) */
1262         case 308:
1263                 if (p.load(msg->param.pstr)) {
1264                         bitBlt(canvas, msg->param.pword[2],
1265                                                 msg->param.pword[3], &p,
1266                                                 0, 0, p.width(), p.height());
1267                         bitBlt(this, msg->param.pword[2] - ox,
1268                                                 msg->param.pword[3] - oy, &p,
1269                                                 0, 0, p.width(), p.height());
1270                 }
1271                 break;
1272         /* Kill map */
1273         case 309:
1274                 pmap = maps.first();
1275                 while (pmap != NULL) {
1276                         if (msg->param.pword[2] == pmap->number) {
1277                                 maps.remove(pmap);
1278                                 break;
1279                         }
1280                         pmap = maps.next();
1281                 }
1282                 break;
1283         /* Line (x1,y1,x2,y2,col) */
1284         case 310:
1285                 f = fcol;
1286                 fcol = msg->param.pword[6];
1287                 Line(msg->param.pword[2], msg->param.pword[3], 
1288                                 msg->param.pword[4], msg->param.pword[5]);
1289                 fcol = f;
1290                 break;
1291         /* Rectangle(x1,y1,x2,y2,fcol,icol) */
1292         case 311:
1293                 Rect(msg->param.pword[2], msg->param.pword[3],
1294                                 msg->param.pword[4], msg->param.pword[5],
1295                                 msg->param.pword[7], 1);
1296                 Rect(msg->param.pword[2], msg->param.pword[3],
1297                                 msg->param.pword[4],msg->param.pword[5],
1298                                 msg->param.pword[6], 0);
1299                 break;
1300         /* ClearArea(x1,y1,w,h) */
1301         case 312:
1302                 ClearArea(msg->param.pword[2],msg->param.pword[3],
1303                                 msg->param.pword[4], msg->param.pword[5]);
1304                 break;
1305         }//switch
1306 }
1307
1308 #include "loggraph.moc"
1309
1310 int main(int argc, char **argv)
1311 {
1312         QApplication app(argc, argv);
1313         VGR gs(argv[1]);
1314         app.setMainWidget(&gs);
1315         gs.show();
1316         return app.exec();
1317 }