]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
62beddd961620fb11aef87f269991e0f9f378725
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/stattext.h"
38 #include "wx/arrstr.h"
42 #include "wx/statline.h"
45 #include "wx/generic/choicdgg.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 #define wxID_LISTBOX 3000
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 /* Macro for avoiding #ifdefs when value have to be different depending on size of
58 device we display on - take it from something like wxDesktopPolicy in the future
61 #if defined(__SMARTPHONE__)
62 #define wxLARGESMALL(large,small) small
64 #define wxLARGESMALL(large,small) large
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // convert wxArrayString into a wxString[] which must be delete[]d by caller
72 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
84 int n
= aChoices
.GetCount();
85 *choices
= new wxString
[n
];
87 for ( int i
= 0; i
< n
; i
++ )
89 (*choices
)[i
] = aChoices
[i
];
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 wxString
wxGetSingleChoice( const wxString
& message
,
100 const wxString
& caption
,
101 int n
, const wxString
*choices
,
103 int WXUNUSED(x
), int WXUNUSED(y
),
104 bool WXUNUSED(centre
),
105 int WXUNUSED(width
), int WXUNUSED(height
) )
107 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
109 if ( dialog
.ShowModal() == wxID_OK
)
110 choice
= dialog
.GetStringSelection();
115 wxString
wxGetSingleChoice( const wxString
& message
,
116 const wxString
& caption
,
117 const wxArrayString
& aChoices
,
121 int width
, int height
)
124 int n
= ConvertWXArrayToC(aChoices
, &choices
);
125 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
126 x
, y
, centre
, width
, height
);
132 int wxGetSingleChoiceIndex( const wxString
& message
,
133 const wxString
& caption
,
134 int n
, const wxString
*choices
,
136 int WXUNUSED(x
), int WXUNUSED(y
),
137 bool WXUNUSED(centre
),
138 int WXUNUSED(width
), int WXUNUSED(height
) )
140 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
142 if ( dialog
.ShowModal() == wxID_OK
)
143 choice
= dialog
.GetSelection();
150 int wxGetSingleChoiceIndex( const wxString
& message
,
151 const wxString
& caption
,
152 const wxArrayString
& aChoices
,
156 int width
, int height
)
159 int n
= ConvertWXArrayToC(aChoices
, &choices
);
160 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
161 x
, y
, centre
, width
, height
);
167 void *wxGetSingleChoiceData( const wxString
& message
,
168 const wxString
& caption
,
169 int n
, const wxString
*choices
,
172 int WXUNUSED(x
), int WXUNUSED(y
),
173 bool WXUNUSED(centre
),
174 int WXUNUSED(width
), int WXUNUSED(height
) )
176 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
177 (char **)client_data
);
179 if ( dialog
.ShowModal() == wxID_OK
)
180 data
= dialog
.GetSelectionClientData();
187 void *wxGetSingleChoiceData( const wxString
& message
,
188 const wxString
& caption
,
189 const wxArrayString
& aChoices
,
194 int width
, int height
)
197 int n
= ConvertWXArrayToC(aChoices
, &choices
);
198 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
200 x
, y
, centre
, width
, height
);
206 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
207 const wxString
& message
,
208 const wxString
& caption
,
209 int n
, const wxString
*choices
,
211 int WXUNUSED(x
), int WXUNUSED(y
),
212 bool WXUNUSED(centre
),
213 int WXUNUSED(width
), int WXUNUSED(height
))
215 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
217 // call this even if selections array is empty and this then (correctly)
218 // deselects the first item which is selected by default
219 dialog
.SetSelections(selections
);
221 if ( dialog
.ShowModal() == wxID_OK
)
222 selections
= dialog
.GetSelections();
226 return selections
.GetCount();
229 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
230 const wxString
& message
,
231 const wxString
& caption
,
232 const wxArrayString
& aChoices
,
236 int width
, int height
)
239 int n
= ConvertWXArrayToC(aChoices
, &choices
);
240 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
242 x
, y
, centre
, width
, height
);
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
253 const wxString
& message
,
254 const wxString
& caption
,
255 int n
, const wxString
*choices
,
260 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
261 styleDlg
&= ~wxBORDER_MASK
;
262 styleDlg
&= ~wxRESIZE_BORDER
;
263 styleDlg
&= ~wxCAPTION
;
266 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
269 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
272 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, wxLARGESMALL(10,0) );
275 m_listbox
= CreateList(n
,choices
,styleLbox
);
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, wxEXPAND
| wxALL
, 10 );
297 #endif // !__SMARTPHONE__
299 SetSizer( topsizer
);
301 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
302 topsizer
->SetSizeHints( this );
303 topsizer
->Fit( this );
305 if ( styleDlg
& wxCENTRE
)
309 m_listbox
->SetFocus();
314 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
315 const wxString
& message
,
316 const wxString
& caption
,
317 const wxArrayString
& choices
,
322 wxCArrayString
chs(choices
);
323 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
324 styleDlg
, pos
, styleLbox
);
327 wxListBoxBase
*wxAnyChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
329 return new wxListBox( this, wxID_LISTBOX
,
330 wxDefaultPosition
, wxDefaultSize
,
335 // ----------------------------------------------------------------------------
336 // wxSingleChoiceDialog
337 // ----------------------------------------------------------------------------
339 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
340 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
341 #ifndef __SMARTPHONE__
342 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
345 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown
)
349 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
351 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
352 const wxString
& message
,
353 const wxString
& caption
,
355 const wxString
*choices
,
358 const wxPoint
& WXUNUSED(pos
))
360 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
363 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
364 const wxString
& message
,
365 const wxString
& caption
,
366 const wxArrayString
& choices
,
369 const wxPoint
& WXUNUSED(pos
))
371 Create(parent
, message
, caption
, choices
, clientData
, style
);
374 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
375 const wxString
& message
,
376 const wxString
& caption
,
378 const wxString
*choices
,
383 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
388 m_selection
= n
> 0 ? 0 : -1;
392 for (int i
= 0; i
< n
; i
++)
393 m_listbox
->SetClientData(i
, clientData
[i
]);
399 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
400 const wxString
& message
,
401 const wxString
& caption
,
402 const wxArrayString
& choices
,
407 wxCArrayString
chs(choices
);
408 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
409 clientData
, style
, pos
);
413 void wxSingleChoiceDialog::SetSelection(int sel
)
415 m_listbox
->SetSelection(sel
);
419 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
424 #ifndef __SMARTPHONE__
425 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
432 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent
& WXUNUSED(event
))
438 void wxSingleChoiceDialog::DoChoice()
440 m_selection
= m_listbox
->GetSelection();
441 m_stringSelection
= m_listbox
->GetStringSelection();
443 if ( m_listbox
->HasClientUntypedData() )
444 SetClientData(m_listbox
->GetClientData(m_selection
));
449 // ----------------------------------------------------------------------------
450 // wxMultiChoiceDialog
451 // ----------------------------------------------------------------------------
453 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
455 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
456 const wxString
& message
,
457 const wxString
& caption
,
459 const wxString
*choices
,
463 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
466 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
472 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
473 const wxString
& message
,
474 const wxString
& caption
,
475 const wxArrayString
& choices
,
479 wxCArrayString
chs(choices
);
480 return Create( parent
, message
, caption
, chs
.GetCount(),
481 chs
.GetStrings(), style
, pos
);
484 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
486 // first clear all currently selected items
488 count
= m_listbox
->GetCount();
489 for ( n
= 0; n
< count
; ++n
)
491 m_listbox
->Deselect(n
);
494 // now select the ones which should be selected
495 count
= selections
.GetCount();
496 for ( n
= 0; n
< count
; n
++ )
498 m_listbox
->Select(selections
[n
]);
502 bool wxMultiChoiceDialog::TransferDataFromWindow()
504 m_selections
.Empty();
505 size_t count
= m_listbox
->GetCount();
506 for ( size_t n
= 0; n
< count
; n
++ )
508 if ( m_listbox
->IsSelected(n
) )
515 #if wxUSE_CHECKLISTBOX
517 wxListBoxBase
*wxMultiChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
519 return new wxCheckListBox( this, wxID_LISTBOX
,
520 wxDefaultPosition
, wxDefaultSize
,
525 #endif // wxUSE_CHECKLISTBOX
527 #endif // wxUSE_CHOICEDLG