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"
32 #include "wx/univ/theme.h"
33 #include "wx/univ/renderer.h"
35 #define ABS(a) (((a) < 0) ? -(a) : (a))
38 #include "wx/thread.h"
41 #if wxUSE_WX_RESOURCES
42 #include "wx/resource.h"
45 #include "wx/x11/private.h"
49 //------------------------------------------------------------------------
51 //------------------------------------------------------------------------
53 extern wxList wxPendingDelete
;
55 wxHashTable
*wxWidgetHashTable
= NULL
;
56 wxHashTable
*wxClientWidgetHashTable
= NULL
;
58 wxApp
*wxTheApp
= NULL
;
60 // This is set within wxEntryStart -- too early on
61 // to put these in wxTheApp
62 static bool g_showIconic
= FALSE
;
63 static wxSize g_initialSize
= wxDefaultSize
;
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
69 static wxWindow
*g_nextFocus
= NULL
;
70 static wxWindow
*g_prevFocus
= NULL
;
72 //------------------------------------------------------------------------
74 //------------------------------------------------------------------------
77 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
79 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
81 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
83 // just forward to the default handler for now
84 if (gs_pfnXErrorHandler
)
85 return gs_pfnXErrorHandler(dpy
, xevent
);
91 //------------------------------------------------------------------------
93 //------------------------------------------------------------------------
95 long wxApp::sm_lastMessageTime
= 0;
96 WXDisplay
*wxApp::ms_display
= NULL
;
98 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
100 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
101 EVT_IDLE(wxApp::OnIdle
)
104 bool wxApp::Initialize()
106 wxClassInfo::InitializeClasses();
109 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
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.
115 wxPendingEventsLocker
= new wxCriticalSection();
118 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
119 wxTheColourDatabase
->Initialize();
121 wxInitializeStockLists();
122 wxInitializeStockObjects();
124 #if wxUSE_WX_RESOURCES
125 wxInitializeResourceSystem();
128 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
129 wxClientWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
131 wxModule::RegisterModules();
132 if (!wxModule::InitializeModules()) return FALSE
;
137 void wxApp::CleanUp()
139 delete wxWidgetHashTable
;
140 wxWidgetHashTable
= NULL
;
141 delete wxClientWidgetHashTable
;
142 wxClientWidgetHashTable
= NULL
;
144 wxModule::CleanUpModules();
146 #if wxUSE_WX_RESOURCES
147 wxCleanUpResourceSystem();
150 delete wxTheColourDatabase
;
151 wxTheColourDatabase
= NULL
;
153 wxDeleteStockObjects();
155 wxDeleteStockLists();
160 wxClassInfo::CleanUpClasses();
163 delete wxPendingEvents
;
164 delete wxPendingEventsLocker
;
167 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
168 // At this point we want to check if there are any memory
169 // blocks that aren't part of the wxDebugContext itself,
170 // as a special case. Then when dumping we need to ignore
171 // wxDebugContext, too.
172 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
174 wxLogDebug("There were memory leaks.");
175 wxDebugContext::Dump();
176 wxDebugContext::PrintStatistics();
180 // do it as the very last thing because everything else can log messages
181 wxLog::DontCreateOnDemand();
182 // do it as the very last thing because everything else can log messages
183 delete wxLog::SetActiveTarget(NULL
);
186 // NB: argc and argv may be changed here, pass by reference!
187 int wxEntryStart( int& argc
, char *argv
[] )
191 // install the X error handler
192 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
194 #endif // __WXDEBUG__
196 char *displayName
= NULL
;
197 bool syncDisplay
= FALSE
;
200 for (i
= 0; i
< argc
; i
++)
202 if (strcmp( argv
[i
], "-display") == 0)
207 displayName
= argv
[i
];
211 else if (strcmp( argv
[i
], "-geometry") == 0)
217 if (sscanf(argv
[i
], "%dx%d", &w
, &h
) != 2)
219 wxLogError( _("Invalid geometry specification '%s'"), wxString::FromAscii(argv
[i
]).c_str() );
223 g_initialSize
= wxSize(w
, h
);
228 else if (strcmp( argv
[i
], "-sync") == 0)
233 else if (strcmp( argv
[i
], "-iconic") == 0)
243 Display
* xdisplay
= XOpenDisplay( displayName
);
246 wxLogError( _("wxWindows could not open display. Exiting.") );
251 XSynchronize(xdisplay
, True
);
253 wxApp::ms_display
= (WXDisplay
*) xdisplay
;
255 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
258 wxSetDetectableAutoRepeat( TRUE
);
261 // Glib's type system required by Pango
265 if (!wxApp::Initialize())
275 if ( !wxTheApp
->OnInitGui() )
282 int wxEntry( int argc
, char *argv
[] )
284 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
285 // This seems to be necessary since there are 'rogue'
286 // objects present at this point (perhaps global objects?)
287 // Setting a checkpoint will ignore them as far as the
288 // memory checking facility is concerned.
289 // Of course you may argue that memory allocated in globals should be
290 // checked, but this is a reasonable compromise.
291 wxDebugContext::SetCheckpoint();
293 int err
= wxEntryStart(argc
, argv
);
299 if (!wxApp::GetInitializerFunction())
301 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
305 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
310 printf( "wxWindows error: wxTheApp == NULL\n" );
314 // Command line argument stuff
315 wxTheApp
->argc
= argc
;
317 wxTheApp
->argv
= new wxChar
*[argc
+1];
319 while (mb_argc
< argc
)
321 wxString tmp
= wxString::FromAscii( argv
[mb_argc
] );
322 wxTheApp
->argv
[mb_argc
] = wxStrdup( tmp
.c_str() );
325 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
327 wxTheApp
->argv
= argv
;
330 if (wxTheApp
->argc
> 0)
332 wxFileName
fname( wxTheApp
->argv
[0] );
333 wxTheApp
->SetAppName( fname
.GetName() );
336 wxTheApp
->m_showIconic
= g_showIconic
;
337 wxTheApp
->m_initialSize
= g_initialSize
;
340 retValue
= wxEntryInitGui();
342 // Here frames insert themselves automatically into wxTopLevelWindows by
343 // getting created in OnInit().
346 if ( !wxTheApp
->OnInit() )
352 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
355 // flush the logged messages if any
356 wxLog
*pLog
= wxLog::GetActiveTarget();
357 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
360 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
361 // for further messages
363 if (wxTheApp
->GetTopWindow())
365 delete wxTheApp
->GetTopWindow();
366 wxTheApp
->SetTopWindow(NULL
);
369 wxTheApp
->DeletePendingObjects();
378 // Static member initialization
379 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
383 // TODO: parse the command line
387 m_mainColormap
= (WXColormap
) NULL
;
388 m_topLevelWidget
= (WXWindow
) NULL
;
389 m_maxRequestSize
= 0;
391 m_showIconic
= FALSE
;
392 m_initialSize
= wxDefaultSize
;
395 m_visualColormap
= NULL
;
406 if (m_visualColormap
)
407 delete [] (XColor
*)m_visualColormap
;
411 bool wxApp::Initialized()
419 int wxApp::MainLoop()
422 m_mainLoop
= new wxEventLoop
;
424 rt
= m_mainLoop
->Run();
432 //-----------------------------------------------------------------------
433 // X11 predicate function for exposure compression
434 //-----------------------------------------------------------------------
439 Bool found_non_matching
;
442 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
444 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
446 if (info
->found_non_matching
)
449 if (xevent
->xany
.type
!= Expose
)
451 info
->found_non_matching
= TRUE
;
455 if (xevent
->xexpose
.window
!= info
->window
)
457 info
->found_non_matching
= TRUE
;
466 //-----------------------------------------------------------------------
467 // Processes an X event, returning TRUE if the event was processed.
468 //-----------------------------------------------------------------------
470 bool wxApp::ProcessXEvent(WXEvent
* _event
)
472 XEvent
* event
= (XEvent
*) _event
;
474 wxWindow
* win
= NULL
;
475 Window window
= XEventGetWindow(event
);
477 Window actualWindow
= window
;
480 // Find the first wxWindow that corresponds to this event window
481 // Because we're receiving events after a window
482 // has been destroyed, assume a 1:1 match between
483 // Window and wxWindow, so if it's not in the table,
484 // it must have been destroyed.
486 win
= wxGetWindowFromTable(window
);
489 #if wxUSE_TWO_WINDOWS
490 win
= wxGetClientWindowFromTable(window
);
497 wxString windowClass
= win
->GetClassInfo()->GetClassName();
504 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
505 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
509 info
.window
= event
->xexpose
.window
;
510 info
.found_non_matching
= FALSE
;
511 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
513 // Don't worry about optimizing redrawing the border etc.
515 win
->NeedUpdateNcAreaInIdle();
520 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
521 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
522 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
523 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
528 info
.window
= event
->xexpose
.window
;
529 info
.found_non_matching
= FALSE
;
530 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
532 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
533 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
535 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
536 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
540 // This simplifies the expose and clear areas to simple
542 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
543 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
545 // If we only have one X11 window, always indicate
546 // that borders might have to be redrawn.
547 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
548 win
->NeedUpdateNcAreaInIdle();
550 // Only erase background, paint in idle time.
551 win
->SendEraseEvents();
563 printf( "GraphicExpose event\n" );
565 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
567 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
568 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
570 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
571 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
573 if (event
->xgraphicsexpose
.count
== 0)
575 // Only erase background, paint in idle time.
576 win
->SendEraseEvents();
586 if (!win
->IsEnabled())
589 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
590 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
592 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
594 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
595 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
598 keyEvent
.SetEventType(wxEVT_CHAR
);
599 // Do the translation again, retaining the ASCII
601 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, TRUE
);
602 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
605 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
606 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
608 wxNavigationKeyEvent new_event
;
609 new_event
.SetEventObject( win
->GetParent() );
610 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
611 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
612 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
613 new_event
.SetWindowChange( keyEvent
.ControlDown() );
614 new_event
.SetCurrentFocus( win
);
615 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
622 if (!win
->IsEnabled())
625 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
626 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
628 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
630 case ConfigureNotify
:
633 if (event
->update
.utype
== GR_UPDATE_SIZE
)
636 if (win
->IsTopLevel())
638 wxTopLevelWindow
*tlw
= (wxTopLevelWindow
*) win
;
639 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
640 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
643 if (win
->IsTopLevel() && win
->IsShown())
645 wxTopLevelWindowX11
*tlw
= (wxTopLevelWindowX11
*) win
;
646 tlw
->SetNeedResizeInIdle();
650 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
651 sizeEvent
.SetEventObject( win
);
653 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
662 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
663 return HandlePropertyChange(_event
);
667 if (!win
->IsEnabled())
670 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
671 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
673 if (event
->xclient
.message_type
== wm_protocols
)
675 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
686 printf( "destroy from %s\n", win
->GetName().c_str() );
691 printf( "create from %s\n", win
->GetName().c_str() );
696 printf( "map request from %s\n", win
->GetName().c_str() );
701 printf( "resize request from %s\n", win
->GetName().c_str() );
703 Display
*disp
= (Display
*) wxGetDisplay();
708 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
710 wxSize sz
= win
->GetSize();
711 wxSizeEvent
sizeEvent(sz
, win
->GetId());
712 sizeEvent
.SetEventObject(win
);
714 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
719 case GR_EVENT_TYPE_CLOSE_REQ
:
736 if (!win
->IsEnabled())
739 // Here we check if the top level window is
740 // disabled, which is one aspect of modality.
742 while (tlw
&& !tlw
->IsTopLevel())
743 tlw
= tlw
->GetParent();
744 if (tlw
&& !tlw
->IsEnabled())
747 if (event
->type
== ButtonPress
)
749 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
751 // This might actually be done in wxWindow::SetFocus()
752 // and not here. TODO.
753 g_prevFocus
= wxWindow::FindFocus();
756 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
758 // Record the fact that this window is
759 // getting the focus, because we'll need to
760 // check if its parent is getting a bogus
761 // focus and duly ignore it.
762 // TODO: may need to have this code in SetFocus, too.
763 extern wxWindow
* g_GettingFocus
;
764 g_GettingFocus
= win
;
770 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
772 // Throw out NotifyGrab and NotifyUngrab
773 if (event
->xcrossing
.mode
!= NotifyNormal
)
777 wxMouseEvent wxevent
;
778 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
779 return win
->GetEventHandler()->ProcessEvent( wxevent
);
784 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
785 (event
->xfocus
.mode
== NotifyNormal
))
788 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
790 extern wxWindow
* g_GettingFocus
;
791 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
793 // Ignore this, this can be a spurious FocusIn
794 // caused by a child having its focus set.
795 g_GettingFocus
= NULL
;
796 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
801 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
802 focusEvent
.SetEventObject(win
);
803 focusEvent
.SetWindow( g_prevFocus
);
806 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
815 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
816 (event
->xfocus
.mode
== NotifyNormal
))
819 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
821 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
822 focusEvent
.SetEventObject(win
);
823 focusEvent
.SetWindow( g_nextFocus
);
825 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
833 //wxString eventName = wxGetXEventName(XEvent& event);
834 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
843 // Returns TRUE if more time is needed.
844 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
845 // but ProcessIdle may be needed by apps, so is kept.
846 bool wxApp::ProcessIdle()
849 event
.SetEventObject(this);
852 return event
.MoreRequested();
855 void wxApp::ExitMainLoop()
861 // Is a message/event pending?
862 bool wxApp::Pending()
864 return wxEventLoop::GetActive()->Pending();
867 // Dispatch a message.
868 void wxApp::Dispatch()
870 wxEventLoop::GetActive()->Dispatch();
873 // This should be redefined in a derived class for
874 // handling property change events for XAtom IPC.
875 bool wxApp::HandlePropertyChange(WXEvent
*event
)
877 // by default do nothing special
878 // TODO: what to do for X11
879 // XtDispatchEvent((XEvent*) event);
883 void wxApp::OnIdle(wxIdleEvent
& event
)
885 static bool s_inOnIdle
= FALSE
;
887 // Avoid recursion (via ProcessEvent default case)
893 // Resend in the main thread events which have been prepared in other
895 ProcessPendingEvents();
897 // 'Garbage' collection of windows deleted with Close()
898 DeletePendingObjects();
900 // Send OnIdle events to all windows
901 bool needMore
= SendIdleEvents();
904 event
.RequestMore(TRUE
);
911 // **** please implement me! ****
912 // Wake up the idle handler processor, even if it is in another thread...
916 // Send idle event to all top-level windows
917 bool wxApp::SendIdleEvents()
919 bool needMore
= FALSE
;
921 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
924 wxWindow
* win
= node
->GetData();
925 if (SendIdleEvents(win
))
927 node
= node
->GetNext();
933 // Send idle event to window and all subwindows
934 bool wxApp::SendIdleEvents(wxWindow
* win
)
936 bool needMore
= FALSE
;
939 event
.SetEventObject(win
);
941 win
->GetEventHandler()->ProcessEvent(event
);
943 if (event
.MoreRequested())
946 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
949 wxWindow
* win
= (wxWindow
*) node
->GetData();
950 if (SendIdleEvents(win
))
953 node
= node
->GetNext();
956 win
->OnInternalIdle();
961 void wxApp::DeletePendingObjects()
963 wxNode
*node
= wxPendingDelete
.GetFirst();
966 wxObject
*obj
= (wxObject
*)node
->GetData();
970 if (wxPendingDelete
.Member(obj
))
973 // Deleting one object may have deleted other pending
974 // objects, so start from beginning of list again.
975 node
= wxPendingDelete
.GetFirst();
979 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
984 while (!(mask
& 0x1))
997 // Create display, and other initialization
998 bool wxApp::OnInitGui()
1000 // Eventually this line will be removed, but for
1001 // now we don't want to try popping up a dialog
1002 // for error messages.
1003 delete wxLog::SetActiveTarget(new wxLogStderr
);
1005 if (!wxAppBase::OnInitGui())
1008 GetMainColormap( wxApp::GetDisplay() );
1010 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
1013 // Get info about the current visual. It is enough
1014 // to do this once here unless we support different
1015 // visuals, displays and screens. Given that wxX11
1016 // mostly for embedded things, that is no real
1018 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
1019 int xscreen
= DefaultScreen(xdisplay
);
1020 Visual
* xvisual
= DefaultVisual(xdisplay
,xscreen
);
1021 int xdepth
= DefaultDepth(xdisplay
, xscreen
);
1023 XVisualInfo vinfo_template
;
1024 vinfo_template
.visual
= xvisual
;
1025 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
1026 vinfo_template
.depth
= xdepth
;
1029 XVisualInfo
*vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1030 wxASSERT_MSG( vi
, wxT("No visual info") );
1032 m_visualType
= vi
->visual
->c_class
;
1033 m_visualScreen
= vi
->screen
;
1035 m_visualRedMask
= vi
->red_mask
;
1036 m_visualGreenMask
= vi
->green_mask
;
1037 m_visualBlueMask
= vi
->blue_mask
;
1039 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
1041 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
, &m_visualRedPrec
);
1042 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
, &m_visualGreenPrec
);
1043 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
, &m_visualBluePrec
);
1046 m_visualDepth
= xdepth
;
1048 xdepth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
1050 m_visualColormapSize
= vi
->colormap_size
;
1054 if (m_visualDepth
> 8)
1057 m_visualColormap
= new XColor
[m_visualColormapSize
];
1058 XColor
* colors
= (XColor
*) m_visualColormap
;
1060 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1061 colors
[i
].pixel
= i
;
1063 XQueryColors( xdisplay
, DefaultColormap(xdisplay
,xscreen
), colors
, m_visualColormapSize
);
1065 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
1067 for (int r
= 0; r
< 32; r
++)
1069 for (int g
= 0; g
< 32; g
++)
1071 for (int b
= 0; b
< 32; b
++)
1073 int rr
= (r
<< 3) | (r
>> 2);
1074 int gg
= (g
<< 3) | (g
>> 2);
1075 int bb
= (b
<< 3) | (b
>> 2);
1081 int max
= 3 * 65536;
1083 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1085 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
1086 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
1087 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
1088 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1091 index
= i
; max
= sum
;
1097 // assume 8-bit true or static colors. this really exists
1098 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
1099 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
1100 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
1102 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
1113 #include <pango/pango.h>
1114 #include <pango/pangox.h>
1115 #include <pango/pangoxft.h>
1117 PangoContext
* wxApp::GetPangoContext()
1119 static PangoContext
*ret
= NULL
;
1123 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
1126 int xscreen
= DefaultScreen(xdisplay
);
1127 static int use_xft
= -1;
1130 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
1131 use_xft
= (val
== L
"1");
1135 ret
= pango_xft_get_context( xdisplay
, xscreen
);
1138 ret
= pango_x_get_context( xdisplay
);
1140 if (!PANGO_IS_CONTEXT(ret
))
1141 wxLogError( wxT("No pango context.") );
1147 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
1149 if (!display
) /* Must be called first with non-NULL display */
1150 return m_mainColormap
;
1152 int defaultScreen
= DefaultScreen((Display
*) display
);
1153 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
1155 Colormap c
= DefaultColormapOfScreen(screen
);
1157 if (!m_mainColormap
)
1158 m_mainColormap
= (WXColormap
) c
;
1160 return (WXColormap
) c
;
1163 Window
wxGetWindowParent(Window window
)
1165 wxASSERT_MSG( window
, "invalid window" );
1169 Window parent
, root
= 0;
1173 unsigned int noChildren
= 0;
1175 Window
* children
= NULL
;
1177 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
1182 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
1183 & children
, & noChildren
);
1196 retValue
= wxTheApp
->OnExit();
1200 * Exit in some platform-specific way. Not recommended that the app calls this:
1201 * only for emergencies.
1206 // Yield to other processes
1208 bool wxApp::Yield(bool onlyIfNeeded
)
1210 // Sometimes only 2 yields seem
1211 // to do the trick, e.g. in the
1214 for (i
= 0; i
< 2; i
++)
1216 bool s_inYield
= FALSE
;
1220 if ( !onlyIfNeeded
)
1222 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1230 // Make sure we have an event loop object,
1231 // or Pending/Dispatch will fail
1232 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
1233 wxEventLoop
* newEventLoop
= NULL
;
1236 newEventLoop
= new wxEventLoop
;
1237 wxEventLoop::SetActive(newEventLoop
);
1240 // Call dispatch at least once so that sockets
1242 wxTheApp
->Dispatch();
1244 while (wxTheApp
&& wxTheApp
->Pending())
1245 wxTheApp
->Dispatch();
1248 wxTimer::NotifyTimers();
1254 wxEventLoop::SetActive(NULL
);
1255 delete newEventLoop
;
1266 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
1268 // While the GUI isn't working that well, just print out the
1271 wxAppBase::OnAssert(file
, line
, cond
, msg
);
1274 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
1279 #endif // __WXDEBUG__