]>
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 #ifdef WXWIN_COMPATIBILITY_2
171 // Overloaded for backward compatibility
172 int wxGetSingleChoiceIndex( const wxString
& message
,
173 const wxString
& caption
,
174 int n
, wxChar
*choices
[],
176 int x
, int y
, bool centre
,
177 int width
, int height
)
179 wxString
*strings
= new wxString
[n
];
180 for ( int i
= 0; i
< n
; i
++)
181 strings
[i
] = choices
[i
];
182 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
183 x
, y
, centre
, width
, height
);
187 #endif // WXWIN_COMPATIBILITY_2
189 void *wxGetSingleChoiceData( const wxString
& message
,
190 const wxString
& caption
,
191 int n
, const wxString
*choices
,
194 int WXUNUSED(x
), int WXUNUSED(y
),
195 bool WXUNUSED(centre
),
196 int WXUNUSED(width
), int WXUNUSED(height
) )
198 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
,
199 (char **)client_data
);
201 if ( dialog
.ShowModal() == wxID_OK
)
202 data
= dialog
.GetSelectionClientData();
209 void *wxGetSingleChoiceData( const wxString
& message
,
210 const wxString
& caption
,
211 const wxArrayString
& aChoices
,
216 int width
, int height
)
219 int n
= ConvertWXArrayToC(aChoices
, &choices
);
220 void *res
= wxGetSingleChoiceData(message
, caption
, n
, choices
,
222 x
, y
, centre
, width
, height
);
228 #ifdef WXWIN_COMPATIBILITY_2
229 // Overloaded for backward compatibility
230 void *wxGetSingleChoiceData( const wxString
& message
,
231 const wxString
& caption
,
232 int n
, wxChar
*choices
[],
235 int x
, int y
, bool centre
, int width
, int height
)
237 wxString
*strings
= new wxString
[n
];
239 for ( i
= 0; i
< n
; i
++)
241 strings
[i
] = choices
[i
];
243 void *data
= wxGetSingleChoiceData(message
, caption
,
244 n
, (const wxString
*)strings
,
246 x
, y
, centre
, width
, height
);
250 #endif // WXWIN_COMPATIBILITY_2
252 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
253 const wxString
& message
,
254 const wxString
& caption
,
255 int n
, const wxString
*choices
,
257 int WXUNUSED(x
), int WXUNUSED(y
),
258 bool WXUNUSED(centre
),
259 int WXUNUSED(width
), int WXUNUSED(height
))
261 wxMultiChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
263 if ( !selections
.IsEmpty() )
264 dialog
.SetSelections(selections
);
266 if ( dialog
.ShowModal() == wxID_OK
)
267 selections
= dialog
.GetSelections();
271 return selections
.GetCount();
274 size_t wxGetMultipleChoices(wxArrayInt
& selections
,
275 const wxString
& message
,
276 const wxString
& caption
,
277 const wxArrayString
& aChoices
,
281 int width
, int height
)
284 int n
= ConvertWXArrayToC(aChoices
, &choices
);
285 size_t res
= wxGetMultipleChoices(selections
, message
, caption
,
287 x
, y
, centre
, width
, height
);
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 bool wxAnyChoiceDialog::Create(wxWindow
*parent
,
298 const wxString
& message
,
299 const wxString
& caption
,
300 int n
, const wxString
*choices
,
301 long WXUNUSED(styleDlg
), // FIXME: why unused?
305 if ( !wxDialog::Create(parent
, -1, caption
, pos
, wxDefaultSize
,
306 wxCHOICEDLG_DIALOG_STYLE
) )
309 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
312 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
315 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
316 wxDefaultPosition
, wxDefaultSize
,
320 m_listbox
->SetSelection(0);
322 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
326 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
330 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
332 SetAutoLayout( TRUE
);
333 SetSizer( topsizer
);
335 topsizer
->SetSizeHints( this );
336 topsizer
->Fit( this );
340 m_listbox
->SetFocus();
345 // ----------------------------------------------------------------------------
346 // wxSingleChoiceDialog
347 // ----------------------------------------------------------------------------
349 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
350 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
351 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
354 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog
, wxDialog
)
356 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
357 const wxString
& message
,
358 const wxString
& caption
,
360 const wxString
*choices
,
363 const wxPoint
& WXUNUSED(pos
))
365 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
368 #ifdef WXWIN_COMPATIBILITY_2
370 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
371 const wxString
& message
,
372 const wxString
& caption
,
373 const wxStringList
& choices
,
376 const wxPoint
& WXUNUSED(pos
))
378 Create(parent
, message
, caption
, choices
, clientData
, style
);
381 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
382 const wxString
& message
,
383 const wxString
& caption
,
384 const wxStringList
& choices
,
389 wxString
*strings
= new wxString
[choices
.Number()];
391 for ( i
= 0; i
< choices
.Number(); i
++)
393 strings
[i
] = (char *)choices
.Nth(i
)->Data();
395 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
400 #endif // WXWIN_COMPATIBILITY_2
402 bool wxSingleChoiceDialog::Create( wxWindow
*parent
,
403 const wxString
& message
,
404 const wxString
& caption
,
406 const wxString
*choices
,
411 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
416 m_selection
= n
> 0 ? 0 : -1;
420 for (int i
= 0; i
< n
; i
++)
421 m_listbox
->SetClientData(i
, clientData
[i
]);
428 void wxSingleChoiceDialog::SetSelection(int sel
)
430 m_listbox
->SetSelection(sel
);
434 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
436 m_selection
= m_listbox
->GetSelection();
437 m_stringSelection
= m_listbox
->GetStringSelection();
440 if ( m_listbox
->HasClientUntypedData() )
441 SetClientData(m_listbox
->GetClientData(m_selection
));
446 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
448 m_selection
= m_listbox
->GetSelection();
449 m_stringSelection
= m_listbox
->GetStringSelection();
453 if ( m_listbox
->HasClientUntypedData() )
454 SetClientData(m_listbox
->GetClientData(m_selection
));
460 // ----------------------------------------------------------------------------
461 // wxMultiChoiceDialog
462 // ----------------------------------------------------------------------------
464 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog
, wxDialog
)
466 bool wxMultiChoiceDialog::Create( wxWindow
*parent
,
467 const wxString
& message
,
468 const wxString
& caption
,
470 const wxString
*choices
,
474 if ( !wxAnyChoiceDialog::Create(parent
, message
, caption
,
477 wxLB_ALWAYS_SB
| wxLB_EXTENDED
) )
483 void wxMultiChoiceDialog::SetSelections(const wxArrayInt
& selections
)
485 size_t count
= selections
.GetCount();
486 for ( size_t n
= 0; n
< count
; n
++ )
488 m_listbox
->Select(selections
[n
]);
492 bool wxMultiChoiceDialog::TransferDataFromWindow()
494 // VZ: I hate to do it but I can't fix wxMotif right now (FIXME)
496 #define IsSelected Selected
499 m_selections
.Empty();
500 size_t count
= m_listbox
->GetCount();
501 for ( size_t n
= 0; n
< count
; n
++ )
503 if ( m_listbox
->IsSelected(n
) )