]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/text/text.cpp
removed datacmn, not a base file any more
[wxWidgets.git] / samples / text / text.cpp
index 0b0d1854ef93c2c5f7cb8a154662bb7002945ee3..7ddeaa84238beff4c90d3302df51ae0b94df1c47 100644 (file)
@@ -44,6 +44,9 @@
     #define wxUSE_CLIPBOARD 0
 #endif
 
+#include "wx/colordlg.h"
+#include "wx/fontdlg.h"
+
 //----------------------------------------------------------------------
 // class definitions
 //----------------------------------------------------------------------
@@ -107,6 +110,7 @@ public:
 
     void DoRemoveText();
     void DoReplaceText();
+    void DoSelectText();
     void DoMoveToEndOfText();
     void DoMoveToEndOfEntry();
 
@@ -163,6 +167,8 @@ public:
         { m_panel->DoRemoveText(); }
     void OnReplaceText( wxCommandEvent& event )
         { m_panel->DoReplaceText(); }
+    void OnSelectText( wxCommandEvent& event )
+        { m_panel->DoSelectText(); }
 
     void OnMoveToEndOfText( wxCommandEvent &event )
         { m_panel->DoMoveToEndOfText(); }
@@ -196,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);
@@ -257,6 +264,34 @@ 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 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;
+
+    DECLARE_EVENT_TABLE()
+};
+
 //----------------------------------------------------------------------
 // main()
 //----------------------------------------------------------------------
@@ -274,6 +309,7 @@ enum
     TEXT_LOAD,
     TEXT_SAVE,
     TEXT_CLEAR,
+    TEXT_RICH_TEXT_TEST,
 
     // clipboard menu
     TEXT_CLIPBOARD_COPY = 200,
@@ -296,6 +332,7 @@ enum
     TEXT_PAGE_UP,
     TEXT_REMOVE,
     TEXT_REPLACE,
+    TEXT_SELECT,
     TEXT_SET,
 
     // log menu
@@ -321,6 +358,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"));
@@ -352,6 +391,7 @@ bool MyApp::OnInit()
     menuText->Append(TEXT_ADD_FREEZE, _T("&Append text with freeze/thaw\tShift-Ctrl-A"));
     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"));
@@ -1046,6 +1086,11 @@ void MyPanel::DoReplaceText()
     GetFocusedText(m_multitext)->Replace(3, 8, _T("ABC"));
 }
 
+void MyPanel::DoSelectText()
+{
+    GetFocusedText(m_multitext)->SetSelection(3, 8);
+}
+
 //----------------------------------------------------------------------
 // MyFrame
 //----------------------------------------------------------------------
@@ -1055,6 +1100,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)
@@ -1075,6 +1121,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
 
     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)
@@ -1212,6 +1259,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
@@ -1238,3 +1291,230 @@ 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_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_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);
+}
+
+// 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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+    }
+}
+
+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);
+    }
+}
+
+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);
+    }
+}
+
+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);
+    }
+}
+
+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);
+    }
+}