X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ab1ca7b3dda18b48e83f9ad2f5f3ea9b22247197..ed3ff7fe09a235dd021c81b2c6e1b30913f28f71:/samples/text/text.cpp diff --git a/samples/text/text.cpp b/samples/text/text.cpp index f4f79a219e..3f69987957 100644 --- a/samples/text/text.cpp +++ b/samples/text/text.cpp @@ -8,10 +8,6 @@ // Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ - #pragma implementation "text.cpp" -#endif - // For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" @@ -36,8 +32,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 +40,9 @@ #define wxUSE_CLIPBOARD 0 #endif +#include "wx/colordlg.h" +#include "wx/fontdlg.h" + //---------------------------------------------------------------------- // class definitions //---------------------------------------------------------------------- @@ -108,6 +105,8 @@ public: #endif // wxUSE_CLIPBOARD void DoRemoveText(); + void DoReplaceText(); + void DoSelectText(); void DoMoveToEndOfText(); void DoMoveToEndOfEntry(); @@ -150,43 +149,47 @@ public: #endif // wxUSE_TOOLTIPS #if wxUSE_CLIPBOARD - void OnPasteFromClipboard( wxCommandEvent &event ) + void OnPasteFromClipboard( wxCommandEvent& WXUNUSED(event) ) { m_panel->DoPasteFromClipboard(); } - void OnCopyToClipboard( wxCommandEvent &event ) + void OnCopyToClipboard( wxCommandEvent& WXUNUSED(event) ) { m_panel->DoCopyToClipboard(); } #endif // wxUSE_CLIPBOARD - void OnAddTextFreeze( wxCommandEvent& event ) + void OnAddTextFreeze( wxCommandEvent& WXUNUSED(event) ) { DoAddText(true); } - void OnAddText( wxCommandEvent& event ) + void OnAddText( wxCommandEvent& WXUNUSED(event) ) { DoAddText(false); } - void OnRemoveText( wxCommandEvent& event ) + void OnRemoveText( wxCommandEvent& WXUNUSED(event) ) { m_panel->DoRemoveText(); } + void OnReplaceText( wxCommandEvent& WXUNUSED(event) ) + { m_panel->DoReplaceText(); } + void OnSelectText( wxCommandEvent& WXUNUSED(event) ) + { m_panel->DoSelectText(); } - void OnMoveToEndOfText( wxCommandEvent &event ) + void OnMoveToEndOfText( wxCommandEvent& WXUNUSED(event) ) { m_panel->DoMoveToEndOfText(); } - void OnMoveToEndOfEntry( wxCommandEvent &event ) + void OnMoveToEndOfEntry( wxCommandEvent& WXUNUSED(event) ) { m_panel->DoMoveToEndOfEntry(); } - void OnScrollLineDown(wxCommandEvent& event) + void OnScrollLineDown(wxCommandEvent& WXUNUSED(event)) { if ( !m_panel->m_textrich->LineDown() ) wxLogMessage(_T("Already at the bottom")); } - void OnScrollLineUp(wxCommandEvent& event) + void OnScrollLineUp(wxCommandEvent& WXUNUSED(event)) { if ( !m_panel->m_textrich->LineUp() ) wxLogMessage(_T("Already at the top")); } - void OnScrollPageDown(wxCommandEvent& event) + void OnScrollPageDown(wxCommandEvent& WXUNUSED(event)) { if ( !m_panel->m_textrich->PageDown() ) wxLogMessage(_T("Already at the bottom")); } - void OnScrollPageUp(wxCommandEvent& event) + void OnScrollPageUp(wxCommandEvent& WXUNUSED(event)) { if ( !m_panel->m_textrich->PageUp() ) wxLogMessage(_T("Already at the top")); @@ -195,6 +198,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); @@ -224,7 +228,7 @@ public: MyTextCtrl::ms_logFocus = event.IsChecked(); } - void OnSetText(wxCommandEvent& event) + void OnSetText(wxCommandEvent& WXUNUSED(event)) { m_panel->m_text->SetValue(_T("Hello, world (what else did you expect)?")); } @@ -234,23 +238,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 +260,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 +307,7 @@ enum TEXT_LOAD, TEXT_SAVE, TEXT_CLEAR, + TEXT_RICH_TEXT_TEST, // clipboard menu TEXT_CLIPBOARD_COPY = 200, @@ -296,6 +329,8 @@ enum TEXT_PAGE_DOWN, TEXT_PAGE_UP, TEXT_REMOVE, + TEXT_REPLACE, + TEXT_SELECT, TEXT_SET, // log menu @@ -321,6 +356,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 +387,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 +463,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 ) { @@ -657,7 +696,7 @@ void MyTextCtrl::OnText(wxCommandEvent& event) } } -void MyTextCtrl::OnTextMaxLen(wxCommandEvent& event) +void MyTextCtrl::OnTextMaxLen(wxCommandEvent& WXUNUSED(event)) { wxLogMessage(_T("You can't enter more characters into this control.")); } @@ -696,7 +735,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 @@ -891,11 +930,7 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) _T("very very very long line to test ") _T("wxHSCROLL style"), wxPoint(450, 10), wxSize(230, 230), - wxTE_RICH | - wxTE_MULTILINE | - // wxTE_AUTO_URL | - wxHSCROLL); - + wxTE_RICH | wxTE_MULTILINE | wxHSCROLL); m_textrich->SetStyle(0, 10, *wxRED); m_textrich->SetStyle(10, 20, *wxBLUE); m_textrich->SetStyle(30, 40, @@ -915,7 +950,8 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) void MyPanel::OnSize( wxSizeEvent &event ) { wxSize client_area( GetClientSize() ); - m_log->SetSize( 0, 260, client_area.x, client_area.y - 260 ); + if (m_log) + m_log->SetSize( 0, 260, client_area.x, client_area.y - 260 ); event.Skip(); } @@ -1033,6 +1069,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 +1088,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 +1108,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) @@ -1111,7 +1160,8 @@ void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) ) } #if wxUSE_TOOLTIPS -void MyFrame::OnSetTooltipDelay(wxCommandEvent& event) + +void MyFrame::OnSetTooltipDelay(wxCommandEvent& WXUNUSED(event)) { static long s_delay = 5000; @@ -1132,7 +1182,7 @@ void MyFrame::OnSetTooltipDelay(wxCommandEvent& event) wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay); } -void MyFrame::OnToggleTooltips(wxCommandEvent& event) +void MyFrame::OnToggleTooltips(wxCommandEvent& WXUNUSED(event)) { static bool s_enabled = TRUE; @@ -1173,7 +1223,7 @@ void MyFrame::OnSetEnabled(wxCommandEvent& WXUNUSED(event)) m_panel->m_textrich->Enable(enabled); } -void MyFrame::OnFileSave(wxCommandEvent& event) +void MyFrame::OnFileSave(wxCommandEvent& WXUNUSED(event)) { if ( m_panel->m_textrich->SaveFile(_T("dummy.txt")) ) { @@ -1190,7 +1240,7 @@ void MyFrame::OnFileSave(wxCommandEvent& event) wxLogStatus(this, _T("Couldn't save the file")); } -void MyFrame::OnFileLoad(wxCommandEvent& event) +void MyFrame::OnFileLoad(wxCommandEvent& WXUNUSED(event)) { if ( m_panel->m_textrich->LoadFile(_T("dummy.txt")) ) wxLogStatus(this, _T("Successfully loaded file")); @@ -1198,6 +1248,12 @@ void MyFrame::OnFileLoad(wxCommandEvent& event) wxLogStatus(this, _T("Couldn't load the file")); } +void MyFrame::OnRichTextTest(wxCommandEvent& WXUNUSED(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 +1280,290 @@ 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& WXUNUSED(event)) +{ + Close(TRUE); +} + +void RichTextFrame::OnLeftAlign(wxCommandEvent& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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; + } +}