1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "button.h"
16 #include "wx/button.h"
21 #pragma message disable nosimpint
23 #include <Xm/PushBG.h>
26 #pragma message enable nosimpint
29 #include "wx/motif/private.h"
31 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
33 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
37 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
39 const wxSize
& size
, long style
,
40 const wxValidator
& validator
,
44 SetValidator(validator
);
45 m_windowStyle
= style
;
46 m_backgroundColour
= parent
->GetBackgroundColour();
47 m_foregroundColour
= parent
->GetForegroundColour();
48 m_font
= parent
->GetFont();
50 parent
->AddChild((wxButton
*)this);
53 m_windowId
= NewControlId();
57 wxString
label1(wxStripMenuCodes(label
));
59 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
60 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
62 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
65 * Patch Note (important)
66 * There is no major reason to put a defaultButtonThickness here.
67 * Not requesting it give the ability to put wxButton with a spacing
68 * as small as requested. However, if some button become a DefaultButton,
69 * other buttons are no more aligned -- This is why we set
70 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
71 * in the ::SetDefaultButton method.
73 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
74 xmPushButtonWidgetClass
,
76 XmNfontList
, fontList
,
78 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
83 XtAddCallback ((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
86 SetCanAddEventHandler(TRUE
);
89 wxFont
new_font( parent
->GetFont() );
90 GetTextExtent( label1
, &x
, &y
, (int*)NULL
, (int*)NULL
, &new_font
);
92 wxSize newSize
= size
;
93 if (newSize
.x
== -1) newSize
.x
= 30+x
;
94 if (newSize
.y
== -1) newSize
.y
= 27+y
;
95 SetSize( newSize
.x
, newSize
.y
);
97 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, newSize
.x
, newSize
.y
);
99 ChangeBackgroundColour();
104 void wxButton::SetDefault()
106 wxWindow
*parent
= GetParent();
107 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
109 panel
->SetDefaultItem(this);
111 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
112 // Unfortunately, buttons are now mis-aligned. We try to correct this
113 // now -- setting this ressource to 1 for each button in the same row.
114 // Because it's very hard to find wxButton in the same row,
115 // correction is straighforward: we set resource for all wxButton
116 // in this parent (but not sub panels)
117 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
119 wxButton
*item
= (wxButton
*) node
->Data ();
120 if (item
->IsKindOf(CLASSINFO(wxButton
)))
122 bool managed
= XtIsManaged((Widget
) item
->GetMainWidget());
124 XtUnmanageChild ((Widget
) item
->GetMainWidget());
126 XtVaSetValues ((Widget
) item
->GetMainWidget(),
127 XmNdefaultButtonShadowThickness
, 1,
131 XtManageChild ((Widget
) item
->GetMainWidget());
135 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
136 XtVaSetValues ((Widget
) parent
->GetMainWidget(), XmNdefaultButton
, (Widget
) GetMainWidget(), NULL
);
140 wxSize
wxButton::GetDefaultSize()
142 // TODO: check font size as in wxMSW ? MB
144 return wxSize(80,26);
147 void wxButton::Command (wxCommandEvent
& event
)
149 ProcessCommand (event
);
152 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
154 if (!wxGetWindowFromTable(w
))
155 // Widget has been deleted!
158 wxButton
*item
= (wxButton
*) clientData
;
159 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
160 event
.SetEventObject(item
);
161 item
->ProcessCommand (event
);
164 void wxButton::ChangeFont(bool keepOriginalSize
)
166 wxWindow::ChangeFont(keepOriginalSize
);
169 void wxButton::ChangeBackgroundColour()
171 DoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, TRUE
);
174 void wxButton::ChangeForegroundColour()
176 wxWindow::ChangeForegroundColour();