]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlparser.cpp
wxFrame::GetClientSize -> DoGetClientSize
[wxWidgets.git] / user / wxLayout / 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 "Mpch.h"
14 #ifdef M_PREFIX
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, int mode)
25 {
26 // in addition to Unix EOL convention we also (but not instead) understand
27 // the DOS one under Windows
28 return
29 (mode == WXLO_EXPORT_WITH_CRLF) ?
30 ((*p == '\r') && (*(p + 1) == '\n'))
31 :
32 (((*p == '\r') && (*(p + 1) == '\n'))||(*p == '\n'));
33 }
34
35 void wxLayoutImportText(wxLayoutList *list, wxString const &str, int withflag)
36 {
37 if(str.Length() == 0)
38 return;
39 char * cptr = (char *)str.c_str(); // string gets changed only temporarily
40 const char * begin = cptr;
41 char backup;
42
43 for(;;)
44 {
45 begin = cptr;
46 while( *cptr && !IsEndOfLine(cptr, withflag) )
47 cptr++;
48 backup = *cptr;
49 *cptr = '\0';
50 list->Insert(begin);
51 *cptr = backup;
52
53 // check if it's the end of this line
54 if ( IsEndOfLine(cptr, withflag) )
55 {
56 // if it was "\r\n", skip the following '\n'
57 if ( *cptr == '\r' )
58 cptr++;
59 list->LineBreak();
60 }
61 else if(backup == '\0') // reached end of string
62 break;
63 cptr++;
64 }
65 }
66
67 static
68 wxString wxLayoutExportCmdAsHTML(wxLayoutObjectCmd const & cmd,
69 wxLayoutStyleInfo *styleInfo)
70 {
71 static char buffer[20];
72 wxString html;
73
74 wxLayoutStyleInfo si;
75 cmd.GetStyle(&si);
76
77 int size, sizecount;
78
79 html += "<font ";
80
81 html +="color=";
82 sprintf(buffer,"\"#%02X%02X%02X\"", si.fg_red,si.fg_green,si.fg_blue);
83 html += buffer;
84
85
86 html += " bgcolor=";
87 sprintf(buffer,"\"#%02X%02X%02X\"", si.bg_red,si.bg_green,si.bg_blue);
88 html += buffer;
89
90 switch(si.family)
91 {
92 case wxSWISS:
93 case wxMODERN:
94 html += " face=\"Arial,Helvetica\""; break;
95 case wxROMAN:
96 html += " face=\"Times New Roman, Times\""; break;
97 case wxTELETYPE:
98 html += " face=\"Courier New, Courier\""; break;
99 default:
100 ;
101 }
102
103 size = BASE_SIZE; sizecount = 0;
104 while(size < si.size && sizecount < 5)
105 {
106 sizecount ++;
107 size = (size*12)/10;
108 }
109 while(size > si.size && sizecount > -5)
110 {
111 sizecount --;
112 size = (size*10)/12;
113 }
114 html += "size=";
115 sprintf(buffer,"%+1d", sizecount);
116 html += buffer;
117
118 html +=">";
119
120 if(styleInfo != NULL)
121 html ="</font>"+html; // terminate any previous font command
122
123 if((si.weight == wxBOLD) && ( (!styleInfo) || (styleInfo->weight != wxBOLD)))
124 html += "<b>";
125 else
126 if(si.weight != wxBOLD && ( styleInfo && (styleInfo->weight == wxBOLD)))
127 html += "</b>";
128
129 if(si.style == wxSLANT)
130 si.style = wxITALIC; // the same for html
131
132 if((si.style == wxITALIC) && ( (!styleInfo) || (styleInfo->style != wxITALIC)))
133 html += "<i>";
134 else
135 if(si.style != wxITALIC && ( styleInfo && (styleInfo->style == wxITALIC)))
136 html += "</i>";
137
138 if(si.underline && ( (!styleInfo) || ! styleInfo->underline))
139 html += "<u>";
140 else if(si.underline == false && ( styleInfo && styleInfo->underline))
141 html += "</u>";
142
143
144 *styleInfo = si; // update last style info
145
146 return html;
147 }
148
149
150
151 wxLayoutExportStatus::wxLayoutExportStatus(wxLayoutList *list,
152 wxPoint fromPos,
153 wxPoint toPos)
154 {
155 list->GetDefaults()->GetStyle(&m_si);
156 m_line = list->GetFirstLine();
157 m_iterator = m_line->GetFirstObject();
158 m_fromPos = fromPos;
159 m_toPos = toPos;
160
161 if(m_fromPos.x != -1)
162 {
163 while(m_line && m_line->GetLineNumber() != m_fromPos.y)
164 m_line = m_line->GetNextLine();
165 wxASSERT(m_line);
166 CoordType dummy;
167 m_iterator = m_line->FindObject(fromPos.x, &dummy);
168 }
169 }
170
171
172
173 #define WXLO_IS_TEXT(type) \
174 ( type == WXLO_TYPE_TEXT \
175 || (type == WXLO_TYPE_CMD \
176 && mode == WXLO_EXPORT_AS_HTML))
177
178
179
180 extern const wxPoint wxLayoutExportNoPosition = wxPoint(-1,-1);
181
182 wxLayoutExportObject *wxLayoutExport(wxLayoutExportStatus *status,
183 int mode, int flags)
184 {
185 wxASSERT(status);
186 wxLayoutExportObject * export;
187
188 if(status->m_iterator == NULLIT) // end of line
189 {
190 if(!status->m_line || status->m_line->GetNextLine() == NULL)
191 // reached end of list
192 return NULL;
193 }
194 export = new wxLayoutExportObject();
195 wxLayoutObjectType type;
196 if(status->m_iterator != NULLIT)
197 {
198 type = (** status->m_iterator).GetType();
199 if( mode == WXLO_EXPORT_AS_OBJECTS || ! WXLO_IS_TEXT(type)) // simple case
200 {
201 export->type = WXLO_EXPORT_OBJECT;
202 export->content.object = *status->m_iterator;
203 status->m_iterator++;
204 return export;
205 }
206 }
207 else
208 { // iterator == NULLIT
209 if(mode == WXLO_EXPORT_AS_OBJECTS)
210 {
211 export->type = WXLO_EXPORT_EMPTYLINE;
212 export->content.object = NULL; //empty line
213 status->m_line = status->m_line->GetNextLine();
214 if(status->m_line)
215 status->m_iterator = status->m_line->GetFirstObject();
216 return export;
217 }
218 else
219 type = WXLO_TYPE_TEXT;
220 }
221
222 wxString *str = new wxString();
223 // text must be concatenated
224 for(;;)
225 {
226 while(status->m_iterator == NULLIT)
227 {
228 if(flags & WXLO_EXPORT_AS_HTML)
229 *str += "<br>";
230 if(flags & WXLO_EXPORT_WITH_CRLF)
231 *str += "\r\n";
232 else
233 *str += '\n';
234
235 status->m_line = status->m_line->GetNextLine();
236 if(status->m_line)
237 status->m_iterator = status->m_line->GetFirstObject();
238 else
239 break; // end of list
240 }
241 if(! status->m_line) // reached end of list, fall through
242 break;
243 type = (** status->m_iterator).GetType();
244 if(type == WXLO_TYPE_ICON)
245 break;
246 switch(type)
247 {
248 case WXLO_TYPE_TEXT:
249 *str += ((wxLayoutObjectText *)*status->m_iterator)->GetText();
250 break;
251 case WXLO_TYPE_CMD:
252 wxASSERT_MSG( mode == WXLO_EXPORT_AS_HTML,
253 "reached cmd object in text mode" );
254
255 *str += wxLayoutExportCmdAsHTML(*(wxLayoutObjectCmd const
256 *)*status->m_iterator, & status->m_si);
257 break;
258 default: // ignore icons
259 ;
260 }
261 status->m_iterator++;
262 }
263
264 export->type = (mode == WXLO_EXPORT_AS_HTML)
265 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
266 export->content.text = str;
267 return export;
268 }
269