]>
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 size_t wxGetMultipleChoices(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
)
207 selections
= dialog
.GetSelections();
211 return selections
.GetCount();
214 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
215 const wxString
& message
,
216 const wxString
& caption
,
217 const wxArrayString
& aChoices
,
221 int width
, int height
)
224 int n
= ConvertWXArrayToC(aChoices
, &choices
);
225 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
227 x
, y
, centre
, width
, height
);
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
238 const wxString
& message
,
239 const wxString
& caption
,
240 int n
, const wxString
*choices
,
247 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
& (~wxCANCEL
) ) )
250 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
254 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
258 Add(CreateTextSizer(message
), wxSizerFlags().Expand().TripleBorder());
261 m_listbox
= CreateList(n
, choices
, styleLbox
);
264 m_listbox
->SetSelection(0);
267 Add(m_listbox
, wxSizerFlags().Expand().Proportion(1).TripleBorder(wxLEFT
| wxRIGHT
));
271 buttonSizer
= CreateSeparatedButtonSizer(styleDlg
& ButtonSizerFlags
);
274 topsizer
->Add(buttonSizer
, wxSizerFlags().Expand().DoubleBorder());
277 SetSizer( topsizer
);
279 topsizer
->SetSizeHints( this );
280 topsizer
->Fit( this );
282 if ( styleDlg
& wxCENTRE
)
285 m_listbox
->SetFocus();
290 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
291 const wxString
& message
,
292 const wxString
& caption
,
293 const wxArrayString
& choices
,
298 wxCArrayString
chs(choices
);
299 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
300 styleDlg
, pos
, styleLbox
);
303 wxListBoxBase
*wxAnyChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
305 wxSize size
= wxDefaultSize
;
306 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA
)
307 size
= wxSize(300, 200);
308 return new wxListBox( this, wxID_LISTBOX
,
309 wxDefaultPosition
, size
,
314 // ----------------------------------------------------------------------------
315 // wxSingleChoiceDialog
316 // ----------------------------------------------------------------------------
318 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
319 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
320 #ifndef __SMARTPHONE__
321 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
324 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown
)
328 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
330 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
331 const wxString
& message
,
332 const wxString
& caption
,
334 const wxString
*choices
,
337 const wxPoint
& WXUNUSED(pos
))
339 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
342 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
343 const wxString
& message
,
344 const wxString
& caption
,
345 const wxArrayString
& choices
,
348 const wxPoint
& WXUNUSED(pos
))
350 Create(parent
, message
, caption
, choices
, clientData
, style
);
353 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
354 const wxString
& message
,
355 const wxString
& caption
,
357 const wxString
*choices
,
362 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
367 m_selection
= n
> 0 ? 0 : -1;
371 for (int i
= 0; i
< n
; i
++)
372 m_listbox
->SetClientData(i
, clientData
[i
]);
378 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
379 const wxString
& message
,
380 const wxString
& caption
,
381 const wxArrayString
& choices
,
386 wxCArrayString
chs(choices
);
387 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
388 clientData
, style
, pos
);
392 void wxSingleChoiceDialog::SetSelection(int sel
)
394 m_listbox
->SetSelection(sel
);
398 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
403 #ifndef __SMARTPHONE__
404 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
411 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent
& WXUNUSED(event
))
417 void wxSingleChoiceDialog::DoChoice()
419 m_selection
= m_listbox
->GetSelection();
420 m_stringSelection
= m_listbox
->GetStringSelection();
422 if ( m_listbox
->HasClientUntypedData() )
423 SetClientData(m_listbox
->GetClientData(m_selection
));
428 // ----------------------------------------------------------------------------
429 // wxMultiChoiceDialog
430 // ----------------------------------------------------------------------------
432 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
434 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
435 const wxString
& message
,
436 const wxString
& caption
,
438 const wxString
*choices
,
443 #if wxUSE_CHECKLISTBOX
444 styleLbox
= wxLB_ALWAYS_SB
;
446 styleLbox
= wxLB_ALWAYS_SB
| wxLB_EXTENDED
;
449 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
458 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
459 const wxString
& message
,
460 const wxString
& caption
,
461 const wxArrayString
& choices
,
465 wxCArrayString
chs(choices
);
466 return Create( parent
, message
, caption
, chs
.GetCount(),
467 chs
.GetStrings(), style
, pos
);
470 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
472 #if wxUSE_CHECKLISTBOX
473 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
476 // first clear all currently selected items
478 count
= checkListBox
->GetCount();
479 for ( n
= 0; n
< count
; ++n
)
481 if (checkListBox
->IsChecked(n
))
482 checkListBox
->Check(n
, false);
485 // now select the ones which should be selected
486 count
= selections
.GetCount();
487 for ( n
= 0; n
< count
; n
++ )
489 checkListBox
->Check(selections
[n
]);
496 // first clear all currently selected items
498 count
= m_listbox
->GetCount();
499 for ( n
= 0; n
< count
; ++n
)
501 m_listbox
->Deselect(n
);
504 // now select the ones which should be selected
505 count
= selections
.GetCount();
506 for ( n
= 0; n
< count
; n
++ )
508 m_listbox
->Select(selections
[n
]);
512 bool wxMultiChoiceDialog::TransferDataFromWindow()
514 m_selections
.Empty();
516 #if wxUSE_CHECKLISTBOX
517 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
520 size_t count
= checkListBox
->GetCount();
521 for ( size_t n
= 0; n
< count
; n
++ )
523 if ( checkListBox
->IsChecked(n
) )
530 size_t count
= m_listbox
->GetCount();
531 for ( size_t n
= 0; n
< count
; n
++ )
533 if ( m_listbox
->IsSelected(n
) )
540 #if wxUSE_CHECKLISTBOX
542 wxListBoxBase
*wxMultiChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
544 wxSize size
= wxDefaultSize
;
545 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA
)
546 size
= wxSize(300, 200);
548 return new wxCheckListBox( this, wxID_LISTBOX
,
549 wxDefaultPosition
, size
,
554 #endif // wxUSE_CHECKLISTBOX
556 #endif // wxUSE_CHOICEDLG