]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "choicdgg.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/dialog.h"
37 #include "wx/button.h"
38 #include "wx/listbox.h"
39 #include "wx/stattext.h"
42 #include "wx/arrstr.h"
46 #include "wx/statline.h"
49 #include "wx/generic/choicdgg.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 #define wxID_LISTBOX 3000
57 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
61 /* Macro for avoiding #ifdefs when value have to be different depending on size of
65 #if defined(__SMARTPHONE__)
66 #define wxLARGESMALL(large,small) small
68 #define wxLARGESMALL(large,small) large
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // convert wxArrayString into a wxString[] which must be delete[]d by caller
76 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
78 // ============================================================================
80 // ============================================================================
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
88 int n
= aChoices
.GetCount();
89 *choices
= new wxString
[n
];
91 for ( int i
= 0; i
< n
; i
++ )
93 (*choices
)[i
] = aChoices
[i
];
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 wxString
wxGetSingleChoice( const wxString
& message
,
104 const wxString
& caption
,
105 int n
, const wxString
*choices
,
107 int WXUNUSED(x
), int WXUNUSED(y
),
108 bool WXUNUSED(centre
),
109 int WXUNUSED(width
), int WXUNUSED(height
) )
111 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
113 if ( dialog
.ShowModal() == wxID_OK
)
114 choice
= dialog
.GetStringSelection();
119 wxString
wxGetSingleChoice( const wxString
& message
,
120 const wxString
& caption
,
121 const wxArrayString
& aChoices
,
125 int width
, int height
)
128 int n
= ConvertWXArrayToC(aChoices
, &choices
);
129 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
130 x
, y
, centre
, width
, height
);
136 int wxGetSingleChoiceIndex( const wxString
& message
,
137 const wxString
& caption
,
138 int n
, const wxString
*choices
,
140 int WXUNUSED(x
), int WXUNUSED(y
),
141 bool WXUNUSED(centre
),
142 int WXUNUSED(width
), int WXUNUSED(height
) )
144 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
146 if ( dialog
.ShowModal() == wxID_OK
)
147 choice
= dialog
.GetSelection();
154 int wxGetSingleChoiceIndex( const wxString
& message
,
155 const wxString
& caption
,
156 const wxArrayString
& aChoices
,
160 int width
, int height
)
163 int n
= ConvertWXArrayToC(aChoices
, &choices
);
164 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
165 x
, y
, centre
, width
, height
);
171 void *wxGetSingleChoiceData( const wxString
& message
,
172 const wxString
& caption
,
173 int n
, const wxString
*choices
,
176 int WXUNUSED(x
), int WXUNUSED(y
),
177 bool WXUNUSED(centre
),
178 int WXUNUSED(width
), int WXUNUSED(height
) )
180 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
181 (char **)client_data
);
183 if ( dialog
.ShowModal() == wxID_OK
)
184 data
= dialog
.GetSelectionClientData();
191 void *wxGetSingleChoiceData( const wxString
& message
,
192 const wxString
& caption
,
193 const wxArrayString
& aChoices
,
198 int width
, int height
)
201 int n
= ConvertWXArrayToC(aChoices
, &choices
);
202 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
204 x
, y
, centre
, width
, height
);
210 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
211 const wxString
& message
,
212 const wxString
& caption
,
213 int n
, const wxString
*choices
,
215 int WXUNUSED(x
), int WXUNUSED(y
),
216 bool WXUNUSED(centre
),
217 int WXUNUSED(width
), int WXUNUSED(height
))
219 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
221 if ( !selections
.IsEmpty() )
222 dialog
.SetSelections(selections
);
224 if ( dialog
.ShowModal() == wxID_OK
)
225 selections
= dialog
.GetSelections();
229 return selections
.GetCount();
232 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
233 const wxString
& message
,
234 const wxString
& caption
,
235 const wxArrayString
& aChoices
,
239 int width
, int height
)
242 int n
= ConvertWXArrayToC(aChoices
, &choices
);
243 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
245 x
, y
, centre
, width
, height
);
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
256 const wxString
& message
,
257 const wxString
& caption
,
258 int n
, const wxString
*choices
,
263 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
266 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
269 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, wxLARGESMALL(10,0) );
272 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
273 wxDefaultPosition
, wxDefaultSize
,
277 m_listbox
->SetSelection(0);
279 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, wxLARGESMALL(15,0) );
281 #ifdef __SMARTPHONE__
283 SetRightMenu(wxID_CANCEL
, _("Cancel"));
285 #else // __SMARTPHONE__/!__SMARTPHONE__
289 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
293 topsizer
->Add( CreateButtonSizer( styleDlg
& (wxOK
|wxCANCEL
) ), 0, wxCENTRE
| wxALL
, 10 );
295 #endif // !__SMARTPHONE__
297 SetAutoLayout( true );
298 SetSizer( topsizer
);
300 topsizer
->SetSizeHints( this );
301 topsizer
->Fit( this );
303 if ( styleDlg
& wxCENTRE
)
306 m_listbox
->SetFocus();
311 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
312 const wxString
& message
,
313 const wxString
& caption
,
314 const wxArrayString
& choices
,
319 wxCArrayString
chs(choices
);
320 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
321 styleDlg
, pos
, styleLbox
);
324 // ----------------------------------------------------------------------------
325 // wxSingleChoiceDialog
326 // ----------------------------------------------------------------------------
328 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
329 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
330 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
333 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
335 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
336 const wxString
& message
,
337 const wxString
& caption
,
339 const wxString
*choices
,
342 const wxPoint
& WXUNUSED(pos
))
344 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
347 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
348 const wxString
& message
,
349 const wxString
& caption
,
350 const wxArrayString
& choices
,
353 const wxPoint
& WXUNUSED(pos
))
355 Create(parent
, message
, caption
, choices
, clientData
, style
);
358 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
359 const wxString
& message
,
360 const wxString
& caption
,
362 const wxString
*choices
,
367 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
372 m_selection
= n
> 0 ? 0 : -1;
376 for (int i
= 0; i
< n
; i
++)
377 m_listbox
->SetClientData(i
, clientData
[i
]);
383 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
384 const wxString
& message
,
385 const wxString
& caption
,
386 const wxArrayString
& choices
,
391 wxCArrayString
chs(choices
);
392 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
393 clientData
, style
, pos
);
397 void wxSingleChoiceDialog::SetSelection(int sel
)
399 m_listbox
->SetSelection(sel
);
403 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
405 m_selection
= m_listbox
->GetSelection();
406 m_stringSelection
= m_listbox
->GetStringSelection();
407 if ( m_listbox
->HasClientUntypedData() )
408 SetClientData(m_listbox
->GetClientData(m_selection
));
412 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
414 m_selection
= m_listbox
->GetSelection();
415 m_stringSelection
= m_listbox
->GetStringSelection();
417 if ( m_listbox
->HasClientUntypedData() )
418 SetClientData(m_listbox
->GetClientData(m_selection
));
423 // ----------------------------------------------------------------------------
424 // wxMultiChoiceDialog
425 // ----------------------------------------------------------------------------
427 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
429 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
430 const wxString
& message
,
431 const wxString
& caption
,
433 const wxString
*choices
,
437 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
440 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
446 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
447 const wxString
& message
,
448 const wxString
& caption
,
449 const wxArrayString
& choices
,
453 wxCArrayString
chs(choices
);
454 return Create( parent
, message
, caption
, chs
.GetCount(),
455 chs
.GetStrings(), style
, pos
);
458 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
460 // first clear all currently selected items
462 count
= m_listbox
->GetCount();
463 for ( n
= 0; n
< count
; ++n
)
465 m_listbox
->Deselect(n
);
468 // now select the ones which should be selected
469 count
= selections
.GetCount();
470 for ( n
= 0; n
< count
; n
++ )
472 m_listbox
->Select(selections
[n
]);
476 bool wxMultiChoiceDialog::TransferDataFromWindow()
478 m_selections
.Empty();
479 size_t count
= m_listbox
->GetCount();
480 for ( size_t n
= 0; n
< count
; n
++ )
482 if ( m_listbox
->IsSelected(n
) )
489 #endif // wxUSE_CHOICEDLG