Precompiled headers support.
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "button.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __VMS
20 #define XtDisplay XTDISPLAY
21 #endif
22
23 #include "wx/defs.h"
24
25 #include "wx/button.h"
26
27 #ifdef __VMS__
28 #pragma message disable nosimpint
29 #endif
30 #include <Xm/PushBG.h>
31 #include <Xm/PushB.h>
32 #ifdef __VMS__
33 #pragma message enable nosimpint
34 #endif
35
36 #include "wx/motif/private.h"
37
38 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
39
40 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
41
42 // Button
43
44 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
45 const wxPoint& pos,
46 const wxSize& size, long style,
47 const wxValidator& validator,
48 const wxString& name)
49 {
50 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
51 return false;
52
53 wxString label1(wxStripMenuCodes(label));
54 wxXmString text( label1 );
55
56 Widget parentWidget = (Widget) parent->GetClientWidget();
57
58 /*
59 * Patch Note (important)
60 * There is no major reason to put a defaultButtonThickness here.
61 * Not requesting it give the ability to put wxButton with a spacing
62 * as small as requested. However, if some button become a DefaultButton,
63 * other buttons are no more aligned -- This is why we set
64 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
65 * in the ::SetDefaultButton method.
66 */
67 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
68 xmPushButtonWidgetClass,
69 parentWidget,
70 wxFont::GetFontTag(), m_font.GetFontType(XtDisplay(parentWidget)),
71 XmNlabelString, text(),
72 XmNrecomputeSize, False,
73 // See comment for wxButton::SetDefault
74 // XmNdefaultButtonShadowThickness, 1,
75 NULL);
76
77 XtAddCallback ((Widget) m_mainWidget,
78 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
79 (XtPointer) this);
80
81 wxSize best = GetBestSize();
82 if( size.x != -1 ) best.x = size.x;
83 if( size.y != -1 ) best.y = size.y;
84
85 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
86 pos.x, pos.y, best.x, best.y);
87
88 ChangeBackgroundColour();
89
90 return TRUE;
91 }
92
93 void wxButton::SetDefaultShadowThicknessAndResize()
94 {
95 Widget buttonWidget = (Widget)GetMainWidget();
96 bool managed = XtIsManaged( buttonWidget );
97 if( managed )
98 XtUnmanageChild( buttonWidget );
99
100 XtVaSetValues( buttonWidget,
101 XmNdefaultButtonShadowThickness, 1,
102 NULL );
103
104 if( managed )
105 XtManageChild( buttonWidget );
106
107 // this can't currently be done, because user code that calls SetDefault
108 // will break otherwise
109 #if 0
110 wxSize best = GetBestSize(), actual = GetSize();
111 if( best.x < actual.x ) best.x = actual.x;
112 if( best.y < actual.y ) best.y = actual.y;
113
114 if( best != actual )
115 SetSize( best );
116 #endif
117 }
118
119
120 void wxButton::SetDefault()
121 {
122 wxWindow *parent = GetParent();
123 if ( parent )
124 parent->SetDefaultItem(this);
125
126 // We initially do not set XmNdefaultShadowThickness, to have
127 // small buttons. Unfortunately, buttons are now mis-aligned. We
128 // try to correct this now -- setting this ressource to 1 for each
129 // button in the same row. Because it's very hard to find
130 // wxButton in the same row, correction is straighforward: we set
131 // resource for all wxButton in this parent (but not sub panels)
132
133 for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst ();
134 node; node = node->GetNext ())
135 {
136 wxWindow *win = node->GetData ();
137 wxButton *item = wxDynamicCast(win, wxButton);
138 if (item)
139 item->SetDefaultShadowThicknessAndResize();
140 }
141
142 XtVaSetValues ((Widget) parent->GetMainWidget(),
143 XmNdefaultButton, (Widget) GetMainWidget(),
144 NULL);
145 }
146
147 /* static */
148 wxSize wxButton::GetDefaultSize()
149 {
150 // TODO: check font size as in wxMSW ? MB
151 // Note: this is the button size (text + margin + shadow + defaultBorder)
152 return wxSize(78,30);
153 }
154
155 wxSize wxButton::DoGetBestSize() const
156 {
157 Dimension xmargin, ymargin, highlight, shadow, defThickness;
158
159 XtVaGetValues( (Widget)m_mainWidget,
160 XmNmarginWidth, &xmargin,
161 XmNmarginHeight, &ymargin,
162 XmNhighlightThickness, &highlight,
163 XmNshadowThickness, &shadow,
164 XmNdefaultButtonShadowThickness, &defThickness,
165 NULL );
166
167 int x = 0; int y = 0;
168 GetTextExtent( GetLabel(), &x, &y );
169
170 int margin = highlight * 2 +
171 ( defThickness ? ( ( shadow + defThickness ) * 4 ) : ( shadow * 2 ) );
172 wxSize best( x + xmargin * 2 + margin,
173 y + ymargin * 2 + margin );
174
175 // all buttons have at least the standard size unless the user explicitly
176 // wants them to be of smaller size and used wxBU_EXACTFIT style when
177 // creating the button
178 if( !HasFlag( wxBU_EXACTFIT ) )
179 {
180 wxSize def = GetDefaultSize();
181 int margin = highlight * 2 +
182 ( defThickness ? ( shadow * 4 + defThickness * 4 ) : 0 );
183 def.x += margin;
184 def.y += margin;
185
186 if( def.x > best.x )
187 best.x = def.x;
188 if( def.y > best.y )
189 best.y = def.y;
190 }
191
192 return best;
193 }
194
195 void wxButton::Command (wxCommandEvent & event)
196 {
197 ProcessCommand (event);
198 }
199
200 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
201 {
202 if (!wxGetWindowFromTable(w))
203 // Widget has been deleted!
204 return;
205
206 wxButton *item = (wxButton *) clientData;
207 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
208 event.SetEventObject(item);
209 item->ProcessCommand (event);
210 }