]>
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
),
93 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
95 dialog
.SetSelection(initialSelection
);
98 if ( dialog
.ShowModal() == wxID_OK
)
99 choice
= dialog
.GetStringSelection();
104 wxString
wxGetSingleChoice( const wxString
& message
,
105 const wxString
& caption
,
106 const wxArrayString
& aChoices
,
110 int width
, int height
,
111 int initialSelection
)
114 int n
= ConvertWXArrayToC(aChoices
, &choices
);
115 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
116 x
, y
, centre
, width
, height
,
123 wxString
wxGetSingleChoice( const wxString
& message
,
124 const wxString
& caption
,
125 const wxArrayString
& choices
,
126 int initialSelection
,
129 return wxGetSingleChoice(message
, caption
, choices
, parent
,
130 wxDefaultCoord
, wxDefaultCoord
,
131 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
135 wxString
wxGetSingleChoice( const wxString
& message
,
136 const wxString
& caption
,
137 int n
, const wxString
*choices
,
138 int initialSelection
,
141 return wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
142 wxDefaultCoord
, wxDefaultCoord
,
143 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
147 int wxGetSingleChoiceIndex( const wxString
& message
,
148 const wxString
& caption
,
149 int n
, const wxString
*choices
,
151 int WXUNUSED(x
), int WXUNUSED(y
),
152 bool WXUNUSED(centre
),
153 int WXUNUSED(width
), int WXUNUSED(height
),
154 int initialSelection
)
156 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
158 dialog
.SetSelection(initialSelection
);
161 if ( dialog
.ShowModal() == wxID_OK
)
162 choice
= dialog
.GetSelection();
169 int wxGetSingleChoiceIndex( const wxString
& message
,
170 const wxString
& caption
,
171 const wxArrayString
& aChoices
,
175 int width
, int height
,
176 int initialSelection
)
179 int n
= ConvertWXArrayToC(aChoices
, &choices
);
180 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
181 x
, y
, centre
, width
, height
,
188 int wxGetSingleChoiceIndex( const wxString
& message
,
189 const wxString
& caption
,
190 const wxArrayString
& choices
,
191 int initialSelection
,
194 return wxGetSingleChoiceIndex(message
, caption
, choices
, parent
,
195 wxDefaultCoord
, wxDefaultCoord
,
196 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
201 int wxGetSingleChoiceIndex( const wxString
& message
,
202 const wxString
& caption
,
203 int n
, const wxString
*choices
,
204 int initialSelection
,
207 return wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
208 wxDefaultCoord
, wxDefaultCoord
,
209 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
214 void *wxGetSingleChoiceData( const wxString
& message
,
215 const wxString
& caption
,
216 int n
, const wxString
*choices
,
219 int WXUNUSED(x
), int WXUNUSED(y
),
220 bool WXUNUSED(centre
),
221 int WXUNUSED(width
), int WXUNUSED(height
),
222 int initialSelection
)
224 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
225 (char **)client_data
);
227 dialog
.SetSelection(initialSelection
);
230 if ( dialog
.ShowModal() == wxID_OK
)
231 data
= dialog
.GetSelectionClientData();
238 void *wxGetSingleChoiceData( const wxString
& message
,
239 const wxString
& caption
,
240 const wxArrayString
& aChoices
,
245 int width
, int height
,
246 int initialSelection
)
249 int n
= ConvertWXArrayToC(aChoices
, &choices
);
250 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
252 x
, y
, centre
, width
, height
,
259 void* wxGetSingleChoiceData( const wxString
& message
,
260 const wxString
& caption
,
261 const wxArrayString
& choices
,
263 int initialSelection
,
266 return wxGetSingleChoiceData(message
, caption
, choices
,
268 wxDefaultCoord
, wxDefaultCoord
,
269 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
273 void* wxGetSingleChoiceData( const wxString
& message
,
274 const wxString
& caption
,
275 int n
, const wxString
*choices
,
277 int initialSelection
,
280 return wxGetSingleChoiceData(message
, caption
, n
, choices
,
282 wxDefaultCoord
, wxDefaultCoord
,
283 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
288 int wxGetSelectedChoices(wxArrayInt
& selections
,
289 const wxString
& message
,
290 const wxString
& caption
,
291 int n
, const wxString
*choices
,
293 int WXUNUSED(x
), int WXUNUSED(y
),
294 bool WXUNUSED(centre
),
295 int WXUNUSED(width
), int WXUNUSED(height
))
297 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
299 // call this even if selections array is empty and this then (correctly)
300 // deselects the first item which is selected by default
301 dialog
.SetSelections(selections
);
303 if ( dialog
.ShowModal() != wxID_OK
)
305 // NB: intentionally do not clear the selections array here, the caller
306 // might want to preserve its original contents if the dialog was
311 selections
= dialog
.GetSelections();
312 return selections
.GetCount();
315 int wxGetSelectedChoices(wxArrayInt
& selections
,
316 const wxString
& message
,
317 const wxString
& caption
,
318 const wxArrayString
& aChoices
,
322 int width
, int height
)
325 int n
= ConvertWXArrayToC(aChoices
, &choices
);
326 int res
= wxGetSelectedChoices(selections
, message
, caption
,
328 x
, y
, centre
, width
, height
);
334 #if WXWIN_COMPATIBILITY_2_8
335 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
336 const wxString
& message
,
337 const wxString
& caption
,
338 int n
, const wxString
*choices
,
342 int width
, int height
)
344 int rc
= wxGetSelectedChoices(selections
, message
, caption
,
346 parent
, x
, y
, centre
, width
, height
);
356 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
357 const wxString
& message
,
358 const wxString
& caption
,
359 const wxArrayString
& aChoices
,
363 int width
, int height
)
365 int rc
= wxGetSelectedChoices(selections
, message
, caption
,
367 parent
, x
, y
, centre
, width
, height
);
376 #endif // WXWIN_COMPATIBILITY_2_8
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
383 const wxString
& message
,
384 const wxString
& caption
,
385 int n
, const wxString
*choices
,
390 // extract the buttons styles from the dialog one and remove them from it
391 const long styleBtns
= styleDlg
& (wxOK
| wxCANCEL
);
392 styleDlg
&= ~styleBtns
;
394 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
397 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
401 Add(CreateTextSizer(message
), wxSizerFlags().Expand().TripleBorder());
404 m_listbox
= CreateList(n
, choices
, styleLbox
);
407 m_listbox
->SetSelection(0);
410 Add(m_listbox
, wxSizerFlags().Expand().Proportion(1).TripleBorder(wxLEFT
| wxRIGHT
));
414 buttonSizer
= CreateSeparatedButtonSizer(styleBtns
);
417 topsizer
->Add(buttonSizer
, wxSizerFlags().Expand().DoubleBorder());
420 SetSizer( topsizer
);
422 topsizer
->SetSizeHints( this );
423 topsizer
->Fit( this );
425 if ( styleDlg
& wxCENTRE
)
428 m_listbox
->SetFocus();
433 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
434 const wxString
& message
,
435 const wxString
& caption
,
436 const wxArrayString
& choices
,
441 wxCArrayString
chs(choices
);
442 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
443 styleDlg
, pos
, styleLbox
);
446 wxListBoxBase
*wxAnyChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
448 return new wxListBox( this, wxID_LISTBOX
,
449 wxDefaultPosition
, wxDefaultSize
,
454 // ----------------------------------------------------------------------------
455 // wxSingleChoiceDialog
456 // ----------------------------------------------------------------------------
458 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
459 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
460 #ifndef __SMARTPHONE__
461 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
464 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown
)
468 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
470 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
471 const wxString
& message
,
472 const wxString
& caption
,
474 const wxString
*choices
,
477 const wxPoint
& WXUNUSED(pos
))
479 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
482 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
483 const wxString
& message
,
484 const wxString
& caption
,
485 const wxArrayString
& choices
,
488 const wxPoint
& WXUNUSED(pos
))
490 Create(parent
, message
, caption
, choices
, clientData
, style
);
493 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
494 const wxString
& message
,
495 const wxString
& caption
,
497 const wxString
*choices
,
502 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
507 m_selection
= n
> 0 ? 0 : -1;
511 for (int i
= 0; i
< n
; i
++)
512 m_listbox
->SetClientData(i
, clientData
[i
]);
518 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
519 const wxString
& message
,
520 const wxString
& caption
,
521 const wxArrayString
& choices
,
526 wxCArrayString
chs(choices
);
527 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
528 clientData
, style
, pos
);
532 void wxSingleChoiceDialog::SetSelection(int sel
)
534 wxCHECK_RET( sel
>= 0 && (unsigned)sel
< m_listbox
->GetCount(),
535 "Invalid initial selection" );
537 m_listbox
->SetSelection(sel
);
541 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
546 #ifndef __SMARTPHONE__
547 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
554 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent
& WXUNUSED(event
))
560 void wxSingleChoiceDialog::DoChoice()
562 m_selection
= m_listbox
->GetSelection();
563 m_stringSelection
= m_listbox
->GetStringSelection();
565 if ( m_listbox
->HasClientUntypedData() )
566 SetClientData(m_listbox
->GetClientData(m_selection
));
571 // ----------------------------------------------------------------------------
572 // wxMultiChoiceDialog
573 // ----------------------------------------------------------------------------
575 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
577 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
578 const wxString
& message
,
579 const wxString
& caption
,
581 const wxString
*choices
,
586 #if wxUSE_CHECKLISTBOX
587 styleLbox
= wxLB_ALWAYS_SB
;
589 styleLbox
= wxLB_ALWAYS_SB
| wxLB_EXTENDED
;
592 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
601 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
602 const wxString
& message
,
603 const wxString
& caption
,
604 const wxArrayString
& choices
,
608 wxCArrayString
chs(choices
);
609 return Create( parent
, message
, caption
, chs
.GetCount(),
610 chs
.GetStrings(), style
, pos
);
613 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
615 #if wxUSE_CHECKLISTBOX
616 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
619 // first clear all currently selected items
621 count
= checkListBox
->GetCount();
622 for ( n
= 0; n
< count
; ++n
)
624 if (checkListBox
->IsChecked(n
))
625 checkListBox
->Check(n
, false);
628 // now select the ones which should be selected
629 count
= selections
.GetCount();
630 for ( n
= 0; n
< count
; n
++ )
632 checkListBox
->Check(selections
[n
]);
639 // first clear all currently selected items
641 count
= m_listbox
->GetCount();
642 for ( n
= 0; n
< count
; ++n
)
644 m_listbox
->Deselect(n
);
647 // now select the ones which should be selected
648 count
= selections
.GetCount();
649 for ( n
= 0; n
< count
; n
++ )
651 m_listbox
->Select(selections
[n
]);
655 bool wxMultiChoiceDialog::TransferDataFromWindow()
657 m_selections
.Empty();
659 #if wxUSE_CHECKLISTBOX
660 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
663 size_t count
= checkListBox
->GetCount();
664 for ( size_t n
= 0; n
< count
; n
++ )
666 if ( checkListBox
->IsChecked(n
) )
673 size_t count
= m_listbox
->GetCount();
674 for ( size_t n
= 0; n
< count
; n
++ )
676 if ( m_listbox
->IsSelected(n
) )
683 #if wxUSE_CHECKLISTBOX
685 wxListBoxBase
*wxMultiChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
687 return new wxCheckListBox( this, wxID_LISTBOX
,
688 wxDefaultPosition
, wxDefaultSize
,
693 #endif // wxUSE_CHECKLISTBOX
695 #endif // wxUSE_CHOICEDLG