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