]>
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"
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 #if defined(__WXMSW__) || defined(__WXMAC__)
57 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
61 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // convert wxArrayString into a wxString[] which must be delete[]d by caller
72 static int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
);
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 int ConvertWXArrayToC(const wxArrayString
& aChoices
, wxString
**choices
)
84 int n
= aChoices
.GetCount();
85 *choices
= new wxString
[n
];
87 for ( int i
= 0; i
< n
; i
++ )
89 (*choices
)[i
] = aChoices
[i
];
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 wxString
wxGetSingleChoice( const wxString
& message
,
100 const wxString
& caption
,
101 int n
, const wxString
*choices
,
103 int WXUNUSED(x
), int WXUNUSED(y
),
104 bool WXUNUSED(centre
),
105 int WXUNUSED(width
), int WXUNUSED(height
) )
107 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
109 if ( dialog
.ShowModal() == wxID_OK
)
110 choice
= dialog
.GetStringSelection();
115 wxString
wxGetSingleChoice( const wxString
& message
,
116 const wxString
& caption
,
117 const wxArrayString
& aChoices
,
121 int width
, int height
)
124 int n
= ConvertWXArrayToC(aChoices
, &choices
);
125 wxString res
= wxGetSingleChoice(message
, caption
, n
, choices
, parent
,
126 x
, y
, centre
, width
, height
);
132 #ifdef WXWIN_COMPATIBILITY_2
133 // Overloaded for backward compatibility
134 wxString
wxGetSingleChoice( const wxString
& message
,
135 const wxString
& caption
,
136 int n
, char *choices
[],
138 int x
, int y
, bool centre
,
139 int width
, int height
)
141 wxString
*strings
= new wxString
[n
];
143 for ( i
= 0; i
< n
; i
++)
145 strings
[i
] = choices
[i
];
147 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
148 x
, y
, centre
, width
, height
));
152 #endif // WXWIN_COMPATIBILITY_2
154 int wxGetSingleChoiceIndex( const wxString
& message
,
155 const wxString
& caption
,
156 int n
, const wxString
*choices
,
158 int WXUNUSED(x
), int WXUNUSED(y
),
159 bool WXUNUSED(centre
),
160 int WXUNUSED(width
), int WXUNUSED(height
) )
162 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
164 if ( dialog
.ShowModal() == wxID_OK
)
165 choice
= dialog
.GetSelection();
172 int wxGetSingleChoiceIndex( const wxString
& message
,
173 const wxString
& caption
,
174 const wxArrayString
& aChoices
,
178 int width
, int height
)
181 int n
= ConvertWXArrayToC(aChoices
, &choices
);
182 int res
= wxGetSingleChoiceIndex(message
, caption
, n
, choices
, parent
,
183 x
, y
, centre
, width
, height
);
189 #ifdef WXWIN_COMPATIBILITY_2
190 // Overloaded for backward compatibility
191 int wxGetSingleChoiceIndex( const wxString
& message
,
192 const wxString
& caption
,
193 int n
, wxChar
*choices
[],
195 int x
, int y
, bool centre
,
196 int width
, int height
)
198 wxString
*strings
= new wxString
[n
];
199 for ( int i
= 0; i
< n
; i
++)
200 strings
[i
] = choices
[i
];
201 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
202 x
, y
, centre
, width
, height
);
206 #endif // WXWIN_COMPATIBILITY_2
208 void *wxGetSingleChoiceData( const wxString
& message
,
209 const wxString
& caption
,
210 int n
, const wxString
*choices
,
213 int WXUNUSED(x
), int WXUNUSED(y
),
214 bool WXUNUSED(centre
),
215 int WXUNUSED(width
), int WXUNUSED(height
) )
217 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
218 (char **)client_data
);
220 if ( dialog
.ShowModal() == wxID_OK
)
221 data
= dialog
.GetSelectionClientData();
228 void *wxGetSingleChoiceData( const wxString
& message
,
229 const wxString
& caption
,
230 const wxArrayString
& aChoices
,
235 int width
, int height
)
238 int n
= ConvertWXArrayToC(aChoices
, &choices
);
239 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
241 x
, y
, centre
, width
, height
);
247 #ifdef WXWIN_COMPATIBILITY_2
248 // Overloaded for backward compatibility
249 void *wxGetSingleChoiceData( const wxString
& message
,
250 const wxString
& caption
,
251 int n
, wxChar
*choices
[],
254 int x
, int y
, bool centre
, int width
, int height
)
256 wxString
*strings
= new wxString
[n
];
258 for ( i
= 0; i
< n
; i
++)
260 strings
[i
] = choices
[i
];
262 void *data
= wxGetSingleChoiceData(message
, caption
,
263 n
, (const wxString
*)strings
,
265 x
, y
, centre
, width
, height
);
269 #endif // WXWIN_COMPATIBILITY_2
271 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
272 const wxString
& message
,
273 const wxString
& caption
,
274 int n
, const wxString
*choices
,
276 int WXUNUSED(x
), int WXUNUSED(y
),
277 bool WXUNUSED(centre
),
278 int WXUNUSED(width
), int WXUNUSED(height
))
280 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
282 if ( !selections
.IsEmpty() )
283 dialog
.SetSelections(selections
);
285 if ( dialog
.ShowModal() == wxID_OK
)
286 selections
= dialog
.GetSelections();
290 return selections
.GetCount();
293 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
294 const wxString
& message
,
295 const wxString
& caption
,
296 const wxArrayString
& aChoices
,
300 int width
, int height
)
303 int n
= ConvertWXArrayToC(aChoices
, &choices
);
304 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
306 x
, y
, centre
, width
, height
);
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
317 const wxString
& message
,
318 const wxString
& caption
,
319 int n
, const wxString
*choices
,
320 long WXUNUSED(styleDlg
), // FIXME: why unused?
324 if ( !wxDialog::Create(parent
, -1, caption
, pos
, wxDefaultSize
,
325 wxCHOICEDLG_DIALOG_STYLE
) )
328 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
331 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
334 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
335 wxDefaultPosition
, wxDefaultSize
,
339 m_listbox
->SetSelection(0);
341 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
345 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
349 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
351 SetAutoLayout( TRUE
);
352 SetSizer( topsizer
);
354 topsizer
->SetSizeHints( this );
355 topsizer
->Fit( this );
359 m_listbox
->SetFocus();
364 // ----------------------------------------------------------------------------
365 // wxSingleChoiceDialog
366 // ----------------------------------------------------------------------------
368 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
369 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
370 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
373 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
375 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
376 const wxString
& message
,
377 const wxString
& caption
,
379 const wxString
*choices
,
382 const wxPoint
& WXUNUSED(pos
))
384 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
387 #ifdef WXWIN_COMPATIBILITY_2
389 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
390 const wxString
& message
,
391 const wxString
& caption
,
392 const wxStringList
& choices
,
395 const wxPoint
& WXUNUSED(pos
))
397 Create(parent
, message
, caption
, choices
, clientData
, style
);
400 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
401 const wxString
& message
,
402 const wxString
& caption
,
403 const wxStringList
& choices
,
408 wxString
*strings
= new wxString
[choices
.Number()];
410 for ( i
= 0; i
< choices
.Number(); i
++)
412 strings
[i
] = (char *)choices
.Nth(i
)->Data();
414 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
419 #endif // WXWIN_COMPATIBILITY_2
421 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
422 const wxString
& message
,
423 const wxString
& caption
,
425 const wxString
*choices
,
430 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
435 m_selection
= n
> 0 ? 0 : -1;
439 for (int i
= 0; i
< n
; i
++)
440 m_listbox
->SetClientData(i
, clientData
[i
]);
447 void wxSingleChoiceDialog::SetSelection(int sel
)
449 m_listbox
->SetSelection(sel
);
453 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
455 m_selection
= m_listbox
->GetSelection();
456 m_stringSelection
= m_listbox
->GetStringSelection();
459 if ( m_listbox
->HasClientUntypedData() )
460 SetClientData(m_listbox
->GetClientData(m_selection
));
465 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
467 m_selection
= m_listbox
->GetSelection();
468 m_stringSelection
= m_listbox
->GetStringSelection();
472 if ( m_listbox
->HasClientUntypedData() )
473 SetClientData(m_listbox
->GetClientData(m_selection
));
479 // ----------------------------------------------------------------------------
480 // wxMultiChoiceDialog
481 // ----------------------------------------------------------------------------
483 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
485 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
486 const wxString
& message
,
487 const wxString
& caption
,
489 const wxString
*choices
,
493 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
496 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
502 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
504 size_t count
= selections
.GetCount();
505 for ( size_t n
= 0; n
< count
; n
++ )
507 m_listbox
->Select(selections
[n
]);
511 bool wxMultiChoiceDialog::TransferDataFromWindow()
513 // VZ: I hate to do it but I can't fix wxMotif right now (FIXME)
515 #define IsSelected Selected
518 m_selections
.Empty();
519 size_t count
= m_listbox
->GetCount();
520 for ( size_t n
= 0; n
< count
; n
++ )
522 if ( m_listbox
->IsSelected(n
) )
529 #endif // wxUSE_CHOICEDLG