]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/text/text.cpp
add a radio box to select the combobox type (#9845)
[wxWidgets.git] / samples / text / text.cpp
index 61eab09b2e8a3b6c2735ac7ad66669db60495714..f8ed400e59a9ae14b365f73ce4b4301ad9de7017 100644 (file)
@@ -43,6 +43,7 @@
 #include "wx/colordlg.h"
 #include "wx/fontdlg.h"
 #include "wx/numdlg.h"
 #include "wx/colordlg.h"
 #include "wx/fontdlg.h"
 #include "wx/numdlg.h"
+#include "wx/tokenzr.h"
 
 //----------------------------------------------------------------------
 // class definitions
 
 //----------------------------------------------------------------------
 // class definitions
@@ -193,6 +194,12 @@ public:
     }
 #endif // wxUSE_CLIPBOARD
 
     }
 #endif // wxUSE_CLIPBOARD
 
+    void OnAddTextLine(wxCommandEvent& WXUNUSED(event))
+    {
+        static int s_n = 0;
+        m_panel->m_textrich->AppendText(wxString::Format("Line %d\n", ++s_n));
+    }
+
     void OnAddTextFreeze( wxCommandEvent& WXUNUSED(event) )
         { DoAddText(true); }
     void OnAddText( wxCommandEvent& WXUNUSED(event) )
     void OnAddTextFreeze( wxCommandEvent& WXUNUSED(event) )
         { DoAddText(true); }
     void OnAddText( wxCommandEvent& WXUNUSED(event) )
@@ -296,7 +303,12 @@ public:
 
     void OnSetText(wxCommandEvent& WXUNUSED(event))
     {
 
     void OnSetText(wxCommandEvent& WXUNUSED(event))
     {
-        m_panel->m_text->SetValue(_T("Hello, world (what else did you expect)?"));
+        m_panel->m_text->SetValue(_T("Hello, world! (what else did you expect?)"));
+    }
+
+    void OnChangeText(wxCommandEvent& WXUNUSED(event))
+    {
+        m_panel->m_text->ChangeValue(_T("Changed, not set: no event"));
     }
 
     void OnIdle( wxIdleEvent& event );
     }
 
     void OnIdle( wxIdleEvent& event );
@@ -305,20 +317,21 @@ private:
     void DoAddText(bool freeze)
     {
         wxTextCtrl * const text = m_panel->m_textrich;
     void DoAddText(bool freeze)
     {
         wxTextCtrl * const text = m_panel->m_textrich;
-        text->Clear();
 
         if ( freeze )
             text->Freeze();
 
 
         if ( freeze )
             text->Freeze();
 
+        text->Clear();
+
         for ( int i = 0; i < 100; i++ )
         {
             text->AppendText(wxString::Format(wxT("Line %i\n"), i));
         }
 
         for ( int i = 0; i < 100; i++ )
         {
             text->AppendText(wxString::Format(wxT("Line %i\n"), i));
         }
 
+        text->SetInsertionPoint(0);
+
         if ( freeze )
             text->Thaw();
         if ( freeze )
             text->Thaw();
-
-        text->SetInsertionPoint(0);
     }
 
     MyPanel *m_panel;
     }
 
     MyPanel *m_panel;
@@ -348,6 +361,7 @@ public:
     void OnChangeBackgroundColour(wxCommandEvent& event);
     void OnLeftIndent(wxCommandEvent& event);
     void OnRightIndent(wxCommandEvent& event);
     void OnChangeBackgroundColour(wxCommandEvent& event);
     void OnLeftIndent(wxCommandEvent& event);
     void OnRightIndent(wxCommandEvent& event);
+    void OnTabStops(wxCommandEvent& event);
 
 private:
     wxTextCtrl *m_textCtrl;
 
 private:
     wxTextCtrl *m_textCtrl;
@@ -387,6 +401,7 @@ enum
     // text menu
     TEXT_ADD_SOME = 400,
     TEXT_ADD_FREEZE,
     // text menu
     TEXT_ADD_SOME = 400,
     TEXT_ADD_FREEZE,
