/////////////////////////////////////////////////////////////////////////////
-// Name: richedit.cpp
+// Name: samples/richtext/richtext.cpp
// Purpose: wxWidgets rich text editor sample
// Author: Julian Smart
// Modified by:
#include "bitmaps/indentless.xpm"
#include "bitmaps/indentmore.xpm"
-#include "richtextctrl.h"
-#include "richtextstyles.h"
-#include "richtextxml.h"
+#include "wx/richtext/richtextctrl.h"
+#include "wx/richtext/richtextstyles.h"
+#include "wx/richtext/richtextxml.h"
+#include "wx/richtext/richtexthtml.h"
// ----------------------------------------------------------------------------
// resources
#if wxUSE_LIBPNG
wxImage::AddHandler( new wxPNGHandler );
#endif
-
+
#if wxUSE_LIBJPEG
wxImage::AddHandler( new wxJPEGHandler );
#endif
wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|
wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER);
normalPara->SetStyle(normalAttr);
-
+
m_styleSheet->AddParagraphStyle(normalPara);
wxRichTextParagraphStyleDefinition* indentedPara = new wxRichTextParagraphStyleDefinition(wxT("Indented"));
// We only want to affect indentation
indentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
indentedPara->SetStyle(indentedAttr);
-
+
m_styleSheet->AddParagraphStyle(indentedPara);
wxRichTextParagraphStyleDefinition* flIndentedPara = new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
// We only want to affect indentation
flIndentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
flIndentedPara->SetStyle(flIndentedAttr);
-
+
m_styleSheet->AddParagraphStyle(flIndentedPara);
// Character styles
// We only want to affect boldness
boldAttr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
boldDef->SetStyle(boldAttr);
-
+
m_styleSheet->AddCharacterStyle(boldDef);
wxRichTextCharacterStyleDefinition* italicDef = new wxRichTextCharacterStyleDefinition(wxT("Italic"));
// We only want to affect italics
italicAttr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
italicDef->SetStyle(italicAttr);
-
+
m_styleSheet->AddCharacterStyle(italicDef);
wxRichTextCharacterStyleDefinition* redDef = new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
// We only want to affect colour, weight and face
redAttr.SetFlags(wxTEXT_ATTR_FONT_FACE|wxTEXT_ATTR_FONT_WEIGHT|wxTEXT_ATTR_TEXT_COLOUR);
redDef->SetStyle(redAttr);
-
+
m_styleSheet->AddCharacterStyle(redDef);
}
fileMenu->AppendSeparator();
fileMenu->Append(ID_VIEW_HTML, _T("&View as HTML"), _T("View HTML"));
fileMenu->AppendSeparator();
- fileMenu->Append(ID_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
+ fileMenu->Append(ID_Quit, _T("E&xit\tAlt+X"), _T("Quit this program"));
wxMenu* editMenu = new wxMenu;
editMenu->Append(wxID_UNDO, _("&Undo\tCtrl+Z"));
formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT, _("&Right Align"));
formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE, _("&Centre"));
formatMenu->AppendSeparator();
- formatMenu->Append(ID_FORMAT_INDENT_MORE, _("I&ndent &More"));
+ formatMenu->Append(ID_FORMAT_INDENT_MORE, _("Indent &More"));
formatMenu->Append(ID_FORMAT_INDENT_LESS, _("Indent &Less"));
formatMenu->AppendSeparator();
formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE, _("Increase Paragraph &Spacing"));
SetMenuBar(menuBar);
// create a status bar just for fun (by default with 1 pane only)
+#if wxUSE_STATUSBAR
CreateStatusBar(2);
SetStatusText(_T("Welcome to wxRichTextCtrl!"));
+#endif
wxToolBar* toolBar = CreateToolBar();
- toolBar->AddTool(wxID_OPEN, wxBitmap(open_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Open"));
- toolBar->AddTool(wxID_SAVEAS, wxBitmap(save_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Save"));
+ toolBar->AddTool(wxID_OPEN, wxBitmap(open_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Open"));
+ toolBar->AddTool(wxID_SAVEAS, wxBitmap(save_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Save"));
toolBar->AddSeparator();
- toolBar->AddTool(wxID_CUT, wxBitmap(cut_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Cut"));
- toolBar->AddTool(wxID_COPY, wxBitmap(copy_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Copy"));
- toolBar->AddTool(wxID_PASTE, wxBitmap(paste_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Paste"));
+ toolBar->AddTool(wxID_CUT, wxBitmap(cut_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Cut"));
+ toolBar->AddTool(wxID_COPY, wxBitmap(copy_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Copy"));
+ toolBar->AddTool(wxID_PASTE, wxBitmap(paste_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Paste"));
toolBar->AddSeparator();
- toolBar->AddTool(wxID_UNDO, wxBitmap(undo_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Undo"));
- toolBar->AddTool(wxID_REDO, wxBitmap(redo_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Redo"));
+ toolBar->AddTool(wxID_UNDO, wxBitmap(undo_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Undo"));
+ toolBar->AddTool(wxID_REDO, wxBitmap(redo_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Redo"));
toolBar->AddSeparator();
- toolBar->AddTool(ID_FORMAT_BOLD, wxBitmap(bold_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Bold"));
- toolBar->AddTool(ID_FORMAT_ITALIC, wxBitmap(italic_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Italic"));
- toolBar->AddTool(ID_FORMAT_UNDERLINE, wxBitmap(underline_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Underline"));
+ toolBar->AddTool(ID_FORMAT_BOLD, wxBitmap(bold_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Bold"));
+ toolBar->AddTool(ID_FORMAT_ITALIC, wxBitmap(italic_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Italic"));
+ toolBar->AddTool(ID_FORMAT_UNDERLINE, wxBitmap(underline_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Underline"));
toolBar->AddSeparator();
- toolBar->AddTool(ID_FORMAT_ALIGN_LEFT, wxBitmap(alignleft_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Align Left"));
- toolBar->AddTool(ID_FORMAT_ALIGN_CENTRE, wxBitmap(centre_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Centre"));
- toolBar->AddTool(ID_FORMAT_ALIGN_RIGHT, wxBitmap(alignright_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Align Right"));
+ toolBar->AddTool(ID_FORMAT_ALIGN_LEFT, wxBitmap(alignleft_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Align Left"));
+ toolBar->AddTool(ID_FORMAT_ALIGN_CENTRE, wxBitmap(centre_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Centre"));
+ toolBar->AddTool(ID_FORMAT_ALIGN_RIGHT, wxBitmap(alignright_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Align Right"));
toolBar->AddSeparator();
- toolBar->AddTool(ID_FORMAT_INDENT_LESS, wxBitmap(indentless_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Indent Less"));
- toolBar->AddTool(ID_FORMAT_INDENT_MORE, wxBitmap(indentmore_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Indent More"));
+ toolBar->AddTool(ID_FORMAT_INDENT_LESS, wxBitmap(indentless_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Indent Less"));
+ toolBar->AddTool(ID_FORMAT_INDENT_MORE, wxBitmap(indentmore_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Indent More"));
toolBar->AddSeparator();
- toolBar->AddTool(ID_FORMAT_FONT, wxBitmap(font_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Font"));
+ toolBar->AddTool(ID_FORMAT_FONT, wxBitmap(font_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Font"));
toolBar->Realize();
- wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxSize(100, 100), wxSP_NO_XP_THEME|wxSP_3D|wxSP_LIVE_UPDATE);
-
+ wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, GetClientSize(), wxSP_NO_XP_THEME|wxSP_3D|wxSP_LIVE_UPDATE);
wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL);
m_richTextCtrl = new wxRichTextCtrl(splitter, wxID_ANY, wxDefaultPosition, wxSize(200, 200), wxVSCROLL|wxHSCROLL|wxNO_BORDER);
- m_richTextCtrl->SetFont(wxFont(12, wxROMAN, wxNORMAL, wxNORMAL));
+ wxFont font(12, wxROMAN, wxNORMAL, wxNORMAL);
+
+#ifdef __WXMAC__
+ font.SetNoAntiAliasing(true);
+#endif
+ m_richTextCtrl->SetFont(font);
wxRichTextStyleListBox* styleListBox = new wxRichTextStyleListBox(splitter, wxID_ANY);
splitter->SplitVertically(m_richTextCtrl, styleListBox, 400);
+ splitter->UpdateSize();
+
styleListBox->SetStyleSheet(wxGetApp().GetStyleSheet());
styleListBox->SetRichTextCtrl(m_richTextCtrl);
styleListBox->UpdateStyles();
{
s_eventType = event.GetEventType();
s_id = event.GetId();
-
+
wxWindow* focusWin = wxFindFocusDescendant(this);
if (focusWin && focusWin->ProcessEvent(event))
{
//s_command = NULL;
s_eventType = 0;
s_id = 0;
- return TRUE;
+ return true;
}
s_eventType = 0;
}
else
{
- return FALSE;
+ return false;
}
}
return wxFrame::ProcessEvent(event);
}
-void MyFrame::OnOpen(wxCommandEvent& event)
+void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
{
- wxString filter = wxRichTextBuffer::GetExtWildcard(false, false);
- if (!filter.IsEmpty())
+ wxString path;
+ wxString filename;
+ wxArrayInt fileTypes;
+
+ wxString filter = wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes);
+ if (!filter.empty())
filter += wxT("|");
filter += wxT("All files (*.*)|*.*");
- wxString path = wxEmptyString;
- wxString filename = wxEmptyString;
-
wxFileDialog dialog(this,
_("Choose a filename"),
path,
if (dialog.ShowModal() == wxID_OK)
{
wxString path = dialog.GetPath();
-
- if (!path.IsEmpty())
+
+ if (!path.empty())
{
- m_richTextCtrl->LoadFile(path);
+ int filterIndex = dialog.GetFilterIndex();
+ int fileType = (filterIndex < (int) fileTypes.GetCount())
+ ? fileTypes[filterIndex]
+ : wxRICHTEXT_TYPE_TEXT;
+ m_richTextCtrl->LoadFile(path, fileType);
}
}
}
void MyFrame::OnSave(wxCommandEvent& event)
{
- if (m_richTextCtrl->GetFilename().IsEmpty())
+ if (m_richTextCtrl->GetFilename().empty())
{
OnSaveAs(event);
return;
m_richTextCtrl->SaveFile();
}
-void MyFrame::OnSaveAs(wxCommandEvent& event)
+void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
{
wxString filter = wxRichTextBuffer::GetExtWildcard(false, true);
- wxString path = wxEmptyString;
- wxString filename = wxEmptyString;
+ wxString path;
+ wxString filename;
wxFileDialog dialog(this,
_("Choose a filename"),
if (dialog.ShowModal() == wxID_OK)
{
wxString path = dialog.GetPath();
-
- if (!path.IsEmpty())
+
+ if (!path.empty())
{
m_richTextCtrl->SaveFile(path);
}
}
}
-void MyFrame::OnBold(wxCommandEvent& event)
+void MyFrame::OnBold(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyBoldToSelection();
}
-void MyFrame::OnItalic(wxCommandEvent& event)
+void MyFrame::OnItalic(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyItalicToSelection();
}
-void MyFrame::OnUnderline(wxCommandEvent& event)
+void MyFrame::OnUnderline(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyUnderlineToSelection();
}
event.Check(m_richTextCtrl->IsSelectionUnderlined());
}
-void MyFrame::OnAlignLeft(wxCommandEvent& event)
+void MyFrame::OnAlignLeft(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT);
}
-void MyFrame::OnAlignCentre(wxCommandEvent& event)
+void MyFrame::OnAlignCentre(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE);
}
-void MyFrame::OnAlignRight(wxCommandEvent& event)
+void MyFrame::OnAlignRight(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT);
}
event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT));
}
-void MyFrame::OnFont(wxCommandEvent& event)
+void MyFrame::OnFont(wxCommandEvent& WXUNUSED(event))
{
if (!m_richTextCtrl->HasSelection())
return;
}
}
-void MyFrame::OnIndentMore(wxCommandEvent& event)
+void MyFrame::OnIndentMore(wxCommandEvent& WXUNUSED(event))
{
wxTextAttrEx attr;
attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
}
}
-void MyFrame::OnIndentLess(wxCommandEvent& event)
+void MyFrame::OnIndentLess(wxCommandEvent& WXUNUSED(event))
{
wxTextAttrEx attr;
attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
{
wxFontData fontData;
attr.SetLeftIndent(attr.GetLeftIndent() - 100);
-
+
m_richTextCtrl->SetStyle(range, attr);
}
}
}
-void MyFrame::OnLineSpacingHalf(wxCommandEvent& event)
+void MyFrame::OnLineSpacingHalf(wxCommandEvent& WXUNUSED(event))
{
wxTextAttrEx attr;
attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
wxFontData fontData;
attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
attr.SetLineSpacing(15);
-
+
m_richTextCtrl->SetStyle(range, attr);
}
}
-void MyFrame::OnLineSpacingDouble(wxCommandEvent& event)
+void MyFrame::OnLineSpacingDouble(wxCommandEvent& WXUNUSED(event))
{
wxTextAttrEx attr;
attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
wxFontData fontData;
attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
attr.SetLineSpacing(20);
-
+
m_richTextCtrl->SetStyle(range, attr);
}
}
-void MyFrame::OnLineSpacingSingle(wxCommandEvent& event)
+void MyFrame::OnLineSpacingSingle(wxCommandEvent& WXUNUSED(event))
{
wxTextAttrEx attr;
attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
wxFontData fontData;
attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
attr.SetLineSpacing(0); // Can also use 10
-
+
m_richTextCtrl->SetStyle(range, attr);
}
}
-void MyFrame::OnParagraphSpacingMore(wxCommandEvent& event)
+void MyFrame::OnParagraphSpacingMore(wxCommandEvent& WXUNUSED(event))
{
wxTextAttrEx attr;
attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
}
}
-void MyFrame::OnParagraphSpacingLess(wxCommandEvent& event)
+void MyFrame::OnParagraphSpacingLess(wxCommandEvent& WXUNUSED(event))
{
wxTextAttrEx attr;
attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
{
wxFontData fontData;
attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() - 20);
-
+
attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
m_richTextCtrl->SetStyle(range, attr);
}
}
}
-void MyFrame::OnViewHTML(wxCommandEvent& event)
+void MyFrame::OnViewHTML(wxCommandEvent& WXUNUSED(event))
{
wxDialog dialog(this, wxID_ANY, _("HTML"), wxDefaultPosition, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE);
dialog.ShowModal();
}
-