X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5d7836c47de679f03b1b7e738c77164c11bd3689..b21d67a04d99a82b74a587f8552b85241a177887:/src/richtext/richtextxml.cpp diff --git a/src/richtext/richtextxml.cpp b/src/richtext/richtextxml.cpp index 2e171c5d07..555ceac23c 100644 --- a/src/richtext/richtextxml.cpp +++ b/src/richtext/richtextxml.cpp @@ -1,10 +1,10 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: richtextxml.cpp +// Name: richtext/richtextxml.cpp // Purpose: XML and HTML I/O for wxRichTextCtrl // Author: Julian Smart -// Modified by: +// Modified by: // Created: 2005-09-30 -// RCS-ID: +// RCS-ID: $Id$ // Copyright: (c) Julian Smart // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -16,14 +16,14 @@ #pragma hdrstop #endif +#if wxUSE_RICHTEXT && wxUSE_XML + +#include "wx/richtext/richtextxml.h" + #ifndef WX_PRECOMP #include "wx/wx.h" #endif -#include "wx/image.h" - -#if wxUSE_RICHTEXT - #include "wx/filename.h" #include "wx/clipbrd.h" #include "wx/wfstream.h" @@ -32,12 +32,10 @@ #include "wx/txtstrm.h" #include "wx/xml/xml.h" -#include "wx/richtext/richtextxml.h" - IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler, wxRichTextFileHandler) #if wxUSE_STREAMS -bool wxRichTextXMLHandler::LoadFile(wxRichTextBuffer *buffer, wxInputStream& stream) +bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream) { if (!stream.IsOk()) return false; @@ -67,7 +65,7 @@ bool wxRichTextXMLHandler::LoadFile(wxRichTextBuffer *buffer, wxInputStream& str else ImportXML(buffer, child); } - + child = child->GetNext(); } } @@ -76,7 +74,7 @@ bool wxRichTextXMLHandler::LoadFile(wxRichTextBuffer *buffer, wxInputStream& str success = false; } } - + delete xmlDoc; buffer->UpdateRanges(); @@ -145,7 +143,7 @@ bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer* buffer, wxXmlNode* node) { int imageType = wxBITMAP_TYPE_PNG; wxString value = node->GetPropVal(wxT("imagetype"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) imageType = wxAtoi(value); wxString data; @@ -163,12 +161,12 @@ bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer* buffer, wxXmlNode* node) // wxLogDebug(data); dataChild = dataChild->GetNext(); } - + } imageChild = imageChild->GetNext(); } - if (!data.IsEmpty()) + if (!data.empty()) { wxRichTextImage* imageObj = new wxRichTextImage(para); para->AppendChild(imageObj); @@ -192,7 +190,7 @@ bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer* buffer, wxXmlNode* node) ImportXML(buffer, child); child = child->GetNext(); } - } + } return true; } @@ -242,7 +240,7 @@ wxString wxRichTextXMLHandler::GetNodeContent(wxXmlNode *node) wxString wxRichTextXMLHandler::GetParamValue(wxXmlNode *node, const wxString& param) { - if (param.IsEmpty()) + if (param.empty()) return GetNodeContent(node); else return GetNodeContent(GetParamNode(node, param)); @@ -257,14 +255,31 @@ wxString wxRichTextXMLHandler::GetText(wxXmlNode *node, const wxString& param, b return str1; } +// For use with earlier versions of wxWidgets +#ifndef WXUNUSED_IN_UNICODE +#if wxUSE_UNICODE +#define WXUNUSED_IN_UNICODE(x) WXUNUSED(x) +#else +#define WXUNUSED_IN_UNICODE(x) x +#endif +#endif + // write string to output: inline static void OutputString(wxOutputStream& stream, const wxString& str, - wxMBConv *convMem = NULL, wxMBConv *convFile = NULL) + wxMBConv *WXUNUSED_IN_UNICODE(convMem) = NULL, wxMBConv *convFile = NULL) { - if (str.IsEmpty()) return; + if (str.empty()) return; #if wxUSE_UNICODE - const wxWX2MBbuf buf(str.mb_str(convFile ? *convFile : wxConvUTF8)); - stream.Write((const char*)buf, strlen((const char*)buf)); + if (convFile) + { + const wxWX2MBbuf buf(str.mb_str(*convFile)); + stream.Write((const char*)buf, strlen((const char*)buf)); + } + else + { + const wxWX2MBbuf buf(str.mb_str(wxConvUTF8)); + stream.Write((const char*)buf, strlen((const char*)buf)); + } #else if ( convFile == NULL ) stream.Write(str.mb_str(), str.Len()); @@ -284,7 +299,7 @@ static void OutputStringEnt(wxOutputStream& stream, const wxString& str, wxString buf; size_t i, last, len; wxChar c; - + len = str.Len(); last = 0; for (i = 0; i < len; i++) @@ -330,6 +345,7 @@ static wxOutputStream& operator <<(wxOutputStream& stream, const wxString& s) return stream; } +#if 0 static wxOutputStream& operator <<(wxOutputStream& stream, long l) { wxString str; @@ -343,6 +359,7 @@ static wxOutputStream& operator <<(wxOutputStream& stream, const char c) str.Printf(wxT("%c"), c); return stream << str; } +#endif // Convert a colour to a 6-digit hex string static wxString ColourToHexString(const wxColour& col) @@ -359,17 +376,14 @@ static wxString ColourToHexString(const wxColour& col) // Convert 6-digit hex string to a colour wxColour HexStringToColour(const wxString& hex) { - unsigned int r = 0; - unsigned int g = 0; - unsigned int b = 0; - r = wxHexToDec(hex.Mid(0, 2)); - g = wxHexToDec(hex.Mid(2, 2)); - b = wxHexToDec(hex.Mid(4, 2)); + unsigned char r = (unsigned char)wxHexToDec(hex.Mid(0, 2)); + unsigned char g = (unsigned char)wxHexToDec(hex.Mid(2, 2)); + unsigned char b = (unsigned char)wxHexToDec(hex.Mid(4, 2)); return wxColour(r, g, b); } -bool wxRichTextXMLHandler::SaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) +bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) { if (!stream.IsOk()) return false; @@ -383,7 +397,7 @@ bool wxRichTextXMLHandler::SaveFile(wxRichTextBuffer *buffer, wxOutputStream& st wxString memencoding(wxT("ISO-8859-1")) ; #endif wxString s ; - + wxMBConv *convMem = NULL, *convFile = NULL; #if wxUSE_UNICODE convFile = new wxCSConv(fileencoding); @@ -394,7 +408,7 @@ bool wxRichTextXMLHandler::SaveFile(wxRichTextBuffer *buffer, wxOutputStream& st convMem = new wxCSConv(memencoding); } #endif - + s.Printf(wxT("\n"), (const wxChar*) version, (const wxChar*) fileencoding ); OutputString(stream, s, NULL, NULL); @@ -405,7 +419,7 @@ bool wxRichTextXMLHandler::SaveFile(wxRichTextBuffer *buffer, wxOutputStream& st OutputString(stream, wxT("\n") , NULL, NULL); OutputString(stream, wxT("\n"), NULL, NULL); - + delete convFile; delete convMem; @@ -433,11 +447,11 @@ bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem, OutputIndentation(stream, indent); stream << wxT("<") << objectName; - + wxString style = CreateStyle(obj.GetAttributes(), false); - + stream << style << wxT(">"); - + wxString str = text.GetText(); if (str.Length() > 0 && (str[0] == wxT(' ') || str[str.Length()-1] == wxT(' '))) { @@ -478,15 +492,15 @@ bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem, { OutputIndentation(stream, indent); stream << wxT("<") << objectName; - + bool isPara = false; if (objectName == wxT("paragraph") || objectName == wxT("paragraphlayout")) isPara = true; wxString style = CreateStyle(obj.GetAttributes(), isPara); - + stream << style << wxT(">"); - + wxRichTextCompositeObject& composite = (wxRichTextCompositeObject&) obj; size_t i; for (i = 0; i < composite.GetChildCount(); i++) @@ -527,7 +541,7 @@ wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx& attr, bool isPara str << wxT(" fontface=\"") << attr.GetFont().GetFaceName() << wxT("\""); } - if (!attr.GetCharacterStyleName().IsEmpty()) + if (!attr.GetCharacterStyleName().empty()) str << wxT(" charactertyle=\"") << wxString(attr.GetCharacterStyleName()) << wxT("\""); if (isPara) @@ -543,7 +557,7 @@ wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx& attr, bool isPara str << wxT(" bulletnumber=\"") << (int) attr.GetBulletNumber() << wxT("\""); str << wxT(" bulletsymbol=\"") << wxString(attr.GetBulletSymbol()) << wxT("\""); - if (!attr.GetParagraphStyleName().IsEmpty()) + if (!attr.GetParagraphStyleName().empty()) str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\""); } @@ -563,29 +577,29 @@ bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool is fontFacename = node->GetPropVal(wxT("fontface"), wxEmptyString); wxString value = node->GetPropVal(wxT("fontfamily"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) fontFamily = wxAtoi(value); value = node->GetPropVal(wxT("fontstyle"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) fontStyle = wxAtoi(value); value = node->GetPropVal(wxT("fontsize"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) fontSize = wxAtoi(value); value = node->GetPropVal(wxT("fontweight"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) fontWeight = wxAtoi(value); value = node->GetPropVal(wxT("fontunderlined"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) fontUnderlined = wxAtoi(value) != 0; attr.SetFont(* wxTheFontList->FindOrCreateFont(fontSize, fontFamily, fontStyle, fontWeight, fontUnderlined, fontFacename)); value = node->GetPropVal(wxT("textcolor"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) { if (value[0] == wxT('#')) attr.SetTextColour(HexStringToColour(value.Mid(1))); @@ -594,7 +608,7 @@ bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool is } value = node->GetPropVal(wxT("backgroundcolor"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) { if (value[0] == wxT('#')) attr.SetBackgroundColour(HexStringToColour(value.Mid(1))); @@ -603,56 +617,56 @@ bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool is } value = node->GetPropVal(wxT("characterstyle"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetCharacterStyleName(value); // Set paragraph attributes if (isPara) { value = node->GetPropVal(wxT("alignment"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetAlignment((wxTextAttrAlignment) wxAtoi(value)); int leftSubIndent = 0; int leftIndent = 0; value = node->GetPropVal(wxT("leftindent"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) leftIndent = wxAtoi(value); value = node->GetPropVal(wxT("leftsubindent"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) leftSubIndent = wxAtoi(value); attr.SetLeftIndent(leftIndent, leftSubIndent); value = node->GetPropVal(wxT("rightindent"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetRightIndent(wxAtoi(value)); value = node->GetPropVal(wxT("parspacingbefore"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetParagraphSpacingBefore(wxAtoi(value)); value = node->GetPropVal(wxT("parspacingafter"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetParagraphSpacingAfter(wxAtoi(value)); value = node->GetPropVal(wxT("linespacing"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetLineSpacing(wxAtoi(value)); value = node->GetPropVal(wxT("bulletstyle"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetBulletStyle(wxAtoi(value)); value = node->GetPropVal(wxT("bulletnumber"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetBulletNumber(wxAtoi(value)); value = node->GetPropVal(wxT("bulletsymbol"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetBulletSymbol(value[0]); value = node->GetPropVal(wxT("parstyle"), wxEmptyString); - if (!value.IsEmpty()) + if (!value.empty()) attr.SetParagraphStyleName(value); } @@ -674,7 +688,7 @@ bool wxRichTextHTMLHandler::CanHandle(const wxString& filename) const #if wxUSE_STREAMS -bool wxRichTextHTMLHandler::LoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream)) +bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream)) { return false; } @@ -683,7 +697,7 @@ bool wxRichTextHTMLHandler::LoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInput * We need to output only _changes_ in character formatting. */ -bool wxRichTextHTMLHandler::SaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) +bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) { buffer->Defragment(); @@ -719,7 +733,7 @@ bool wxRichTextHTMLHandler::SaveFile(wxRichTextBuffer *buffer, wxOutputStream& s } node2 = node2->GetNext(); - } + } OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, false); @@ -804,5 +818,4 @@ void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx& WXUNUS #endif #endif - // wxUSE_RICHTEXT - + // wxUSE_RICHTEXT && wxUSE_XML