+    TEXT_ADD_LINE,
     TEXT_MOVE_ENDTEXT,
     TEXT_MOVE_ENDENTRY,
     TEXT_SET_EDITABLE,
     TEXT_MOVE_ENDTEXT,
     TEXT_MOVE_ENDENTRY,
     TEXT_SET_EDITABLE,
@@ -403,6 +418,7 @@ enum
     TEXT_REPLACE,
     TEXT_SELECT,
     TEXT_SET,
     TEXT_REPLACE,
     TEXT_SELECT,
     TEXT_SET,
+    TEXT_CHANGE,
 
     // log menu
     TEXT_LOG_KEY,
 
     // log menu
     TEXT_LOG_KEY,
@@ -417,6 +433,9 @@ enum
 
 bool MyApp::OnInit()
 {
 
 bool MyApp::OnInit()
 {
+    if ( !wxApp::OnInit() )
+        return false;
+
     // Create the main frame window
     MyFrame *frame = new MyFrame((wxFrame *) NULL,
             _T("Text wxWidgets sample"), 50, 50, 700, 550);
     // Create the main frame window
     MyFrame *frame = new MyFrame((wxFrame *) NULL,
             _T("Text wxWidgets sample"), 50, 50, 700, 550);
@@ -464,10 +483,12 @@ 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"));
     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_ADD_LINE, _T("Append a new &line\tAlt-Shift-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->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->Append(TEXT_CHANGE, _T("&Change the first text zone value\tShift-Ctrl-E"));
     menuText->AppendSeparator();
     menuText->Append(TEXT_MOVE_ENDTEXT, _T("Move cursor to the end of &text"));
     menuText->Append(TEXT_MOVE_ENDENTRY, _T("Move cursor to the end of &entry"));
     menuText->AppendSeparator();
     menuText->Append(TEXT_MOVE_ENDTEXT, _T("Move cursor to the end of &text"));
     menuText->Append(TEXT_MOVE_ENDENTRY, _T("Move cursor to the end of &entry"));
@@ -909,6 +930,10 @@ void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
                 wxLogMessage(_T("Selection = '%s' (len = %u)"),
                              sel.c_str(),
                              (unsigned int) sel.length());
                 wxLogMessage(_T("Selection = '%s' (len = %u)"),
                              sel.c_str(),
                              (unsigned int) sel.length());
+
+                const wxString text = GetLineText(line);
+                wxLogMessage(_T("Current line: \"%s\"; length = %lu"),
+                             text.c_str(), text.length());
             }
             break;
 
             }
             break;
 
@@ -945,7 +970,7 @@ void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
         case WXK_F6:
             wxLogMessage(_T("IsModified() before SetValue(): %d"),
                          IsModified());
         case WXK_F6:
             wxLogMessage(_T("IsModified() before SetValue(): %d"),
                          IsModified());
-            SetValue(_T("SetValue() has been called"));
+            ChangeValue(_T("ChangeValue() has been called"));
             wxLogMessage(_T("IsModified() after SetValue(): %d"),
                          IsModified());
             break;
             wxLogMessage(_T("IsModified() after SetValue(): %d"),
                          IsModified());
             break;
@@ -1024,7 +1049,7 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
     // a little hack to use the command line argument for encoding testing
     if ( wxTheApp->argc == 2 )
     {
     // a little hack to use the command line argument for encoding testing
     if ( wxTheApp->argc == 2 )
     {
-        switch ( wxTheApp->argv[1][0] )
+        switch ( (wxChar)wxTheApp->argv[1][0] )
         {
             case '2':
                 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
         {
             case '2':
                 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
@@ -1056,8 +1081,9 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
         m_horizontal->AppendText(_T("Text in default encoding"));
     }
 
         m_horizontal->AppendText(_T("Text in default encoding"));
     }
 
-    m_multitext = new MyTextCtrl( this, wxID_ANY, _T("Multi line."),
-      wxPoint(180,10), wxSize(200,70), wxTE_MULTILINE );
+    m_multitext = new MyTextCtrl( this, wxID_ANY,
+                                  _T("Multi line without vertical scrollbar."),
+      wxPoint(180,10), wxSize(200,70), wxTE_MULTILINE | wxTE_NO_VSCROLL );
     m_multitext->SetFont(*wxITALIC_FONT);
     (*m_multitext) << _T(" Appended.");
     m_multitext->SetInsertionPoint(0);
     m_multitext->SetFont(*wxITALIC_FONT);
     (*m_multitext) << _T(" Appended.");
     m_multitext->SetInsertionPoint(0);
@@ -1127,7 +1153,6 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
     topSizer->Add( m_log, 1, wxALL | wxEXPAND, 10 );
 #endif
 
     topSizer->Add( m_log, 1, wxALL | wxEXPAND, 10 );
 #endif
 
-    SetAutoLayout( true );
     SetSizer(topSizer);
 }
 
     SetSizer(topSizer);
 }
 
@@ -1320,6 +1345,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(TEXT_SELECT,             MyFrame::OnSelectText)
     EVT_MENU(TEXT_ADD_SOME,           MyFrame::OnAddText)
     EVT_MENU(TEXT_ADD_FREEZE,         MyFrame::OnAddTextFreeze)
     EVT_MENU(TEXT_SELECT,             MyFrame::OnSelectText)
     EVT_MENU(TEXT_ADD_SOME,           MyFrame::OnAddText)
     EVT_MENU(TEXT_ADD_FREEZE,         MyFrame::OnAddTextFreeze)
+    EVT_MENU(TEXT_ADD_LINE,           MyFrame::OnAddTextLine)
     EVT_MENU(TEXT_MOVE_ENDTEXT,       MyFrame::OnMoveToEndOfText)
     EVT_MENU(TEXT_MOVE_ENDENTRY,      MyFrame::OnMoveToEndOfEntry)
 
     EVT_MENU(TEXT_MOVE_ENDTEXT,       MyFrame::OnMoveToEndOfText)
     EVT_MENU(TEXT_MOVE_ENDENTRY,      MyFrame::OnMoveToEndOfEntry)
 
@@ -1331,10 +1357,11 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(TEXT_PAGE_DOWN,          MyFrame::OnScrollPageDown)
     EVT_MENU(TEXT_PAGE_UP,            MyFrame::OnScrollPageUp)
 
     EVT_MENU(TEXT_PAGE_DOWN,          MyFrame::OnScrollPageDown)
     EVT_MENU(TEXT_PAGE_UP,            MyFrame::OnScrollPageUp)
 
-    EVT_MENU(TEXT_GET_LINE,            MyFrame::OnGetLine)
-    EVT_MENU(TEXT_GET_LINELENGTH,            MyFrame::OnGetLineLength)
+    EVT_MENU(TEXT_GET_LINE,           MyFrame::OnGetLine)
+    EVT_MENU(TEXT_GET_LINELENGTH,     MyFrame::OnGetLineLength)
 
     EVT_MENU(TEXT_SET,                MyFrame::OnSetText)
 
     EVT_MENU(TEXT_SET,                MyFrame::OnSetText)
+    EVT_MENU(TEXT_CHANGE,             MyFrame::OnChangeText)
 
     EVT_IDLE(MyFrame::OnIdle)
 END_EVENT_TABLE()
 
     EVT_IDLE(MyFrame::OnIdle)
 END_EVENT_TABLE()
@@ -1481,13 +1508,13 @@ void MyFrame::OnIdle( wxIdleEvent& event )
         wxString msg;
         msg.Printf(
 #ifdef __WXMSW__
         wxString msg;
         msg.Printf(
 #ifdef __WXMSW__
-                _T("Focus: wxWindow = %p, HWND = %08x"),
+                _T("Focus: wxWindow = %p, HWND = %p"),
 #else
                 _T("Focus: wxWindow = %p"),
 #endif
                 s_windowFocus
 #ifdef __WXMSW__
 #else
                 _T("Focus: wxWindow = %p"),
 #endif
                 s_windowFocus
 #ifdef __WXMSW__
-                , (unsigned int) s_windowFocus->GetHWND()
+                , s_windowFocus->GetHWND()
 #endif
                   );
 
 #endif
                   );
 
@@ -1513,7 +1540,8 @@ enum
     RICHTEXT_CHANGE_TEXT_COLOUR,
     RICHTEXT_CHANGE_BACKGROUND_COLOUR,
     RICHTEXT_LEFT_INDENT,
     RICHTEXT_CHANGE_TEXT_COLOUR,
     RICHTEXT_CHANGE_BACKGROUND_COLOUR,
     RICHTEXT_LEFT_INDENT,
-    RICHTEXT_RIGHT_INDENT
+    RICHTEXT_RIGHT_INDENT,
+    RICHTEXT_TAB_STOPS
 };
 
 BEGIN_EVENT_TABLE(RichTextFrame, wxFrame)
 };
 
 BEGIN_EVENT_TABLE(RichTextFrame, wxFrame)
