]>
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
];
85 for ( int i
= 0; i
< n
; i
++ )
87 (*choices
)[i
] = aChoices
[i
];
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 wxString
wxGetSingleChoice( const wxString
& message
,
98 const wxString
& caption
,
99 int n
, const wxString
*choices
,
101 int WXUNUSED(x
), int WXUNUSED(y
),
102 bool WXUNUSED(centre
),
103 int WXUNUSED(width
), int WXUNUSED(height
) )
105 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
107 if ( dialog
.ShowModal() == wxID_OK
)
108 choice
= dialog
.GetStringSelection();
113 wxString
wxGetSingleChoice( const wxString
& message
,
114 const wxString
& caption
,
115 const wxArrayString
& aChoices
,
119 int width
, int height
)
122 int n
= ConvertWXArrayToC(aChoices
, &choices
);
123 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
124 x
, y
, centre
, width
, height
);
130 #ifdef WXWIN_COMPATIBILITY_2
131 // Overloaded for backward compatibility
132 wxString
wxGetSingleChoice( const wxString
& message
,
133 const wxString
& caption
,
134 int n
, char *choices
[],
136 int x
, int y
, bool centre
,
137 int width
, int height
)
139 wxString
*strings
= new wxString
[n
];
141 for ( i
= 0; i
< n
; i
++)
143 strings
[i
] = choices
[i
];
145 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
146 x
, y
, centre
, width
, height
));
150 #endif // WXWIN_COMPATIBILITY_2
152 int wxGetSingleChoiceIndex( const wxString
& message
,
153 const wxString
& caption
,
154 int n
, const wxString
*choices
,
156 int WXUNUSED(x
), int WXUNUSED(y
),
157 bool WXUNUSED(centre
),
158 int WXUNUSED(width
), int WXUNUSED(height
) )
160 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
162 if ( dialog
.ShowModal() == wxID_OK
)
163 choice
= dialog
.GetSelection();
170 int wxGetSingleChoiceIndex( const wxString
& message
,
171 const wxString
& caption
,
172 const wxArrayString
& aChoices
,
176 int width
, int height
)
179 int n
= ConvertWXArrayToC(aChoices
, &choices
);
180 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
181 x
, y
, centre
, width
, height
);
187 #ifdef WXWIN_COMPATIBILITY_2
188 // Overloaded for backward compatibility
189 int wxGetSingleChoiceIndex( const wxString
& message
,
190 const wxString
& caption
,
191 int n
, wxChar
*choices
[],
193 int x
, int y
, bool centre
,
194 int width
, int height
)
196 wxString
*strings
= new wxString
[n
];
197 for ( int i
= 0; i
< n
; i
++)
198 strings
[i
] = choices
[i
];
199 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
200 x
, y
, centre
, width
, height
);
204 #endif // WXWIN_COMPATIBILITY_2
206 void *wxGetSingleChoiceData( const wxString
& message
,
207 const wxString
& caption
,
208 int n
, const wxString
*choices
,
211 int WXUNUSED(x
), int WXUNUSED(y
),
212 bool WXUNUSED(centre
),
213 int WXUNUSED(width
), int WXUNUSED(height
) )
215 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
216 (char **)client_data
);
218 if ( dialog
.ShowModal() == wxID_OK
)
219 data
= dialog
.GetSelectionClientData();
226 void *wxGetSingleChoiceData( const wxString
& message
,
227 const wxString
& caption
,
228 const wxArrayString
& aChoices
,
233 int width
, int height
)
236 int n
= ConvertWXArrayToC(aChoices
, &choices
);
237 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
239 x
, y
, centre
, width
, height
);
245 #ifdef WXWIN_COMPATIBILITY_2
246 // Overloaded for backward compatibility
247 void *wxGetSingleChoiceData( const wxString
& message
,
248 const wxString
& caption
,
249 int n
, wxChar
*choices
[],
252 int x
, int y
, bool centre
, int width
, int height
)
254 wxString
*strings
= new wxString
[n
];
256 for ( i
= 0; i
< n
; i
++)
258 strings
[i
] = choices
[i
];
260 void *data
= wxGetSingleChoiceData(message
, caption
,
261 n
, (const wxString
*)strings
,
263 x
, y
, centre
, width
, height
);
267 #endif // WXWIN_COMPATIBILITY_2
269 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
270 const wxString
& message
,
271 const wxString
& caption
,
272 int n
, const wxString
*choices
,
274 int WXUNUSED(x
), int WXUNUSED(y
),
275 bool WXUNUSED(centre
),
276 int WXUNUSED(width
), int WXUNUSED(height
))
278 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
280 if ( !selections
.IsEmpty() )
281 dialog
.SetSelections(selections
);
283 if ( dialog
.ShowModal() == wxID_OK
)
284 selections
= dialog
.GetSelections();
288 return selections
.GetCount();
291 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
292 const wxString
& message
,
293 const wxString
& caption
,
294 const wxArrayString
& aChoices
,
298 int width
, int height
)
301 int n
= ConvertWXArrayToC(aChoices
, &choices
);
302 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
304 x
, y
, centre
, width
, height
);
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
315 const wxString
& message
,
316 const wxString
& caption
,
317 int n
, const wxString
*choices
,
318 long WXUNUSED(styleDlg
), // FIXME: why unused?
322 if ( !wxDialog::Create(parent
, -1, caption
, pos
, wxDefaultSize
,
323 wxCHOICEDLG_DIALOG_STYLE
) )
326 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
329 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
332 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
333 wxDefaultPosition
, wxDefaultSize
,
337 m_listbox
->SetSelection(0);
339 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
343 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
347 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
349 SetAutoLayout( TRUE
);
350 SetSizer( topsizer
);
352 topsizer
->SetSizeHints( this );
353 topsizer
->Fit( this );
357 m_listbox
->SetFocus();
362 // ----------------------------------------------------------------------------
363 // wxSingleChoiceDialog
364 // ----------------------------------------------------------------------------
366 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
367 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
368 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
371 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
373 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
374 const wxString
& message
,
375 const wxString
& caption
,
377 const wxString
*choices
,
380 const wxPoint
& WXUNUSED(pos
))
382 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
385 #ifdef WXWIN_COMPATIBILITY_2
387 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
388 const wxString
& message
,
389 const wxString
& caption
,
390 const wxStringList
& choices
,
393 const wxPoint
& WXUNUSED(pos
))
395 Create(parent
, message
, caption
, choices
, clientData
, style
);
398 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
399 const wxString
& message
,
400 const wxString
& caption
,
401 const wxStringList
& choices
,
406 wxString
*strings
= new wxString
[choices
.Number()];
408 for ( i
= 0; i
< choices
.Number(); i
++)
410 strings
[i
] = (char *)choices
.Nth(i
)->Data();
412 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
417 #endif // WXWIN_COMPATIBILITY_2
419 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
420 const wxString
& message
,
421 const wxString
& caption
,
423 const wxString
*choices
,
428 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
433 m_selection
= n
> 0 ? 0 : -1;
437 for (int i
= 0; i
< n
; i
++)
438 m_listbox
->SetClientData(i
, clientData
[i
]);
445 void wxSingleChoiceDialog::SetSelection(int sel
)
447 m_listbox
->SetSelection(sel
);
451 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
453 m_selection
= m_listbox
->GetSelection();
454 m_stringSelection
= m_listbox
->GetStringSelection();
457 if ( m_listbox
->HasClientUntypedData() )
458 SetClientData(m_listbox
->GetClientData(m_selection
));
463 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
465 m_selection
= m_listbox
->GetSelection();
466 m_stringSelection
= m_listbox
->GetStringSelection();
470 if ( m_listbox
->HasClientUntypedData() )
471 SetClientData(m_listbox
->GetClientData(m_selection
));
477 // ----------------------------------------------------------------------------
478 // wxMultiChoiceDialog
479 // ----------------------------------------------------------------------------
481 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
483 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
484 const wxString
& message
,
485 const wxString
& caption
,
487 const wxString
*choices
,
491 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
494 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
500 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
502 size_t count
= selections
.GetCount();
503 for ( size_t n
= 0; n
< count
; n
++ )
505 m_listbox
->Select(selections
[n
]);
509 bool wxMultiChoiceDialog::TransferDataFromWindow()
511 // VZ: I hate to do it but I can't fix wxMotif right now (FIXME)
513 #define IsSelected Selected
516 m_selections
.Empty();
517 size_t count
= m_listbox
->GetCount();
518 for ( size_t n
= 0; n
< count
; n
++ )
520 if ( m_listbox
->IsSelected(n
) )