]>
git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxlparser.cpp
1 /*-*- c++ -*-********************************************************
2 * wxlparser.h : parsers, import/export for wxLayoutList *
4 * (C) 1998,1999 by Karsten Ballüder (Ballueder@usa.net) *
7 *******************************************************************/
10 # pragma implementation "wxlparser.h"
13 #include "wx/wxprec.h"
22 # include "gui/wxllist.h"
23 # include "gui/wxlparser.h"
26 # include "wxlparser.h"
31 inline static bool IsEndOfLine(const wxChar
*p
)
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'));
39 void wxLayoutImportText(wxLayoutList
*list
, wxString
const &str
)
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
;
54 while( *cptr
&& !IsEndOfLine(cptr
) )
61 // check if it's the end of this line
62 if ( IsEndOfLine(cptr
) )
64 // if it was "\r\n", skip the following '\n'
69 else if(backup
== '\0') // reached end of string
76 wxString
wxLayoutExportCmdAsHTML(wxLayoutObjectCmd
const & cmd
,
77 wxLayoutStyleInfo
*styleInfo
,
80 static wxChar buffer
[20];
83 wxLayoutStyleInfo
*si
= cmd
.GetStyle();
92 wxSprintf(buffer
,_T("\"#%02X%02X%02X\""), si
->m_fg
.Red(),si
->m_fg
.Green(),si
->m_fg
.Blue());
98 html
+= _T(" bgcolor=");
99 wxSprintf(buffer
,_T("\"#%02X%02X%02X\""), si
->m_bg
.Red(),si
->m_bg
.Green(),si
->m_bg
.Blue());
107 html
+= _T(" face=\"Arial,Helvetica\""); break;
109 html
+= _T(" face=\"Times New Roman, Times\""); break;
111 html
+= _T(" face=\"Courier New, Courier\""); break;
116 size
= BASE_SIZE
; sizecount
= 0;
117 while(size
< si
->size
&& sizecount
< 5)
122 while(size
> si
->size
&& sizecount
> -5)
128 wxSprintf(buffer
,_T("%+1d"), sizecount
);
133 if(styleInfo
!= NULL
&& ! firstTime
)
134 html
= _T("</font>")+html
; // terminate any previous font command
136 if((si
->weight
== wxBOLD
) && ( (!styleInfo
) || (styleInfo
->weight
!= wxBOLD
)))
139 if(si
->weight
!= wxBOLD
&& ( styleInfo
&& (styleInfo
->weight
== wxBOLD
)))
142 if(si
->style
== wxSLANT
)
143 si
->style
= wxITALIC
; // the same for html
145 if((si
->style
== wxITALIC
) && ( (!styleInfo
) || (styleInfo
->style
!= wxITALIC
)))
148 if(si
->style
!= wxITALIC
&& ( styleInfo
&& (styleInfo
->style
== wxITALIC
)))
151 if(si
->underline
&& ( (!styleInfo
) || ! styleInfo
->underline
))
153 else if(si
->underline
== false && ( styleInfo
&& styleInfo
->underline
))
157 *styleInfo
= *si
; // update last style info
164 wxLayoutExportStatus::wxLayoutExportStatus(wxLayoutList
*list
)
166 m_si
= list
->GetDefaultStyleInfo();
167 m_line
= list
->GetFirstLine();
168 m_iterator
= m_line
->GetFirstObject();
174 #define WXLO_IS_TEXT(type) \
175 ( type == WXLO_TYPE_TEXT \
176 || (type == WXLO_TYPE_CMD \
177 && mode == WXLO_EXPORT_AS_HTML))
180 wxLayoutExportObject
*wxLayoutExport(wxLayoutExportStatus
*status
,
184 wxLayoutExportObject
* exp
;
186 if(status
->m_iterator
== NULLIT
) // end of line
188 if(!status
->m_line
|| status
->m_line
->GetNextLine() == NULL
)
189 // reached end of list
192 exp
= new wxLayoutExportObject();
193 wxLayoutObjectType type
;
194 if(status
->m_iterator
!= NULLIT
)
196 type
= (** status
->m_iterator
).GetType();
197 if( mode
== WXLO_EXPORT_AS_OBJECTS
|| ! WXLO_IS_TEXT(type
)) // simple case
199 exp
->type
= WXLO_EXPORT_OBJECT
;
200 exp
->content
.object
= *status
->m_iterator
;
201 status
->m_iterator
++;
206 { // iterator == NULLIT
207 if(mode
== WXLO_EXPORT_AS_OBJECTS
)
209 exp
->type
= WXLO_EXPORT_EMPTYLINE
;
210 exp
->content
.object
= NULL
; //empty line
211 status
->m_line
= status
->m_line
->GetNextLine();
213 status
->m_iterator
= status
->m_line
->GetFirstObject();
217 type
= WXLO_TYPE_TEXT
;
221 wxString
*str
= new wxString();
222 // text must be concatenated
225 while(status
->m_iterator
== NULLIT
)
227 if(mode
& WXLO_EXPORT_AS_HTML
)
229 if(flags
& WXLO_EXPORT_WITH_CRLF
)
234 status
->m_line
= status
->m_line
->GetNextLine();
236 status
->m_iterator
= status
->m_line
->GetFirstObject();
238 break; // end of list
240 if(! status
->m_line
) // reached end of list, fall through
242 type
= (** status
->m_iterator
).GetType();
243 if(type
== WXLO_TYPE_ICON
)
248 *str
+= ((wxLayoutObjectText
*)*status
->m_iterator
)->GetText();
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;
257 default: // ignore icons
260 status
->m_iterator
++;
262 exp
->type
= (mode
== WXLO_EXPORT_AS_HTML
)
263 ? WXLO_EXPORT_HTML
: WXLO_EXPORT_TEXT
;
264 exp
->content
.text
= str
;