@@ -1528,6 +1556,7 @@ BEGIN_EVENT_TABLE(RichTextFrame, wxFrame)
     EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR, RichTextFrame::OnChangeBackgroundColour)
     EVT_MENU(RICHTEXT_LEFT_INDENT, RichTextFrame::OnLeftIndent)
     EVT_MENU(RICHTEXT_RIGHT_INDENT, RichTextFrame::OnRightIndent)
     EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR, RichTextFrame::OnChangeBackgroundColour)
     EVT_MENU(RICHTEXT_LEFT_INDENT, RichTextFrame::OnLeftIndent)
     EVT_MENU(RICHTEXT_RIGHT_INDENT, RichTextFrame::OnRightIndent)
+    EVT_MENU(RICHTEXT_TAB_STOPS, RichTextFrame::OnTabStops)
 END_EVENT_TABLE()
 
 RichTextFrame::RichTextFrame(wxWindow* parent, const wxString& title):
 END_EVENT_TABLE()
 
 RichTextFrame::RichTextFrame(wxWindow* parent, const wxString& title):
@@ -1566,7 +1595,8 @@ RichTextFrame::RichTextFrame(wxWindow* parent, const wxString& title):
     editMenu->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR, _("Change Background Colour"));
     editMenu->AppendSeparator();
     editMenu->Append(RICHTEXT_LEFT_INDENT, _("Left Indent"));
     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"));
