]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlparser.cpp
another API change fix
[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 "Mpch.h"
14 #ifdef M_BASEDIR
15 # include "gui/wxllist.h"
16 # include "gui/wxlparser.h"
17 #else
18 # include "wxllist.h"
19 # include "wxlparser.h"
20 #endif
21
22 #define BASE_SIZE 12
23
24 inline static bool IsEndOfLine(const char *p)
25 {
26 // in addition to Unix EOL convention we also (but not instead) understand
27 // the DOS one under Windows
28 return
29 #ifdef OS_WIN
30 ((*p == '\r') && (*(p + 1) == '\n')) ||
31 #endif
32 (*p == '\n');
33 }
34
35 void wxLayoutImportText(wxLayoutList &list, String const &str)
36 {
37 char * cptr = (char *)str.c_str(); // string gets changed only temporarily
38 const char * begin = cptr;
39 char backup;
40
41 for(;;)
42 {
43 begin = cptr;
44 while( *cptr && !IsEndOfLine(cptr) )
45 cptr++;
46 backup = *cptr;
47 *cptr = '\0';
48 list.Insert(begin);
49 *cptr = backup;
50
51 // check if it's the end of this line
52 if ( IsEndOfLine(cptr) )
53 {
54 // if it was "\r\n", skip the following '\n'
55 if ( *cptr == '\r' )
56 cptr++;
57 list.LineBreak();
58 }
59 else if(backup == '\0') // reached end of string
60 break;
61 cptr++;
62 }
63 }
64
65 static
66 String wxLayoutExportCmdAsHTML(wxLayoutObjectCmd const & cmd,
67 wxLayoutStyleInfo **lastStylePtr)
68 {
69 static char buffer[20];
70 String html;
71
72 wxLayoutStyleInfo *si = cmd.GetStyle();
73 wxLayoutStyleInfo *last_si = NULL;
74
75 int size, sizecount;
76
77 if(lastStylePtr != NULL)
78 last_si = *lastStylePtr;
79
80 html += "<font ";
81
82 html +="color=";
83 sprintf(buffer,"\"#%02X%02X%02X\"", si->fg_red,si->fg_green,si->fg_blue);
84 html += buffer;
85
86
87 html += " bgcolor=";
88 sprintf(buffer,"\"#%02X%02X%02X\"", si->bg_red,si->bg_green,si->bg_blue);
89 html += buffer;
90
91 switch(si->family)
92 {
93 case wxSWISS:
94 case wxMODERN:
95 html += " face=\"Arial,Helvetica\""; break;
96 case wxROMAN:
97 html += " face=\"Times New Roman, Times\""; break;
98 case wxTELETYPE:
99 html += " face=\"Courier New, Courier\""; break;
100 default:
101 ;
102 }
103
104 size = BASE_SIZE; sizecount = 0;
105 while(size < si->size && sizecount < 5)
106 {
107 sizecount ++;
108 size = (size*12)/10;
109 }
110 while(size > si->size && sizecount > -5)
111 {
112 sizecount --;
113 size = (size*10)/12;
114 }
115 html += "size=";
116 sprintf(buffer,"%+1d", sizecount);
117 html += buffer;
118
119 html +=">";
120
121 if(last_si != NULL)
122 html ="</font>"+html; // terminate any previous font command
123
124 if((si->weight == wxBOLD) && ( (!last_si) || (last_si->weight != wxBOLD)))
125 html += "<b>";
126 else
127 if(si->weight != wxBOLD && ( last_si && (last_si->weight == wxBOLD)))
128 html += "</b>";
129
130 if(si->style == wxSLANT)
131 si->style = wxITALIC; // the same for html
132
133 if((si->style == wxITALIC) && ( (!last_si) || (last_si->style != wxITALIC)))
134 html += "<i>";
135 else
136 if(si->style != wxITALIC && ( last_si && (last_si->style == wxITALIC)))
137 html += "</i>";
138
139 if(si->underline && ( (!last_si) || ! last_si->underline))
140 html += "<u>";
141 else if(si->underline == false && ( last_si && last_si->underline))
142 html += "</u>";
143
144 if(last_si)
145 delete last_si;
146 *lastStylePtr = si;
147 return html;
148 }
149
150
151
152 #define WXLO_IS_TEXT(type) \
153 ( (type == WXLO_TYPE_TEXT || type == WXLO_TYPE_LINEBREAK) \
154 || (type == WXLO_TYPE_CMD \
155 && mode == WXLO_EXPORT_AS_HTML))
156
157
158
159 wxLayoutExportObject *wxLayoutExport(wxLayoutList &list,
160 wxLayoutList::iterator &from,
161 wxLayoutExportMode mode)
162 {
163 if(from == list.end())
164 return NULL;
165
166 wxLayoutObjectType type = (*from)->GetType();
167 wxLayoutExportObject * export = new wxLayoutExportObject();
168
169 static wxLayoutStyleInfo *s_si = NULL;
170
171 if( mode == WXLO_EXPORT_AS_OBJECTS || ! WXLO_IS_TEXT(type)) // simple case
172 {
173 export->type = WXLO_EXPORT_OBJECT;
174 export->content.object = *from;
175 from++;
176 return export;
177 }
178
179 String *str = new String();
180
181 // text must be concatenated
182 while(from != list.end() && WXLO_IS_TEXT(type))
183 {
184 switch(type)
185 {
186 case WXLO_TYPE_TEXT:
187 *str += ((wxLayoutObjectText *)*from)->GetText();
188 break;
189 case WXLO_TYPE_LINEBREAK:
190 if(mode == WXLO_EXPORT_AS_HTML)
191 *str += "<br>";
192 *str += '\n';
193 break;
194 case WXLO_TYPE_CMD:
195 wxASSERT_MSG( mode == WXLO_EXPORT_AS_HTML,
196 "reached cmd object in text mode" );
197
198 *str += wxLayoutExportCmdAsHTML(*(wxLayoutObjectCmd const
199 *)*from, &s_si);
200 break;
201 default: // ignore icons
202 ;
203 }
204 from++;
205 if(from != list.end())
206 type = (*from)->GetType();
207 }
208 export->type = (mode == WXLO_EXPORT_AS_HTML) ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
209 export->content.text = str;
210 return export;
211 }