]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/app.cpp
wxNO_BORDER honoured by wxTextCtrl
[wxWidgets.git] / src / motif / app.cpp
... / ...
CommitLineData
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#include "wx/thread.h"
33
34#if wxUSE_WX_RESOURCES
35 #include "wx/resource.h"
36#endif
37
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)
57
58BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
59 EVT_IDLE(wxApp::OnIdle)
60END_EVENT_TABLE()
61#endif
62
63long wxApp::sm_lastMessageTime = 0;
64
65bool wxApp::Initialize()
66{
67 wxBuffer = new char[BUFSIZ + 512];
68
69 wxClassInfo::InitializeClasses();
70
71 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
72 wxTheColourDatabase->Initialize();
73
74 wxInitializeStockLists();
75 wxInitializeStockObjects();
76
77#if wxUSE_WX_RESOURCES
78 wxInitializeResourceSystem();
79#endif
80
81 // For PostScript printing
82#if wxUSE_POSTSCRIPT
83 /* Done using wxModule now
84 wxInitializePrintSetupData();
85 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
86 wxThePrintPaperDatabase->CreateDatabase();
87 */
88#endif
89
90 wxBitmap::InitStandardHandlers();
91
92 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
93
94 wxModule::RegisterModules();
95 if (!wxModule::InitializeModules()) return FALSE;
96
97 return TRUE;
98}
99
100void wxApp::CleanUp()
101{
102 delete wxWidgetHashTable;
103 wxWidgetHashTable = NULL;
104
105 wxModule::CleanUpModules();
106
107#if wxUSE_WX_RESOURCES
108 wxCleanUpResourceSystem();
109#endif
110
111 wxDeleteStockObjects() ;
112
113 // Destroy all GDI lists, etc.
114
115 delete wxTheBrushList;
116 wxTheBrushList = NULL;
117
118 delete wxThePenList;
119 wxThePenList = NULL;
120
121 delete wxTheFontList;
122 wxTheFontList = NULL;
123
124 delete wxTheBitmapList;
125 wxTheBitmapList = NULL;
126
127 delete wxTheColourDatabase;
128 wxTheColourDatabase = NULL;
129
130#if wxUSE_POSTSCRIPT
131 /* Done using wxModule now
132 wxInitializePrintSetupData(FALSE);
133 delete wxThePrintPaperDatabase;
134 wxThePrintPaperDatabase = NULL;
135 */
136#endif
137
138 wxBitmap::CleanUpHandlers();
139
140 delete[] wxBuffer;
141 wxBuffer = NULL;
142
143 wxClassInfo::CleanUpClasses();
144
145 delete wxTheApp;
146 wxTheApp = NULL;
147
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.
153 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
154 {
155 wxLogDebug("There were memory leaks.\n");
156 wxDebugContext::Dump();
157 wxDebugContext::PrintStatistics();
158 }
159#endif
160
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{
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
178
179 if (!wxApp::Initialize())
180 return FALSE;
181
182 if (!wxTheApp)
183 {
184 if (!wxApp::GetInitializerFunction())
185 {
186 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
187 return 0;
188 };
189
190 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
191 };
192
193 if (!wxTheApp)
194 {
195 printf( "wxWindows error: wxTheApp == NULL\n" );
196 return 0;
197 };
198
199 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
200 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
201
202 wxTheApp->argc = argc;
203 wxTheApp->argv = argv;
204
205 // GUI-specific initialization, such as creating an app context.
206 wxTheApp->OnInitGui();
207
208 // Here frames insert themselves automatically
209 // into wxTopLevelWindows by getting created
210 // in OnInit().
211
212 int retValue = 0;
213 if (wxTheApp->OnInit())
214 {
215 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
216 }
217
218 // flush the logged messages if any
219 wxLog *pLog = wxLog::GetActiveTarget();
220 if ( pLog != NULL && pLog->HasPendingMessages() )
221 pLog->Flush();
222
223 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
224 // for further messages
225
226 if (wxTheApp->GetTopWindow())
227 {
228 delete wxTheApp->GetTopWindow();
229 wxTheApp->SetTopWindow(NULL);
230 }
231
232 wxTheApp->DeletePendingObjects();
233
234 wxTheApp->OnExit();
235
236 wxApp::CleanUp();
237
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;
256
257 m_mainColormap = (WXColormap) NULL;
258 m_appContext = (WXAppContext) NULL;
259 m_topLevelWidget = (WXWidget) NULL;
260 m_maxRequestSize = 0;
261 m_initialDisplay = (WXDisplay*) 0;
262}
263
264bool wxApp::Initialized()
265{
266 if (GetTopWindow())
267 return TRUE;
268 else
269 return FALSE;
270}
271
272int wxApp::MainLoop()
273{
274 m_keepGoing = TRUE;
275
276 /*
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 */
282
283 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
284 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
285 PropertyChangeMask);
286
287 XEvent event;
288
289 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
290 while (m_keepGoing)
291 {
292 XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event);
293
294 ProcessXEvent((WXEvent*) & event);
295
296 if (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) == 0)
297 {
298 if (!ProcessIdle())
299 {
300 // leave the main loop to give other threads a chance to
301 // perform their GUI work
302 wxMutexGuiLeave();
303 wxUsleep(20);
304 wxMutexGuiEnter();
305 }
306 }
307
308 }
309
310 return 0;
311}
312
313// Processes an X event.
314void 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.
355bool wxApp::ProcessIdle()
356{
357 wxIdleEvent event;
358 event.SetEventObject(this);
359 ProcessEvent(event);
360
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() ));
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.
380void 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.
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;
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
425bool 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
441bool 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
464void 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
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,
503 (const char*) wxTheApp->GetClassName(), NULL, 0,
504# if XtSpecificationRelease < 5
505 (Cardinal*) &argc,
506# else
507 &argc,
508# endif
509 argv);
510
511 if (!dpy) {
512 wxLogError(_("wxWindows could not open display for '%s': exiting."),
513 wxTheApp->GetClassName());
514 exit(-1);
515 }
516 m_initialDisplay = (WXDisplay*) dpy;
517
518 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
519 applicationShellWidgetClass,dpy,
520 NULL,0) ;
521
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);
527
528 GetMainColormap(dpy);
529 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
530
531 return TRUE;
532}
533
534WXColormap wxApp::GetMainColormap(WXDisplay* display)
535{
536 if (!display) /* Must be called first with non-NULL display */
537 return m_mainColormap;
538
539 int defaultScreen = DefaultScreen((Display*) display);
540 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
541
542 Colormap c = DefaultColormapOfScreen(screen);
543
544 if (!m_mainColormap)
545 m_mainColormap = (WXColormap) c;
546
547 return (WXColormap) c;
548}
549
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;
560
561 // Find the first wxWindow that corresponds to this event window
562 while (widget && !(win = wxGetWindowFromTable(widget)))
563 widget = XtParent(widget);
564
565 if (!widget || !win)
566 return FALSE;
567
568 wxKeyEvent keyEvent(wxEVT_CHAR);
569 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
570
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
585void wxExit()
586{
587 int retValue = 0;
588 if (wxTheApp)
589 retValue = wxTheApp->OnExit();
590
591 wxApp::CleanUp();
592 /*
593 * Exit in some platform-specific way. Not recommended that the app calls this:
594 * only for emergencies.
595 */
596 exit(retValue);
597}
598
599// Yield to other processes
600bool wxYield()
601{
602 while (wxTheApp && wxTheApp->Pending())
603 wxTheApp->Dispatch();
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
610 return TRUE;
611}
612