]>
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/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
) )
91 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
93 if ( dialog
.ShowModal() == wxID_OK
)
94 choice
= dialog
.GetStringSelection();
99 wxString
wxGetSingleChoice( const wxString
& message
,
100 const wxString
& caption
,
101 const wxArrayString
& aChoices
,
105 int width
, int height
)
108 int n
= ConvertWXArrayToC(aChoices
, &choices
);
109 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
110 x
, y
, centre
, width
, height
);
116 int wxGetSingleChoiceIndex( const wxString
& message
,
117 const wxString
& caption
,
118 int n
, const wxString
*choices
,
120 int WXUNUSED(x
), int WXUNUSED(y
),
121 bool WXUNUSED(centre
),
122 int WXUNUSED(width
), int WXUNUSED(height
) )
124 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
126 if ( dialog
.ShowModal() == wxID_OK
)
127 choice
= dialog
.GetSelection();
134 int wxGetSingleChoiceIndex( const wxString
& message
,
135 const wxString
& caption
,
136 const wxArrayString
& aChoices
,
140 int width
, int height
)
143 int n
= ConvertWXArrayToC(aChoices
, &choices
);
144 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
145 x
, y
, centre
, width
, height
);
151 void *wxGetSingleChoiceData( const wxString
& message
,
152 const wxString
& caption
,
153 int n
, const wxString
*choices
,
156 int WXUNUSED(x
), int WXUNUSED(y
),
157 bool WXUNUSED(centre
),
158 int WXUNUSED(width
), int WXUNUSED(height
) )
160 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
161 (char **)client_data
);
163 if ( dialog
.ShowModal() == wxID_OK
)
164 data
= dialog
.GetSelectionClientData();
171 void *wxGetSingleChoiceData( const wxString
& message
,
172 const wxString
& caption
,
173 const wxArrayString
& aChoices
,
178 int width
, int height
)
181 int n
= ConvertWXArrayToC(aChoices
, &choices
);
182 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
184 x
, y
, centre
, width
, height
);
190 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
191 const wxString
& message
,
192 const wxString
& caption
,
193 int n
, const wxString
*choices
,
195 int WXUNUSED(x
), int WXUNUSED(y
),
196 bool WXUNUSED(centre
),
197 int WXUNUSED(width
), int WXUNUSED(height
))
199 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
201 // call this even if selections array is empty and this then (correctly)
202 // deselects the first item which is selected by default
203 dialog
.SetSelections(selections
);
205 if ( dialog
.ShowModal() == wxID_OK
)
206 selections
= dialog
.GetSelections();
210 return selections
.GetCount();
213 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
214 const wxString
& message
,
215 const wxString
& caption
,
216 const wxArrayString
& aChoices
,
220 int width
, int height
)
223 int n
= ConvertWXArrayToC(aChoices
, &choices
);
224 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
226 x
, y
, centre
, width
, height
);
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
237 const wxString
& message
,
238 const wxString
& caption
,
239 int n
, const wxString
*choices
,
246 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
& (~wxCANCEL
) ) )
249 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
253 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
257 Add(CreateTextSizer(message
), wxSizerFlags().Expand().TripleBorder());
260 m_listbox
= CreateList(n
, choices
, styleLbox
);
263 m_listbox
->SetSelection(0);
266 Add(m_listbox
, wxSizerFlags().Expand().TripleBorder(wxLEFT
| wxRIGHT
));
270 buttonSizer
= CreateSeparatedButtonSizer(styleDlg
& ButtonSizerFlags
);
273 topsizer
->Add(buttonSizer
, wxSizerFlags().Expand().DoubleBorder());
276 SetSizer( topsizer
);
278 topsizer
->SetSizeHints( this );
279 topsizer
->Fit( this );
281 if ( styleDlg
& wxCENTRE
)
284 m_listbox
->SetFocus();
289 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
290 const wxString
& message
,
291 const wxString
& caption
,
292 const wxArrayString
& choices
,
297 wxCArrayString
chs(choices
);
298 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
299 styleDlg
, pos
, styleLbox
);
302 wxListBoxBase
*wxAnyChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
304 return new wxListBox( this, wxID_LISTBOX
,
305 wxDefaultPosition
, wxDefaultSize
,
310 // ----------------------------------------------------------------------------
311 // wxSingleChoiceDialog
312 // ----------------------------------------------------------------------------
314 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
315 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
316 #ifndef __SMARTPHONE__
317 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
320 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown
)
324 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
326 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
327 const wxString
& message
,
328 const wxString
& caption
,
330 const wxString
*choices
,
333 const wxPoint
& WXUNUSED(pos
))
335 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
338 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
339 const wxString
& message
,
340 const wxString
& caption
,
341 const wxArrayString
& choices
,
344 const wxPoint
& WXUNUSED(pos
))
346 Create(parent
, message
, caption
, choices
, clientData
, style
);
349 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
350 const wxString
& message
,
351 const wxString
& caption
,
353 const wxString
*choices
,
358 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
363 m_selection
= n
> 0 ? 0 : -1;
367 for (int i
= 0; i
< n
; i
++)
368 m_listbox
->SetClientData(i
, clientData
[i
]);
374 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
375 const wxString
& message
,
376 const wxString
& caption
,
377 const wxArrayString
& choices
,
382 wxCArrayString
chs(choices
);
383 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
384 clientData
, style
, pos
);
388 void wxSingleChoiceDialog::SetSelection(int sel
)
390 m_listbox
->SetSelection(sel
);
394 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
399 #ifndef __SMARTPHONE__
400 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
407 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent
& WXUNUSED(event
))
413 void wxSingleChoiceDialog::DoChoice()
415 m_selection
= m_listbox
->GetSelection();
416 m_stringSelection
= m_listbox
->GetStringSelection();
418 if ( m_listbox
->HasClientUntypedData() )
419 SetClientData(m_listbox
->GetClientData(m_selection
));
424 // ----------------------------------------------------------------------------
425 // wxMultiChoiceDialog
426 // ----------------------------------------------------------------------------
428 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
430 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
431 const wxString
& message
,
432 const wxString
& caption
,
434 const wxString
*choices
,
439 #if wxUSE_CHECKLISTBOX
440 styleLbox
= wxLB_ALWAYS_SB
;
442 styleLbox
= wxLB_ALWAYS_SB
| wxLB_EXTENDED
;
445 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
454 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
455 const wxString
& message
,
456 const wxString
& caption
,
457 const wxArrayString
& choices
,
461 wxCArrayString
chs(choices
);
462 return Create( parent
, message
, caption
, chs
.GetCount(),
463 chs
.GetStrings(), style
, pos
);
466 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
468 #if wxUSE_CHECKLISTBOX
469 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
472 // first clear all currently selected items
474 count
= checkListBox
->GetCount();
475 for ( n
= 0; n
< count
; ++n
)
477 if (checkListBox
->IsChecked(n
))
478 checkListBox
->Check(n
, false);
481 // now select the ones which should be selected
482 count
= selections
.GetCount();
483 for ( n
= 0; n
< count
; n
++ )
485 checkListBox
->Check(selections
[n
]);
492 // first clear all currently selected items
494 count
= m_listbox
->GetCount();
495 for ( n
= 0; n
< count
; ++n
)
497 m_listbox
->Deselect(n
);
500 // now select the ones which should be selected
501 count
= selections
.GetCount();
502 for ( n
= 0; n
< count
; n
++ )
504 m_listbox
->Select(selections
[n
]);
508 bool wxMultiChoiceDialog::TransferDataFromWindow()
510 m_selections
.Empty();
512 #if wxUSE_CHECKLISTBOX
513 wxCheckListBox
* checkListBox
= wxDynamicCast(m_listbox
, wxCheckListBox
);
516 size_t count
= checkListBox
->GetCount();
517 for ( size_t n
= 0; n
< count
; n
++ )
519 if ( checkListBox
->IsChecked(n
) )
526 size_t count
= m_listbox
->GetCount();
527 for ( size_t n
= 0; n
< count
; n
++ )
529 if ( m_listbox
->IsSelected(n
) )
536 #if wxUSE_CHECKLISTBOX
538 wxListBoxBase
*wxMultiChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
540 return new wxCheckListBox( this, wxID_LISTBOX
,
541 wxDefaultPosition
, wxDefaultSize
,
546 #endif // wxUSE_CHECKLISTBOX
548 #endif // wxUSE_CHOICEDLG