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();
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 #if wxUSE_WX_RESOURCES
121 wxInitializeResourceSystem();
124 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
125 wxClientWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
127 wxModule::RegisterModules();
128 if (!wxModule::InitializeModules()) return FALSE
;
133 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 wxString displayName
;
197 bool syncDisplay
= FALSE
;
199 // Parse the arguments.
200 // We can't use wxCmdLineParser or OnInitCmdLine and friends because
201 // we have to create the Display earlier. If we can find a way to
202 // use the wxAppBase API then I'll be quite happy to change it.
203 g_newArgv
= new wxChar
*[argc
];
206 for (i
= 0; i
< argc
; i
++)
208 wxString
arg(argv
[i
]);
209 if (arg
== wxT("-display"))
214 displayName
= argv
[i
];
218 else if (arg
== wxT("-geometry"))
223 wxString windowGeometry
= argv
[i
];
225 if (wxSscanf(windowGeometry
.c_str(), _T("%dx%d"), &w
, &h
) != 2)
227 wxLogError(_("Invalid geometry specification '%s'"), windowGeometry
.c_str());
231 g_initialSize
= wxSize(w
, h
);
236 else if (arg
== wxT("-sync"))
241 else if (arg
== wxT("-iconic"))
248 // Not eaten by wxWindows, so pass through
249 g_newArgv
[g_newArgc
] = argv
[i
];
253 Display
* xdisplay
= NULL
;
254 if (displayName
.IsEmpty())
255 xdisplay
= XOpenDisplay(NULL
);
257 xdisplay
= XOpenDisplay((char*) displayName
.c_str());
261 wxLogError( _("wxWindows could not open display. Exiting.") );
267 XSynchronize(xdisplay
, True
);
270 wxApp::ms_display
= (WXDisplay
*) xdisplay
;
272 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
274 wxSetDetectableAutoRepeat( TRUE
);
276 if (!wxApp::Initialize())
286 if ( !wxTheApp
->OnInitGui() )
293 int wxEntry( int argc
, char *argv
[] )
295 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
296 // This seems to be necessary since there are 'rogue'
297 // objects present at this point (perhaps global objects?)
298 // Setting a checkpoint will ignore them as far as the
299 // memory checking facility is concerned.
300 // Of course you may argue that memory allocated in globals should be
301 // checked, but this is a reasonable compromise.
302 wxDebugContext::SetCheckpoint();
304 int err
= wxEntryStart(argc
, argv
);
310 if (!wxApp::GetInitializerFunction())
312 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
316 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
321 printf( "wxWindows error: wxTheApp == NULL\n" );
325 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
326 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
328 // The command line may have been changed
329 // by stripping out -display etc.
332 wxTheApp
->argc
= g_newArgc
;
333 wxTheApp
->argv
= g_newArgv
;
337 wxTheApp
->argc
= argc
;
338 wxTheApp
->argv
= argv
;
340 wxTheApp
->m_showIconic
= g_showIconic
;
341 wxTheApp
->m_initialSize
= g_initialSize
;
344 retValue
= wxEntryInitGui();
346 // Here frames insert themselves automatically into wxTopLevelWindows by
347 // getting created in OnInit().
350 if ( !wxTheApp
->OnInit() )
356 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
359 // flush the logged messages if any
360 wxLog
*pLog
= wxLog::GetActiveTarget();
361 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
364 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
365 // for further messages
367 if (wxTheApp
->GetTopWindow())
369 delete wxTheApp
->GetTopWindow();
370 wxTheApp
->SetTopWindow(NULL
);
373 wxTheApp
->DeletePendingObjects();
382 // Static member initialization
383 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
390 m_wantDebugOutput
= TRUE
;
394 m_exitOnFrameDelete
= TRUE
;
395 m_mainColormap
= (WXColormap
) NULL
;
396 m_topLevelWidget
= (WXWindow
) NULL
;
397 m_maxRequestSize
= 0;
399 m_showIconic
= FALSE
;
400 m_initialSize
= wxDefaultSize
;
403 m_visualColormap
= NULL
;
414 if (m_visualColormap
)
415 delete [] (XColor
*)m_visualColormap
;
419 bool wxApp::Initialized()
427 int wxApp::MainLoop()
430 m_mainLoop
= new wxEventLoop
;
432 rt
= m_mainLoop
->Run();
440 //-----------------------------------------------------------------------
441 // X11 predicate function for exposure compression
442 //-----------------------------------------------------------------------
447 Bool found_non_matching
;
450 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
452 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
454 if (info
->found_non_matching
)
457 if (xevent
->xany
.type
!= Expose
)
459 info
->found_non_matching
= TRUE
;
463 if (xevent
->xexpose
.window
!= info
->window
)
465 info
->found_non_matching
= TRUE
;
474 //-----------------------------------------------------------------------
475 // Processes an X event, returning TRUE if the event was processed.
476 //-----------------------------------------------------------------------
478 bool wxApp::ProcessXEvent(WXEvent
* _event
)
480 XEvent
* event
= (XEvent
*) _event
;
482 wxWindow
* win
= NULL
;
483 Window window
= XEventGetWindow(event
);
485 Window actualWindow
= window
;
488 // Find the first wxWindow that corresponds to this event window
489 // Because we're receiving events after a window
490 // has been destroyed, assume a 1:1 match between
491 // Window and wxWindow, so if it's not in the table,
492 // it must have been destroyed.
494 win
= wxGetWindowFromTable(window
);
497 #if wxUSE_TWO_WINDOWS
498 win
= wxGetClientWindowFromTable(window
);
505 wxString windowClass
= win
->GetClassInfo()->GetClassName();
512 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
513 if (event
->xexpose
.window
!= (Window
)win
->GetClientWindow())
517 info
.window
= event
->xexpose
.window
;
518 info
.found_non_matching
= FALSE
;
519 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
521 // Don't worry about optimizing redrawing the border etc.
523 win
->NeedUpdateNcAreaInIdle();
528 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
529 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
530 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
531 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
536 info
.window
= event
->xexpose
.window
;
537 info
.found_non_matching
= FALSE
;
538 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
540 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
541 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
543 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
544 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
548 // This simplifies the expose and clear areas to simple
550 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
551 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
553 // If we only have one X11 window, always indicate
554 // that borders might have to be redrawn.
555 if (win
->GetMainWindow() == win
->GetClientWindow())
556 win
->NeedUpdateNcAreaInIdle();
558 // Only erase background, paint in idle time.
559 win
->SendEraseEvents();
569 printf( "GraphicExpose event\n" );
571 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str(),
572 event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
573 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
575 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
576 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
578 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
579 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
581 if (event
->xgraphicsexpose
.count
== 0)
583 // Only erase background, paint in idle time.
584 win
->SendEraseEvents();
594 if (!win
->IsEnabled())
597 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
598 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
600 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
602 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
603 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
606 keyEvent
.SetEventType(wxEVT_CHAR
);
607 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
610 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
611 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
613 wxNavigationKeyEvent new_event
;
614 new_event
.SetEventObject( win
->GetParent() );
615 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
616 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
617 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
618 new_event
.SetWindowChange( keyEvent
.ControlDown() );
619 new_event
.SetCurrentFocus( win
);
620 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
627 if (!win
->IsEnabled())
630 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
631 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
633 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
635 case ConfigureNotify
:
638 if (event
->update
.utype
== GR_UPDATE_SIZE
)
641 if (win
->IsTopLevel())
643 wxTopLevelWindow
*tlw
= (wxTopLevelWindow
*) win
;
644 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
645 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
648 if (win
->IsTopLevel() && win
->IsShown())
650 wxTopLevelWindowX11
*tlw
= (wxTopLevelWindowX11
*) win
;
651 tlw
->SetNeedResizeInIdle();
655 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
656 sizeEvent
.SetEventObject( win
);
658 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
667 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
668 return HandlePropertyChange(_event
);
672 if (!win
->IsEnabled())
675 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
676 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
678 if (event
->xclient
.message_type
== wm_protocols
)
680 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
691 printf( "destroy from %s\n", win
->GetName().c_str() );
696 printf( "create from %s\n", win
->GetName().c_str() );
701 printf( "map request from %s\n", win
->GetName().c_str() );
706 printf( "resize request from %s\n", win
->GetName().c_str() );
708 Display
*disp
= (Display
*) wxGetDisplay();
713 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
715 wxSize sz
= win
->GetSize();
716 wxSizeEvent
sizeEvent(sz
, win
->GetId());
717 sizeEvent
.SetEventObject(win
);
719 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
724 case GR_EVENT_TYPE_CLOSE_REQ
:
741 if (!win
->IsEnabled())
744 // Here we check if the top level window is
745 // disabled, which is one aspect of modality.
747 while (tlw
&& !tlw
->IsTopLevel())
748 tlw
= tlw
->GetParent();
749 if (tlw
&& !tlw
->IsEnabled())
752 if (event
->type
== ButtonPress
)
754 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
756 // This might actually be done in wxWindow::SetFocus()
757 // and not here. TODO.
758 g_prevFocus
= wxWindow::FindFocus();
761 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
763 // Record the fact that this window is
764 // getting the focus, because we'll need to
765 // check if its parent is getting a bogus
766 // focus and duly ignore it.
767 // TODO: may need to have this code in SetFocus, too.
768 extern wxWindow
* g_GettingFocus
;
769 g_GettingFocus
= win
;
775 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
777 // Throw out NotifyGrab and NotifyUngrab
778 if (event
->xcrossing
.mode
!= NotifyNormal
)
782 wxMouseEvent wxevent
;
783 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
784 return win
->GetEventHandler()->ProcessEvent( wxevent
);
789 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
790 (event
->xfocus
.mode
== NotifyNormal
))
793 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
795 extern wxWindow
* g_GettingFocus
;
796 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
798 // Ignore this, this can be a spurious FocusIn
799 // caused by a child having its focus set.
800 g_GettingFocus
= NULL
;
801 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
806 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
807 focusEvent
.SetEventObject(win
);
808 focusEvent
.SetWindow( g_prevFocus
);
811 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
820 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
821 (event
->xfocus
.mode
== NotifyNormal
))
824 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
826 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
827 focusEvent
.SetEventObject(win
);
828 focusEvent
.SetWindow( g_nextFocus
);
830 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
838 //wxString eventName = wxGetXEventName(XEvent& event);
839 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
848 // Returns TRUE if more time is needed.
849 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
850 // but ProcessIdle may be needed by apps, so is kept.
851 bool wxApp::ProcessIdle()
854 event
.SetEventObject(this);
857 return event
.MoreRequested();
860 void wxApp::ExitMainLoop()
866 // Is a message/event pending?
867 bool wxApp::Pending()
869 return wxEventLoop::GetActive()->Pending();
872 // Dispatch a message.
873 void wxApp::Dispatch()
875 wxEventLoop::GetActive()->Dispatch();
878 // This should be redefined in a derived class for
879 // handling property change events for XAtom IPC.
880 bool wxApp::HandlePropertyChange(WXEvent
*event
)
882 // by default do nothing special
883 // TODO: what to do for X11
884 // XtDispatchEvent((XEvent*) event);
888 void wxApp::OnIdle(wxIdleEvent
& event
)
890 static bool s_inOnIdle
= FALSE
;
892 // Avoid recursion (via ProcessEvent default case)
898 // Resend in the main thread events which have been prepared in other
900 ProcessPendingEvents();
902 // 'Garbage' collection of windows deleted with Close()
903 DeletePendingObjects();
905 // Send OnIdle events to all windows
906 bool needMore
= SendIdleEvents();
909 event
.RequestMore(TRUE
);
916 // **** please implement me! ****
917 // Wake up the idle handler processor, even if it is in another thread...
921 // Send idle event to all top-level windows
922 bool wxApp::SendIdleEvents()
924 bool needMore
= FALSE
;
926 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
929 wxWindow
* win
= node
->GetData();
930 if (SendIdleEvents(win
))
932 node
= node
->GetNext();
938 // Send idle event to window and all subwindows
939 bool wxApp::SendIdleEvents(wxWindow
* win
)
941 bool needMore
= FALSE
;
944 event
.SetEventObject(win
);
946 win
->GetEventHandler()->ProcessEvent(event
);
948 if (event
.MoreRequested())
951 wxNode
* node
= win
->GetChildren().First();
954 wxWindow
* win
= (wxWindow
*) node
->Data();
955 if (SendIdleEvents(win
))
961 win
->OnInternalIdle();
966 void wxApp::DeletePendingObjects()
968 wxNode
*node
= wxPendingDelete
.First();
971 wxObject
*obj
= (wxObject
*)node
->Data();
975 if (wxPendingDelete
.Member(obj
))
978 // Deleting one object may have deleted other pending
979 // objects, so start from beginning of list again.
980 node
= wxPendingDelete
.First();
984 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
989 while (!(mask
& 0x1))
1002 // Create display, and other initialization
1003 bool wxApp::OnInitGui()
1005 // Eventually this line will be removed, but for
1006 // now we don't want to try popping up a dialog
1007 // for error messages.
1008 delete wxLog::SetActiveTarget(new wxLogStderr
);
1010 if (!wxAppBase::OnInitGui())
1013 GetMainColormap( wxApp::GetDisplay() );
1015 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
1018 // Get info about the current visual. It is enough
1019 // to do this once here unless we support different
1020 // visuals, displays and screens. Given that wxX11
1021 // mostly for embedded things, that is no real
1023 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
1024 int xscreen
= DefaultScreen(xdisplay
);
1025 Visual
* xvisual
= DefaultVisual(xdisplay
,xscreen
);
1026 int xdepth
= DefaultDepth(xdisplay
, xscreen
);
1028 XVisualInfo vinfo_template
;
1029 vinfo_template
.visual
= xvisual
;
1030 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
1031 vinfo_template
.depth
= xdepth
;
1034 XVisualInfo
*vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1035 wxASSERT_MSG( vi
, wxT("No visual info") );
1037 m_visualType
= vi
->visual
->c_class
;
1038 m_visualScreen
= vi
->screen
;
1040 m_visualRedMask
= vi
->red_mask
;
1041 m_visualGreenMask
= vi
->green_mask
;
1042 m_visualBlueMask
= vi
->blue_mask
;
1044 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
1046 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
, &m_visualRedPrec
);
1047 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
, &m_visualGreenPrec
);
1048 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
, &m_visualBluePrec
);
1051 m_visualDepth
= xdepth
;
1053 xdepth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
1055 m_visualColormapSize
= vi
->colormap_size
;
1059 if (m_visualDepth
> 8)
1062 m_visualColormap
= new XColor
[m_visualColormapSize
];
1063 XColor
* colors
= (XColor
*) m_visualColormap
;
1065 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1066 colors
[i
].pixel
= i
;
1068 XQueryColors( xdisplay
, DefaultColormap(xdisplay
,xscreen
), colors
, m_visualColormapSize
);
1070 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
1072 for (int r
= 0; r
< 32; r
++)
1074 for (int g
= 0; g
< 32; g
++)
1076 for (int b
= 0; b
< 32; b
++)
1078 int rr
= (r
<< 3) | (r
>> 2);
1079 int gg
= (g
<< 3) | (g
>> 2);
1080 int bb
= (b
<< 3) | (b
>> 2);
1086 int max
= 3 * 65536;
1088 for (int i
= 0; i
< m_visualColormapSize
; i
++)
1090 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
1091 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
1092 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
1093 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1096 index
= i
; max
= sum
;
1102 // assume 8-bit true or static colors. this really exists
1103 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
1104 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
1105 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
1107 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
1116 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
1118 if (!display
) /* Must be called first with non-NULL display */
1119 return m_mainColormap
;
1121 int defaultScreen
= DefaultScreen((Display
*) display
);
1122 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
1124 Colormap c
= DefaultColormapOfScreen(screen
);
1126 if (!m_mainColormap
)
1127 m_mainColormap
= (WXColormap
) c
;
1129 return (WXColormap
) c
;
1132 Window
wxGetWindowParent(Window window
)
1134 wxASSERT_MSG( window
, "invalid window" );
1138 Window parent
, root
= 0;
1142 unsigned int noChildren
= 0;
1144 Window
* children
= NULL
;
1146 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
1151 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
1152 & children
, & noChildren
);
1165 retValue
= wxTheApp
->OnExit();
1169 * Exit in some platform-specific way. Not recommended that the app calls this:
1170 * only for emergencies.
1175 // Yield to other processes
1177 bool wxApp::Yield(bool onlyIfNeeded
)
1179 bool s_inYield
= FALSE
;
1183 if ( !onlyIfNeeded
)
1185 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1193 // Make sure we have an event loop object,
1194 // or Pending/Dispatch will fail
1195 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
1196 wxEventLoop
* newEventLoop
= NULL
;
1199 newEventLoop
= new wxEventLoop
;
1200 wxEventLoop::SetActive(newEventLoop
);
1203 while (wxTheApp
&& wxTheApp
->Pending())
1204 wxTheApp
->Dispatch();
1207 wxTimer::NotifyTimers();
1213 wxEventLoop::SetActive(NULL
);
1214 delete newEventLoop
;
1222 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
1224 // While the GUI isn't working that well, just print out the
1227 wxAppBase::OnAssert(file
, line
, msg
);
1230 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);