More Motif stuff incl. beginnings of wxToolBar
[wxWidgets.git] / src / motif / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: radiobut.cpp
3 // Purpose: wxRadioButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "radiobut.h"
14 #endif
15
16 #include "wx/radiobut.h"
17 #include "wx/utils.h"
18
19 #include <Xm/Label.h>
20 #include <Xm/LabelG.h>
21 #include <Xm/ToggleB.h>
22 #include <Xm/ToggleBG.h>
23 #include <Xm/RowColumn.h>
24 #include <Xm/Form.h>
25
26 #include <wx/motif/private.h>
27
28 void wxRadioButtonCallback (Widget w, XtPointer clientData,
29 XmToggleButtonCallbackStruct * cbs);
30
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
33 #endif
34
35 wxRadioButton::wxRadioButton()
36 {
37 m_labelWidget = (WXWidget) 0;
38 m_formWidget = (WXWidget) 0;
39 }
40
41 bool wxRadioButton::Create(wxWindow *parent, wxWindowID id,
42 const wxString& label,
43 const wxPoint& pos,
44 const wxSize& size, long style,
45 const wxValidator& validator,
46 const wxString& name)
47 {
48 SetName(name);
49 SetValidator(validator);
50 m_backgroundColour = parent->GetBackgroundColour();
51 m_foregroundColour = parent->GetForegroundColour();
52
53 if (parent) parent->AddChild(this);
54
55 if ( id == -1 )
56 m_windowId = (int)NewControlId();
57 else
58 m_windowId = id;
59
60 m_windowStyle = style ;
61
62 Widget parentWidget = (Widget) parent->GetClientWidget();
63
64 wxString label1(wxStripMenuCodes(label));
65
66 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
67
68 Widget formWidget = XtVaCreateManagedWidget ((char*) (const char*) name,
69 xmFormWidgetClass, parentWidget,
70 XmNmarginHeight, 0,
71 XmNmarginWidth, 0,
72 NULL);
73
74 m_formWidget = (WXWidget) formWidget;
75
76 Widget labelWidget = XtVaCreateManagedWidget ((char*) (const char*) label1,
77 #if wxUSE_GADGETS
78 xmLabelGadgetClass,
79 formWidget,
80 #else
81 xmLabelWidgetClass, formWidget,
82 #endif
83 XmNlabelString, text,
84 NULL);
85 m_labelWidget = (WXWidget) labelWidget;
86 /* TODO
87 if (labelFont)
88 XtVaSetValues (labelWidget,
89 XmNfontList, labelFont->GetInternalFont (XtDisplay(formWidget)),
90 NULL);
91 */
92
93 XmStringFree (text);
94
95 Widget radioButtonWidget = XtVaCreateManagedWidget ("toggle",
96 #if wxUSE_GADGETS
97 xmToggleButtonGadgetClass, formWidget,
98 #else
99 xmToggleButtonWidgetClass, formWidget,
100 #endif
101 NULL);
102 XtAddCallback (radioButtonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxRadioButtonCallback,
103 (XtCallbackProc) this);
104
105 m_mainWidget = (WXWidget) radioButtonWidget;
106
107 /* TODO
108 if (labelFont)
109 XtVaSetValues (radioButtonWidget,
110 XmNfontList, labelFont->GetInternalFont (XtDisplay(formWidget)),
111 NULL);
112 */
113
114 if (labelWidget)
115 XtVaSetValues (labelWidget,
116 XmNtopAttachment, XmATTACH_FORM,
117 XmNleftAttachment, XmATTACH_FORM,
118 XmNbottomAttachment, XmATTACH_FORM,
119 XmNalignment, XmALIGNMENT_BEGINNING,
120 NULL);
121 XtVaSetValues (radioButtonWidget,
122 XmNleftOffset, 4,
123 XmNtopAttachment, XmATTACH_FORM,
124 XmNbottomAttachment, XmATTACH_FORM,
125 XmNleftAttachment, (Widget) m_labelWidget ? XmATTACH_WIDGET : XmATTACH_FORM,
126 XmNleftWidget, (Widget) m_labelWidget ? (Widget) m_labelWidget : formWidget,
127 NULL);
128
129 XtManageChild (radioButtonWidget);
130
131 SetCanAddEventHandler(TRUE);
132 AttachWidget (parent, m_mainWidget, m_formWidget, pos.x, pos.y, size.x, size.y);
133
134 SetFont(* parent->GetFont());
135 ChangeBackgroundColour();
136
137 return TRUE;
138 }
139
140 void wxRadioButton::SetValue(bool value)
141 {
142 m_inSetValue = TRUE;
143 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, FALSE);
144 m_inSetValue = FALSE;
145 }
146
147 // Get single selection, for single choice list items
148 bool wxRadioButton::GetValue() const
149 {
150 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
151 }
152
153 void wxRadioButton::Command (wxCommandEvent & event)
154 {
155 SetValue ( (event.m_commandInt != 0) );
156 ProcessCommand (event);
157 }
158
159 void wxRadioButton::ChangeFont()
160 {
161 // TODO
162 }
163
164 void wxRadioButton::ChangeBackgroundColour()
165 {
166 // TODO
167 }
168
169 void wxRadioButton::ChangeForegroundColour()
170 {
171 // TODO
172 }
173
174 void wxRadioButtonCallback (Widget w, XtPointer clientData,
175 XmToggleButtonCallbackStruct * cbs)
176 {
177 if (!cbs->set)
178 return;
179
180 wxRadioButton *item = (wxRadioButton *) clientData;
181 if (item->InSetValue())
182 return;
183
184 wxCommandEvent event (wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId());
185 event.SetEventObject(item);
186
187 item->ProcessCommand (event);
188 }
189