]> git.saurik.com Git - wxWidgets.git/blob - src/motif/button.cpp
implemented Freeze/Thaw() (patch 922156)
[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 #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 wxSize best = GetBestSize();
79 if( size.x != -1 ) best.x = size.x;
80 if( size.y != -1 ) best.y = size.y;
81
82 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
83 pos.x, pos.y, best.x, best.y);
84
85 ChangeBackgroundColour();
86
87 return TRUE;
88 }
89
90 void wxButton::SetDefaultShadowThicknessAndResize()
91 {
92 Widget buttonWidget = (Widget)GetMainWidget();
93 bool managed = XtIsManaged( buttonWidget );
94 if( managed )
95 XtUnmanageChild( buttonWidget );
96
97 XtVaSetValues( buttonWidget,
98 XmNdefaultButtonShadowThickness, 1,
99 NULL );
100
101 if( managed )
102 XtManageChild( buttonWidget );
103
104 // this can't currently be done, because user code that calls SetDefault
105 // will break otherwise
106 #if 0
107 wxSize best = GetBestSize(), actual = GetSize();
108 if( best.x < actual.x ) best.x = actual.x;
109 if( best.y < actual.y ) best.y = actual.y;
110
111 if( best != actual )
112 SetSize( best );
113 #endif
114 }
115
116
117 void wxButton::SetDefault()
118 {
119 wxWindow *parent = GetParent();
120 if ( parent )
121 parent->SetDefaultItem(this);
122
123 // We initially do not set XmNdefaultShadowThickness, to have
124 // small buttons. Unfortunately, buttons are now mis-aligned. We
125 // try to correct this now -- setting this ressource to 1 for each
126 // button in the same row. Because it's very hard to find
127 // wxButton in the same row, correction is straighforward: we set
128 // resource for all wxButton in this parent (but not sub panels)
129
130 for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst ();
131 node; node = node->GetNext ())
132 {
133 wxWindow *win = node->GetData ();
134 wxButton *item = wxDynamicCast(win, wxButton);
135 if (item)
136 item->SetDefaultShadowThicknessAndResize();
137 }
138
139 XtVaSetValues ((Widget) parent->GetMainWidget(),
140 XmNdefaultButton, (Widget) GetMainWidget(),
141 NULL);
142 }
143
144 /* static */
145 wxSize wxButton::GetDefaultSize()
146 {
147 // TODO: check font size as in wxMSW ? MB
148 // Note: this is the button size (text + margin + shadow + defaultBorder)
149 return wxSize(78,30);
150 }
151
152 wxSize wxButton::DoGetBestSize() const
153 {
154 Dimension xmargin, ymargin, highlight, shadow, defThickness;
155
156 XtVaGetValues( (Widget)m_mainWidget,
157 XmNmarginWidth, &xmargin,
158 XmNmarginHeight, &ymargin,
159 XmNhighlightThickness, &highlight,
160 XmNshadowThickness, &shadow,
161 XmNdefaultButtonShadowThickness, &defThickness,
162 NULL );
163
164 int x = 0; int y = 0;
165 GetTextExtent( GetLabel(), &x, &y );
166
167 int margin = highlight * 2 +
168 ( defThickness ? ( ( shadow + defThickness ) * 4 ) : ( shadow * 2 ) );
169 wxSize best( x + xmargin * 2 + margin,
170 y + ymargin * 2 + margin );
171
172 // all buttons have at least the standard size unless the user explicitly
173 // wants them to be of smaller size and used wxBU_EXACTFIT style when
174 // creating the button
175 if( !HasFlag( wxBU_EXACTFIT ) )
176 {
177 wxSize def = GetDefaultSize();
178 int margin = highlight * 2 +
179 ( defThickness ? ( shadow * 4 + defThickness * 4 ) : 0 );
180 def.x += margin;
181 def.y += margin;
182
183 if( def.x > best.x )
184 best.x = def.x;
185 if( def.y > best.y )
186 best.y = def.y;
187 }
188
189 return best;
190 }
191
192 void wxButton::Command (wxCommandEvent & event)
193 {
194 ProcessCommand (event);
195 }
196
197 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
198 {
199 if (!wxGetWindowFromTable(w))
200 // Widget has been deleted!
201 return;
202
203 wxButton *item = (wxButton *) clientData;
204 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
205 event.SetEventObject(item);
206 item->ProcessCommand (event);
207 }