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
];
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
];
257 Display
* xdisplay
= NULL
;
258 if (displayName
.IsEmpty())
259 xdisplay
= XOpenDisplay(NULL
);
261 xdisplay
= XOpenDisplay((char*) displayName
.c_str());
265 wxLogError( _("wxWindows could not open display. Exiting.") );
271 XSynchronize(xdisplay
, True
);
274 wxApp::ms_display
= (WXDisplay
*) xdisplay
;
276 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
278 wxSetDetectableAutoRepeat( TRUE
);
280 if (!wxApp::Initialize())
290 if ( !wxTheApp
->OnInitGui() )
297 int wxEntry( int argc
, char *argv
[] )
299 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
300 // This seems to be necessary since there are 'rogue'
301 // objects present at this point (perhaps global objects?)
302 // Setting a checkpoint will ignore them as far as the
303 // memory checking facility is concerned.
304 // Of course you may argue that memory allocated in globals should be
305 // checked, but this is a reasonable compromise.
306 wxDebugContext::SetCheckpoint();
308 int err
= wxEntryStart(argc
, argv
);
314 if (!wxApp::GetInitializerFunction())
316 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
320 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
325 printf( "wxWindows error: wxTheApp == NULL\n" );
329 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
330 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
332 // The command line may have been changed
333 // by stripping out -display etc.
336 wxTheApp
->argc
= g_newArgc
;
337 wxTheApp
->argv
= g_newArgv
;
341 wxTheApp
->argc
= argc
;
342 wxTheApp
->argv
= argv
;
344 wxTheApp
->m_showIconic
= g_showIconic
;
345 wxTheApp
->m_initialSize
= g_initialSize
;
348 retValue
= wxEntryInitGui();
350 // Here frames insert themselves automatically into wxTopLevelWindows by
351 // getting created in OnInit().
354 if ( !wxTheApp
->OnInit() )
360 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
363 // flush the logged messages if any
364 wxLog
*pLog
= wxLog::GetActiveTarget();
365 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
368 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
369 // for further messages
371 if (wxTheApp
->GetTopWindow())
373 delete wxTheApp
->GetTopWindow();
374 wxTheApp
->SetTopWindow(NULL
);
377 wxTheApp
->DeletePendingObjects();
386 // Static member initialization
387 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
394 m_wantDebugOutput
= TRUE
;
398 m_exitOnFrameDelete
= TRUE
;
399 m_mainColormap
= (WXColormap
) NULL
;
400 m_topLevelWidget
= (WXWindow
) NULL
;
401 m_maxRequestSize
= 0;
403 m_showIconic
= FALSE
;
404 m_initialSize
= wxDefaultSize
;
407 m_visualColormap
= NULL
;
418 if (m_visualColormap
)
419 delete [] (XColor
*)m_visualColormap
;
423 bool wxApp::Initialized()
431 int wxApp::MainLoop()
434 m_mainLoop
= new wxEventLoop
;
436 rt
= m_mainLoop
->Run();
444 //-----------------------------------------------------------------------
445 // X11 predicate function for exposure compression
446 //-----------------------------------------------------------------------
451 Bool found_non_matching
;
454 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
456 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
458 if (info
->found_non_matching
)
461 if (xevent
->xany
.type
!= Expose
)
463 info
->found_non_matching
= TRUE
;
467 if (xevent
->xexpose
.window
!= info
->window
)
469 info
->found_non_matching
= TRUE
;
478 //-----------------------------------------------------------------------
479 // Processes an X event, returning TRUE if the event was processed.
480 //-----------------------------------------------------------------------
482 bool wxApp::ProcessXEvent(WXEvent
* _event
)
484 XEvent
* event
= (XEvent
*) _event
;
486 wxWindow
* win
= NULL
;
487 Window window
= XEventGetWindow(event
);
489 Window actualWindow
= window
;
492 // Find the first wxWindow that corresponds to this event window
493 // Because we're receiving events after a window
494 // has been destroyed, assume a 1:1 match between
495 // Window and wxWindow, so if it's not in the table,
496 // it must have been destroyed.
498 win
= wxGetWindowFromTable(window
);
501 #if wxUSE_TWO_WINDOWS
502 win
= wxGetClientWindowFromTable(window
);
509 wxString windowClass
= win
->GetClassInfo()->GetClassName();
516 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
517 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
521 info
.window
= event
->xexpose
.window
;
522 info
.found_non_matching
= FALSE
;
523 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
525 // Don't worry about optimizing redrawing the border etc.
527 win
->NeedUpdateNcAreaInIdle();
532 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
533 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
534 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
535 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
540 info
.window
= event
->xexpose
.window
;
541 info
.found_non_matching
= FALSE
;
542 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
544 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
545 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
547 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
548 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
552 // This simplifies the expose and clear areas to simple
554 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
555 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
557 // If we only have one X11 window, always indicate
558 // that borders might have to be redrawn.
559 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
560 win
->NeedUpdateNcAreaInIdle();
562 // Only erase background, paint in idle time.
563 win
->SendEraseEvents();
573 printf( "GraphicExpose event\n" );
575 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str(),
576 event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
577 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
579 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
580 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
582 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
583 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
585 if (event
->xgraphicsexpose
.count
== 0)
587 // Only erase background, paint in idle time.
588 win
->SendEraseEvents();
598 if (!win
->IsEnabled())
601 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
602 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
604 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
606 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
607 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
610 keyEvent
.SetEventType(wxEVT_CHAR
);
611 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
614 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
615 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
617 wxNavigationKeyEvent new_event
;
618 new_event
.SetEventObject( win
->GetParent() );
619 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
620 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
621 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
622 new_event
.SetWindowChange( keyEvent
.ControlDown() );
623 new_event
.SetCurrentFocus( win
);
624 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
631 if (!win
->IsEnabled())
634 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
635 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
637 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
639 case ConfigureNotify
:
642 if (event
->update
.utype
== GR_UPDATE_SIZE
)
645 if (win
->IsTopLevel())
647 wxTopLevelWindow
*tlw
= (wxTopLevelWindow
*) win
;
648 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
649 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
652 if (win
->IsTopLevel() && win
->IsShown())
654 wxTopLevelWindowX11
*tlw
= (wxTopLevelWindowX11
*) win
;
655 tlw
->SetNeedResizeInIdle();
659 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
660 sizeEvent
.SetEventObject( win
);
662 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
671 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
672 return HandlePropertyChange(_event
);
676 if (!win
->IsEnabled())
679 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
680 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
682 if (event
->xclient
.message_type
== wm_protocols
)
684 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
695 printf( "destroy from %s\n", win
->GetName().c_str() );
700 printf( "create from %s\n", win
->GetName().c_str() );
705 printf( "map request from %s\n", win
->GetName().c_str() );
710 printf( "resize request from %s\n", win
->GetName().c_str() );
712 Display
*disp
= (Display
*) wxGetDisplay();
717 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
719 wxSize sz
= win
->GetSize();
720 wxSizeEvent
sizeEvent(sz
, win
->GetId());
721 sizeEvent
.SetEventObject(win
);
723 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
728 case GR_EVENT_TYPE_CLOSE_REQ
:
745 if (!win
->IsEnabled())
748 // Here we check if the top level window is
749 // disabled, which is one aspect of modality.
751 while (tlw
&& !tlw
->IsTopLevel())
752 tlw
= tlw
->GetParent();
753 if (tlw
&& !tlw
->IsEnabled())
756 if (event
->type
== ButtonPress
)
758 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
760 // This might actually be done in wxWindow::SetFocus()
761 // and not here. TODO.
762 g_prevFocus
= wxWindow::FindFocus();
765 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
767 // Record the fact that this window is
768 // getting the focus, because we'll need to
769 // check if its parent is getting a bogus
770 // focus and duly ignore it.
771 // TODO: may need to have this code in SetFocus, too.
772 extern wxWindow
* g_GettingFocus
;
773 g_GettingFocus
= win
;
779 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
781 // Throw out NotifyGrab and NotifyUngrab
782 if (event
->xcrossing
.mode
!= NotifyNormal
)
786 wxMouseEvent wxevent
;
787 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
788 return win
->GetEventHandler()->ProcessEvent( wxevent
);
793 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
794 (event
->xfocus
.mode
== NotifyNormal
))
797 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
799 extern wxWindow
* g_GettingFocus
;
800 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
802 // Ignore this, this can be a spurious FocusIn
803 // caused by a child having its focus set.
804 g_GettingFocus
= NULL
;
805 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
810 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
811 focusEvent
.SetEventObject(win
);
812 focusEvent
.SetWindow( g_prevFocus
);
815 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
824 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
825 (event
->xfocus
.mode
== NotifyNormal
))
828 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
830 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
831 focusEvent
.SetEventObject(win
);
832 focusEvent
.SetWindow( g_nextFocus
);
834 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
842 //wxString eventName = wxGetXEventName(XEvent& event);
843 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
852 // Returns TRUE if more time is needed.
853 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
854 // but ProcessIdle may be needed by apps, so is kept.
855 bool wxApp::ProcessIdle()
858 event
.SetEventObject(this);
861 return event
.MoreRequested();
864 void wxApp::ExitMainLoop()
870 // Is a message/event pending?
871 bool wxApp::Pending()
873 return wxEventLoop::GetActive()->Pending();
876 // Dispatch a message.
877 void wxApp::Dispatch()
879 wxEventLoop::GetActive()->Dispatch();
882 // This should be redefined in a derived class for
883 // handling property change events for XAtom IPC.
884 bool wxApp::HandlePropertyChange(WXEvent
*event
)
886 // by default do nothing special
887 // TODO: what to do for X11
888 // XtDispatchEvent((XEvent*) event);
892 void wxApp::OnIdle(wxIdleEvent
& event
)
894 static bool s_inOnIdle
= FALSE
;
896 // Avoid recursion (via ProcessEvent default case)
902 // Resend in the main thread events which have been prepared in other
904 ProcessPendingEvents();
906 // 'Garbage' collection of windows deleted with Close()
907 DeletePendingObjects();
909 // Send OnIdle events to all windows
910 bool needMore
= SendIdleEvents();
913 event
.RequestMore(TRUE
);
920 // **** please implement me! ****
921 // Wake up the idle handler processor, even if it is in another thread...
925 // Send idle event to all top-level windows
926 bool wxApp::SendIdleEvents()
928 bool needMore
= FALSE
;
930 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
933 wxWindow
* win
= node
->GetData();
934 if (SendIdleEvents(win
))
936 node
= node
->GetNext();
942 // Send idle event to window and all subwindows
943 bool wxApp::SendIdleEvents(wxWindow
* win
)
945 bool needMore
= FALSE
;
948 event
.SetEventObject(win
);
950 win
->GetEventHandler()->ProcessEvent(event
);
952 if (event
.MoreRequested())
955 wxNode
* node
= win
->GetChildren().First();
958 wxWindow
* win
= (wxWindow
*) node
->Data();
959 if (SendIdleEvents(win
))
965 win
->OnInternalIdle();
970 void wxApp::DeletePendingObjects()
972 wxNode
*node
= wxPendingDelete
.First();
975 wxObject
*obj
= (wxObject
*)node
->Data();
979 if (wxPendingDelete
.Member(obj
))
982 // Deleting one object may have deleted other pending
983 // objects, so start from beginning of list again.
984 node
= wxPendingDelete
.First();
988 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
993 while (!(mask
& 0x1))
1006 // Create display, and other initialization
1007 bool wxApp::OnInitGui()
1009 // Eventually this line will be removed, but for
1010 // now we don't want to try popping up a dialog
1011 // for error messages.
1012 delete wxLog::SetActiveTarget(new wxLogStderr
);
1014 if (!wxAppBase::OnInitGui())
1017 GetMainColormap( wxApp::GetDisplay() );
1019 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
1022 // Get info about the current visual. It is enough
1023 // to do this once here unless we support different
1024 // visuals, displays and screens. Given that wxX11
1025 // mostly for embedded things, that is no real
1027 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
1028 int xscreen
= DefaultScreen(xdisplay
);
1029 Visual
* xvisual
= DefaultVisual(xdisplay
,xscreen
);
1030 int xdepth
= DefaultDepth(xdisplay
, xscreen
);
1032 XVisualInfo vinfo_template
;
1033 vinfo_template
.visual
= xvisual
;
1034 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
1035 vinfo_template
.depth
= xdepth
;
1038 XVisualInfo
*vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1039 wxASSERT_MSG( vi
, wxT("No visual info") );
1041 m_visualType
= vi
->visual
->c_class
;
1042 m_visualScreen
= vi
->screen
;
1044 m_visualRedMask
= vi
->red_mask
;
1045 m_visualGreenMask
= vi
->green_mask
;
1046 m_visualBlueMask
= vi
->blue_mask
;
1048 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
1050 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
, &m_visualRedPrec
);
1051 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
, &m_visualGreenPrec
);
1052 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
, &m_visualBluePrec
);
1055 m_visualDepth
= xdepth
;
1057 xdepth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
1059 m_visualColormapSize
= vi
->colormap_size
;
1063 if (m_visualDepth
> 8)
1066 m_visualColormap
= new XColor
[m_visualColormapSize
];
1067 XColor
* colors
= (XColor
*) m_visualColormap
;
1069 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1070 colors
[i
].pixel
= i
;
1072 XQueryColors( xdisplay
, DefaultColormap(xdisplay
,xscreen
), colors
, m_visualColormapSize
);
1074 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
1076 for (int r
= 0; r
< 32; r
++)
1078 for (int g
= 0; g
< 32; g
++)
1080 for (int b
= 0; b
< 32; b
++)
1082 int rr
= (r
<< 3) | (r
>> 2);
1083 int gg
= (g
<< 3) | (g
>> 2);
1084 int bb
= (b
<< 3) | (b
>> 2);
1090 int max
= 3 * 65536;
1092 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1094 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
1095 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
1096 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
1097 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1100 index
= i
; max
= sum
;
1106 // assume 8-bit true or static colors. this really exists
1107 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
1108 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
1109 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
1111 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
1120 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
1122 if (!display
) /* Must be called first with non-NULL display */
1123 return m_mainColormap
;
1125 int defaultScreen
= DefaultScreen((Display
*) display
);
1126 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
1128 Colormap c
= DefaultColormapOfScreen(screen
);
1130 if (!m_mainColormap
)
1131 m_mainColormap
= (WXColormap
) c
;
1133 return (WXColormap
) c
;
1136 Window
wxGetWindowParent(Window window
)
1138 wxASSERT_MSG( window
, "invalid window" );
1142 Window parent
, root
= 0;
1146 unsigned int noChildren
= 0;
1148 Window
* children
= NULL
;
1150 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
1155 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
1156 & children
, & noChildren
);
1169 retValue
= wxTheApp
->OnExit();
1173 * Exit in some platform-specific way. Not recommended that the app calls this:
1174 * only for emergencies.
1179 // Yield to other processes
1181 bool wxApp::Yield(bool onlyIfNeeded
)
1183 bool s_inYield
= FALSE
;
1187 if ( !onlyIfNeeded
)
1189 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1197 // Make sure we have an event loop object,
1198 // or Pending/Dispatch will fail
1199 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
1200 wxEventLoop
* newEventLoop
= NULL
;
1203 newEventLoop
= new wxEventLoop
;
1204 wxEventLoop::SetActive(newEventLoop
);
1207 while (wxTheApp
&& wxTheApp
->Pending())
1208 wxTheApp
->Dispatch();
1211 wxTimer::NotifyTimers();
1217 wxEventLoop::SetActive(NULL
);
1218 delete newEventLoop
;
1228 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
1230 // While the GUI isn't working that well, just print out the
1233 wxAppBase::OnAssert(file
, line
, cond
, msg
);
1236 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
1241 #endif // __WXDEBUG__