1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "button.h"
17 #define XtDisplay XTDISPLAY
20 #include "wx/button.h"
25 #pragma message disable nosimpint
27 #include <Xm/PushBG.h>
30 #pragma message enable nosimpint
33 #include "wx/motif/private.h"
35 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
37 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
41 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
43 const wxSize
& size
, long style
,
44 const wxValidator
& validator
,
48 SetValidator(validator
);
49 m_windowStyle
= style
;
50 m_backgroundColour
= parent
->GetBackgroundColour();
51 m_foregroundColour
= parent
->GetForegroundColour();
52 m_font
= parent
->GetFont();
54 parent
->AddChild((wxButton
*)this);
57 m_windowId
= NewControlId();
61 wxString
label1(wxStripMenuCodes(label
));
63 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
64 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
66 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
69 * Patch Note (important)
70 * There is no major reason to put a defaultButtonThickness here.
71 * Not requesting it give the ability to put wxButton with a spacing
72 * as small as requested. However, if some button become a DefaultButton,
73 * other buttons are no more aligned -- This is why we set
74 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
75 * in the ::SetDefaultButton method.
77 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
78 xmPushButtonWidgetClass
,
80 XmNfontList
, fontList
,
82 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
87 XtAddCallback ((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
90 SetCanAddEventHandler(TRUE
);
93 wxFont
new_font( parent
->GetFont() );
94 GetTextExtent( label1
, &x
, &y
, (int*)NULL
, (int*)NULL
, &new_font
);
96 wxSize newSize
= size
;
97 if (newSize
.x
== -1) newSize
.x
= 30+x
;
98 if (newSize
.y
== -1) newSize
.y
= 27+y
;
99 SetSize( newSize
.x
, newSize
.y
);
101 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, newSize
.x
, newSize
.y
);
103 ChangeBackgroundColour();
108 void wxButton::SetDefault()
110 wxWindow
*parent
= GetParent();
111 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
113 panel
->SetDefaultItem(this);
115 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
116 // Unfortunately, buttons are now mis-aligned. We try to correct this
117 // now -- setting this ressource to 1 for each button in the same row.
118 // Because it's very hard to find wxButton in the same row,
119 // correction is straighforward: we set resource for all wxButton
120 // in this parent (but not sub panels)
121 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
123 wxButton
*item
= (wxButton
*) node
->Data ();
124 if (item
->IsKindOf(CLASSINFO(wxButton
)))
126 bool managed
= XtIsManaged((Widget
) item
->GetMainWidget());
128 XtUnmanageChild ((Widget
) item
->GetMainWidget());
130 XtVaSetValues ((Widget
) item
->GetMainWidget(),
131 XmNdefaultButtonShadowThickness
, 1,
135 XtManageChild ((Widget
) item
->GetMainWidget());
139 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
140 XtVaSetValues ((Widget
) parent
->GetMainWidget(), XmNdefaultButton
, (Widget
) GetMainWidget(), NULL
);
144 wxSize
wxButton::GetDefaultSize()
146 // TODO: check font size as in wxMSW ? MB
148 return wxSize(80,26);
151 void wxButton::Command (wxCommandEvent
& event
)
153 ProcessCommand (event
);
156 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
158 if (!wxGetWindowFromTable(w
))
159 // Widget has been deleted!
162 wxButton
*item
= (wxButton
*) clientData
;
163 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
164 event
.SetEventObject(item
);
165 item
->ProcessCommand (event
);
168 void wxButton::ChangeFont(bool keepOriginalSize
)
170 wxWindow::ChangeFont(keepOriginalSize
);
173 void wxButton::ChangeBackgroundColour()
175 DoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, TRUE
);
178 void wxButton::ChangeForegroundColour()
180 wxWindow::ChangeForegroundColour();