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