]>
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
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
31 #include "wx/dialog.h"
32 #include "wx/button.h"
33 #include "wx/listbox.h"
34 #include "wx/checklst.h"
35 #include "wx/stattext.h"
38 #include "wx/arrstr.h"
41 #include "wx/statline.h"
42 #include "wx/settings.h"
43 #include "wx/generic/choicdgg.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #define wxID_LISTBOX 3000
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // convert wxArrayString into a wxString[] which must be delete[]d by caller
56 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
68 int n
= aChoices
.GetCount();
69 *choices
= new wxString
[n
];
71 for ( int i
= 0; i
< n
; i
++ )
73 (*choices
)[i
] = aChoices
[i
];
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 wxString
wxGetSingleChoice( const wxString
& message
,
84 const wxString
& caption
,
85 int n
, const wxString
*choices
,
87 int WXUNUSED(x
), int WXUNUSED(y
),
88 bool WXUNUSED(centre
),
89 int WXUNUSED(width
), int WXUNUSED(height
),
92 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
94 dialog
.SetSelection(initialSelection
);
97 if ( dialog
.ShowModal() == wxID_OK
)
98 choice
= dialog
.GetStringSelection();
103 wxString
wxGetSingleChoice( const wxString
& message
,
104 const wxString
& caption
,
105 const wxArrayString
& aChoices
,
109 int width
, int height
,
110 int initialSelection
)
113 int n
= ConvertWXArrayToC(aChoices
, &choices
);
114 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
115 x
, y
, centre
, width
, height
,
122 wxString
wxGetSingleChoice( const wxString
& message
,
123 const wxString
& caption
,
124 const wxArrayString
& choices
,
125 int initialSelection
,
128 return wxGetSingleChoice(message
, caption
, choices
, parent
,
129 wxDefaultCoord
, wxDefaultCoord
,
130 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
134 wxString
wxGetSingleChoice( const wxString
& message
,
135 const wxString
& caption
,
136 int n
, const wxString
*choices
,
137 int initialSelection
,
140 return wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
141 wxDefaultCoord
, wxDefaultCoord
,
142 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
146 int wxGetSingleChoiceIndex( const wxString
& message
,
147 const wxString
& caption
,
148 int n
, const wxString
*choices
,
150 int WXUNUSED(x
), int WXUNUSED(y
),
151 bool WXUNUSED(centre
),
152 int WXUNUSED(width
), int WXUNUSED(height
),
153 int initialSelection
)
155 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
157 dialog
.SetSelection(initialSelection
);
160 if ( dialog
.ShowModal() == wxID_OK
)
161 choice
= dialog
.GetSelection();
168 int wxGetSingleChoiceIndex( const wxString
& message
,
169 const wxString
& caption
,
170 const wxArrayString
& aChoices
,
174 int width
, int height
,
175 int initialSelection
)
178 int n
= ConvertWXArrayToC(aChoices
, &choices
);
179 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
180 x
, y
, centre
, width
, height
,
187 int wxGetSingleChoiceIndex( const wxString
& message
,
188 const wxString
& caption
,
189 const wxArrayString
& choices
,
190 int initialSelection
,
193 return wxGetSingleChoiceIndex(message
, caption
, choices
, parent
,
194 wxDefaultCoord
, wxDefaultCoord
,
195 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
200 int wxGetSingleChoiceIndex( const wxString
& message
,
201 const wxString
& caption
,
202 int n
, const wxString
*choices
,
203 int initialSelection
,
206 return wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
207 wxDefaultCoord
, wxDefaultCoord
,
208 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
213 void *wxGetSingleChoiceData( const wxString
& message
,
214 const wxString
& caption
,
215 int n
, const wxString
*choices
,
218 int WXUNUSED(x
), int WXUNUSED(y
),
219 bool WXUNUSED(centre
),
220 int WXUNUSED(width
), int WXUNUSED(height
),
221 int initialSelection
)
223 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
226 dialog
.SetSelection(initialSelection
);
229 if ( dialog
.ShowModal() == wxID_OK
)
230 data
= dialog
.GetSelectionData();
237 void *wxGetSingleChoiceData( const wxString
& message
,
238 const wxString
& caption
,
239 const wxArrayString
& aChoices
,
244 int width
, int height
,
245 int initialSelection
)
248 int n
= ConvertWXArrayToC(aChoices
, &choices
);
249 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
251 x
, y
, centre
, width
, height
,
258 void* wxGetSingleChoiceData( const wxString
& message
,
259 const wxString
& caption
,
260 const wxArrayString
& choices
,
262 int initialSelection
,
265 return wxGetSingleChoiceData(message
, caption
, choices
,
267 wxDefaultCoord
, wxDefaultCoord
,
268 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
272 void* wxGetSingleChoiceData( const wxString
& message
,
273 const wxString
& caption
,
274 int n
, const wxString
*choices
,
276 int initialSelection
,
279 return wxGetSingleChoiceData(message
, caption
, n
, choices
,
281 wxDefaultCoord
, wxDefaultCoord
,
282 true, wxCHOICE_WIDTH
, wxCHOICE_HEIGHT
,
287 int wxGetSelectedChoices(wxArrayInt
& selections
,
288 const wxString
& message
,
289 const wxString
& caption
,
290 int n
, const wxString
*choices
,
292 int WXUNUSED(x
), int WXUNUSED(y
),
293 bool WXUNUSED(centre
),
294 int WXUNUSED(width
), int WXUNUSED(height
))
296 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
298 // call this even if selections array is empty and this then (correctly)
299 // deselects the first item which is selected by default
300 dialog
.SetSelections(selections
);
302 if ( dialog
.ShowModal() != wxID_OK
)
304 // NB: intentionally do not clear the selections array here, the caller
305 // might want to preserve its original contents if the dialog was
310 selections
= dialog
.GetSelections();
311 return selections
.GetCount();
314 int wxGetSelectedChoices(wxArrayInt
& selections
,
315 const wxString
& message
,
316 const wxString
& caption
,
317 const wxArrayString
& aChoices
,
321 int width
, int height
)
324 int n
= ConvertWXArrayToC(aChoices
, &choices
);
325 int res
= wxGetSelectedChoices(selections
, message
, caption
,
327 x
, y
, centre
, width
, height
);
333 #if WXWIN_COMPATIBILITY_2_8
334 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
335 const wxString
& message
,
336 const wxString
& caption
,
337 int n
, const wxString
*choices
,
341 int width
, int height
)
343 int rc
= wxGetSelectedChoices(selections
, message
, caption
,
345 parent
, x
, y
, centre
, width
, height
);
355 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
356 const wxString
& message
,
357 const wxString
& caption
,
358 const wxArrayString
& aChoices
,
362 int width
, int height
)
364 int rc
= wxGetSelectedChoices(selections
, message
, caption
,
366 parent
, x
, y
, centre
, width
, height
);
375 #endif // WXWIN_COMPATIBILITY_2_8
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
382 const wxString
& message
,
383 const wxString
& caption
,
384 int n
, const wxString
*choices
,
389 // extract the buttons styles from the dialog one and remove them from it
390 const long styleBtns
= styleDlg
& (wxOK
| wxCANCEL
);
391 styleDlg
&= ~styleBtns
;
393 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
396 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
400 Add(CreateTextSizer(message
), wxSizerFlags().Expand().TripleBorder());
403 m_listbox
= CreateList(n
, choices
, styleLbox
);
406 m_listbox
->SetSelection(0);
409 Add(m_listbox
, wxSizerFlags().Expand().Proportion(1).TripleBorder(wxLEFT
| wxRIGHT
));
413 buttonSizer
= CreateSeparatedButtonSizer(styleBtns
);
416 topsizer
->Add(buttonSizer
, wxSizerFlags().Expand().DoubleBorder());
419 SetSizer( topsizer
);
421 topsizer
->SetSizeHints( this );
422 topsizer
->Fit( this );
424 if ( styleDlg
& wxCENTRE
)
427 m_listbox
->SetFocus();
432 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
433 const wxString
& message
,
434 const wxString
& caption
,
435 const wxArrayString
& choices
,
440 wxCArrayString
chs(choices
);
441 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
442 styleDlg
, pos
, styleLbox
);
445 wxListBoxBase
*wxAnyChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
447 return new wxListBox( this, wxID_LISTBOX
,
448 wxDefaultPosition
, wxDefaultSize
,
453 // ----------------------------------------------------------------------------
454 // wxSingleChoiceDialog
455 // ----------------------------------------------------------------------------
457 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
458 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
459 #ifndef __SMARTPHONE__
460 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
463 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown
)
467 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
469 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
470 const wxString
& message
,
471 const wxString
& caption
,
473 const wxString
*choices
,
478 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
483 m_selection
= n
> 0 ? 0 : -1;
487 for (int i
= 0; i
< n
; i
++)
488 m_listbox
->SetClientData(i
, clientData
[i
]);
494 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
495 const wxString
& message
,
496 const wxString
& caption
,
497 const wxArrayString
& choices
,
502 wxCArrayString
chs(choices
);
503 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
504 clientData
, style
, pos
);
508 void wxSingleChoiceDialog::SetSelection(int sel
)
510 wxCHECK_RET( sel
>= 0 && (unsigned)sel
< m_listbox
->GetCount(),
511 "Invalid initial selection" );
513 m_listbox
->SetSelection(sel
);
517 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
522 #ifndef __SMARTPHONE__
523 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
530 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent
& WXUNUSED(event
))
536 void wxSingleChoiceDialog::DoChoice()
538 m_selection
= m_listbox
->GetSelection();
539 m_stringSelection
= m_listbox
->GetStringSelection();
541 if ( m_listbox
->HasClientUntypedData() )
542 SetClientData(m_listbox
->GetClientData(m_selection
));
547 // ----------------------------------------------------------------------------
548 // wxMultiChoiceDialog
549 // ----------------------------------------------------------------------------
551 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
553 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
554 const wxString
& message
,
555 const wxString
& caption
,
557 const wxString
*choices
,
562 #if wxUSE_CHECKLISTBOX
563 styleLbox
= wxLB_ALWAYS_SB
;
565 styleLbox
= wxLB_ALWAYS_SB
| wxLB_EXTENDED
;
568 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
577 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
578 const wxString
& message
,
579 const wxString
& caption
,
580 const wxArrayString
& choices
,
584 wxCArrayString
chs(choices
);
585 return Create( parent
, message
, caption
, chs
.GetCount(),
586 chs
.GetStrings(), style
, pos
);
589 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
591 #if wxUSE_CHECKLISTBOX
592 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
595 // first clear all currently selected items
597 count
= checkListBox
->GetCount();
598 for ( n
= 0; n
< count
; ++n
)
600 if (checkListBox
->IsChecked(n
))
601 checkListBox
->Check(n
, false);
604 // now select the ones which should be selected
605 count
= selections
.GetCount();
606 for ( n
= 0; n
< count
; n
++ )
608 checkListBox
->Check(selections
[n
]);
615 // first clear all currently selected items
617 count
= m_listbox
->GetCount();
618 for ( n
= 0; n
< count
; ++n
)
620 m_listbox
->Deselect(n
);
623 // now select the ones which should be selected
624 count
= selections
.GetCount();
625 for ( n
= 0; n
< count
; n
++ )
627 m_listbox
->Select(selections
[n
]);
631 bool wxMultiChoiceDialog::TransferDataFromWindow()
633 m_selections
.Empty();
635 #if wxUSE_CHECKLISTBOX
636 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
639 size_t count
= checkListBox
->GetCount();
640 for ( size_t n
= 0; n
< count
; n
++ )
642 if ( checkListBox
->IsChecked(n
) )
649 size_t count
= m_listbox
->GetCount();
650 for ( size_t n
= 0; n
< count
; n
++ )
652 if ( m_listbox
->IsSelected(n
) )
659 #if wxUSE_CHECKLISTBOX
661 wxListBoxBase
*wxMultiChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
663 return new wxCheckListBox( this, wxID_LISTBOX
,
664 wxDefaultPosition
, wxDefaultSize
,
669 #endif // wxUSE_CHECKLISTBOX
671 #endif // wxUSE_CHOICEDLG