]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/app.cpp
use unsigned for wxString [] operator
[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"
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
fd0eed64
RR
326 wxImage::InitStandardHandlers();
327
328// g_globalCursor = new wxCursor;
ff7b1510 329}
c801d85f
KB
330
331void wxApp::CommonCleanUp(void)
332{
a3622daa 333 wxDELETE(wxTheColourDatabase);
a3622daa 334 wxDELETE(wxTheFontNameDirectory);
c801d85f 335 wxDeleteStockObjects();
0cf2cb36 336
c801d85f 337 wxFlushResources();
a3622daa
VZ
338
339 wxDELETE(wxTheResourceCache);
340
341 wxDeleteStockLists();
342
8d71b555 343 wxCleanUpResourceSystem();
fd0eed64
RR
344
345 wxImage::CleanUpHandlers();
0cf2cb36 346
a3622daa 347 wxSystemSettings::Done();
ff7b1510 348}
0cf2cb36 349
c801d85f
KB
350wxLog *wxApp::CreateLogTarget()
351{
352 return new wxLogGui;
353}
354
355//-----------------------------------------------------------------------------
356// wxEntry
357//-----------------------------------------------------------------------------
358
359int wxEntry( int argc, char *argv[] )
360{
361 wxBuffer = new char[BUFSIZ + 512];
362
363 wxClassInfo::InitializeClasses();
0cf2cb36 364
184b5d99 365 /* Debug stream no longer used
ea57084d 366#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
46dc76ba 367
46dc76ba 368 streambuf* sBuf = new wxDebugStreamBuf;
46dc76ba
RR
369 ostream* oStr = new ostream(sBuf) ;
370 wxDebugContext::SetStream(oStr, sBuf);
46dc76ba 371#endif
184b5d99 372*/
0cf2cb36 373
c801d85f
KB
374 if (!wxTheApp)
375 {
376 if (!wxApp::GetInitializerFunction())
377 {
8429bec1 378 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
c801d85f 379 return 0;
ff7b1510 380 }
0cf2cb36 381
c801d85f 382 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
0cf2cb36 383
c801d85f 384 wxObject *test_app = app_ini();
0cf2cb36 385
c801d85f 386 wxTheApp = (wxApp*) test_app;
ff7b1510 387 }
0cf2cb36
RD
388
389 if (!wxTheApp)
c801d85f 390 {
2f2aa628 391 printf( "wxWindows error: wxTheApp == NULL\n" );
c801d85f 392 return 0;
ff7b1510 393 }
c801d85f 394
c801d85f
KB
395 wxTheApp->argc = argc;
396 wxTheApp->argv = argv;
0cf2cb36 397
e55ad60e
RR
398 char name[200];
399 strcpy( name, argv[0] );
400 strcpy( name, wxFileNameFromPath(name) );
401 wxStripExtension( name );
402 wxTheApp->SetAppName( name );
403
edaa81ae
RR
404 gtk_set_locale();
405
c801d85f
KB
406 gtk_init( &argc, &argv );
407
01111366 408 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
0cf2cb36 409
01111366
RR
410 for (int i = 0; i < 64; i++)
411 {
412 GdkColor col;
413 col.red = g_palette[i*3 + 0] << 8;
414 col.green = g_palette[i*3 + 1] << 8;
415 col.blue = g_palette[i*3 + 2] << 8;
416 col.pixel = 0;
417
418 gdk_color_alloc( cmap, &col );
419 }
420
421 gtk_widget_push_colormap( cmap );
422
423 gtk_widget_set_default_colormap( cmap );
0cf2cb36 424
c801d85f
KB
425 wxApp::CommonInit();
426
427 wxTheApp->OnInitGui();
428
429 // Here frames insert themselves automatically
430 // into wxTopLevelWindows by getting created
431 // in OnInit().
0cf2cb36 432
c801d85f
KB
433 if (!wxTheApp->OnInit()) return 0;
434
435 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
0cf2cb36 436
c801d85f 437 int retValue = 0;
0cf2cb36 438
c801d85f 439 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
0cf2cb36
RD
440
441 wxTheApp->DeletePendingObjects();
442
c801d85f 443 wxTheApp->OnExit();
0cf2cb36 444
c801d85f 445 wxApp::CommonCleanUp();
a3622daa
VZ
446
447 wxDELETE(wxTheApp);
0cf2cb36 448
e55ad60e
RR
449 wxClassInfo::CleanUpClasses();
450
451 delete[] wxBuffer;
452
ea57084d 453#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
e55ad60e 454
46dc76ba
RR
455 if (wxDebugContext::CountObjectsLeft() > 0)
456 {
184b5d99 457 wxLogDebug("There were memory leaks.\n");
46dc76ba
RR
458 wxDebugContext::Dump();
459 wxDebugContext::PrintStatistics();
460 }
184b5d99 461// wxDebugContext::SetStream(NULL, NULL);
e55ad60e 462
46dc76ba 463#endif
0cf2cb36 464
184b5d99
JS
465 wxLog *oldLog = wxLog::SetActiveTarget( NULL );
466 if (oldLog) delete oldLog;
467
468
c801d85f 469 return retValue;
ff7b1510 470}
1a56f55c
RR
471
472//-----------------------------------------------------------------------------
473// main()
474//-----------------------------------------------------------------------------
475
0cf2cb36 476#if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
1a56f55c
RR
477
478 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
479
480#else
481
482 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
483
484#endif
485
486