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