]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlparser.cpp
latest sources from M
[wxWidgets.git] / user / wxLayout / wxlparser.cpp
1 /*-*- c++ -*-********************************************************
2 * wxlparser.h : parsers, import/export for wxLayoutList *
3 * *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8
9 #ifdef __GNUG__
10 # pragma implementation "wxlparser.h"
11 #endif
12
13 #include "wxllist.h"
14 #include "wxlparser.h"
15
16 #define BASE_SIZE 12
17
18 void wxLayoutImportText(wxLayoutList &list, String const &str)
19 {
20 char * cptr = (char *)str.c_str(); // string gets changed only temporarily
21 const char * begin = cptr;
22 char backup;
23
24 for(;;)
25 {
26 begin = cptr++;
27 while(*cptr && *cptr != '\n')
28 cptr++;
29 backup = *cptr;
30 *cptr = '\0';
31 list.Insert(begin);
32 *cptr = backup;
33 if(backup == '\n')
34 list.LineBreak();
35 else if(backup == '\0') // reached end of string
36 break;
37 cptr++;
38 }
39 }
40
41 static
42 String wxLayoutExportCmdAsHTML(wxLayoutObjectCmd const & cmd,
43 wxLayoutStyleInfo **lastStylePtr)
44 {
45 static char buffer[20];
46 String html;
47
48 wxLayoutStyleInfo *si = cmd.GetStyle();
49 wxLayoutStyleInfo *last_si = NULL;
50
51 int size, sizecount;
52
53 if(lastStylePtr != NULL)
54 last_si = *lastStylePtr;
55
56 html += "<font ";
57
58 html +="color=";
59 sprintf(buffer,"\"#%02X%02X%02X\"", si->fg_red,si->fg_green,si->fg_blue);
60 html += buffer;
61
62
63 html += " bgcolor=";
64 sprintf(buffer,"\"#%02X%02X%02X\"", si->bg_red,si->bg_green,si->bg_blue);
65 html += buffer;
66
67 switch(si->family)
68 {
69 case wxSWISS:
70 case wxMODERN:
71 html += " face=\"Arial,Helvetica\""; break;
72 case wxROMAN:
73 html += " face=\"Times New Roman, Times\""; break;
74 case wxTELETYPE:
75 html += " face=\"Courier New, Courier\""; break;
76 default:
77 ;
78 }
79
80 size = BASE_SIZE; sizecount = 0;
81 while(size < si->size && sizecount < 5)
82 {
83 sizecount ++;
84 size = (size*12)/10;
85 }
86 while(size > si->size && sizecount > -5)
87 {
88 sizecount --;
89 size = (size*10)/12;
90 }
91 html += "size=";
92 sprintf(buffer,"%+1d", sizecount);
93 html += buffer;
94
95 html +=">";
96
97 if(last_si != NULL)
98 html ="</font>"+html; // terminate any previous font command
99
100 if((si->weight == wxBOLD) && ( (!last_si) || (last_si->weight != wxBOLD)))
101 html += "<b>";
102 else
103 if(si->weight != wxBOLD && ( last_si && (last_si->weight == wxBOLD)))
104 html += "</b>";
105
106 if(si->style == wxSLANT)
107 si->style = wxITALIC; // the same for html
108
109 if((si->style == wxITALIC) && ( (!last_si) || (last_si->style != wxITALIC)))
110 html += "<i>";
111 else
112 if(si->style != wxITALIC && ( last_si && (last_si->style == wxITALIC)))
113 html += "</i>";
114
115 if(si->underline && ( (!last_si) || ! last_si->underline))
116 html += "<u>";
117 else if(si->underline == false && ( last_si && last_si->underline))
118 html += "</u>";
119
120 if(last_si)
121 delete last_si;
122 *lastStylePtr = si;
123 return html;
124 }
125
126
127
128 #define WXLO_IS_TEXT(type) \
129 ( (type == WXLO_TYPE_TEXT || type == WXLO_TYPE_LINEBREAK) \
130 || (type == WXLO_TYPE_CMD \
131 && mode == WXLO_EXPORT_AS_HTML))
132
133
134
135 wxLayoutExportObject *wxLayoutExport(wxLayoutList &list,
136 wxLayoutList::iterator &from,
137 wxLayoutExportMode mode)
138 {
139 if(from == list.end())
140 return NULL;
141
142 wxLayoutObjectType type = (*from)->GetType();
143 wxLayoutExportObject * export = new wxLayoutExportObject();
144
145 static wxLayoutStyleInfo *s_si = NULL;
146
147 if( mode == WXLO_EXPORT_AS_OBJECTS || ! WXLO_IS_TEXT(type)) // simple case
148 {
149 export->type = WXLO_EXPORT_OBJECT;
150 export->content.object = *from;
151 from++;
152 return export;
153 }
154
155 String *str = new String();
156
157 // text must be concatenated
158 while(from != list.end() && WXLO_IS_TEXT(type))
159 {
160 switch(type)
161 {
162 case WXLO_TYPE_TEXT:
163 *str += ((wxLayoutObjectText *)*from)->GetText();
164 break;
165 case WXLO_TYPE_LINEBREAK:
166 if(mode == WXLO_EXPORT_AS_HTML)
167 *str += "<br>";
168 *str += '\n';
169 break;
170 case WXLO_TYPE_CMD:
171 //wxASSERT(mode == WXLO_EXPORT_AS_HTML,"reached cmd object in text mode")
172 assert(mode == WXLO_EXPORT_AS_HTML);
173 *str += wxLayoutExportCmdAsHTML(*(wxLayoutObjectCmd const
174 *)*from, &s_si);
175 break;
176 default: // ignore icons
177 ;
178 }
179 from++;
180 if(from != list.end())
181 type = (*from)->GetType();
182 }
183 export->type = (mode == WXLO_EXPORT_AS_HTML) ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
184 export->content.text = str;
185 return export;
186 }