timer.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: src/gtk/timer.cpp
  3. // Purpose: wxTimer implementation
  4. // Author: Robert Roebling
  5. // Copyright: (c) 1998 Robert Roebling
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. // For compilers that support precompilation, includes "wx.h".
  9. #include "wx/wxprec.h"
  10. #if wxUSE_TIMER
  11. #include "wx/gtk/private/timer.h"
  12. #include "wx/app.h"
  13. #include <gtk/gtk.h>
  14. // ----------------------------------------------------------------------------
  15. // wxTimerImpl
  16. // ----------------------------------------------------------------------------
  17. extern "C" {
  18. static gboolean timeout_callback(gpointer data)
  19. {
  20. wxGTKTimerImpl *timer = (wxGTKTimerImpl*)data;
  21. const bool keepGoing = !timer->IsOneShot();
  22. if ( !keepGoing )
  23. timer->Stop();
  24. // When getting called from GDK's timer handler we
  25. // are no longer within GDK's grab on the GUI
  26. // thread so we must lock it here ourselves.
  27. gdk_threads_enter();
  28. timer->Notify();
  29. // Release lock again.
  30. gdk_threads_leave();
  31. wxApp* app = wxTheApp;
  32. if (app)
  33. app->WakeUpIdle();
  34. return keepGoing;
  35. }
  36. } // extern "C"
  37. bool wxGTKTimerImpl::Start(int millisecs, bool oneShot)
  38. {
  39. if ( !wxTimerImpl::Start(millisecs, oneShot) )
  40. return false;
  41. wxASSERT_MSG( !m_sourceId, wxT("shouldn't be still running") );
  42. m_sourceId = g_timeout_add(m_milli, timeout_callback, this);
  43. return true;
  44. }
  45. void wxGTKTimerImpl::Stop()
  46. {
  47. wxASSERT_MSG( m_sourceId, wxT("should be running") );
  48. g_source_remove(m_sourceId);
  49. m_sourceId = 0;
  50. }
  51. #endif // wxUSE_TIMER