]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/choicdgg.cpp
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 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/dialog.h"
33 #include "wx/button.h"
34 #include "wx/listbox.h"
35 #include "wx/checklst.h"
36 #include "wx/stattext.h"
39 #include "wx/arrstr.h"
42 #include "wx/statline.h"
43 #include "wx/settings.h"
44 #include "wx/generic/choicdgg.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 #define wxID_LISTBOX 3000
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // convert wxArrayString into a wxString[] which must be delete[]d by caller
57 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
59 // ============================================================================
61 // ============================================================================
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
69 int n
= aChoices
.GetCount();
70 *choices
= new wxString
[n
];
72 for ( int i
= 0; i
< n
; i
++ )
74 (*choices
)[i
] = aChoices
[i
];
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 wxString
wxGetSingleChoice( const wxString
& message
,
85 const wxString
& caption
,
86 int n
, const wxString
*choices
,
88 int WXUNUSED(x
), int WXUNUSED(y
),
89 bool WXUNUSED(centre
),
90 int WXUNUSED(width
), int WXUNUSED(height
) )
92 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
94 if ( dialog
.ShowModal() == wxID_OK
)
95 choice
= dialog
.GetStringSelection();
100 wxString
wxGetSingleChoice( const wxString
& message
,
101 const wxString
& caption
,
102 const wxArrayString
& aChoices
,
106 int width
, int height
)
109 int n
= ConvertWXArrayToC(aChoices
, &choices
);
110 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
111 x
, y
, centre
, width
, height
);
117 int wxGetSingleChoiceIndex( const wxString
& message
,
118 const wxString
& caption
,
119 int n
, const wxString
*choices
,
121 int WXUNUSED(x
), int WXUNUSED(y
),
122 bool WXUNUSED(centre
),
123 int WXUNUSED(width
), int WXUNUSED(height
) )
125 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
127 if ( dialog
.ShowModal() == wxID_OK
)
128 choice
= dialog
.GetSelection();
135 int wxGetSingleChoiceIndex( const wxString
& message
,
136 const wxString
& caption
,
137 const wxArrayString
& aChoices
,
141 int width
, int height
)
144 int n
= ConvertWXArrayToC(aChoices
, &choices
);
145 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
146 x
, y
, centre
, width
, height
);
152 void *wxGetSingleChoiceData( const wxString
& message
,
153 const wxString
& caption
,
154 int n
, const wxString
*choices
,
157 int WXUNUSED(x
), int WXUNUSED(y
),
158 bool WXUNUSED(centre
),
159 int WXUNUSED(width
), int WXUNUSED(height
) )
161 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
162 (char **)client_data
);
164 if ( dialog
.ShowModal() == wxID_OK
)
165 data
= dialog
.GetSelectionClientData();
172 void *wxGetSingleChoiceData( const wxString
& message
,
173 const wxString
& caption
,
174 const wxArrayString
& aChoices
,
179 int width
, int height
)
182 int n
= ConvertWXArrayToC(aChoices
, &choices
);
183 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
185 x
, y
, centre
, width
, height
);
191 int wxGetSelectedChoices(wxArrayInt
& selections
,
192 const wxString
& message
,
193 const wxString
& caption
,
194 int n
, const wxString
*choices
,
196 int WXUNUSED(x
), int WXUNUSED(y
),
197 bool WXUNUSED(centre
),
198 int WXUNUSED(width
), int WXUNUSED(height
))
200 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
202 // call this even if selections array is empty and this then (correctly)
203 // deselects the first item which is selected by default
204 dialog
.SetSelections(selections
);
206 if ( dialog
.ShowModal() != wxID_OK
)
208 // NB: intentionally do not clear the selections array here, the caller
209 // might want to preserve its original contents if the dialog was
214 selections
= dialog
.GetSelections();
215 return selections
.GetCount();
218 int wxGetSelectedChoices(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 int res
= wxGetSelectedChoices(selections
, message
, caption
,
231 x
, y
, centre
, width
, height
);
237 #if WXWIN_COMPATIBILITY_2_8
238 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
239 const wxString
& message
,
240 const wxString
& caption
,
241 int n
, const wxString
*choices
,
245 int width
, int height
)
247 int rc
= wxGetSelectedChoices(selections
, message
, caption
,
249 parent
, x
, y
, centre
, width
, height
);
259 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
260 const wxString
& message
,
261 const wxString
& caption
,
262 const wxArrayString
& aChoices
,
266 int width
, int height
)
268 int rc
= wxGetSelectedChoices(selections
, message
, caption
,
270 parent
, x
, y
, centre
, width
, height
);
279 #endif // WXWIN_COMPATIBILITY_2_8
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
286 const wxString
& message
,
287 const wxString
& caption
,
288 int n
, const wxString
*choices
,
295 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
& (~wxCANCEL
) ) )
298 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
302 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
306 Add(CreateTextSizer(message
), wxSizerFlags().Expand().TripleBorder());
309 m_listbox
= CreateList(n
, choices
, styleLbox
);
312 m_listbox
->SetSelection(0);
315 Add(m_listbox
, wxSizerFlags().Expand().Proportion(1).TripleBorder(wxLEFT
| wxRIGHT
));
319 buttonSizer
= CreateSeparatedButtonSizer(styleDlg
& ButtonSizerFlags
);
322 topsizer
->Add(buttonSizer
, wxSizerFlags().Expand().DoubleBorder());
325 SetSizer( topsizer
);
327 topsizer
->SetSizeHints( this );
328 topsizer
->Fit( this );
330 if ( styleDlg
& wxCENTRE
)
333 m_listbox
->SetFocus();
338 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
339 const wxString
& message
,
340 const wxString
& caption
,
341 const wxArrayString
& choices
,
346 wxCArrayString
chs(choices
);
347 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
348 styleDlg
, pos
, styleLbox
);
351 wxListBoxBase
*wxAnyChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
353 return new wxListBox( this, wxID_LISTBOX
,
354 wxDefaultPosition
, wxDefaultSize
,
359 // ----------------------------------------------------------------------------
360 // wxSingleChoiceDialog
361 // ----------------------------------------------------------------------------
363 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
364 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
365 #ifndef __SMARTPHONE__
366 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
369 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown
)
373 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
375 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
376 const wxString
& message
,
377 const wxString
& caption
,
379 const wxString
*choices
,
382 const wxPoint
& WXUNUSED(pos
))
384 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
387 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
388 const wxString
& message
,
389 const wxString
& caption
,
390 const wxArrayString
& choices
,
393 const wxPoint
& WXUNUSED(pos
))
395 Create(parent
, message
, caption
, choices
, clientData
, style
);
398 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
399 const wxString
& message
,
400 const wxString
& caption
,
402 const wxString
*choices
,
407 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
412 m_selection
= n
> 0 ? 0 : -1;
416 for (int i
= 0; i
< n
; i
++)
417 m_listbox
->SetClientData(i
, clientData
[i
]);
423 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
424 const wxString
& message
,
425 const wxString
& caption
,
426 const wxArrayString
& choices
,
431 wxCArrayString
chs(choices
);
432 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
433 clientData
, style
, pos
);
437 void wxSingleChoiceDialog::SetSelection(int sel
)
439 m_listbox
->SetSelection(sel
);
443 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
448 #ifndef __SMARTPHONE__
449 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
456 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent
& WXUNUSED(event
))
462 void wxSingleChoiceDialog::DoChoice()
464 m_selection
= m_listbox
->GetSelection();
465 m_stringSelection
= m_listbox
->GetStringSelection();
467 if ( m_listbox
->HasClientUntypedData() )
468 SetClientData(m_listbox
->GetClientData(m_selection
));
473 // ----------------------------------------------------------------------------
474 // wxMultiChoiceDialog
475 // ----------------------------------------------------------------------------
477 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
479 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
480 const wxString
& message
,
481 const wxString
& caption
,
483 const wxString
*choices
,
488 #if wxUSE_CHECKLISTBOX
489 styleLbox
= wxLB_ALWAYS_SB
;
491 styleLbox
= wxLB_ALWAYS_SB
| wxLB_EXTENDED
;
494 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
503 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
504 const wxString
& message
,
505 const wxString
& caption
,
506 const wxArrayString
& choices
,
510 wxCArrayString
chs(choices
);
511 return Create( parent
, message
, caption
, chs
.GetCount(),
512 chs
.GetStrings(), style
, pos
);
515 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
517 #if wxUSE_CHECKLISTBOX
518 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
521 // first clear all currently selected items
523 count
= checkListBox
->GetCount();
524 for ( n
= 0; n
< count
; ++n
)
526 if (checkListBox
->IsChecked(n
))
527 checkListBox
->Check(n
, false);
530 // now select the ones which should be selected
531 count
= selections
.GetCount();
532 for ( n
= 0; n
< count
; n
++ )
534 checkListBox
->Check(selections
[n
]);
541 // first clear all currently selected items
543 count
= m_listbox
->GetCount();
544 for ( n
= 0; n
< count
; ++n
)
546 m_listbox
->Deselect(n
);
549 // now select the ones which should be selected
550 count
= selections
.GetCount();
551 for ( n
= 0; n
< count
; n
++ )
553 m_listbox
->Select(selections
[n
]);
557 bool wxMultiChoiceDialog::TransferDataFromWindow()
559 m_selections
.Empty();
561 #if wxUSE_CHECKLISTBOX
562 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
565 size_t count
= checkListBox
->GetCount();
566 for ( size_t n
= 0; n
< count
; n
++ )
568 if ( checkListBox
->IsChecked(n
) )
575 size_t count
= m_listbox
->GetCount();
576 for ( size_t n
= 0; n
< count
; n
++ )
578 if ( m_listbox
->IsSelected(n
) )
585 #if wxUSE_CHECKLISTBOX
587 wxListBoxBase
*wxMultiChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
589 return new wxCheckListBox( this, wxID_LISTBOX
,
590 wxDefaultPosition
, wxDefaultSize
,
595 #endif // wxUSE_CHECKLISTBOX
597 #endif // wxUSE_CHOICEDLG