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