1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/control.cpp
3 // Purpose: wxControl implementation for wxGTK
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/control.h"
18 #include "wx/settings.h"
21 #include "wx/fontutil.h"
22 #include "wx/gtk1/private.h"
24 // ============================================================================
25 // wxControl implementation
26 // ============================================================================
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
34 wxControl::wxControl()
39 bool wxControl::Create( wxWindow
*parent
,
44 const wxValidator
& validator
,
45 const wxString
&name
)
47 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
50 SetValidator(validator
);
56 wxSize
wxControl::DoGetBestSize() const
58 // Do not return any arbitrary default value...
59 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
64 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
67 wxSize
best(req
.width
, req
.height
);
73 void wxControl::PostCreation(const wxSize
& size
)
75 wxWindow::PostCreation();
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
);
88 // ----------------------------------------------------------------------------
89 // wxControl dealing with labels
90 // ----------------------------------------------------------------------------
92 void wxControl::SetLabel( const wxString
&label
)
94 // keep the original string internally to be able to return it later (for
95 // consistency with the other ports)
101 wxString
wxControl::GetLabel() const
106 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
108 // don't call the virtual function which might call this one back again
109 wxControl::SetLabel(label
);
111 const wxString labelGTK
= GTKRemoveMnemonics(label
);
113 gtk_label_set(w
, wxGTK_CONV(labelGTK
));
116 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
118 wxControl::SetLabel(label
);
120 const wxString labelGTK
= GTKRemoveMnemonics(label
);
122 gtk_frame_set_label(w
, labelGTK
.empty() ? (const char *)NULL
123 : wxGTK_CONV(labelGTK
));
127 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
129 const size_t len
= label
.length();
131 labelGTK
.reserve(len
);
132 for ( size_t i
= 0; i
< len
; i
++ )
134 wxChar ch
= label
[i
];
136 if ( ch
== wxT('&') )
140 // "&" at the end of string is an error
141 wxLogDebug(wxT("Invalid label \"%s\"."), label
.c_str());
145 ch
= label
[++i
]; // skip '&' itself
146 if ( ch
== wxT('&') )
148 // special case: "&&" is not a mnemonic at all but just an
150 labelGTK
+= wxT('&');
161 // ----------------------------------------------------------------------------
162 // wxControl styles (a.k.a. attributes)
163 // ----------------------------------------------------------------------------
165 wxVisualAttributes
wxControl::GetDefaultAttributes() const
167 return GetDefaultAttributesFromGTKWidget(m_widget
,
172 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
176 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
181 wxVisualAttributes attr
;
183 style
= gtk_rc_get_style(widget
);
185 style
= gtk_widget_get_default_style();
189 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
193 state
= GTK_STATE_NORMAL
;
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
);
200 attr
.colBg
= wxColour(style
->base
[state
].red
>> SHIFT
,
201 style
->base
[state
].green
>> SHIFT
,
202 style
->base
[state
].blue
>> SHIFT
);
204 attr
.colBg
= wxColour(style
->bg
[state
].red
>> SHIFT
,
205 style
->bg
[state
].green
>> SHIFT
,
206 style
->bg
[state
].blue
>> SHIFT
);
208 // get the style's font
209 // TODO: isn't there a way to get a standard gtk 1.2 font?
210 attr
.font
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
218 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
222 wxVisualAttributes attr
;
223 // NB: we need toplevel window so that GTK+ can find the right style
224 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
225 GtkWidget
* widget
= widget_new();
226 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
227 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
228 gtk_widget_destroy(wnd
);
234 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
238 wxVisualAttributes attr
;
239 // NB: we need toplevel window so that GTK+ can find the right style
240 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
241 GtkWidget
* widget
= widget_new("");
242 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
243 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
244 gtk_widget_destroy(wnd
);
251 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
255 wxVisualAttributes attr
;
256 // NB: we need toplevel window so that GTK+ can find the right style
257 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
258 GtkWidget
* widget
= widget_new(NULL
);
259 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
260 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
261 gtk_widget_destroy(wnd
);
265 #endif // wxUSE_CONTROLS