]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/text/text.cpp
1. some DDE tests in exec
[wxWidgets.git] / samples / text / text.cpp
index 391cc7ce0e427e8b962ce759559503116a140002..f534553804011ffea7760de78ca2adbdb8de81ac 100644 (file)
     #include "wx/clipbrd.h"
 #endif
 
+#if wxUSE_FILE
+    #include "wx/file.h"
+#endif
+
 #if wxUSE_TOOLTIPS
     #include "wx/tooltip.h"
 #endif
@@ -62,6 +66,7 @@ public:
     void OnKeyDown(wxKeyEvent& event);
     void OnKeyUp(wxKeyEvent& event);
     void OnChar(wxKeyEvent& event);
+    void OnText(wxCommandEvent& event);
 
     bool  m_hasCapture;
 
@@ -96,6 +101,8 @@ public:
     MyTextCtrl    *m_multitext;
     MyTextCtrl    *m_horizontal;
 
+    MyTextCtrl    *m_textrich;
+
     wxTextCtrl    *m_log;
 
 private:
@@ -127,6 +134,7 @@ public:
         { m_panel->DoMoveToEndOfEntry(); }
 
     void OnLogClear(wxCommandEvent& event);
+    void OnFileSave(wxCommandEvent& event);
     void OnFileLoad(wxCommandEvent& event);
 
     void OnIdle( wxIdleEvent& event );
@@ -152,6 +160,7 @@ enum
     TEXT_QUIT = 100,
     TEXT_ABOUT,
     TEXT_LOAD,
+    TEXT_SAVE,
     TEXT_CLEAR,
 
     // clipboard menu
@@ -171,12 +180,14 @@ bool MyApp::OnInit()
 {
     // Create the main frame window
     MyFrame *frame = new MyFrame((wxFrame *) NULL,
-            "Text wxWindows sample", 50, 50, 640, 420);
+            "Text wxWindows sample", 50, 50, 660, 420);
     frame->SetSizeHints( 500, 400 );
 
     wxMenu *file_menu = new wxMenu;
     file_menu->Append(TEXT_CLEAR, "&Clear the log\tCtrl-C",
                       "Clear the log window contents");
+    file_menu->Append(TEXT_SAVE, "&Save file\tCtrl-S",
+                      "Save the text control contents to file");
     file_menu->Append(TEXT_LOAD, "&Load file\tCtrl-O",
                       "Load the sample file into text control");
     file_menu->AppendSeparator();
@@ -229,6 +240,7 @@ BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
     EVT_KEY_DOWN(MyTextCtrl::OnKeyDown)
     EVT_KEY_UP(MyTextCtrl::OnKeyUp)
     EVT_CHAR(MyTextCtrl::OnChar)
+    EVT_TEXT(-1, MyTextCtrl::OnText)
 END_EVENT_TABLE()
 
 void MyTextCtrl::LogEvent(const wxChar *name, wxKeyEvent& event) const
@@ -340,13 +352,13 @@ void MyTextCtrl::LogEvent(const wxChar *name, wxKeyEvent& event) const
             case WXK_NUMPAD_SUBTRACT: key = "NUMPAD_SUBTRACT"; break;
             case WXK_NUMPAD_DECIMAL: key = "NUMPAD_DECIMAL"; break;
 
-           default:
+            default:
             {
                if ( wxIsprint((int)keycode) )
                    key.Printf( _T("'%c'") , (char)keycode);
                else
-                  key.Printf( _T("unknown (%ld)"), keycode);
-           }
+                   key.Printf( _T("unknown (%ld)"), keycode);
+            }
         }
     }
 
@@ -360,6 +372,16 @@ void MyTextCtrl::LogEvent(const wxChar *name, wxKeyEvent& event) const
 
 }
 
