]>
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 // call this even if selections array is empty and this then (correctly)
222 // deselects the first item which is selected by default
223 dialog
.SetSelections(selections
);
225 if ( dialog
.ShowModal() == wxID_OK
)
226 selections
= dialog
.GetSelections();
230 return selections
.GetCount();
233 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
234 const wxString
& message
,
235 const wxString
& caption
,
236 const wxArrayString
& aChoices
,
240 int width
, int height
)
243 int n
= ConvertWXArrayToC(aChoices
, &choices
);
244 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
246 x
, y
, centre
, width
, height
);
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
257 const wxString
& message
,
258 const wxString
& caption
,
259 int n
, const wxString
*choices
,
264 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
265 styleDlg
&= ~wxBORDER_MASK
;
266 styleDlg
&= ~wxRESIZE_BORDER
;
267 styleDlg
&= ~wxCAPTION
;
270 if ( !wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, wxDefaultSize
, styleDlg
) )
273 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
276 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, wxLARGESMALL(10,0) );
279 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
280 wxDefaultPosition
, wxDefaultSize
,
284 m_listbox
->SetSelection(0);
286 topsizer
->Add( m_listbox
, 1, wxEXPAND
|wxLEFT
|wxRIGHT
, wxLARGESMALL(15,0) );
288 // smart phones does not support or do not waste space for wxButtons
289 #ifdef __SMARTPHONE__
291 SetRightMenu(wxID_CANCEL
, _("Cancel"));
293 #else // __SMARTPHONE__/!__SMARTPHONE__
297 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
301 topsizer
->Add( CreateButtonSizer( styleDlg
& (wxOK
|wxCANCEL
) ), 0, wxEXPAND
| wxALL
, 10 );
303 #endif // !__SMARTPHONE__
305 SetSizer( topsizer
);
307 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
308 topsizer
->SetSizeHints( this );
309 topsizer
->Fit( this );
311 if ( styleDlg
& wxCENTRE
)
315 m_listbox
->SetFocus();
320 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
321 const wxString
& message
,
322 const wxString
& caption
,
323 const wxArrayString
& choices
,
328 wxCArrayString
chs(choices
);
329 return Create(parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
330 styleDlg
, pos
, styleLbox
);
333 // ----------------------------------------------------------------------------
334 // wxSingleChoiceDialog
335 // ----------------------------------------------------------------------------
337 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
338 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
339 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
342 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
344 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
345 const wxString
& message
,
346 const wxString
& caption
,
348 const wxString
*choices
,
351 const wxPoint
& WXUNUSED(pos
))
353 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
356 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
357 const wxString
& message
,
358 const wxString
& caption
,
359 const wxArrayString
& choices
,
362 const wxPoint
& WXUNUSED(pos
))
364 Create(parent
, message
, caption
, choices
, clientData
, style
);
367 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
368 const wxString
& message
,
369 const wxString
& caption
,
371 const wxString
*choices
,
376 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
381 m_selection
= n
> 0 ? 0 : -1;
385 for (int i
= 0; i
< n
; i
++)
386 m_listbox
->SetClientData(i
, clientData
[i
]);
392 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
393 const wxString
& message
,
394 const wxString
& caption
,
395 const wxArrayString
& choices
,
400 wxCArrayString
chs(choices
);
401 return Create( parent
, message
, caption
, chs
.GetCount(), chs
.GetStrings(),
402 clientData
, style
, pos
);
406 void wxSingleChoiceDialog::SetSelection(int sel
)
408 m_listbox
->SetSelection(sel
);
412 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
414 m_selection
= m_listbox
->GetSelection();
415 m_stringSelection
= m_listbox
->GetStringSelection();
416 if ( m_listbox
->HasClientUntypedData() )
417 SetClientData(m_listbox
->GetClientData(m_selection
));
421 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
423 m_selection
= m_listbox
->GetSelection();
424 m_stringSelection
= m_listbox
->GetStringSelection();
426 if ( m_listbox
->HasClientUntypedData() )
427 SetClientData(m_listbox
->GetClientData(m_selection
));
432 // ----------------------------------------------------------------------------
433 // wxMultiChoiceDialog
434 // ----------------------------------------------------------------------------
436 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
438 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
439 const wxString
& message
,
440 const wxString
& caption
,
442 const wxString
*choices
,
446 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
449 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
455 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
456 const wxString
& message
,
457 const wxString
& caption
,
458 const wxArrayString
& choices
,
462 wxCArrayString
chs(choices
);
463 return Create( parent
, message
, caption
, chs
.GetCount(),
464 chs
.GetStrings(), style
, pos
);
467 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
469 // first clear all currently selected items
471 count
= m_listbox
->GetCount();
472 for ( n
= 0; n
< count
; ++n
)
474 m_listbox
->Deselect(n
);
477 // now select the ones which should be selected
478 count
= selections
.GetCount();
479 for ( n
= 0; n
< count
; n
++ )
481 m_listbox
->Select(selections
[n
]);
485 bool wxMultiChoiceDialog::TransferDataFromWindow()
487 m_selections
.Empty();
488 size_t count
= m_listbox
->GetCount();
489 for ( size_t n
= 0; n
< count
; n
++ )
491 if ( m_listbox
->IsSelected(n
) )
498 #endif // wxUSE_CHOICEDLG