]>
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) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
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"
45 #include "wx/statline.h"
48 #include "wx/generic/choicdgg.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 #define wxID_LISTBOX 3000
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // convert wxArrayString into a wxString[] which must be delete[]d by caller
61 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
73 int n
= aChoices
.GetCount();
74 *choices
= new wxString
[n
];
76 for ( int i
= 0; i
< n
; i
++ )
78 (*choices
)[i
] = aChoices
[i
];
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 wxString
wxGetSingleChoice( const wxString
& message
,
89 const wxString
& caption
,
90 int n
, const wxString
*choices
,
92 int WXUNUSED(x
), int WXUNUSED(y
),
93 bool WXUNUSED(centre
),
94 int WXUNUSED(width
), int WXUNUSED(height
) )
96 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
98 if ( dialog
.ShowModal() == wxID_OK
)
99 choice
= dialog
.GetStringSelection();
104 wxString
wxGetSingleChoice( const wxString
& message
,
105 const wxString
& caption
,
106 const wxArrayString
& aChoices
,
110 int width
, int height
)
113 int n
= ConvertWXArrayToC(aChoices
, &choices
);
114 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
115 x
, y
, centre
, width
, height
);
121 #if WXWIN_COMPATIBILITY_2
122 // Overloaded for backward compatibility
123 wxString
wxGetSingleChoice( const wxString
& message
,
124 const wxString
& caption
,
125 int n
, wxChar
*choices
[],
127 int x
, int y
, bool centre
,
128 int width
, int height
)
130 wxString
*strings
= new wxString
[n
];
132 for ( i
= 0; i
< n
; i
++)
134 strings
[i
] = choices
[i
];
136 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
137 x
, y
, centre
, width
, height
));
141 #endif // WXWIN_COMPATIBILITY_2
143 int wxGetSingleChoiceIndex( const wxString
& message
,
144 const wxString
& caption
,
145 int n
, const wxString
*choices
,
147 int WXUNUSED(x
), int WXUNUSED(y
),
148 bool WXUNUSED(centre
),
149 int WXUNUSED(width
), int WXUNUSED(height
) )
151 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
153 if ( dialog
.ShowModal() == wxID_OK
)
154 choice
= dialog
.GetSelection();
161 int wxGetSingleChoiceIndex( const wxString
& message
,
162 const wxString
& caption
,
163 const wxArrayString
& aChoices
,
167 int width
, int height
)
170 int n
= ConvertWXArrayToC(aChoices
, &choices
);
171 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
172 x
, y
, centre
, width
, height
);
178 #if WXWIN_COMPATIBILITY_2
179 // Overloaded for backward compatibility
180 int wxGetSingleChoiceIndex( const wxString
& message
,
181 const wxString
& caption
,
182 int n
, wxChar
*choices
[],
184 int x
, int y
, bool centre
,
185 int width
, int height
)
187 wxString
*strings
= new wxString
[n
];
188 for ( int i
= 0; i
< n
; i
++)
189 strings
[i
] = choices
[i
];
190 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
191 x
, y
, centre
, width
, height
);
195 #endif // WXWIN_COMPATIBILITY_2
197 void *wxGetSingleChoiceData( const wxString
& message
,
198 const wxString
& caption
,
199 int n
, const wxString
*choices
,
202 int WXUNUSED(x
), int WXUNUSED(y
),
203 bool WXUNUSED(centre
),
204 int WXUNUSED(width
), int WXUNUSED(height
) )
206 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
207 (char **)client_data
);
209 if ( dialog
.ShowModal() == wxID_OK
)
210 data
= dialog
.GetSelectionClientData();
217 void *wxGetSingleChoiceData( const wxString
& message
,
218 const wxString
& caption
,
219 const wxArrayString
& aChoices
,
224 int width
, int height
)
227 int n
= ConvertWXArrayToC(aChoices
, &choices
);
228 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
230 x
, y
, centre
, width
, height
);
236 #if WXWIN_COMPATIBILITY_2
237 // Overloaded for backward compatibility
238 void *wxGetSingleChoiceData( const wxString
& message
,
239 const wxString
& caption
,
240 int n
, wxChar
*choices
[],
243 int x
, int y
, bool centre
, int width
, int height
)
245 wxString
*strings
= new wxString
[n
];
247 for ( i
= 0; i
< n
; i
++)
249 strings
[i
] = choices
[i
];
251 void *data
= wxGetSingleChoiceData(message
, caption
,
252 n
, (const wxString
*)strings
,
254 x
, y
, centre
, width
, height
);
258 #endif // WXWIN_COMPATIBILITY_2
260 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
261 const wxString
& message
,
262 const wxString
& caption
,
263 int n
, const wxString
*choices
,
265 int WXUNUSED(x
), int WXUNUSED(y
),
266 bool WXUNUSED(centre
),
267 int WXUNUSED(width
), int WXUNUSED(height
))
269 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
271 if ( !selections
.IsEmpty() )
272 dialog
.SetSelections(selections
);
274 if ( dialog
.ShowModal() == wxID_OK
)
275 selections
= dialog
.GetSelections();
279 return selections
.GetCount();
282 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
283 const wxString
& message
,
284 const wxString
& caption
,
285 const wxArrayString
& aChoices
,
289 int width
, int height
)
292 int n
= ConvertWXArrayToC(aChoices
, &choices
);
293 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
295 x
, y
, centre
, width
, height
);
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
306 const wxString
& message
,
307 const wxString
& caption
,
308 int n
, const wxString
*choices
,
313 if ( !wxDialog::Create(parent
, -1, caption
, pos
, wxDefaultSize
, styleDlg
) )
316 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
319 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
322 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
323 wxDefaultPosition
, wxDefaultSize
,
327 m_listbox
->SetSelection(0);
329 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
333 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
337 topsizer
->Add( CreateButtonSizer( styleDlg
& (wxOK
|wxCANCEL
) ), 0, wxCENTRE
| wxALL
, 10 );
339 SetAutoLayout( TRUE
);
340 SetSizer( topsizer
);
342 topsizer
->SetSizeHints( this );
343 topsizer
->Fit( this );
347 m_listbox
->SetFocus();
352 // ----------------------------------------------------------------------------
353 // wxSingleChoiceDialog
354 // ----------------------------------------------------------------------------
356 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
357 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
358 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
361 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
363 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
364 const wxString
& message
,
365 const wxString
& caption
,
367 const wxString
*choices
,
370 const wxPoint
& WXUNUSED(pos
))
372 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
375 #if WXWIN_COMPATIBILITY_2
377 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
378 const wxString
& message
,
379 const wxString
& caption
,
380 const wxStringList
& choices
,
383 const wxPoint
& WXUNUSED(pos
))
385 Create(parent
, message
, caption
, choices
, clientData
, style
);
388 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
389 const wxString
& message
,
390 const wxString
& caption
,
391 const wxStringList
& choices
,
396 wxString
*strings
= new wxString
[choices
.Number()];
398 for ( i
= 0; i
< choices
.Number(); i
++)
400 strings
[i
] = (char *)choices
.Nth(i
)->Data();
402 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
407 #endif // WXWIN_COMPATIBILITY_2
409 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
410 const wxString
& message
,
411 const wxString
& caption
,
413 const wxString
*choices
,
418 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
423 m_selection
= n
> 0 ? 0 : -1;
427 for (int i
= 0; i
< n
; i
++)
428 m_listbox
->SetClientData(i
, clientData
[i
]);
435 void wxSingleChoiceDialog::SetSelection(int sel
)
437 m_listbox
->SetSelection(sel
);
441 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
443 m_selection
= m_listbox
->GetSelection();
444 m_stringSelection
= m_listbox
->GetStringSelection();
445 if ( m_listbox
->HasClientUntypedData() )
446 SetClientData(m_listbox
->GetClientData(m_selection
));
450 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
452 m_selection
= m_listbox
->GetSelection();
453 m_stringSelection
= m_listbox
->GetStringSelection();
455 if ( m_listbox
->HasClientUntypedData() )
456 SetClientData(m_listbox
->GetClientData(m_selection
));
461 // ----------------------------------------------------------------------------
462 // wxMultiChoiceDialog
463 // ----------------------------------------------------------------------------
465 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
467 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
468 const wxString
& message
,
469 const wxString
& caption
,
471 const wxString
*choices
,
475 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
478 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
484 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
486 size_t count
= selections
.GetCount();
487 for ( size_t n
= 0; n
< count
; n
++ )
489 m_listbox
->Select(selections
[n
]);
493 bool wxMultiChoiceDialog::TransferDataFromWindow()
495 m_selections
.Empty();
496 size_t count
= m_listbox
->GetCount();
497 for ( size_t n
= 0; n
< count
; n
++ )
499 if ( m_listbox
->IsSelected(n
) )
506 #endif // wxUSE_CHOICEDLG