]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
Added wxHelpControllerHtml, derived from wxHtmlHelpControllerBase. API like
[wxWidgets.git] / src / gtk1 / choice.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: choice.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
29006414 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "choice.h"
13#endif
14
15#include "wx/choice.h"
16
ce4169a4
RR
17#if wxUSE_CHOICE
18
83624f79
RR
19#include "gdk/gdk.h"
20#include "gtk/gtk.h"
21
acfd422a
RR
22//-----------------------------------------------------------------------------
23// idle system
24//-----------------------------------------------------------------------------
25
26extern void wxapp_install_idle_handler();
27extern bool g_isIdle;
28
66bd6b93
RR
29//-----------------------------------------------------------------------------
30// data
31//-----------------------------------------------------------------------------
32
33extern bool g_blockEventsOnDrag;
34
c801d85f 35//-----------------------------------------------------------------------------
e1e955e1 36// "activate"
c801d85f
KB
37//-----------------------------------------------------------------------------
38
66bd6b93 39static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 40{
acfd422a
RR
41 if (g_isIdle) wxapp_install_idle_handler();
42
a2053b27 43 if (!choice->m_hasVMT) return;
29006414 44
acfd422a 45 if (g_blockEventsOnDrag) return;
29006414 46
acfd422a
RR
47 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
48 event.SetInt( choice->GetSelection() );
49 event.SetString( choice->GetStringSelection() );
50 event.SetEventObject(choice);
51 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 52}
c801d85f 53
e1e955e1
RR
54//-----------------------------------------------------------------------------
55// wxChoice
c801d85f
KB
56//-----------------------------------------------------------------------------
57
7f4dc78d 58IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 59
fd0eed64 60wxChoice::wxChoice()
c801d85f 61{
6de97a3b 62}
c801d85f 63
debe6624 64bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
65 const wxPoint &pos, const wxSize &size,
66 int n, const wxString choices[],
67 long style, const wxValidator& validator, const wxString &name )
c801d85f 68{
fd0eed64 69 m_needParent = TRUE;
034be888
RR
70#if (GTK_MINOR_VERSION > 0)
71 m_acceptsFocus = TRUE;
72#endif
29006414 73
fd0eed64 74 PreCreation( parent, id, pos, size, style, name );
29006414 75
ce4169a4 76#if wxUSE_VALIDATORS
fd0eed64 77 SetValidator( validator );
ce4169a4 78#endif
6de97a3b 79
fd0eed64 80 m_widget = gtk_option_menu_new();
29006414
VZ
81
82 wxSize newSize(size);
83 if (newSize.x == -1)
84 newSize.x = 80;
85 if (newSize.y == -1)
86 newSize.y = 26;
fd0eed64 87 SetSize( newSize.x, newSize.y );
29006414 88
fd0eed64 89 GtkWidget *menu = gtk_menu_new();
29006414 90
fd0eed64
RR
91 for (int i = 0; i < n; i++)
92 {
93 m_clientDataList.Append( (wxObject*) NULL );
f5e27805 94 m_clientObjectList.Append( (wxObject*) NULL );
29006414 95
93c5dd39 96 GtkWidget *item = gtk_menu_item_new_with_label( choices[i].mbc_str() );
fd0eed64 97 gtk_menu_append( GTK_MENU(menu), item );
29006414 98
fd0eed64 99 gtk_widget_show( item );
29006414
VZ
100
101 gtk_signal_connect( GTK_OBJECT( item ), "activate",
fd0eed64
RR
102 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
103 }
104 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 105
f03fc89f 106 m_parent->DoAddChild( this );
29006414 107
fd0eed64 108 PostCreation();
29006414 109
fd0eed64
RR
110 SetBackgroundColour( parent->GetBackgroundColour() );
111 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 112 SetFont( parent->GetFont() );
f96aa4d9 113
fd0eed64 114 Show( TRUE );
29006414 115
fd0eed64 116 return TRUE;
6de97a3b 117}
29006414 118
fd0eed64
RR
119wxChoice::~wxChoice()
120{
f5e27805 121 Clear();
fd0eed64
RR
122}
123
124void wxChoice::AppendCommon( const wxString &item )
125{
93c5dd39 126 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
29006414 127
fd0eed64 128 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
93c5dd39 129 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
29006414 130
fd0eed64 131 gtk_menu_append( GTK_MENU(menu), menu_item );
29006414 132
2b07d713
RR
133 if (GTK_WIDGET_REALIZED(m_widget))
134 {
135 gtk_widget_realize( menu_item );
136 gtk_widget_realize( GTK_BIN(menu_item)->child );
29006414 137
2b07d713
RR
138 if (m_widgetStyle) ApplyWidgetStyle();
139 }
29006414
VZ
140
141 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
fd0eed64 142 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
29006414 143
fd0eed64
RR
144 gtk_widget_show( menu_item );
145}
146
c801d85f
KB
147void wxChoice::Append( const wxString &item )
148{
f5e27805
RR
149 m_clientDataList.Append( (wxObject*) NULL );
150 m_clientObjectList.Append( (wxObject*) NULL );
29006414 151
fd0eed64
RR
152 AppendCommon( item );
153}
154
155void wxChoice::Append( const wxString &item, void *clientData )
156{
f5e27805
RR
157 m_clientDataList.Append( (wxObject*) clientData );
158 m_clientObjectList.Append( (wxObject*) NULL );
29006414 159
fd0eed64
RR
160 AppendCommon( item );
161}
162
163void wxChoice::Append( const wxString &item, wxClientData *clientData )
164{
f5e27805
RR
165 m_clientObjectList.Append( (wxObject*) clientData );
166 m_clientDataList.Append( (wxObject*) NULL );
29006414 167
fd0eed64
RR
168 AppendCommon( item );
169}
f96aa4d9 170
fd0eed64
RR
171void wxChoice::SetClientData( int n, void* clientData )
172{
93c5dd39 173 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
29006414 174
fd0eed64
RR
175 wxNode *node = m_clientDataList.Nth( n );
176 if (!node) return;
29006414 177
f5e27805 178 node->SetData( (wxObject*) clientData );
fd0eed64
RR
179}
180
181void* wxChoice::GetClientData( int n )
182{
93c5dd39 183 wxCHECK_MSG( m_widget != NULL, NULL, _T("invalid combobox") );
29006414 184
fd0eed64
RR
185 wxNode *node = m_clientDataList.Nth( n );
186 if (!node) return NULL;
29006414 187
f5e27805 188 return node->Data();
6de97a3b 189}
fd0eed64
RR
190
191void wxChoice::SetClientObject( int n, wxClientData* clientData )
192{
93c5dd39 193 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
29006414 194
f5e27805 195 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64 196 if (!node) return;
29006414 197
fd0eed64
RR
198 wxClientData *cd = (wxClientData*) node->Data();
199 if (cd) delete cd;
29006414 200
fd0eed64
RR
201 node->SetData( (wxObject*) clientData );
202}
203
204wxClientData* wxChoice::GetClientObject( int n )
205{
93c5dd39 206 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, _T("invalid combobox") );
29006414 207
f5e27805 208 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64 209 if (!node) return (wxClientData*) NULL;
29006414 210
fd0eed64
RR
211 return (wxClientData*) node->Data();
212}
29006414 213
fd0eed64 214void wxChoice::Clear()
c801d85f 215{
93c5dd39 216 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 217
fd0eed64
RR
218 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
219 GtkWidget *menu = gtk_menu_new();
220 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 221
f5e27805 222 wxNode *node = m_clientObjectList.First();
fd0eed64
RR
223 while (node)
224 {
225 wxClientData *cd = (wxClientData*)node->Data();
226 if (cd) delete cd;
227 node = node->Next();
228 }
f5e27805 229 m_clientObjectList.Clear();
29006414 230
fd0eed64 231 m_clientDataList.Clear();
6de97a3b 232}
c801d85f 233
2f6407b9
RR
234void wxChoice::Delete( int WXUNUSED(n) )
235{
93c5dd39 236 wxFAIL_MSG( _T("wxChoice:Delete not implemented") );
2f6407b9
RR
237}
238
c801d85f
KB
239int wxChoice::FindString( const wxString &string ) const
240{
93c5dd39 241 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid choice") );
fd0eed64
RR
242
243 // If you read this code once and you think you understand
244 // it, then you are very wrong. Robert Roebling.
29006414 245
fd0eed64
RR
246 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
247 int count = 0;
248 GList *child = menu_shell->children;
249 while (child)
250 {
251 GtkBin *bin = GTK_BIN( child->data );
252 GtkLabel *label = (GtkLabel *) NULL;
253 if (bin->child) label = GTK_LABEL(bin->child);
254 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 255
93c5dd39 256 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 257
dcf924a3 258 if (string == wxString(label->label,*wxConvCurrent))
29006414
VZ
259 return count;
260
fd0eed64
RR
261 child = child->next;
262 count++;
263 }
29006414 264
fd0eed64 265 return -1;
6de97a3b 266}
c801d85f 267
fd0eed64 268int wxChoice::GetColumns() const
c801d85f 269{
fd0eed64 270 return 1;
6de97a3b 271}
c801d85f 272
fd0eed64 273int wxChoice::GetSelection()
c801d85f 274{
93c5dd39 275 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid choice") );
fd0eed64
RR
276
277 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
278 int count = 0;
279 GList *child = menu_shell->children;
280 while (child)
281 {
282 GtkBin *bin = GTK_BIN( child->data );
283 if (!bin->child) return count;
284 child = child->next;
285 count++;
286 }
29006414 287
93c5dd39 288 wxFAIL_MSG( _T("wxChoice: no selection") );
29006414 289
fd0eed64 290 return -1;
6de97a3b 291}
c801d85f 292
debe6624 293wxString wxChoice::GetString( int n ) const
c801d85f 294{
93c5dd39 295 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
fd0eed64
RR
296
297 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
298 int count = 0;
299 GList *child = menu_shell->children;
300 while (child)
c801d85f 301 {
fd0eed64
RR
302 GtkBin *bin = GTK_BIN( child->data );
303 if (count == n)
304 {
305 GtkLabel *label = (GtkLabel *) NULL;
306 if (bin->child) label = GTK_LABEL(bin->child);
307 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 308
93c5dd39 309 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 310
dcf924a3 311 return wxString(label->label,*wxConvCurrent);
fd0eed64
RR
312 }
313 child = child->next;
314 count++;
6de97a3b 315 }
29006414 316
93c5dd39 317 wxFAIL_MSG( _T("wxChoice: invalid index in GetString()") );
29006414 318
002f4218 319 return _T("");
6de97a3b 320}
c801d85f 321
fd0eed64 322wxString wxChoice::GetStringSelection() const
c801d85f 323{
93c5dd39 324 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
f96aa4d9 325
fd0eed64 326 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 327
93c5dd39 328 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 329
dcf924a3 330 return wxString(label->label,*wxConvCurrent);
6de97a3b 331}
c801d85f 332
fd0eed64 333int wxChoice::Number() const
c801d85f 334{
93c5dd39 335 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid choice") );
fd0eed64
RR
336
337 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
338 int count = 0;
339 GList *child = menu_shell->children;
340 while (child)
341 {
342 count++;
343 child = child->next;
344 }
345 return count;
6de97a3b 346}
c801d85f 347
debe6624 348void wxChoice::SetColumns( int WXUNUSED(n) )
c801d85f 349{
6de97a3b 350}
c801d85f 351
debe6624 352void wxChoice::SetSelection( int n )
c801d85f 353{
93c5dd39 354 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 355
fd0eed64
RR
356 int tmp = n;
357 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
29006414 358
fd0eed64 359 gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
6de97a3b 360}
c801d85f
KB
361
362void wxChoice::SetStringSelection( const wxString &string )
363{
93c5dd39 364 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 365
fd0eed64
RR
366 int n = FindString( string );
367 if (n != -1) SetSelection( n );
6de97a3b 368}
c801d85f 369
58614078 370void wxChoice::ApplyWidgetStyle()
868a2826 371{
fd0eed64 372 SetWidgetStyle();
29006414 373
fd0eed64 374 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 375
fd0eed64
RR
376 gtk_widget_set_style( m_widget, m_widgetStyle );
377 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 378
fd0eed64
RR
379 GList *child = menu_shell->children;
380 while (child)
381 {
382 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 383
fd0eed64
RR
384 GtkBin *bin = GTK_BIN( child->data );
385 GtkWidget *label = (GtkWidget *) NULL;
386 if (bin->child) label = bin->child;
387 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 388
fd0eed64 389 gtk_widget_set_style( label, m_widgetStyle );
29006414 390
fd0eed64
RR
391 child = child->next;
392 }
f96aa4d9
RR
393}
394
ce4169a4 395#endif