]>
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
62 device we display on - take it from something like wxDesktopPolicy in the future
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 // call this even if selections array is empty and this then (correctly)
222 // deselects the first item which is selected by default
223 dialog
.SetSelections(selections
);
225 if ( dialog
.ShowModal() == wxID_OK
)
226 selections
= dialog
.GetSelections();
230 return selections
.GetCount();
233 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
234 const wxString
& message
,
235 const wxString
& caption
,
236 const wxArrayString
& aChoices
,
240 int width
, int height
)
243 int n
= ConvertWXArrayToC(aChoices
, &choices
);
244 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
246 x
, y
, centre
, width
, height
);
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
257 const wxString
& message
,
258 const wxString
& caption
,
259 int n
, const wxString
*choices
,
264 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
267 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
270 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, wxLARGESMALL(10,0) );
273 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
274 wxDefaultPosition
, wxDefaultSize
,
278 m_listbox
->SetSelection(0);
280 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, wxLARGESMALL(15,0) );
282 // smart phones does not support or do not waste space for wxButtons
283 #ifdef __SMARTPHONE__
285 SetRightMenu(wxID_CANCEL
, _("Cancel"));
287 #else // __SMARTPHONE__/!__SMARTPHONE__
291 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
295 topsizer
->Add( CreateButtonSizer( styleDlg
& (wxOK
|wxCANCEL
) ), 0, wxCENTRE
| wxALL
, 10 );
297 #endif // !__SMARTPHONE__
299 SetAutoLayout( true );
300 SetSizer( topsizer
);
302 topsizer
->SetSizeHints( this );
303 topsizer
->Fit( this );
305 if ( styleDlg
& wxCENTRE
)
308 m_listbox
->SetFocus();
313 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
314 const wxString
& message
,
315 const wxString
& caption
,
316 const wxArrayString
& choices
,
321 wxCArrayString
chs(choices
);
322 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
323 styleDlg
, pos
, styleLbox
);
326 // ----------------------------------------------------------------------------
327 // wxSingleChoiceDialog
328 // ----------------------------------------------------------------------------
330 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
331 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
332 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
335 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
337 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
338 const wxString
& message
,
339 const wxString
& caption
,
341 const wxString
*choices
,
344 const wxPoint
& WXUNUSED(pos
))
346 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
349 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
350 const wxString
& message
,
351 const wxString
& caption
,
352 const wxArrayString
& choices
,
355 const wxPoint
& WXUNUSED(pos
))
357 Create(parent
, message
, caption
, choices
, clientData
, style
);
360 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
361 const wxString
& message
,
362 const wxString
& caption
,
364 const wxString
*choices
,
369 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
374 m_selection
= n
> 0 ? 0 : -1;
378 for (int i
= 0; i
< n
; i
++)
379 m_listbox
->SetClientData(i
, clientData
[i
]);
385 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
386 const wxString
& message
,
387 const wxString
& caption
,
388 const wxArrayString
& choices
,
393 wxCArrayString
chs(choices
);
394 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
395 clientData
, style
, pos
);
399 void wxSingleChoiceDialog::SetSelection(int sel
)
401 m_listbox
->SetSelection(sel
);
405 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
407 m_selection
= m_listbox
->GetSelection();
408 m_stringSelection
= m_listbox
->GetStringSelection();
409 if ( m_listbox
->HasClientUntypedData() )
410 SetClientData(m_listbox
->GetClientData(m_selection
));
414 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
416 m_selection
= m_listbox
->GetSelection();
417 m_stringSelection
= m_listbox
->GetStringSelection();
419 if ( m_listbox
->HasClientUntypedData() )
420 SetClientData(m_listbox
->GetClientData(m_selection
));
425 // ----------------------------------------------------------------------------
426 // wxMultiChoiceDialog
427 // ----------------------------------------------------------------------------
429 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
431 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
432 const wxString
& message
,
433 const wxString
& caption
,
435 const wxString
*choices
,
439 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
442 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
448 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
449 const wxString
& message
,
450 const wxString
& caption
,
451 const wxArrayString
& choices
,
455 wxCArrayString
chs(choices
);
456 return Create( parent
, message
, caption
, chs
.GetCount(),
457 chs
.GetStrings(), style
, pos
);
460 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
462 // first clear all currently selected items
464 count
= m_listbox
->GetCount();
465 for ( n
= 0; n
< count
; ++n
)
467 m_listbox
->Deselect(n
);
470 // now select the ones which should be selected
471 count
= selections
.GetCount();
472 for ( n
= 0; n
< count
; n
++ )
474 m_listbox
->Select(selections
[n
]);
478 bool wxMultiChoiceDialog::TransferDataFromWindow()
480 m_selections
.Empty();
481 size_t count
= m_listbox
->GetCount();
482 for ( size_t n
= 0; n
< count
; n
++ )
484 if ( m_listbox
->IsSelected(n
) )
491 #endif // wxUSE_CHOICEDLG