+    editMenu->Append(RICHTEXT_RIGHT_INDENT, _("Right Indent"));
+    editMenu->Append(RICHTEXT_TAB_STOPS, _("Tab Stops"));
     menuBar->Append(editMenu, _("Edit"));
 
     SetMenuBar(menuBar);
     menuBar->Append(editMenu, _("Edit"));
 
     SetMenuBar(menuBar);
@@ -1758,6 +1788,35 @@ void RichTextFrame::OnRightIndent(wxCommandEvent& WXUNUSED(event))
     }
 }
 
     }
 }
 
+void RichTextFrame::OnTabStops(wxCommandEvent& WXUNUSED(event))
+{
+    wxString tabsStr = wxGetTextFromUser
+                         (
+                            _("Please enter the tab stop positions in tenths of a millimetre, separated by spaces.\nLeave empty to reset tab stops."),
+                            _("Tab Stops"),
+                            wxEmptyString,
+                            this
+                         );
+
+    wxArrayInt tabs;
+
+    wxStringTokenizer tokens(tabsStr, _T(" "));
+    while (tokens.HasMoreTokens())
+    {
+        wxString token = tokens.GetNextToken();
+        tabs.Add(wxAtoi(token));
+    }
+
+    wxTextAttr attr;
+    attr.SetTabs(tabs);
+
+    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();
 void RichTextFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
 {
     long insertionPoint = m_textCtrl->GetInsertionPoint();
@@ -1782,10 +1841,9 @@ void RichTextFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
                 alignment = wxT("left-aligned");
             else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED)
                 alignment = wxT("justified");
                 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,
+            msg.Printf( "Facename: %s, wxColour(%d, %d, %d), %s", facename,
                 attr.GetTextColour().Red(), attr.GetTextColour().Green(), attr.GetTextColour().Blue(),
                 attr.GetTextColour().Red(), attr.GetTextColour().Green(), attr.GetTextColour().Blue(),
-                (const wxChar*) alignment);
+                alignment );
 
             if (attr.HasFont())
             {
 
             if (attr.HasFont())
             {