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