]> git.saurik.com Git - wxWidgets.git/blame - src/motif/button.cpp
Applied Patch #1502801: Missing Clone() functions from some events
[wxWidgets.git] / src / motif / button.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
7520f3da 2// Name: src/motif/button.cpp
4bb6408c
JS
3// Purpose: wxButton
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
bcd055ae
JJ
15#ifdef __VMS
16#define XtDisplay XTDISPLAY
17#endif
18
4bb6408c
JS
19#include "wx/button.h"
20
338dd992
JJ
21#ifdef __VMS__
22#pragma message disable nosimpint
23#endif
02e8b2f9
JS
24#include <Xm/PushBG.h>
25#include <Xm/PushB.h>
338dd992
JJ
26#ifdef __VMS__
27#pragma message enable nosimpint
28#endif
02e8b2f9 29
5f7bcb48 30#include "wx/stockitem.h"
02e8b2f9 31#include "wx/motif/private.h"
e04b7e8e 32#include "wx/sysopt.h"
02e8b2f9
JS
33
34void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
35
4bb6408c 36IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
4bb6408c 37
e04b7e8e
MB
38#define MIN_WIDTH 78
39#define MIN_LARGE_HEIGHT 30
40
4bb6408c
JS
41// Button
42
5f7bcb48 43bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& lbl,
2d120f83
JS
44 const wxPoint& pos,
45 const wxSize& size, long style,
46 const wxValidator& validator,
47 const wxString& name)
4bb6408c 48{
5f7bcb48
VS
49 wxString label(lbl);
50 if (label.empty() && wxIsStockID(id))
51 label = wxGetStockLabel(id);
7520f3da 52
458ca8c1
MB
53 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
54 return false;
dfe1eee3 55
89c7e962 56 wxString label1(wxStripMenuCodes(label));
458ca8c1 57 wxXmString text( label1 );
dfe1eee3 58
02e8b2f9 59 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 60
02e8b2f9 61 /*
2d120f83
JS
62 * Patch Note (important)
63 * There is no major reason to put a defaultButtonThickness here.
64 * Not requesting it give the ability to put wxButton with a spacing
65 * as small as requested. However, if some button become a DefaultButton,
66 * other buttons are no more aligned -- This is why we set
67 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
68 * in the ::SetDefaultButton method.
69 */
02e8b2f9 70 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
2d120f83
JS
71 xmPushButtonWidgetClass,
72 parentWidget,
73608949 73 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)),
458ca8c1 74 XmNlabelString, text(),
84fb430b 75 XmNrecomputeSize, False,
458ca8c1 76 // See comment for wxButton::SetDefault
7520f3da 77 // XmNdefaultButtonShadowThickness, 1,
2d120f83 78 NULL);
dfe1eee3 79
458ca8c1
MB
80 XtAddCallback ((Widget) m_mainWidget,
81 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
82 (XtPointer) this);
dfe1eee3 83
458ca8c1
MB
84 wxSize best = GetBestSize();
85 if( size.x != -1 ) best.x = size.x;
86 if( size.y != -1 ) best.y = size.y;
87
88 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
89 pos.x, pos.y, best.x, best.y);
dfe1eee3 90
0d57be45 91 ChangeBackgroundColour();
dfe1eee3 92
e04b7e8e 93 return true;
4bb6408c
JS
94}
95
458ca8c1
MB
96void wxButton::SetDefaultShadowThicknessAndResize()
97{
98 Widget buttonWidget = (Widget)GetMainWidget();
99 bool managed = XtIsManaged( buttonWidget );
100 if( managed )
101 XtUnmanageChild( buttonWidget );
102
103 XtVaSetValues( buttonWidget,
104 XmNdefaultButtonShadowThickness, 1,
105 NULL );
106
107 if( managed )
108 XtManageChild( buttonWidget );
109
84fb430b
MB
110 // this can't currently be done, because user code that calls SetDefault
111 // will break otherwise
112#if 0
458ca8c1
MB
113 wxSize best = GetBestSize(), actual = GetSize();
114 if( best.x < actual.x ) best.x = actual.x;
115 if( best.y < actual.y ) best.y = actual.y;
116
117 if( best != actual )
118 SetSize( best );
84fb430b 119#endif
4e025aa2 120 InvalidateBestSize();
458ca8c1
MB
121}
122
123
4bb6408c
JS
124void wxButton::SetDefault()
125{
dfe1eee3 126 wxWindow *parent = GetParent();
b0ee47ff 127 if ( parent )
d5f005cc 128 parent->SetDefaultItem(this);
dfe1eee3 129
458ca8c1
MB
130 // We initially do not set XmNdefaultShadowThickness, to have
131 // small buttons. Unfortunately, buttons are now mis-aligned. We
132 // try to correct this now -- setting this ressource to 1 for each
133 // button in the same row. Because it's very hard to find
134 // wxButton in the same row, correction is straighforward: we set
135 // resource for all wxButton in this parent (but not sub panels)
136
ac32ba44 137 for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst ();
fd304d98 138 node; node = node->GetNext ())
02e8b2f9 139 {
fd304d98
MB
140 wxWindow *win = node->GetData ();
141 wxButton *item = wxDynamicCast(win, wxButton);
142 if (item)
458ca8c1
MB
143 item->SetDefaultShadowThicknessAndResize();
144 }
145
146 XtVaSetValues ((Widget) parent->GetMainWidget(),
147 XmNdefaultButton, (Widget) GetMainWidget(),
148 NULL);
4bb6408c
JS
149}
150
e04b7e8e
MB
151static inline bool wxMotifLargeButtons()
152{
153 return wxSystemOptions::HasOption("motif.largebuttons")
154 && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0;
155}
156
4d194d63
MB
157/* static */
158wxSize wxButton::GetDefaultSize()
159{
160 // TODO: check font size as in wxMSW ? MB
84fb430b 161 // Note: this is the button size (text + margin + shadow + defaultBorder)
e04b7e8e 162 return wxSize( MIN_WIDTH, MIN_LARGE_HEIGHT );
458ca8c1
MB
163}
164
165wxSize wxButton::DoGetBestSize() const
e04b7e8e
MB
166{
167 if( wxMotifLargeButtons() )
168 return OldGetBestSize();
169
170 wxSize best = wxControl::DoGetBestSize();
171
172 if( HasFlag( wxBU_EXACTFIT ) )
173 return best;
174 else if( best.x < MIN_WIDTH )
175 best.x = MIN_WIDTH;
176
177 return best;
178}
179
4e025aa2
MB
180wxSize wxButton::GetMinSize() const
181{
182 if( wxMotifLargeButtons() )
183 return OldGetMinSize();
184
185 return DoGetBestSize();
186}
187
188wxSize wxButton::OldGetMinSize() const
189{
190 return OldGetBestSize();
191}
192
e04b7e8e 193wxSize wxButton::OldGetBestSize() const
458ca8c1
MB
194{
195 Dimension xmargin, ymargin, highlight, shadow, defThickness;
196
197 XtVaGetValues( (Widget)m_mainWidget,
198 XmNmarginWidth, &xmargin,
199 XmNmarginHeight, &ymargin,
200 XmNhighlightThickness, &highlight,
201 XmNshadowThickness, &shadow,
202 XmNdefaultButtonShadowThickness, &defThickness,
203 NULL );
204
205 int x = 0; int y = 0;
206 GetTextExtent( GetLabel(), &x, &y );
207
208 int margin = highlight * 2 +
209 ( defThickness ? ( ( shadow + defThickness ) * 4 ) : ( shadow * 2 ) );
4e025aa2 210
458ca8c1
MB
211 wxSize best( x + xmargin * 2 + margin,
212 y + ymargin * 2 + margin );
213
214 // all buttons have at least the standard size unless the user explicitly
215 // wants them to be of smaller size and used wxBU_EXACTFIT style when
216 // creating the button
217 if( !HasFlag( wxBU_EXACTFIT ) )
218 {
219 wxSize def = GetDefaultSize();
220 int margin = highlight * 2 +
221 ( defThickness ? ( shadow * 4 + defThickness * 4 ) : 0 );
222 def.x += margin;
223 def.y += margin;
224
225 if( def.x > best.x )
226 best.x = def.x;
227 if( def.y > best.y )
228 best.y = def.y;
229 }
230
231 return best;
4d194d63
MB
232}
233
4bb6408c
JS
234void wxButton::Command (wxCommandEvent & event)
235{
236 ProcessCommand (event);
237}
238
f9e02ac7 239void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
02e8b2f9 240{
2d120f83
JS
241 if (!wxGetWindowFromTable(w))
242 // Widget has been deleted!
243 return;
dfe1eee3 244
2d120f83
JS
245 wxButton *item = (wxButton *) clientData;
246 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
247 event.SetEventObject(item);
248 item->ProcessCommand (event);
02e8b2f9 249}