1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "app.h"
15 #include <vms_jackets.h>
19 #include "wx/gdicmn.h"
23 #include "wx/memory.h"
25 #include "wx/settings.h"
26 #include "wx/dialog.h"
28 #if wxUSE_WX_RESOURCES
29 #include "wx/resource.h"
32 #include "wx/module.h"
35 #ifdef __WXUNIVERSAL__
36 #include "wx/univ/theme.h"
37 #include "wx/univ/renderer.h"
41 #include "wx/thread.h"
45 #if defined(__DARWIN__)
46 // FIXME: select must be used instead of poll (GD)
50 # include <sys/poll.h>
52 #include "wx/gtk/win_gtk.h"
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 wxApp
*wxTheApp
= (wxApp
*) NULL
;
62 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
64 bool g_mainThreadLocked
= FALSE
;
65 gint g_pendingTag
= 0;
67 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
75 void wxapp_install_idle_handler();
77 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
90 // not static because used by textctrl.cpp
93 bool wxIsInsideYield
= FALSE
;
95 bool wxApp::Yield(bool onlyIfNeeded
)
97 if ( wxIsInsideYield
)
101 wxFAIL_MSG( wxT("wxYield called recursively" ) );
108 if ( !wxThread::IsMain() )
110 // can't call gtk_main_iteration() from other threads like this
113 #endif // wxUSE_THREADS
115 wxIsInsideYield
= TRUE
;
119 // We need to remove idle callbacks or the loop will
121 gtk_idle_remove( m_idleTag
);
126 // disable log flushing from here because a call to wxYield() shouldn't
127 // normally result in message boxes popping up &c
130 while (gtk_events_pending())
131 gtk_main_iteration();
133 // It's necessary to call ProcessIdle() to update the frames sizes which
134 // might have been changed (it also will update other things set from
135 // OnUpdateUI() which is a nice (and desired) side effect). But we
136 // call ProcessIdle() only once since this is not meant for longish
137 // background jobs (controlled by wxIdleEvent::RequestMore() and the
138 // return value of Processidle().
141 // let the logs be flashed again
144 wxIsInsideYield
= FALSE
;
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
156 if (!wxThread::IsMain())
161 wxapp_install_idle_handler();
164 if (!wxThread::IsMain())
169 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
173 // the callback functions must be extern "C" to comply with GTK+ declarations
177 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
179 if (!wxTheApp
) return TRUE
;
181 // When getting called from GDK's time-out handler
182 // we are no longer within GDK's grab on the GUI
183 // thread so we must lock it here ourselves.
186 // Sent idle event to all who request them.
187 wxTheApp
->ProcessPendingEvents();
191 // Flush the logged messages if any.
193 wxLog::FlushActive();
196 // Release lock again
199 // Return FALSE to indicate that no more idle events are
200 // to be sent (single shot instead of continuous stream)
204 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
210 // don't generate the idle events while the assert modal dialog is shown,
211 // this completely confuses the apps which don't expect to be reentered
212 // from some safely-looking functions
213 if ( wxTheApp
->IsInAssert() )
217 #endif // __WXDEBUG__
219 // When getting called from GDK's time-out handler
220 // we are no longer within GDK's grab on the GUI
221 // thread so we must lock it here ourselves.
224 // Indicate that we are now in idle mode and event handlers
225 // will have to reinstall the idle handler again.
227 wxTheApp
->m_idleTag
= 0;
229 // Send idle event to all who request them as long as
230 // no events have popped up in the event queue.
231 while (wxTheApp
->ProcessIdle() && (gtk_events_pending() == 0))
234 // Release lock again
237 // Return FALSE to indicate that no more idle events are
238 // to be sent (single shot instead of continuous stream).
244 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
250 g_mainThreadLocked
= TRUE
;
253 // FIXME: poll is not available under Darwin/Mac OS X and this needs
254 // to be implemented using select instead (GD)
255 // what about other BSD derived systems?
258 res
= poll( (struct pollfd
*) ufds
, nfds
, timeout
);
262 g_mainThreadLocked
= FALSE
;
269 #endif // wxUSE_THREADS
273 void wxapp_install_idle_handler()
275 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
279 if (g_pendingTag
== 0)
280 g_pendingTag
= gtk_idle_add_priority( 900, wxapp_pending_callback
, (gpointer
) NULL
);
282 // This routine gets called by all event handlers
283 // indicating that the idle is over. It may also
284 // get called from other thread for sending events
285 // to the main thread (and processing these in
286 // idle time). Very low priority.
287 wxTheApp
->m_idleTag
= gtk_idle_add_priority( 1000, wxapp_idle_callback
, (gpointer
) NULL
);
290 //-----------------------------------------------------------------------------
291 // Access to the root window global
292 //-----------------------------------------------------------------------------
294 GtkWidget
* wxGetRootWindow()
296 if (gs_RootWindow
== NULL
)
298 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
299 gtk_widget_realize( gs_RootWindow
);
301 return gs_RootWindow
;
304 //-----------------------------------------------------------------------------
306 //-----------------------------------------------------------------------------
308 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
310 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
311 EVT_IDLE(wxApp::OnIdle
)
316 m_initialized
= FALSE
;
318 m_isInAssert
= FALSE
;
319 #endif // __WXDEBUG__
322 wxapp_install_idle_handler();
325 g_main_set_poll_func( wxapp_poll_func
);
328 m_colorCube
= (unsigned char*) NULL
;
330 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
331 m_glVisualInfo
= (void *) NULL
;
336 if (m_idleTag
) gtk_idle_remove( m_idleTag
);
338 if (m_colorCube
) free(m_colorCube
);
341 bool wxApp::OnInitGui()
343 if ( !wxAppBase::OnInitGui() )
346 GdkVisual
*visual
= gdk_visual_get_system();
348 // if this is a wxGLApp (derived from wxApp), and we've already
349 // chosen a specific visual, then derive the GdkVisual from that
350 if (m_glVisualInfo
!= NULL
)
353 // seems gtk_widget_set_default_visual no longer exists?
354 GdkVisual
* vis
= gtk_widget_get_default_visual();
356 GdkVisual
* vis
= gdkx_visual_get(
357 ((XVisualInfo
*) m_glVisualInfo
) ->visualid
);
358 gtk_widget_set_default_visual( vis
);
361 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
362 gtk_widget_set_default_colormap( colormap
);
367 // On some machines, the default visual is just 256 colours, so
368 // we make sure we get the best. This can sometimes be wasteful.
371 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
374 /* seems gtk_widget_set_default_visual no longer exists? */
375 GdkVisual
* vis
= gtk_widget_get_default_visual();
377 GdkVisual
* vis
= gdk_visual_get_best();
378 gtk_widget_set_default_visual( vis
);
381 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
382 gtk_widget_set_default_colormap( colormap
);
387 // Nothing to do for 15, 16, 24, 32 bit displays
388 if (visual
->depth
> 8) return TRUE
;
390 // initialize color cube for 8-bit color reduction dithering
392 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
394 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
396 for (int r
= 0; r
< 32; r
++)
398 for (int g
= 0; g
< 32; g
++)
400 for (int b
= 0; b
< 32; b
++)
402 int rr
= (r
<< 3) | (r
>> 2);
403 int gg
= (g
<< 3) | (g
>> 2);
404 int bb
= (b
<< 3) | (b
>> 2);
408 GdkColor
*colors
= cmap
->colors
;
413 for (int i
= 0; i
< cmap
->size
; i
++)
415 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
416 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
417 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
418 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
421 index
= i
; max
= sum
;
427 // assume 8-bit true or static colors. this really exists
428 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
429 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
430 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
431 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
433 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
441 GdkVisual
*wxApp::GetGdkVisual()
443 GdkVisual
*visual
= NULL
;
446 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
448 visual
= gdk_window_get_visual( wxGetRootWindow()->window
);
455 bool wxApp::ProcessIdle()
458 event
.SetEventObject( this );
459 ProcessEvent( event
);
461 return event
.MoreRequested();
464 void wxApp::OnIdle( wxIdleEvent
&event
)
466 static bool s_inOnIdle
= FALSE
;
468 // Avoid recursion (via ProcessEvent default case)
474 // Resend in the main thread events which have been prepared in other
476 ProcessPendingEvents();
478 // 'Garbage' collection of windows deleted with Close()
479 DeletePendingObjects();
481 // Send OnIdle events to all windows
482 bool needMore
= SendIdleEvents();
485 event
.RequestMore(TRUE
);
490 bool wxApp::SendIdleEvents()
492 bool needMore
= FALSE
;
494 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
497 wxWindow
* win
= node
->GetData();
498 if (SendIdleEvents(win
))
501 node
= node
->GetNext();
504 node
= wxTopLevelWindows
.GetFirst();
507 wxWindow
* win
= node
->GetData();
508 CallInternalIdle( win
);
510 node
= node
->GetNext();
515 bool wxApp::CallInternalIdle( wxWindow
* win
)
517 win
->OnInternalIdle();
519 wxNode
* node
= win
->GetChildren().First();
522 wxWindow
* win
= (wxWindow
*) node
->Data();
523 CallInternalIdle( win
);
531 bool wxApp::SendIdleEvents( wxWindow
* win
)
533 bool needMore
= FALSE
;
536 event
.SetEventObject(win
);
538 win
->GetEventHandler()->ProcessEvent(event
);
540 if (event
.MoreRequested())
543 wxNode
* node
= win
->GetChildren().First();
546 wxWindow
* win
= (wxWindow
*) node
->Data();
547 if (SendIdleEvents(win
))
556 int wxApp::MainLoop()
562 void wxApp::ExitMainLoop()
564 if (gtk_main_level() > 0)
568 bool wxApp::Initialized()
570 return m_initialized
;
573 bool wxApp::Pending()
575 return (gtk_events_pending() > 0);
578 void wxApp::Dispatch()
580 gtk_main_iteration();
583 void wxApp::DeletePendingObjects()
585 wxNode
*node
= wxPendingDelete
.First();
588 wxObject
*obj
= (wxObject
*)node
->Data();
592 if (wxPendingDelete
.Find(obj
))
595 node
= wxPendingDelete
.First();
599 bool wxApp::Initialize()
601 wxClassInfo::InitializeClasses();
604 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
607 // GL: I'm annoyed ... I don't know where to put this and I don't want to
608 // create a module for that as it's part of the core.
610 wxPendingEvents
= new wxList();
611 wxPendingEventsLocker
= new wxCriticalSection();
614 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
615 wxTheColourDatabase
->Initialize();
617 wxInitializeStockLists();
618 wxInitializeStockObjects();
620 #if wxUSE_WX_RESOURCES
621 wxInitializeResourceSystem();
624 wxModule::RegisterModules();
625 if (!wxModule::InitializeModules()) return FALSE
;
630 void wxApp::CleanUp()
632 wxModule::CleanUpModules();
634 #if wxUSE_WX_RESOURCES
635 wxCleanUpResourceSystem();
638 delete wxTheColourDatabase
;
639 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
641 wxDeleteStockObjects();
643 wxDeleteStockLists();
646 wxTheApp
= (wxApp
*) NULL
;
648 wxClassInfo::CleanUpClasses();
651 delete wxPendingEvents
;
652 delete wxPendingEventsLocker
;
655 // check for memory leaks
656 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
657 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
659 wxLogDebug(wxT("There were memory leaks.\n"));
660 wxDebugContext::Dump();
661 wxDebugContext::PrintStatistics();
666 // do this as the very last thing because everything else can log messages
667 wxLog::DontCreateOnDemand();
669 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
675 //-----------------------------------------------------------------------------
677 //-----------------------------------------------------------------------------
679 // NB: argc and argv may be changed here, pass by reference!
680 int wxEntryStart( int& argc
, char *argv
[] )
683 // GTK 1.2 up to version 1.2.3 has broken threads
684 if ((gtk_major_version
== 1) &&
685 (gtk_minor_version
== 2) &&
686 (gtk_micro_version
< 4))
688 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
698 // We should have the wxUSE_WCHAR_T test on the _outside_
700 #if defined(__WXGTK20__)
701 // gtk+ 2.0 supports Unicode through UTF-8 strings
702 wxConvCurrent
= &wxConvUTF8
;
704 if (!wxOKlibc()) wxConvCurrent
= &wxConvLocal
;
707 if (!wxOKlibc()) wxConvCurrent
= (wxMBConv
*) NULL
;
712 gtk_init( &argc
, &argv
);
714 wxSetDetectableAutoRepeat( TRUE
);
716 if (!wxApp::Initialize())
730 if ( !wxTheApp
->OnInitGui() )
739 void wxEntryCleanup()
742 // flush the logged messages if any
743 wxLog
*log
= wxLog::GetActiveTarget();
744 if (log
!= NULL
&& log
->HasPendingMessages())
747 // continuing to use user defined log target is unsafe from now on because
748 // some resources may be already unavailable, so replace it by something
750 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
761 int wxEntry( int argc
, char *argv
[] )
763 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
764 // This seems to be necessary since there are 'rogue'
765 // objects present at this point (perhaps global objects?)
766 // Setting a checkpoint will ignore them as far as the
767 // memory checking facility is concerned.
768 // Of course you may argue that memory allocated in globals should be
769 // checked, but this is a reasonable compromise.
770 wxDebugContext::SetCheckpoint();
772 int err
= wxEntryStart(argc
, argv
);
778 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
779 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
781 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
783 wxObject
*test_app
= app_ini();
785 wxTheApp
= (wxApp
*) test_app
;
788 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
790 wxTheApp
->argc
= argc
;
792 wxTheApp
->argv
= new wxChar
*[argc
+1];
794 while (mb_argc
< argc
)
796 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
799 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
801 wxTheApp
->argv
= argv
;
804 wxString
name(wxFileNameFromPath(argv
[0]));
805 wxStripExtension( name
);
806 wxTheApp
->SetAppName( name
);
809 retValue
= wxEntryInitGui();
811 // Here frames insert themselves automatically into wxTopLevelWindows by
812 // getting created in OnInit().
815 if ( !wxTheApp
->OnInit() )
821 // Delete pending toplevel windows
822 wxTheApp
->DeletePendingObjects();
824 // When is the app not initialized ?
825 wxTheApp
->m_initialized
= TRUE
;
827 if (wxTheApp
->Initialized())
831 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
833 // Delete all pending windows if any
834 wxTheApp
->DeletePendingObjects();
838 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
840 retValue
= wxTheApp
->OnExit();
851 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
855 wxAppBase::OnAssert(file
, line
, cond
, msg
);
857 m_isInAssert
= FALSE
;
860 #endif // __WXDEBUG__