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