]> git.saurik.com Git - wxWidgets.git/blob - src/stubs/app.cpp
67271c4c3b0f4d19c2334674253c3f24d146fe9a
[wxWidgets.git] / src / stubs / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "app.h"
14 #endif
15
16 #include "wx/frame.h"
17 #include "wx/app.h"
18 #include "wx/utils.h"
19 #include "wx/gdicmn.h"
20 #include "wx/pen.h"
21 #include "wx/brush.h"
22 #include "wx/cursor.h"
23 #include "wx/icon.h"
24 #include "wx/palette.h"
25 #include "wx/dc.h"
26 #include "wx/dialog.h"
27 #include "wx/msgdlg.h"
28 #include "wx/log.h"
29 #include "wx/module.h"
30 #include "wx/memory.h"
31
32 #if USE_WX_RESOURCES
33 #include "wx/resource.h"
34 #endif
35
36 #if USE_POSTSCRIPT
37 #include "wx/postscrp.h"
38 #endif
39
40 #include <string.h>
41
42 extern char *wxBuffer;
43 extern wxList wxPendingDelete;
44
45 wxApp *wxTheApp = NULL;
46
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
49 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
50 EVT_IDLE(wxApp::OnIdle)
51 END_EVENT_TABLE()
52 #endif
53
54 long wxApp::sm_lastMessageTime = 0;
55
56 void wxApp::CommonInit()
57 {
58 #ifdef __WXMSW__
59 wxBuffer = new char[1500];
60 #else
61 wxBuffer = new char[BUFSIZ + 512];
62 #endif
63
64 wxClassInfo::InitializeClasses();
65
66 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
67 wxTheColourDatabase->Initialize();
68 wxInitializeStockObjects();
69
70 #if USE_WX_RESOURCES
71 wxInitializeResourceSystem();
72 #endif
73
74 // For PostScript printing
75 #if USE_POSTSCRIPT
76 wxInitializePrintSetupData();
77 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
78 wxThePrintPaperDatabase->CreateDatabase();
79 #endif
80
81 wxBitmap::InitStandardHandlers();
82
83 wxModule::RegisterModules();
84 wxASSERT( wxModule::InitializeModules() == TRUE );
85 }
86
87 void wxApp::CommonCleanUp()
88 {
89 wxModule::CleanUpModules();
90
91 #if USE_WX_RESOURCES
92 wxCleanUpResourceSystem();
93 #endif
94
95 wxDeleteStockObjects() ;
96
97 // Destroy all GDI lists, etc.
98
99 delete wxTheBrushList;
100 wxTheBrushList = NULL;
101
102 delete wxThePenList;
103 wxThePenList = NULL;
104
105 delete wxTheFontList;
106 wxTheFontList = NULL;
107
108 delete wxTheBitmapList;
109 wxTheBitmapList = NULL;
110
111 delete wxTheColourDatabase;
112 wxTheColourDatabase = NULL;
113
114 #if USE_POSTSCRIPT
115 wxInitializePrintSetupData(FALSE);
116 delete wxThePrintPaperDatabase;
117 wxThePrintPaperDatabase = NULL;
118 #endif
119
120 wxBitmap::CleanUpHandlers();
121
122 delete[] wxBuffer;
123 wxBuffer = NULL;
124
125 wxClassInfo::CleanUpClasses();
126
127 // do it as the very last thing because everything else can log messages
128 wxLog::DontCreateOnDemand();
129 // do it as the very last thing because everything else can log messages
130 delete wxLog::SetActiveTarget(NULL);
131 }
132
133 int wxEntry( int argc, char *argv[] )
134 {
135 wxClassInfo::InitializeClasses();
136
137 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
138
139 #if !defined(_WINDLL)
140 streambuf* sBuf = new wxDebugStreamBuf;
141 #else
142 streambuf* sBuf = NULL;
143 #endif
144 ostream* oStr = new ostream(sBuf) ;
145 wxDebugContext::SetStream(oStr, sBuf);
146
147 #endif
148
149 if (!wxTheApp)
150 {
151 if (!wxApp::GetInitializerFunction())
152 {
153 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
154 return 0;
155 };
156
157 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
158 };
159
160 if (!wxTheApp)
161 {
162 printf( "wxWindows error: wxTheApp == NULL\n" );
163 return 0;
164 };
165
166 wxTheApp->argc = argc;
167 wxTheApp->argv = argv;
168
169 // TODO: your platform-specific initialization.
170
171 wxApp::CommonInit();
172
173 // GUI-specific initialization, such as creating an app context.
174 wxTheApp->OnInitGui();
175
176 // Here frames insert themselves automatically
177 // into wxTopLevelWindows by getting created
178 // in OnInit().
179
180 if (!wxTheApp->OnInit()) return 0;
181
182 int retValue = 0;
183
184 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
185
186 if (wxTheApp->GetTopWindow())
187 {
188 delete wxTheApp->GetTopWindow();
189 wxTheApp->SetTopWindow(NULL);
190 }
191
192 wxTheApp->DeletePendingObjects();
193
194 wxTheApp->OnExit();
195
196 wxApp::CommonCleanUp();
197
198 delete wxTheApp;
199 wxTheApp = NULL;
200
201 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
202 // At this point we want to check if there are any memory
203 // blocks that aren't part of the wxDebugContext itself,
204 // as a special case. Then when dumping we need to ignore
205 // wxDebugContext, too.
206 if (wxDebugContext::CountObjectsLeft() > 0)
207 {
208 wxTrace("There were memory leaks.\n");
209 wxDebugContext::Dump();
210 wxDebugContext::PrintStatistics();
211 }
212 wxDebugContext::SetStream(NULL, NULL);
213 #endif
214
215 return retValue;
216 };
217
218 // Static member initialization
219 wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
220
221 wxApp::wxApp()
222 {
223 m_topWindow = NULL;
224 wxTheApp = this;
225 m_className = "";
226 m_wantDebugOutput = TRUE ;
227 m_appName = "";
228 argc = 0;
229 argv = NULL;
230 #ifdef __WXMSW__
231 m_printMode = wxPRINT_WINDOWS;
232 #else
233 m_printMode = wxPRINT_POSTSCRIPT;
234 #endif
235 m_exitOnFrameDelete = TRUE;
236 m_auto3D = TRUE;
237 }
238
239 bool wxApp::Initialized()
240 {
241 if (GetTopWindow())
242 return TRUE;
243 else
244 return FALSE;
245 }
246
247 int wxApp::MainLoop()
248 {
249 m_keepGoing = TRUE;
250
251 /* TODO: implement your main loop here, calling ProcessIdle in idle time.
252 while (m_keepGoing)
253 {
254 while (!::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) &&
255 ProcessIdle()) {}
256 if (!DoMessage())
257 m_keepGoing = FALSE;
258 }
259 */
260
261 return 0;
262 }
263
264 // Returns TRUE if more time is needed.
265 bool wxApp::ProcessIdle()
266 {
267 wxIdleEvent event;
268 event.SetEventObject(this);
269 ProcessEvent(event);
270
271 return event.MoreRequested();
272 }
273
274 void wxApp::ExitMainLoop()
275 {
276 m_keepGoing = FALSE;
277 }
278
279 // Is a message/event pending?
280 bool wxApp::Pending()
281 {
282 /* TODO.
283 */
284 return FALSE;
285 }
286
287 // Dispatch a message.
288 void wxApp::Dispatch()
289 {
290 /* TODO.
291 */
292 }
293
294 void wxApp::OnIdle(wxIdleEvent& event)
295 {
296 static bool inOnIdle = FALSE;
297
298 // Avoid recursion (via ProcessEvent default case)
299 if (inOnIdle)
300 return;
301
302 inOnIdle = TRUE;
303
304 // 'Garbage' collection of windows deleted with Close().
305 DeletePendingObjects();
306
307 // flush the logged messages if any
308 wxLog *pLog = wxLog::GetActiveTarget();
309 if ( pLog != NULL && pLog->HasPendingMessages() )
310 pLog->Flush();
311
312 // Send OnIdle events to all windows
313 bool needMore = SendIdleEvents();
314
315 if (needMore)
316 event.RequestMore(TRUE);
317
318 inOnIdle = FALSE;
319 }
320
321 // Send idle event to all top-level windows
322 bool wxApp::SendIdleEvents()
323 {
324 bool needMore = FALSE;
325 wxNode* node = wxTopLevelWindows.First();
326 while (node)
327 {
328 wxWindow* win = (wxWindow*) node->Data();
329 if (SendIdleEvents(win))
330 needMore = TRUE;
331
332 node = node->Next();
333 }
334 return needMore;
335 }
336
337 // Send idle event to window and all subwindows
338 bool wxApp::SendIdleEvents(wxWindow* win)
339 {
340 bool needMore = FALSE;
341
342 wxIdleEvent event;
343 event.SetEventObject(win);
344 win->ProcessEvent(event);
345
346 if (event.MoreRequested())
347 needMore = TRUE;
348
349 wxNode* node = win->GetChildren()->First();
350 while (node)
351 {
352 wxWindow* win = (wxWindow*) node->Data();
353 if (SendIdleEvents(win))
354 needMore = TRUE;
355
356 node = node->Next();
357 }
358 return needMore ;
359 }
360
361 void wxApp::DeletePendingObjects()
362 {
363 wxNode *node = wxPendingDelete.First();
364 while (node)
365 {
366 wxObject *obj = (wxObject *)node->Data();
367
368 delete obj;
369
370 if (wxPendingDelete.Member(obj))
371 delete node;
372
373 // Deleting one object may have deleted other pending
374 // objects, so start from beginning of list again.
375 node = wxPendingDelete.First();
376 }
377 }
378
379 wxLog* wxApp::CreateLogTarget()
380 {
381 return new wxLogGui;
382 }
383
384 wxWindow* wxApp::GetTopWindow() const
385 {
386 if (m_topWindow)
387 return m_topWindow;
388 else if (wxTopLevelWindows.Number() > 0)
389 return (wxWindow*) wxTopLevelWindows.First()->Data();
390 else
391 return NULL;
392 }
393
394 void wxExit()
395 {
396 wxApp::CommonCleanUp();
397 /*
398 * TODO: Exit in some platform-specific way. Not recommended that the app calls this:
399 * only for emergencies.
400 */
401 }
402
403 // Yield to other processes
404 bool wxYield()
405 {
406 /*
407 * TODO
408 */
409 return TRUE;
410 }
411