1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/control.cpp
3 // Purpose: wxControl implementation for wxGTK
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/control.h"
19 #include "wx/settings.h"
22 #include "wx/fontutil.h"
23 #include "wx/gtk1/private.h"
25 // ============================================================================
26 // wxControl implementation
27 // ============================================================================
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
35 wxControl::wxControl()
40 bool wxControl::Create( wxWindow
*parent
,
45 const wxValidator
& validator
,
46 const wxString
&name
)
48 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
51 SetValidator(validator
);
57 wxSize
wxControl::DoGetBestSize() const
59 // Do not return any arbitrary default value...
60 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
65 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
68 wxSize
best(req
.width
, req
.height
);
74 void wxControl::PostCreation(const wxSize
& size
)
76 wxWindow::PostCreation();
78 // NB: GetBestSize needs to know the style, otherwise it will assume
79 // default font and if the user uses a different font, determined
80 // best size will be different (typically, smaller) than the desired
81 // size. This call ensure that a style is available at the time
82 // GetBestSize is called.
83 gtk_widget_ensure_style(m_widget
);
86 SetInitialBestSize(size
);
89 // ----------------------------------------------------------------------------
90 // wxControl dealing with labels
91 // ----------------------------------------------------------------------------
93 void wxControl::SetLabel( const wxString
&label
)
95 // keep the original string internally to be able to return it later (for
96 // consistency with the other ports)
102 wxString
wxControl::GetLabel() const
107 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
109 // don't call the virtual function which might call this one back again
110 wxControl::SetLabel(label
);
112 const wxString labelGTK
= GTKRemoveMnemonics(label
);
114 gtk_label_set(w
, wxGTK_CONV(labelGTK
));
117 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
119 wxControl::SetLabel(label
);
121 const wxString labelGTK
= GTKRemoveMnemonics(label
);
123 gtk_frame_set_label(w
, labelGTK
.empty() ? (char *)NULL
124 : wxGTK_CONV(labelGTK
));
128 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
130 const size_t len
= label
.length();
132 labelGTK
.reserve(len
);
133 for ( size_t i
= 0; i
< len
; i
++ )
135 wxChar ch
= label
[i
];
141 // "&" at the end of string is an error
142 wxLogDebug(wxT("Invalid label \"%s\"."), label
.c_str());
146 ch
= label
[++i
]; // skip '&' itself
149 // special case: "&&" is not a mnemonic at all but just an
151 labelGTK
+= wxT('&');
162 // ----------------------------------------------------------------------------
163 // wxControl styles (a.k.a. attributes)
164 // ----------------------------------------------------------------------------
166 wxVisualAttributes
wxControl::GetDefaultAttributes() const
168 return GetDefaultAttributesFromGTKWidget(m_widget
,
173 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
177 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
182 wxVisualAttributes attr
;
184 style
= gtk_rc_get_style(widget
);
186 style
= gtk_widget_get_default_style();
190 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
194 state
= GTK_STATE_NORMAL
;
196 // get the style's colours
197 attr
.colFg
= wxColour(style
->fg
[state
].red
>> SHIFT
,
198 style
->fg
[state
].green
>> SHIFT
,
199 style
->fg
[state
].blue
>> SHIFT
);
201 attr
.colBg
= wxColour(style
->base
[state
].red
>> SHIFT
,
202 style
->base
[state
].green
>> SHIFT
,
203 style
->base
[state
].blue
>> SHIFT
);
205 attr
.colBg
= wxColour(style
->bg
[state
].red
>> SHIFT
,
206 style
->bg
[state
].green
>> SHIFT
,
207 style
->bg
[state
].blue
>> SHIFT
);
209 // get the style's font
210 // TODO: isn't there a way to get a standard gtk 1.2 font?
211 attr
.font
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
219 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
223 wxVisualAttributes attr
;
224 // NB: we need toplevel window so that GTK+ can find the right style
225 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
226 GtkWidget
* widget
= widget_new();
227 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
228 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
229 gtk_widget_destroy(wnd
);
235 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
239 wxVisualAttributes attr
;
240 // NB: we need toplevel window so that GTK+ can find the right style
241 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
242 GtkWidget
* widget
= widget_new("");
243 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
244 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
245 gtk_widget_destroy(wnd
);
252 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
256 wxVisualAttributes attr
;
257 // NB: we need toplevel window so that GTK+ can find the right style
258 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
259 GtkWidget
* widget
= widget_new(NULL
);
260 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
261 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
262 gtk_widget_destroy(wnd
);
266 #endif // wxUSE_CONTROLS