]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Choice dialogs
4 // Author: Julian Smart
5 // Modified by: 03.11.00: VZ to add wxArrayString and multiple sel functions
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "choicdgg.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/dialog.h"
37 #include "wx/button.h"
38 #include "wx/listbox.h"
39 #include "wx/stattext.h"
42 #include "wx/arrstr.h"
46 #include "wx/statline.h"
49 #include "wx/generic/choicdgg.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 #define wxID_LISTBOX 3000
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // convert wxArrayString into a wxString[] which must be delete[]d by caller
62 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
74 int n
= aChoices
.GetCount();
75 *choices
= new wxString
[n
];
77 for ( int i
= 0; i
< n
; i
++ )
79 (*choices
)[i
] = aChoices
[i
];
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 wxString
wxGetSingleChoice( const wxString
& message
,
90 const wxString
& caption
,
91 int n
, const wxString
*choices
,
93 int WXUNUSED(x
), int WXUNUSED(y
),
94 bool WXUNUSED(centre
),
95 int WXUNUSED(width
), int WXUNUSED(height
) )
97 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
99 if ( dialog
.ShowModal() == wxID_OK
)
100 choice
= dialog
.GetStringSelection();
105 wxString
wxGetSingleChoice( const wxString
& message
,
106 const wxString
& caption
,
107 const wxArrayString
& aChoices
,
111 int width
, int height
)
114 int n
= ConvertWXArrayToC(aChoices
, &choices
);
115 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
116 x
, y
, centre
, width
, height
);
122 int wxGetSingleChoiceIndex( const wxString
& message
,
123 const wxString
& caption
,
124 int n
, const wxString
*choices
,
126 int WXUNUSED(x
), int WXUNUSED(y
),
127 bool WXUNUSED(centre
),
128 int WXUNUSED(width
), int WXUNUSED(height
) )
130 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
132 if ( dialog
.ShowModal() == wxID_OK
)
133 choice
= dialog
.GetSelection();
140 int wxGetSingleChoiceIndex( const wxString
& message
,
141 const wxString
& caption
,
142 const wxArrayString
& aChoices
,
146 int width
, int height
)
149 int n
= ConvertWXArrayToC(aChoices
, &choices
);
150 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
151 x
, y
, centre
, width
, height
);
157 void *wxGetSingleChoiceData( const wxString
& message
,
158 const wxString
& caption
,
159 int n
, const wxString
*choices
,
162 int WXUNUSED(x
), int WXUNUSED(y
),
163 bool WXUNUSED(centre
),
164 int WXUNUSED(width
), int WXUNUSED(height
) )
166 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
167 (char **)client_data
);
169 if ( dialog
.ShowModal() == wxID_OK
)
170 data
= dialog
.GetSelectionClientData();
177 void *wxGetSingleChoiceData( const wxString
& message
,
178 const wxString
& caption
,
179 const wxArrayString
& aChoices
,
184 int width
, int height
)
187 int n
= ConvertWXArrayToC(aChoices
, &choices
);
188 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
190 x
, y
, centre
, width
, height
);
196 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
197 const wxString
& message
,
198 const wxString
& caption
,
199 int n
, const wxString
*choices
,
201 int WXUNUSED(x
), int WXUNUSED(y
),
202 bool WXUNUSED(centre
),
203 int WXUNUSED(width
), int WXUNUSED(height
))
205 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
207 if ( !selections
.IsEmpty() )
208 dialog
.SetSelections(selections
);
210 if ( dialog
.ShowModal() == wxID_OK
)
211 selections
= dialog
.GetSelections();
215 return selections
.GetCount();
218 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
219 const wxString
& message
,
220 const wxString
& caption
,
221 const wxArrayString
& aChoices
,
225 int width
, int height
)
228 int n
= ConvertWXArrayToC(aChoices
, &choices
);
229 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
231 x
, y
, centre
, width
, height
);
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
242 const wxString
& message
,
243 const wxString
& caption
,
244 int n
, const wxString
*choices
,
249 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
252 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
255 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
258 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
259 wxDefaultPosition
, wxDefaultSize
,
263 m_listbox
->SetSelection(0);
265 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
269 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
273 topsizer
->Add( CreateButtonSizer( styleDlg
& (wxOK
|wxCANCEL
) ), 0, wxCENTRE
| wxALL
, 10 );
275 SetAutoLayout( true );
276 SetSizer( topsizer
);
278 topsizer
->SetSizeHints( this );
279 topsizer
->Fit( this );
283 m_listbox
->SetFocus();
288 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
289 const wxString
& message
,
290 const wxString
& caption
,
291 const wxArrayString
& choices
,
296 wxCArrayString
chs(choices
);
297 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
298 styleDlg
, pos
, styleLbox
);
301 // ----------------------------------------------------------------------------
302 // wxSingleChoiceDialog
303 // ----------------------------------------------------------------------------
305 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
306 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
307 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
310 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
312 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
313 const wxString
& message
,
314 const wxString
& caption
,
316 const wxString
*choices
,
319 const wxPoint
& WXUNUSED(pos
))
321 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
324 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
325 const wxString
& message
,
326 const wxString
& caption
,
327 const wxArrayString
& choices
,
330 const wxPoint
& WXUNUSED(pos
))
332 Create(parent
, message
, caption
, choices
, clientData
, style
);
335 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
336 const wxString
& message
,
337 const wxString
& caption
,
339 const wxString
*choices
,
344 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
349 m_selection
= n
> 0 ? 0 : -1;
353 for (int i
= 0; i
< n
; i
++)
354 m_listbox
->SetClientData(i
, clientData
[i
]);
360 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
361 const wxString
& message
,
362 const wxString
& caption
,
363 const wxArrayString
& choices
,
368 wxCArrayString
chs(choices
);
369 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
370 clientData
, style
, pos
);
374 void wxSingleChoiceDialog::SetSelection(int sel
)
376 m_listbox
->SetSelection(sel
);
380 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
382 m_selection
= m_listbox
->GetSelection();
383 m_stringSelection
= m_listbox
->GetStringSelection();
384 if ( m_listbox
->HasClientUntypedData() )
385 SetClientData(m_listbox
->GetClientData(m_selection
));
389 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
391 m_selection
= m_listbox
->GetSelection();
392 m_stringSelection
= m_listbox
->GetStringSelection();
394 if ( m_listbox
->HasClientUntypedData() )
395 SetClientData(m_listbox
->GetClientData(m_selection
));
400 // ----------------------------------------------------------------------------
401 // wxMultiChoiceDialog
402 // ----------------------------------------------------------------------------
404 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
406 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
407 const wxString
& message
,
408 const wxString
& caption
,
410 const wxString
*choices
,
414 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
417 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
423 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
424 const wxString
& message
,
425 const wxString
& caption
,
426 const wxArrayString
& choices
,
430 wxCArrayString
chs(choices
);
431 return Create( parent
, message
, caption
, chs
.GetCount(),
432 chs
.GetStrings(), style
, pos
);
435 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
437 size_t count
= selections
.GetCount();
438 for ( size_t n
= 0; n
< count
; n
++ )
440 m_listbox
->Select(selections
[n
]);
444 bool wxMultiChoiceDialog::TransferDataFromWindow()
446 m_selections
.Empty();
447 size_t count
= m_listbox
->GetCount();
448 for ( size_t n
= 0; n
< count
; n
++ )
450 if ( m_listbox
->IsSelected(n
) )
457 #endif // wxUSE_CHOICEDLG