]>
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 );
305 m_listbox
->SetFocus();
310 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
311 const wxString
& message
,
312 const wxString
& caption
,
313 const wxArrayString
& choices
,
318 wxCArrayString
chs(choices
);
319 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
320 styleDlg
, pos
, styleLbox
);
323 // ----------------------------------------------------------------------------
324 // wxSingleChoiceDialog
325 // ----------------------------------------------------------------------------
327 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
328 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
329 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
332 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
334 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
335 const wxString
& message
,
336 const wxString
& caption
,
338 const wxString
*choices
,
341 const wxPoint
& WXUNUSED(pos
))
343 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
346 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
347 const wxString
& message
,
348 const wxString
& caption
,
349 const wxArrayString
& choices
,
352 const wxPoint
& WXUNUSED(pos
))
354 Create(parent
, message
, caption
, choices
, clientData
, style
);
357 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
358 const wxString
& message
,
359 const wxString
& caption
,
361 const wxString
*choices
,
366 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
371 m_selection
= n
> 0 ? 0 : -1;
375 for (int i
= 0; i
< n
; i
++)
376 m_listbox
->SetClientData(i
, clientData
[i
]);
382 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
383 const wxString
& message
,
384 const wxString
& caption
,
385 const wxArrayString
& choices
,
390 wxCArrayString
chs(choices
);
391 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
392 clientData
, style
, pos
);
396 void wxSingleChoiceDialog::SetSelection(int sel
)
398 m_listbox
->SetSelection(sel
);
402 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
404 m_selection
= m_listbox
->GetSelection();
405 m_stringSelection
= m_listbox
->GetStringSelection();
406 if ( m_listbox
->HasClientUntypedData() )
407 SetClientData(m_listbox
->GetClientData(m_selection
));
411 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
413 m_selection
= m_listbox
->GetSelection();
414 m_stringSelection
= m_listbox
->GetStringSelection();
416 if ( m_listbox
->HasClientUntypedData() )
417 SetClientData(m_listbox
->GetClientData(m_selection
));
422 // ----------------------------------------------------------------------------
423 // wxMultiChoiceDialog
424 // ----------------------------------------------------------------------------
426 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
428 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
429 const wxString
& message
,
430 const wxString
& caption
,
432 const wxString
*choices
,
436 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
439 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
445 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
446 const wxString
& message
,
447 const wxString
& caption
,
448 const wxArrayString
& choices
,
452 wxCArrayString
chs(choices
);
453 return Create( parent
, message
, caption
, chs
.GetCount(),
454 chs
.GetStrings(), style
, pos
);
457 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
459 size_t count
= selections
.GetCount();
460 for ( size_t n
= 0; n
< count
; n
++ )
462 m_listbox
->Select(selections
[n
]);
466 bool wxMultiChoiceDialog::TransferDataFromWindow()
468 m_selections
.Empty();
469 size_t count
= m_listbox
->GetCount();
470 for ( size_t n
= 0; n
< count
; n
++ )
472 if ( m_listbox
->IsSelected(n
) )
479 #endif // wxUSE_CHOICEDLG