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