]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/app.cpp
no message
[wxWidgets.git] / src / gtk1 / app.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose:
4// Author: Robert Roebling
32e9da8b 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling, Julian Smart
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "app.h"
12#endif
13
14#include "wx/app.h"
15#include "wx/gdicmn.h"
16#include "wx/utils.h"
c801d85f
KB
17#include "wx/intl.h"
18#include "wx/log.h"
46dc76ba 19#include "wx/memory.h"
a3622daa
VZ
20#include "wx/font.h"
21#include "wx/settings.h"
8d71b555 22#include "wx/resource.h"
c801d85f
KB
23
24#include "unistd.h"
25
c801d85f
KB
26//-----------------------------------------------------------------------------
27// global data
28//-----------------------------------------------------------------------------
29
c67daf87 30wxApp *wxTheApp = (wxApp *) NULL;
c801d85f
KB
31wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
32
33extern wxList wxPendingDelete;
a3622daa 34extern wxResourceCache *wxTheResourceCache;
c801d85f 35
01111366
RR
36unsigned char g_palette[64*3] =
37{
38 0x0, 0x0, 0x0,
39 0xff, 0xff, 0xff,
40 0xff, 0x0, 0x0,
41 0xff, 0xff, 0x0,
42 0x0, 0xff, 0x0,
43 0x0, 0x0, 0xff,
44 0x0, 0xff, 0xff,
45 0x99, 0x99, 0x99,
46 0xff, 0x88, 0x0,
47 0x88, 0x0, 0x0,
48 0x0, 0x88, 0x88,
49 0x88, 0x88, 0x0,
50 0xff, 0xcc, 0x97,
51 0xbb, 0xbb, 0xbb,
52 0x9f, 0x6b, 0x42,
53 0x55, 0x55, 0x55,
54 0xdd, 0xdd, 0xdd,
55 0x77, 0x77, 0x77,
56 0x33, 0x33, 0x33,
57 0xcc, 0x0, 0x0,
58 0xff, 0x44, 0x0,
59 0xff, 0xcc, 0x0,
60 0xcc, 0xcc, 0x0,
61 0x60, 0x60, 0x0,
62 0x0, 0x43, 0x0,
63 0x0, 0x7f, 0x0,
64 0x0, 0xcc, 0x0,
65 0x0, 0x44, 0x44,
66 0x0, 0x0, 0x44,
67 0x0, 0x0, 0x88,
68 0xef, 0xb1, 0x7b,
69 0xdf, 0x98, 0x5f,
70 0xbf, 0x87, 0x56,
71 0x7f, 0x57, 0x26,
72 0x5f, 0x39, 0xc,
73 0x3f, 0x1c, 0x0,
74 0x21, 0x0, 0x0,
75 0x0, 0x43, 0x87,
76 0x2d, 0x70, 0xaf,
77 0x5a, 0x9e, 0xd7,
78 0x87, 0xcc, 0xff,
79 0xff, 0xe0, 0xba,
80 0x21, 0x43, 0xf,
81 0x3d, 0x5d, 0x25,
82 0x59, 0x78, 0x3a,
83 0x75, 0x93, 0x4f,
84 0x91, 0xae, 0x64,
85 0xad, 0xc8, 0x7a,
86 0xf0, 0xa8, 0xef,
87 0xd0, 0x88, 0xd0,
88 0xaf, 0x66, 0xaf,
89 0x8e, 0x44, 0x8e,
90 0x6d, 0x22, 0x6d,
91 0x4b, 0x0, 0x4b,
92 0xff, 0xc0, 0xbc,
93 0xff, 0x93, 0x91,
94 0xff, 0x66, 0x67,
95 0xd8, 0xf2, 0xbf,
96 0xff, 0xc9, 0x68,
97 0xff, 0x96, 0x67,
98 0xa5, 0x60, 0xff,
99 0x51, 0xff, 0x99,
100 0x3f, 0xa5, 0x63,
101 0x98, 0x90, 0x67
102};
103
c801d85f
KB
104//-----------------------------------------------------------------------------
105// local functions
106//-----------------------------------------------------------------------------
107
108extern void wxFlushResources(void);
109
110//-----------------------------------------------------------------------------
111// global functions
112//-----------------------------------------------------------------------------
113
114void wxExit(void)
115{
116 gtk_main_quit();
ff7b1510 117}
c801d85f
KB
118
119bool wxYield(void)
120{
121 while (gtk_events_pending() > 0) gtk_main_iteration();
122 return TRUE;
ff7b1510 123}
c801d85f
KB
124
125//-----------------------------------------------------------------------------
126// wxApp
127//-----------------------------------------------------------------------------
128
129IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
130
53010e52
RR
131BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
132 EVT_IDLE(wxApp::OnIdle)
133END_EVENT_TABLE()
134
c801d85f
KB
135gint wxapp_idle_callback( gpointer WXUNUSED(data) )
136{
ff7b1510 137 if (wxTheApp) while (wxTheApp->ProcessIdle()) {}
c801d85f
KB
138 usleep( 10000 );
139 return TRUE;
ff7b1510 140}
c801d85f
KB
141
142wxApp::wxApp()
143{
144 m_idleTag = 0;
c67daf87 145 m_topWindow = (wxWindow *) NULL;
c801d85f 146 m_exitOnFrameDelete = TRUE;
0cf2cb36 147 wxTheApp = this;
ff7b1510 148}
c801d85f
KB
149
150wxApp::~wxApp(void)
151{
152 gtk_idle_remove( m_idleTag );
ff7b1510 153}
c801d85f
KB
154
155bool wxApp::OnInit(void)
156{
157 return TRUE;
ff7b1510 158}
c801d85f
KB
159
160bool wxApp::OnInitGui(void)
0cf2cb36 161{
c801d85f 162 m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL );
0cf2cb36 163 return TRUE;
ff7b1510 164}
c801d85f 165
0cf2cb36
RD
166int wxApp::OnRun(void)
167{
168 return MainLoop();
ff7b1510 169}
c801d85f 170
53010e52
RR
171bool wxApp::ProcessIdle(void)
172{
173 wxIdleEvent event;
174 event.SetEventObject( this );
175 ProcessEvent( event );
0cf2cb36 176
53010e52 177 return event.MoreRequested();
ff7b1510 178}
53010e52
RR
179
180void wxApp::OnIdle( wxIdleEvent &event )
c801d85f 181{
53010e52
RR
182 static bool inOnIdle = FALSE;
183
184 // Avoid recursion (via ProcessEvent default case)
185 if (inOnIdle)
186 return;
187
188 inOnIdle = TRUE;
189
190 // 'Garbage' collection of windows deleted with Close().
c801d85f 191 DeletePendingObjects();
53010e52
RR
192
193 // flush the logged messages if any
194 wxLog *pLog = wxLog::GetActiveTarget();
195 if ( pLog != NULL && pLog->HasPendingMessages() )
196 pLog->Flush();
197
198 // Send OnIdle events to all windows
199 bool needMore = SendIdleEvents();
200
201 if (needMore)
202 event.RequestMore(TRUE);
203
204 inOnIdle = FALSE;
ff7b1510 205}
53010e52
RR
206
207bool wxApp::SendIdleEvents(void)
208{
209 bool needMore = FALSE;
210 wxNode* node = wxTopLevelWindows.First();
211 while (node)
212 {
213 wxWindow* win = (wxWindow*) node->Data();
214 if (SendIdleEvents(win))
215 needMore = TRUE;
216
217 node = node->Next();
218 }
219 return needMore;
ff7b1510 220}
53010e52
RR
221
222bool wxApp::SendIdleEvents( wxWindow* win )
223{
224 bool needMore = FALSE;
225
226 wxIdleEvent event;
227 event.SetEventObject(win);
228 win->ProcessEvent(event);
229
230 if (event.MoreRequested())
231 needMore = TRUE;
232
233 wxNode* node = win->GetChildren()->First();
234 while (node)
235 {
236 wxWindow* win = (wxWindow*) node->Data();
237 if (SendIdleEvents(win))
238 needMore = TRUE;
239
240 node = node->Next();
241 }
242 return needMore ;
ff7b1510 243}
c801d85f
KB
244
245int wxApp::OnExit(void)
246{
247 return 0;
ff7b1510 248}
c801d85f
KB
249
250int wxApp::MainLoop(void)
251{
252 gtk_main();
253 return 0;
ff7b1510 254}
c801d85f
KB
255
256void wxApp::ExitMainLoop(void)
257{
258 gtk_main_quit();
ff7b1510 259}
c801d85f
KB
260
261bool wxApp::Initialized(void)
262{
263 return m_initialized;
ff7b1510 264}
c801d85f 265
0cf2cb36 266bool wxApp::Pending(void)
c801d85f
KB
267{
268 return FALSE;
ff7b1510 269}
c801d85f 270
0cf2cb36 271void wxApp::Dispatch(void)
c801d85f 272{
ff7b1510 273}
c801d85f
KB
274
275void wxApp::DeletePendingObjects(void)
276{
277 wxNode *node = wxPendingDelete.First();
278 while (node)
279 {
280 wxObject *obj = (wxObject *)node->Data();
0cf2cb36 281
c801d85f
KB
282 delete obj;
283
284 if (wxPendingDelete.Member(obj))
285 delete node;
286
287 node = wxPendingDelete.First();
ff7b1510
RR
288 }
289}
c801d85f
KB
290
291wxWindow *wxApp::GetTopWindow(void)
292{
293 if (m_topWindow) return m_topWindow;
294 wxNode *node = wxTopLevelWindows.First();
c67daf87 295 if (!node) return (wxWindow *) NULL;
c801d85f 296 return (wxWindow*)node->Data();
ff7b1510 297}
c801d85f
KB
298
299void wxApp::SetTopWindow( wxWindow *win )
300{
301 m_topWindow = win;
ff7b1510 302}
c801d85f
KB
303
304void wxApp::CommonInit(void)
305{
306
307/*
47d67540 308#if wxUSE_RESOURCES
c801d85f
KB
309 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
310#endif
311*/
a3622daa
VZ
312 wxSystemSettings::Init();
313 wxTheResourceCache = new wxResourceCache(wxKEY_STRING);
314
315 wxTheFontNameDirectory = new wxFontNameDirectory;
316 wxTheFontNameDirectory->Initialize();
c801d85f
KB
317
318 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
319 wxTheColourDatabase->Initialize();
a3622daa
VZ
320
321 wxInitializeStockLists();
c801d85f
KB
322 wxInitializeStockObjects();
323
8d71b555 324 wxInitializeResourceSystem();
0cf2cb36 325
c801d85f 326 // For PostScript printing
47d67540 327#if wxUSE_POSTSCRIPT
dfad0599 328/* Now done in wxPostScriptModule
c801d85f
KB
329 wxInitializePrintSetupData();
330 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
331 wxThePrintPaperDatabase->CreateDatabase();
dfad0599 332 */
c801d85f
KB
333#endif
334
335
336/*
337 wxBitmap::InitStandardHandlers();
338
339 g_globalCursor = new wxCursor;
340*/
ff7b1510 341}
c801d85f
KB
342
343void wxApp::CommonCleanUp(void)
344{
a3622daa 345 wxDELETE(wxTheColourDatabase);
dfad0599 346/* Now done in wxPostScriptModule
a3622daa
VZ
347 wxDELETE(wxThePrintPaperDatabase);
348 wxDELETE(wxThePrintSetupData);
dfad0599 349 */
a3622daa 350 wxDELETE(wxTheFontNameDirectory);
c801d85f 351 wxDeleteStockObjects();
0cf2cb36 352
c801d85f 353 wxFlushResources();
a3622daa
VZ
354
355 wxDELETE(wxTheResourceCache);
356
357 wxDeleteStockLists();
358
8d71b555 359 wxCleanUpResourceSystem();
0cf2cb36 360
a3622daa 361 wxSystemSettings::Done();
ff7b1510 362}
0cf2cb36 363
c801d85f
KB
364wxLog *wxApp::CreateLogTarget()
365{
366 return new wxLogGui;
367}
368
369//-----------------------------------------------------------------------------
370// wxEntry
371//-----------------------------------------------------------------------------
372
373int wxEntry( int argc, char *argv[] )
374{
375 wxBuffer = new char[BUFSIZ + 512];
376
377 wxClassInfo::InitializeClasses();
0cf2cb36 378
47d67540 379#if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
46dc76ba 380
46dc76ba 381 streambuf* sBuf = new wxDebugStreamBuf;
46dc76ba
RR
382 ostream* oStr = new ostream(sBuf) ;
383 wxDebugContext::SetStream(oStr, sBuf);
384
385#endif
0cf2cb36 386
c801d85f
KB
387 if (!wxTheApp)
388 {
389 if (!wxApp::GetInitializerFunction())
390 {
8429bec1 391 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
c801d85f 392 return 0;
ff7b1510 393 }
0cf2cb36 394
c801d85f 395 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
0cf2cb36 396
c801d85f 397 wxObject *test_app = app_ini();
0cf2cb36 398
c801d85f 399 wxTheApp = (wxApp*) test_app;
ff7b1510 400 }
0cf2cb36
RD
401
402 if (!wxTheApp)
c801d85f 403 {
2f2aa628 404 printf( "wxWindows error: wxTheApp == NULL\n" );
c801d85f 405 return 0;
ff7b1510 406 }
c801d85f 407
c801d85f
KB
408 wxTheApp->argc = argc;
409 wxTheApp->argv = argv;
0cf2cb36 410
e55ad60e
RR
411 char name[200];
412 strcpy( name, argv[0] );
413 strcpy( name, wxFileNameFromPath(name) );
414 wxStripExtension( name );
415 wxTheApp->SetAppName( name );
416
edaa81ae
RR
417 gtk_set_locale();
418
c801d85f
KB
419 gtk_init( &argc, &argv );
420
01111366 421 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
0cf2cb36 422
01111366
RR
423 for (int i = 0; i < 64; i++)
424 {
425 GdkColor col;
426 col.red = g_palette[i*3 + 0] << 8;
427 col.green = g_palette[i*3 + 1] << 8;
428 col.blue = g_palette[i*3 + 2] << 8;
429 col.pixel = 0;
430
431 gdk_color_alloc( cmap, &col );
432 }
433
434 gtk_widget_push_colormap( cmap );
435
436 gtk_widget_set_default_colormap( cmap );
0cf2cb36 437
c801d85f
KB
438 wxApp::CommonInit();
439
440 wxTheApp->OnInitGui();
441
442 // Here frames insert themselves automatically
443 // into wxTopLevelWindows by getting created
444 // in OnInit().
0cf2cb36 445
c801d85f
KB
446 if (!wxTheApp->OnInit()) return 0;
447
448 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
0cf2cb36 449
c801d85f 450 int retValue = 0;
0cf2cb36 451
c801d85f 452 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
0cf2cb36
RD
453
454 wxTheApp->DeletePendingObjects();
455
c801d85f 456 wxTheApp->OnExit();
0cf2cb36 457
c801d85f 458 wxApp::CommonCleanUp();
a3622daa
VZ
459
460 wxDELETE(wxTheApp);
0cf2cb36 461
e55ad60e
RR
462 wxLog *oldLog = wxLog::SetActiveTarget( NULL );
463 if (oldLog) delete oldLog;
464
465 wxClassInfo::CleanUpClasses();
466
467 delete[] wxBuffer;
468
47d67540 469#if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
e55ad60e 470
46dc76ba
RR
471 if (wxDebugContext::CountObjectsLeft() > 0)
472 {
473 wxTrace("There were memory leaks.\n");
474 wxDebugContext::Dump();
475 wxDebugContext::PrintStatistics();
476 }
477 wxDebugContext::SetStream(NULL, NULL);
e55ad60e 478
46dc76ba 479#endif
0cf2cb36 480
c801d85f 481 return retValue;
ff7b1510 482}
1a56f55c
RR
483
484//-----------------------------------------------------------------------------
485// main()
486//-----------------------------------------------------------------------------
487
0cf2cb36 488#if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
1a56f55c
RR
489
490 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
491
492#else
493
494 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
495
496#endif
497
498