]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Since wxREADONLY has disappeared, I had to change to wxTE_READONLY
[wxWidgets.git] / src / gtk / choice.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: choice.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
13#pragma implementation "choice.h"
14#endif
15
16#include "wx/choice.h"
17
66bd6b93
RR
18//-----------------------------------------------------------------------------
19// data
20//-----------------------------------------------------------------------------
21
22extern bool g_blockEventsOnDrag;
23
c801d85f
KB
24//-----------------------------------------------------------------------------
25// wxChoice
26//-----------------------------------------------------------------------------
27
66bd6b93 28static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 29{
66bd6b93
RR
30 if (!choice->HasVMT()) return;
31 if (g_blockEventsOnDrag) return;
32
47908e25 33 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
c801d85f
KB
34 event.SetInt( choice->GetSelection() );
35 wxString tmp( choice->GetStringSelection() );
36 event.SetString( WXSTRINGCAST(tmp) );
37 event.SetEventObject(choice);
47908e25 38 choice->GetEventHandler()->ProcessEvent(event);
c801d85f
KB
39};
40
41//-----------------------------------------------------------------------------
42
7f4dc78d 43IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f
KB
44
45wxChoice::wxChoice(void)
46{
47};
48
debe6624
JS
49wxChoice::wxChoice( wxWindow *parent, wxWindowID id,
50 const wxPoint &pos, const wxSize &size,
51 int n, const wxString choices[],
52 long style, const wxString &name )
c801d85f
KB
53{
54 Create( parent, id, pos, size, n, choices, style, name );
55};
56
debe6624
JS
57bool wxChoice::Create( wxWindow *parent, wxWindowID id,
58 const wxPoint &pos, const wxSize &size,
59 int n, const wxString choices[],
60 long style, const wxString &name )
c801d85f
KB
61{
62 m_needParent = TRUE;
63
64 PreCreation( parent, id, pos, size, style, name );
65
66 m_widget = gtk_option_menu_new();
67
68 wxSize newSize = size;
69 if (newSize.x == -1) newSize.x = 80;
70 if (newSize.y == -1) newSize.y = 26;
71 SetSize( newSize.x, newSize.y );
72
73 GtkWidget *menu;
74 menu = gtk_menu_new();
75
76 for (int i = 0; i < n; i++)
77 {
78 GtkWidget *item;
79 item = gtk_menu_item_new_with_label( choices[i] );
80 gtk_signal_connect( GTK_OBJECT( item ), "activate",
81 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
82 gtk_menu_append( GTK_MENU(menu), item );
83 gtk_widget_show( item );
84 };
85 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
86
87 PostCreation();
88
89 Show( TRUE );
90
91 return TRUE;
92};
93
94void wxChoice::Append( const wxString &item )
95{
96 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
97 GtkWidget *menu_item;
98 menu_item = gtk_menu_item_new_with_label( item );
99 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
100 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
101 gtk_menu_append( GTK_MENU(menu), menu_item );
102 gtk_widget_show( menu_item );
103};
104
105void wxChoice::Clear(void)
106{
107 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
108 GtkWidget *menu = gtk_menu_new();
109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
110};
111
112int wxChoice::FindString( const wxString &string ) const
113{
47908e25
RR
114 // If you read this code once and you think you understand
115 // it, then you are very wrong. Robert Roebling.
c801d85f
KB
116
117 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
118 int count = 0;
119 GList *child = menu_shell->children;
120 while (child)
121 {
122 GtkBin *bin = GTK_BIN( child->data );
47908e25
RR
123 GtkLabel *label = NULL;
124 if (bin->child) label = GTK_LABEL(bin->child);
b6af8d80
RR
125
126 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
127
c801d85f
KB
128 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
129 if (string == label->label) return count;
130 child = child->next;
131 count++;
132 };
b6af8d80
RR
133
134 wxFAIL_MSG( "wxChoice: string not found" );
135
c801d85f
KB
136 return -1;
137};
138
139int wxChoice::GetColumns(void) const
140{
141 return 1;
142};
143
144int wxChoice::GetSelection(void)
145{
146 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
147 int count = 0;
148 GList *child = menu_shell->children;
149 while (child)
150 {
151 GtkBin *bin = GTK_BIN( child->data );
152 if (!bin->child) return count;
153 child = child->next;
154 count++;
155 };
b6af8d80
RR
156
157 wxFAIL_MSG( "wxChoice: no selection" );
158
c801d85f
KB
159 return -1;
160};
161
debe6624 162wxString wxChoice::GetString( int n ) const
c801d85f
KB
163{
164 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
165 int count = 0;
166 GList *child = menu_shell->children;
167 while (child)
168 {
169 GtkBin *bin = GTK_BIN( child->data );
170 if (count == n)
171 {
47908e25
RR
172 GtkLabel *label = NULL;
173 if (bin->child) label = GTK_LABEL(bin->child);
b6af8d80
RR
174
175 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
176
c801d85f
KB
177 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
178 return label->label;
179 };
180 child = child->next;
181 count++;
182 };
b6af8d80
RR
183
184 wxFAIL_MSG( "wxChoice: string not found" );
185
c801d85f
KB
186 return "";
187};
188
189wxString wxChoice::GetStringSelection(void) const
190{
191 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
b6af8d80
RR
192
193 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
194
c801d85f
KB
195 return label->label;
196};
197
198int wxChoice::Number(void) const
199{
7a4b9130 200 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
c801d85f 201 int count = 0;
7a4b9130 202 GList *child = menu_shell->children;
c801d85f
KB
203 while (child)
204 {
205 count++;
206 child = child->next;
207 };
208 return count;
209};
210
debe6624 211void wxChoice::SetColumns( int WXUNUSED(n) )
c801d85f
KB
212{
213};
214
debe6624 215void wxChoice::SetSelection( int n )
c801d85f
KB
216{
217 int tmp = n;
218 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
47908e25
RR
219
220 gtk_choice_clicked_callback( NULL, this );
c801d85f
KB
221};
222
223void wxChoice::SetStringSelection( const wxString &string )
224{
225 int n = FindString( string );
226 if (n != -1) SetSelection( n );
227};
228