]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
implemented wxActivateEvent properly
[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
1e6feb95 15#include "wx/defs.h"
c801d85f 16
ce4169a4
RR
17#if wxUSE_CHOICE
18
1e6feb95
VZ
19#include "wx/choice.h"
20
071a2d78
RR
21#include <gdk/gdk.h>
22#include <gtk/gtk.h>
83624f79 23
acfd422a
RR
24//-----------------------------------------------------------------------------
25// idle system
26//-----------------------------------------------------------------------------
27
28extern void wxapp_install_idle_handler();
29extern bool g_isIdle;
30
66bd6b93
RR
31//-----------------------------------------------------------------------------
32// data
33//-----------------------------------------------------------------------------
34
35extern bool g_blockEventsOnDrag;
36
c801d85f 37//-----------------------------------------------------------------------------
e1e955e1 38// "activate"
c801d85f
KB
39//-----------------------------------------------------------------------------
40
66bd6b93 41static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 42{
e01c8145 43 if (g_isIdle)
4dcaf11a 44 wxapp_install_idle_handler();
acfd422a 45
a2053b27 46 if (!choice->m_hasVMT) return;
29006414 47
acfd422a 48 if (g_blockEventsOnDrag) return;
29006414 49
acfd422a 50 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
6c8a980f
VZ
51 int n = choice->GetSelection();
52
53 event.SetInt( n );
acfd422a
RR
54 event.SetString( choice->GetStringSelection() );
55 event.SetEventObject(choice);
6c8a980f
VZ
56
57 if ( choice->HasClientObjectData() )
58 event.SetClientObject( choice->GetClientObject(n) );
59 else if ( choice->HasClientUntypedData() )
60 event.SetClientData( choice->GetClientData(n) );
61
acfd422a 62 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 63}
c801d85f 64
e1e955e1
RR
65//-----------------------------------------------------------------------------
66// wxChoice
c801d85f
KB
67//-----------------------------------------------------------------------------
68
7f4dc78d 69IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 70
fd0eed64 71wxChoice::wxChoice()
c801d85f 72{
e01c8145 73 m_strings = (wxSortedArrayString *)NULL;
6de97a3b 74}
c801d85f 75
debe6624 76bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
77 const wxPoint &pos, const wxSize &size,
78 int n, const wxString choices[],
79 long style, const wxValidator& validator, const wxString &name )
c801d85f 80{
fd0eed64 81 m_needParent = TRUE;
034be888
RR
82#if (GTK_MINOR_VERSION > 0)
83 m_acceptsFocus = TRUE;
84#endif
29006414 85
4dcaf11a
RR
86 if (!PreCreation( parent, pos, size ) ||
87 !CreateBase( parent, id, pos, size, style, validator, name ))
88 {
223d09f6 89 wxFAIL_MSG( wxT("wxChoice creation failed") );
e01c8145 90 return FALSE;
4dcaf11a 91 }
6de97a3b 92
fd0eed64 93 m_widget = gtk_option_menu_new();
29006414 94
e01c8145
VZ
95 if ( style & wxCB_SORT )
96 {
97 // if our m_strings != NULL, DoAppend() will check for it and insert
98 // items in the correct order
99 m_strings = new wxSortedArrayString;
100 }
101
fd0eed64 102 GtkWidget *menu = gtk_menu_new();
29006414 103
fd0eed64
RR
104 for (int i = 0; i < n; i++)
105 {
071a2d78 106 GtkAppendHelper(menu, choices[i]);
fd0eed64 107 }
e01c8145 108
fd0eed64 109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 110
f03fc89f 111 m_parent->DoAddChild( this );
29006414 112
fd0eed64 113 PostCreation();
29006414 114
db434467
RR
115 SetFont( parent->GetFont() );
116
117 wxSize size_best( DoGetBestSize() );
118 wxSize new_size( size );
119 if (new_size.x == -1)
120 new_size.x = size_best.x;
121 if (new_size.y == -1)
122 new_size.y = size_best.y;
123 if ((new_size.x != size.x) || (new_size.y != size.y))
124 SetSize( new_size.x, new_size.y );
125
fd0eed64
RR
126 SetBackgroundColour( parent->GetBackgroundColour() );
127 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 128
fd0eed64 129 Show( TRUE );
29006414 130
fd0eed64 131 return TRUE;
6de97a3b 132}
29006414 133
fd0eed64
RR
134wxChoice::~wxChoice()
135{
f5e27805 136 Clear();
e01c8145
VZ
137
138 delete m_strings;
fd0eed64
RR
139}
140
9abe166a 141int wxChoice::DoAppend( const wxString &item )
fd0eed64 142{
2ee3ee1b 143 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 144
fd0eed64 145 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 146
071a2d78 147 return GtkAppendHelper(menu, item);
fd0eed64 148}
f96aa4d9 149
6c8a980f 150void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 151{
2ee3ee1b 152 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 153
d6538e2c 154 wxNode *node = m_clientList.Nth( n );
6c8a980f 155 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 156
f5e27805 157 node->SetData( (wxObject*) clientData );
fd0eed64
RR
158}
159
6c8a980f 160void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 161{
2ee3ee1b 162 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 163
d6538e2c 164 wxNode *node = m_clientList.Nth( n );
6c8a980f 165 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 166
f5e27805 167 return node->Data();
6de97a3b 168}
fd0eed64 169
6c8a980f 170void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 171{
2ee3ee1b 172 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 173
d6538e2c 174 wxNode *node = m_clientList.Nth( n );
6c8a980f 175 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 176
fd0eed64 177 wxClientData *cd = (wxClientData*) node->Data();
9abe166a 178 delete cd;
29006414 179
fd0eed64
RR
180 node->SetData( (wxObject*) clientData );
181}
182
6c8a980f 183wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 184{
2ee3ee1b 185 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 186
d6538e2c 187 wxNode *node = m_clientList.Nth( n );
9abe166a 188 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 189 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 190
fd0eed64
RR
191 return (wxClientData*) node->Data();
192}
29006414 193
fd0eed64 194void wxChoice::Clear()
c801d85f 195{
223d09f6 196 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 197
fd0eed64
RR
198 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
199 GtkWidget *menu = gtk_menu_new();
200 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 201
6c8a980f
VZ
202 if ( HasClientObjectData() )
203 {
204 // destroy the data (due to Robert's idea of using wxList<wxObject>
205 // and not wxList<wxClientData> we can't just say
206 // m_clientList.DeleteContents(TRUE) - this would crash!
207 wxNode *node = m_clientList.First();
208 while ( node )
209 {
210 delete (wxClientData *)node->Data();
211 node = node->Next();
212 }
213 }
d6538e2c 214 m_clientList.Clear();
2ee3ee1b
VZ
215
216 if ( m_strings )
217 m_strings->Clear();
6de97a3b 218}
c801d85f 219
2f6407b9
RR
220void wxChoice::Delete( int WXUNUSED(n) )
221{
223d09f6 222 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
2f6407b9
RR
223}
224
c801d85f
KB
225int wxChoice::FindString( const wxString &string ) const
226{
223d09f6 227 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
228
229 // If you read this code once and you think you understand
230 // it, then you are very wrong. Robert Roebling.
29006414 231
fd0eed64
RR
232 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
233 int count = 0;
234 GList *child = menu_shell->children;
235 while (child)
236 {
237 GtkBin *bin = GTK_BIN( child->data );
238 GtkLabel *label = (GtkLabel *) NULL;
239 if (bin->child) label = GTK_LABEL(bin->child);
240 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 241
223d09f6 242 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 243
dcf924a3 244 if (string == wxString(label->label,*wxConvCurrent))
29006414
VZ
245 return count;
246
fd0eed64
RR
247 child = child->next;
248 count++;
249 }
29006414 250
fd0eed64 251 return -1;
6de97a3b 252}
c801d85f 253
9abe166a 254int wxChoice::GetSelection() const
c801d85f 255{
223d09f6 256 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
257
258 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
259 int count = 0;
260 GList *child = menu_shell->children;
261 while (child)
262 {
263 GtkBin *bin = GTK_BIN( child->data );
264 if (!bin->child) return count;
265 child = child->next;
266 count++;
267 }
29006414 268
fd0eed64 269 return -1;
6de97a3b 270}
c801d85f 271
6c8a980f
VZ
272void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
273{
274 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
275
276 wxFAIL_MSG(wxT("not implemented"));
277}
278
debe6624 279wxString wxChoice::GetString( int n ) const
c801d85f 280{
223d09f6 281 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
282
283 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
284 int count = 0;
285 GList *child = menu_shell->children;
286 while (child)
c801d85f 287 {
fd0eed64
RR
288 GtkBin *bin = GTK_BIN( child->data );
289 if (count == n)
290 {
291 GtkLabel *label = (GtkLabel *) NULL;
292 if (bin->child) label = GTK_LABEL(bin->child);
293 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 294
223d09f6 295 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 296
dcf924a3 297 return wxString(label->label,*wxConvCurrent);
fd0eed64
RR
298 }
299 child = child->next;
300 count++;
6de97a3b 301 }
29006414 302
223d09f6 303 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 304
223d09f6 305 return wxT("");
6de97a3b 306}
c801d85f 307
9abe166a 308int wxChoice::GetCount() const
c801d85f 309{
223d09f6 310 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
311
312 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
313 int count = 0;
314 GList *child = menu_shell->children;
315 while (child)
316 {
317 count++;
318 child = child->next;
319 }
320 return count;
6de97a3b 321}
c801d85f 322
debe6624 323void wxChoice::SetSelection( int n )
c801d85f 324{
223d09f6 325 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 326
fd0eed64
RR
327 int tmp = n;
328 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 329}
c801d85f 330
58614078 331void wxChoice::ApplyWidgetStyle()
868a2826 332{
fd0eed64 333 SetWidgetStyle();
29006414 334
fd0eed64 335 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 336
fd0eed64
RR
337 gtk_widget_set_style( m_widget, m_widgetStyle );
338 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 339
fd0eed64
RR
340 GList *child = menu_shell->children;
341 while (child)
342 {
343 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 344
fd0eed64
RR
345 GtkBin *bin = GTK_BIN( child->data );
346 GtkWidget *label = (GtkWidget *) NULL;
347 if (bin->child) label = bin->child;
348 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 349
fd0eed64 350 gtk_widget_set_style( label, m_widgetStyle );
29006414 351
fd0eed64
RR
352 child = child->next;
353 }
f96aa4d9
RR
354}
355
071a2d78 356size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item)
e01c8145
VZ
357{
358 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
359
360 size_t index;
361 if ( m_strings )
362 {
363 // sorted control, need to insert at the correct index
364 index = m_strings->Add(item);
365
366 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
367
368 if ( index )
369 {
370 m_clientList.Insert( m_clientList.Item(index - 1),
371 (wxObject*) NULL );
372 }
373 else
374 {
6c8a980f 375 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
376 }
377 }
378 else
379 {
380 // normal control, just append
381 gtk_menu_append( GTK_MENU(menu), menu_item );
382
383 m_clientList.Append( (wxObject*) NULL );
384
385 // don't call wxChoice::GetCount() from here because it doesn't work
386 // if we're called from ctor (and GtkMenuShell is still NULL)
11e1c70d 387 index = m_clientList.GetCount() - 1;
e01c8145
VZ
388 }
389
390 if (GTK_WIDGET_REALIZED(m_widget))
391 {
392 gtk_widget_realize( menu_item );
393 gtk_widget_realize( GTK_BIN(menu_item)->child );
394
395 if (m_widgetStyle) ApplyWidgetStyle();
396 }
397
398 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
399 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
400
401 gtk_widget_show( menu_item );
402
403 // return the index of the item in the control
404 return index;
405}
406
f68586e5
VZ
407wxSize wxChoice::DoGetBestSize() const
408{
db434467
RR
409 wxSize ret( wxControl::DoGetBestSize() );
410 if (ret.x < 80) ret.x = 80;
411 ret.y = 16 + gdk_char_height( m_widget->style->font, 'H' );
412 return ret;
f68586e5
VZ
413}
414
ce4169a4 415#endif