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