Some Motif fixes; makefile fixes; added wxTransferStreamToFile/FileToStream for wxWin
[wxWidgets.git] / src / motif / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
13 #pragma implementation "checkbox.h"
14 #endif
15
16 #include "wx/checkbox.h"
17 #include "wx/utils.h"
18
19 #ifdef __VMS__
20 #pragma message disable nosimpint
21 #endif
22 #include <Xm/Label.h>
23 #include <Xm/LabelG.h>
24 #include <Xm/ToggleB.h>
25 #include <Xm/ToggleBG.h>
26 #ifdef __VMS__
27 #pragma message enable nosimpint
28 #endif
29
30 #include "wx/motif/private.h"
31
32 void wxCheckBoxCallback (Widget w, XtPointer clientData,
33 XtPointer ptr);
34
35 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
36 IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox, wxCheckBox)
37
38 // Single check box item
39 bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
40 const wxPoint& pos,
41 const wxSize& size, long style,
42 const wxValidator& validator,
43 const wxString& name)
44 {
45 SetName(name);
46 SetValidator(validator);
47 m_windowStyle = style;
48 m_backgroundColour = parent->GetBackgroundColour();
49 m_foregroundColour = parent->GetForegroundColour();
50 m_font = parent->GetFont();
51
52 if (parent) parent->AddChild(this);
53
54 if ( id == -1 )
55 m_windowId = NewControlId();
56 else
57 m_windowId = id;
58
59 #if 0 // gcc 2.95 doesn't like this apparently
60 char* label1 = (label.IsNull() ? "" : (char*) (const char*) label);
61 XmString text = XmStringCreateSimple (label1);
62 #endif
63 wxString label1(wxStripMenuCodes(label));
64
65 wxXmString text( label1 );
66
67 Widget parentWidget = (Widget) parent->GetClientWidget();
68 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
69
70 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("toggle",
71 xmToggleButtonWidgetClass, parentWidget,
72 XmNfontList, fontList,
73 XmNlabelString, text(),
74 NULL);
75 #if 0
76 XmStringFree (text);
77 #endif
78
79 XtAddCallback ((Widget) m_mainWidget, XmNvalueChangedCallback, (XtCallbackProc) wxCheckBoxCallback,
80 (XtPointer) this);
81
82 XmToggleButtonSetState ((Widget) m_mainWidget, FALSE, TRUE);
83
84 SetCanAddEventHandler(TRUE);
85 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
86
87 ChangeBackgroundColour();
88 return TRUE;
89 }
90
91 void wxCheckBox::SetValue(bool val)
92 {
93 m_inSetValue = TRUE;
94 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) val, TRUE);
95 m_inSetValue = FALSE;
96 }
97
98 bool wxCheckBox::GetValue() const
99 {
100 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
101 }
102
103 void wxCheckBox::Command (wxCommandEvent & event)
104 {
105 SetValue ((event.GetInt() != 0));
106 ProcessCommand (event);
107 }
108
109 // Bitmap checkbox
110 bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *WXUNUSED(label),
111 const wxPoint& WXUNUSED(pos),
112 const wxSize& WXUNUSED(size), long style,
113 const wxValidator& validator,
114 const wxString& name)
115 {
116 SetName(name);
117 SetValidator(validator);
118 m_windowStyle = style;
119
120 if (parent) parent->AddChild(this);
121
122 if ( id == -1 )
123 m_windowId = NewControlId();
124 else
125 m_windowId = id;
126
127 // TODO: Create the bitmap checkbox
128
129 return FALSE;
130 }
131
132 void wxBitmapCheckBox::SetLabel(const wxBitmap& WXUNUSED(bitmap))
133 {
134 // TODO
135 }
136
137 void wxBitmapCheckBox::DoSetSize(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height), int WXUNUSED(sizeFlags))
138 {
139 // TODO
140 }
141
142 void wxBitmapCheckBox::SetValue(bool WXUNUSED(val))
143 {
144 // TODO
145 }
146
147 bool wxBitmapCheckBox::GetValue() const
148 {
149 // TODOD
150 return FALSE;
151 }
152
153 void wxCheckBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
154 XtPointer WXUNUSED(ptr))
155 {
156 wxCheckBox *item = (wxCheckBox *) clientData;
157
158 if (item->InSetValue())
159 return;
160
161 wxCommandEvent event (wxEVT_COMMAND_CHECKBOX_CLICKED, item->GetId());
162 event.SetInt((int) item->GetValue ());
163 event.SetEventObject(item);
164 item->ProcessCommand (event);
165 }
166
167 void wxCheckBox::ChangeFont(bool keepOriginalSize)
168 {
169 wxWindow::ChangeFont(keepOriginalSize);
170 }
171
172 void wxCheckBox::ChangeBackgroundColour()
173 {
174 wxComputeColours (XtDisplay((Widget) m_mainWidget), & m_backgroundColour,
175 (wxColour*) NULL);
176
177 XtVaSetValues ((Widget) m_mainWidget,
178 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
179 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
180 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
181 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
182 NULL);
183
184 int selectPixel = wxBLACK->AllocColour(wxGetDisplay());
185
186 // Better to have the checkbox selection in black, or it's
187 // hard to determine what state it is in.
188 XtVaSetValues ((Widget) m_mainWidget,
189 // XmNselectColor, g_itemColors[wxSELE_INDEX].pixel,
190 XmNselectColor, selectPixel,
191 NULL);
192 }
193
194 void wxCheckBox::ChangeForegroundColour()
195 {
196 wxWindow::ChangeForegroundColour();
197 }