]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
3cbab641 | 2 | // Name: src/gtk1/control.cpp |
634fb750 | 3 | // Purpose: wxControl implementation for wxGTK |
c801d85f | 4 | // Author: Robert Roebling |
01111366 | 5 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin |
65571936 | 6 | // Licence: wxWindows licence |
c801d85f KB |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
14f355c2 VS |
9 | // For compilers that support precompilation, includes "wx.h". |
10 | #include "wx/wxprec.h" | |
11 | ||
1e6feb95 VZ |
12 | #if wxUSE_CONTROLS |
13 | ||
c801d85f | 14 | #include "wx/control.h" |
e4db172a WS |
15 | |
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/log.h" | |
9eddec69 | 18 | #include "wx/settings.h" |
e4db172a WS |
19 | #endif |
20 | ||
9d522606 | 21 | #include "wx/fontutil.h" |
3cbab641 | 22 | #include "wx/gtk1/private.h" |
034be888 | 23 | |
634fb750 VZ |
24 | // ============================================================================ |
25 | // wxControl implementation | |
26 | // ============================================================================ | |
27 | ||
28 | // ---------------------------------------------------------------------------- | |
29 | // wxControl creation | |
30 | // ---------------------------------------------------------------------------- | |
c801d85f | 31 | |
9abe166a | 32 | IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow) |
c801d85f | 33 | |
31528cd3 | 34 | wxControl::wxControl() |
c801d85f | 35 | { |
9eddec69 | 36 | m_needParent = true; |
6de97a3b | 37 | } |
c801d85f | 38 | |
04165bec | 39 | bool wxControl::Create( wxWindow *parent, |
31528cd3 VZ |
40 | wxWindowID id, |
41 | const wxPoint &pos, | |
42 | const wxSize &size, | |
43 | long style, | |
8d772832 | 44 | const wxValidator& validator, |
04165bec | 45 | const wxString &name ) |
8d772832 | 46 | { |
04165bec | 47 | bool ret = wxWindow::Create(parent, id, pos, size, style, name); |
b2ff89d6 | 48 | |
04165bec | 49 | #if wxUSE_VALIDATORS |
8d772832 | 50 | SetValidator(validator); |
8d772832 RD |
51 | #endif |
52 | ||
04165bec RR |
53 | return ret; |
54 | } | |
55 | ||
f68586e5 VZ |
56 | wxSize wxControl::DoGetBestSize() const |
57 | { | |
0279e844 RR |
58 | // Do not return any arbitrary default value... |
59 | wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") ); | |
60 | ||
f68586e5 | 61 | GtkRequisition req; |
33720b2d RR |
62 | req.width = 2; |
63 | req.height = 2; | |
2afa14f2 | 64 | (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request ) |
f68586e5 VZ |
65 | (m_widget, &req ); |
66 | ||
9f884528 RD |
67 | wxSize best(req.width, req.height); |
68 | CacheBestSize(best); | |
69 | return best; | |
f68586e5 VZ |
70 | } |
71 | ||
abdeb9e7 RD |
72 | |
73 | void wxControl::PostCreation(const wxSize& size) | |
74 | { | |
75 | wxWindow::PostCreation(); | |
f40fdaa3 VS |
76 | |
77 | // NB: GetBestSize needs to know the style, otherwise it will assume | |
78 | // default font and if the user uses a different font, determined | |
79 | // best size will be different (typically, smaller) than the desired | |
80 | // size. This call ensure that a style is available at the time | |
81 | // GetBestSize is called. | |
82 | gtk_widget_ensure_style(m_widget); | |
b2ff89d6 | 83 | |
abdeb9e7 | 84 | ApplyWidgetStyle(); |
170acdc9 | 85 | SetInitialSize(size); |
abdeb9e7 RD |
86 | } |
87 | ||
b2ff89d6 VZ |
88 | // ---------------------------------------------------------------------------- |
89 | // wxControl dealing with labels | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | void wxControl::SetLabel( const wxString &label ) | |
93 | { | |
94 | // keep the original string internally to be able to return it later (for | |
95 | // consistency with the other ports) | |
96 | m_label = label; | |
97 | ||
98 | InvalidateBestSize(); | |
99 | } | |
100 | ||
101 | wxString wxControl::GetLabel() const | |
102 | { | |
103 | return m_label; | |
104 | } | |
105 | ||
106 | void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label) | |
107 | { | |
108 | // don't call the virtual function which might call this one back again | |
109 | wxControl::SetLabel(label); | |
110 | ||
ee6dd41a | 111 | const wxString labelGTK = GTKRemoveMnemonics(label); |
abdeb9e7 | 112 | |
b2ff89d6 | 113 | gtk_label_set(w, wxGTK_CONV(labelGTK)); |
b2ff89d6 VZ |
114 | } |
115 | ||
116 | void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label) | |
117 | { | |
118 | wxControl::SetLabel(label); | |
119 | ||
b2ff89d6 VZ |
120 | const wxString labelGTK = GTKRemoveMnemonics(label); |
121 | ||
2e53e220 | 122 | gtk_frame_set_label(w, labelGTK.empty() ? (const char *)NULL |
b2ff89d6 VZ |
123 | : wxGTK_CONV(labelGTK)); |
124 | } | |
125 | ||
ee6dd41a VZ |
126 | /* static */ |
127 | wxString wxControl::GTKRemoveMnemonics(const wxString& label) | |
b2ff89d6 VZ |
128 | { |
129 | const size_t len = label.length(); | |
130 | wxString labelGTK; | |
131 | labelGTK.reserve(len); | |
132 | for ( size_t i = 0; i < len; i++ ) | |
eaafd2f8 | 133 | { |
b2ff89d6 VZ |
134 | wxChar ch = label[i]; |
135 | ||
9a83f860 | 136 | if ( ch == wxT('&') ) |
eaafd2f8 | 137 | { |
ee6dd41a VZ |
138 | if ( i == len - 1 ) |
139 | { | |
140 | // "&" at the end of string is an error | |
141 | wxLogDebug(wxT("Invalid label \"%s\"."), label.c_str()); | |
b2ff89d6 | 142 | break; |
ee6dd41a VZ |
143 | } |
144 | ||
145 | ch = label[++i]; // skip '&' itself | |
9a83f860 | 146 | if ( ch == wxT('&') ) |
ee6dd41a VZ |
147 | { |
148 | // special case: "&&" is not a mnemonic at all but just an | |
149 | // escaped "&" | |
150 | labelGTK += wxT('&'); | |
151 | continue; | |
152 | } | |
eaafd2f8 | 153 | } |
ee6dd41a VZ |
154 | |
155 | labelGTK += ch; | |
eaafd2f8 | 156 | } |
b2ff89d6 VZ |
157 | |
158 | return labelGTK; | |
159 | } | |
160 | ||
b2ff89d6 VZ |
161 | // ---------------------------------------------------------------------------- |
162 | // wxControl styles (a.k.a. attributes) | |
163 | // ---------------------------------------------------------------------------- | |
9d522606 RD |
164 | |
165 | wxVisualAttributes wxControl::GetDefaultAttributes() const | |
166 | { | |
167 | return GetDefaultAttributesFromGTKWidget(m_widget, | |
168 | UseGTKStyleBase()); | |
169 | } | |
170 | ||
171 | ||
172 | #define SHIFT (8*(sizeof(short int)-sizeof(char))) | |
173 | ||
174 | // static | |
175 | wxVisualAttributes | |
176 | wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget, | |
177 | bool useBase, | |
178 | int state) | |
179 | { | |
180 | GtkStyle* style; | |
181 | wxVisualAttributes attr; | |
182 | ||
183 | style = gtk_rc_get_style(widget); | |
184 | if (!style) | |
185 | style = gtk_widget_get_default_style(); | |
186 | ||
187 | if (!style) | |
188 | { | |
189 | return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL); | |
190 | } | |
191 | ||
192 | if (state == -1) | |
193 | state = GTK_STATE_NORMAL; | |
b2ff89d6 | 194 | |
9d522606 RD |
195 | // get the style's colours |
196 | attr.colFg = wxColour(style->fg[state].red >> SHIFT, | |
197 | style->fg[state].green >> SHIFT, | |
198 | style->fg[state].blue >> SHIFT); | |
199 | if (useBase) | |
200 | attr.colBg = wxColour(style->base[state].red >> SHIFT, | |
201 | style->base[state].green >> SHIFT, | |
202 | style->base[state].blue >> SHIFT); | |
203 | else | |
204 | attr.colBg = wxColour(style->bg[state].red >> SHIFT, | |
205 | style->bg[state].green >> SHIFT, | |
206 | style->bg[state].blue >> SHIFT); | |
207 | ||
208 | // get the style's font | |
9d522606 RD |
209 | // TODO: isn't there a way to get a standard gtk 1.2 font? |
210 | attr.font = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL ); | |
b2ff89d6 | 211 | |
9d522606 RD |
212 | return attr; |
213 | } | |
214 | ||
215 | ||
216 | //static | |
217 | wxVisualAttributes | |
865bb325 | 218 | wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new, |
9d522606 RD |
219 | bool useBase, |
220 | int state) | |
221 | { | |
222 | wxVisualAttributes attr; | |
66d8fe77 VS |
223 | // NB: we need toplevel window so that GTK+ can find the right style |
224 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
9d522606 | 225 | GtkWidget* widget = widget_new(); |
66d8fe77 | 226 | gtk_container_add(GTK_CONTAINER(wnd), widget); |
9d522606 | 227 | attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state); |
66d8fe77 | 228 | gtk_widget_destroy(wnd); |
9d522606 RD |
229 | return attr; |
230 | } | |
231 | ||
232 | //static | |
233 | wxVisualAttributes | |
865bb325 | 234 | wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new, |
9d522606 RD |
235 | bool useBase, |
236 | int state) | |
237 | { | |
238 | wxVisualAttributes attr; | |
66d8fe77 VS |
239 | // NB: we need toplevel window so that GTK+ can find the right style |
240 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
9d522606 | 241 | GtkWidget* widget = widget_new(""); |
66d8fe77 | 242 | gtk_container_add(GTK_CONTAINER(wnd), widget); |
9d522606 | 243 | attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state); |
66d8fe77 | 244 | gtk_widget_destroy(wnd); |
9d522606 RD |
245 | return attr; |
246 | } | |
247 | ||
248 | ||
249 | //static | |
250 | wxVisualAttributes | |
865bb325 | 251 | wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new, |
9d522606 RD |
252 | bool useBase, |
253 | int state) | |
254 | { | |
255 | wxVisualAttributes attr; | |
66d8fe77 VS |
256 | // NB: we need toplevel window so that GTK+ can find the right style |
257 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
9d522606 | 258 | GtkWidget* widget = widget_new(NULL); |
66d8fe77 | 259 | gtk_container_add(GTK_CONTAINER(wnd), widget); |
9d522606 | 260 | attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state); |
66d8fe77 | 261 | gtk_widget_destroy(wnd); |
9d522606 RD |
262 | return attr; |
263 | } | |
264 | ||
1e6feb95 | 265 | #endif // wxUSE_CONTROLS |