insert member funcs PositionToXY and XYToPosition
[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{
117 gtk_main_quit();
ff7b1510 118}
c801d85f
KB
119
120bool wxYield(void)
121{
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{
ff7b1510 138 if (wxTheApp) while (wxTheApp->ProcessIdle()) {}
c801d85f
KB
139 usleep( 10000 );
140 return TRUE;
ff7b1510 141}
c801d85f
KB
142
143wxApp::wxApp()
144{
145 m_idleTag = 0;
c67daf87 146 m_topWindow = (wxWindow *) NULL;
c801d85f 147 m_exitOnFrameDelete = TRUE;
0cf2cb36 148 wxTheApp = this;
ff7b1510 149}
c801d85f
KB
150
151wxApp::~wxApp(void)
152{
153 gtk_idle_remove( m_idleTag );
ff7b1510 154}
c801d85f
KB
155
156bool wxApp::OnInit(void)
157{
158 return TRUE;
ff7b1510 159}
c801d85f
KB
160
161bool wxApp::OnInitGui(void)
0cf2cb36 162{
c801d85f 163 m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL );
0cf2cb36 164 return TRUE;
ff7b1510 165}
c801d85f 166
0cf2cb36
RD
167int wxApp::OnRun(void)
168{
169 return MainLoop();
ff7b1510 170}
c801d85f 171
53010e52
RR
172bool wxApp::ProcessIdle(void)
173{
174 wxIdleEvent event;
175 event.SetEventObject( this );
176 ProcessEvent( event );
0cf2cb36 177
53010e52 178 return event.MoreRequested();
ff7b1510 179}
53010e52
RR
180
181void wxApp::OnIdle( wxIdleEvent &event )
c801d85f 182{
53010e52
RR
183 static bool inOnIdle = FALSE;
184
185 // Avoid recursion (via ProcessEvent default case)
186 if (inOnIdle)
187 return;
188
189 inOnIdle = TRUE;
190
191 // 'Garbage' collection of windows deleted with Close().
c801d85f 192 DeletePendingObjects();
53010e52
RR
193
194 // flush the logged messages if any
195 wxLog *pLog = wxLog::GetActiveTarget();
196 if ( pLog != NULL && pLog->HasPendingMessages() )
197 pLog->Flush();
198
199 // Send OnIdle events to all windows
200 bool needMore = SendIdleEvents();
201
202 if (needMore)
203 event.RequestMore(TRUE);
204
205 inOnIdle = FALSE;
ff7b1510 206}
53010e52
RR
207
208bool wxApp::SendIdleEvents(void)
209{
210 bool needMore = FALSE;
211 wxNode* node = wxTopLevelWindows.First();
212 while (node)
213 {
214 wxWindow* win = (wxWindow*) node->Data();
215 if (SendIdleEvents(win))
216 needMore = TRUE;
217
218 node = node->Next();
219 }
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{
248 return 0;
ff7b1510 249}
c801d85f
KB
250
251int wxApp::MainLoop(void)
252{
253 gtk_main();
254 return 0;
ff7b1510 255}
c801d85f
KB
256
257void wxApp::ExitMainLoop(void)
258{
259 gtk_main_quit();
ff7b1510 260}
c801d85f
KB
261
262bool wxApp::Initialized(void)
263{
264 return m_initialized;
ff7b1510 265}
c801d85f 266
0cf2cb36 267bool wxApp::Pending(void)
c801d85f
KB
268{
269 return FALSE;
ff7b1510 270}
c801d85f 271
0cf2cb36 272void wxApp::Dispatch(void)
c801d85f 273{
ff7b1510 274}
c801d85f
KB
275
276void wxApp::DeletePendingObjects(void)
277{
278 wxNode *node = wxPendingDelete.First();
279 while (node)
280 {
281 wxObject *obj = (wxObject *)node->Data();
0cf2cb36 282
c801d85f
KB
283 delete obj;
284
285 if (wxPendingDelete.Member(obj))
286 delete node;
287
288 node = wxPendingDelete.First();
ff7b1510
RR
289 }
290}
c801d85f
KB
291
292wxWindow *wxApp::GetTopWindow(void)
293{
294 if (m_topWindow) return m_topWindow;
295 wxNode *node = wxTopLevelWindows.First();
c67daf87 296 if (!node) return (wxWindow *) NULL;
c801d85f 297 return (wxWindow*)node->Data();
ff7b1510 298}
c801d85f
KB
299
300void wxApp::SetTopWindow( wxWindow *win )
301{
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{
a3622daa 334 wxDELETE(wxTheColourDatabase);
a3622daa 335 wxDELETE(wxTheFontNameDirectory);
c801d85f 336 wxDeleteStockObjects();
0cf2cb36 337
c801d85f 338 wxFlushResources();
a3622daa
VZ
339
340 wxDELETE(wxTheResourceCache);
341
342 wxDeleteStockLists();
343
8d71b555 344 wxCleanUpResourceSystem();
fd0eed64
RR
345
346 wxImage::CleanUpHandlers();
0cf2cb36 347
a3622daa 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{
362 wxBuffer = new char[BUFSIZ + 512];
363
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
c801d85f
KB
375 if (!wxTheApp)
376 {
377 if (!wxApp::GetInitializerFunction())
378 {
8429bec1 379 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
c801d85f 380 return 0;
ff7b1510 381 }
0cf2cb36 382
c801d85f 383 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
0cf2cb36 384
c801d85f 385 wxObject *test_app = app_ini();
0cf2cb36 386
c801d85f 387 wxTheApp = (wxApp*) test_app;
ff7b1510 388 }
0cf2cb36
RD
389
390 if (!wxTheApp)
c801d85f 391 {
2f2aa628 392 printf( "wxWindows error: wxTheApp == NULL\n" );
c801d85f 393 return 0;
ff7b1510 394 }
c801d85f 395
c801d85f
KB
396 wxTheApp->argc = argc;
397 wxTheApp->argv = argv;
0cf2cb36 398
e55ad60e
RR
399 char name[200];
400 strcpy( name, argv[0] );
401 strcpy( name, wxFileNameFromPath(name) );
402 wxStripExtension( name );
403 wxTheApp->SetAppName( name );
404
edaa81ae
RR
405 gtk_set_locale();
406
c801d85f
KB
407 gtk_init( &argc, &argv );
408
01111366 409 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
0cf2cb36 410
01111366
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;
418
419 gdk_color_alloc( cmap, &col );
420 }
421
422 gtk_widget_push_colormap( cmap );
423
424 gtk_widget_set_default_colormap( cmap );
0cf2cb36 425
c801d85f
KB
426 wxApp::CommonInit();
427
031b2a7b
RR
428 wxModule::RegisterModules();
429 if (!wxModule::InitializeModules()) return FALSE;
430
c801d85f
KB
431 wxTheApp->OnInitGui();
432
433 // Here frames insert themselves automatically
434 // into wxTopLevelWindows by getting created
435 // in OnInit().
0cf2cb36 436
c801d85f
KB
437 if (!wxTheApp->OnInit()) return 0;
438
439 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
0cf2cb36 440
c801d85f 441 int retValue = 0;
0cf2cb36 442
c801d85f 443 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
0cf2cb36
RD
444
445 wxTheApp->DeletePendingObjects();
446
c801d85f 447 wxTheApp->OnExit();
0cf2cb36 448
031b2a7b
RR
449 wxModule::CleanUpModules();
450
c801d85f 451 wxApp::CommonCleanUp();
a3622daa
VZ
452
453 wxDELETE(wxTheApp);
0cf2cb36 454
e55ad60e
RR
455 wxClassInfo::CleanUpClasses();
456
457 delete[] wxBuffer;
458
ea57084d 459#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
e55ad60e 460
46dc76ba
RR
461 if (wxDebugContext::CountObjectsLeft() > 0)
462 {
184b5d99 463 wxLogDebug("There were memory leaks.\n");
46dc76ba
RR
464 wxDebugContext::Dump();
465 wxDebugContext::PrintStatistics();
466 }
184b5d99 467// wxDebugContext::SetStream(NULL, NULL);
e55ad60e 468
46dc76ba 469#endif
0cf2cb36 470
184b5d99
JS
471 wxLog *oldLog = wxLog::SetActiveTarget( NULL );
472 if (oldLog) delete oldLog;
473
474
c801d85f 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