]> git.saurik.com Git - wxWidgets.git/blob - src/motif/button.cpp
Implemented wxToggleButton under Motif.
[wxWidgets.git] / src / motif / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: button.cpp
3 // Purpose: wxButton
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 "button.h"
14 #endif
15
16 #ifdef __VMS
17 #define XtDisplay XTDISPLAY
18 #endif
19
20 #include "wx/defs.h"
21
22 #include "wx/button.h"
23 #include "wx/utils.h"
24 #include "wx/panel.h"
25
26 #ifdef __VMS__
27 #pragma message disable nosimpint
28 #endif
29 #include <Xm/PushBG.h>
30 #include <Xm/PushB.h>
31 #ifdef __VMS__
32 #pragma message enable nosimpint
33 #endif
34
35 #include "wx/motif/private.h"
36
37 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
38
39 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
40
41 // Button
42
43 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
44 const wxPoint& pos,
45 const wxSize& size, long style,
46 const wxValidator& validator,
47 const wxString& name)
48 {
49 SetName(name);
50 SetValidator(validator);
51 m_windowStyle = style;
52 m_backgroundColour = parent->GetBackgroundColour();
53 m_foregroundColour = parent->GetForegroundColour();
54 m_font = parent->GetFont();
55
56 parent->AddChild((wxButton *)this);
57
58 if (id == -1)
59 m_windowId = NewControlId();
60 else
61 m_windowId = id;
62
63 wxString label1(wxStripMenuCodes(label));
64
65 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
66 Widget parentWidget = (Widget) parent->GetClientWidget();
67
68 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
69
70 /*
71 * Patch Note (important)
72 * There is no major reason to put a defaultButtonThickness here.
73 * Not requesting it give the ability to put wxButton with a spacing
74 * as small as requested. However, if some button become a DefaultButton,
75 * other buttons are no more aligned -- This is why we set
76 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
77 * in the ::SetDefaultButton method.
78 */
79 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
80 xmPushButtonWidgetClass,
81 parentWidget,
82 XmNfontList, fontList,
83 XmNlabelString, text,
84 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
85 NULL);
86
87 XmStringFree (text);
88
89 XtAddCallback ((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
90 (XtPointer) this);
91
92 SetCanAddEventHandler(TRUE);
93
94 int x = 0; int y = 0;
95 wxFont new_font( parent->GetFont() );
96 GetTextExtent( label1, &x, &y, (int*)NULL, (int*)NULL, &new_font );
97
98 wxSize newSize = size;
99 if (newSize.x == -1) newSize.x = 30+x;
100 if (newSize.y == -1) newSize.y = 27+y;
101 SetSize( newSize.x, newSize.y );
102
103 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, newSize.x, newSize.y);
104
105 ChangeBackgroundColour();
106
107 return TRUE;
108 }
109
110 void wxButton::SetDefault()
111 {
112 wxWindow *parent = GetParent();
113 if ( parent )
114 parent->SetDefaultItem(this);
115
116 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
117 // Unfortunately, buttons are now mis-aligned. We try to correct this
118 // now -- setting this ressource to 1 for each button in the same row.
119 // Because it's very hard to find wxButton in the same row,
120 // correction is straighforward: we set resource for all wxButton
121 // in this parent (but not sub panels)
122 for (wxWindowList::Node * node = parent->GetChildren().GetFirst ();
123 node; node = node->GetNext ())
124 {
125 wxWindow *win = node->GetData ();
126 wxButton *item = wxDynamicCast(win, wxButton);
127 if (item)
128 {
129 bool managed = XtIsManaged((Widget) item->GetMainWidget());
130 if (managed)
131 XtUnmanageChild ((Widget) item->GetMainWidget());
132
133 XtVaSetValues ((Widget) item->GetMainWidget(),
134 XmNdefaultButtonShadowThickness, 1,
135 NULL);
136
137 if (managed)
138 XtManageChild ((Widget) item->GetMainWidget());
139 }
140 } // while
141
142 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
143 XtVaSetValues ((Widget) parent->GetMainWidget(), XmNdefaultButton, (Widget) GetMainWidget(), NULL);
144 }
145
146 /* static */
147 wxSize wxButton::GetDefaultSize()
148 {
149 // TODO: check font size as in wxMSW ? MB
150 //
151 return wxSize(80,26);
152 }
153
154 void wxButton::Command (wxCommandEvent & event)
155 {
156 ProcessCommand (event);
157 }
158
159 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
160 {
161 if (!wxGetWindowFromTable(w))
162 // Widget has been deleted!
163 return;
164
165 wxButton *item = (wxButton *) clientData;
166 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
167 event.SetEventObject(item);
168 item->ProcessCommand (event);
169 }
170
171 void wxButton::ChangeFont(bool keepOriginalSize)
172 {
173 wxWindow::ChangeFont(keepOriginalSize);
174 }
175
176 void wxButton::ChangeBackgroundColour()
177 {
178 DoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE);
179 }
180
181 void wxButton::ChangeForegroundColour()
182 {
183 wxWindow::ChangeForegroundColour();
184 }
185