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