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