]>
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
,
293 // extract the buttons styles from the dialog one and remove them from it
294 const long styleBtns
= styleDlg
& (wxOK
| wxCANCEL
);
295 styleDlg
&= ~styleBtns
;
297 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
300 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
304 Add(CreateTextSizer(message
), wxSizerFlags().Expand().TripleBorder());
307 m_listbox
= CreateList(n
, choices
, styleLbox
);
310 m_listbox
->SetSelection(0);
313 Add(m_listbox
, wxSizerFlags().Expand().Proportion(1).TripleBorder(wxLEFT
| wxRIGHT
));
317 buttonSizer
= CreateSeparatedButtonSizer(styleBtns
);
320 topsizer
->Add(buttonSizer
, wxSizerFlags().Expand().DoubleBorder());
323 SetSizer( topsizer
);
325 topsizer
->SetSizeHints( this );
326 topsizer
->Fit( this );
328 if ( styleDlg
& wxCENTRE
)
331 m_listbox
->SetFocus();
336 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
337 const wxString
& message
,
338 const wxString
& caption
,
339 const wxArrayString
& choices
,
344 wxCArrayString
chs(choices
);
345 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
346 styleDlg
, pos
, styleLbox
);
349 wxListBoxBase
*wxAnyChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
351 return new wxListBox( this, wxID_LISTBOX
,
352 wxDefaultPosition
, wxDefaultSize
,
357 // ----------------------------------------------------------------------------
358 // wxSingleChoiceDialog
359 // ----------------------------------------------------------------------------
361 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
362 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
363 #ifndef __SMARTPHONE__
364 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
367 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown
)
371 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
373 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
374 const wxString
& message
,
375 const wxString
& caption
,
377 const wxString
*choices
,
380 const wxPoint
& WXUNUSED(pos
))
382 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
385 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
386 const wxString
& message
,
387 const wxString
& caption
,
388 const wxArrayString
& choices
,
391 const wxPoint
& WXUNUSED(pos
))
393 Create(parent
, message
, caption
, choices
, clientData
, style
);
396 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
397 const wxString
& message
,
398 const wxString
& caption
,
400 const wxString
*choices
,
405 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
410 m_selection
= n
> 0 ? 0 : -1;
414 for (int i
= 0; i
< n
; i
++)
415 m_listbox
->SetClientData(i
, clientData
[i
]);
421 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
422 const wxString
& message
,
423 const wxString
& caption
,
424 const wxArrayString
& choices
,
429 wxCArrayString
chs(choices
);
430 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
431 clientData
, style
, pos
);
435 void wxSingleChoiceDialog::SetSelection(int sel
)
437 m_listbox
->SetSelection(sel
);
441 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
446 #ifndef __SMARTPHONE__
447 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
454 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent
& WXUNUSED(event
))
460 void wxSingleChoiceDialog::DoChoice()
462 m_selection
= m_listbox
->GetSelection();
463 m_stringSelection
= m_listbox
->GetStringSelection();
465 if ( m_listbox
->HasClientUntypedData() )
466 SetClientData(m_listbox
->GetClientData(m_selection
));
471 // ----------------------------------------------------------------------------
472 // wxMultiChoiceDialog
473 // ----------------------------------------------------------------------------
475 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
477 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
478 const wxString
& message
,
479 const wxString
& caption
,
481 const wxString
*choices
,
486 #if wxUSE_CHECKLISTBOX
487 styleLbox
= wxLB_ALWAYS_SB
;
489 styleLbox
= wxLB_ALWAYS_SB
| wxLB_EXTENDED
;
492 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
501 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
502 const wxString
& message
,
503 const wxString
& caption
,
504 const wxArrayString
& choices
,
508 wxCArrayString
chs(choices
);
509 return Create( parent
, message
, caption
, chs
.GetCount(),
510 chs
.GetStrings(), style
, pos
);
513 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
515 #if wxUSE_CHECKLISTBOX
516 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
519 // first clear all currently selected items
521 count
= checkListBox
->GetCount();
522 for ( n
= 0; n
< count
; ++n
)
524 if (checkListBox
->IsChecked(n
))
525 checkListBox
->Check(n
, false);
528 // now select the ones which should be selected
529 count
= selections
.GetCount();
530 for ( n
= 0; n
< count
; n
++ )
532 checkListBox
->Check(selections
[n
]);
539 // first clear all currently selected items
541 count
= m_listbox
->GetCount();
542 for ( n
= 0; n
< count
; ++n
)
544 m_listbox
->Deselect(n
);
547 // now select the ones which should be selected
548 count
= selections
.GetCount();
549 for ( n
= 0; n
< count
; n
++ )
551 m_listbox
->Select(selections
[n
]);
555 bool wxMultiChoiceDialog::TransferDataFromWindow()
557 m_selections
.Empty();
559 #if wxUSE_CHECKLISTBOX
560 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
563 size_t count
= checkListBox
->GetCount();
564 for ( size_t n
= 0; n
< count
; n
++ )
566 if ( checkListBox
->IsChecked(n
) )
573 size_t count
= m_listbox
->GetCount();
574 for ( size_t n
= 0; n
< count
; n
++ )
576 if ( m_listbox
->IsSelected(n
) )
583 #if wxUSE_CHECKLISTBOX
585 wxListBoxBase
*wxMultiChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
587 return new wxCheckListBox( this, wxID_LISTBOX
,
588 wxDefaultPosition
, wxDefaultSize
,
593 #endif // wxUSE_CHECKLISTBOX
595 #endif // wxUSE_CHOICEDLG