]> git.saurik.com Git - wxWidgets.git/blame - src/motif/button.cpp
Committing in .
[wxWidgets.git] / src / motif / button.cpp
CommitLineData
4bb6408c
JS
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
dfe1eee3 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "button.h"
14#endif
15
bcd055ae
JJ
16#ifdef __VMS
17#define XtDisplay XTDISPLAY
18#endif
19
f6045f99
GD
20#include "wx/defs.h"
21
4bb6408c
JS
22#include "wx/button.h"
23
338dd992
JJ
24#ifdef __VMS__
25#pragma message disable nosimpint
26#endif
02e8b2f9
JS
27#include <Xm/PushBG.h>
28#include <Xm/PushB.h>
338dd992
JJ
29#ifdef __VMS__
30#pragma message enable nosimpint
31#endif
02e8b2f9
JS
32
33#include "wx/motif/private.h"
34
35void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
36
4bb6408c 37IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
4bb6408c
JS
38
39// Button
40
41bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
42 const wxPoint& pos,
43 const wxSize& size, long style,
44 const wxValidator& validator,
45 const wxString& name)
4bb6408c 46{
458ca8c1
MB
47 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
48 return false;
dfe1eee3 49
89c7e962 50 wxString label1(wxStripMenuCodes(label));
458ca8c1 51 wxXmString text( label1 );
dfe1eee3 52
02e8b2f9 53 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 54
02e8b2f9 55 /*
2d120f83
JS
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 */
02e8b2f9 64 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
2d120f83
JS
65 xmPushButtonWidgetClass,
66 parentWidget,
da494b40 67 wxFont::GetFontTag(), m_font.GetFontType(XtDisplay(parentWidget)),
458ca8c1 68 XmNlabelString, text(),
84fb430b 69 XmNrecomputeSize, False,
458ca8c1
MB
70 // See comment for wxButton::SetDefault
71 // XmNdefaultButtonShadowThickness, 1,
2d120f83 72 NULL);
dfe1eee3 73
458ca8c1
MB
74 XtAddCallback ((Widget) m_mainWidget,
75 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
76 (XtPointer) this);
dfe1eee3 77
458ca8c1
MB
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);
dfe1eee3 84
0d57be45 85 ChangeBackgroundColour();
dfe1eee3 86
02e8b2f9 87 return TRUE;
4bb6408c
JS
88}
89
458ca8c1
MB
90void 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
84fb430b
MB
104 // this can't currently be done, because user code that calls SetDefault
105 // will break otherwise
106#if 0
458ca8c1
MB
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 );
84fb430b 113#endif
458ca8c1
MB
114}
115
116
4bb6408c
JS
117void wxButton::SetDefault()
118{
dfe1eee3 119 wxWindow *parent = GetParent();
b0ee47ff 120 if ( parent )
d5f005cc 121 parent->SetDefaultItem(this);
dfe1eee3 122
458ca8c1
MB
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
ac32ba44 130 for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst ();
fd304d98 131 node; node = node->GetNext ())
02e8b2f9 132 {
fd304d98
MB
133 wxWindow *win = node->GetData ();
134 wxButton *item = wxDynamicCast(win, wxButton);
135 if (item)
458ca8c1
MB
136 item->SetDefaultShadowThicknessAndResize();
137 }
138
139 XtVaSetValues ((Widget) parent->GetMainWidget(),
140 XmNdefaultButton, (Widget) GetMainWidget(),
141 NULL);
4bb6408c
JS
142}
143
4d194d63
MB
144/* static */
145wxSize wxButton::GetDefaultSize()
146{
147 // TODO: check font size as in wxMSW ? MB
84fb430b
MB
148 // Note: this is the button size (text + margin + shadow + defaultBorder)
149 return wxSize(78,30);
458ca8c1
MB
150}
151
152wxSize 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;
4d194d63
MB
190}
191
4bb6408c
JS
192void wxButton::Command (wxCommandEvent & event)
193{
194 ProcessCommand (event);
195}
196
f9e02ac7 197void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
02e8b2f9 198{
2d120f83
JS
199 if (!wxGetWindowFromTable(w))
200 // Widget has been deleted!
201 return;
dfe1eee3 202
2d120f83
JS
203 wxButton *item = (wxButton *) clientData;
204 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
205 event.SetEventObject(item);
206 item->ProcessCommand (event);
02e8b2f9 207}