PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/ExtLibs/wxWidgets/src/gtk1/checkbox.cpp

https://bitbucket.org/lennonchan/cafu
C++ | 213 lines | 136 code | 48 blank | 29 comment | 20 complexity | 5b09109cf01f9be214dad7e096a1f8b5 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: src/gtk1/checkbox.cpp
  3. // Purpose:
  4. // Author: Robert Roebling
  5. // Id: $Id$
  6. // Copyright: (c) 1998 Robert Roebling
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. // For compilers that support precompilation, includes "wx.h".
  10. #include "wx/wxprec.h"
  11. #if wxUSE_CHECKBOX
  12. #include "wx/checkbox.h"
  13. #include "wx/gtk1/private.h"
  14. //-----------------------------------------------------------------------------
  15. // idle system
  16. //-----------------------------------------------------------------------------
  17. extern void wxapp_install_idle_handler();
  18. extern bool g_isIdle;
  19. //-----------------------------------------------------------------------------
  20. // data
  21. //-----------------------------------------------------------------------------
  22. extern bool g_blockEventsOnDrag;
  23. extern wxCursor g_globalCursor;
  24. extern wxWindowGTK *g_delayedFocus;
  25. //-----------------------------------------------------------------------------
  26. // "clicked"
  27. //-----------------------------------------------------------------------------
  28. extern "C" {
  29. static void gtk_checkbox_toggled_callback(GtkWidget *WXUNUSED(widget),
  30. wxCheckBox *cb)
  31. {
  32. if (g_isIdle) wxapp_install_idle_handler();
  33. if (!cb->m_hasVMT) return;
  34. if (g_blockEventsOnDrag) return;
  35. if (cb->m_blockEvent) return;
  36. wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
  37. event.SetInt(cb->GetValue());
  38. event.SetEventObject(cb);
  39. cb->HandleWindowEvent(event);
  40. }
  41. }
  42. //-----------------------------------------------------------------------------
  43. // wxCheckBox
  44. //-----------------------------------------------------------------------------
  45. wxCheckBox::wxCheckBox()
  46. {
  47. }
  48. bool wxCheckBox::Create(wxWindow *parent,
  49. wxWindowID id,
  50. const wxString &label,
  51. const wxPoint &pos,
  52. const wxSize &size,
  53. long style,
  54. const wxValidator& validator,
  55. const wxString &name )
  56. {
  57. m_needParent = true;
  58. m_acceptsFocus = true;
  59. m_blockEvent = false;
  60. WXValidateStyle(&style);
  61. if (!PreCreation( parent, pos, size ) ||
  62. !CreateBase( parent, id, pos, size, style, validator, name ))
  63. {
  64. wxFAIL_MSG( wxT("wxCheckBox creation failed") );
  65. return false;
  66. }
  67. if ( style & wxALIGN_RIGHT )
  68. {
  69. // VZ: as I don't know a way to create a right aligned checkbox with
  70. // GTK we will create a checkbox without label and a label at the
  71. // left of it
  72. m_widgetCheckbox = gtk_check_button_new();
  73. m_widgetLabel = gtk_label_new("");
  74. gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
  75. m_widget = gtk_hbox_new(FALSE, 0);
  76. gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
  77. gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
  78. gtk_widget_show( m_widgetLabel );
  79. gtk_widget_show( m_widgetCheckbox );
  80. }
  81. else
  82. {
  83. m_widgetCheckbox = gtk_check_button_new_with_label("");
  84. m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
  85. m_widget = m_widgetCheckbox;
  86. }
  87. SetLabel( label );
  88. gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
  89. "toggled",
  90. GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback),
  91. (gpointer *)this );
  92. m_parent->DoAddChild( this );
  93. PostCreation(size);
  94. return true;
  95. }
  96. void wxCheckBox::SetValue( bool state )
  97. {
  98. wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
  99. if (state == GetValue())
  100. return;
  101. m_blockEvent = true;
  102. gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
  103. m_blockEvent = false;
  104. }
  105. bool wxCheckBox::GetValue() const
  106. {
  107. wxCHECK_MSG( m_widgetCheckbox != NULL, false, wxT("invalid checkbox") );
  108. return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
  109. }
  110. void wxCheckBox::SetLabel( const wxString& label )
  111. {
  112. wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
  113. GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label);
  114. }
  115. bool wxCheckBox::Enable( bool enable )
  116. {
  117. if ( !wxControl::Enable( enable ) )
  118. return false;
  119. gtk_widget_set_sensitive( m_widgetLabel, enable );
  120. return true;
  121. }
  122. void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
  123. {
  124. gtk_widget_modify_style(m_widgetCheckbox, style);
  125. gtk_widget_modify_style(m_widgetLabel, style);
  126. }
  127. bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
  128. {
  129. return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
  130. }
  131. void wxCheckBox::OnInternalIdle()
  132. {
  133. wxCursor cursor = m_cursor;
  134. if (g_globalCursor.IsOk()) cursor = g_globalCursor;
  135. GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
  136. if ( event_window && cursor.IsOk() )
  137. {
  138. /* I now set the cursor the anew in every OnInternalIdle call
  139. as setting the cursor in a parent window also effects the
  140. windows above so that checking for the current cursor is
  141. not possible. */
  142. gdk_window_set_cursor( event_window, cursor.GetCursor() );
  143. }
  144. if (g_delayedFocus == this)
  145. {
  146. if (GTK_WIDGET_REALIZED(m_widget))
  147. {
  148. gtk_widget_grab_focus( m_widget );
  149. g_delayedFocus = NULL;
  150. }
  151. }
  152. if (wxUpdateUIEvent::CanUpdate(this))
  153. UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
  154. }
  155. wxSize wxCheckBox::DoGetBestSize() const
  156. {
  157. return wxControl::DoGetBestSize();
  158. }
  159. // static
  160. wxVisualAttributes
  161. wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
  162. {
  163. return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
  164. }
  165. #endif