X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ab1ca7b3dda18b48e83f9ad2f5f3ea9b22247197..bf6e8dc931325404850952d90d4998a306793dcd:/samples/text/text.cpp diff --git a/samples/text/text.cpp b/samples/text/text.cpp index f4f79a219e..6c8c47677a 100644 --- a/samples/text/text.cpp +++ b/samples/text/text.cpp @@ -36,8 +36,6 @@ #include "wx/tooltip.h" #endif - #include "wx/progdlg.h" - // We test for wxUSE_DRAG_AND_DROP also, because data objects may not be // implemented for compilers that can't cope with the OLE parts in // wxUSE_DRAG_AND_DROP. @@ -46,6 +44,9 @@ #define wxUSE_CLIPBOARD 0 #endif +#include "wx/colordlg.h" +#include "wx/fontdlg.h" + //---------------------------------------------------------------------- // class definitions //---------------------------------------------------------------------- @@ -108,6 +109,8 @@ public: #endif // wxUSE_CLIPBOARD void DoRemoveText(); + void DoReplaceText(); + void DoSelectText(); void DoMoveToEndOfText(); void DoMoveToEndOfEntry(); @@ -162,6 +165,10 @@ public: { DoAddText(false); } void OnRemoveText( wxCommandEvent& event ) { m_panel->DoRemoveText(); } + void OnReplaceText( wxCommandEvent& event ) + { m_panel->DoReplaceText(); } + void OnSelectText( wxCommandEvent& event ) + { m_panel->DoSelectText(); } void OnMoveToEndOfText( wxCommandEvent &event ) { m_panel->DoMoveToEndOfText(); } @@ -195,6 +202,7 @@ public: void OnLogClear(wxCommandEvent& event); void OnFileSave(wxCommandEvent& event); void OnFileLoad(wxCommandEvent& event); + void OnRichTextTest(wxCommandEvent& event); void OnSetEditable(wxCommandEvent& event); void OnSetEnabled(wxCommandEvent& event); @@ -234,23 +242,21 @@ public: private: void DoAddText(bool freeze) { - wxTextCtrl *text = m_panel->m_textrich; + wxTextCtrl * const text = m_panel->m_textrich; + text->Clear(); + if ( freeze ) text->Freeze(); - text->Clear(); - - wxProgressDialog dlg(_T("Wait..."), _T("Updating"), 100, this); for ( int i = 0; i < 100; i++ ) { - dlg.Update(i); text->AppendText(wxString::Format(wxT("Line %i\n"), i)); } - text->SetInsertionPoint(0); - if ( freeze ) text->Thaw(); + + text->SetInsertionPoint(0); } MyPanel *m_panel; @@ -258,6 +264,36 @@ private: DECLARE_EVENT_TABLE() }; +/* + * RichTextFrame is used to demonstrate rich text behaviour + */ + +class RichTextFrame: public wxFrame +{ +public: + RichTextFrame(wxWindow* parent, const wxString& title); + +// Event handlers + + void OnClose(wxCommandEvent& event); + void OnIdle(wxIdleEvent& event); + void OnLeftAlign(wxCommandEvent& event); + void OnRightAlign(wxCommandEvent& event); + void OnJustify(wxCommandEvent& event); + void OnCentre(wxCommandEvent& event); + void OnChangeFont(wxCommandEvent& event); + void OnChangeTextColour(wxCommandEvent& event); + void OnChangeBackgroundColour(wxCommandEvent& event); + void OnLeftIndent(wxCommandEvent& event); + void OnRightIndent(wxCommandEvent& event); + +private: + wxTextCtrl *m_textCtrl; + long m_currentPosition; + + DECLARE_EVENT_TABLE() +}; + //---------------------------------------------------------------------- // main() //---------------------------------------------------------------------- @@ -275,6 +311,7 @@ enum TEXT_LOAD, TEXT_SAVE, TEXT_CLEAR, + TEXT_RICH_TEXT_TEST, // clipboard menu TEXT_CLIPBOARD_COPY = 200, @@ -296,6 +333,8 @@ enum TEXT_PAGE_DOWN, TEXT_PAGE_UP, TEXT_REMOVE, + TEXT_REPLACE, + TEXT_SELECT, TEXT_SET, // log menu @@ -321,6 +360,8 @@ bool MyApp::OnInit() file_menu->Append(TEXT_LOAD, _T("&Load file\tCtrl-O"), _T("Load the sample file into text control")); file_menu->AppendSeparator(); + file_menu->Append(TEXT_RICH_TEXT_TEST, _T("Show Rich Text Editor")); + file_menu->AppendSeparator(); file_menu->Append(TEXT_ABOUT, _T("&About\tAlt-A")); file_menu->AppendSeparator(); file_menu->Append(TEXT_QUIT, _T("E&xit\tAlt-X"), _T("Quit this sample")); @@ -350,7 +391,9 @@ bool MyApp::OnInit() wxMenu *menuText = new wxMenu; menuText->Append(TEXT_ADD_SOME, _T("&Append some text\tCtrl-A")); menuText->Append(TEXT_ADD_FREEZE, _T("&Append text with freeze/thaw\tShift-Ctrl-A")); - menuText->Append(TEXT_REMOVE, _T("&Remove first 10 characters\tCtrl-X")); + menuText->Append(TEXT_REMOVE, _T("&Remove first 10 characters\tCtrl-Y")); + menuText->Append(TEXT_REPLACE, _T("&Replace characters 4 to 8 with ABC\tCtrl-R")); + menuText->Append(TEXT_SELECT, _T("&Select characters 4 to 8\tCtrl-I")); menuText->Append(TEXT_SET, _T("&Set the first text zone value\tCtrl-E")); menuText->AppendSeparator(); menuText->Append(TEXT_MOVE_ENDTEXT, _T("Move cursor to the end of &text")); @@ -424,7 +467,7 @@ bool MyTextCtrl::ms_logFocus = FALSE; void MyTextCtrl::LogKeyEvent(const wxChar *name, wxKeyEvent& event) const { wxString key; - long keycode = event.KeyCode(); + long keycode = event.GetKeyCode(); { switch ( keycode ) { @@ -696,7 +739,7 @@ void MyTextCtrl::OnKeyUp(wxKeyEvent& event) void MyTextCtrl::OnKeyDown(wxKeyEvent& event) { - switch ( event.KeyCode() ) + switch ( event.GetKeyCode() ) { case WXK_F1: // show current position and text length @@ -885,17 +928,17 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE); m_enter->SetClientData((void *)_T("enter")); +#if 0 m_textrich = new MyTextCtrl(this, -1, _T("Allows more than 30Kb of text\n") _T("(even under broken Win9x)\n") _T("and a very very very very very ") _T("very very very long line to test ") _T("wxHSCROLL style"), wxPoint(450, 10), wxSize(230, 230), - wxTE_RICH | + wxTE_RICH2 | wxTE_MULTILINE | // wxTE_AUTO_URL | wxHSCROLL); - m_textrich->SetStyle(0, 10, *wxRED); m_textrich->SetStyle(10, 20, *wxBLUE); m_textrich->SetStyle(30, 40, @@ -910,6 +953,13 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) m_textrich->SetDefaultStyle(wxTextAttr(*wxBLUE, *wxWHITE)); m_textrich->AppendText(_T("And this should be in blue and the text you ") _T("type should be in blue as well")); +#else + m_textrich = new MyTextCtrl(this, -1, _T(""), + wxPoint(450, 10), wxSize(230, 230), + wxTE_RICH2 | + wxTE_MULTILINE | + wxHSCROLL); +#endif } void MyPanel::OnSize( wxSizeEvent &event ) @@ -1033,6 +1083,16 @@ void MyPanel::DoRemoveText() GetFocusedText(m_multitext)->Remove(0, 10); } +void MyPanel::DoReplaceText() +{ + GetFocusedText(m_multitext)->Replace(3, 8, _T("ABC")); +} + +void MyPanel::DoSelectText() +{ + GetFocusedText(m_multitext)->SetSelection(3, 8); +} + //---------------------------------------------------------------------- // MyFrame //---------------------------------------------------------------------- @@ -1042,6 +1102,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(TEXT_ABOUT, MyFrame::OnAbout) EVT_MENU(TEXT_SAVE, MyFrame::OnFileSave) EVT_MENU(TEXT_LOAD, MyFrame::OnFileLoad) + EVT_MENU(TEXT_RICH_TEXT_TEST, MyFrame::OnRichTextTest) EVT_MENU(TEXT_LOG_KEY, MyFrame::OnLogKey) EVT_MENU(TEXT_LOG_CHAR, MyFrame::OnLogChar) @@ -1061,6 +1122,8 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) #endif // wxUSE_CLIPBOARD EVT_MENU(TEXT_REMOVE, MyFrame::OnRemoveText) + EVT_MENU(TEXT_REPLACE, MyFrame::OnReplaceText) + EVT_MENU(TEXT_SELECT, MyFrame::OnSelectText) EVT_MENU(TEXT_ADD_SOME, MyFrame::OnAddText) EVT_MENU(TEXT_ADD_FREEZE, MyFrame::OnAddTextFreeze) EVT_MENU(TEXT_MOVE_ENDTEXT, MyFrame::OnMoveToEndOfText) @@ -1198,6 +1261,12 @@ void MyFrame::OnFileLoad(wxCommandEvent& event) wxLogStatus(this, _T("Couldn't load the file")); } +void MyFrame::OnRichTextTest(wxCommandEvent& event) +{ + RichTextFrame* frame = new RichTextFrame(this, _T("Rich Text Editor")); + frame->Show(TRUE); +} + void MyFrame::OnIdle( wxIdleEvent& event ) { // track the window which has the focus in the status bar @@ -1224,3 +1293,284 @@ void MyFrame::OnIdle( wxIdleEvent& event ) } event.Skip(); } + +/* + * RichTextFrame is used to demonstrate rich text behaviour + */ + +enum +{ + RICHTEXT_CLOSE = 1000, + RICHTEXT_LEFT_ALIGN, + RICHTEXT_RIGHT_ALIGN, + RICHTEXT_CENTRE, + RICHTEXT_JUSTIFY, + RICHTEXT_CHANGE_FONT, + RICHTEXT_CHANGE_TEXT_COLOUR, + RICHTEXT_CHANGE_BACKGROUND_COLOUR, + RICHTEXT_LEFT_INDENT, + RICHTEXT_RIGHT_INDENT +}; + +BEGIN_EVENT_TABLE(RichTextFrame, wxFrame) + EVT_IDLE(RichTextFrame::OnIdle) + EVT_MENU(RICHTEXT_CLOSE, RichTextFrame::OnClose) + EVT_MENU(RICHTEXT_LEFT_ALIGN, RichTextFrame::OnLeftAlign) + EVT_MENU(RICHTEXT_RIGHT_ALIGN, RichTextFrame::OnRightAlign) + EVT_MENU(RICHTEXT_CENTRE, RichTextFrame::OnCentre) + EVT_MENU(RICHTEXT_JUSTIFY, RichTextFrame::OnJustify) + EVT_MENU(RICHTEXT_CHANGE_FONT, RichTextFrame::OnChangeFont) + EVT_MENU(RICHTEXT_CHANGE_TEXT_COLOUR, RichTextFrame::OnChangeTextColour) + EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR, RichTextFrame::OnChangeBackgroundColour) + EVT_MENU(RICHTEXT_LEFT_INDENT, RichTextFrame::OnLeftIndent) + EVT_MENU(RICHTEXT_RIGHT_INDENT, RichTextFrame::OnRightIndent) +END_EVENT_TABLE() + +RichTextFrame::RichTextFrame(wxWindow* parent, const wxString& title): + wxFrame(parent, -1, title, wxDefaultPosition, wxSize(300, 400)) +{ + m_currentPosition = -1; + m_textCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, + wxDefaultSize, wxTE_MULTILINE|wxTE_RICH2); + + wxString value; + int i; + for (i = 0; i < 10; i++) + { + int j; + for (j = 0; j < 10; j++) + { + value << wxT("Hello, welcome to a very simple rich text editor. You can set some character and paragraph styles from the Edit menu. "); + } + value << wxT("\n\n"); + } + m_textCtrl->SetValue(value); + + wxMenuBar* menuBar = new wxMenuBar; + wxMenu* fileMenu = new wxMenu; + fileMenu->Append(RICHTEXT_CLOSE, _("Close\tCtrl+W")); + menuBar->Append(fileMenu, _("File")); + + wxMenu* editMenu = new wxMenu; + editMenu->Append(RICHTEXT_LEFT_ALIGN, _("Left Align")); + editMenu->Append(RICHTEXT_RIGHT_ALIGN, _("Right Align")); + editMenu->Append(RICHTEXT_CENTRE, _("Centre")); + editMenu->Append(RICHTEXT_JUSTIFY, _("Justify")); + editMenu->AppendSeparator(); + editMenu->Append(RICHTEXT_CHANGE_FONT, _("Change Font")); + editMenu->Append(RICHTEXT_CHANGE_TEXT_COLOUR, _("Change Text Colour")); + editMenu->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR, _("Change Background Colour")); + editMenu->AppendSeparator(); + editMenu->Append(RICHTEXT_LEFT_INDENT, _("Left Indent")); + editMenu->Append(RICHTEXT_RIGHT_INDENT, _("Right indent")); + menuBar->Append(editMenu, _("Edit")); + + SetMenuBar(menuBar); + CreateStatusBar(); +} + +// Event handlers + +void RichTextFrame::OnClose(wxCommandEvent& event) +{ + Close(TRUE); +} + +void RichTextFrame::OnLeftAlign(wxCommandEvent& event) +{ + wxTextAttr attr; + attr.SetAlignment(wxTEXT_ALIGNMENT_LEFT); + + long start, end; + m_textCtrl->GetSelection(& start, & end); + m_textCtrl->SetStyle(start, end, attr); + + m_currentPosition = -1; +} + +void RichTextFrame::OnRightAlign(wxCommandEvent& event) +{ + wxTextAttr attr; + attr.SetAlignment(wxTEXT_ALIGNMENT_RIGHT); + + long start, end; + m_textCtrl->GetSelection(& start, & end); + m_textCtrl->SetStyle(start, end, attr); + + m_currentPosition = -1; +} + +void RichTextFrame::OnJustify(wxCommandEvent& event) +{ + wxTextAttr attr; + attr.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED); + + long start, end; + m_textCtrl->GetSelection(& start, & end); + m_textCtrl->SetStyle(start, end, attr); + + m_currentPosition = -1; +} + +void RichTextFrame::OnCentre(wxCommandEvent& event) +{ + wxTextAttr attr; + attr.SetAlignment(wxTEXT_ALIGNMENT_CENTRE); + + long start, end; + m_textCtrl->GetSelection(& start, & end); + m_textCtrl->SetStyle(start, end, attr); + + m_currentPosition = -1; +} + +void RichTextFrame::OnChangeFont(wxCommandEvent& event) +{ + wxFontData data; + + wxFontDialog dialog(this, data); + + if (dialog.ShowModal() == wxID_OK) + { + wxFontData retData = dialog.GetFontData(); + wxFont font = retData.GetChosenFont(); + + wxTextAttr attr; + attr.SetFont(font); + + long start, end; + m_textCtrl->GetSelection(& start, & end); + m_textCtrl->SetStyle(start, end, attr); + + m_currentPosition = -1; + } +} + +void RichTextFrame::OnChangeTextColour(wxCommandEvent& event) +{ + wxColourData data; + data.SetColour(* wxBLACK); + data.SetChooseFull(TRUE); + for (int i = 0; i < 16; i++) + { + wxColour colour(i*16, i*16, i*16); + data.SetCustomColour(i, colour); + } + + wxColourDialog dialog(this, &data); + dialog.SetTitle(_T("Choose the text colour")); + if (dialog.ShowModal() == wxID_OK) + { + wxColourData retData = dialog.GetColourData(); + wxColour col = retData.GetColour(); + + wxTextAttr attr; + attr.SetTextColour(col); + + long start, end; + m_textCtrl->GetSelection(& start, & end); + m_textCtrl->SetStyle(start, end, attr); + + m_currentPosition = -1; + } +} + +void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent& event) +{ + wxColourData data; + data.SetColour(* wxWHITE); + data.SetChooseFull(TRUE); + for (int i = 0; i < 16; i++) + { + wxColour colour(i*16, i*16, i*16); + data.SetCustomColour(i, colour); + } + + wxColourDialog dialog(this, &data); + dialog.SetTitle(_T("Choose the text background colour")); + if (dialog.ShowModal() == wxID_OK) + { + wxColourData retData = dialog.GetColourData(); + wxColour col = retData.GetColour(); + + wxTextAttr attr; + attr.SetBackgroundColour(col); + + long start, end; + m_textCtrl->GetSelection(& start, & end); + m_textCtrl->SetStyle(start, end, attr); + + m_currentPosition = -1; + } +} + +void RichTextFrame::OnLeftIndent(wxCommandEvent& event) +{ + wxString indentStr = wxGetTextFromUser(_("Please enter the left indent in tenths of a millimetre."), + _("Left Indent")); + if (!indentStr.IsEmpty()) + { + int indent = wxAtoi(indentStr); + + wxTextAttr attr; + attr.SetLeftIndent(indent); + + long start, end; + m_textCtrl->GetSelection(& start, & end); + m_textCtrl->SetStyle(start, end, attr); + + m_currentPosition = -1; + } +} + +void RichTextFrame::OnRightIndent(wxCommandEvent& event) +{ + wxString indentStr = wxGetTextFromUser(_("Please enter the right indent in tenths of a millimetre."), + _("Right Indent")); + if (!indentStr.IsEmpty()) + { + int indent = wxAtoi(indentStr); + + wxTextAttr attr; + attr.SetRightIndent(indent); + + long start, end; + m_textCtrl->GetSelection(& start, & end); + m_textCtrl->SetStyle(start, end, attr); + + m_currentPosition = -1; + } +} + +void RichTextFrame::OnIdle(wxIdleEvent& event) +{ + long insertionPoint = m_textCtrl->GetInsertionPoint(); + if (insertionPoint != m_currentPosition) + { + wxTextAttr attr; + if (m_textCtrl->GetStyle(insertionPoint, attr)) + { + wxString msg; + wxString facename(wxT("unknown")); + if (attr.GetFont().Ok()) + { + facename = attr.GetFont().GetFaceName(); + } + wxString alignment(wxT("unknown alignment")); + if (attr.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE) + alignment = wxT("centred"); + else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT) + alignment = wxT("right-aligned"); + else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_LEFT) + alignment = wxT("left-aligned"); + else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED) + alignment = wxT("justified"); + msg.Printf(wxT("Facename: %s, wxColour(%d, %d, %d), %s"), + (const wxChar*) facename, + attr.GetTextColour().Red(), attr.GetTextColour().Green(), attr.GetTextColour().Blue(), + (const wxChar*) alignment); + SetStatusText(msg); + } + m_currentPosition = insertionPoint; + } +}