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"
16 #include "wx/control.h"
17 #include "wx/fontutil.h"
18 #include "wx/settings.h"
19 #include "wx/gtk1/private.h"
21 // ============================================================================
22 // wxControl implementation
23 // ============================================================================
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
31 wxControl::wxControl()
36 bool wxControl::Create( wxWindow
*parent
,
41 const wxValidator
& validator
,
42 const wxString
&name
)
44 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
47 SetValidator(validator
);
53 wxSize
wxControl::DoGetBestSize() const
55 // Do not return any arbitrary default value...
56 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
61 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
64 wxSize
best(req
.width
, req
.height
);
70 void wxControl::PostCreation(const wxSize
& size
)
72 wxWindow::PostCreation();
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
);
82 SetInitialBestSize(size
);
85 // ----------------------------------------------------------------------------
86 // wxControl dealing with labels
87 // ----------------------------------------------------------------------------
89 void wxControl::SetLabel( const wxString
&label
)
91 // keep the original string internally to be able to return it later (for
92 // consistency with the other ports)
98 wxString
wxControl::GetLabel() const
103 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
105 // don't call the virtual function which might call this one back again
106 wxControl::SetLabel(label
);
108 const wxString labelGTK
= GTKRemoveMnemonics(label
);
110 gtk_label_set(w
, wxGTK_CONV(labelGTK
));
113 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
115 wxControl::SetLabel(label
);
117 const wxString labelGTK
= GTKRemoveMnemonics(label
);
119 gtk_frame_set_label(w
, labelGTK
.empty() ? (char *)NULL
120 : wxGTK_CONV(labelGTK
));
124 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
126 const size_t len
= label
.length();
128 labelGTK
.reserve(len
);
129 for ( size_t i
= 0; i
< len
; i
++ )
131 wxChar ch
= label
[i
];
137 // "&" at the end of string is an error
138 wxLogDebug(wxT("Invalid label \"%s\"."), label
.c_str());
142 ch
= label
[++i
]; // skip '&' itself
145 // special case: "&&" is not a mnemonic at all but just an
147 labelGTK
+= wxT('&');
158 // ----------------------------------------------------------------------------
159 // wxControl styles (a.k.a. attributes)
160 // ----------------------------------------------------------------------------
162 wxVisualAttributes
wxControl::GetDefaultAttributes() const
164 return GetDefaultAttributesFromGTKWidget(m_widget
,
169 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
173 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
178 wxVisualAttributes attr
;
180 style
= gtk_rc_get_style(widget
);
182 style
= gtk_widget_get_default_style();
186 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
190 state
= GTK_STATE_NORMAL
;
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
);
197 attr
.colBg
= wxColour(style
->base
[state
].red
>> SHIFT
,
198 style
->base
[state
].green
>> SHIFT
,
199 style
->base
[state
].blue
>> SHIFT
);
201 attr
.colBg
= wxColour(style
->bg
[state
].red
>> SHIFT
,
202 style
->bg
[state
].green
>> SHIFT
,
203 style
->bg
[state
].blue
>> SHIFT
);
205 // get the style's font
206 // TODO: isn't there a way to get a standard gtk 1.2 font?
207 attr
.font
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
215 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
219 wxVisualAttributes attr
;
220 // NB: we need toplevel window so that GTK+ can find the right style
221 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
222 GtkWidget
* widget
= widget_new();
223 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
224 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
225 gtk_widget_destroy(wnd
);
231 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
235 wxVisualAttributes attr
;
236 // NB: we need toplevel window so that GTK+ can find the right style
237 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
238 GtkWidget
* widget
= widget_new("");
239 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
240 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
241 gtk_widget_destroy(wnd
);
248 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
252 wxVisualAttributes attr
;
253 // NB: we need toplevel window so that GTK+ can find the right style
254 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
255 GtkWidget
* widget
= widget_new(NULL
);
256 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
257 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
258 gtk_widget_destroy(wnd
);
262 #endif // wxUSE_CONTROLS