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"
30 #include "wx/univ/theme.h"
31 #include "wx/univ/renderer.h"
33 #define ABS(a) (((a) < 0) ? -(a) : (a))
36 #include "wx/thread.h"
39 #if wxUSE_WX_RESOURCES
40 #include "wx/resource.h"
43 #include "wx/x11/private.h"
47 //------------------------------------------------------------------------
49 //------------------------------------------------------------------------
51 extern wxList wxPendingDelete
;
53 wxHashTable
*wxWidgetHashTable
= NULL
;
54 wxHashTable
*wxClientWidgetHashTable
= NULL
;
56 wxApp
*wxTheApp
= NULL
;
58 // This is set within wxEntryStart -- too early on
59 // to put these in wxTheApp
60 static int g_newArgc
= 0;
61 static wxChar
** g_newArgv
= NULL
;
62 static bool g_showIconic
= FALSE
;
63 static wxSize g_initialSize
= wxDefaultSize
;
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()
143 delete wxWidgetHashTable
;
144 wxWidgetHashTable
= NULL
;
145 delete wxClientWidgetHashTable
;
146 wxClientWidgetHashTable
= NULL
;
148 wxModule::CleanUpModules();
150 #if wxUSE_WX_RESOURCES
151 wxCleanUpResourceSystem();
154 delete wxTheColourDatabase
;
155 wxTheColourDatabase
= NULL
;
157 wxDeleteStockObjects();
159 wxDeleteStockLists();
164 wxClassInfo::CleanUpClasses();
167 delete wxPendingEvents
;
168 delete wxPendingEventsLocker
;
171 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
172 // At this point we want to check if there are any memory
173 // blocks that aren't part of the wxDebugContext itself,
174 // as a special case. Then when dumping we need to ignore
175 // wxDebugContext, too.
176 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
178 wxLogDebug("There were memory leaks.");
179 wxDebugContext::Dump();
180 wxDebugContext::PrintStatistics();
184 // do it as the very last thing because everything else can log messages
185 wxLog::DontCreateOnDemand();
186 // do it as the very last thing because everything else can log messages
187 delete wxLog::SetActiveTarget(NULL
);
190 // NB: argc and argv may be changed here, pass by reference!
191 int wxEntryStart( int& argc
, char *argv
[] )
195 // install the X error handler
196 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
198 #endif // __WXDEBUG__
200 wxString displayName
;
201 bool syncDisplay
= FALSE
;
203 // Parse the arguments.
204 // We can't use wxCmdLineParser or OnInitCmdLine and friends because
205 // we have to create the Display earlier. If we can find a way to
206 // use the wxAppBase API then I'll be quite happy to change it.
207 g_newArgv
= new wxChar
*[argc
+ 1];
210 for (i
= 0; i
< argc
; i
++)
212 wxString
arg(argv
[i
]);
213 if (arg
== wxT("-display"))
218 displayName
= argv
[i
];
222 else if (arg
== wxT("-geometry"))
227 wxString windowGeometry
= argv
[i
];
229 if (wxSscanf(windowGeometry
.c_str(), _T("%dx%d"), &w
, &h
) != 2)
231 wxLogError(_("Invalid geometry specification '%s'"), windowGeometry
.c_str());
235 g_initialSize
= wxSize(w
, h
);
240 else if (arg
== wxT("-sync"))
245 else if (arg
== wxT("-iconic"))
252 // Not eaten by wxWindows, so pass through
253 g_newArgv
[g_newArgc
] = argv
[i
];
256 g_newArgv
[g_newArgc
] = NULL
;
258 Display
* xdisplay
= NULL
;
259 if (displayName
.IsEmpty())
260 xdisplay
= XOpenDisplay(NULL
);
262 xdisplay
= XOpenDisplay((char*) displayName
.c_str());
266 wxLogError( _("wxWindows could not open display. Exiting.") );
272 XSynchronize(xdisplay
, True
);
275 wxApp::ms_display
= (WXDisplay
*) xdisplay
;
277 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
279 wxSetDetectableAutoRepeat( TRUE
);
281 if (!wxApp::Initialize())
291 if ( !wxTheApp
->OnInitGui() )
298 int wxEntry( int argc
, char *argv
[] )
300 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
301 // This seems to be necessary since there are 'rogue'
302 // objects present at this point (perhaps global objects?)
303 // Setting a checkpoint will ignore them as far as the
304 // memory checking facility is concerned.
305 // Of course you may argue that memory allocated in globals should be
306 // checked, but this is a reasonable compromise.
307 wxDebugContext::SetCheckpoint();
309 int err
= wxEntryStart(argc
, argv
);
315 if (!wxApp::GetInitializerFunction())
317 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
321 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
326 printf( "wxWindows error: wxTheApp == NULL\n" );
330 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
331 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
333 // The command line may have been changed
334 // by stripping out -display etc.
337 wxTheApp
->argc
= g_newArgc
;
338 wxTheApp
->argv
= g_newArgv
;
342 wxTheApp
->argc
= argc
;
343 wxTheApp
->argv
= argv
;
345 wxTheApp
->m_showIconic
= g_showIconic
;
346 wxTheApp
->m_initialSize
= g_initialSize
;
349 retValue
= wxEntryInitGui();
351 // Here frames insert themselves automatically into wxTopLevelWindows by
352 // getting created in OnInit().
355 if ( !wxTheApp
->OnInit() )
361 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
364 // flush the logged messages if any
365 wxLog
*pLog
= wxLog::GetActiveTarget();
366 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
369 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
370 // for further messages
372 if (wxTheApp
->GetTopWindow())
374 delete wxTheApp
->GetTopWindow();
375 wxTheApp
->SetTopWindow(NULL
);
378 wxTheApp
->DeletePendingObjects();
387 // Static member initialization
388 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
392 // TODO: parse the command line
396 m_mainColormap
= (WXColormap
) NULL
;
397 m_topLevelWidget
= (WXWindow
) NULL
;
398 m_maxRequestSize
= 0;
400 m_showIconic
= FALSE
;
401 m_initialSize
= wxDefaultSize
;
404 m_visualColormap
= NULL
;
415 if (m_visualColormap
)
416 delete [] (XColor
*)m_visualColormap
;
420 bool wxApp::Initialized()
428 int wxApp::MainLoop()
431 m_mainLoop
= new wxEventLoop
;
433 rt
= m_mainLoop
->Run();
441 //-----------------------------------------------------------------------
442 // X11 predicate function for exposure compression
443 //-----------------------------------------------------------------------
448 Bool found_non_matching
;
451 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
453 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
455 if (info
->found_non_matching
)
458 if (xevent
->xany
.type
!= Expose
)
460 info
->found_non_matching
= TRUE
;
464 if (xevent
->xexpose
.window
!= info
->window
)
466 info
->found_non_matching
= TRUE
;
475 //-----------------------------------------------------------------------
476 // Processes an X event, returning TRUE if the event was processed.
477 //-----------------------------------------------------------------------
479 bool wxApp::ProcessXEvent(WXEvent
* _event
)
481 XEvent
* event
= (XEvent
*) _event
;
483 wxWindow
* win
= NULL
;
484 Window window
= XEventGetWindow(event
);
486 Window actualWindow
= window
;
489 // Find the first wxWindow that corresponds to this event window
490 // Because we're receiving events after a window
491 // has been destroyed, assume a 1:1 match between
492 // Window and wxWindow, so if it's not in the table,
493 // it must have been destroyed.
495 win
= wxGetWindowFromTable(window
);
498 #if wxUSE_TWO_WINDOWS
499 win
= wxGetClientWindowFromTable(window
);
506 wxString windowClass
= win
->GetClassInfo()->GetClassName();
513 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
514 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
518 info
.window
= event
->xexpose
.window
;
519 info
.found_non_matching
= FALSE
;
520 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
522 // Don't worry about optimizing redrawing the border etc.
524 win
->NeedUpdateNcAreaInIdle();
529 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
530 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
531 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
532 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
537 info
.window
= event
->xexpose
.window
;
538 info
.found_non_matching
= FALSE
;
539 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
541 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
542 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
544 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
545 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
549 // This simplifies the expose and clear areas to simple
551 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
552 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
554 // If we only have one X11 window, always indicate
555 // that borders might have to be redrawn.
556 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
557 win
->NeedUpdateNcAreaInIdle();
559 // Only erase background, paint in idle time.
560 win
->SendEraseEvents();
570 printf( "GraphicExpose event\n" );
572 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
574 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
575 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
577 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
578 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
580 if (event
->xgraphicsexpose
.count
== 0)
582 // Only erase background, paint in idle time.
583 win
->SendEraseEvents();
593 if (!win
->IsEnabled())
596 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
597 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
599 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
601 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
602 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
605 keyEvent
.SetEventType(wxEVT_CHAR
);
606 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
609 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
610 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
612 wxNavigationKeyEvent new_event
;
613 new_event
.SetEventObject( win
->GetParent() );
614 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
615 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
616 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
617 new_event
.SetWindowChange( keyEvent
.ControlDown() );
618 new_event
.SetCurrentFocus( win
);
619 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
626 if (!win
->IsEnabled())
629 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
630 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
632 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
634 case ConfigureNotify
:
637 if (event
->update
.utype
== GR_UPDATE_SIZE
)
640 if (win
->IsTopLevel())
642 wxTopLevelWindow
*tlw
= (wxTopLevelWindow
*) win
;
643 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
644 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
647 if (win
->IsTopLevel() && win
->IsShown())
649 wxTopLevelWindowX11
*tlw
= (wxTopLevelWindowX11
*) win
;
650 tlw
->SetNeedResizeInIdle();
654 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
655 sizeEvent
.SetEventObject( win
);
657 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
666 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
667 return HandlePropertyChange(_event
);
671 if (!win
->IsEnabled())
674 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
675 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
677 if (event
->xclient
.message_type
== wm_protocols
)
679 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
690 printf( "destroy from %s\n", win
->GetName().c_str() );
695 printf( "create from %s\n", win
->GetName().c_str() );
700 printf( "map request from %s\n", win
->GetName().c_str() );
705 printf( "resize request from %s\n", win
->GetName().c_str() );
707 Display
*disp
= (Display
*) wxGetDisplay();
712 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
714 wxSize sz
= win
->GetSize();
715 wxSizeEvent
sizeEvent(sz
, win
->GetId());
716 sizeEvent
.SetEventObject(win
);
718 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
723 case GR_EVENT_TYPE_CLOSE_REQ
:
740 if (!win
->IsEnabled())
743 // Here we check if the top level window is
744 // disabled, which is one aspect of modality.
746 while (tlw
&& !tlw
->IsTopLevel())
747 tlw
= tlw
->GetParent();
748 if (tlw
&& !tlw
->IsEnabled())
751 if (event
->type
== ButtonPress
)
753 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
755 // This might actually be done in wxWindow::SetFocus()
756 // and not here. TODO.
757 g_prevFocus
= wxWindow::FindFocus();
760 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
762 // Record the fact that this window is
763 // getting the focus, because we'll need to
764 // check if its parent is getting a bogus
765 // focus and duly ignore it.
766 // TODO: may need to have this code in SetFocus, too.
767 extern wxWindow
* g_GettingFocus
;
768 g_GettingFocus
= win
;
774 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
776 // Throw out NotifyGrab and NotifyUngrab
777 if (event
->xcrossing
.mode
!= NotifyNormal
)
781 wxMouseEvent wxevent
;
782 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
783 return win
->GetEventHandler()->ProcessEvent( wxevent
);
788 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
789 (event
->xfocus
.mode
== NotifyNormal
))
792 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
794 extern wxWindow
* g_GettingFocus
;
795 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
797 // Ignore this, this can be a spurious FocusIn
798 // caused by a child having its focus set.
799 g_GettingFocus
= NULL
;
800 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
805 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
806 focusEvent
.SetEventObject(win
);
807 focusEvent
.SetWindow( g_prevFocus
);
810 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
819 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
820 (event
->xfocus
.mode
== NotifyNormal
))
823 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
825 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
826 focusEvent
.SetEventObject(win
);
827 focusEvent
.SetWindow( g_nextFocus
);
829 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
837 //wxString eventName = wxGetXEventName(XEvent& event);
838 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
847 // Returns TRUE if more time is needed.
848 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
849 // but ProcessIdle may be needed by apps, so is kept.
850 bool wxApp::ProcessIdle()
853 event
.SetEventObject(this);
856 return event
.MoreRequested();
859 void wxApp::ExitMainLoop()
865 // Is a message/event pending?
866 bool wxApp::Pending()
868 return wxEventLoop::GetActive()->Pending();
871 // Dispatch a message.
872 void wxApp::Dispatch()
874 wxEventLoop::GetActive()->Dispatch();
877 // This should be redefined in a derived class for
878 // handling property change events for XAtom IPC.
879 bool wxApp::HandlePropertyChange(WXEvent
*event
)
881 // by default do nothing special
882 // TODO: what to do for X11
883 // XtDispatchEvent((XEvent*) event);
887 void wxApp::OnIdle(wxIdleEvent
& event
)
889 static bool s_inOnIdle
= FALSE
;
891 // Avoid recursion (via ProcessEvent default case)
897 // Resend in the main thread events which have been prepared in other
899 ProcessPendingEvents();
901 // 'Garbage' collection of windows deleted with Close()
902 DeletePendingObjects();
904 // Send OnIdle events to all windows
905 bool needMore
= SendIdleEvents();
908 event
.RequestMore(TRUE
);
915 // **** please implement me! ****
916 // Wake up the idle handler processor, even if it is in another thread...
920 // Send idle event to all top-level windows
921 bool wxApp::SendIdleEvents()
923 bool needMore
= FALSE
;
925 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
928 wxWindow
* win
= node
->GetData();
929 if (SendIdleEvents(win
))
931 node
= node
->GetNext();
937 // Send idle event to window and all subwindows
938 bool wxApp::SendIdleEvents(wxWindow
* win
)
940 bool needMore
= FALSE
;
943 event
.SetEventObject(win
);
945 win
->GetEventHandler()->ProcessEvent(event
);
947 if (event
.MoreRequested())
950 wxNode
* node
= win
->GetChildren().First();
953 wxWindow
* win
= (wxWindow
*) node
->Data();
954 if (SendIdleEvents(win
))
960 win
->OnInternalIdle();
965 void wxApp::DeletePendingObjects()
967 wxNode
*node
= wxPendingDelete
.First();
970 wxObject
*obj
= (wxObject
*)node
->Data();
974 if (wxPendingDelete
.Member(obj
))
977 // Deleting one object may have deleted other pending
978 // objects, so start from beginning of list again.
979 node
= wxPendingDelete
.First();
983 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
988 while (!(mask
& 0x1))
1001 // Create display, and other initialization
1002 bool wxApp::OnInitGui()
1004 // Eventually this line will be removed, but for
1005 // now we don't want to try popping up a dialog
1006 // for error messages.
1007 delete wxLog::SetActiveTarget(new wxLogStderr
);
1009 if (!wxAppBase::OnInitGui())
1012 GetMainColormap( wxApp::GetDisplay() );
1014 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
1017 // Get info about the current visual. It is enough
1018 // to do this once here unless we support different
1019 // visuals, displays and screens. Given that wxX11
1020 // mostly for embedded things, that is no real
1022 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
1023 int xscreen
= DefaultScreen(xdisplay
);
1024 Visual
* xvisual
= DefaultVisual(xdisplay
,xscreen
);
1025 int xdepth
= DefaultDepth(xdisplay
, xscreen
);
1027 XVisualInfo vinfo_template
;
1028 vinfo_template
.visual
= xvisual
;
1029 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
1030 vinfo_template
.depth
= xdepth
;
1033 XVisualInfo
*vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1034 wxASSERT_MSG( vi
, wxT("No visual info") );
1036 m_visualType
= vi
->visual
->c_class
;
1037 m_visualScreen
= vi
->screen
;
1039 m_visualRedMask
= vi
->red_mask
;
1040 m_visualGreenMask
= vi
->green_mask
;
1041 m_visualBlueMask
= vi
->blue_mask
;
1043 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
1045 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
, &m_visualRedPrec
);
1046 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
, &m_visualGreenPrec
);
1047 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
, &m_visualBluePrec
);
1050 m_visualDepth
= xdepth
;
1052 xdepth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
1054 m_visualColormapSize
= vi
->colormap_size
;
1058 if (m_visualDepth
> 8)
1061 m_visualColormap
= new XColor
[m_visualColormapSize
];
1062 XColor
* colors
= (XColor
*) m_visualColormap
;
1064 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1065 colors
[i
].pixel
= i
;
1067 XQueryColors( xdisplay
, DefaultColormap(xdisplay
,xscreen
), colors
, m_visualColormapSize
);
1069 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
1071 for (int r
= 0; r
< 32; r
++)
1073 for (int g
= 0; g
< 32; g
++)
1075 for (int b
= 0; b
< 32; b
++)
1077 int rr
= (r
<< 3) | (r
>> 2);
1078 int gg
= (g
<< 3) | (g
>> 2);
1079 int bb
= (b
<< 3) | (b
>> 2);
1085 int max
= 3 * 65536;
1087 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1089 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
1090 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
1091 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
1092 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1095 index
= i
; max
= sum
;
1101 // assume 8-bit true or static colors. this really exists
1102 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
1103 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
1104 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
1106 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
1115 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
1117 if (!display
) /* Must be called first with non-NULL display */
1118 return m_mainColormap
;
1120 int defaultScreen
= DefaultScreen((Display
*) display
);
1121 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
1123 Colormap c
= DefaultColormapOfScreen(screen
);
1125 if (!m_mainColormap
)
1126 m_mainColormap
= (WXColormap
) c
;
1128 return (WXColormap
) c
;
1131 Window
wxGetWindowParent(Window window
)
1133 wxASSERT_MSG( window
, "invalid window" );
1137 Window parent
, root
= 0;
1141 unsigned int noChildren
= 0;
1143 Window
* children
= NULL
;
1145 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
1150 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
1151 & children
, & noChildren
);
1164 retValue
= wxTheApp
->OnExit();
1168 * Exit in some platform-specific way. Not recommended that the app calls this:
1169 * only for emergencies.
1174 // Yield to other processes
1176 bool wxApp::Yield(bool onlyIfNeeded
)
1178 // Sometimes only 2 yields seem
1179 // to do the trick, e.g. in the
1182 for (i
= 0; i
< 2; i
++)
1184 bool s_inYield
= FALSE
;
1188 if ( !onlyIfNeeded
)
1190 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1198 // Make sure we have an event loop object,
1199 // or Pending/Dispatch will fail
1200 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
1201 wxEventLoop
* newEventLoop
= NULL
;
1204 newEventLoop
= new wxEventLoop
;
1205 wxEventLoop::SetActive(newEventLoop
);
1208 while (wxTheApp
&& wxTheApp
->Pending())
1209 wxTheApp
->Dispatch();
1212 wxTimer::NotifyTimers();
1218 wxEventLoop::SetActive(NULL
);
1219 delete newEventLoop
;
1230 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
1232 // While the GUI isn't working that well, just print out the
1235 wxAppBase::OnAssert(file
, line
, cond
, msg
);
1238 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
1243 #endif // __WXDEBUG__