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