Committing in .
[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 #include "wx/button.h"
17 #include "wx/utils.h"
18 #include "wx/panel.h"
19
20 #ifdef __VMS__
21 #pragma message disable nosimpint
22 #endif
23 #include <Xm/PushBG.h>
24 #include <Xm/PushB.h>
25 #ifdef __VMS__
26 #pragma message enable nosimpint
27 #endif
28
29 #include "wx/motif/private.h"
30
31 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
32
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
35 #endif
36
37 // Button
38
39 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
40 const wxPoint& pos,
41 const wxSize& size, long style,
42 const wxValidator& validator,
43 const wxString& name)
44 {
45 SetName(name);
46 SetValidator(validator);
47 m_windowStyle = style;
48 m_backgroundColour = parent->GetBackgroundColour();
49 m_foregroundColour = parent->GetForegroundColour();
50 m_font = parent->GetFont();
51
52 parent->AddChild((wxButton *)this);
53
54 if (id == -1)
55 m_windowId = NewControlId();
56 else
57 m_windowId = id;
58
59 wxString label1(wxStripMenuCodes(label));
60
61 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
62 Widget parentWidget = (Widget) parent->GetClientWidget();
63
64 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
65
66 /*
67 * Patch Note (important)
68 * There is no major reason to put a defaultButtonThickness here.
69 * Not requesting it give the ability to put wxButton with a spacing
70 * as small as requested. However, if some button become a DefaultButton,
71 * other buttons are no more aligned -- This is why we set
72 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
73 * in the ::SetDefaultButton method.
74 */
75 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
76 xmPushButtonWidgetClass,
77 parentWidget,
78 XmNfontList, fontList,
79 XmNlabelString, text,
80 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
81 NULL);
82
83 XmStringFree (text);
84
85 XtAddCallback ((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
86 (XtPointer) this);
87
88 SetCanAddEventHandler(TRUE);
89
90 int x = 0; int y = 0;
91 wxFont new_font( parent->GetFont() );
92 GetTextExtent( label1, &x, &y, (int*)NULL, (int*)NULL, &new_font );
93
94 wxSize newSize = size;
95 if (newSize.x == -1) newSize.x = 30+x;
96 if (newSize.y == -1) newSize.y = 27+y;
97 SetSize( newSize.x, newSize.y );
98
99 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, newSize.x, newSize.y);
100
101 ChangeBackgroundColour();
102
103 return TRUE;
104 }
105
106 void wxButton::SetDefault()
107 {
108 wxWindow *parent = GetParent();
109 wxPanel *panel = wxDynamicCast(parent, wxPanel);
110 if ( panel )
111 panel->SetDefaultItem(this);
112
113 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
114 // Unfortunately, buttons are now mis-aligned. We try to correct this
115 // now -- setting this ressource to 1 for each button in the same row.
116 // Because it's very hard to find wxButton in the same row,
117 // correction is straighforward: we set resource for all wxButton
118 // in this parent (but not sub panels)
119 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
120 {
121 wxButton *item = (wxButton *) node->Data ();
122 if (item->IsKindOf(CLASSINFO(wxButton)))
123 {
124 bool managed = XtIsManaged((Widget) item->GetMainWidget());
125 if (managed)
126 XtUnmanageChild ((Widget) item->GetMainWidget());
127
128 XtVaSetValues ((Widget) item->GetMainWidget(),
129 XmNdefaultButtonShadowThickness, 1,
130 NULL);
131
132 if (managed)
133 XtManageChild ((Widget) item->GetMainWidget());
134 }
135 } // while
136
137 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
138 XtVaSetValues ((Widget) parent->GetMainWidget(), XmNdefaultButton, (Widget) GetMainWidget(), NULL);
139 }
140
141 /* static */
142 wxSize wxButton::GetDefaultSize()
143 {
144 // TODO: check font size as in wxMSW ? MB
145 //
146 return wxSize(80,26);
147 }
148
149 void wxButton::Command (wxCommandEvent & event)
150 {
151 ProcessCommand (event);
152 }
153
154 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
155 {
156 if (!wxGetWindowFromTable(w))
157 // Widget has been deleted!
158 return;
159
160 wxButton *item = (wxButton *) clientData;
161 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
162 event.SetEventObject(item);
163 item->ProcessCommand (event);
164 }
165
166 void wxButton::ChangeFont(bool keepOriginalSize)
167 {
168 wxWindow::ChangeFont(keepOriginalSize);
169 }
170
171 void wxButton::ChangeBackgroundColour()
172 {
173 DoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE);
174 }
175
176 void wxButton::ChangeForegroundColour()
177 {
178 wxWindow::ChangeForegroundColour();
179 }
180