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