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