]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
if the wxTextCtrl is empty set by SetInsertionPointEnd to 0 and not to -1.
[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 (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 (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 if(event.type == PropertyNotify)
285 {
286 HandlePropertyChange((WXEvent*) &event);
287 } else
288 {
289 // Terry Gitnick <terryg@scientech.com> - 1/21/98
290 /* if resize event, don't resize until the last resize event for this
291 window is recieved. Prevents flicker as windows are resized. */
292 if (event.type == ResizeRequest)
293 {
294 Display *disp = XtDisplay((Widget) wxTheApp->GetTopLevelWidget());
295 Window win = event.xany.window;
296 XEvent report;
297
298 // to avoid flicker
299 report = event;
300 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
301 }
302 // TODO: when implementing refresh optimization, we can use
303 // XtAddExposureToRegion to expand the window's paint region.
304
305 XtDispatchEvent(&event);
306
307 ProcessIdle();
308 }
309 }
310
311 return 0;
312 }
313
314 // Returns TRUE if more time is needed.
315 bool wxApp::ProcessIdle()
316 {
317 wxIdleEvent event;
318 event.SetEventObject(this);
319 ProcessEvent(event);
320
321 return event.MoreRequested();
322 }
323
324 void wxApp::ExitMainLoop()
325 {
326 m_keepGoing = FALSE;
327 }
328
329 // Is a message/event pending?
330 bool wxApp::Pending()
331 {
332 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
333 return (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) != 0) ;
334 }
335
336 // Dispatch a message.
337 void wxApp::Dispatch()
338 {
339 XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
340 }
341
342 // This should be redefined in a derived class for
343 // handling property change events for XAtom IPC.
344 void wxApp::HandlePropertyChange(WXEvent *event)
345 {
346 // by default do nothing special
347 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
348 }
349
350 void wxApp::OnIdle(wxIdleEvent& event)
351 {
352 static bool inOnIdle = FALSE;
353
354 // Avoid recursion (via ProcessEvent default case)
355 if (inOnIdle)
356 return;
357
358 inOnIdle = TRUE;
359
360 // 'Garbage' collection of windows deleted with Close().
361 DeletePendingObjects();
362
363 // flush the logged messages if any
364 wxLog *pLog = wxLog::GetActiveTarget();
365 if ( pLog != NULL && pLog->HasPendingMessages() )
366 pLog->Flush();
367
368 // Send OnIdle events to all windows
369 bool needMore = SendIdleEvents();
370
371 if (needMore)
372 event.RequestMore(TRUE);
373
374 inOnIdle = FALSE;
375 }
376
377 // Send idle event to all top-level windows
378 bool wxApp::SendIdleEvents()
379 {
380 bool needMore = FALSE;
381 wxNode* node = wxTopLevelWindows.First();
382 while (node)
383 {
384 wxWindow* win = (wxWindow*) node->Data();
385 if (SendIdleEvents(win))
386 needMore = TRUE;
387
388 node = node->Next();
389 }
390 return needMore;
391 }
392
393 // Send idle event to window and all subwindows
394 bool wxApp::SendIdleEvents(wxWindow* win)
395 {
396 bool needMore = FALSE;
397
398 wxIdleEvent event;
399 event.SetEventObject(win);
400 win->ProcessEvent(event);
401
402 if (event.MoreRequested())
403 needMore = TRUE;
404
405 wxNode* node = win->GetChildren()->First();
406 while (node)
407 {
408 wxWindow* win = (wxWindow*) node->Data();
409 if (SendIdleEvents(win))
410 needMore = TRUE;
411
412 node = node->Next();
413 }
414 return needMore ;
415 }
416
417 void wxApp::DeletePendingObjects()
418 {
419 wxNode *node = wxPendingDelete.First();
420 while (node)
421 {
422 wxObject *obj = (wxObject *)node->Data();
423
424 delete obj;
425
426 if (wxPendingDelete.Member(obj))
427 delete node;
428
429 // Deleting one object may have deleted other pending
430 // objects, so start from beginning of list again.
431 node = wxPendingDelete.First();
432 }
433 }
434
435 wxLog* wxApp::CreateLogTarget()
436 {
437 return new wxLogGui;
438 }
439
440 wxWindow* wxApp::GetTopWindow() const
441 {
442 if (m_topWindow)
443 return m_topWindow;
444 else if (wxTopLevelWindows.Number() > 0)
445 return (wxWindow*) wxTopLevelWindows.First()->Data();
446 else
447 return NULL;
448 }
449
450 // Create an application context
451 bool wxApp::OnInitGui()
452 {
453 XtToolkitInitialize() ;
454 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
455 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
456 (const char*) wxTheApp->GetClassName(), NULL,
457 # if XtSpecificationRelease < 5
458 0,(Cardinal*) &argc,argv) ;
459 # else
460 0,&argc,argv) ;
461 # endif
462 if (!dpy) {
463 cerr << "wxWindows could not open display for " << wxTheApp->GetClassName() << ": exiting.\n";
464 exit(-1);
465 }
466 m_initialDisplay = (WXDisplay*) dpy;
467
468 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
469 applicationShellWidgetClass,dpy,
470 NULL,0) ;
471
472 // Add general resize proc
473 XtActionsRec rec;
474 rec.string = "resize";
475 rec.proc = (XtActionProc)wxWidgetResizeProc;
476 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
477
478 GetMainColormap(dpy);
479 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
480
481 return TRUE;
482 }
483
484 WXColormap wxApp::GetMainColormap(WXDisplay* display)
485 {
486 if (!display) /* Must be called first with non-NULL display */
487 return m_mainColormap;
488
489 Colormap c =
490 DefaultColormapOfScreen(XScreenOfDisplay((Display*) display,
491 DefaultScreen((Display*) display)));
492
493 if (!m_mainColormap)
494 m_mainColormap = (WXColormap) c;
495
496 return (WXColormap) c;
497 }
498
499 void wxExit()
500 {
501 int retValue = 0;
502 if (wxTheApp)
503 retValue = wxTheApp->OnExit();
504
505 wxApp::CleanUp();
506 /*
507 * Exit in some platform-specific way. Not recommended that the app calls this:
508 * only for emergencies.
509 */
510 exit(retValue);
511 }
512
513 // Yield to other processes
514 bool wxYield()
515 {
516 while (wxTheApp && wxTheApp->Pending())
517 wxTheApp->Dispatch();
518 return TRUE;
519 }
520