]>
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"
43 #include "wx/statline.h"
46 #include "wx/generic/choicdgg.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #define wxID_LISTBOX 3000
54 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
58 /* Macro for avoiding #ifdefs when value have to be different depending on size of
59 device we display on - take it from something like wxDesktopPolicy in the future
62 #if defined(__SMARTPHONE__)
63 #define wxLARGESMALL(large,small) small
65 #define wxLARGESMALL(large,small) large
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // convert wxArrayString into a wxString[] which must be delete[]d by caller
73 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
85 int n
= aChoices
.GetCount();
86 *choices
= new wxString
[n
];
88 for ( int i
= 0; i
< n
; i
++ )
90 (*choices
)[i
] = aChoices
[i
];
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 wxString
wxGetSingleChoice( const wxString
& message
,
101 const wxString
& caption
,
102 int n
, const wxString
*choices
,
104 int WXUNUSED(x
), int WXUNUSED(y
),
105 bool WXUNUSED(centre
),
106 int WXUNUSED(width
), int WXUNUSED(height
) )
108 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
110 if ( dialog
.ShowModal() == wxID_OK
)
111 choice
= dialog
.GetStringSelection();
116 wxString
wxGetSingleChoice( const wxString
& message
,
117 const wxString
& caption
,
118 const wxArrayString
& aChoices
,
122 int width
, int height
)
125 int n
= ConvertWXArrayToC(aChoices
, &choices
);
126 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
127 x
, y
, centre
, width
, height
);
133 int wxGetSingleChoiceIndex( const wxString
& message
,
134 const wxString
& caption
,
135 int n
, const wxString
*choices
,
137 int WXUNUSED(x
), int WXUNUSED(y
),
138 bool WXUNUSED(centre
),
139 int WXUNUSED(width
), int WXUNUSED(height
) )
141 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
143 if ( dialog
.ShowModal() == wxID_OK
)
144 choice
= dialog
.GetSelection();
151 int wxGetSingleChoiceIndex( const wxString
& message
,
152 const wxString
& caption
,
153 const wxArrayString
& aChoices
,
157 int width
, int height
)
160 int n
= ConvertWXArrayToC(aChoices
, &choices
);
161 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
162 x
, y
, centre
, width
, height
);
168 void *wxGetSingleChoiceData( const wxString
& message
,
169 const wxString
& caption
,
170 int n
, const wxString
*choices
,
173 int WXUNUSED(x
), int WXUNUSED(y
),
174 bool WXUNUSED(centre
),
175 int WXUNUSED(width
), int WXUNUSED(height
) )
177 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
178 (char **)client_data
);
180 if ( dialog
.ShowModal() == wxID_OK
)
181 data
= dialog
.GetSelectionClientData();
188 void *wxGetSingleChoiceData( const wxString
& message
,
189 const wxString
& caption
,
190 const wxArrayString
& aChoices
,
195 int width
, int height
)
198 int n
= ConvertWXArrayToC(aChoices
, &choices
);
199 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
201 x
, y
, centre
, width
, height
);
207 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
208 const wxString
& message
,
209 const wxString
& caption
,
210 int n
, const wxString
*choices
,
212 int WXUNUSED(x
), int WXUNUSED(y
),
213 bool WXUNUSED(centre
),
214 int WXUNUSED(width
), int WXUNUSED(height
))
216 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
218 // call this even if selections array is empty and this then (correctly)
219 // deselects the first item which is selected by default
220 dialog
.SetSelections(selections
);
222 if ( dialog
.ShowModal() == wxID_OK
)
223 selections
= dialog
.GetSelections();
227 return selections
.GetCount();
230 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
231 const wxString
& message
,
232 const wxString
& caption
,
233 const wxArrayString
& aChoices
,
237 int width
, int height
)
240 int n
= ConvertWXArrayToC(aChoices
, &choices
);
241 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
243 x
, y
, centre
, width
, height
);
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
254 const wxString
& message
,
255 const wxString
& caption
,
256 int n
, const wxString
*choices
,
261 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
262 styleDlg
&= ~wxBORDER_MASK
;
263 styleDlg
&= ~wxRESIZE_BORDER
;
264 styleDlg
&= ~wxCAPTION
;
267 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
270 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
273 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, wxLARGESMALL(10,0) );
276 m_listbox
= CreateList(n
,choices
,styleLbox
);
279 m_listbox
->SetSelection(0);
281 topsizer
->Add( m_listbox
, 1, wxEXPAND
|wxLEFT
|wxRIGHT
, wxLARGESMALL(15,0) );
283 // smart phones does not support or do not waste space for wxButtons
284 #ifdef __SMARTPHONE__
286 SetRightMenu(wxID_CANCEL
, _("Cancel"));
288 #else // __SMARTPHONE__/!__SMARTPHONE__
292 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
296 topsizer
->Add( CreateButtonSizer( styleDlg
& (wxOK
|wxCANCEL
) ), 0, wxEXPAND
| wxALL
, 10 );
298 #endif // !__SMARTPHONE__
300 SetSizer( topsizer
);
302 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
303 topsizer
->SetSizeHints( this );
304 topsizer
->Fit( this );
306 if ( styleDlg
& wxCENTRE
)
310 m_listbox
->SetFocus();
315 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
316 const wxString
& message
,
317 const wxString
& caption
,
318 const wxArrayString
& choices
,
323 wxCArrayString
chs(choices
);
324 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
325 styleDlg
, pos
, styleLbox
);
328 wxListBoxBase
*wxAnyChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
330 return new wxListBox( this, wxID_LISTBOX
,
331 wxDefaultPosition
, wxDefaultSize
,
336 // ----------------------------------------------------------------------------
337 // wxSingleChoiceDialog
338 // ----------------------------------------------------------------------------
340 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
341 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
342 #ifndef __SMARTPHONE__
343 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
346 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown
)
350 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
352 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
353 const wxString
& message
,
354 const wxString
& caption
,
356 const wxString
*choices
,
359 const wxPoint
& WXUNUSED(pos
))
361 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
364 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
365 const wxString
& message
,
366 const wxString
& caption
,
367 const wxArrayString
& choices
,
370 const wxPoint
& WXUNUSED(pos
))
372 Create(parent
, message
, caption
, choices
, clientData
, style
);
375 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
376 const wxString
& message
,
377 const wxString
& caption
,
379 const wxString
*choices
,
384 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
389 m_selection
= n
> 0 ? 0 : -1;
393 for (int i
= 0; i
< n
; i
++)
394 m_listbox
->SetClientData(i
, clientData
[i
]);
400 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
401 const wxString
& message
,
402 const wxString
& caption
,
403 const wxArrayString
& choices
,
408 wxCArrayString
chs(choices
);
409 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
410 clientData
, style
, pos
);
414 void wxSingleChoiceDialog::SetSelection(int sel
)
416 m_listbox
->SetSelection(sel
);
420 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
425 #ifndef __SMARTPHONE__
426 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
433 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent
& WXUNUSED(event
))
439 void wxSingleChoiceDialog::DoChoice()
441 m_selection
= m_listbox
->GetSelection();
442 m_stringSelection
= m_listbox
->GetStringSelection();
444 if ( m_listbox
->HasClientUntypedData() )
445 SetClientData(m_listbox
->GetClientData(m_selection
));
450 // ----------------------------------------------------------------------------
451 // wxMultiChoiceDialog
452 // ----------------------------------------------------------------------------
454 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
456 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
457 const wxString
& message
,
458 const wxString
& caption
,
460 const wxString
*choices
,
464 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
467 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
473 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
474 const wxString
& message
,
475 const wxString
& caption
,
476 const wxArrayString
& choices
,
480 wxCArrayString
chs(choices
);
481 return Create( parent
, message
, caption
, chs
.GetCount(),
482 chs
.GetStrings(), style
, pos
);
485 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
487 // first clear all currently selected items
489 count
= m_listbox
->GetCount();
490 for ( n
= 0; n
< count
; ++n
)
492 m_listbox
->Deselect(n
);
495 // now select the ones which should be selected
496 count
= selections
.GetCount();
497 for ( n
= 0; n
< count
; n
++ )
499 m_listbox
->Select(selections
[n
]);
503 bool wxMultiChoiceDialog::TransferDataFromWindow()
505 m_selections
.Empty();
506 size_t count
= m_listbox
->GetCount();
507 for ( size_t n
= 0; n
< count
; n
++ )
509 if ( m_listbox
->IsSelected(n
) )
516 #if wxUSE_CHECKLISTBOX
518 wxListBoxBase
*wxMultiChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
520 return new wxCheckListBox( this, wxID_LISTBOX
,
521 wxDefaultPosition
, wxDefaultSize
,
526 #endif // wxUSE_CHECKLISTBOX
528 #endif // wxUSE_CHOICEDLG