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