]> git.saurik.com Git - wxWidgets.git/blame - src/motif/app.cpp
wxCommandEvent::SetString() change - now uses wxString
[wxWidgets.git] / src / motif / app.cpp
CommitLineData
4bb6408c
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
afb74891 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "app.h"
14#endif
15
16#include "wx/frame.h"
17#include "wx/app.h"
18#include "wx/utils.h"
19#include "wx/gdicmn.h"
20#include "wx/pen.h"
21#include "wx/brush.h"
22#include "wx/cursor.h"
23#include "wx/icon.h"
24#include "wx/palette.h"
25#include "wx/dc.h"
26#include "wx/dialog.h"
27#include "wx/msgdlg.h"
28#include "wx/log.h"
29#include "wx/module.h"
30#include "wx/memory.h"
b412f9be 31
aaa38880 32#include "wx/thread.h"
4bb6408c 33
47d67540 34#if wxUSE_WX_RESOURCES
9c6e1335 35 #include "wx/resource.h"
4bb6408c
JS
36#endif
37
4bb6408c
JS
38#include <Xm/Xm.h>
39#include <X11/Xlib.h>
40#include <X11/Xutil.h>
41#include <X11/Xresource.h>
42#include <X11/Xatom.h>
43
44#include "wx/motif/private.h"
45
46#include <string.h>
47
48extern char *wxBuffer;
49extern wxList wxPendingDelete;
50
51wxApp *wxTheApp = NULL;
52
53wxHashTable *wxWidgetHashTable = NULL;
54
55#if !USE_SHARED_LIBRARY
56IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
9c6e1335 57
4bb6408c 58BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
9c6e1335 59 EVT_IDLE(wxApp::OnIdle)
4bb6408c
JS
60END_EVENT_TABLE()
61#endif
62
63long wxApp::sm_lastMessageTime = 0;
64
65bool wxApp::Initialize()
66{
4bb6408c 67 wxBuffer = new char[BUFSIZ + 512];
afb74891 68
4bb6408c 69 wxClassInfo::InitializeClasses();
afb74891 70
4bb6408c
JS
71 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
72 wxTheColourDatabase->Initialize();
afb74891 73
4b5f3fe6 74 wxInitializeStockLists();
4bb6408c 75 wxInitializeStockObjects();
afb74891 76
47d67540 77#if wxUSE_WX_RESOURCES
4bb6408c
JS
78 wxInitializeResourceSystem();
79#endif
afb74891 80
2d120f83 81 // For PostScript printing
47d67540 82#if wxUSE_POSTSCRIPT
2d120f83 83 /* Done using wxModule now
4bb6408c
JS
84 wxInitializePrintSetupData();
85 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
86 wxThePrintPaperDatabase->CreateDatabase();
2d120f83 87 */
4bb6408c 88#endif
afb74891 89
4bb6408c 90 wxBitmap::InitStandardHandlers();
afb74891 91
4bb6408c 92 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
afb74891 93
4bb6408c 94 wxModule::RegisterModules();
aaa38880 95 if (!wxModule::InitializeModules()) return FALSE;
afb74891 96
4bb6408c
JS
97 return TRUE;
98}
99
100void wxApp::CleanUp()
101{
102 delete wxWidgetHashTable;
103 wxWidgetHashTable = NULL;
afb74891 104
4bb6408c 105 wxModule::CleanUpModules();
afb74891 106
47d67540 107#if wxUSE_WX_RESOURCES
4bb6408c
JS
108 wxCleanUpResourceSystem();
109#endif
afb74891 110
4bb6408c 111 wxDeleteStockObjects() ;
afb74891 112
4bb6408c 113 // Destroy all GDI lists, etc.
afb74891 114
4bb6408c
JS
115 delete wxTheBrushList;
116 wxTheBrushList = NULL;
afb74891 117
4bb6408c
JS
118 delete wxThePenList;
119 wxThePenList = NULL;
afb74891 120
4bb6408c
JS
121 delete wxTheFontList;
122 wxTheFontList = NULL;
afb74891 123
4bb6408c
JS
124 delete wxTheBitmapList;
125 wxTheBitmapList = NULL;
afb74891 126
4bb6408c
JS
127 delete wxTheColourDatabase;
128 wxTheColourDatabase = NULL;
afb74891 129
47d67540 130#if wxUSE_POSTSCRIPT
2d120f83 131 /* Done using wxModule now
4bb6408c
JS
132 wxInitializePrintSetupData(FALSE);
133 delete wxThePrintPaperDatabase;
134 wxThePrintPaperDatabase = NULL;
2d120f83 135 */
4bb6408c 136#endif
afb74891 137
4bb6408c 138 wxBitmap::CleanUpHandlers();
afb74891 139
4bb6408c
JS
140 delete[] wxBuffer;
141 wxBuffer = NULL;
afb74891 142
4bb6408c 143 wxClassInfo::CleanUpClasses();
afb74891 144
184b5d99
JS
145 delete wxTheApp;
146 wxTheApp = NULL;
afb74891 147
184b5d99
JS
148#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
149 // At this point we want to check if there are any memory
150 // blocks that aren't part of the wxDebugContext itself,
151 // as a special case. Then when dumping we need to ignore
152 // wxDebugContext, too.
4fabb575 153 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
184b5d99 154 {
2d120f83
JS
155 wxLogDebug("There were memory leaks.\n");
156 wxDebugContext::Dump();
157 wxDebugContext::PrintStatistics();
184b5d99 158 }
184b5d99 159#endif
afb74891 160
4bb6408c
JS
161 // do it as the very last thing because everything else can log messages
162 wxLog::DontCreateOnDemand();
163 // do it as the very last thing because everything else can log messages
164 delete wxLog::SetActiveTarget(NULL);
165}
166
167int wxEntry( int argc, char *argv[] )
168{
4fabb575
JS
169#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
170 // This seems to be necessary since there are 'rogue'
171 // objects present at this point (perhaps global objects?)
172 // Setting a checkpoint will ignore them as far as the
173 // memory checking facility is concerned.
174 // Of course you may argue that memory allocated in globals should be
175 // checked, but this is a reasonable compromise.
176 wxDebugContext::SetCheckpoint();
177#endif
afb74891 178
4bb6408c 179 if (!wxApp::Initialize())
2d120f83 180 return FALSE;
afb74891 181
4bb6408c
JS
182 if (!wxTheApp)
183 {
2d120f83
JS
184 if (!wxApp::GetInitializerFunction())
185 {
186 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
187 return 0;
188 };
afb74891 189
2d120f83 190 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
4bb6408c 191 };
afb74891 192
4bb6408c
JS
193 if (!wxTheApp)
194 {
2d120f83
JS
195 printf( "wxWindows error: wxTheApp == NULL\n" );
196 return 0;
4bb6408c 197 };
afb74891 198
4bb6408c
JS
199 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
200 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
afb74891 201
4bb6408c
JS
202 wxTheApp->argc = argc;
203 wxTheApp->argv = argv;
afb74891 204
4bb6408c
JS
205 // GUI-specific initialization, such as creating an app context.
206 wxTheApp->OnInitGui();
afb74891 207
4bb6408c
JS
208 // Here frames insert themselves automatically
209 // into wxTopLevelWindows by getting created
210 // in OnInit().
afb74891 211
4bb6408c 212 int retValue = 0;
4fabb575
JS
213 if (wxTheApp->OnInit())
214 {
2d120f83 215 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
4fabb575 216 }
afb74891 217
f57fe24c
JS
218 // flush the logged messages if any
219 wxLog *pLog = wxLog::GetActiveTarget();
220 if ( pLog != NULL && pLog->HasPendingMessages() )
2d120f83 221 pLog->Flush();
afb74891 222
f57fe24c 223 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
2d120f83 224 // for further messages
afb74891 225
4bb6408c
JS
226 if (wxTheApp->GetTopWindow())
227 {
2d120f83
JS
228 delete wxTheApp->GetTopWindow();
229 wxTheApp->SetTopWindow(NULL);
4bb6408c 230 }
afb74891 231
4bb6408c 232 wxTheApp->DeletePendingObjects();
afb74891 233
4bb6408c 234 wxTheApp->OnExit();
afb74891 235
4bb6408c 236 wxApp::CleanUp();
afb74891 237
4bb6408c
JS
238 return retValue;
239};
240
241// Static member initialization
242wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
243
244wxApp::wxApp()
245{
246 m_topWindow = NULL;
247 wxTheApp = this;
248 m_className = "";
249 m_wantDebugOutput = TRUE ;
250 m_appName = "";
251 argc = 0;
252 argv = NULL;
253 m_printMode = wxPRINT_POSTSCRIPT;
254 m_exitOnFrameDelete = TRUE;
255 m_auto3D = TRUE;
afb74891 256
4bb6408c
JS
257 m_mainColormap = (WXColormap) NULL;
258 m_appContext = (WXAppContext) NULL;
259 m_topLevelWidget = (WXWidget) NULL;
260 m_maxRequestSize = 0;
47bc1060 261 m_initialDisplay = (WXDisplay*) 0;
4bb6408c
JS
262}
263
264bool wxApp::Initialized()
265{
266 if (GetTopWindow())
2d120f83 267 return TRUE;
4bb6408c 268 else
2d120f83 269 return FALSE;
4bb6408c
JS
270}
271
272int wxApp::MainLoop()
273{
274 m_keepGoing = TRUE;
afb74891 275
2d120f83 276 /*
4bb6408c
JS
277 * Sit around forever waiting to process X-events. Property Change
278 * event are handled special, because they have to refer to
279 * the root window rather than to a widget. therefore we can't
280 * use an Xt-eventhandler.
281 */
afb74891 282
4bb6408c 283 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
2d120f83
JS
284 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
285 PropertyChangeMask);
afb74891 286
4bb6408c 287 XEvent event;
afb74891 288
4bb6408c
JS
289 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
290 while (m_keepGoing)
291 {
2d120f83 292 XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event);
afb74891 293
2d120f83 294 ProcessXEvent((WXEvent*) & event);
afb74891 295
2d120f83
JS
296 if (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) == 0)
297 {
298 if (!ProcessIdle())
afb74891
VZ
299 {
300 // leave the main loop to give other threads a chance to
301 // perform their GUI work
2d120f83 302 wxMutexGuiLeave();
afb74891 303 wxUsleep(20);
2d120f83 304 wxMutexGuiEnter();
2d120f83
JS
305 }
306 }
afb74891 307
8aa04e8b 308 }
afb74891 309
8aa04e8b
JS
310 return 0;
311}
312
313// Processes an X event.
314void wxApp::ProcessXEvent(WXEvent* _event)
315{
316 XEvent* event = (XEvent*) _event;
afb74891 317
f57fe24c 318 if ((event->type == KeyPress) && CheckForAccelerator(_event))
8aa04e8b
JS
319 {
320 // Do nothing! We intercepted and processed the event as an accelerator.
321 return;
322 }
323 else if (event->type == PropertyNotify)
324 {
325 HandlePropertyChange(_event);
326 return;
327 }
328 else if (event->type == ResizeRequest)
329 {
2d120f83
JS
330 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
331 * If resize event, don't resize until the last resize event for this
332 * window is recieved. Prevents flicker as windows are resized.
333 */
afb74891 334
8aa04e8b
JS
335 Display *disp = XtDisplay((Widget) wxTheApp->GetTopLevelWidget());
336 Window win = event->xany.window;
337 XEvent report;
afb74891 338
8aa04e8b
JS
339 // to avoid flicker
340 report = * event;
341 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
afb74891 342
f97c9854
JS
343 // TODO: when implementing refresh optimization, we can use
344 // XtAddExposureToRegion to expand the window's paint region.
afb74891 345
8aa04e8b
JS
346 XtDispatchEvent(event);
347 }
348 else
349 {
350 XtDispatchEvent(event);
4bb6408c 351 }
4bb6408c
JS
352}
353
354// Returns TRUE if more time is needed.
355bool wxApp::ProcessIdle()
356{
357 wxIdleEvent event;
358 event.SetEventObject(this);
359 ProcessEvent(event);
afb74891 360
4bb6408c
JS
361 return event.MoreRequested();
362}
363
364void wxApp::ExitMainLoop()
365{
366 m_keepGoing = FALSE;
367}
368
369// Is a message/event pending?
370bool wxApp::Pending()
371{
372 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
afb74891 373
8aa04e8b
JS
374 // Fix by Doug from STI, to prevent a stall if non-X event
375 // is found.
376 return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ;
4bb6408c
JS
377}
378
379// Dispatch a message.
380void wxApp::Dispatch()
381{
2d120f83 382 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
afb74891 383
8aa04e8b
JS
384 XEvent event;
385 XtAppNextEvent((XtAppContext) GetAppContext(), &event);
386 ProcessXEvent((WXEvent*) & event);
4bb6408c
JS
387}
388
389// This should be redefined in a derived class for
390// handling property change events for XAtom IPC.
391void wxApp::HandlePropertyChange(WXEvent *event)
392{
393 // by default do nothing special
394 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
395}
396
397void wxApp::OnIdle(wxIdleEvent& event)
398{
399 static bool inOnIdle = FALSE;
afb74891 400
4bb6408c
JS
401 // Avoid recursion (via ProcessEvent default case)
402 if (inOnIdle)
2d120f83 403 return;
afb74891 404
4bb6408c 405 inOnIdle = TRUE;
afb74891 406
4bb6408c
JS
407 // 'Garbage' collection of windows deleted with Close().
408 DeletePendingObjects();
afb74891 409
4bb6408c
JS
410 // flush the logged messages if any
411 wxLog *pLog = wxLog::GetActiveTarget();
412 if ( pLog != NULL && pLog->HasPendingMessages() )
2d120f83 413 pLog->Flush();
afb74891 414
4bb6408c
JS
415 // Send OnIdle events to all windows
416 bool needMore = SendIdleEvents();
afb74891 417
4bb6408c 418 if (needMore)
2d120f83 419 event.RequestMore(TRUE);
afb74891 420
4bb6408c
JS
421 inOnIdle = FALSE;
422}
423
424// Send idle event to all top-level windows
425bool wxApp::SendIdleEvents()
426{
427 bool needMore = FALSE;
2d120f83
JS
428 wxNode* node = wxTopLevelWindows.First();
429 while (node)
430 {
431 wxWindow* win = (wxWindow*) node->Data();
432 if (SendIdleEvents(win))
4bb6408c 433 needMore = TRUE;
afb74891 434
2d120f83
JS
435 node = node->Next();
436 }
4bb6408c
JS
437 return needMore;
438}
439
440// Send idle event to window and all subwindows
441bool wxApp::SendIdleEvents(wxWindow* win)
442{
443 bool needMore = FALSE;
afb74891 444
2d120f83
JS
445 wxIdleEvent event;
446 event.SetEventObject(win);
447 win->ProcessEvent(event);
afb74891 448
4bb6408c
JS
449 if (event.MoreRequested())
450 needMore = TRUE;
afb74891 451
2d120f83
JS
452 wxNode* node = win->GetChildren().First();
453 while (node)
454 {
455 wxWindow* win = (wxWindow*) node->Data();
456 if (SendIdleEvents(win))
4bb6408c 457 needMore = TRUE;
afb74891 458
2d120f83
JS
459 node = node->Next();
460 }
4bb6408c
JS
461 return needMore ;
462}
463
464void wxApp::DeletePendingObjects()
465{
466 wxNode *node = wxPendingDelete.First();
467 while (node)
468 {
2d120f83 469 wxObject *obj = (wxObject *)node->Data();
afb74891 470
2d120f83 471 delete obj;
afb74891 472
2d120f83
JS
473 if (wxPendingDelete.Member(obj))
474 delete node;
afb74891 475
2d120f83
JS
476 // Deleting one object may have deleted other pending
477 // objects, so start from beginning of list again.
478 node = wxPendingDelete.First();
4bb6408c
JS
479 }
480}
481
482wxLog* wxApp::CreateLogTarget()
483{
484 return new wxLogGui;
485}
486
487wxWindow* wxApp::GetTopWindow() const
488{
489 if (m_topWindow)
490 return m_topWindow;
491 else if (wxTopLevelWindows.Number() > 0)
492 return (wxWindow*) wxTopLevelWindows.First()->Data();
493 else
494 return NULL;
495}
496
497// Create an application context
498bool wxApp::OnInitGui()
499{
500 XtToolkitInitialize() ;
501 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
502 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
9c6e1335 503 (const char*) wxTheApp->GetClassName(), NULL, 0,
4bb6408c 504# if XtSpecificationRelease < 5
9c6e1335 505 (Cardinal*) &argc,
4bb6408c 506# else
9c6e1335 507 &argc,
4bb6408c 508# endif
9c6e1335
VZ
509 argv);
510
4bb6408c 511 if (!dpy) {
9c6e1335
VZ
512 wxLogError(_("wxWindows could not open display for '%s': exiting."),
513 wxTheApp->GetClassName());
2d120f83 514 exit(-1);
4bb6408c 515 }
47bc1060 516 m_initialDisplay = (WXDisplay*) dpy;
afb74891 517
4bb6408c 518 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
2d120f83
JS
519 applicationShellWidgetClass,dpy,
520 NULL,0) ;
afb74891 521
4bb6408c
JS
522 // Add general resize proc
523 XtActionsRec rec;
524 rec.string = "resize";
525 rec.proc = (XtActionProc)wxWidgetResizeProc;
526 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
afb74891 527
4bb6408c
JS
528 GetMainColormap(dpy);
529 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
afb74891 530
4bb6408c
JS
531 return TRUE;
532}
533
534WXColormap wxApp::GetMainColormap(WXDisplay* display)
535{
536 if (!display) /* Must be called first with non-NULL display */
2d120f83 537 return m_mainColormap;
a91b47e8
JS
538
539 int defaultScreen = DefaultScreen((Display*) display);
540 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
afb74891 541
a91b47e8 542 Colormap c = DefaultColormapOfScreen(screen);
afb74891 543
4bb6408c 544 if (!m_mainColormap)
2d120f83 545 m_mainColormap = (WXColormap) c;
afb74891 546
4bb6408c
JS
547 return (WXColormap) c;
548}
549
8aa04e8b
JS
550// Returns TRUE if an accelerator has been processed
551bool wxApp::CheckForAccelerator(WXEvent* event)
552{
553 XEvent* xEvent = (XEvent*) event;
554 if (xEvent->xany.type == KeyPress)
555 {
556 // Find a wxWindow for this window
557 // TODO: should get display for the window, not the current display
558 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), xEvent->xany.window);
559 wxWindow* win = NULL;
afb74891 560
8aa04e8b
JS
561 // Find the first wxWindow that corresponds to this event window
562 while (widget && !(win = wxGetWindowFromTable(widget)))
563 widget = XtParent(widget);
afb74891 564
8aa04e8b
JS
565 if (!widget || !win)
566 return FALSE;
afb74891 567
8aa04e8b
JS
568 wxKeyEvent keyEvent(wxEVT_CHAR);
569 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
afb74891 570
8aa04e8b
JS
571 // Now we have a wxKeyEvent and we have a wxWindow.
572 // Go up the hierarchy until we find a matching accelerator,
573 // or we get to the top.
574 while (win)
575 {
576 if (win->ProcessAccelerator(keyEvent))
577 return TRUE;
578 win = win->GetParent();
579 }
580 return FALSE;
581 }
582 return FALSE;
583}
584
4bb6408c
JS
585void wxExit()
586{
587 int retValue = 0;
588 if (wxTheApp)
2d120f83 589 retValue = wxTheApp->OnExit();
afb74891 590
4bb6408c
JS
591 wxApp::CleanUp();
592 /*
2d120f83
JS
593 * Exit in some platform-specific way. Not recommended that the app calls this:
594 * only for emergencies.
595 */
4bb6408c
JS
596 exit(retValue);
597}
598
599// Yield to other processes
600bool wxYield()
601{
602 while (wxTheApp && wxTheApp->Pending())
2d120f83 603 wxTheApp->Dispatch();
518b5d2f
VZ
604
605 // VZ: is it the same as this (taken from old wxExecute)?
606#if 0
607 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
608#endif
609
4bb6408c
JS
610 return TRUE;
611}
612