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