+void MyTextCtrl::OnText(wxCommandEvent& event)
+{
+    MyTextCtrl *win = (MyTextCtrl *)event.GetEventObject();
+    const wxChar *data = (const wxChar *)(win->GetClientData());
+    if ( data )
+    {
+        wxLogMessage(_T("text changed in control '%s'"), data);
+    }
+}
+
 void MyTextCtrl::OnChar(wxKeyEvent& event)
 {
     LogEvent( _T("Char"), event);
@@ -380,7 +402,7 @@ void MyTextCtrl::OnChar(wxKeyEvent& event)
 
 void MyTextCtrl::OnKeyUp(wxKeyEvent& event)
 {
-    LogEvent( _("Key up"), event);
+    LogEvent( _T("Key up"), event);
 
     event.Skip();
 }
@@ -417,24 +439,30 @@ void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
             // go to position 10
             SetInsertionPoint(10);
             break;
-           
+
         case WXK_F4:
-           if (!m_hasCapture)
-           {
+            if (!m_hasCapture)
+            {
                 wxLogDebug( wxT("Now capturing mouse and events.") );
-               m_hasCapture = TRUE;
-               CaptureMouse();
-           }
-           else
-           {
+                m_hasCapture = TRUE;
+                CaptureMouse();
+            }
+            else
+            {
                 wxLogDebug( wxT("Stopped capturing mouse and events.") );
-               m_hasCapture = TRUE;
-               ReleaseMouse();
-           }
+                m_hasCapture = TRUE;
+                ReleaseMouse();
+            }
             break;
-    }
 
-    LogEvent( _("Key down"), event);
+        case WXK_F5:
+            // insert a blank line
+            WriteText("\n");
+            break;
+
+        default:
+            LogEvent( wxT("Key down"), event);
+    }
 
     event.Skip();
 }
@@ -487,11 +515,18 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
     m_multitext->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
 #endif
 
-    m_tab = new MyTextCtrl( this, -1, "Multiline, allow <TAB> processing.",
+    m_tab = new MyTextCtrl( this, 100, "Multiline, allow <TAB> processing.",
       wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE |  wxTE_PROCESS_TAB );
+    m_tab->SetClientData(_T("tab"));
 
-    m_enter = new MyTextCtrl( this, -1, "Multiline, allow <ENTER> processing.",
+    m_enter = new MyTextCtrl( this, 100, "Multiline, allow <ENTER> processing.",
       wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE);
+    m_enter->SetClientData(_T("enter"));
+
+    m_textrich = new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n"
+                                "(even under broken Win9x)",
+                                wxPoint(450, 10), wxSize(200, 230),
+                                wxTE_RICH | wxTE_MULTILINE);
 }
 
 void MyPanel::OnSize( wxSizeEvent &event )
@@ -609,6 +644,7 @@ void MyPanel::DoMoveToEndOfEntry()
 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(TEXT_QUIT,   MyFrame::OnQuit)
     EVT_MENU(TEXT_ABOUT,  MyFrame::OnAbout)
+    EVT_MENU(TEXT_SAVE,   MyFrame::OnFileSave)
     EVT_MENU(TEXT_LOAD,   MyFrame::OnFileLoad)
     EVT_MENU(TEXT_CLEAR,  MyFrame::OnLogClear)
 
@@ -698,9 +734,26 @@ void MyFrame::OnLogClear(wxCommandEvent& WXUNUSED(event))
     m_panel->m_log->Clear();
 }
 
+void MyFrame::OnFileSave(wxCommandEvent& event)
+{
+    if ( m_panel->m_textrich->SaveFile("dummy.txt") )
+    {
+#if wxUSE_FILE
+        // verify that the fil length is correct (it wasn't under Win95)
+        wxFile file("dummy.txt");
+        wxLogStatus(this, _T("Successfully saved file "
+                             "(text len = %ld, file size = %ld)"),
+                    m_panel->m_textrich->GetValue().length(),
+                    file.Length());
+#endif
+    }
+    else
+        wxLogStatus(this, _T("Couldn't save the file"));
+}
+
 void MyFrame::OnFileLoad(wxCommandEvent& event)
 {
-    if ( m_panel->m_multitext->LoadFile("text.rc") )
+    if ( m_panel->m_textrich->LoadFile("dummy.txt") )
         wxLogStatus(this, _T("Successfully loaded file"));
     else
         wxLogStatus(this, _T("Couldn't load the file"));