| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/motif/checkbox.cpp |
| 3 | // Purpose: wxCheckBox |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #include "wx/checkbox.h" |
| 16 | |
| 17 | #ifndef WX_PRECOMP |
| 18 | #include "wx/utils.h" |
| 19 | #endif |
| 20 | |
| 21 | #include "wx/tglbtn.h" |
| 22 | |
| 23 | #ifdef __VMS__ |
| 24 | #pragma message disable nosimpint |
| 25 | #endif |
| 26 | #include <Xm/Label.h> |
| 27 | #include <Xm/LabelG.h> |
| 28 | #include <Xm/ToggleB.h> |
| 29 | #include <Xm/ToggleBG.h> |
| 30 | #ifdef __VMS__ |
| 31 | #pragma message enable nosimpint |
| 32 | #endif |
| 33 | |
| 34 | #include "wx/motif/private.h" |
| 35 | |
| 36 | // define symbols that are missing in old versions of Motif. |
| 37 | #if wxCHECK_MOTIF_VERSION( 2, 0 ) |
| 38 | #define wxHAS_3STATE 1 |
| 39 | #else |
| 40 | #define wxHAS_3STATE 0 |
| 41 | #endif |
| 42 | |
| 43 | #include "wx/motif/private.h" |
| 44 | |
| 45 | void wxCheckBoxCallback (Widget w, XtPointer clientData, |
| 46 | XtPointer ptr); |
| 47 | |
| 48 | IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl) |
| 49 | |
| 50 | // Single check box item |
| 51 | bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label, |
| 52 | const wxPoint& pos, |
| 53 | const wxSize& size, long style, |
| 54 | const wxValidator& validator, |
| 55 | const wxString& name) |
| 56 | { |
| 57 | if( !wxControl::CreateControl( parent, id, pos, size, style, validator, |
| 58 | name ) ) |
| 59 | return false; |
| 60 | PreCreation(); |
| 61 | |
| 62 | wxXmString text( GetLabelText(label) ); |
| 63 | |
| 64 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
| 65 | |
| 66 | m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("toggle", |
| 67 | xmToggleButtonWidgetClass, parentWidget, |
| 68 | wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)), |
| 69 | XmNlabelString, text(), |
| 70 | XmNrecomputeSize, False, |
| 71 | // XmNindicatorOn, XmINDICATOR_CHECK_BOX, |
| 72 | // XmNfillOnSelect, False, |
| 73 | #if wxHAS_3STATE |
| 74 | XmNtoggleMode, Is3State() ? XmTOGGLE_INDETERMINATE : XmTOGGLE_BOOLEAN, |
| 75 | #endif |
| 76 | NULL); |
| 77 | |
| 78 | XtAddCallback( (Widget)m_mainWidget, |
| 79 | XmNvalueChangedCallback, (XtCallbackProc)wxCheckBoxCallback, |
| 80 | (XtPointer)this ); |
| 81 | |
| 82 | XmToggleButtonSetState ((Widget) m_mainWidget, False, True); |
| 83 | |
| 84 | PostCreation(); |
| 85 | AttachWidget( parent, m_mainWidget, (WXWidget)NULL, |
| 86 | pos.x, pos.y, size.x, size.y ); |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | void wxCheckBox::SetValue(bool val) |
| 92 | { |
| 93 | if (val) |
| 94 | { |
| 95 | Set3StateValue(wxCHK_CHECKED); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | Set3StateValue(wxCHK_UNCHECKED); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | bool wxCheckBox::GetValue() const |
| 104 | { |
| 105 | return (Get3StateValue() != 0); |
| 106 | } |
| 107 | |
| 108 | void wxCheckBox::Command (wxCommandEvent & event) |
| 109 | { |
| 110 | int state = event.GetInt(); |
| 111 | wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED) |
| 112 | || (state == wxCHK_UNDETERMINED), |
| 113 | wxT("event.GetInt() returned an invalid checkbox state") ); |
| 114 | |
| 115 | Set3StateValue((wxCheckBoxState) state); |
| 116 | ProcessCommand(event); |
| 117 | } |
| 118 | |
| 119 | void wxCheckBoxCallback (Widget WXUNUSED(w), XtPointer clientData, |
| 120 | XtPointer WXUNUSED(ptr)) |
| 121 | { |
| 122 | wxCheckBox *item = (wxCheckBox *) clientData; |
| 123 | |
| 124 | if (item->InSetValue()) |
| 125 | return; |
| 126 | |
| 127 | wxCheckBoxState state = item->Get3StateValue(); |
| 128 | |
| 129 | if( !item->Is3rdStateAllowedForUser() && state == wxCHK_UNDETERMINED ) |
| 130 | { |
| 131 | state = wxCHK_UNCHECKED; |
| 132 | item->Set3StateValue( state ); |
| 133 | } |
| 134 | |
| 135 | wxCommandEvent event( item->m_evtType, item->GetId() ); |
| 136 | event.SetInt( (int)state ); |
| 137 | event.SetEventObject( item ); |
| 138 | item->ProcessCommand( event ); |
| 139 | } |
| 140 | |
| 141 | void wxCheckBox::ChangeBackgroundColour() |
| 142 | { |
| 143 | if (!m_backgroundColour.Ok()) |
| 144 | return; |
| 145 | |
| 146 | wxComputeColours (XtDisplay((Widget) m_mainWidget), & m_backgroundColour, |
| 147 | NULL); |
| 148 | |
| 149 | XtVaSetValues ((Widget) m_mainWidget, |
| 150 | XmNbackground, g_itemColors[wxBACK_INDEX].pixel, |
| 151 | XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel, |
| 152 | XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel, |
| 153 | XmNforeground, g_itemColors[wxFORE_INDEX].pixel, |
| 154 | NULL); |
| 155 | |
| 156 | wxColour colour = *wxBLACK; |
| 157 | WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget)); |
| 158 | |
| 159 | // Better to have the checkbox selection in black, or it's |
| 160 | // hard to determine what state it is in. |
| 161 | XtVaSetValues ((Widget) m_mainWidget, |
| 162 | XmNselectColor, selectPixel, |
| 163 | NULL); |
| 164 | } |
| 165 | |
| 166 | void wxCheckBox::DoSet3StateValue(wxCheckBoxState state) |
| 167 | { |
| 168 | m_inSetValue = true; |
| 169 | |
| 170 | #if wxHAS_3STATE |
| 171 | unsigned char value; |
| 172 | |
| 173 | switch (state) |
| 174 | { |
| 175 | case wxCHK_UNCHECKED: value = XmUNSET; break; |
| 176 | case wxCHK_CHECKED: value = XmSET; break; |
| 177 | case wxCHK_UNDETERMINED: value = XmINDETERMINATE; break; |
| 178 | default: wxASSERT(0); return; |
| 179 | } |
| 180 | |
| 181 | XtVaSetValues( (Widget) m_mainWidget, |
| 182 | XmNset, value, |
| 183 | NULL ); |
| 184 | #else |
| 185 | XmToggleButtonSetState ((Widget) m_mainWidget, |
| 186 | state == wxCHK_CHECKED, True); |
| 187 | #endif |
| 188 | |
| 189 | m_inSetValue = false; |
| 190 | } |
| 191 | |
| 192 | wxCheckBoxState wxCheckBox::DoGet3StateValue() const |
| 193 | { |
| 194 | #if wxHAS_3STATE |
| 195 | unsigned char value = 0; |
| 196 | |
| 197 | XtVaGetValues( (Widget) m_mainWidget, |
| 198 | XmNset, &value, |
| 199 | NULL ); |
| 200 | |
| 201 | switch (value) |
| 202 | { |
| 203 | case XmUNSET: return wxCHK_UNCHECKED; |
| 204 | case XmSET: return wxCHK_CHECKED; |
| 205 | case XmINDETERMINATE: return wxCHK_UNDETERMINED; |
| 206 | } |
| 207 | |
| 208 | // impossible... |
| 209 | return wxCHK_UNDETERMINED; |
| 210 | #else |
| 211 | return wxCheckBoxState(XmToggleButtonGetState ((Widget) m_mainWidget)); |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | /////////////////////////////////////////////////////////////////////////////// |
| 216 | // wxToggleButton |
| 217 | /////////////////////////////////////////////////////////////////////////////// |
| 218 | |
| 219 | #if wxUSE_TOGGLEBTN |
| 220 | |
| 221 | wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEvent ); |
| 222 | IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl) |
| 223 | |
| 224 | bool wxToggleButton::Create( wxWindow* parent, wxWindowID id, |
| 225 | const wxString& label, |
| 226 | const wxPoint& pos, |
| 227 | const wxSize& size, |
| 228 | long style, |
| 229 | const wxValidator& val, |
| 230 | const wxString &name ) |
| 231 | { |
| 232 | if( !wxCheckBox::Create( parent, id, label, pos, size, style, val, name ) ) |
| 233 | return false; |
| 234 | |
| 235 | XtVaSetValues( (Widget)m_mainWidget, |
| 236 | XmNindicatorSize, 0, |
| 237 | #if XmVersion >= 2000 |
| 238 | XmNindicatorOn, XmINDICATOR_NONE, |
| 239 | #else |
| 240 | XmNindicatorOn, False, |
| 241 | #endif |
| 242 | XmNfillOnSelect, False, |
| 243 | XmNshadowThickness, 2, |
| 244 | XmNalignment, XmALIGNMENT_CENTER, |
| 245 | XmNmarginLeft, 0, |
| 246 | XmNmarginRight, 0, |
| 247 | NULL ); |
| 248 | |
| 249 | // set it again, because the XtVaSetValue above resets it |
| 250 | if( size.x != -1 || size.y != -1 ) |
| 251 | SetSize( size ); |
| 252 | |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | #endif // wxUSE_TOGGLEBTN |