]>
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 // ============================================================================
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 // convert wxArrayString into a wxString[] which must be delete[]d by caller
62 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
74 int n
= aChoices
.GetCount();
75 *choices
= new wxString
[n
];
77 for ( int i
= 0; i
< n
; i
++ )
79 (*choices
)[i
] = aChoices
[i
];
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 wxString
wxGetSingleChoice( const wxString
& message
,
90 const wxString
& caption
,
91 int n
, const wxString
*choices
,
93 int WXUNUSED(x
), int WXUNUSED(y
),
94 bool WXUNUSED(centre
),
95 int WXUNUSED(width
), int WXUNUSED(height
) )
97 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
99 if ( dialog
.ShowModal() == wxID_OK
)
100 choice
= dialog
.GetStringSelection();
105 wxString
wxGetSingleChoice( const wxString
& message
,
106 const wxString
& caption
,
107 const wxArrayString
& aChoices
,
111 int width
, int height
)
114 int n
= ConvertWXArrayToC(aChoices
, &choices
);
115 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
116 x
, y
, centre
, width
, height
);
122 #if WXWIN_COMPATIBILITY_2
123 // Overloaded for backward compatibility
124 wxString
wxGetSingleChoice( const wxString
& message
,
125 const wxString
& caption
,
126 int n
, wxChar
*choices
[],
128 int x
, int y
, bool centre
,
129 int width
, int height
)
131 wxString
*strings
= new wxString
[n
];
133 for ( i
= 0; i
< n
; i
++)
135 strings
[i
] = choices
[i
];
137 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
138 x
, y
, centre
, width
, height
));
142 #endif // WXWIN_COMPATIBILITY_2
144 int wxGetSingleChoiceIndex( const wxString
& message
,
145 const wxString
& caption
,
146 int n
, const wxString
*choices
,
148 int WXUNUSED(x
), int WXUNUSED(y
),
149 bool WXUNUSED(centre
),
150 int WXUNUSED(width
), int WXUNUSED(height
) )
152 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
154 if ( dialog
.ShowModal() == wxID_OK
)
155 choice
= dialog
.GetSelection();
162 int wxGetSingleChoiceIndex( const wxString
& message
,
163 const wxString
& caption
,
164 const wxArrayString
& aChoices
,
168 int width
, int height
)
171 int n
= ConvertWXArrayToC(aChoices
, &choices
);
172 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
173 x
, y
, centre
, width
, height
);
179 #if WXWIN_COMPATIBILITY_2
180 // Overloaded for backward compatibility
181 int wxGetSingleChoiceIndex( const wxString
& message
,
182 const wxString
& caption
,
183 int n
, wxChar
*choices
[],
185 int x
, int y
, bool centre
,
186 int width
, int height
)
188 wxString
*strings
= new wxString
[n
];
189 for ( int i
= 0; i
< n
; i
++)
190 strings
[i
] = choices
[i
];
191 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
192 x
, y
, centre
, width
, height
);
196 #endif // WXWIN_COMPATIBILITY_2
198 void *wxGetSingleChoiceData( const wxString
& message
,
199 const wxString
& caption
,
200 int n
, const wxString
*choices
,
203 int WXUNUSED(x
), int WXUNUSED(y
),
204 bool WXUNUSED(centre
),
205 int WXUNUSED(width
), int WXUNUSED(height
) )
207 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
208 (char **)client_data
);
210 if ( dialog
.ShowModal() == wxID_OK
)
211 data
= dialog
.GetSelectionClientData();
218 void *wxGetSingleChoiceData( const wxString
& message
,
219 const wxString
& caption
,
220 const wxArrayString
& aChoices
,
225 int width
, int height
)
228 int n
= ConvertWXArrayToC(aChoices
, &choices
);
229 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
231 x
, y
, centre
, width
, height
);
237 #if WXWIN_COMPATIBILITY_2
238 // Overloaded for backward compatibility
239 void *wxGetSingleChoiceData( const wxString
& message
,
240 const wxString
& caption
,
241 int n
, wxChar
*choices
[],
244 int x
, int y
, bool centre
, int width
, int height
)
246 wxString
*strings
= new wxString
[n
];
248 for ( i
= 0; i
< n
; i
++)
250 strings
[i
] = choices
[i
];
252 void *data
= wxGetSingleChoiceData(message
, caption
,
253 n
, (const wxString
*)strings
,
255 x
, y
, centre
, width
, height
);
259 #endif // WXWIN_COMPATIBILITY_2
261 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
262 const wxString
& message
,
263 const wxString
& caption
,
264 int n
, const wxString
*choices
,
266 int WXUNUSED(x
), int WXUNUSED(y
),
267 bool WXUNUSED(centre
),
268 int WXUNUSED(width
), int WXUNUSED(height
))
270 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
272 if ( !selections
.IsEmpty() )
273 dialog
.SetSelections(selections
);
275 if ( dialog
.ShowModal() == wxID_OK
)
276 selections
= dialog
.GetSelections();
280 return selections
.GetCount();
283 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
284 const wxString
& message
,
285 const wxString
& caption
,
286 const wxArrayString
& aChoices
,
290 int width
, int height
)
293 int n
= ConvertWXArrayToC(aChoices
, &choices
);
294 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
296 x
, y
, centre
, width
, height
);
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
307 const wxString
& message
,
308 const wxString
& caption
,
309 int n
, const wxString
*choices
,
314 if ( !wxDialog::Create(parent
, -1, caption
, pos
, wxDefaultSize
, styleDlg
) )
317 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
320 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
323 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
324 wxDefaultPosition
, wxDefaultSize
,
328 m_listbox
->SetSelection(0);
330 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
334 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
338 topsizer
->Add( CreateButtonSizer( styleDlg
& (wxOK
|wxCANCEL
) ), 0, wxCENTRE
| wxALL
, 10 );
340 SetAutoLayout( TRUE
);
341 SetSizer( topsizer
);
343 topsizer
->SetSizeHints( this );
344 topsizer
->Fit( this );
348 m_listbox
->SetFocus();
353 // ----------------------------------------------------------------------------
354 // wxSingleChoiceDialog
355 // ----------------------------------------------------------------------------
357 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
358 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
359 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
362 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
364 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
365 const wxString
& message
,
366 const wxString
& caption
,
368 const wxString
*choices
,
371 const wxPoint
& WXUNUSED(pos
))
373 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
376 #if WXWIN_COMPATIBILITY_2
378 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
379 const wxString
& message
,
380 const wxString
& caption
,
381 const wxStringList
& choices
,
384 const wxPoint
& WXUNUSED(pos
))
386 Create(parent
, message
, caption
, choices
, clientData
, style
);
389 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
390 const wxString
& message
,
391 const wxString
& caption
,
392 const wxStringList
& choices
,
397 wxString
*strings
= new wxString
[choices
.Number()];
399 for ( i
= 0; i
< choices
.Number(); i
++)
401 strings
[i
] = (char *)choices
.Nth(i
)->Data();
403 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
408 #endif // WXWIN_COMPATIBILITY_2
410 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
411 const wxString
& message
,
412 const wxString
& caption
,
414 const wxString
*choices
,
419 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
424 m_selection
= n
> 0 ? 0 : -1;
428 for (int i
= 0; i
< n
; i
++)
429 m_listbox
->SetClientData(i
, clientData
[i
]);
436 void wxSingleChoiceDialog::SetSelection(int sel
)
438 m_listbox
->SetSelection(sel
);
442 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
444 m_selection
= m_listbox
->GetSelection();
445 m_stringSelection
= m_listbox
->GetStringSelection();
446 if ( m_listbox
->HasClientUntypedData() )
447 SetClientData(m_listbox
->GetClientData(m_selection
));
451 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
453 m_selection
= m_listbox
->GetSelection();
454 m_stringSelection
= m_listbox
->GetStringSelection();
456 if ( m_listbox
->HasClientUntypedData() )
457 SetClientData(m_listbox
->GetClientData(m_selection
));
462 // ----------------------------------------------------------------------------
463 // wxMultiChoiceDialog
464 // ----------------------------------------------------------------------------
466 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
468 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
469 const wxString
& message
,
470 const wxString
& caption
,
472 const wxString
*choices
,
476 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
479 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
485 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
487 size_t count
= selections
.GetCount();
488 for ( size_t n
= 0; n
< count
; n
++ )
490 m_listbox
->Select(selections
[n
]);
494 bool wxMultiChoiceDialog::TransferDataFromWindow()
496 m_selections
.Empty();
497 size_t count
= m_listbox
->GetCount();
498 for ( size_t n
= 0; n
< count
; n
++ )
500 if ( m_listbox
->IsSelected(n
) )
507 #endif // wxUSE_CHOICEDLG