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