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