File indexing completed on 2024-12-08 09:47:15

0001 /***************************************************************************
0002  *   Copyright (C) 2004 by Tomas Mecir                                     *
0003  *   kmuddy@kmuddy.org                                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU Library General Public License as       *
0007  *   published by the Free Software Foundation; either version 2 of the    *
0008  *   License, or (at your option) any later version.                       *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU Library General Public License for more details.                  *
0014  ***************************************************************************/
0015 
0016 #include <libmxp/libmxp.h>
0017 
0018 #include <stdio.h>
0019 #include <string.h>
0020 
0021 void displayResult (mxpResult *res)
0022 {
0023   switch (res->type)
0024   {
0025     case 0: {
0026       printf ("Nothing.\n");
0027     }
0028     break;
0029     case 1: {
0030       char *s = (char *) res->data;
0031       if (s && (strcmp (s, "\r\n") == 0))  //newlines are always sent separately
0032         printf ("Newline.\n");
0033       else
0034         printf ("Text: %s\n", s?s:"");
0035     }
0036     break;
0037     case 2: {
0038       int *t = (int *) res->data;
0039       printf ("Line tag: %d\n", *t);
0040     }
0041     break;
0042     case 3: {
0043       flagStruct *fs = (flagStruct *) res->data;
0044       char *s = fs->name;
0045       if (fs->begin)
0046         printf ("Start of flag: %s\n", s?s:"");
0047       else
0048         printf ("End of flag: %s\n", s?s:"");
0049     }
0050     break;
0051     case 4: {
0052       varStruct *vs = (varStruct *) res->data;
0053       char *n = vs->name;
0054       char *v = vs->value;
0055       if (vs->erase)
0056         printf ("Erase variable: %s\n", n?n:"");
0057       else
0058         printf ("Variable: %s, value: %s\n", n?n:"", v?v:"");
0059     }
0060     break;
0061     case 5: {
0062       formatStruct *fs = (formatStruct *) res->data;
0063       unsigned char mask = fs->usemask;
0064       if (mask & USE_BOLD)
0065         printf ("Formatting: %s\n",
0066             (fs->attributes & Bold) ? "bold" : "not bold");
0067       if (mask & USE_ITALICS)
0068         printf ("Formatting: %s\n",
0069             (fs->attributes & Italic) ? "italics" : "not italics");
0070       if (mask & USE_UNDERLINE)
0071         printf ("Formatting: %s\n",
0072             (fs->attributes & Underline) ? "underline" : "not underline");
0073       if (mask & USE_STRIKEOUT)
0074         printf ("Formatting: %s\n",
0075             (fs->attributes & Strikeout) ? "strikeout" : "not strikeout");
0076       if (mask & USE_FG)
0077         printf ("Formatting: foreground color (%d, %d, %d)\n", fs->fg.r, fs->fg.g, fs->fg.b);
0078       if (mask & USE_BG)
0079         printf ("Formatting: background color (%d, %d, %d)\n", fs->bg.r, fs->bg.g, fs->bg.b);
0080       if (mask & USE_FONT)
0081         printf ("Formatting: font %s\n", fs->font?fs->font:"");
0082       if (mask & USE_SIZE)
0083         printf ("Formatting: size %d\n", fs->size);
0084     }
0085     break;
0086     case 6: {
0087       linkStruct *ls = (linkStruct *) res->data;
0088       printf ("URL link: name %s, URL %s, text %s, hint %s\n",
0089           ls->name?ls->name:"", ls->url?ls->url:"", ls->text?ls->text:"", ls->hint?ls->hint:"");
0090     }
0091     break;
0092     case 7: {
0093       sendStruct *ss = (sendStruct *) res->data;
0094       printf ("Send link: name %s, command %s, text %s, hint %s, to prompt: %s, menu: %s\n",
0095           ss->name?ss->name:"", ss->command?ss->command:"", ss->text?ss->text:"",
0096           ss->hint?ss->hint:"", ss->toprompt?"yes":"no", ss->ismenu?"yes":"no");
0097     }
0098     break;
0099     case 8: {
0100       char *s = (char *) res->data;
0101       printf ("Expire: %s\n", s?s:"");
0102     }
0103     break;
0104     case 9: {
0105       char *s = (char *) res->data;
0106       printf ("Send this: %s\n", s?s:"");
0107     }
0108     break;
0109     case 10: {
0110       printf ("Horizontal line\n");
0111     }
0112     break;
0113     case 11: {
0114       soundStruct *ss = (soundStruct *) res->data;
0115       if (ss->isSOUND)
0116         printf ("Sound: file %s, URL %s, volume %d, %d repeats, priority %d, type %s\n",
0117             ss->fname?ss->fname:"", ss->url?ss->url:"", ss->vol, ss->repeats, ss->priority,
0118             ss->type?ss->type:"");
0119       else
0120         printf ("Music: file %s, URL %s, volume %d, %d repeats, continue %s, type %s\n",
0121             ss->fname?ss->fname:"", ss->url?ss->url:"", ss->vol, ss->repeats,
0122             ss->continuemusic?"yes":"no", ss->type?ss->type:"");
0123     }
0124     break;
0125     case 12: {
0126       windowStruct *ws = (windowStruct *) res->data;
0127       printf ("Create window: name %s, title %s, left %d, top %d, width %d, height %d, scrolling %s, floating %s\n",
0128           ws->name?ws->name:"", ws->title?ws->title:"", ws->left, ws->top, ws->width, ws->height,
0129           ws->scrolling?"yes":"no", ws->floating?"yes":"no");
0130     }
0131     break;
0132     case 13: {
0133       internalWindowStruct *ws = (internalWindowStruct *) res->data;
0134       char *s = 0;
0135       switch (ws->align) {
0136         case Left: s = "left"; break;
0137         case Right: s = "right"; break;
0138         case Bottom: s = "bottom"; break;
0139         case Top: s = "top"; break;
0140         case Middle: s = "middle (invalid!)"; break;
0141       };
0142       printf ("Create internal window: name %s, title %s, align %s, scrolling %s\n",
0143           ws->name?ws->name:"", ws->title?ws->title:"", s, ws->scrolling?"yes":"no");
0144     }
0145     break;
0146     case 14: {
0147       char *s = (char *) res->data;
0148       printf ("Close window: %s\n", s?s:"");
0149     }
0150     break;
0151     case 15: {
0152       char *s = (char *) res->data;
0153       printf ("Set active window: %s\n", s?s:"");
0154     }
0155     break;
0156     case 16: {
0157       moveStruct *ms = (moveStruct *) res->data;
0158       printf ("Move cursor: X=%d, Y=%d\n", ms->x, ms->y);
0159     }
0160     break;
0161     case 17: {
0162       printf ("Erase text: %s\n", res->data ? "rest of frame" : "rest of line");
0163     }
0164     break;
0165     case 18: {
0166       relocateStruct *rs = (relocateStruct *) res->data;
0167       printf ("Relocate: server %s, port %d\n", rs->server, rs->port);
0168     }
0169     break;
0170     case 19: {
0171       printf (res->data ? "Send username\n" : "Send password\n");
0172     }
0173     break;
0174     case 20: {
0175       imageStruct *is = (imageStruct *) res->data;
0176       char *s = 0;
0177       switch (is->align) {
0178         case Left: s = "left"; break;
0179         case Right: s = "right"; break;
0180         case Bottom: s = "bottom"; break;
0181         case Top: s = "top"; break;
0182         case Middle: s = "middle"; break;
0183       };
0184       printf ("Image: name %s, URL %s, type %s, height %d, width %d, hspace %d, vspace %d, align %s\n",
0185           is->fname?is->fname:"", is->url?is->url:"", is->type?is->type:"", is->height,
0186           is->width, is->hspace, is->vspace, s);
0187     }
0188     break;
0189     case 21: {
0190       char *s = (char *) res->data;
0191       printf ("Image map: %s\n", s?s:"");
0192     }
0193     break;
0194     case 22: {
0195       gaugeStruct *gs = (gaugeStruct *) res->data;
0196       printf ("Gauge: variable %s, max.variable %s, caption %s, color (%d, %d, %d)\n",
0197           gs->variable?gs->variable:"", gs->maxvariable?gs->maxvariable:"",
0198           gs->caption?gs->caption:"", gs->color.r, gs->color.g, gs->color.b);
0199     }
0200     break;
0201     case 23: {
0202       statStruct *ss = (statStruct *) res->data;
0203       printf ("Status bar: variable %s, max.variable %s, caption %s\n",
0204           ss->variable?ss->variable:"", ss->maxvariable?ss->maxvariable:"",
0205           ss->caption?ss->caption:"");
0206     }
0207     break;
0208     case -1: {
0209       char *s = (char *) res->data;
0210       printf ("Error: %s\n", s?s:"");
0211     }
0212     break;
0213     case -2: {
0214       char *s = (char *) res->data;
0215       printf ("Warning: %s\n", s?s:"");
0216     }
0217     break;
0218   }
0219 }
0220 
0221 int main ()
0222 {
0223   RGB white = {255, 255, 255};
0224   RGB black = {0, 0, 0};
0225   RGB yellow = {255, 255, 0};
0226 
0227   //create MXP handler
0228   MXPHANDLER h = mxpCreateHandler ();
0229 
0230   //initialize the MXP handler
0231   mxpSetDefaultText (h, "fixed", 12, false, false, false, false, white, black);
0232   mxpSetHeaderParams (h, 1, "fixed", 28, true, true, true, false, yellow, black);
0233   mxpSetHeaderParams (h, 2, "fixed", 24, true, true, true, false, yellow, black);
0234   mxpSetHeaderParams (h, 3, "fixed", 20, true, true, true, false, yellow, black);
0235   mxpSetHeaderParams (h, 4, "fixed", 16, true, true, false, false, yellow, black);
0236   mxpSetHeaderParams (h, 5, "fixed", 14, true, true, false, false, yellow, black);
0237   mxpSetHeaderParams (h, 6, "fixed", 12, true, true, false, false, yellow, black);
0238   mxpSetDefaultGaugeColor (h, white);
0239   mxpSetNonProportFont (h, "fixed");
0240   mxpSetClient (h, "KMuddy", "0.7");
0241 
0242   //feed the library with text
0243   mxpProcessText (h, "\e[1z");
0244   mxpProcessText (h, "&unknownentity;&lt;&gt;<c yellow back=blue>starting</c> text");
0245   mxpProcessText (h, " and<font size=11 blue blackx face='\"\"><<>Times New Roman' 13>more");
0246   mxpProcessText (h, "<font face=\"baaah\"></font></font> text\r\n\e[1zline 2");
0247   mxpProcessText (h, " <S><b><i>with bold text, &Aacute;, &lt;, &gt;, unfinished &");
0248   mxpProcessText (h, "\e[3z</BoLd>:)<a href=\r\nline 3");
0249   mxpProcessText (h, "\e[6z");
0250   mxpProcessText (h, "<hr><p>line1\r\nline2\r\nline3\r\nline4\r\n all on the same line!</p>");
0251   mxpProcessText (h, "break line here<br><nobr>this won't break \r\nstill");
0252   mxpProcessText (h, "the same line\r\nanother line");
0253   mxpProcessText (h, "<c darkgreen> dark green <h>High-color</h> Normal color</c>\r\n");
0254   mxpProcessText (h, "<a \"http://www.kmuddy.org\" KMuddy kmuddy>Click here for KMuddy</a>");
0255   mxpProcessText (h, "<send href=\"buy bread|buy milk\" hINt=\"this is a hint|bread hint|");
0256   mxpProcessText (h, "milk hint\" expire=link>yes, bread</send>");
0257   mxpProcessText (h, "<expire link><expire>\n");
0258   mxpProcessText (h, "<VeRsIoN>\n");
0259   mxpProcessText (h, "<sound bah.wav v=10000 p=30 c=1><music chrm.mid u=none>");
0260   mxpProcessText (h, "<gauge hp max=maxhp caption='Your HP' color=green><stat sp max=maxsp");
0261   mxpProcessText (h, "caption='Spell points' invalid>");
0262   mxpProcessText (h, "<relocate port=8080 name=bah server=test><user><password>\n");
0263   mxpProcessText (h, "<image bah.jpg URL=www.url.org t=type1 h=100 w=50 align=middle");
0264   mxpProcessText (h, "vspace=10 hspace=10><image bah.jpg ISMaP><send showmap>");
0265   mxpProcessText (h, "<image bah.jpg ismap></send>\n");
0266   mxpProcessText (h, "<!en test mytest DESC=desc PUBLISH>&test;<!en test DELETE>");
0267   mxpProcessText (h, "<v bah>some<b></b>thing</v>\n");
0268   mxpProcessText (h, "<h2>Heading 2</h2><h5>Heading 5</h5>\r\n<small>small</small>");
0269   mxpProcessText (h, "<tt>non-proportional</tt>\r\n");
0270 
0271   mxpProcessText (h, "<frame name=Status redirect Height=30>");
0272   mxpProcessText (h, "text for status");
0273   mxpProcessText (h, "<frame _previous redirect>");
0274   mxpProcessText (h, "<dest status>100</dest>");
0275   mxpProcessText (h, "<dest status eof>100</dest>");
0276   mxpProcessText (h, "<frame Status close>\r\n");
0277   mxpProcessText (h, "<!-- this is a comment, <b> stuff not parsed -->\r\n");
0278   mxpProcessText (h, "This will break the line: \rnew line\r\n");
0279   
0280   //OKay, here goes the !element testing...
0281   //EMPTY and OPEN flags have been successfully tested :)
0282   mxpProcessText (h, "<!element boldred '<color red><b>'>");
0283   mxpProcessText (h, "normal, <boldred>bold red</boldred>, normal again\n");
0284   mxpProcessText (h, "<!element ex '<send>'><ex>n</ex><ex>s</ex>\r\n");
0285   mxpProcessText (h, "<!entity col 'This should not be displayed' private>");
0286   mxpProcessText (h, "<!element bt '<b><c &col;>' att='col=red' flag=hrm>");
0287   mxpProcessText (h, "<bt>bold red <bt blue>bold blue</bt></bt>\r\n");
0288   mxpProcessText (h, "<!attlist bt 'col=green'>");
0289   mxpProcessText (h, "<bt>bold green <bt blue>bold blue</bt></bt>\r\n");
0290   mxpProcessText (h, "<!el bt delete><bt>in invalid tag</bt>\r\n");
0291   mxpProcessText (h, "<!el tagtest '<b><u><i>' tag=28>\r\n");
0292   mxpProcessText (h, "\e[28zthis should be b,u,i\r\nnormal\r\n");
0293   
0294   //display results
0295   while (mxpHasResults (h))
0296   {
0297     mxpResult *res = mxpNextResult (h);
0298     displayResult (res);
0299   }
0300 
0301   //free memory
0302   mxpDestroyHandler (h);
0303 }