]> git.saurik.com Git - wxWidgets.git/blob - src/x11/app.cpp
74fb19c9af6c29fad664456ec36fea48a19cf935
[wxWidgets.git] / src / x11 / 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/icon.h"
21 #include "wx/dialog.h"
22 #include "wx/log.h"
23 #include "wx/module.h"
24 #include "wx/memory.h"
25 #include "wx/log.h"
26 #include "wx/intl.h"
27 #include "wx/evtloop.h"
28 #include "wx/timer.h"
29
30 #include "wx/univ/theme.h"
31 #include "wx/univ/renderer.h"
32
33 #define ABS(a) (((a) < 0) ? -(a) : (a))
34
35 #if wxUSE_THREADS
36 #include "wx/thread.h"
37 #endif
38
39 #if wxUSE_WX_RESOURCES
40 #include "wx/resource.h"
41 #endif
42
43 #include "wx/x11/private.h"
44
45 #include <string.h>
46
47 //------------------------------------------------------------------------
48 // global data
49 //------------------------------------------------------------------------
50
51 extern wxList wxPendingDelete;
52
53 wxHashTable *wxWidgetHashTable = NULL;
54 wxHashTable *wxClientWidgetHashTable = NULL;
55
56 wxApp *wxTheApp = NULL;
57
58 // This is set within wxEntryStart -- too early on
59 // to put these in wxTheApp
60 static int g_newArgc = 0;
61 static wxChar** g_newArgv = NULL;
62 static bool g_showIconic = FALSE;
63 static wxSize g_initialSize = wxDefaultSize;
64
65 // This is required for wxFocusEvent::SetWindow(). It will only
66 // work for focus events which we provoke ourselves (by calling
67 // SetFocus()). It will not work for those events, which X11
68 // generates itself.
69 static wxWindow *g_nextFocus = NULL;
70 static wxWindow *g_prevFocus = NULL;
71
72 //------------------------------------------------------------------------
73 // X11 error handling
74 //------------------------------------------------------------------------
75
76 #ifdef __WXDEBUG__
77 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
78
79 XErrorHandlerFunc gs_pfnXErrorHandler = 0;
80
81 static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
82 {
83 // just forward to the default handler for now
84 if (gs_pfnXErrorHandler)
85 return gs_pfnXErrorHandler(dpy, xevent);
86 else
87 return 0;
88 }
89 #endif // __WXDEBUG__
90
91 //------------------------------------------------------------------------
92 // wxApp
93 //------------------------------------------------------------------------
94
95 long wxApp::sm_lastMessageTime = 0;
96 WXDisplay *wxApp::ms_display = NULL;
97
98 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
99
100 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
101 EVT_IDLE(wxApp::OnIdle)
102 END_EVENT_TABLE()
103
104 bool wxApp::Initialize()
105 {
106 wxClassInfo::InitializeClasses();
107
108 #if wxUSE_INTL
109 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
110 #endif
111
112 // GL: I'm annoyed ... I don't know where to put this and I don't want to
113 // create a module for that as it's part of the core.
114 #if wxUSE_THREADS
115 wxPendingEventsLocker = new wxCriticalSection();
116 #endif
117
118 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
119 wxTheColourDatabase->Initialize();
120
121 wxInitializeStockLists();
122 wxInitializeStockObjects();
123
124 #if wxUSE_WX_RESOURCES
125 wxInitializeResourceSystem();
126 #endif
127
128 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
129 wxClientWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
130
131 wxModule::RegisterModules();
132 if (!wxModule::InitializeModules()) return FALSE;
133
134 return TRUE;
135 }
136
137 void wxApp::CleanUp()
138 {
139 if (g_newArgv)
140 delete[] g_newArgv;
141 g_newArgv = NULL;
142
143 delete wxWidgetHashTable;
144 wxWidgetHashTable = NULL;
145 delete wxClientWidgetHashTable;
146 wxClientWidgetHashTable = NULL;
147
148 wxModule::CleanUpModules();
149
150 #if wxUSE_WX_RESOURCES
151 wxCleanUpResourceSystem();
152 #endif
153
154 delete wxTheColourDatabase;
155 wxTheColourDatabase = NULL;
156
157 wxDeleteStockObjects();
158
159 wxDeleteStockLists();
160
161 delete wxTheApp;
162 wxTheApp = NULL;
163
164 wxClassInfo::CleanUpClasses();
165
166 #if wxUSE_THREADS
167 delete wxPendingEvents;
168 delete wxPendingEventsLocker;
169 #endif
170
171 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
172 // At this point we want to check if there are any memory
173 // blocks that aren't part of the wxDebugContext itself,
174 // as a special case. Then when dumping we need to ignore
175 // wxDebugContext, too.
176 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
177 {
178 wxLogDebug("There were memory leaks.");
179 wxDebugContext::Dump();
180 wxDebugContext::PrintStatistics();
181 }
182 #endif
183
184 // do it as the very last thing because everything else can log messages
185 wxLog::DontCreateOnDemand();
186 // do it as the very last thing because everything else can log messages
187 delete wxLog::SetActiveTarget(NULL);
188 }
189
190 // NB: argc and argv may be changed here, pass by reference!
191 int wxEntryStart( int& argc, char *argv[] )
192 {
193 #ifdef __WXDEBUG__
194 #if !wxUSE_NANOX
195 // install the X error handler
196 gs_pfnXErrorHandler = XSetErrorHandler( wxXErrorHandler );
197 #endif
198 #endif // __WXDEBUG__
199
200 wxString displayName;
201 bool syncDisplay = FALSE;
202
203 // Parse the arguments.
204 // We can't use wxCmdLineParser or OnInitCmdLine and friends because
205 // we have to create the Display earlier. If we can find a way to
206 // use the wxAppBase API then I'll be quite happy to change it.
207 g_newArgv = new wxChar*[argc + 1];
208 g_newArgc = 0;
209 int i;
210 for (i = 0; i < argc; i++)
211 {
212 wxString arg(argv[i]);
213 if (arg == wxT("-display"))
214 {
215 if (i < (argc - 1))
216 {
217 i ++;
218 displayName = argv[i];
219 continue;
220 }
221 }
222 else if (arg == wxT("-geometry"))
223 {
224 if (i < (argc - 1))
225 {
226 i ++;
227 wxString windowGeometry = argv[i];
228 int w, h;
229 if (wxSscanf(windowGeometry.c_str(), _T("%dx%d"), &w, &h) != 2)
230 {
231 wxLogError(_("Invalid geometry specification '%s'"), windowGeometry.c_str());
232 }
233 else
234 {
235 g_initialSize = wxSize(w, h);
236 }
237 continue;
238 }
239 }
240 else if (arg == wxT("-sync"))
241 {
242 syncDisplay = TRUE;
243 continue;
244 }
245 else if (arg == wxT("-iconic"))
246 {
247 g_showIconic = TRUE;
248
249 continue;
250 }
251
252 // Not eaten by wxWindows, so pass through
253 g_newArgv[g_newArgc] = argv[i];
254 g_newArgc ++;
255 }
256 g_newArgv[g_newArgc] = NULL;
257
258 Display* xdisplay = NULL;
259 if (displayName.IsEmpty())
260 xdisplay = XOpenDisplay(NULL);
261 else
262 xdisplay = XOpenDisplay((char*) displayName.c_str());
263
264 if (!xdisplay)
265 {
266 wxLogError( _("wxWindows could not open display. Exiting.") );
267 return -1;
268 }
269
270 if (syncDisplay)
271 {
272 XSynchronize(xdisplay, True);
273 }
274
275 wxApp::ms_display = (WXDisplay*) xdisplay;
276
277 XSelectInput( xdisplay, XDefaultRootWindow(xdisplay), PropertyChangeMask);
278
279 wxSetDetectableAutoRepeat( TRUE );
280
281 if (!wxApp::Initialize())
282 return -1;
283
284 return 0;
285 }
286
287 int wxEntryInitGui()
288 {
289 int retValue = 0;
290
291 if ( !wxTheApp->OnInitGui() )
292 retValue = -1;
293
294 return retValue;
295 }
296
297
298 int wxEntry( int argc, char *argv[] )
299 {
300 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
301 // This seems to be necessary since there are 'rogue'
302 // objects present at this point (perhaps global objects?)
303 // Setting a checkpoint will ignore them as far as the
304 // memory checking facility is concerned.
305 // Of course you may argue that memory allocated in globals should be
306 // checked, but this is a reasonable compromise.
307 wxDebugContext::SetCheckpoint();
308 #endif
309 int err = wxEntryStart(argc, argv);
310 if (err)
311 return err;
312
313 if (!wxTheApp)
314 {
315 if (!wxApp::GetInitializerFunction())
316 {
317 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
318 return 0;
319 };
320
321 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
322 };
323
324 if (!wxTheApp)
325 {
326 printf( "wxWindows error: wxTheApp == NULL\n" );
327 return 0;
328 };
329
330 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
331 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
332
333 // The command line may have been changed
334 // by stripping out -display etc.
335 if (g_newArgc > 0)
336 {
337 wxTheApp->argc = g_newArgc;
338 wxTheApp->argv = g_newArgv;
339 }
340 else
341 {
342 wxTheApp->argc = argc;
343 wxTheApp->argv = argv;
344 }
345 wxTheApp->m_showIconic = g_showIconic;
346 wxTheApp->m_initialSize = g_initialSize;
347
348 int retValue;
349 retValue = wxEntryInitGui();
350
351 // Here frames insert themselves automatically into wxTopLevelWindows by
352 // getting created in OnInit().
353 if ( retValue == 0 )
354 {
355 if ( !wxTheApp->OnInit() )
356 retValue = -1;
357 }
358
359 if ( retValue == 0 )
360 {
361 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
362 }
363
364 // flush the logged messages if any
365 wxLog *pLog = wxLog::GetActiveTarget();
366 if ( pLog != NULL && pLog->HasPendingMessages() )
367 pLog->Flush();
368
369 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
370 // for further messages
371
372 if (wxTheApp->GetTopWindow())
373 {
374 delete wxTheApp->GetTopWindow();
375 wxTheApp->SetTopWindow(NULL);
376 }
377
378 wxTheApp->DeletePendingObjects();
379
380 wxTheApp->OnExit();
381
382 wxApp::CleanUp();
383
384 return retValue;
385 };
386
387 // Static member initialization
388 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
389
390 wxApp::wxApp()
391 {
392 // TODO: parse the command line
393 argc = 0;
394 argv = NULL;
395
396 m_mainColormap = (WXColormap) NULL;
397 m_topLevelWidget = (WXWindow) NULL;
398 m_maxRequestSize = 0;
399 m_mainLoop = NULL;
400 m_showIconic = FALSE;
401 m_initialSize = wxDefaultSize;
402
403 #if !wxUSE_NANOX
404 m_visualColormap = NULL;
405 m_colorCube = NULL;
406 #endif
407 }
408
409 wxApp::~wxApp()
410 {
411 #if !wxUSE_NANOX
412 if (m_colorCube)
413 free( m_colorCube );
414
415 if (m_visualColormap)
416 delete [] (XColor*)m_visualColormap;
417 #endif
418 }
419
420 bool wxApp::Initialized()
421 {
422 if (GetTopWindow())
423 return TRUE;
424 else
425 return FALSE;
426 }
427
428 int wxApp::MainLoop()
429 {
430 int rt;
431 m_mainLoop = new wxEventLoop;
432
433 rt = m_mainLoop->Run();
434
435 delete m_mainLoop;
436 m_mainLoop = NULL;
437 return rt;
438 }
439
440 #if !wxUSE_NANOX
441 //-----------------------------------------------------------------------
442 // X11 predicate function for exposure compression
443 //-----------------------------------------------------------------------
444
445 struct wxExposeInfo
446 {
447 Window window;
448 Bool found_non_matching;
449 };
450
451 static Bool expose_predicate (Display *display, XEvent *xevent, XPointer arg)
452 {
453 wxExposeInfo *info = (wxExposeInfo*) arg;
454
455 if (info->found_non_matching)
456 return FALSE;
457
458 if (xevent->xany.type != Expose)
459 {
460 info->found_non_matching = TRUE;
461 return FALSE;
462 }
463
464 if (xevent->xexpose.window != info->window)
465 {
466 info->found_non_matching = TRUE;
467 return FALSE;
468 }
469
470 return TRUE;
471 }
472 #endif
473 // wxUSE_NANOX
474
475 //-----------------------------------------------------------------------
476 // Processes an X event, returning TRUE if the event was processed.
477 //-----------------------------------------------------------------------
478
479 bool wxApp::ProcessXEvent(WXEvent* _event)
480 {
481 XEvent* event = (XEvent*) _event;
482
483 wxWindow* win = NULL;
484 Window window = XEventGetWindow(event);
485 #if 0
486 Window actualWindow = window;
487 #endif
488
489 // Find the first wxWindow that corresponds to this event window
490 // Because we're receiving events after a window
491 // has been destroyed, assume a 1:1 match between
492 // Window and wxWindow, so if it's not in the table,
493 // it must have been destroyed.
494
495 win = wxGetWindowFromTable(window);
496 if (!win)
497 {
498 #if wxUSE_TWO_WINDOWS
499 win = wxGetClientWindowFromTable(window);
500 if (!win)
501 #endif
502 return FALSE;
503 }
504
505 #ifdef __WXDEBUG__
506 wxString windowClass = win->GetClassInfo()->GetClassName();
507 #endif
508
509 switch (event->type)
510 {
511 case Expose:
512 {
513 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
514 if (event->xexpose.window != (Window)win->GetClientAreaWindow())
515 {
516 XEvent tmp_event;
517 wxExposeInfo info;
518 info.window = event->xexpose.window;
519 info.found_non_matching = FALSE;
520 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, expose_predicate, (XPointer) &info ))
521 {
522 // Don't worry about optimizing redrawing the border etc.
523 }
524 win->NeedUpdateNcAreaInIdle();
525 }
526 else
527 #endif
528 {
529 win->GetUpdateRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
530 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
531 win->GetClearRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
532 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
533
534 #if !wxUSE_NANOX
535 XEvent tmp_event;
536 wxExposeInfo info;
537 info.window = event->xexpose.window;
538 info.found_non_matching = FALSE;
539 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, expose_predicate, (XPointer) &info ))
540 {
541 win->GetUpdateRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
542 tmp_event.xexpose.width, tmp_event.xexpose.height );
543
544 win->GetClearRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
545 tmp_event.xexpose.width, tmp_event.xexpose.height );
546 }
547 #endif
548
549 // This simplifies the expose and clear areas to simple
550 // rectangles.
551 win->GetUpdateRegion() = win->GetUpdateRegion().GetBox();
552 win->GetClearRegion() = win->GetClearRegion().GetBox();
553
554 // If we only have one X11 window, always indicate
555 // that borders might have to be redrawn.
556 if (win->GetMainWindow() == win->GetClientAreaWindow())
557 win->NeedUpdateNcAreaInIdle();
558
559 // Only erase background, paint in idle time.
560 win->SendEraseEvents();
561 // win->Update();
562 }
563
564 return TRUE;
565 }
566
567 #if !wxUSE_NANOX
568 case GraphicsExpose:
569 {
570 printf( "GraphicExpose event\n" );
571
572 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win->GetName().c_str());
573
574 win->GetUpdateRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
575 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
576
577 win->GetClearRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
578 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
579
580 if (event->xgraphicsexpose.count == 0)
581 {
582 // Only erase background, paint in idle time.
583 win->SendEraseEvents();
584 // win->Update();
585 }
586
587 return TRUE;
588 }
589 #endif
590
591 case KeyPress:
592 {
593 if (!win->IsEnabled())
594 return FALSE;
595
596 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
597 wxTranslateKeyEvent(keyEvent, win, window, event);
598
599 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
600
601 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
602 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
603 return TRUE;
604
605 keyEvent.SetEventType(wxEVT_CHAR);
606 if (win->GetEventHandler()->ProcessEvent( keyEvent ))
607 return TRUE;
608
609 if ( (keyEvent.m_keyCode == WXK_TAB) &&
610 win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
611 {
612 wxNavigationKeyEvent new_event;
613 new_event.SetEventObject( win->GetParent() );
614 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
615 new_event.SetDirection( (keyEvent.m_keyCode == WXK_TAB) );
616 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
617 new_event.SetWindowChange( keyEvent.ControlDown() );
618 new_event.SetCurrentFocus( win );
619 return win->GetParent()->GetEventHandler()->ProcessEvent( new_event );
620 }
621
622 return FALSE;
623 }
624 case KeyRelease:
625 {
626 if (!win->IsEnabled())
627 return FALSE;
628
629 wxKeyEvent keyEvent(wxEVT_KEY_UP);
630 wxTranslateKeyEvent(keyEvent, win, window, event);
631
632 return win->GetEventHandler()->ProcessEvent( keyEvent );
633 }
634 case ConfigureNotify:
635 {
636 #if wxUSE_NANOX
637 if (event->update.utype == GR_UPDATE_SIZE)
638 #endif
639 {
640 if (win->IsTopLevel())
641 {
642 wxTopLevelWindow *tlw = (wxTopLevelWindow*) win;
643 tlw->SetConfigureGeometry( XConfigureEventGetX(event), XConfigureEventGetY(event),
644 XConfigureEventGetWidth(event), XConfigureEventGetHeight(event) );
645 }
646
647 if (win->IsTopLevel() && win->IsShown())
648 {
649 wxTopLevelWindowX11 *tlw = (wxTopLevelWindowX11 *) win;
650 tlw->SetNeedResizeInIdle();
651 }
652 else
653 {
654 wxSizeEvent sizeEvent( wxSize(XConfigureEventGetWidth(event), XConfigureEventGetHeight(event)), win->GetId() );
655 sizeEvent.SetEventObject( win );
656
657 return win->GetEventHandler()->ProcessEvent( sizeEvent );
658 }
659 }
660 return FALSE;
661 break;
662 }
663 #if !wxUSE_NANOX
664 case PropertyNotify:
665 {
666 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
667 return HandlePropertyChange(_event);
668 }
669 case ClientMessage:
670 {
671 if (!win->IsEnabled())
672 return FALSE;
673
674 Atom wm_delete_window = XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True);
675 Atom wm_protocols = XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True);
676
677 if (event->xclient.message_type == wm_protocols)
678 {
679 if ((Atom) (event->xclient.data.l[0]) == wm_delete_window)
680 {
681 win->Close(FALSE);
682 return TRUE;
683 }
684 }
685 return FALSE;
686 }
687 #if 0
688 case DestroyNotify:
689 {
690 printf( "destroy from %s\n", win->GetName().c_str() );
691 break;
692 }
693 case CreateNotify:
694 {
695 printf( "create from %s\n", win->GetName().c_str() );
696 break;
697 }
698 case MapRequest:
699 {
700 printf( "map request from %s\n", win->GetName().c_str() );
701 break;
702 }
703 case ResizeRequest:
704 {
705 printf( "resize request from %s\n", win->GetName().c_str() );
706
707 Display *disp = (Display*) wxGetDisplay();
708 XEvent report;
709
710 // to avoid flicker
711 report = * event;
712 while( XCheckTypedWindowEvent (disp, actualWindow, ResizeRequest, &report));
713
714 wxSize sz = win->GetSize();
715 wxSizeEvent sizeEvent(sz, win->GetId());
716 sizeEvent.SetEventObject(win);
717
718 return win->GetEventHandler()->ProcessEvent( sizeEvent );
719 }
720 #endif
721 #endif
722 #if wxUSE_NANOX
723 case GR_EVENT_TYPE_CLOSE_REQ:
724 {
725 if (win)
726 {
727 win->Close(FALSE);
728 return TRUE;
729 }
730 return FALSE;
731 break;
732 }
733 #endif
734 case EnterNotify:
735 case LeaveNotify:
736 case ButtonPress:
737 case ButtonRelease:
738 case MotionNotify:
739 {
740 if (!win->IsEnabled())
741 return FALSE;
742
743 // Here we check if the top level window is
744 // disabled, which is one aspect of modality.
745 wxWindow *tlw = win;
746 while (tlw && !tlw->IsTopLevel())
747 tlw = tlw->GetParent();
748 if (tlw && !tlw->IsEnabled())
749 return FALSE;
750
751 if (event->type == ButtonPress)
752 {
753 if ((win != wxWindow::FindFocus()) && win->AcceptsFocus())
754 {
755 // This might actually be done in wxWindow::SetFocus()
756 // and not here. TODO.
757 g_prevFocus = wxWindow::FindFocus();
758 g_nextFocus = win;
759
760 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
761
762 // Record the fact that this window is
763 // getting the focus, because we'll need to
764 // check if its parent is getting a bogus
765 // focus and duly ignore it.
766 // TODO: may need to have this code in SetFocus, too.
767 extern wxWindow* g_GettingFocus;
768 g_GettingFocus = win;
769 win->SetFocus();
770 }
771 }
772
773 #if !wxUSE_NANOX
774 if (event->type == LeaveNotify || event->type == EnterNotify)
775 {
776 // Throw out NotifyGrab and NotifyUngrab
777 if (event->xcrossing.mode != NotifyNormal)
778 return FALSE;
779 }
780 #endif
781 wxMouseEvent wxevent;
782 wxTranslateMouseEvent(wxevent, win, window, event);
783 return win->GetEventHandler()->ProcessEvent( wxevent );
784 }
785 case FocusIn:
786 {
787 #if !wxUSE_NANOX
788 if ((event->xfocus.detail != NotifyPointer) &&
789 (event->xfocus.mode == NotifyNormal))
790 #endif
791 {
792 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
793
794 extern wxWindow* g_GettingFocus;
795 if (g_GettingFocus && g_GettingFocus->GetParent() == win)
796 {
797 // Ignore this, this can be a spurious FocusIn
798 // caused by a child having its focus set.
799 g_GettingFocus = NULL;
800 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
801 return TRUE;
802 }
803 else
804 {
805 wxFocusEvent focusEvent(wxEVT_SET_FOCUS, win->GetId());
806 focusEvent.SetEventObject(win);
807 focusEvent.SetWindow( g_prevFocus );
808 g_prevFocus = NULL;
809
810 return win->GetEventHandler()->ProcessEvent(focusEvent);
811 }
812 }
813 return FALSE;
814 break;
815 }
816 case FocusOut:
817 {
818 #if !wxUSE_NANOX
819 if ((event->xfocus.detail != NotifyPointer) &&
820 (event->xfocus.mode == NotifyNormal))
821 #endif
822 {
823 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
824
825 wxFocusEvent focusEvent(wxEVT_KILL_FOCUS, win->GetId());
826 focusEvent.SetEventObject(win);
827 focusEvent.SetWindow( g_nextFocus );
828 g_nextFocus = NULL;
829 return win->GetEventHandler()->ProcessEvent(focusEvent);
830 }
831 return FALSE;
832 break;
833 }
834 default:
835 {
836 #ifdef __WXDEBUG__
837 //wxString eventName = wxGetXEventName(XEvent& event);
838 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
839 #endif
840 return FALSE;
841 break;
842 }
843 }
844 return FALSE;
845 }
846
847 // Returns TRUE if more time is needed.
848 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
849 // but ProcessIdle may be needed by apps, so is kept.
850 bool wxApp::ProcessIdle()
851 {
852 wxIdleEvent event;
853 event.SetEventObject(this);
854 ProcessEvent(event);
855
856 return event.MoreRequested();
857 }
858
859 void wxApp::ExitMainLoop()
860 {
861 if (m_mainLoop)
862 m_mainLoop->Exit(0);
863 }
864
865 // Is a message/event pending?
866 bool wxApp::Pending()
867 {
868 return wxEventLoop::GetActive()->Pending();
869 }
870
871 // Dispatch a message.
872 void wxApp::Dispatch()
873 {
874 wxEventLoop::GetActive()->Dispatch();
875 }
876
877 // This should be redefined in a derived class for
878 // handling property change events for XAtom IPC.
879 bool wxApp::HandlePropertyChange(WXEvent *event)
880 {
881 // by default do nothing special
882 // TODO: what to do for X11
883 // XtDispatchEvent((XEvent*) event);
884 return FALSE;
885 }
886
887 void wxApp::OnIdle(wxIdleEvent& event)
888 {
889 static bool s_inOnIdle = FALSE;
890
891 // Avoid recursion (via ProcessEvent default case)
892 if (s_inOnIdle)
893 return;
894
895 s_inOnIdle = TRUE;
896
897 // Resend in the main thread events which have been prepared in other
898 // threads
899 ProcessPendingEvents();
900
901 // 'Garbage' collection of windows deleted with Close()
902 DeletePendingObjects();
903
904 // Send OnIdle events to all windows
905 bool needMore = SendIdleEvents();
906
907 if (needMore)
908 event.RequestMore(TRUE);
909
910 s_inOnIdle = FALSE;
911 }
912
913 void wxWakeUpIdle()
914 {
915 // **** please implement me! ****
916 // Wake up the idle handler processor, even if it is in another thread...
917 }
918
919
920 // Send idle event to all top-level windows
921 bool wxApp::SendIdleEvents()
922 {
923 bool needMore = FALSE;
924
925 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
926 while (node)
927 {
928 wxWindow* win = node->GetData();
929 if (SendIdleEvents(win))
930 needMore = TRUE;
931 node = node->GetNext();
932 }
933
934 return needMore;
935 }
936
937 // Send idle event to window and all subwindows
938 bool wxApp::SendIdleEvents(wxWindow* win)
939 {
940 bool needMore = FALSE;
941
942 wxIdleEvent event;
943 event.SetEventObject(win);
944
945 win->GetEventHandler()->ProcessEvent(event);
946
947 if (event.MoreRequested())
948 needMore = TRUE;
949
950 wxNode* node = win->GetChildren().First();
951 while (node)
952 {
953 wxWindow* win = (wxWindow*) node->Data();
954 if (SendIdleEvents(win))
955 needMore = TRUE;
956
957 node = node->Next();
958 }
959
960 win->OnInternalIdle();
961
962 return needMore;
963 }
964
965 void wxApp::DeletePendingObjects()
966 {
967 wxNode *node = wxPendingDelete.First();
968 while (node)
969 {
970 wxObject *obj = (wxObject *)node->Data();
971
972 delete obj;
973
974 if (wxPendingDelete.Member(obj))
975 delete node;
976
977 // Deleting one object may have deleted other pending
978 // objects, so start from beginning of list again.
979 node = wxPendingDelete.First();
980 }
981 }
982
983 static void wxCalcPrecAndShift( unsigned long mask, int *shift, int *prec )
984 {
985 *shift = 0;
986 *prec = 0;
987
988 while (!(mask & 0x1))
989 {
990 (*shift)++;
991 mask >>= 1;
992 }
993
994 while (mask & 0x1)
995 {
996 (*prec)++;
997 mask >>= 1;
998 }
999 }
1000
1001 // Create display, and other initialization
1002 bool wxApp::OnInitGui()
1003 {
1004 // Eventually this line will be removed, but for
1005 // now we don't want to try popping up a dialog
1006 // for error messages.
1007 delete wxLog::SetActiveTarget(new wxLogStderr);
1008
1009 if (!wxAppBase::OnInitGui())
1010 return FALSE;
1011
1012 GetMainColormap( wxApp::GetDisplay() );
1013
1014 m_maxRequestSize = XMaxRequestSize( (Display*) wxApp::GetDisplay() );
1015
1016 #if !wxUSE_NANOX
1017 // Get info about the current visual. It is enough
1018 // to do this once here unless we support different
1019 // visuals, displays and screens. Given that wxX11
1020 // mostly for embedded things, that is no real
1021 // limitation.
1022 Display *xdisplay = (Display*) wxApp::GetDisplay();
1023 int xscreen = DefaultScreen(xdisplay);
1024 Visual* xvisual = DefaultVisual(xdisplay,xscreen);
1025 int xdepth = DefaultDepth(xdisplay, xscreen);
1026
1027 XVisualInfo vinfo_template;
1028 vinfo_template.visual = xvisual;
1029 vinfo_template.visualid = XVisualIDFromVisual( xvisual );
1030 vinfo_template.depth = xdepth;
1031
1032 int nitem = 0;
1033 XVisualInfo *vi = XGetVisualInfo( xdisplay, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1034 wxASSERT_MSG( vi, wxT("No visual info") );
1035
1036 m_visualType = vi->visual->c_class;
1037 m_visualScreen = vi->screen;
1038
1039 m_visualRedMask = vi->red_mask;
1040 m_visualGreenMask = vi->green_mask;
1041 m_visualBlueMask = vi->blue_mask;
1042
1043 if (m_visualType != GrayScale && m_visualType != PseudoColor)
1044 {
1045 wxCalcPrecAndShift( m_visualRedMask, &m_visualRedShift, &m_visualRedPrec );
1046 wxCalcPrecAndShift( m_visualGreenMask, &m_visualGreenShift, &m_visualGreenPrec );
1047 wxCalcPrecAndShift( m_visualBlueMask, &m_visualBlueShift, &m_visualBluePrec );
1048 }
1049
1050 m_visualDepth = xdepth;
1051 if (xdepth == 16)
1052 xdepth = m_visualRedPrec + m_visualGreenPrec + m_visualBluePrec;
1053
1054 m_visualColormapSize = vi->colormap_size;
1055
1056 XFree( vi );
1057
1058 if (m_visualDepth > 8)
1059 return TRUE;
1060
1061 m_visualColormap = new XColor[m_visualColormapSize];
1062 XColor* colors = (XColor*) m_visualColormap;
1063
1064 for (int i = 0; i < m_visualColormapSize; i++)
1065 colors[i].pixel = i;
1066
1067 XQueryColors( xdisplay, DefaultColormap(xdisplay,xscreen), colors, m_visualColormapSize );
1068
1069 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
1070
1071 for (int r = 0; r < 32; r++)
1072 {
1073 for (int g = 0; g < 32; g++)
1074 {
1075 for (int b = 0; b < 32; b++)
1076 {
1077 int rr = (r << 3) | (r >> 2);
1078 int gg = (g << 3) | (g >> 2);
1079 int bb = (b << 3) | (b >> 2);
1080
1081 int index = -1;
1082
1083 if (colors)
1084 {
1085 int max = 3 * 65536;
1086
1087 for (int i = 0; i < m_visualColormapSize; i++)
1088 {
1089 int rdiff = ((rr << 8) - colors[i].red);
1090 int gdiff = ((gg << 8) - colors[i].green);
1091 int bdiff = ((bb << 8) - colors[i].blue);
1092 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1093 if (sum < max)
1094 {
1095 index = i; max = sum;
1096 }
1097 }
1098 }
1099 else
1100 {
1101 // assume 8-bit true or static colors. this really exists
1102 index = (r >> (5 - m_visualRedPrec)) << m_visualRedShift;
1103 index |= (g >> (5 - m_visualGreenPrec)) << m_visualGreenShift;
1104 index |= (b >> (5 - m_visualBluePrec)) << m_visualBlueShift;
1105 }
1106 m_colorCube[ (r*1024) + (g*32) + b ] = index;
1107 }
1108 }
1109 }
1110 #endif
1111
1112 return TRUE;
1113 }
1114
1115 WXColormap wxApp::GetMainColormap(WXDisplay* display)
1116 {
1117 if (!display) /* Must be called first with non-NULL display */
1118 return m_mainColormap;
1119
1120 int defaultScreen = DefaultScreen((Display*) display);
1121 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
1122
1123 Colormap c = DefaultColormapOfScreen(screen);
1124
1125 if (!m_mainColormap)
1126 m_mainColormap = (WXColormap) c;
1127
1128 return (WXColormap) c;
1129 }
1130
1131 Window wxGetWindowParent(Window window)
1132 {
1133 wxASSERT_MSG( window, "invalid window" );
1134
1135 return (Window) 0;
1136
1137 Window parent, root = 0;
1138 #if wxUSE_NANOX
1139 int noChildren = 0;
1140 #else
1141 unsigned int noChildren = 0;
1142 #endif
1143 Window* children = NULL;
1144
1145 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
1146 int res = 1;
1147 #if !wxUSE_NANOX
1148 res =
1149 #endif
1150 XQueryTree((Display*) wxGetDisplay(), window, & root, & parent,
1151 & children, & noChildren);
1152 if (children)
1153 XFree(children);
1154 if (res)
1155 return parent;
1156 else
1157 return (Window) 0;
1158 }
1159
1160 void wxExit()
1161 {
1162 int retValue = 0;
1163 if (wxTheApp)
1164 retValue = wxTheApp->OnExit();
1165
1166 wxApp::CleanUp();
1167 /*
1168 * Exit in some platform-specific way. Not recommended that the app calls this:
1169 * only for emergencies.
1170 */
1171 exit(retValue);
1172 }
1173
1174 // Yield to other processes
1175
1176 bool wxApp::Yield(bool onlyIfNeeded)
1177 {
1178 // Sometimes only 2 yields seem
1179 // to do the trick, e.g. in the
1180 // progress dialog
1181 int i;
1182 for (i = 0; i < 2; i++)
1183 {
1184 bool s_inYield = FALSE;
1185
1186 if ( s_inYield )
1187 {
1188 if ( !onlyIfNeeded )
1189 {
1190 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1191 }
1192
1193 return FALSE;
1194 }
1195
1196 s_inYield = TRUE;
1197
1198 // Make sure we have an event loop object,
1199 // or Pending/Dispatch will fail
1200 wxEventLoop* eventLoop = wxEventLoop::GetActive();
1201 wxEventLoop* newEventLoop = NULL;
1202 if (!eventLoop)
1203 {
1204 newEventLoop = new wxEventLoop;
1205 wxEventLoop::SetActive(newEventLoop);
1206 }
1207
1208 while (wxTheApp && wxTheApp->Pending())
1209 wxTheApp->Dispatch();
1210
1211 #if wxUSE_TIMER
1212 wxTimer::NotifyTimers();
1213 #endif
1214 ProcessIdle();
1215
1216 if (newEventLoop)
1217 {
1218 wxEventLoop::SetActive(NULL);
1219 delete newEventLoop;
1220 }
1221
1222 s_inYield = FALSE;
1223 }
1224
1225 return TRUE;
1226 }
1227
1228 #ifdef __WXDEBUG__
1229
1230 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
1231 {
1232 // While the GUI isn't working that well, just print out the
1233 // message.
1234 #if 1
1235 wxAppBase::OnAssert(file, line, cond, msg);
1236 #else
1237 wxString msg2;
1238 msg2.Printf("At file %s:%d: %s", file, line, msg);
1239 wxLogDebug(msg2);
1240 #endif
1241 }
1242
1243 #endif // __WXDEBUG__
1244