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