]>
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 /* Macro for avoiding #ifdefs when value have to be different depending on size of
56 device we display on - take it from something like wxDesktopPolicy in the future
59 #if defined(__SMARTPHONE__)
60 #define wxLARGESMALL(large,small) small
62 #define wxLARGESMALL(large,small) large
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // convert wxArrayString into a wxString[] which must be delete[]d by caller
70 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
72 // ============================================================================
74 // ============================================================================
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
82 int n
= aChoices
.GetCount();
83 *choices
= new wxString
[n
];
85 for ( int i
= 0; i
< n
; i
++ )
87 (*choices
)[i
] = aChoices
[i
];
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 wxString
wxGetSingleChoice( const wxString
& message
,
98 const wxString
& caption
,
99 int n
, const wxString
*choices
,
101 int WXUNUSED(x
), int WXUNUSED(y
),
102 bool WXUNUSED(centre
),
103 int WXUNUSED(width
), int WXUNUSED(height
) )
105 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
107 if ( dialog
.ShowModal() == wxID_OK
)
108 choice
= dialog
.GetStringSelection();
113 wxString
wxGetSingleChoice( const wxString
& message
,
114 const wxString
& caption
,
115 const wxArrayString
& aChoices
,
119 int width
, int height
)
122 int n
= ConvertWXArrayToC(aChoices
, &choices
);
123 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
124 x
, y
, centre
, width
, height
);
130 int wxGetSingleChoiceIndex( const wxString
& message
,
131 const wxString
& caption
,
132 int n
, const wxString
*choices
,
134 int WXUNUSED(x
), int WXUNUSED(y
),
135 bool WXUNUSED(centre
),
136 int WXUNUSED(width
), int WXUNUSED(height
) )
138 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
140 if ( dialog
.ShowModal() == wxID_OK
)
141 choice
= dialog
.GetSelection();
148 int wxGetSingleChoiceIndex( const wxString
& message
,
149 const wxString
& caption
,
150 const wxArrayString
& aChoices
,
154 int width
, int height
)
157 int n
= ConvertWXArrayToC(aChoices
, &choices
);
158 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
159 x
, y
, centre
, width
, height
);
165 void *wxGetSingleChoiceData( const wxString
& message
,
166 const wxString
& caption
,
167 int n
, const wxString
*choices
,
170 int WXUNUSED(x
), int WXUNUSED(y
),
171 bool WXUNUSED(centre
),
172 int WXUNUSED(width
), int WXUNUSED(height
) )
174 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
175 (char **)client_data
);
177 if ( dialog
.ShowModal() == wxID_OK
)
178 data
= dialog
.GetSelectionClientData();
185 void *wxGetSingleChoiceData( const wxString
& message
,
186 const wxString
& caption
,
187 const wxArrayString
& aChoices
,
192 int width
, int height
)
195 int n
= ConvertWXArrayToC(aChoices
, &choices
);
196 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
198 x
, y
, centre
, width
, height
);
204 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
205 const wxString
& message
,
206 const wxString
& caption
,
207 int n
, const wxString
*choices
,
209 int WXUNUSED(x
), int WXUNUSED(y
),
210 bool WXUNUSED(centre
),
211 int WXUNUSED(width
), int WXUNUSED(height
))
213 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
215 // call this even if selections array is empty and this then (correctly)
216 // deselects the first item which is selected by default
217 dialog
.SetSelections(selections
);
219 if ( dialog
.ShowModal() == wxID_OK
)
220 selections
= dialog
.GetSelections();
224 return selections
.GetCount();
227 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
228 const wxString
& message
,
229 const wxString
& caption
,
230 const wxArrayString
& aChoices
,
234 int width
, int height
)
237 int n
= ConvertWXArrayToC(aChoices
, &choices
);
238 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
240 x
, y
, centre
, width
, height
);
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
250 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
251 const wxString
& message
,
252 const wxString
& caption
,
253 int n
, const wxString
*choices
,
259 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
& (~wxCANCEL
) ) )
262 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
266 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
270 // align text and list at least on mac
271 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, wxLARGESMALL(15,0) );
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) );
284 wxSizer
*buttonSizer
= CreateButtonSizer( styleDlg
& ButtonSizerFlags
, true, wxLARGESMALL(10,0) );
285 if(buttonSizer
->GetChildren().GetCount() > 0 )
287 topsizer
->Add( buttonSizer
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
291 topsizer
->AddSpacer( wxLARGESMALL(15,0) );
295 SetSizer( topsizer
);
297 topsizer
->SetSizeHints( this );
298 topsizer
->Fit( this );
300 if ( styleDlg
& wxCENTRE
)
303 m_listbox
->SetFocus();
308 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
309 const wxString
& message
,
310 const wxString
& caption
,
311 const wxArrayString
& choices
,
316 wxCArrayString
chs(choices
);
317 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
318 styleDlg
, pos
, styleLbox
);
321 wxListBoxBase
*wxAnyChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
323 return new wxListBox( this, wxID_LISTBOX
,
324 wxDefaultPosition
, wxDefaultSize
,
329 // ----------------------------------------------------------------------------
330 // wxSingleChoiceDialog
331 // ----------------------------------------------------------------------------
333 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
334 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
335 #ifndef __SMARTPHONE__
336 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
339 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown
)
343 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
345 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
346 const wxString
& message
,
347 const wxString
& caption
,
349 const wxString
*choices
,
352 const wxPoint
& WXUNUSED(pos
))
354 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
357 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
358 const wxString
& message
,
359 const wxString
& caption
,
360 const wxArrayString
& choices
,
363 const wxPoint
& WXUNUSED(pos
))
365 Create(parent
, message
, caption
, choices
, clientData
, style
);
368 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
369 const wxString
& message
,
370 const wxString
& caption
,
372 const wxString
*choices
,
377 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
382 m_selection
= n
> 0 ? 0 : -1;
386 for (int i
= 0; i
< n
; i
++)
387 m_listbox
->SetClientData(i
, clientData
[i
]);
393 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
394 const wxString
& message
,
395 const wxString
& caption
,
396 const wxArrayString
& choices
,
401 wxCArrayString
chs(choices
);
402 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
403 clientData
, style
, pos
);
407 void wxSingleChoiceDialog::SetSelection(int sel
)
409 m_listbox
->SetSelection(sel
);
413 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
418 #ifndef __SMARTPHONE__
419 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
426 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent
& WXUNUSED(event
))
432 void wxSingleChoiceDialog::DoChoice()
434 m_selection
= m_listbox
->GetSelection();
435 m_stringSelection
= m_listbox
->GetStringSelection();
437 if ( m_listbox
->HasClientUntypedData() )
438 SetClientData(m_listbox
->GetClientData(m_selection
));
443 // ----------------------------------------------------------------------------
444 // wxMultiChoiceDialog
445 // ----------------------------------------------------------------------------
447 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
449 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
450 const wxString
& message
,
451 const wxString
& caption
,
453 const wxString
*choices
,
457 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
460 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
466 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
467 const wxString
& message
,
468 const wxString
& caption
,
469 const wxArrayString
& choices
,
473 wxCArrayString
chs(choices
);
474 return Create( parent
, message
, caption
, chs
.GetCount(),
475 chs
.GetStrings(), style
, pos
);
478 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
480 // first clear all currently selected items
482 count
= m_listbox
->GetCount();
483 for ( n
= 0; n
< count
; ++n
)
485 m_listbox
->Deselect(n
);
488 // now select the ones which should be selected
489 count
= selections
.GetCount();
490 for ( n
= 0; n
< count
; n
++ )
492 m_listbox
->Select(selections
[n
]);
496 bool wxMultiChoiceDialog::TransferDataFromWindow()
498 m_selections
.Empty();
499 size_t count
= m_listbox
->GetCount();
500 for ( size_t n
= 0; n
< count
; n
++ )
502 if ( m_listbox
->IsSelected(n
) )
509 #if wxUSE_CHECKLISTBOX
511 wxListBoxBase
*wxMultiChoiceDialog::CreateList(int n
, const wxString
*choices
, long styleLbox
)
513 return new wxCheckListBox( this, wxID_LISTBOX
,
514 wxDefaultPosition
, wxDefaultSize
,
519 #endif // wxUSE_CHECKLISTBOX
521 #endif // wxUSE_CHOICEDLG