]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxlparser.cpp
Type casting warning fixes.
[wxWidgets.git] / samples / richedit / wxlparser.cpp
1 /*-*- c++ -*-********************************************************
2 * wxlparser.h : parsers, import/export for wxLayoutList *
3 * *
4 * (C) 1998,1999 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 "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 # pragma hdrstop
17 #endif
18
19 #include "Mpch.h"
20
21 #ifdef M_PREFIX
22 # include "gui/wxllist.h"
23 # include "gui/wxlparser.h"
24 #else
25 # include "wxllist.h"
26 # include "wxlparser.h"
27 #endif
28
29 #define BASE_SIZE 12
30
31 inline static bool IsEndOfLine(const wxChar *p)
32 {
33 // the end of line is either just '\n' or "\r\n" - we understand both (even
34 // though the second is used only under DOS/Windows) to be able to import
35 // DOS text files even under Unix
36 return (*p == '\n') || ((*p == '\r') && (*(p + 1) == '\n'));
37 }
38
39 void wxLayoutImportText(wxLayoutList *list, wxString const &str)
40 {
41 if ( !str )
42 return;
43
44 // we change the string only temporarily inside this function
45 // VZ: I still don't like it... the string data may be shared...
46 wxChar * cptr = (wxChar *)str.c_str(); // const_cast
47 const wxChar * begin = cptr;
48 wxUnusedVar(begin);
49 wxChar backup;
50
51 for(;;)
52 {
53 begin = cptr;
54 while( *cptr && !IsEndOfLine(cptr) )
55 cptr++;
56 backup = *cptr;
57 *cptr = '\0';
58 list->Insert(begin);
59 *cptr = backup;
60
61 // check if it's the end of this line
62 if ( IsEndOfLine(cptr) )
63 {
64 // if it was "\r\n", skip the following '\n'
65 if ( *cptr == '\r' )
66 cptr++;
67 list->LineBreak();
68 }
69 else if(backup == '\0') // reached end of string
70 break;
71 cptr++;
72 }
73 }
74
75 static
76 wxString wxLayoutExportCmdAsHTML(wxLayoutObjectCmd const & cmd,
77 wxLayoutStyleInfo *styleInfo,
78 bool firstTime)
79 {
80 static wxChar buffer[20];
81 wxString html;
82
83 wxLayoutStyleInfo *si = cmd.GetStyle();
84
85 int size, sizecount;
86
87 html += _T("<font ");
88
89 if(si->m_fg_valid)
90 {
91 html += _T("color=");
92 wxSprintf(buffer,_T("\"#%02X%02X%02X\""), si->m_fg.Red(),si->m_fg.Green(),si->m_fg.Blue());
93 html += buffer;
94 }
95
96 if(si->m_bg_valid)
97 {
98 html += _T(" bgcolor=");
99 wxSprintf(buffer,_T("\"#%02X%02X%02X\""), si->m_bg.Red(),si->m_bg.Green(),si->m_bg.Blue());
100 html += buffer;
101 }
102
103 switch(si->family)
104 {
105 case wxSWISS:
106 case wxMODERN:
107 html += _T(" face=\"Arial,Helvetica\""); break;
108 case wxROMAN:
109 html += _T(" face=\"Times New Roman, Times\""); break;
110 case wxTELETYPE:
111 html += _T(" face=\"Courier New, Courier\""); break;
112 default:
113 ;
114 }
115
116 size = BASE_SIZE; sizecount = 0;
117 while(size < si->size && sizecount < 5)
118 {
119 sizecount ++;
120 size = (size*12)/10;
121 }
122 while(size > si->size && sizecount > -5)
123 {
124 sizecount --;
125 size = (size*10)/12;
126 }
127 html += _T("size=");
128 wxSprintf(buffer,_T("%+1d"), sizecount);
129 html += buffer;
130
131 html += _T(">");
132
133 if(styleInfo != NULL && ! firstTime)
134 html = _T("</font>")+html; // terminate any previous font command
135
136 if((si->weight == wxBOLD) && ( (!styleInfo) || (styleInfo->weight != wxBOLD)))
137 html += _T("<b>");
138 else
139 if(si->weight != wxBOLD && ( styleInfo && (styleInfo->weight == wxBOLD)))
140 html += _T("</b>");
141
142 if(si->style == wxSLANT)
143 si->style = wxITALIC; // the same for html
144
145 if((si->style == wxITALIC) && ( (!styleInfo) || (styleInfo->style != wxITALIC)))
146 html += _T("<i>");
147 else
148 if(si->style != wxITALIC && ( styleInfo && (styleInfo->style == wxITALIC)))
149 html += _T("</i>");
150
151 if(si->underline && ( (!styleInfo) || ! styleInfo->underline))
152 html += _T("<u>");
153 else if(si->underline == false && ( styleInfo && styleInfo->underline))
154 html += _T("</u>");
155
156
157 *styleInfo = *si; // update last style info
158
159 return html;
160 }
161
162
163
164 wxLayoutExportStatus::wxLayoutExportStatus(wxLayoutList *list)
165 {
166 m_si = list->GetDefaultStyleInfo();
167 m_line = list->GetFirstLine();
168 m_iterator = m_line->GetFirstObject();
169 m_FirstTime = true;
170 }
171
172
173
174 #define WXLO_IS_TEXT(type) \
175 ( type == WXLO_TYPE_TEXT \
176 || (type == WXLO_TYPE_CMD \
177 && mode == WXLO_EXPORT_AS_HTML))
178
179
180 wxLayoutExportObject *wxLayoutExport(wxLayoutExportStatus *status,
181 int mode, int flags)
182 {
183 wxASSERT(status);
184 wxLayoutExportObject * exp;
185
186 if(status->m_iterator == NULLIT) // end of line
187 {
188 if(!status->m_line || status->m_line->GetNextLine() == NULL)
189 // reached end of list
190 return NULL;
191 }
192 exp = new wxLayoutExportObject();
193 wxLayoutObjectType type;
194 if(status->m_iterator != NULLIT)
195 {
196 type = (** status->m_iterator).GetType();
197 if( mode == WXLO_EXPORT_AS_OBJECTS || ! WXLO_IS_TEXT(type)) // simple case
198 {
199 exp->type = WXLO_EXPORT_OBJECT;
200 exp->content.object = *status->m_iterator;
201 status->m_iterator++;
202 return exp;
203 }
204 }
205 else
206 { // iterator == NULLIT
207 if(mode == WXLO_EXPORT_AS_OBJECTS)
208 {
209 exp->type = WXLO_EXPORT_EMPTYLINE;
210 exp->content.object = NULL; //empty line
211 status->m_line = status->m_line->GetNextLine();
212 if(status->m_line)
213 status->m_iterator = status->m_line->GetFirstObject();
214 return exp;
215 }
216 else
217 type = WXLO_TYPE_TEXT;
218 }
219 wxUnusedVar(type);
220
221 wxString *str = new wxString();
222 // text must be concatenated
223 for(;;)
224 {
225 while(status->m_iterator == NULLIT)
226 {
227 if(mode & WXLO_EXPORT_AS_HTML)
228 *str += _T("<br>");
229 if(flags & WXLO_EXPORT_WITH_CRLF)
230 *str += _T("\r\n");
231 else
232 *str += '\n';
233
234 status->m_line = status->m_line->GetNextLine();
235 if(status->m_line)
236 status->m_iterator = status->m_line->GetFirstObject();
237 else
238 break; // end of list
239 }
240 if(! status->m_line) // reached end of list, fall through
241 break;
242 type = (** status->m_iterator).GetType();
243 if(type == WXLO_TYPE_ICON)
244 break;
245 switch(type)
246 {
247 case WXLO_TYPE_TEXT:
248 *str += ((wxLayoutObjectText *)*status->m_iterator)->GetText();
249 break;
250 case WXLO_TYPE_CMD:
251 if(mode == WXLO_EXPORT_AS_HTML)
252 *str += wxLayoutExportCmdAsHTML(
253 *(wxLayoutObjectCmd const *)*status->m_iterator,
254 & status->m_si, status->m_FirstTime);
255 status->m_FirstTime = false;
256 break;
257 default: // ignore icons
258 ;
259 }
260 status->m_iterator++;
261 }
262 exp->type = (mode == WXLO_EXPORT_AS_HTML)
263 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
264 exp->content.text = str;
265 return exp;
266 }
267