]>
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 license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "choicdgg.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/dialog.h"
35 #include "wx/button.h"
36 #include "wx/listbox.h"
37 #include "wx/stattext.h"
43 #include "wx/statline.h"
46 #include "wx/generic/choicdgg.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #define wxID_LISTBOX 3000
54 #if defined(__WXMSW__) || defined(__WXMAC__)
55 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
59 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // convert wxArrayString into a wxString[] which must be delete[]d by caller
70 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
72 // ============================================================================
74 // ============================================================================
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
82 int n
= aChoices
.GetCount();
83 *choices
= new wxString
[n
];
84 for ( int i
= 0; i
< n
; i
++ )
86 *choices
[i
] = aChoices
[i
];
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 wxString
wxGetSingleChoice( const wxString
& message
,
97 const wxString
& caption
,
98 int n
, const wxString
*choices
,
100 int WXUNUSED(x
), int WXUNUSED(y
),
101 bool WXUNUSED(centre
),
102 int WXUNUSED(width
), int WXUNUSED(height
) )
104 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
106 if ( dialog
.ShowModal() == wxID_OK
)
107 choice
= dialog
.GetStringSelection();
112 wxString
wxGetSingleChoice( const wxString
& message
,
113 const wxString
& caption
,
114 const wxArrayString
& aChoices
,
118 int width
, int height
)
121 int n
= ConvertWXArrayToC(aChoices
, &choices
);
122 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
123 x
, y
, centre
, width
, height
);
129 #ifdef WXWIN_COMPATIBILITY_2
130 // Overloaded for backward compatibility
131 wxString
wxGetSingleChoice( const wxString
& message
,
132 const wxString
& caption
,
133 int n
, char *choices
[],
135 int x
, int y
, bool centre
,
136 int width
, int height
)
138 wxString
*strings
= new wxString
[n
];
140 for ( i
= 0; i
< n
; i
++)
142 strings
[i
] = choices
[i
];
144 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
145 x
, y
, centre
, width
, height
));
149 #endif // WXWIN_COMPATIBILITY_2
151 int wxGetSingleChoiceIndex( const wxString
& message
,
152 const wxString
& caption
,
153 int n
, const wxString
*choices
,
155 int WXUNUSED(x
), int WXUNUSED(y
),
156 bool WXUNUSED(centre
),
157 int WXUNUSED(width
), int WXUNUSED(height
) )
159 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
161 if ( dialog
.ShowModal() == wxID_OK
)
162 choice
= dialog
.GetSelection();
169 #ifdef WXWIN_COMPATIBILITY_2
170 // Overloaded for backward compatibility
171 int wxGetSingleChoiceIndex( const wxString
& message
,
172 const wxString
& caption
,
173 int n
, wxChar
*choices
[],
175 int x
, int y
, bool centre
,
176 int width
, int height
)
178 wxString
*strings
= new wxString
[n
];
179 for ( int i
= 0; i
< n
; i
++)
180 strings
[i
] = choices
[i
];
181 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
182 x
, y
, centre
, width
, height
);
186 #endif // WXWIN_COMPATIBILITY_2
188 void *wxGetSingleChoiceData( const wxString
& message
,
189 const wxString
& caption
,
190 int n
, const wxString
*choices
,
193 int WXUNUSED(x
), int WXUNUSED(y
),
194 bool WXUNUSED(centre
),
195 int WXUNUSED(width
), int WXUNUSED(height
) )
197 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
, (char **)client_data
);
199 if ( dialog
.ShowModal() == wxID_OK
)
200 data
= dialog
.GetSelectionClientData();
207 #ifdef WXWIN_COMPATIBILITY_2
208 // Overloaded for backward compatibility
209 void *wxGetSingleChoiceData( const wxString
& message
,
210 const wxString
& caption
,
211 int n
, wxChar
*choices
[],
214 int x
, int y
, bool centre
, int width
, int height
)
216 wxString
*strings
= new wxString
[n
];
218 for ( i
= 0; i
< n
; i
++)
220 strings
[i
] = choices
[i
];
222 void *data
= wxGetSingleChoiceData(message
, caption
,
223 n
, (const wxString
*)strings
,
225 x
, y
, centre
, width
, height
);
229 #endif // WXWIN_COMPATIBILITY_2
231 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
232 const wxString
& message
,
233 const wxString
& caption
,
234 int n
, const wxString
*choices
,
236 int WXUNUSED(x
), int WXUNUSED(y
),
237 bool WXUNUSED(centre
),
238 int WXUNUSED(width
), int WXUNUSED(height
))
240 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
241 if ( dialog
.ShowModal() == wxID_OK
)
242 selections
= dialog
.GetSelections();
246 return selections
.GetCount();
249 size_t wxGetMultipleChoices(wxArrayInt selections
,
250 const wxString
& message
,
251 const wxString
& caption
,
252 const wxArrayString
& aChoices
,
256 int width
, int height
)
259 int n
= ConvertWXArrayToC(aChoices
, &choices
);
260 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
262 x
, y
, centre
, width
, height
);
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
273 const wxString
& message
,
274 const wxString
& caption
,
275 int n
, const wxString
*choices
,
280 if ( !wxDialog::Create(parent
, -1, caption
, pos
, wxDefaultSize
,
281 wxCHOICEDLG_DIALOG_STYLE
) )
284 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
287 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
290 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
291 wxDefaultPosition
, wxDefaultSize
,
295 m_listbox
->SetSelection(0);
297 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
301 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
305 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
307 SetAutoLayout( TRUE
);
308 SetSizer( topsizer
);
310 topsizer
->SetSizeHints( this );
311 topsizer
->Fit( this );
315 m_listbox
->SetFocus();
320 // ----------------------------------------------------------------------------
321 // wxSingleChoiceDialog
322 // ----------------------------------------------------------------------------
324 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
325 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
326 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
329 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
331 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
332 const wxString
& message
,
333 const wxString
& caption
,
335 const wxString
*choices
,
340 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
343 #ifdef WXWIN_COMPATIBILITY_2
345 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
346 const wxString
& message
,
347 const wxString
& caption
,
348 const wxStringList
& choices
,
353 Create(parent
, message
, caption
, choices
, clientData
, style
);
356 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
357 const wxString
& message
,
358 const wxString
& caption
,
359 const wxStringList
& choices
,
364 wxString
*strings
= new wxString
[choices
.Number()];
366 for ( i
= 0; i
< choices
.Number(); i
++)
368 strings
[i
] = (char *)choices
.Nth(i
)->Data();
370 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
375 #endif // WXWIN_COMPATIBILITY_2
377 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
378 const wxString
& message
,
379 const wxString
& caption
,
381 const wxString
*choices
,
386 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
391 m_selection
= n
> 0 ? 0 : -1;
395 for (int i
= 0; i
< n
; i
++)
396 m_listbox
->SetClientData(i
, clientData
[i
]);
403 void wxSingleChoiceDialog::SetSelection(int sel
)
405 m_listbox
->SetSelection(sel
);
409 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
411 m_selection
= m_listbox
->GetSelection();
412 m_stringSelection
= m_listbox
->GetStringSelection();
415 if ( m_listbox
->HasClientUntypedData() )
416 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();
428 if ( m_listbox
->HasClientUntypedData() )
429 SetClientData(m_listbox
->GetClientData(m_selection
));
435 // ----------------------------------------------------------------------------
436 // wxMultiChoiceDialog
437 // ----------------------------------------------------------------------------
439 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
441 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
442 const wxString
& message
,
443 const wxString
& caption
,
445 const wxString
*choices
,
449 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
452 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
458 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
460 size_t count
= selections
.GetCount();
461 for ( size_t n
= 0; n
< count
; n
++ )
463 m_listbox
->Select(selections
[n
]);
467 bool wxMultiChoiceDialog::TransferDataFromWindow()
469 m_selections
.Empty();
470 size_t count
= m_listbox
->GetCount();
471 for ( size_t n
= 0; n
< count
; n
++ )
473 if ( m_listbox
->IsSelected(n
) )