40 #define TAG_BOLD_OFF 5
41 #define TAG_ITALIC_ON 6
42 #define TAG_ITALIC_OFF 7
45 #define TAG_LINK_END 10
46 #define TAG_LIST_ON 11
47 #define TAG_LIST_OFF 12
48 #define TAG_LIST_ITEM 13
50 #define PIX_HEIGHT 2000
52 char names[14][40] = {"text","other","link","anchor","bold on",
53 "bold off","italic on","italic off","break",
54 "line","end link","list on","list off","list item"};
61 Tag() {tag_type=0; strcpy(tag_label,""); strcpy(tag_link,"");
62 strcpy(tag_text,"");};
64 char tag_label[255],tag_link[255],tag_text[255];
78 void AnalyzeTag(QString*);
81 Tag *CheckTag(int,int);
82 Tag *FindAnchor(char*);
87 HTMLAnalyzer::HTMLAnalyzer()
94 bool HTMLAnalyzer::LoadFile(char *fname)
104 if (!f.open(IO_ReadOnly)) return(FALSE);
110 poms = fs.readLine();
111 while (poms.length()>0)
118 poms1 = poms.left(i);
120 pomt->tag_type = TAG_TEXT;
121 if (!verbatim) poms1=poms1.simplifyWhiteSpace();
122 sprintf(pomt->tag_text," %s",poms1.data());
125 poms = poms.right(poms.length()-i);
130 poms1 = poms.mid(1,i-1);
132 poms=poms.right(poms.length()-i-1);
134 else { not_ended=TRUE;break;}
139 pomt->tag_type = TAG_TEXT;
140 if (!verbatim) poms=poms.simplifyWhiteSpace();
141 sprintf(pomt->tag_text," %s",poms.data());
146 if (verbatim) {pomt = new Tag; pomt->tag_type=TAG_BREAK; tags.append(pomt);}
154 void HTMLAnalyzer::AnalyzeTag(QString *t)
160 *t = t->simplifyWhiteSpace();
163 if ( (t->data()[0]!='A') && (t->data()[0]!='a') )
166 if (strcmp(t->data(),"B")==0 ) {pom->tag_type = TAG_BOLD_ON;}
168 if (strcmp(t->data(),"/B")==0 ) {pom->tag_type = TAG_BOLD_OFF;}
170 if (strcmp(t->data(),"I")==0 ) {pom->tag_type = TAG_ITALIC_ON;}
172 if (strcmp(t->data(),"/I")==0 ) {pom->tag_type = TAG_ITALIC_OFF;}
174 if (strcmp(t->data(),"BR")==0 ) {pom->tag_type = TAG_BREAK;}
176 if (strcmp(t->data(),"HR")==0 ) {pom->tag_type = TAG_LINE;}
178 if (strcmp(t->data(),"/A")==0 ) {pom->tag_type = TAG_LINK_END;}
180 if (strcmp(t->data(),"UL")==0 ) {pom->tag_type = TAG_LIST_ON;}
182 if (strcmp(t->data(),"/UL")==0 ) {pom->tag_type = TAG_LIST_OFF;}
184 if (strcmp(t->data(),"LI")==0 ) {pom->tag_type = TAG_LIST_ITEM;}
186 if (strcmp(t->data(),"PRE")==0 ) {verbatim=TRUE;}
188 if (strcmp(t->data(),"/PRE")==0 ) {verbatim=FALSE;}
199 poms=poms.simplifyWhiteSpace();
201 poms1=t->right(t->length()-i-1);
202 poms1=poms1.simplifyWhiteSpace();
204 if (strcmp(poms.data(),"HREF")==0)
206 pom->tag_type = TAG_LINK;
207 strcpy(pom->tag_link,poms1.data());
210 if (strcmp(poms.data(),"NAME")==0)
212 pom->tag_type = TAG_ANCHOR;
213 if (poms1.data()[0]=='"') poms1=poms1.right(poms1.length()-1);
214 if (poms1.data()[poms1.length()-1]=='"') poms1=poms1.left(poms1.length()-1);
215 strcpy(pom->tag_label,poms1.data());
222 void HTMLAnalyzer::DumpList()
228 fprintf(stderr,"%s:%s,%s,%s\n",names[pom->tag_type], pom->tag_text,
229 pom->tag_link,pom->tag_label);
235 void HTMLAnalyzer::PackLinks()
243 if ( (pom->tag_type==TAG_LINK ) || (pom->tag_type==TAG_ANCHOR) )
247 while ( (pom1!=NULL) && (pom1->tag_type!=TAG_LINK_END) )
249 if (pom1->tag_type==TAG_TEXT) strcat(s,pom1->tag_text);
253 strcpy(pom->tag_text,s);
263 Tag *HTMLAnalyzer::CheckTag(int x,int y)
269 if ( pom->tag_type==TAG_LINK)
270 if ( (x>=pom->x) && (x<=pom->x+pom->w) &&
271 (y>=pom->y) && (y<=pom->y+pom->h)) return(pom);
277 Tag *HTMLAnalyzer::FindAnchor(char *name)
283 if ( (pom->tag_type==TAG_ANCHOR) && (strcmp(pom->tag_label,name)==0) )
289 //******************************
293 class QHTML: public QFrame
299 HTMLAnalyzer *analyzer;
301 int cx,cy,oy,lstep,pstep;
303 QFont *normal,*bold,*italic,*bold_italic,*actual_font;
308 virtual void paintEvent(QPaintEvent *ev);
309 virtual void resizeEvent(QResizeEvent *ev);
310 virtual void mousePressEvent(QMouseEvent *ev);
324 QHTML::QHTML(char *d)
330 QFont f("Helvetica",12,QFont::Bold);
331 normal = new QFont("Helvetica",12,QFont::Normal);
332 bold = new QFont("Helvetica",12,QFont::Bold);
333 italic = new QFont("Helvetica",12,QFont::Normal,TRUE);
334 bold_italic = new QFont("Helvetica",12,QFont::Bold,TRUE);
337 actual_font = normal;
338 bar = new QMenuBar(this);
340 pp->insertItem("Index",this,SLOT(contents()));
341 pp->insertItem("User Guide",this,SLOT(user_guide()));
342 pp->insertItem("Language reference",this,SLOT(lang_guide()));
344 //pp->setStyle(WindowsStyle);
345 bar->insertItem("File",this,SLOT(load()));
346 bar->insertItem("Contents",pp);
347 bar->insertItem("Quit",app,SLOT(quit()));
349 setCaption("LOGLAN Help");
350 setBackgroundColor(gray);
351 analyzer = new HTMLAnalyzer;
352 map = new QPixmap(500,PIX_HEIGHT);
353 map->fill(backgroundColor());
354 resize(500,height());
355 setFixedSize(width(),height());
356 oy=0;lstep=10;pstep=height()-bar->height();
357 vscroll = new QScrollBar(0,PIX_HEIGHT,lstep,pstep,0,QScrollBar::Vertical,
359 vscroll->setTracking(TRUE);
360 vscroll->setGeometry(width()-16,bar->height(),16,height()-bar->height());
361 connect(vscroll,SIGNAL(valueChanged(int)),this,SLOT(vscrolled(int)));
362 sprintf(s,"%s/index.html",homedir);
363 analyzer->LoadFile(s);
364 analyzer->PackLinks();
370 void QHTML::vscrolled(int v)
378 QString s(QFileDialog::getOpenFileName(homedir,"*",this));
381 vscroll->setValue(0);
382 analyzer->LoadFile((char*)s.ascii());
383 analyzer->PackLinks();
389 void QHTML::contents()
392 sprintf(ss,"%s/index.html",homedir);
393 analyzer->LoadFile(ss);
394 analyzer->PackLinks();
399 void QHTML::user_guide()
402 sprintf(ss,"%s/userg.html",homedir);
403 analyzer->LoadFile(ss);
404 analyzer->PackLinks();
408 void QHTML::lang_guide()
411 sprintf(ss,"%s/langg.html",homedir);
412 analyzer->LoadFile(ss);
413 analyzer->PackLinks();
417 void QHTML::paintEvent(QPaintEvent *ev)
419 if (map!=NULL) bitBlt(this,0,bar->height(),map,0,oy,width()-16,height()-16);
422 void QHTML::resizeEvent(QResizeEvent *ev)
427 void QHTML::mousePressEvent(QMouseEvent *ev)
433 pom=analyzer->CheckTag(ev->x(),ev->y()+oy);
436 poms.sprintf(pom->tag_link);
437 if (poms.data()[0]=='"') poms=poms.right(poms.length()-1);
438 if (poms.data()[poms.length()-1]=='"') poms=poms.left(poms.length()-1);
439 if (poms.data()[0]=='#')
441 poms=poms.right(poms.length()-1);
442 pom1=analyzer->FindAnchor((char*)poms.ascii());
445 vscroll->setValue(pom1->y);
450 sprintf(ss,"%s/%s",homedir,poms.data());
451 analyzer->LoadFile(ss);
452 analyzer->PackLinks();
462 void QHTML::DrawList()
467 if (!analyzer->tags.isEmpty())
470 map->fill(backgroundColor());
473 pom=analyzer->tags.first();
476 switch(pom->tag_type)
478 case TAG_TEXT:p.setFont(*actual_font);
479 if (cx+p.fontMetrics().width(pom->tag_text)>width()-16)
480 {cx=5;cy=cy+p.fontMetrics().height();}
481 p.drawText(cx,cy,pom->tag_text);
482 cx=cx+p.fontMetrics().width(pom->tag_text);
484 case TAG_BREAK: p.setFont(*actual_font);
485 cy=cy+p.fontMetrics().height();cx=5;
488 if (actual_font==bold) actual_font=bold_italic;
489 else actual_font=italic;
492 if (actual_font==bold_italic) actual_font=bold;
493 else actual_font=normal;
496 if (actual_font==italic) actual_font=bold_italic;
497 else actual_font=bold;
500 if (actual_font==bold_italic) actual_font=italic;
501 else actual_font=normal;
503 case TAG_LINK:p.setFont(*actual_font);
504 if (cx+p.fontMetrics().width(pom->tag_text)>width()-16)
505 {cx=5;cy=cy+p.fontMetrics().height();}
506 p.setPen(QColor(255,0,0));
507 p.drawText(cx,cy,pom->tag_text);
508 pom->x = cx;pom->y=cy+p.fontMetrics().height();
509 pom->w = p.fontMetrics().width(pom->tag_text);
510 pom->h = p.fontMetrics().height();
511 p.setPen(QColor(0,0,0));
512 cx=cx+p.fontMetrics().width(pom->tag_text);
514 case TAG_ANCHOR: p.setFont(*actual_font);
515 if (cx+p.fontMetrics().width(pom->tag_text)>width()-16)
516 {cx=5;cy=cy+p.fontMetrics().height();}
517 //p.setPen(QColor(0,255,0));
518 p.drawText(cx,cy,pom->tag_text);
519 pom->x = cx;pom->y=cy-p.fontMetrics().height();
520 pom->w = p.fontMetrics().width(pom->tag_text);
521 pom->h = p.fontMetrics().height();
522 p.setPen(QColor(0,0,0));
523 cx=cx+p.fontMetrics().width(pom->tag_text);
526 case TAG_LIST_ON:p.setFont(*actual_font);
527 cx=5;cy=cy+p.fontMetrics().height();
529 case TAG_LIST_ITEM:p.setFont(*actual_font);
530 p.setBrush(QBrush(QColor(0,0,255)));
531 cx=5;cy=cy+p.fontMetrics().height();
532 p.drawPie(cx,cy-5,5,5,0,5760);
535 case TAG_LINE:p.setFont(*actual_font);
536 cx=5;cy=cy+p.fontMetrics().height();
537 p.drawLine(cx,cy-(int)(p.fontMetrics().height()/2),
538 width()-16-5,cy-(int)(p.fontMetrics().height()/2));
539 cx=5;cy=cy+p.fontMetrics().height();
544 pom = analyzer->tags.next();
555 int main( int argc, char **argv )
559 app = new QApplication(argc,argv);
560 if (argc==2) ps.sprintf(argv[1]);
561 else ps.sprintf(".");
562 QHTML cfg((char*)ps.ascii());
563 //app->setStyle(WindowsStyle);
564 app->setMainWidget(&cfg);