]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/console/console.cpp
Create a wrapper file for X11/XKBlib.h header.
[wxWidgets.git] / samples / console / console.cpp
index 8b8fbcf9e1a9b0bccb21524844d0793aa43946bc..fa356714adb5c0d858c2ea5082421ec2e9c7cc0a 100644 (file)
@@ -1,12 +1,12 @@
 /////////////////////////////////////////////////////////////////////////////
 // Name:        samples/console/console.cpp
-// Purpose:     a sample console (as opposed to GUI) progam using wxWindows
+// Purpose:     A sample console (as opposed to GUI) program using wxWidgets
 // Author:      Vadim Zeitlin
 // Modified by:
 // Created:     04.10.99
 // RCS-ID:      $Id$
 // Copyright:   (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
-// Licence:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
 // headers
 // ----------------------------------------------------------------------------
 
-#include <stdio.h>
+// For compilers that support precompilation, includes "wx/wx.h".
+#include "wx/wxprec.h"
 
-#include <wx/string.h>
-#include <wx/file.h>
-#include <wx/app.h>
+#ifdef __BORLANDC__
+    #pragma hdrstop
+#endif
 
-// ----------------------------------------------------------------------------
-// conditional compilation
-// ----------------------------------------------------------------------------
+// for all others, include the necessary headers (this file is usually all you
+// need because it includes almost all "standard" wxWidgets headers)
+#ifndef WX_PRECOMP
+    #include "wx/wx.h"
+#endif
 
-// what to test?
-#define TEST_ARRAYS
-#undef TEST_THREADS
+#include <wx/app.h>
+#include <wx/cmdline.h>
 
 // ============================================================================
 // implementation
 // ============================================================================
 
-// ----------------------------------------------------------------------------
-// threads
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_THREADS
-
-#include <wx/thread.h>
-
-static size_t gs_counter = (size_t)-1;
-static wxCriticalSection gs_critsect;
-
-class MyThread : public wxThread
+static const wxCmdLineEntryDesc cmdLineDesc[] =
 {
-public:
-    MyThread(char ch);
-
-    // thread execution starts here
-    virtual void *Entry();
-
-    // and stops here
-    virtual void OnExit();
+    { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
+        wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
+    { wxCMD_LINE_SWITCH, "d", "dummy", "a dummy switch" },
+    // ... your other command line options here...
 
-public:
-    char m_ch;
+    { wxCMD_LINE_NONE }
 };
 
-MyThread::MyThread(char ch)
+int main(int argc, char **argv)
 {
-    m_ch = ch;
-
-    Create();
-}
+    wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
 
-void *MyThread::Entry()
-{
+    wxInitializer initializer;
+    if ( !initializer )
     {
-        wxCriticalSectionLocker lock(gs_critsect);
-        if ( gs_counter == (size_t)-1 )
-            gs_counter = 1;
-        else
-            gs_counter++;
+        fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
+        return -1;
     }
 
-    for ( size_t n = 0; n < 10; n++ )
+    wxCmdLineParser parser(cmdLineDesc, argc, argv);
+    switch ( parser.Parse() )
     {
-        if ( TestDestroy() )
+        case -1:
+            // help was given, terminating
             break;
 
-        putchar(m_ch);
-        fflush(stdout);
-
-        wxThread::Sleep(100);
-    }
-
-    return NULL;
-}
-
-void MyThread::OnExit()
-{
-    wxCriticalSectionLocker lock(gs_critsect);
-    gs_counter--;
-}
-
-#endif // TEST_THREADS
-
-// ----------------------------------------------------------------------------
-// arrays
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_ARRAYS
-
-void PrintArray(const char* name, const wxArrayString& array)
-{
-    printf("Dump of the array '%s'\n", name);
-
-    size_t nCount = array.GetCount();
-    for ( size_t n = 0; n < nCount; n++ )
-    {
-        printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
-    }
-}
-
-#endif // TEST_ARRAYS
-
-// ----------------------------------------------------------------------------
-// entry point
-// ----------------------------------------------------------------------------
-
-int main(int argc, char **argv)
-{
-    if ( !wxInitialize() )
-    {
-        fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
-    }
-
-#ifdef TEST_ARRAYS
-    wxArrayString a1;
-    a1.Add("tiger");
-    a1.Add("cat");
-    a1.Add("lion");
-    a1.Add("dog");
-    a1.Add("human");
-    a1.Add("ape");
-
-    puts("*** Initially:");
-
-    PrintArray("a1", a1);
-
-    wxArrayString a2(a1);
-    PrintArray("a2", a2);
-
-    wxSortedArrayString a3(a1);
-    PrintArray("a3", a3);
-
-    puts("*** After deleting a string from a1");
-    a1.Remove(2);
-
-    PrintArray("a1", a1);
-    PrintArray("a2", a2);
-    PrintArray("a3", a3);
-
-    puts("*** After reassigning a1 to a2 and a3");
-    a3 = a2 = a1;
-    PrintArray("a2", a2);
-    PrintArray("a3", a3);
-#endif // TEST_ARRAYS
-
-#ifdef TEST_THREADS
-    static const size_t nThreads = 3;
-    MyThread *threads[nThreads];
-    size_t n;
-    for ( n = 0; n < nThreads; n++ )
-    {
-        threads[n] = new MyThread('+' + n);
-        threads[n]->Run();
-    }
+        case 0:
+            // everything is ok; proceed
+            if (parser.Found("d"))
+            {
+                wxPrintf("Dummy switch was given...\n");
+
+                while (1)
+                {
+                    wxChar input[128];
+                    wxPrintf("Try to guess the magic number (type 'quit' to escape): ");
+                    if ( !wxFgets(input, WXSIZEOF(input), stdin) )
+                        break;
+
+                    // kill the last '\n'
+                    input[wxStrlen(input) - 1] = 0;
+
+                    if (wxStrcmp(input, "quit") == 0)
+                        break;
+
+                    long val;
+                    if (!wxString(input).ToLong(&val))
+                    {
+                        wxPrintf("Invalid number...\n");
+                        continue;
+                    }
+
+                    if (val == 42)
+                        wxPrintf("You guessed!\n");
+                    else
+                        wxPrintf("Bad luck!\n");
+                }
+            }
+            break;
 
-    // wait until all threads terminate
-    for ( ;; )
-    {
-        wxCriticalSectionLocker lock(gs_critsect);
-        if ( !gs_counter )
+        default:
             break;
     }
 
-    puts("\nThat's all, folks!");
-
-    for ( n = 0; n < nThreads; n++ )
+    if ( argc == 1 )
     {
-        threads[n]->Delete();
+        // If there were no command-line options supplied, emit a message
+        // otherwise it's not obvious that the sample ran successfully
+        wxPrintf("Welcome to the wxWidgets 'console' sample!\n");
+        wxPrintf("For more information, run it again with the --help option\n");
     }
-#endif // TEST_THREADS
 
-    wxUninitialize();
+    // do something useful here
 
     return 0;
 }