X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/011b3dcbef1f34e476a7a8be04d9525ec47b8062..914f51576275c0f5411db2eed52099c71a7489a3:/samples/richtext/richtext.cpp?ds=sidebyside diff --git a/samples/richtext/richtext.cpp b/samples/richtext/richtext.cpp index 43871e3b9f..2c8d0a3df6 100644 --- a/samples/richtext/richtext.cpp +++ b/samples/richtext/richtext.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: richedit.cpp +// Name: samples/richtext/richtext.cpp // Purpose: wxWidgets rich text editor sample // Author: Julian Smart // Modified by: @@ -35,7 +35,10 @@ #include "wx/sstream.h" #include "wx/html/htmlwin.h" -#include "bitmaps/sample.xpm" +#ifndef __WXMSW__ + #include "../sample.xpm" +#endif + #include "bitmaps/smiley.xpm" // #include "bitmaps/idea.xpm" #include "bitmaps/zebra.xpm" @@ -61,6 +64,7 @@ #include "wx/richtext/richtextctrl.h" #include "wx/richtext/richtextstyles.h" #include "wx/richtext/richtextxml.h" +#include "wx/richtext/richtexthtml.h" // ---------------------------------------------------------------------------- // resources @@ -253,7 +257,7 @@ bool MyApp::OnInit() #if wxUSE_LIBPNG wxImage::AddHandler( new wxPNGHandler ); #endif - + #if wxUSE_LIBJPEG wxImage::AddHandler( new wxJPEGHandler ); #endif @@ -297,7 +301,7 @@ void MyApp::CreateStyles() 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")); @@ -308,9 +312,23 @@ void MyApp::CreateStyles() // We only want to affect indentation indentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT); indentedPara->SetStyle(indentedAttr); - + m_styleSheet->AddParagraphStyle(indentedPara); + wxRichTextParagraphStyleDefinition* indentedPara2 = new wxRichTextParagraphStyleDefinition(wxT("Red Bold Indented")); + wxRichTextAttr indentedAttr2; + indentedAttr2.SetFontFaceName(romanFont.GetFaceName()); + indentedAttr2.SetFontSize(12); + indentedAttr2.SetFontWeight(wxBOLD); + indentedAttr2.SetTextColour(*wxRED); + indentedAttr2.SetFontSize(12); + indentedAttr2.SetLeftIndent(100, 0); + // We want to affect indentation, font and text colour + indentedAttr2.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_FONT|wxTEXT_ATTR_TEXT_COLOUR); + indentedPara2->SetStyle(indentedAttr2); + + m_styleSheet->AddParagraphStyle(indentedPara2); + wxRichTextParagraphStyleDefinition* flIndentedPara = new wxRichTextParagraphStyleDefinition(wxT("First Line Indented")); wxRichTextAttr flIndentedAttr; flIndentedAttr.SetFontFaceName(swissFont.GetFaceName()); @@ -319,7 +337,7 @@ void MyApp::CreateStyles() // 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 @@ -332,7 +350,7 @@ void MyApp::CreateStyles() // 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")); @@ -343,7 +361,7 @@ void MyApp::CreateStyles() // 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")); @@ -355,7 +373,7 @@ void MyApp::CreateStyles() // 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); } @@ -369,7 +387,7 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, : wxFrame(NULL, id, title, pos, size, style) { // set the frame icon - SetIcon(sample_xpm); + SetIcon(wxICON(sample)); // create a menu bar wxMenu *fileMenu = new wxMenu; @@ -384,7 +402,7 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, 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")); @@ -413,7 +431,7 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, 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")); @@ -436,48 +454,68 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, SetMenuBar(menuBar); // create a status bar just for fun (by default with 1 pane only) - CreateStatusBar(2); - SetStatusText(_T("Welcome to wxRichTextCtrl!")); + // but don't create it on limited screen space (WinCE) + bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA; + +#if wxUSE_STATUSBAR + if ( !is_pda ) + { + 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)); + m_richTextCtrl = new wxRichTextCtrl(splitter, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, 200), wxVSCROLL|wxHSCROLL|wxNO_BORDER|wxWANTS_CHARS); + wxFont font(12, wxROMAN, wxNORMAL, wxNORMAL); + + m_richTextCtrl->SetFont(font); wxRichTextStyleListBox* styleListBox = new wxRichTextStyleListBox(splitter, wxID_ANY); - splitter->SplitVertically(m_richTextCtrl, styleListBox, 400); + + wxSize display = wxGetDisplaySize(); + if ( is_pda && ( display.GetWidth() < display.GetHeight() ) ) + { + splitter->SplitHorizontally(m_richTextCtrl, styleListBox); + } + else + { + splitter->SplitVertically(m_richTextCtrl, styleListBox, 400); + } + + splitter->UpdateSize(); styleListBox->SetStyleSheet(wxGetApp().GetStyleSheet()); styleListBox->SetRichTextCtrl(m_richTextCtrl); @@ -594,6 +632,19 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, r.EndLineSpacing(); r.EndAlignment(); + wxArrayInt tabs; + tabs.Add(400); + tabs.Add(600); + tabs.Add(800); + tabs.Add(1000); + wxTextAttrEx attr; + attr.SetFlags(wxTEXT_ATTR_TABS); + attr.SetTabs(tabs); + r.SetDefaultStyle(attr); + + r.Newline(); + r.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab")); + r.Newline(); r.WriteText(wxT("Other notable features of wxRichTextCtrl include:")); @@ -674,14 +725,14 @@ bool MyFrame::ProcessEvent(wxEvent& event) { 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; @@ -689,7 +740,7 @@ bool MyFrame::ProcessEvent(wxEvent& event) } else { - return FALSE; + return false; } } @@ -698,35 +749,40 @@ bool MyFrame::ProcessEvent(wxEvent& 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, filename, filter, - wxOPEN); + wxFD_OPEN); 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; @@ -737,21 +793,21 @@ void MyFrame::OnSave(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"), path, filename, filter, - wxSAVE); + wxFD_SAVE); if (dialog.ShowModal() == wxID_OK) { wxString path = dialog.GetPath(); - - if (!path.IsEmpty()) + + if (!path.empty()) { m_richTextCtrl->SaveFile(path); } @@ -880,7 +936,7 @@ void MyFrame::OnIndentLess(wxCommandEvent& WXUNUSED(event)) { wxFontData fontData; attr.SetLeftIndent(attr.GetLeftIndent() - 100); - + m_richTextCtrl->SetStyle(range, attr); } } @@ -900,7 +956,7 @@ void MyFrame::OnLineSpacingHalf(wxCommandEvent& WXUNUSED(event)) wxFontData fontData; attr.SetFlags(wxTEXT_ATTR_LINE_SPACING); attr.SetLineSpacing(15); - + m_richTextCtrl->SetStyle(range, attr); } } @@ -919,7 +975,7 @@ void MyFrame::OnLineSpacingDouble(wxCommandEvent& WXUNUSED(event)) wxFontData fontData; attr.SetFlags(wxTEXT_ATTR_LINE_SPACING); attr.SetLineSpacing(20); - + m_richTextCtrl->SetStyle(range, attr); } } @@ -938,7 +994,7 @@ void MyFrame::OnLineSpacingSingle(wxCommandEvent& WXUNUSED(event)) wxFontData fontData; attr.SetFlags(wxTEXT_ATTR_LINE_SPACING); attr.SetLineSpacing(0); // Can also use 10 - + m_richTextCtrl->SetStyle(range, attr); } } @@ -977,7 +1033,7 @@ void MyFrame::OnParagraphSpacingLess(wxCommandEvent& WXUNUSED(event)) { wxFontData fontData; attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() - 20); - + attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER); m_richTextCtrl->SetStyle(range, attr); } @@ -1010,4 +1066,3 @@ void MyFrame::OnViewHTML(wxCommandEvent& WXUNUSED(event)) dialog.ShowModal(); } -