]>
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
,
198 (char **)client_data
);
200 if ( dialog
.ShowModal() == wxID_OK
)
201 data
= dialog
.GetSelectionClientData();
208 void *wxGetSingleChoiceData( const wxString
& message
,
209 const wxString
& caption
,
210 const wxArrayString
& aChoices
,
215 int width
, int height
)
218 int n
= ConvertWXArrayToC(aChoices
, &choices
);
219 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
221 x
, y
, centre
, width
, height
);
227 #ifdef WXWIN_COMPATIBILITY_2
228 // Overloaded for backward compatibility
229 void *wxGetSingleChoiceData( const wxString
& message
,
230 const wxString
& caption
,
231 int n
, wxChar
*choices
[],
234 int x
, int y
, bool centre
, int width
, int height
)
236 wxString
*strings
= new wxString
[n
];
238 for ( i
= 0; i
< n
; i
++)
240 strings
[i
] = choices
[i
];
242 void *data
= wxGetSingleChoiceData(message
, caption
,
243 n
, (const wxString
*)strings
,
245 x
, y
, centre
, width
, height
);
249 #endif // WXWIN_COMPATIBILITY_2
251 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
252 const wxString
& message
,
253 const wxString
& caption
,
254 int n
, const wxString
*choices
,
256 int WXUNUSED(x
), int WXUNUSED(y
),
257 bool WXUNUSED(centre
),
258 int WXUNUSED(width
), int WXUNUSED(height
))
260 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
261 if ( dialog
.ShowModal() == wxID_OK
)
262 selections
= dialog
.GetSelections();
266 return selections
.GetCount();
269 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
270 const wxString
& message
,
271 const wxString
& caption
,
272 const wxArrayString
& aChoices
,
276 int width
, int height
)
279 int n
= ConvertWXArrayToC(aChoices
, &choices
);
280 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
282 x
, y
, centre
, width
, height
);
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
293 const wxString
& message
,
294 const wxString
& caption
,
295 int n
, const wxString
*choices
,
296 long WXUNUSED(styleDlg
), // FIXME: why unused?
300 if ( !wxDialog::Create(parent
, -1, caption
, pos
, wxDefaultSize
,
301 wxCHOICEDLG_DIALOG_STYLE
) )
304 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
307 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
310 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
311 wxDefaultPosition
, wxDefaultSize
,
315 m_listbox
->SetSelection(0);
317 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
321 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
325 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
327 SetAutoLayout( TRUE
);
328 SetSizer( topsizer
);
330 topsizer
->SetSizeHints( this );
331 topsizer
->Fit( this );
335 m_listbox
->SetFocus();
340 // ----------------------------------------------------------------------------
341 // wxSingleChoiceDialog
342 // ----------------------------------------------------------------------------
344 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
345 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
346 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
349 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
351 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
352 const wxString
& message
,
353 const wxString
& caption
,
355 const wxString
*choices
,
358 const wxPoint
& WXUNUSED(pos
))
360 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
363 #ifdef WXWIN_COMPATIBILITY_2
365 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
366 const wxString
& message
,
367 const wxString
& caption
,
368 const wxStringList
& choices
,
371 const wxPoint
& WXUNUSED(pos
))
373 Create(parent
, message
, caption
, choices
, clientData
, style
);
376 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
377 const wxString
& message
,
378 const wxString
& caption
,
379 const wxStringList
& choices
,
384 wxString
*strings
= new wxString
[choices
.Number()];
386 for ( i
= 0; i
< choices
.Number(); i
++)
388 strings
[i
] = (char *)choices
.Nth(i
)->Data();
390 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
395 #endif // WXWIN_COMPATIBILITY_2
397 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
398 const wxString
& message
,
399 const wxString
& caption
,
401 const wxString
*choices
,
406 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
411 m_selection
= n
> 0 ? 0 : -1;
415 for (int i
= 0; i
< n
; i
++)
416 m_listbox
->SetClientData(i
, clientData
[i
]);
423 void wxSingleChoiceDialog::SetSelection(int sel
)
425 m_listbox
->SetSelection(sel
);
429 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
431 m_selection
= m_listbox
->GetSelection();
432 m_stringSelection
= m_listbox
->GetStringSelection();
435 if ( m_listbox
->HasClientUntypedData() )
436 SetClientData(m_listbox
->GetClientData(m_selection
));
441 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
443 m_selection
= m_listbox
->GetSelection();
444 m_stringSelection
= m_listbox
->GetStringSelection();
448 if ( m_listbox
->HasClientUntypedData() )
449 SetClientData(m_listbox
->GetClientData(m_selection
));
455 // ----------------------------------------------------------------------------
456 // wxMultiChoiceDialog
457 // ----------------------------------------------------------------------------
459 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
461 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
462 const wxString
& message
,
463 const wxString
& caption
,
465 const wxString
*choices
,
469 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
472 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
478 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
480 size_t count
= selections
.GetCount();
481 for ( size_t n
= 0; n
< count
; n
++ )
483 m_listbox
->Select(selections
[n
]);
487 bool wxMultiChoiceDialog::TransferDataFromWindow()
489 // VZ: I hate to do it but I can't fix wxMotif right now (FIXME)
491 #define IsSelected Selected
494 m_selections
.Empty();
495 size_t count
= m_listbox
->GetCount();
496 for ( size_t n
= 0; n
< count
; n
++ )
498 if ( m_listbox
->IsSelected(n
) )