]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
Added wizard sample.
[wxWidgets.git] / samples / console / console.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include <stdio.h>
21
22 #include <wx/string.h>
23 #include <wx/file.h>
24 #include <wx/app.h>
25
26 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
29
30 // what to test?
31 #define TEST_ARRAYS
32 #undef TEST_THREADS
33
34 // ============================================================================
35 // implementation
36 // ============================================================================
37
38 // ----------------------------------------------------------------------------
39 // threads
40 // ----------------------------------------------------------------------------
41
42 #ifdef TEST_THREADS
43
44 #include <wx/thread.h>
45
46 static size_t gs_counter = (size_t)-1;
47 static wxCriticalSection gs_critsect;
48
49 class MyThread : public wxThread
50 {
51 public:
52 MyThread(char ch);
53
54 // thread execution starts here
55 virtual void *Entry();
56
57 // and stops here
58 virtual void OnExit();
59
60 public:
61 char m_ch;
62 };
63
64 MyThread::MyThread(char ch)
65 {
66 m_ch = ch;
67
68 Create();
69 }
70
71 void *MyThread::Entry()
72 {
73 {
74 wxCriticalSectionLocker lock(gs_critsect);
75 if ( gs_counter == (size_t)-1 )
76 gs_counter = 1;
77 else
78 gs_counter++;
79 }
80
81 for ( size_t n = 0; n < 10; n++ )
82 {
83 if ( TestDestroy() )
84 break;
85
86 putchar(m_ch);
87 fflush(stdout);
88
89 wxThread::Sleep(100);
90 }
91
92 return NULL;
93 }
94
95 void MyThread::OnExit()
96 {
97 wxCriticalSectionLocker lock(gs_critsect);
98 gs_counter--;
99 }
100
101 #endif // TEST_THREADS
102
103 // ----------------------------------------------------------------------------
104 // arrays
105 // ----------------------------------------------------------------------------
106
107 #ifdef TEST_ARRAYS
108
109 void PrintArray(const char* name, const wxArrayString& array)
110 {
111 printf("Dump of the array '%s'\n", name);
112
113 size_t nCount = array.GetCount();
114 for ( size_t n = 0; n < nCount; n++ )
115 {
116 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
117 }
118 }
119
120 #endif // TEST_ARRAYS
121
122 // ----------------------------------------------------------------------------
123 // entry point
124 // ----------------------------------------------------------------------------
125
126 int main(int argc, char **argv)
127 {
128 if ( !wxInitialize() )
129 {
130 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
131 }
132
133 #ifdef TEST_ARRAYS
134 wxArrayString a1;
135 a1.Add("tiger");
136 a1.Add("cat");
137 a1.Add("lion");
138 a1.Add("dog");
139 a1.Add("human");
140 a1.Add("ape");
141
142 puts("*** Initially:");
143
144 PrintArray("a1", a1);
145
146 wxArrayString a2(a1);
147 PrintArray("a2", a2);
148
149 wxSortedArrayString a3(a1);
150 PrintArray("a3", a3);
151
152 puts("*** After deleting a string from a1");
153 a1.Remove(2);
154
155 PrintArray("a1", a1);
156 PrintArray("a2", a2);
157 PrintArray("a3", a3);
158
159 puts("*** After reassigning a1 to a2 and a3");
160 a3 = a2 = a1;
161 PrintArray("a2", a2);
162 PrintArray("a3", a3);
163 #endif // TEST_ARRAYS
164
165 #ifdef TEST_THREADS
166 static const size_t nThreads = 3;
167 MyThread *threads[nThreads];
168 size_t n;
169 for ( n = 0; n < nThreads; n++ )
170 {
171 threads[n] = new MyThread('+' + n);
172 threads[n]->Run();
173 }
174
175 // wait until all threads terminate
176 for ( ;; )
177 {
178 wxCriticalSectionLocker lock(gs_critsect);
179 if ( !gs_counter )
180 break;
181 }
182
183 puts("\nThat's all, folks!");
184
185 for ( n = 0; n < nThreads; n++ )
186 {
187 threads[n]->Delete();
188 }
189 #endif // TEST_THREADS
190
191 wxUninitialize();
192
193 return 0;
194 }