]>
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 char *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 char * cptr
= (char *)str
.c_str(); // const_cast
47 const char * begin
= cptr
;
53 while( *cptr
&& !IsEndOfLine(cptr
) )
60 // check if it's the end of this line
61 if ( IsEndOfLine(cptr
) )
63 // if it was "\r\n", skip the following '\n'
68 else if(backup
== '\0') // reached end of string
75 wxString
wxLayoutExportCmdAsHTML(wxLayoutObjectCmd
const & cmd
,
76 wxLayoutStyleInfo
*styleInfo
,
79 static char buffer
[20];
82 wxLayoutStyleInfo
*si
= cmd
.GetStyle();
91 sprintf(buffer
,"\"#%02X%02X%02X\"", si
->m_fg
.Red(),si
->m_fg
.Green(),si
->m_fg
.Blue());
98 sprintf(buffer
,"\"#%02X%02X%02X\"", si
->m_bg
.Red(),si
->m_bg
.Green(),si
->m_bg
.Blue());
106 html
+= " face=\"Arial,Helvetica\""; break;
108 html
+= " face=\"Times New Roman, Times\""; break;
110 html
+= " face=\"Courier New, Courier\""; break;
115 size
= BASE_SIZE
; sizecount
= 0;
116 while(size
< si
->size
&& sizecount
< 5)
121 while(size
> si
->size
&& sizecount
> -5)
127 sprintf(buffer
,"%+1d", sizecount
);
132 if(styleInfo
!= NULL
&& ! firstTime
)
133 html
="</font>"+html
; // terminate any previous font command
135 if((si
->weight
== wxBOLD
) && ( (!styleInfo
) || (styleInfo
->weight
!= wxBOLD
)))
138 if(si
->weight
!= wxBOLD
&& ( styleInfo
&& (styleInfo
->weight
== wxBOLD
)))
141 if(si
->style
== wxSLANT
)
142 si
->style
= wxITALIC
; // the same for html
144 if((si
->style
== wxITALIC
) && ( (!styleInfo
) || (styleInfo
->style
!= wxITALIC
)))
147 if(si
->style
!= wxITALIC
&& ( styleInfo
&& (styleInfo
->style
== wxITALIC
)))
150 if(si
->underline
&& ( (!styleInfo
) || ! styleInfo
->underline
))
152 else if(si
->underline
== false && ( styleInfo
&& styleInfo
->underline
))
156 *styleInfo
= *si
; // update last style info
163 wxLayoutExportStatus::wxLayoutExportStatus(wxLayoutList
*list
)
165 m_si
= list
->GetDefaultStyleInfo();
166 m_line
= list
->GetFirstLine();
167 m_iterator
= m_line
->GetFirstObject();
173 #define WXLO_IS_TEXT(type) \
174 ( type == WXLO_TYPE_TEXT \
175 || (type == WXLO_TYPE_CMD \
176 && mode == WXLO_EXPORT_AS_HTML))
179 wxLayoutExportObject
*wxLayoutExport(wxLayoutExportStatus
*status
,
183 wxLayoutExportObject
* exp
;
185 if(status
->m_iterator
== NULLIT
) // end of line
187 if(!status
->m_line
|| status
->m_line
->GetNextLine() == NULL
)
188 // reached end of list
191 exp
= new wxLayoutExportObject();
192 wxLayoutObjectType type
;
193 if(status
->m_iterator
!= NULLIT
)
195 type
= (** status
->m_iterator
).GetType();
196 if( mode
== WXLO_EXPORT_AS_OBJECTS
|| ! WXLO_IS_TEXT(type
)) // simple case
198 exp
->type
= WXLO_EXPORT_OBJECT
;
199 exp
->content
.object
= *status
->m_iterator
;
200 status
->m_iterator
++;
205 { // iterator == NULLIT
206 if(mode
== WXLO_EXPORT_AS_OBJECTS
)
208 exp
->type
= WXLO_EXPORT_EMPTYLINE
;
209 exp
->content
.object
= NULL
; //empty line
210 status
->m_line
= status
->m_line
->GetNextLine();
212 status
->m_iterator
= status
->m_line
->GetFirstObject();
216 type
= WXLO_TYPE_TEXT
;
219 wxString
*str
= new wxString();
220 // text must be concatenated
223 while(status
->m_iterator
== NULLIT
)
225 if(mode
& WXLO_EXPORT_AS_HTML
)
227 if(flags
& WXLO_EXPORT_WITH_CRLF
)
232 status
->m_line
= status
->m_line
->GetNextLine();
234 status
->m_iterator
= status
->m_line
->GetFirstObject();
236 break; // end of list
238 if(! status
->m_line
) // reached end of list, fall through
240 type
= (** status
->m_iterator
).GetType();
241 if(type
== WXLO_TYPE_ICON
)
246 *str
+= ((wxLayoutObjectText
*)*status
->m_iterator
)->GetText();
249 if(mode
== WXLO_EXPORT_AS_HTML
)
250 *str
+= wxLayoutExportCmdAsHTML(
251 *(wxLayoutObjectCmd
const *)*status
->m_iterator
,
252 & status
->m_si
, status
->m_FirstTime
);
253 status
->m_FirstTime
= FALSE
;
255 default: // ignore icons
258 status
->m_iterator
++;
260 exp
->type
= (mode
== WXLO_EXPORT_AS_HTML
)
261 ? WXLO_EXPORT_HTML
: WXLO_EXPORT_TEXT
;
262 exp
->content
.text
= str
;