+ * wxRichTextRange
+ * This stores beginning and end positions for a range of data.
+ */
+
+/// Limit this range to be within 'range'
+bool wxRichTextRange::LimitTo(const wxRichTextRange& range)
+{
+ if (m_start < range.m_start)
+ m_start = range.m_start;
+
+ if (m_end > range.m_end)
+ m_end = range.m_end;
+
+ return true;
+}
+
+/*!
+ * wxRichTextImage implementation
+ * This object represents an image.
+ */
+
+IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage, wxRichTextObject)
+
+wxRichTextImage::wxRichTextImage(const wxImage& image, wxRichTextObject* parent, wxRichTextAttr* charStyle):
+ wxRichTextObject(parent)
+{
+ m_imageBlock.MakeImageBlockDefaultQuality(image, wxBITMAP_TYPE_PNG);
+ if (charStyle)
+ SetAttributes(*charStyle);
+}
+
+wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent, wxRichTextAttr* charStyle):
+ wxRichTextObject(parent)
+{
+ m_imageBlock = imageBlock;
+ if (charStyle)
+ SetAttributes(*charStyle);
+}
+
+/// Create a cached image at the required size
+bool wxRichTextImage::LoadImageCache(wxDC& dc, bool resetCache)
+{
+ if (resetCache || !m_imageCache.IsOk() /* || m_imageCache.GetWidth() != size.x || m_imageCache.GetHeight() != size.y */)
+ {
+ if (!m_imageBlock.IsOk())
+ return false;
+
+ wxImage image;
+ m_imageBlock.Load(image);
+ if (!image.IsOk())
+ return false;
+
+ int width = image.GetWidth();
+ int height = image.GetHeight();
+
+ if (GetAttributes().GetTextBoxAttr().GetWidth().IsPresent() && GetAttributes().GetTextBoxAttr().GetWidth().GetValue() > 0)
+ {
+ if (GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ width = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetWidth().GetValue());
+ else
+ width = GetAttributes().GetTextBoxAttr().GetWidth().GetValue();
+ }
+ if (GetAttributes().GetTextBoxAttr().GetHeight().IsPresent() && GetAttributes().GetTextBoxAttr().GetHeight().GetValue() > 0)
+ {
+ if (GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ height = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetHeight().GetValue());
+ else
+ height = GetAttributes().GetTextBoxAttr().GetHeight().GetValue();
+ }
+
+ if (image.GetWidth() == width && image.GetHeight() == height)
+ m_imageCache = wxBitmap(image);
+ else
+ {
+ // If the original width and height is small, e.g. 400 or below,
+ // scale up and then down to improve image quality. This can make
+ // a big difference, with not much performance hit.
+ int upscaleThreshold = 400;
+ wxImage img;
+ if (image.GetWidth() <= upscaleThreshold || image.GetHeight() <= upscaleThreshold)
+ {
+ img = image.Scale(image.GetWidth()*2, image.GetHeight()*2);
+ img.Rescale(width, height, wxIMAGE_QUALITY_HIGH);
+ }
+ else
+ img = image.Scale(width, height, wxIMAGE_QUALITY_HIGH);
+ m_imageCache = wxBitmap(img);
+ }
+ }
+
+ return m_imageCache.IsOk();
+}
+
+/// Draw the item
+bool wxRichTextImage::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int WXUNUSED(descent), int WXUNUSED(style))
+{
+ // Don't need cached size AFAIK
+ // wxSize size = GetCachedSize();
+ if (!LoadImageCache(dc))
+ return false;
+
+ int y = rect.y + (rect.height - m_imageCache.GetHeight());
+
+ dc.DrawBitmap(m_imageCache, rect.x, y, true);
+
+ if (selectionRange.Contains(range.GetStart()))
+ {
+ wxCheckSetBrush(dc, *wxBLACK_BRUSH);
+ wxCheckSetPen(dc, *wxBLACK_PEN);
+ dc.SetLogicalFunction(wxINVERT);
+ dc.DrawRectangle(rect);
+ dc.SetLogicalFunction(wxCOPY);
+ }
+
+ return true;
+}
+
+/// Lay the item out
+bool wxRichTextImage::Layout(wxDC& dc, const wxRect& rect, int WXUNUSED(style))
+{
+ if (!LoadImageCache(dc))
+ return false;
+
+ SetCachedSize(wxSize(m_imageCache.GetWidth(), m_imageCache.GetHeight()));
+ SetPosition(rect.GetPosition());
+
+ return true;
+}
+
+/// Get/set the object size for the given range. Returns false if the range
+/// is invalid for this object.
+bool wxRichTextImage::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& WXUNUSED(descent), wxDC& dc, int WXUNUSED(flags), wxPoint WXUNUSED(position), wxArrayInt* partialExtents) const
+{
+ if (!range.IsWithin(GetRange()))
+ return false;
+
+ if (!((wxRichTextImage*)this)->LoadImageCache(dc))
+ {
+ size.x = 0; size.y = 0;
+ if (partialExtents)
+ partialExtents->Add(0);
+ return false;
+ }
+
+ int width = m_imageCache.GetWidth();
+ int height = m_imageCache.GetHeight();
+
+ if (partialExtents)
+ partialExtents->Add(width);
+
+ size.x = width;
+ size.y = height;
+
+ return true;
+}
+
+/// Copy
+void wxRichTextImage::Copy(const wxRichTextImage& obj)
+{
+ wxRichTextObject::Copy(obj);
+
+ m_imageBlock = obj.m_imageBlock;
+}
+
+/// Edit properties via a GUI
+bool wxRichTextImage::EditProperties(wxWindow* parent, wxRichTextBuffer* buffer)
+{
+ wxRichTextImageDialog imageDlg(wxGetTopLevelParent(parent));
+ imageDlg.SetImageObject(this, buffer);
+
+ if (imageDlg.ShowModal() == wxID_OK)
+ {
+ imageDlg.ApplyImageAttr();
+ return true;
+ }
+ else
+ return false;
+}
+
+/*!
+ * Utilities
+ *
+ */
+
+/// Compare two attribute objects
+bool wxTextAttrEq(const wxRichTextAttr& attr1, const wxRichTextAttr& attr2)
+{
+ return (attr1 == attr2);
+}
+
+// Partial equality test taking flags into account
+bool wxTextAttrEqPartial(const wxRichTextAttr& attr1, const wxRichTextAttr& attr2)
+{
+ return attr1.EqPartial(attr2);
+}
+
+/// Compare tabs
+bool wxRichTextTabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
+{
+ if (tabs1.GetCount() != tabs2.GetCount())
+ return false;
+
+ size_t i;
+ for (i = 0; i < tabs1.GetCount(); i++)
+ {
+ if (tabs1[i] != tabs2[i])
+ return false;
+ }
+ return true;
+}
+
+bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxRichTextAttr& style, wxRichTextAttr* compareWith)
+{
+ return destStyle.Apply(style, compareWith);
+}
+
+// Remove attributes
+bool wxRichTextRemoveStyle(wxRichTextAttr& destStyle, const wxRichTextAttr& style)
+{
+ return destStyle.RemoveStyle(style);
+}
+
+/// Combine two bitlists, specifying the bits of interest with separate flags.
+bool wxRichTextCombineBitlists(int& valueA, int valueB, int& flagsA, int flagsB)
+{
+ return wxRichTextAttr::CombineBitlists(valueA, valueB, flagsA, flagsB);
+}
+
+/// Compare two bitlists
+bool wxRichTextBitlistsEqPartial(int valueA, int valueB, int flags)
+{
+ return wxRichTextAttr::BitlistsEqPartial(valueA, valueB, flags);
+}
+
+/// Split into paragraph and character styles
+bool wxRichTextSplitParaCharStyles(const wxRichTextAttr& style, wxRichTextAttr& parStyle, wxRichTextAttr& charStyle)
+{
+ return wxRichTextAttr::SplitParaCharStyles(style, parStyle, charStyle);
+}
+
+/// Convert a decimal to Roman numerals
+wxString wxRichTextDecimalToRoman(long n)
+{
+ static wxArrayInt decimalNumbers;
+ static wxArrayString romanNumbers;
+
+ // Clean up arrays
+ if (n == -1)
+ {
+ decimalNumbers.Clear();
+ romanNumbers.Clear();
+ return wxEmptyString;
+ }
+
+ if (decimalNumbers.GetCount() == 0)
+ {
+ #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
+
+ wxRichTextAddDecRom(1000, wxT("M"));
+ wxRichTextAddDecRom(900, wxT("CM"));
+ wxRichTextAddDecRom(500, wxT("D"));
+ wxRichTextAddDecRom(400, wxT("CD"));
+ wxRichTextAddDecRom(100, wxT("C"));
+ wxRichTextAddDecRom(90, wxT("XC"));
+ wxRichTextAddDecRom(50, wxT("L"));
+ wxRichTextAddDecRom(40, wxT("XL"));
+ wxRichTextAddDecRom(10, wxT("X"));
+ wxRichTextAddDecRom(9, wxT("IX"));
+ wxRichTextAddDecRom(5, wxT("V"));
+ wxRichTextAddDecRom(4, wxT("IV"));
+ wxRichTextAddDecRom(1, wxT("I"));
+ }
+
+ int i = 0;
+ wxString roman;
+
+ while (n > 0 && i < 13)
+ {
+ if (n >= decimalNumbers[i])
+ {
+ n -= decimalNumbers[i];
+ roman += romanNumbers[i];
+ }
+ else
+ {
+ i ++;
+ }
+ }
+ if (roman.IsEmpty())
+ roman = wxT("0");
+ return roman;
+}
+
+/*!
+ * wxRichTextFileHandler
+ * Base class for file handlers
+ */
+
+IMPLEMENT_CLASS(wxRichTextFileHandler, wxObject)
+
+#if wxUSE_FFILE && wxUSE_STREAMS
+bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer *buffer, const wxString& filename)
+{
+ wxFFileInputStream stream(filename);
+ if (stream.Ok())
+ return LoadFile(buffer, stream);
+
+ return false;
+}
+
+bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& filename)
+{
+ wxFFileOutputStream stream(filename);
+ if (stream.Ok())
+ return SaveFile(buffer, stream);
+
+ return false;
+}
+#endif // wxUSE_FFILE && wxUSE_STREAMS
+
+/// Can we handle this filename (if using files)? By default, checks the extension.
+bool wxRichTextFileHandler::CanHandle(const wxString& filename) const
+{
+ wxString path, file, ext;
+ wxFileName::SplitPath(filename, & path, & file, & ext);
+
+ return (ext.Lower() == GetExtension());
+}
+
+/*!
+ * wxRichTextTextHandler
+ * Plain text handler
+ */
+
+IMPLEMENT_CLASS(wxRichTextPlainTextHandler, wxRichTextFileHandler)
+
+#if wxUSE_STREAMS
+bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
+{
+ if (!stream.IsOk())
+ return false;
+
+ wxString str;
+ int lastCh = 0;
+
+ while (!stream.Eof())
+ {
+ int ch = stream.GetC();
+
+ if (!stream.Eof())
+ {
+ if (ch == 10 && lastCh != 13)
+ str += wxT('\n');
+
+ if (ch > 0 && ch != 10)
+ str += wxChar(ch);
+
+ lastCh = ch;
+ }
+ }
+
+ buffer->ResetAndClearCommands();
+ buffer->Clear();
+ buffer->AddParagraphs(str);
+ buffer->UpdateRanges();
+
+ return true;
+}
+
+bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
+{
+ if (!stream.IsOk())
+ return false;
+
+ wxString text = buffer->GetText();
+
+ wxString newLine = wxRichTextLineBreakChar;
+ text.Replace(newLine, wxT("\n"));
+
+ wxCharBuffer buf = text.ToAscii();
+
+ stream.Write((const char*) buf, text.length());
+ return true;
+}
+#endif // wxUSE_STREAMS
+
+/*
+ * Stores information about an image, in binary in-memory form
+ */
+
+wxRichTextImageBlock::wxRichTextImageBlock()
+{
+ Init();
+}
+
+wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock& block):wxObject()
+{
+ Init();
+ Copy(block);
+}
+
+wxRichTextImageBlock::~wxRichTextImageBlock()
+{
+ wxDELETEA(m_data);
+}
+
+void wxRichTextImageBlock::Init()
+{
+ m_data = NULL;
+ m_dataSize = 0;
+ m_imageType = wxBITMAP_TYPE_INVALID;
+}
+
+void wxRichTextImageBlock::Clear()
+{
+ wxDELETEA(m_data);
+ m_dataSize = 0;
+ m_imageType = wxBITMAP_TYPE_INVALID;
+}
+
+
+// Load the original image into a memory block.
+// If the image is not a JPEG, we must convert it into a JPEG
+// to conserve space.
+// If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
+// load the image a 2nd time.
+
+bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, wxBitmapType imageType,
+ wxImage& image, bool convertToJPEG)
+{
+ m_imageType = imageType;
+
+ wxString filenameToRead(filename);
+ bool removeFile = false;
+
+ if (imageType == wxBITMAP_TYPE_INVALID)
+ return false; // Could not determine image type
+
+ if ((imageType != wxBITMAP_TYPE_JPEG) && convertToJPEG)
+ {
+ wxString tempFile =
+ wxFileName::CreateTempFileName(_("image"));
+
+ wxASSERT(!tempFile.IsEmpty());
+
+ image.SaveFile(tempFile, wxBITMAP_TYPE_JPEG);
+ filenameToRead = tempFile;
+ removeFile = true;
+
+ m_imageType = wxBITMAP_TYPE_JPEG;
+ }
+ wxFile file;
+ if (!file.Open(filenameToRead))
+ return false;
+
+ m_dataSize = (size_t) file.Length();
+ file.Close();
+
+ if (m_data)
+ delete[] m_data;
+ m_data = ReadBlock(filenameToRead, m_dataSize);
+
+ if (removeFile)
+ wxRemoveFile(filenameToRead);
+
+ return (m_data != NULL);
+}
+
+// Make an image block from the wxImage in the given
+// format.
+bool wxRichTextImageBlock::MakeImageBlock(wxImage& image, wxBitmapType imageType, int quality)
+{
+ image.SetOption(wxT("quality"), quality);
+
+ if (imageType == wxBITMAP_TYPE_INVALID)
+ return false; // Could not determine image type
+
+ return DoMakeImageBlock(image, imageType);
+}
+
+// Uses a const wxImage for efficiency, but can't set quality (only relevant for JPEG)
+bool wxRichTextImageBlock::MakeImageBlockDefaultQuality(const wxImage& image, wxBitmapType imageType)
+{
+ if (imageType == wxBITMAP_TYPE_INVALID)
+ return false; // Could not determine image type
+
+ return DoMakeImageBlock(image, imageType);
+}
+
+// Makes the image block
+bool wxRichTextImageBlock::DoMakeImageBlock(const wxImage& image, wxBitmapType imageType)
+{
+ wxMemoryOutputStream memStream;
+ if (!image.SaveFile(memStream, imageType))
+ {
+ return false;
+ }
+
+ unsigned char* block = new unsigned char[memStream.GetSize()];
+ if (!block)
+ return NULL;
+
+ if (m_data)
+ delete[] m_data;
+ m_data = block;
+
+ m_imageType = imageType;
+ m_dataSize = memStream.GetSize();
+
+ memStream.CopyTo(m_data, m_dataSize);
+
+ return (m_data != NULL);
+}
+
+// Write to a file
+bool wxRichTextImageBlock::Write(const wxString& filename)
+{
+ return WriteBlock(filename, m_data, m_dataSize);
+}
+
+void wxRichTextImageBlock::Copy(const wxRichTextImageBlock& block)
+{
+ m_imageType = block.m_imageType;
+ wxDELETEA(m_data);
+ m_dataSize = block.m_dataSize;
+ if (m_dataSize == 0)
+ return;
+
+ m_data = new unsigned char[m_dataSize];
+ unsigned int i;
+ for (i = 0; i < m_dataSize; i++)
+ m_data[i] = block.m_data[i];
+}
+
+//// Operators
+void wxRichTextImageBlock::operator=(const wxRichTextImageBlock& block)
+{
+ Copy(block);
+}
+
+// Load a wxImage from the block
+bool wxRichTextImageBlock::Load(wxImage& image)
+{
+ if (!m_data)
+ return false;
+
+ // Read in the image.
+#if wxUSE_STREAMS
+ wxMemoryInputStream mstream(m_data, m_dataSize);
+ bool success = image.LoadFile(mstream, GetImageType());
+#else
+ wxString tempFile = wxFileName::CreateTempFileName(_("image"));
+ wxASSERT(!tempFile.IsEmpty());
+
+ if (!WriteBlock(tempFile, m_data, m_dataSize))
+ {
+ return false;
+ }
+ success = image.LoadFile(tempFile, GetImageType());
+ wxRemoveFile(tempFile);
+#endif
+
+ return success;
+}
+
+// Write data in hex to a stream
+bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream)
+{
+ const int bufSize = 512;
+ char buf[bufSize+1];
+
+ int left = m_dataSize;
+ int n, i, j;
+ j = 0;
+ while (left > 0)
+ {
+ if (left*2 > bufSize)
+ {
+ n = bufSize; left -= (bufSize/2);
+ }
+ else
+ {
+ n = left*2; left = 0;
+ }
+
+ char* b = buf;
+ for (i = 0; i < (n/2); i++)
+ {
+ wxDecToHex(m_data[j], b, b+1);
+ b += 2; j ++;
+ }
+
+ buf[n] = 0;
+ stream.Write((const char*) buf, n);
+ }
+ return true;
+}
+
+// Read data in hex from a stream
+bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, wxBitmapType imageType)
+{
+ int dataSize = length/2;
+
+ if (m_data)
+ delete[] m_data;
+
+ // create a null terminated temporary string:
+ char str[3];
+ str[2] = '\0';
+
+ m_data = new unsigned char[dataSize];
+ int i;
+ for (i = 0; i < dataSize; i ++)
+ {
+ str[0] = (char)stream.GetC();
+ str[1] = (char)stream.GetC();
+
+ m_data[i] = (unsigned char)wxHexToDec(str);
+ }
+
+ m_dataSize = dataSize;
+ m_imageType = imageType;
+
+ return true;
+}
+
+// Allocate and read from stream as a block of memory
+unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream& stream, size_t size)
+{
+ unsigned char* block = new unsigned char[size];
+ if (!block)
+ return NULL;
+
+ stream.Read(block, size);
+
+ return block;
+}
+
+unsigned char* wxRichTextImageBlock::ReadBlock(const wxString& filename, size_t size)
+{
+ wxFileInputStream stream(filename);
+ if (!stream.Ok())
+ return NULL;
+
+ return ReadBlock(stream, size);
+}
+
+// Write memory block to stream
+bool wxRichTextImageBlock::WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size)
+{
+ stream.Write((void*) block, size);
+ return stream.IsOk();
+
+}
+
+// Write memory block to file
+bool wxRichTextImageBlock::WriteBlock(const wxString& filename, unsigned char* block, size_t size)
+{
+ wxFileOutputStream outStream(filename);
+ if (!outStream.Ok())
+ return false;
+
+ return WriteBlock(outStream, block, size);
+}
+
+// Gets the extension for the block's type
+wxString wxRichTextImageBlock::GetExtension() const
+{
+ wxImageHandler* handler = wxImage::FindHandler(GetImageType());
+ if (handler)
+ return handler->GetExtension();
+ else
+ return wxEmptyString;
+}
+
+#if wxUSE_DATAOBJ
+
+/*!
+ * The data object for a wxRichTextBuffer