]> git.saurik.com Git - wxWidgets.git/blob - src/univ/radiobut.cpp
new module definition file for OS/2 Dll build.
[wxWidgets.git] / src / univ / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/radiobut.cpp
3 // Purpose: wxRadioButton implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 10.09.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "univradiobut.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #if wxUSE_RADIOBTN
31
32 #ifndef WX_PRECOMP
33 #include "wx/dcclient.h"
34 #include "wx/radiobut.h"
35 #include "wx/validate.h"
36 #endif
37
38 #include "wx/univ/theme.h"
39 #include "wx/univ/renderer.h"
40 #include "wx/univ/inphand.h"
41 #include "wx/univ/colschem.h"
42
43 // ============================================================================
44 // implementation
45 // ============================================================================
46
47 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
48
49 // ----------------------------------------------------------------------------
50 // wxRadioButton
51 // ----------------------------------------------------------------------------
52
53 bool wxRadioButton::Create(wxWindow *parent,
54 wxWindowID id,
55 const wxString &label,
56 const wxPoint &pos,
57 const wxSize &size,
58 long style,
59 const wxValidator& validator,
60 const wxString &name)
61 {
62 if ( !wxCheckBox::Create(parent, id, label, pos, size, style,
63 wxDefaultValidator, name) )
64 {
65 return FALSE;
66 }
67
68 return TRUE;
69 }
70
71 // ----------------------------------------------------------------------------
72 // radio button methods
73 // ----------------------------------------------------------------------------
74
75 void wxRadioButton::OnCheck()
76 {
77 // clear all others radio buttons in our group: for this we need to
78 // find the radio button which is the first in the group, i.e. the one
79 // with wxRB_GROUP style
80 const wxWindowList& siblings = GetParent()->GetChildren();
81 wxWindowList::Node *nodeStart = siblings.Find(this);
82 while ( nodeStart )
83 {
84 // stop if we found a radio button with wxRB_GROUP style or it we
85 // are at the first control
86 if ( !nodeStart->GetPrevious() ||
87 (nodeStart->GetData()->GetWindowStyle() & wxRB_GROUP) )
88 break;
89
90 nodeStart = nodeStart->GetPrevious();
91 }
92
93 // now clear all radio buttons from the starting one until the next
94 // one with wxRB_GROUP style
95 while ( nodeStart )
96 {
97 wxWindow *win = nodeStart->GetData();
98 if ( win != this )
99 {
100 wxRadioButton *btn = wxDynamicCast(win, wxRadioButton);
101 if ( btn )
102 {
103 btn->ClearValue();
104 }
105 }
106
107 nodeStart = nodeStart->GetNext();
108 if ( !nodeStart ||
109 (nodeStart->GetData()->GetWindowStyle() & wxRB_GROUP) )
110 {
111 // we reached the next group
112 break;
113 }
114 }
115 }
116
117 void wxRadioButton::ChangeValue(bool value)
118 {
119 if ( value == IsChecked() )
120 return;
121
122 if ( !IsChecked() )
123 {
124 wxCheckBox::ChangeValue(value);
125 }
126 else // attempt to clear a radio button - this can't be done
127 {
128 // but still refresh as our PRESSED flag changed
129 Refresh();
130 }
131 }
132
133 void wxRadioButton::ClearValue()
134 {
135 if ( IsChecked() )
136 {
137 SetValue(FALSE);
138 }
139 }
140
141 void wxRadioButton::SendEvent()
142 {
143 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId());
144 InitCommandEvent(event);
145 event.SetInt(IsChecked());
146 Command(event);
147 }
148
149 // ----------------------------------------------------------------------------
150 // overridden wxCheckBox methods
151 // ----------------------------------------------------------------------------
152
153 wxSize wxRadioButton::GetBitmapSize() const
154 {
155 wxBitmap bmp = GetBitmap(State_Normal, Status_Checked);
156 return bmp.Ok() ? wxSize(bmp.GetWidth(), bmp.GetHeight())
157 : GetRenderer()->GetRadioBitmapSize();
158 }
159
160 void wxRadioButton::DoDraw(wxControlRenderer *renderer)
161 {
162 wxDC& dc = renderer->GetDC();
163 dc.SetFont(GetFont());
164 dc.SetTextForeground(GetForegroundColour());
165
166 int flags = GetStateFlags();
167 Status status = GetStatus();
168 if ( status == Status_Checked )
169 flags |= wxCONTROL_CHECKED;
170
171 renderer->GetRenderer()->
172 DrawRadioButton(dc,
173 GetLabel(),
174 GetBitmap(GetState(flags), status),
175 renderer->GetRect(),
176 flags,
177 GetWindowStyle() & wxALIGN_RIGHT ? wxALIGN_RIGHT
178 : wxALIGN_LEFT,
179 GetAccelIndex());
180 }
181
182 #endif // wxUSE_RADIOBTN
183