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