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 #include "wx/x11/private.h"
45 //------------------------------------------------------------------------
47 //------------------------------------------------------------------------
49 extern wxList wxPendingDelete
;
51 wxHashTable
*wxWidgetHashTable
= NULL
;
52 wxHashTable
*wxClientWidgetHashTable
= NULL
;
54 wxApp
*wxTheApp
= NULL
;
56 // This is set within wxEntryStart -- too early on
57 // to put these in wxTheApp
58 static bool g_showIconic
= FALSE
;
59 static wxSize g_initialSize
= wxDefaultSize
;
61 // This is required for wxFocusEvent::SetWindow(). It will only
62 // work for focus events which we provoke ourselves (by calling
63 // SetFocus()). It will not work for those events, which X11
65 static wxWindow
*g_nextFocus
= NULL
;
66 static wxWindow
*g_prevFocus
= NULL
;
68 //------------------------------------------------------------------------
70 //------------------------------------------------------------------------
73 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
75 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
77 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
79 // just forward to the default handler for now
80 if (gs_pfnXErrorHandler
)
81 return gs_pfnXErrorHandler(dpy
, xevent
);
87 //------------------------------------------------------------------------
89 //------------------------------------------------------------------------
91 long wxApp::sm_lastMessageTime
= 0;
92 WXDisplay
*wxApp::ms_display
= NULL
;
94 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
96 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
97 EVT_IDLE(wxApp::OnIdle
)
100 bool wxApp::Initialize()
102 wxClassInfo::InitializeClasses();
105 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
108 // GL: I'm annoyed ... I don't know where to put this and I don't want to
109 // create a module for that as it's part of the core.
111 wxPendingEventsLocker
= new wxCriticalSection();
114 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
115 wxTheColourDatabase
->Initialize();
117 wxInitializeStockLists();
118 wxInitializeStockObjects();
120 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
121 wxClientWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
123 wxModule::RegisterModules();
124 if (!wxModule::InitializeModules()) return FALSE
;
129 void wxApp::CleanUp()
131 delete wxWidgetHashTable
;
132 wxWidgetHashTable
= NULL
;
133 delete wxClientWidgetHashTable
;
134 wxClientWidgetHashTable
= NULL
;
136 wxModule::CleanUpModules();
138 delete wxTheColourDatabase
;
139 wxTheColourDatabase
= NULL
;
141 wxDeleteStockObjects();
143 wxDeleteStockLists();
148 wxClassInfo::CleanUpClasses();
151 delete wxPendingEvents
;
152 delete wxPendingEventsLocker
;
155 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
156 // At this point we want to check if there are any memory
157 // blocks that aren't part of the wxDebugContext itself,
158 // as a special case. Then when dumping we need to ignore
159 // wxDebugContext, too.
160 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
162 wxLogDebug("There were memory leaks.");
163 wxDebugContext::Dump();
164 wxDebugContext::PrintStatistics();
168 // do it as the very last thing because everything else can log messages
169 wxLog::DontCreateOnDemand();
170 // do it as the very last thing because everything else can log messages
171 delete wxLog::SetActiveTarget(NULL
);
174 // NB: argc and argv may be changed here, pass by reference!
175 int wxEntryStart( int& argc
, char *argv
[] )
179 // install the X error handler
180 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
182 #endif // __WXDEBUG__
184 char *displayName
= NULL
;
185 bool syncDisplay
= FALSE
;
188 for (i
= 0; i
< argc
; i
++)
190 if (strcmp( argv
[i
], "-display") == 0)
195 displayName
= argv
[i
];
199 else if (strcmp( argv
[i
], "-geometry") == 0)
205 if (sscanf(argv
[i
], "%dx%d", &w
, &h
) != 2)
207 wxLogError( _("Invalid geometry specification '%s'"), wxString::FromAscii(argv
[i
]).c_str() );
211 g_initialSize
= wxSize(w
, h
);
216 else if (strcmp( argv
[i
], "-sync") == 0)
221 else if (strcmp( argv
[i
], "-iconic") == 0)
231 Display
* xdisplay
= XOpenDisplay( displayName
);
234 wxLogError( _("wxWindows could not open display. Exiting.") );
239 XSynchronize(xdisplay
, True
);
241 wxApp::ms_display
= (WXDisplay
*) xdisplay
;
243 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
246 wxSetDetectableAutoRepeat( TRUE
);
249 // Glib's type system required by Pango
253 if (!wxApp::Initialize())
263 if ( !wxTheApp
->OnInitGui() )
270 int wxEntry( int argc
, char *argv
[] )
272 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
273 // This seems to be necessary since there are 'rogue'
274 // objects present at this point (perhaps global objects?)
275 // Setting a checkpoint will ignore them as far as the
276 // memory checking facility is concerned.
277 // Of course you may argue that memory allocated in globals should be
278 // checked, but this is a reasonable compromise.
279 wxDebugContext::SetCheckpoint();
281 int err
= wxEntryStart(argc
, argv
);
287 if (!wxApp::GetInitializerFunction())
289 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
293 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
298 printf( "wxWindows error: wxTheApp == NULL\n" );
302 // Command line argument stuff
303 wxTheApp
->argc
= argc
;
305 wxTheApp
->argv
= new wxChar
*[argc
+1];
307 while (mb_argc
< argc
)
309 wxString tmp
= wxString::FromAscii( argv
[mb_argc
] );
310 wxTheApp
->argv
[mb_argc
] = wxStrdup( tmp
.c_str() );
313 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
315 wxTheApp
->argv
= argv
;
318 if (wxTheApp
->argc
> 0)
320 wxFileName
fname( wxTheApp
->argv
[0] );
321 wxTheApp
->SetAppName( fname
.GetName() );
324 wxTheApp
->m_showIconic
= g_showIconic
;
325 wxTheApp
->m_initialSize
= g_initialSize
;
328 retValue
= wxEntryInitGui();
330 // Here frames insert themselves automatically into wxTopLevelWindows by
331 // getting created in OnInit().
334 if ( !wxTheApp
->OnInit() )
340 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
343 // flush the logged messages if any
344 wxLog
*pLog
= wxLog::GetActiveTarget();
345 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
348 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
349 // for further messages
351 if (wxTheApp
->GetTopWindow())
353 delete wxTheApp
->GetTopWindow();
354 wxTheApp
->SetTopWindow(NULL
);
357 wxTheApp
->DeletePendingObjects();
366 // Static member initialization
367 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
371 // TODO: parse the command line
375 m_mainColormap
= (WXColormap
) NULL
;
376 m_topLevelWidget
= (WXWindow
) NULL
;
377 m_maxRequestSize
= 0;
379 m_showIconic
= FALSE
;
380 m_initialSize
= wxDefaultSize
;
383 m_visualColormap
= NULL
;
394 if (m_visualColormap
)
395 delete [] (XColor
*)m_visualColormap
;
399 bool wxApp::Initialized()
407 int wxApp::MainLoop()
410 m_mainLoop
= new wxEventLoop
;
412 rt
= m_mainLoop
->Run();
420 //-----------------------------------------------------------------------
421 // X11 predicate function for exposure compression
422 //-----------------------------------------------------------------------
427 Bool found_non_matching
;
430 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
432 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
434 if (info
->found_non_matching
)
437 if (xevent
->xany
.type
!= Expose
)
439 info
->found_non_matching
= TRUE
;
443 if (xevent
->xexpose
.window
!= info
->window
)
445 info
->found_non_matching
= TRUE
;
454 //-----------------------------------------------------------------------
455 // Processes an X event, returning TRUE if the event was processed.
456 //-----------------------------------------------------------------------
458 bool wxApp::ProcessXEvent(WXEvent
* _event
)
460 XEvent
* event
= (XEvent
*) _event
;
462 wxWindow
* win
= NULL
;
463 Window window
= XEventGetWindow(event
);
465 Window actualWindow
= window
;
468 // Find the first wxWindow that corresponds to this event window
469 // Because we're receiving events after a window
470 // has been destroyed, assume a 1:1 match between
471 // Window and wxWindow, so if it's not in the table,
472 // it must have been destroyed.
474 win
= wxGetWindowFromTable(window
);
477 #if wxUSE_TWO_WINDOWS
478 win
= wxGetClientWindowFromTable(window
);
485 wxString windowClass
= win
->GetClassInfo()->GetClassName();
492 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
493 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
497 info
.window
= event
->xexpose
.window
;
498 info
.found_non_matching
= FALSE
;
499 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
501 // Don't worry about optimizing redrawing the border etc.
503 win
->NeedUpdateNcAreaInIdle();
508 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
509 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
510 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
511 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
516 info
.window
= event
->xexpose
.window
;
517 info
.found_non_matching
= FALSE
;
518 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
520 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
521 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
523 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
524 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
528 // This simplifies the expose and clear areas to simple
530 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
531 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
533 // If we only have one X11 window, always indicate
534 // that borders might have to be redrawn.
535 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
536 win
->NeedUpdateNcAreaInIdle();
538 // Only erase background, paint in idle time.
539 win
->SendEraseEvents();
551 printf( "GraphicExpose event\n" );
553 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
555 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
556 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
558 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
559 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
561 if (event
->xgraphicsexpose
.count
== 0)
563 // Only erase background, paint in idle time.
564 win
->SendEraseEvents();
574 if (!win
->IsEnabled())
577 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
578 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
580 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
582 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
583 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
586 keyEvent
.SetEventType(wxEVT_CHAR
);
587 // Do the translation again, retaining the ASCII
589 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, TRUE
);
590 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
593 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
594 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
596 wxNavigationKeyEvent new_event
;
597 new_event
.SetEventObject( win
->GetParent() );
598 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
599 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
600 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
601 new_event
.SetWindowChange( keyEvent
.ControlDown() );
602 new_event
.SetCurrentFocus( win
);
603 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
610 if (!win
->IsEnabled())
613 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
614 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
616 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
618 case ConfigureNotify
:
621 if (event
->update
.utype
== GR_UPDATE_SIZE
)
624 if (win
->IsTopLevel())
626 wxTopLevelWindow
*tlw
= (wxTopLevelWindow
*) win
;
627 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
628 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
631 if (win
->IsTopLevel() && win
->IsShown())
633 wxTopLevelWindowX11
*tlw
= (wxTopLevelWindowX11
*) win
;
634 tlw
->SetNeedResizeInIdle();
638 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
639 sizeEvent
.SetEventObject( win
);
641 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
650 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
651 return HandlePropertyChange(_event
);
655 if (!win
->IsEnabled())
658 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
659 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
661 if (event
->xclient
.message_type
== wm_protocols
)
663 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
674 printf( "destroy from %s\n", win
->GetName().c_str() );
679 printf( "create from %s\n", win
->GetName().c_str() );
684 printf( "map request from %s\n", win
->GetName().c_str() );
689 printf( "resize request from %s\n", win
->GetName().c_str() );
691 Display
*disp
= (Display
*) wxGetDisplay();
696 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
698 wxSize sz
= win
->GetSize();
699 wxSizeEvent
sizeEvent(sz
, win
->GetId());
700 sizeEvent
.SetEventObject(win
);
702 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
707 case GR_EVENT_TYPE_CLOSE_REQ
:
724 if (!win
->IsEnabled())
727 // Here we check if the top level window is
728 // disabled, which is one aspect of modality.
730 while (tlw
&& !tlw
->IsTopLevel())
731 tlw
= tlw
->GetParent();
732 if (tlw
&& !tlw
->IsEnabled())
735 if (event
->type
== ButtonPress
)
737 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
739 // This might actually be done in wxWindow::SetFocus()
740 // and not here. TODO.
741 g_prevFocus
= wxWindow::FindFocus();
744 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
746 // Record the fact that this window is
747 // getting the focus, because we'll need to
748 // check if its parent is getting a bogus
749 // focus and duly ignore it.
750 // TODO: may need to have this code in SetFocus, too.
751 extern wxWindow
* g_GettingFocus
;
752 g_GettingFocus
= win
;
758 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
760 // Throw out NotifyGrab and NotifyUngrab
761 if (event
->xcrossing
.mode
!= NotifyNormal
)
765 wxMouseEvent wxevent
;
766 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
767 return win
->GetEventHandler()->ProcessEvent( wxevent
);
772 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
773 (event
->xfocus
.mode
== NotifyNormal
))
776 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
778 extern wxWindow
* g_GettingFocus
;
779 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
781 // Ignore this, this can be a spurious FocusIn
782 // caused by a child having its focus set.
783 g_GettingFocus
= NULL
;
784 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
789 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
790 focusEvent
.SetEventObject(win
);
791 focusEvent
.SetWindow( g_prevFocus
);
794 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
803 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
804 (event
->xfocus
.mode
== NotifyNormal
))
807 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
809 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
810 focusEvent
.SetEventObject(win
);
811 focusEvent
.SetWindow( g_nextFocus
);
813 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
821 //wxString eventName = wxGetXEventName(XEvent& event);
822 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
831 // Returns TRUE if more time is needed.
832 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
833 // but ProcessIdle may be needed by apps, so is kept.
834 bool wxApp::ProcessIdle()
837 event
.SetEventObject(this);
840 return event
.MoreRequested();
843 void wxApp::ExitMainLoop()
849 // Is a message/event pending?
850 bool wxApp::Pending()
852 return wxEventLoop::GetActive()->Pending();
855 // Dispatch a message.
856 void wxApp::Dispatch()
858 wxEventLoop::GetActive()->Dispatch();
861 // This should be redefined in a derived class for
862 // handling property change events for XAtom IPC.
863 bool wxApp::HandlePropertyChange(WXEvent
*event
)
865 // by default do nothing special
866 // TODO: what to do for X11
867 // XtDispatchEvent((XEvent*) event);
871 void wxApp::OnIdle(wxIdleEvent
& event
)
873 static bool s_inOnIdle
= FALSE
;
875 // Avoid recursion (via ProcessEvent default case)
881 // Resend in the main thread events which have been prepared in other
883 ProcessPendingEvents();
885 // 'Garbage' collection of windows deleted with Close()
886 DeletePendingObjects();
888 // Send OnIdle events to all windows
889 bool needMore
= SendIdleEvents();
892 event
.RequestMore(TRUE
);
899 // **** please implement me! ****
900 // Wake up the idle handler processor, even if it is in another thread...
904 // Send idle event to all top-level windows
905 bool wxApp::SendIdleEvents()
907 bool needMore
= FALSE
;
909 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
912 wxWindow
* win
= node
->GetData();
913 if (SendIdleEvents(win
))
915 node
= node
->GetNext();
921 // Send idle event to window and all subwindows
922 bool wxApp::SendIdleEvents(wxWindow
* win
)
924 bool needMore
= FALSE
;
927 event
.SetEventObject(win
);
929 win
->GetEventHandler()->ProcessEvent(event
);
931 if (event
.MoreRequested())
934 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
937 wxWindow
* win
= (wxWindow
*) node
->GetData();
938 if (SendIdleEvents(win
))
941 node
= node
->GetNext();
944 win
->OnInternalIdle();
949 void wxApp::DeletePendingObjects()
951 wxNode
*node
= wxPendingDelete
.GetFirst();
954 wxObject
*obj
= (wxObject
*)node
->GetData();
958 if (wxPendingDelete
.Member(obj
))
961 // Deleting one object may have deleted other pending
962 // objects, so start from beginning of list again.
963 node
= wxPendingDelete
.GetFirst();
967 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
972 while (!(mask
& 0x1))
985 // Create display, and other initialization
986 bool wxApp::OnInitGui()
988 // Eventually this line will be removed, but for
989 // now we don't want to try popping up a dialog
990 // for error messages.
991 delete wxLog::SetActiveTarget(new wxLogStderr
);
993 if (!wxAppBase::OnInitGui())
996 GetMainColormap( wxApp::GetDisplay() );
998 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
1001 // Get info about the current visual. It is enough
1002 // to do this once here unless we support different
1003 // visuals, displays and screens. Given that wxX11
1004 // mostly for embedded things, that is no real
1006 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
1007 int xscreen
= DefaultScreen(xdisplay
);
1008 Visual
* xvisual
= DefaultVisual(xdisplay
,xscreen
);
1009 int xdepth
= DefaultDepth(xdisplay
, xscreen
);
1011 XVisualInfo vinfo_template
;
1012 vinfo_template
.visual
= xvisual
;
1013 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
1014 vinfo_template
.depth
= xdepth
;
1017 XVisualInfo
*vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1018 wxASSERT_MSG( vi
, wxT("No visual info") );
1020 m_visualType
= vi
->visual
->c_class
;
1021 m_visualScreen
= vi
->screen
;
1023 m_visualRedMask
= vi
->red_mask
;
1024 m_visualGreenMask
= vi
->green_mask
;
1025 m_visualBlueMask
= vi
->blue_mask
;
1027 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
1029 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
, &m_visualRedPrec
);
1030 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
, &m_visualGreenPrec
);
1031 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
, &m_visualBluePrec
);
1034 m_visualDepth
= xdepth
;
1036 xdepth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
1038 m_visualColormapSize
= vi
->colormap_size
;
1042 if (m_visualDepth
> 8)
1045 m_visualColormap
= new XColor
[m_visualColormapSize
];
1046 XColor
* colors
= (XColor
*) m_visualColormap
;
1048 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1049 colors
[i
].pixel
= i
;
1051 XQueryColors( xdisplay
, DefaultColormap(xdisplay
,xscreen
), colors
, m_visualColormapSize
);
1053 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
1055 for (int r
= 0; r
< 32; r
++)
1057 for (int g
= 0; g
< 32; g
++)
1059 for (int b
= 0; b
< 32; b
++)
1061 int rr
= (r
<< 3) | (r
>> 2);
1062 int gg
= (g
<< 3) | (g
>> 2);
1063 int bb
= (b
<< 3) | (b
>> 2);
1069 int max
= 3 * 65536;
1071 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1073 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
1074 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
1075 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
1076 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1079 index
= i
; max
= sum
;
1085 // assume 8-bit true or static colors. this really exists
1086 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
1087 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
1088 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
1090 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
1101 #include <pango/pango.h>
1102 #include <pango/pangox.h>
1103 #include <pango/pangoxft.h>
1105 PangoContext
* wxApp::GetPangoContext()
1107 static PangoContext
*ret
= NULL
;
1111 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
1114 int xscreen
= DefaultScreen(xdisplay
);
1115 static int use_xft
= -1;
1118 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
1119 use_xft
= (val
== L
"1");
1123 ret
= pango_xft_get_context( xdisplay
, xscreen
);
1126 ret
= pango_x_get_context( xdisplay
);
1128 if (!PANGO_IS_CONTEXT(ret
))
1129 wxLogError( wxT("No pango context.") );
1135 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
1137 if (!display
) /* Must be called first with non-NULL display */
1138 return m_mainColormap
;
1140 int defaultScreen
= DefaultScreen((Display
*) display
);
1141 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
1143 Colormap c
= DefaultColormapOfScreen(screen
);
1145 if (!m_mainColormap
)
1146 m_mainColormap
= (WXColormap
) c
;
1148 return (WXColormap
) c
;
1151 Window
wxGetWindowParent(Window window
)
1153 wxASSERT_MSG( window
, "invalid window" );
1157 Window parent
, root
= 0;
1161 unsigned int noChildren
= 0;
1163 Window
* children
= NULL
;
1165 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
1170 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
1171 & children
, & noChildren
);
1184 retValue
= wxTheApp
->OnExit();
1188 * Exit in some platform-specific way. Not recommended that the app calls this:
1189 * only for emergencies.
1194 // Yield to other processes
1196 bool wxApp::Yield(bool onlyIfNeeded
)
1198 // Sometimes only 2 yields seem
1199 // to do the trick, e.g. in the
1202 for (i
= 0; i
< 2; i
++)
1204 bool s_inYield
= FALSE
;
1208 if ( !onlyIfNeeded
)
1210 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1218 // Make sure we have an event loop object,
1219 // or Pending/Dispatch will fail
1220 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
1221 wxEventLoop
* newEventLoop
= NULL
;
1224 newEventLoop
= new wxEventLoop
;
1225 wxEventLoop::SetActive(newEventLoop
);
1228 // Call dispatch at least once so that sockets
1230 wxTheApp
->Dispatch();
1232 while (wxTheApp
&& wxTheApp
->Pending())
1233 wxTheApp
->Dispatch();
1236 wxTimer::NotifyTimers();
1242 wxEventLoop::SetActive(NULL
);
1243 delete newEventLoop
;
1254 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
1256 // While the GUI isn't working that well, just print out the
1259 wxAppBase::OnAssert(file
, line
, cond
, msg
);
1262 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
1267 #endif // __WXDEBUG__