]>
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 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 // smart phones does not support or do not waste space for wxButtons
282 #ifdef __SMARTPHONE__
284 SetRightMenu(wxID_CANCEL
, _("Cancel"));
286 #else // __SMARTPHONE__/!__SMARTPHONE__
290 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
294 topsizer
->Add( CreateButtonSizer( styleDlg
& (wxOK
|wxCANCEL
) ), 0, wxCENTRE
| wxALL
, 10 );
296 #endif // !__SMARTPHONE__
298 SetAutoLayout( true );
299 SetSizer( topsizer
);
301 topsizer
->SetSizeHints( this );
302 topsizer
->Fit( this );
304 if ( styleDlg
& wxCENTRE
)
307 m_listbox
->SetFocus();
312 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
313 const wxString
& message
,
314 const wxString
& caption
,
315 const wxArrayString
& choices
,
320 wxCArrayString
chs(choices
);
321 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
322 styleDlg
, pos
, styleLbox
);
325 // ----------------------------------------------------------------------------
326 // wxSingleChoiceDialog
327 // ----------------------------------------------------------------------------
329 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
330 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
331 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
334 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
336 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
337 const wxString
& message
,
338 const wxString
& caption
,
340 const wxString
*choices
,
343 const wxPoint
& WXUNUSED(pos
))
345 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
348 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
349 const wxString
& message
,
350 const wxString
& caption
,
351 const wxArrayString
& choices
,
354 const wxPoint
& WXUNUSED(pos
))
356 Create(parent
, message
, caption
, choices
, clientData
, style
);
359 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
360 const wxString
& message
,
361 const wxString
& caption
,
363 const wxString
*choices
,
368 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
373 m_selection
= n
> 0 ? 0 : -1;
377 for (int i
= 0; i
< n
; i
++)
378 m_listbox
->SetClientData(i
, clientData
[i
]);
384 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
385 const wxString
& message
,
386 const wxString
& caption
,
387 const wxArrayString
& choices
,
392 wxCArrayString
chs(choices
);
393 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
394 clientData
, style
, pos
);
398 void wxSingleChoiceDialog::SetSelection(int sel
)
400 m_listbox
->SetSelection(sel
);
404 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
406 m_selection
= m_listbox
->GetSelection();
407 m_stringSelection
= m_listbox
->GetStringSelection();
408 if ( m_listbox
->HasClientUntypedData() )
409 SetClientData(m_listbox
->GetClientData(m_selection
));
413 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
415 m_selection
= m_listbox
->GetSelection();
416 m_stringSelection
= m_listbox
->GetStringSelection();
418 if ( m_listbox
->HasClientUntypedData() )
419 SetClientData(m_listbox
->GetClientData(m_selection
));
424 // ----------------------------------------------------------------------------
425 // wxMultiChoiceDialog
426 // ----------------------------------------------------------------------------
428 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
430 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
431 const wxString
& message
,
432 const wxString
& caption
,
434 const wxString
*choices
,
438 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
441 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
447 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
448 const wxString
& message
,
449 const wxString
& caption
,
450 const wxArrayString
& choices
,
454 wxCArrayString
chs(choices
);
455 return Create( parent
, message
, caption
, chs
.GetCount(),
456 chs
.GetStrings(), style
, pos
);
459 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
461 // first clear all currently selected items
463 count
= m_listbox
->GetCount();
464 for ( n
= 0; n
< count
; ++n
)
466 m_listbox
->Deselect(n
);
469 // now select the ones which should be selected
470 count
= selections
.GetCount();
471 for ( n
= 0; n
< count
; n
++ )
473 m_listbox
->Select(selections
[n
]);
477 bool wxMultiChoiceDialog::TransferDataFromWindow()
479 m_selections
.Empty();
480 size_t count
= m_listbox
->GetCount();
481 for ( size_t n
= 0; n
< count
; n
++ )
483 if ( m_listbox
->IsSelected(n
) )
490 #endif // wxUSE_CHOICEDLG