]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
Changed to use current wxSocket flag names instead of old ones
[wxWidgets.git] / src / generic / choicdgg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
dfad0599 2// Name: choicdgg.cpp
c801d85f
KB
3// Purpose: Choice dialogs
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
d427503c 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
d427503c 13 #pragma implementation "choicdgg.h"
c801d85f
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
d427503c 20 #pragma hdrstop
c801d85f
KB
21#endif
22
23#ifndef WX_PRECOMP
257bf510
VZ
24 #include <stdio.h>
25 #include "wx/utils.h"
26 #include "wx/dialog.h"
27 #include "wx/button.h"
28 #include "wx/listbox.h"
29 #include "wx/stattext.h"
30 #include "wx/intl.h"
92afa2b1 31 #include "wx/sizer.h"
dcf924a3
RR
32#endif
33
34#if wxUSE_STATLINE
c50f1fb9 35 #include "wx/statline.h"
c801d85f
KB
36#endif
37
38#include "wx/generic/choicdgg.h"
39
257bf510 40#define wxID_LISTBOX 3000
dcf924a3 41
c50f1fb9 42wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
debe6624 43 const wxString *choices, wxWindow *parent,
c50f1fb9 44 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
257bf510 45 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 46{
d427503c
VZ
47 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
48 if ( dialog.ShowModal() == wxID_OK )
d427503c 49 return dialog.GetStringSelection();
d427503c 50 else
223d09f6 51 return wxT("");
c801d85f
KB
52}
53
54// Overloaded for backward compatibility
c50f1fb9 55wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
debe6624 56 char *choices[], wxWindow *parent,
c50f1fb9 57 int x, int y, bool centre,
257bf510 58 int width, int height )
c801d85f 59{
d427503c
VZ
60 wxString *strings = new wxString[n];
61 int i;
62 for ( i = 0; i < n; i++)
63 {
64 strings[i] = choices[i];
65 }
66 wxString ans(wxGetSingleChoice(message, caption, n, (const wxString *)strings, parent,
67 x, y, centre, width, height));
68 delete[] strings;
69 return ans;
c801d85f
KB
70}
71
c50f1fb9 72int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
debe6624 73 const wxString *choices, wxWindow *parent,
c50f1fb9 74 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
d427503c 75 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 76{
d427503c
VZ
77 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
78 if ( dialog.ShowModal() == wxID_OK )
d427503c 79 return dialog.GetSelection();
d427503c
VZ
80 else
81 return -1;
c801d85f
KB
82}
83
84// Overloaded for backward compatibility
c50f1fb9 85int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
87138c52 86 wxChar *choices[], wxWindow *parent,
c50f1fb9 87 int x, int y, bool centre,
d427503c 88 int width, int height )
c801d85f 89{
d427503c 90 wxString *strings = new wxString[n];
dcf924a3 91 for ( int i = 0; i < n; i++)
d427503c 92 strings[i] = choices[i];
d427503c
VZ
93 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
94 x, y, centre, width, height);
95 delete[] strings;
96 return ans;
c801d85f
KB
97}
98
2695a14e
OK
99void *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
100 const wxString *choices, void **client_data, wxWindow *parent,
c50f1fb9 101 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
d427503c 102 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 103{
b91b2200 104 wxSingleChoiceDialog dialog(parent, message, caption, n, choices, (char **)client_data);
d427503c 105 if ( dialog.ShowModal() == wxID_OK )
2695a14e 106 return dialog.GetSelectionClientData();
d427503c
VZ
107 else
108 return NULL;
c801d85f
KB
109}
110
111// Overloaded for backward compatibility
2695a14e
OK
112void *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
113 wxChar *choices[], void **client_data, wxWindow *parent,
c50f1fb9 114 int x, int y, bool centre,
d427503c 115 int width, int height )
c801d85f 116{
d427503c
VZ
117 wxString *strings = new wxString[n];
118 int i;
119 for ( i = 0; i < n; i++)
120 {
121 strings[i] = choices[i];
122 }
2695a14e 123 void *data = wxGetSingleChoiceData(message, caption, n, (const wxString *)strings, client_data, parent,
d427503c
VZ
124 x, y, centre, width, height);
125 delete[] strings;
126 return data;
c801d85f
KB
127}
128
129
130/* Multiple choice dialog contributed by Robert Cowell
131 *
132
133The new data passed are in the "int nsel" and "int * selection"
134
135The idea is to make a multiple selection from list of strings.
136The returned value is the total number selected. initialily there
137are nsel selected, with indices stored in
138selection[0],...,selection[nsel-1] which appear highlighted to
139begin with. On exit with value i
140selection[0..i-1] contains the indices of the selected items.
141(Some prior selectecions might be deselected.)
142Thus selection must be as big as choices, in case all items are
143selected.
144
145*/
146/*
147int wxGetMultipleChoice(const wxString& message, const wxString& caption,
d427503c
VZ
148 int n, const wxString *choices,
149 int nsel, int * selection,
150 wxWindow *parent , int x , int y, bool centre,
151 int width, int height)
c801d85f 152{
d427503c 153 return -1;
c801d85f
KB
154}
155*/
156
157// wxSingleChoiceDialog
158
c801d85f 159BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c
VZ
160 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
161 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
c801d85f
KB
162END_EVENT_TABLE()
163
164IMPLEMENT_CLASS(wxSingleChoiceDialog, wxDialog)
c801d85f 165
92afa2b1 166#if defined(__WXMSW__) || defined(__WXMAC__)
257bf510
VZ
167#define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
168 wxDIALOG_MODAL | \
169 wxTAB_TRAVERSAL)
92afa2b1
RR
170#else
171#define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
172 wxDIALOG_MODAL | \
173 wxRESIZE_BORDER | \
174 wxTAB_TRAVERSAL)
175#endif
176
257bf510
VZ
177
178wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
179 const wxString& message,
180 const wxString& caption,
c50f1fb9 181 int n,
257bf510
VZ
182 const wxString *choices,
183 char **clientData,
184 long style,
185 const wxPoint& pos)
186 : wxDialog(parent, -1, caption, pos, wxDefaultSize,
187 wxCHOICEDLG_DIALOG_STYLE)
c801d85f 188{
257bf510 189 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
190}
191
257bf510
VZ
192wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
193 const wxString& message,
194 const wxString& caption,
c50f1fb9 195 const wxStringList& choices,
b91b2200 196 char **clientData,
c50f1fb9 197 long style,
257bf510
VZ
198 const wxPoint& pos)
199 : wxDialog(parent, -1, caption, pos, wxDefaultSize,
200 wxCHOICEDLG_DIALOG_STYLE)
c801d85f 201{
257bf510 202 Create(parent, message, caption, choices, clientData, style);
c801d85f
KB
203}
204
257bf510
VZ
205bool wxSingleChoiceDialog::Create(wxWindow *parent,
206 const wxString& message,
207 const wxString& caption,
208 const wxStringList& choices,
209 char **clientData,
210 long style,
211 const wxPoint& pos)
c801d85f 212{
d427503c
VZ
213 wxString *strings = new wxString[choices.Number()];
214 int i;
215 for ( i = 0; i < choices.Number(); i++)
216 {
217 strings[i] = (char *)choices.Nth(i)->Data();
218 }
219 bool ans = Create(parent, message, caption, choices.Number(), strings, clientData, style, pos);
220 delete[] strings;
221 return ans;
c801d85f
KB
222}
223
257bf510 224bool wxSingleChoiceDialog::Create( wxWindow *WXUNUSED(parent),
c50f1fb9 225 const wxString& message,
257bf510 226 const wxString& WXUNUSED(caption),
c50f1fb9 227 int n,
257bf510
VZ
228 const wxString *choices,
229 char **clientData,
230 long style,
d427503c 231 const wxPoint& WXUNUSED(pos) )
c801d85f 232{
d427503c 233 m_selection = 0;
92afa2b1
RR
234
235 m_dialogStyle = style;
236
237 wxBeginBusyCursor();
479cd5de 238
92afa2b1
RR
239 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
240
241 // 1) text message
242 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
479cd5de 243
92afa2b1 244 // 2) list box
479cd5de 245 m_listbox = new wxListBox( this, wxID_LISTBOX, wxDefaultPosition, wxSize(160,100) ,
92afa2b1
RR
246 n, choices, wxLB_ALWAYS_SB );
247 m_listbox->SetSelection( m_selection );
248 if (clientData)
d427503c 249 {
257bf510
VZ
250 for (int i = 0; i < n; i++)
251 m_listbox->SetClientData(i, clientData[i]);
d427503c 252 }
92afa2b1 253 topsizer->Add( m_listbox, 1, wxEXPAND | wxLEFT|wxRIGHT, 15 );
c801d85f 254
257bf510 255#if wxUSE_STATLINE
92afa2b1
RR
256 // 3) static line
257 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
257bf510 258#endif
c50f1fb9 259
92afa2b1
RR
260 // 4) buttons
261 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
257bf510 262
dc6c62a9
RR
263 SetAutoLayout( TRUE );
264 SetSizer( topsizer );
479cd5de 265
92afa2b1
RR
266 topsizer->SetSizeHints( this );
267 topsizer->Fit( this );
257bf510 268
92afa2b1 269 Centre( wxBOTH );
257bf510 270
92afa2b1 271 m_listbox->SetFocus();
c801d85f 272
92afa2b1 273 wxEndBusyCursor();
c801d85f 274
d427503c 275 return TRUE;
c801d85f
KB
276}
277
ef77f91e
JS
278// Set the selection
279void wxSingleChoiceDialog::SetSelection(int sel)
280{
257bf510 281 m_listbox->SetSelection(sel);
ef77f91e
JS
282 m_selection = sel;
283}
284
c801d85f
KB
285void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
286{
257bf510
VZ
287 m_selection = m_listbox->GetSelection();
288 m_stringSelection = m_listbox->GetStringSelection();
25f47127
JS
289 // TODO!
290#ifndef __WXMOTIF__
eb553cb2
VZ
291 if ( m_listbox->HasClientUntypedData() )
292 SetClientData(m_listbox->GetClientData(m_selection));
25f47127 293#endif
d427503c 294 EndModal(wxID_OK);
c801d85f
KB
295}
296
debe6624
JS
297void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
298{
257bf510
VZ
299 m_selection = m_listbox->GetSelection();
300 m_stringSelection = m_listbox->GetStringSelection();
25f47127
JS
301
302 // TODO!
303#ifndef __WXMOTIF__
eb553cb2
VZ
304 if ( m_listbox->HasClientUntypedData() )
305 SetClientData(m_listbox->GetClientData(m_selection));
25f47127 306#endif
d427503c
VZ
307
308 EndModal(wxID_OK);
debe6624
JS
309}
310