1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
19 #include "wx/gdicmn.h"
21 #include "wx/dialog.h"
23 #include "wx/module.h"
24 #include "wx/memory.h"
27 #include "wx/evtloop.h"
29 #include "wx/filename.h"
31 #include "wx/univ/theme.h"
32 #include "wx/univ/renderer.h"
34 #define ABS(a) (((a) < 0) ? -(a) : (a))
37 #include "wx/thread.h"
40 #if wxUSE_WX_RESOURCES
41 #include "wx/resource.h"
44 #include "wx/x11/private.h"
48 //------------------------------------------------------------------------
50 //------------------------------------------------------------------------
52 extern wxList wxPendingDelete
;
54 wxHashTable
*wxWidgetHashTable
= NULL
;
55 wxHashTable
*wxClientWidgetHashTable
= NULL
;
57 wxApp
*wxTheApp
= NULL
;
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
;
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
68 static wxWindow
*g_nextFocus
= NULL
;
69 static wxWindow
*g_prevFocus
= NULL
;
71 //------------------------------------------------------------------------
73 //------------------------------------------------------------------------
76 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
78 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
80 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
82 // just forward to the default handler for now
83 if (gs_pfnXErrorHandler
)
84 return gs_pfnXErrorHandler(dpy
, xevent
);
90 //------------------------------------------------------------------------
92 //------------------------------------------------------------------------
94 long wxApp::sm_lastMessageTime
= 0;
95 WXDisplay
*wxApp::ms_display
= NULL
;
97 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
99 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
100 EVT_IDLE(wxApp::OnIdle
)
103 bool wxApp::Initialize()
105 wxClassInfo::InitializeClasses();
108 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
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.
114 wxPendingEventsLocker
= new wxCriticalSection();
117 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
118 wxTheColourDatabase
->Initialize();
120 wxInitializeStockLists();
121 wxInitializeStockObjects();
123 #if wxUSE_WX_RESOURCES
124 wxInitializeResourceSystem();
127 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
128 wxClientWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
130 wxModule::RegisterModules();
131 if (!wxModule::InitializeModules()) return FALSE
;
136 void wxApp::CleanUp()
138 delete wxWidgetHashTable
;
139 wxWidgetHashTable
= NULL
;
140 delete wxClientWidgetHashTable
;
141 wxClientWidgetHashTable
= NULL
;
143 wxModule::CleanUpModules();
145 #if wxUSE_WX_RESOURCES
146 wxCleanUpResourceSystem();
149 delete wxTheColourDatabase
;
150 wxTheColourDatabase
= NULL
;
152 wxDeleteStockObjects();
154 wxDeleteStockLists();
159 wxClassInfo::CleanUpClasses();
162 delete wxPendingEvents
;
163 delete wxPendingEventsLocker
;
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)
173 wxLogDebug("There were memory leaks.");
174 wxDebugContext::Dump();
175 wxDebugContext::PrintStatistics();
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
);
185 // NB: argc and argv may be changed here, pass by reference!
186 int wxEntryStart( int& argc
, char *argv
[] )
190 // install the X error handler
191 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
193 #endif // __WXDEBUG__
195 char *displayName
= NULL
;
196 bool syncDisplay
= FALSE
;
199 for (i
= 0; i
< argc
; i
++)
201 if (strcmp( argv
[i
], "-display") == 0)
206 displayName
= argv
[i
];
210 else if (strcmp( argv
[i
], "-geometry") == 0)
216 if (sscanf(argv
[i
], "%dx%d", &w
, &h
) != 2)
218 wxLogError( _("Invalid geometry specification '%s'"), wxString::FromAscii(argv
[i
]).c_str() );
222 g_initialSize
= wxSize(w
, h
);
227 else if (strcmp( argv
[i
], "-sync") == 0)
232 else if (strcmp( argv
[i
], "-iconic") == 0)
242 Display
* xdisplay
= XOpenDisplay( displayName
);
245 wxLogError( _("wxWindows could not open display. Exiting.") );
250 XSynchronize(xdisplay
, True
);
252 wxApp::ms_display
= (WXDisplay
*) xdisplay
;
254 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
257 wxSetDetectableAutoRepeat( TRUE
);
260 // Glib's type system required by Pango
264 if (!wxApp::Initialize())
274 if ( !wxTheApp
->OnInitGui() )
281 int wxEntry( int argc
, char *argv
[] )
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();
292 int err
= wxEntryStart(argc
, argv
);
298 if (!wxApp::GetInitializerFunction())
300 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
304 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
309 printf( "wxWindows error: wxTheApp == NULL\n" );
313 // Command line argument stuff
314 wxTheApp
->argc
= argc
;
316 wxTheApp
->argv
= new wxChar
*[argc
+1];
318 while (mb_argc
< argc
)
320 wxString tmp
= wxString::FromAscii( argv
[mb_argc
] );
321 wxTheApp
->argv
[mb_argc
] = wxStrdup( tmp
.c_str() );
324 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
326 wxTheApp
->argv
= argv
;
329 if (wxTheApp
->argc
> 0)
331 wxFileName
fname( wxTheApp
->argv
[0] );
332 wxTheApp
->SetAppName( fname
.GetName() );
335 wxTheApp
->m_showIconic
= g_showIconic
;
336 wxTheApp
->m_initialSize
= g_initialSize
;
339 retValue
= wxEntryInitGui();
341 // Here frames insert themselves automatically into wxTopLevelWindows by
342 // getting created in OnInit().
345 if ( !wxTheApp
->OnInit() )
351 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
354 // flush the logged messages if any
355 wxLog
*pLog
= wxLog::GetActiveTarget();
356 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
359 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
360 // for further messages
362 if (wxTheApp
->GetTopWindow())
364 delete wxTheApp
->GetTopWindow();
365 wxTheApp
->SetTopWindow(NULL
);
368 wxTheApp
->DeletePendingObjects();
377 // Static member initialization
378 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
382 // TODO: parse the command line
386 m_mainColormap
= (WXColormap
) NULL
;
387 m_topLevelWidget
= (WXWindow
) NULL
;
388 m_maxRequestSize
= 0;
390 m_showIconic
= FALSE
;
391 m_initialSize
= wxDefaultSize
;
394 m_visualColormap
= NULL
;
405 if (m_visualColormap
)
406 delete [] (XColor
*)m_visualColormap
;
410 bool wxApp::Initialized()
418 int wxApp::MainLoop()
421 m_mainLoop
= new wxEventLoop
;
423 rt
= m_mainLoop
->Run();
431 //-----------------------------------------------------------------------
432 // X11 predicate function for exposure compression
433 //-----------------------------------------------------------------------
438 Bool found_non_matching
;
441 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
443 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
445 if (info
->found_non_matching
)
448 if (xevent
->xany
.type
!= Expose
)
450 info
->found_non_matching
= TRUE
;
454 if (xevent
->xexpose
.window
!= info
->window
)
456 info
->found_non_matching
= TRUE
;
465 //-----------------------------------------------------------------------
466 // Processes an X event, returning TRUE if the event was processed.
467 //-----------------------------------------------------------------------
469 bool wxApp::ProcessXEvent(WXEvent
* _event
)
471 XEvent
* event
= (XEvent
*) _event
;
473 wxWindow
* win
= NULL
;
474 Window window
= XEventGetWindow(event
);
476 Window actualWindow
= window
;
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.
485 win
= wxGetWindowFromTable(window
);
488 #if wxUSE_TWO_WINDOWS
489 win
= wxGetClientWindowFromTable(window
);
496 wxString windowClass
= win
->GetClassInfo()->GetClassName();
503 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
504 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
508 info
.window
= event
->xexpose
.window
;
509 info
.found_non_matching
= FALSE
;
510 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
512 // Don't worry about optimizing redrawing the border etc.
514 win
->NeedUpdateNcAreaInIdle();
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
));
527 info
.window
= event
->xexpose
.window
;
528 info
.found_non_matching
= FALSE
;
529 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
531 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
532 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
534 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
535 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
539 // This simplifies the expose and clear areas to simple
541 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
542 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
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();
549 // Only erase background, paint in idle time.
550 win
->SendEraseEvents();
560 printf( "GraphicExpose event\n" );
562 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
564 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
565 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
567 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
568 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
570 if (event
->xgraphicsexpose
.count
== 0)
572 // Only erase background, paint in idle time.
573 win
->SendEraseEvents();
583 if (!win
->IsEnabled())
586 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
587 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
589 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
591 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
592 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
595 keyEvent
.SetEventType(wxEVT_CHAR
);
596 // Do the translation again, retaining the ASCII
598 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, TRUE
);
599 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
602 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
603 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
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
);
619 if (!win
->IsEnabled())
622 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
623 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
625 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
627 case ConfigureNotify
:
630 if (event
->update
.utype
== GR_UPDATE_SIZE
)
633 if (win
->IsTopLevel())
635 wxTopLevelWindow
*tlw
= (wxTopLevelWindow
*) win
;
636 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
637 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
640 if (win
->IsTopLevel() && win
->IsShown())
642 wxTopLevelWindowX11
*tlw
= (wxTopLevelWindowX11
*) win
;
643 tlw
->SetNeedResizeInIdle();
647 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
648 sizeEvent
.SetEventObject( win
);
650 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
659 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
660 return HandlePropertyChange(_event
);
664 if (!win
->IsEnabled())
667 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
668 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
670 if (event
->xclient
.message_type
== wm_protocols
)
672 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
683 printf( "destroy from %s\n", win
->GetName().c_str() );
688 printf( "create from %s\n", win
->GetName().c_str() );
693 printf( "map request from %s\n", win
->GetName().c_str() );
698 printf( "resize request from %s\n", win
->GetName().c_str() );
700 Display
*disp
= (Display
*) wxGetDisplay();
705 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
707 wxSize sz
= win
->GetSize();
708 wxSizeEvent
sizeEvent(sz
, win
->GetId());
709 sizeEvent
.SetEventObject(win
);
711 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
716 case GR_EVENT_TYPE_CLOSE_REQ
:
733 if (!win
->IsEnabled())
736 // Here we check if the top level window is
737 // disabled, which is one aspect of modality.
739 while (tlw
&& !tlw
->IsTopLevel())
740 tlw
= tlw
->GetParent();
741 if (tlw
&& !tlw
->IsEnabled())
744 if (event
->type
== ButtonPress
)
746 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
748 // This might actually be done in wxWindow::SetFocus()
749 // and not here. TODO.
750 g_prevFocus
= wxWindow::FindFocus();
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() );
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
;
767 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
769 // Throw out NotifyGrab and NotifyUngrab
770 if (event
->xcrossing
.mode
!= NotifyNormal
)
774 wxMouseEvent wxevent
;
775 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
776 return win
->GetEventHandler()->ProcessEvent( wxevent
);
781 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
782 (event
->xfocus
.mode
== NotifyNormal
))
785 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
787 extern wxWindow
* g_GettingFocus
;
788 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
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() );
798 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
799 focusEvent
.SetEventObject(win
);
800 focusEvent
.SetWindow( g_prevFocus
);
803 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
812 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
813 (event
->xfocus
.mode
== NotifyNormal
))
816 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
818 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
819 focusEvent
.SetEventObject(win
);
820 focusEvent
.SetWindow( g_nextFocus
);
822 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
830 //wxString eventName = wxGetXEventName(XEvent& event);
831 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
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()
846 event
.SetEventObject(this);
849 return event
.MoreRequested();
852 void wxApp::ExitMainLoop()
858 // Is a message/event pending?
859 bool wxApp::Pending()
861 return wxEventLoop::GetActive()->Pending();
864 // Dispatch a message.
865 void wxApp::Dispatch()
867 wxEventLoop::GetActive()->Dispatch();
870 // This should be redefined in a derived class for
871 // handling property change events for XAtom IPC.
872 bool wxApp::HandlePropertyChange(WXEvent
*event
)
874 // by default do nothing special
875 // TODO: what to do for X11
876 // XtDispatchEvent((XEvent*) event);
880 void wxApp::OnIdle(wxIdleEvent
& event
)
882 static bool s_inOnIdle
= FALSE
;
884 // Avoid recursion (via ProcessEvent default case)
890 // Resend in the main thread events which have been prepared in other
892 ProcessPendingEvents();
894 // 'Garbage' collection of windows deleted with Close()
895 DeletePendingObjects();
897 // Send OnIdle events to all windows
898 bool needMore
= SendIdleEvents();
901 event
.RequestMore(TRUE
);
908 // **** please implement me! ****
909 // Wake up the idle handler processor, even if it is in another thread...
913 // Send idle event to all top-level windows
914 bool wxApp::SendIdleEvents()
916 bool needMore
= FALSE
;
918 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
921 wxWindow
* win
= node
->GetData();
922 if (SendIdleEvents(win
))
924 node
= node
->GetNext();
930 // Send idle event to window and all subwindows
931 bool wxApp::SendIdleEvents(wxWindow
* win
)
933 bool needMore
= FALSE
;
936 event
.SetEventObject(win
);
938 win
->GetEventHandler()->ProcessEvent(event
);
940 if (event
.MoreRequested())
943 wxNode
* node
= win
->GetChildren().First();
946 wxWindow
* win
= (wxWindow
*) node
->Data();
947 if (SendIdleEvents(win
))
953 win
->OnInternalIdle();
958 void wxApp::DeletePendingObjects()
960 wxNode
*node
= wxPendingDelete
.First();
963 wxObject
*obj
= (wxObject
*)node
->Data();
967 if (wxPendingDelete
.Member(obj
))
970 // Deleting one object may have deleted other pending
971 // objects, so start from beginning of list again.
972 node
= wxPendingDelete
.First();
976 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
981 while (!(mask
& 0x1))
994 // Create display, and other initialization
995 bool wxApp::OnInitGui()
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
);
1002 if (!wxAppBase::OnInitGui())
1005 GetMainColormap( wxApp::GetDisplay() );
1007 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
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
1015 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
1016 int xscreen
= DefaultScreen(xdisplay
);
1017 Visual
* xvisual
= DefaultVisual(xdisplay
,xscreen
);
1018 int xdepth
= DefaultDepth(xdisplay
, xscreen
);
1020 XVisualInfo vinfo_template
;
1021 vinfo_template
.visual
= xvisual
;
1022 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
1023 vinfo_template
.depth
= xdepth
;
1026 XVisualInfo
*vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1027 wxASSERT_MSG( vi
, wxT("No visual info") );
1029 m_visualType
= vi
->visual
->c_class
;
1030 m_visualScreen
= vi
->screen
;
1032 m_visualRedMask
= vi
->red_mask
;
1033 m_visualGreenMask
= vi
->green_mask
;
1034 m_visualBlueMask
= vi
->blue_mask
;
1036 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
1038 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
, &m_visualRedPrec
);
1039 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
, &m_visualGreenPrec
);
1040 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
, &m_visualBluePrec
);
1043 m_visualDepth
= xdepth
;
1045 xdepth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
1047 m_visualColormapSize
= vi
->colormap_size
;
1051 if (m_visualDepth
> 8)
1054 m_visualColormap
= new XColor
[m_visualColormapSize
];
1055 XColor
* colors
= (XColor
*) m_visualColormap
;
1057 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1058 colors
[i
].pixel
= i
;
1060 XQueryColors( xdisplay
, DefaultColormap(xdisplay
,xscreen
), colors
, m_visualColormapSize
);
1062 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
1064 for (int r
= 0; r
< 32; r
++)
1066 for (int g
= 0; g
< 32; g
++)
1068 for (int b
= 0; b
< 32; b
++)
1070 int rr
= (r
<< 3) | (r
>> 2);
1071 int gg
= (g
<< 3) | (g
>> 2);
1072 int bb
= (b
<< 3) | (b
>> 2);
1078 int max
= 3 * 65536;
1080 for (int i
= 0; i
< m_visualColormapSize
; i
++)
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
);
1088 index
= i
; max
= sum
;
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
;
1099 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
1110 #include <pango/pango.h>
1111 #include <pango/pangox.h>
1112 #include <pango/pangoxft.h>
1114 PangoContext
* wxApp::GetPangoContext()
1116 static PangoContext
*ret
= NULL
;
1120 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
1123 int xscreen
= DefaultScreen(xdisplay
);
1124 static int use_xft
= -1;
1127 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
1128 use_xft
= (val
== L
"1");
1132 ret
= pango_xft_get_context( xdisplay
, xscreen
);
1135 ret
= pango_x_get_context( xdisplay
);
1137 if (!PANGO_IS_CONTEXT(ret
))
1138 wxLogError( wxT("No pango context.") );
1144 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
1146 if (!display
) /* Must be called first with non-NULL display */
1147 return m_mainColormap
;
1149 int defaultScreen
= DefaultScreen((Display
*) display
);
1150 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
1152 Colormap c
= DefaultColormapOfScreen(screen
);
1154 if (!m_mainColormap
)
1155 m_mainColormap
= (WXColormap
) c
;
1157 return (WXColormap
) c
;
1160 Window
wxGetWindowParent(Window window
)
1162 wxASSERT_MSG( window
, "invalid window" );
1166 Window parent
, root
= 0;
1170 unsigned int noChildren
= 0;
1172 Window
* children
= NULL
;
1174 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
1179 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
1180 & children
, & noChildren
);
1193 retValue
= wxTheApp
->OnExit();
1197 * Exit in some platform-specific way. Not recommended that the app calls this:
1198 * only for emergencies.
1203 // Yield to other processes
1205 bool wxApp::Yield(bool onlyIfNeeded
)
1207 // Sometimes only 2 yields seem
1208 // to do the trick, e.g. in the
1211 for (i
= 0; i
< 2; i
++)
1213 bool s_inYield
= FALSE
;
1217 if ( !onlyIfNeeded
)
1219 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1227 // Make sure we have an event loop object,
1228 // or Pending/Dispatch will fail
1229 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
1230 wxEventLoop
* newEventLoop
= NULL
;
1233 newEventLoop
= new wxEventLoop
;
1234 wxEventLoop::SetActive(newEventLoop
);
1237 // Call dispatch at least once so that sockets
1239 wxTheApp
->Dispatch();
1241 while (wxTheApp
&& wxTheApp
->Pending())
1242 wxTheApp
->Dispatch();
1245 wxTimer::NotifyTimers();
1251 wxEventLoop::SetActive(NULL
);
1252 delete newEventLoop
;
1263 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
1265 // While the GUI isn't working that well, just print out the
1268 wxAppBase::OnAssert(file
, line
, cond
, msg
);
1271 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
1276 #endif // __WXDEBUG__