]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Choice dialogs
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choicdgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/dialog.h"
27 #include "wx/listbox.h"
28 #include "wx/button.h"
29 #include "wx/stattext.h"
30 #include "wx/layout.h"
34 #include "wx/generic/choicdgg.h"
36 extern void wxSplitMessage2(const char *message
, wxList
*messageList
, wxWindow
*parent
, wxRowColSizer
*sizer
);
38 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
39 const wxString
*choices
, wxWindow
*parent
,
40 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
41 int WXUNUSED(width
), int WXUNUSED(height
) )
43 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
44 if ( dialog
.ShowModal() == wxID_OK
)
46 return dialog
.GetStringSelection();
52 // Overloaded for backward compatibility
53 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
54 char *choices
[], wxWindow
*parent
,
55 int x
, int y
, bool centre
,
56 int width
, int height
)
58 wxString
*strings
= new wxString
[n
];
60 for ( i
= 0; i
< n
; i
++)
62 strings
[i
] = choices
[i
];
64 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
65 x
, y
, centre
, width
, height
));
70 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
71 const wxString
*choices
, wxWindow
*parent
,
72 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
73 int WXUNUSED(width
), int WXUNUSED(height
) )
75 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
76 if ( dialog
.ShowModal() == wxID_OK
)
78 return dialog
.GetSelection();
84 // Overloaded for backward compatibility
85 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
86 char *choices
[], wxWindow
*parent
,
87 int x
, int y
, bool centre
,
88 int width
, int height
)
90 wxString
*strings
= new wxString
[n
];
92 for ( i
= 0; i
< n
; i
++)
94 strings
[i
] = choices
[i
];
96 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
97 x
, y
, centre
, width
, height
);
102 char *wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
103 const wxString
*choices
, char **client_data
, wxWindow
*parent
,
104 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
105 int WXUNUSED(width
), int WXUNUSED(height
) )
107 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
, client_data
);
108 if ( dialog
.ShowModal() == wxID_OK
)
110 return dialog
.GetSelectionClientData();
116 // Overloaded for backward compatibility
117 char *wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
118 char *choices
[], char **client_data
, wxWindow
*parent
,
119 int x
, int y
, bool centre
,
120 int width
, int height
)
122 wxString
*strings
= new wxString
[n
];
124 for ( i
= 0; i
< n
; i
++)
126 strings
[i
] = choices
[i
];
128 char *data
= wxGetSingleChoiceData(message
, caption
, n
, (const wxString
*)strings
, client_data
, parent
,
129 x
, y
, centre
, width
, height
);
135 /* Multiple choice dialog contributed by Robert Cowell
138 The new data passed are in the "int nsel" and "int * selection"
140 The idea is to make a multiple selection from list of strings.
141 The returned value is the total number selected. initialily there
142 are nsel selected, with indices stored in
143 selection[0],...,selection[nsel-1] which appear highlighted to
144 begin with. On exit with value i
145 selection[0..i-1] contains the indices of the selected items.
146 (Some prior selectecions might be deselected.)
147 Thus selection must be as big as choices, in case all items are
152 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
153 int n, const wxString *choices,
154 int nsel, int * selection,
155 wxWindow *parent , int x , int y, bool centre,
156 int width, int height)
162 // wxSingleChoiceDialog
164 #if !USE_SHARED_LIBRARY
165 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
166 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
167 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
170 IMPLEMENT_CLASS(wxSingleChoiceDialog
, wxDialog
)
173 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
174 int n
, const wxString
*choices
, char **clientData
, long style
, const wxPoint
& pos
):
175 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
177 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
180 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
181 const wxStringList
& choices
, char **clientData
, long style
, const wxPoint
& pos
):
182 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
184 Create(parent
, message
, caption
, choices
, clientData
, style
);
187 bool wxSingleChoiceDialog::Create(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
188 const wxStringList
& choices
, char **clientData
, long style
, const wxPoint
& pos
)
190 wxString
*strings
= new wxString
[choices
.Number()];
192 for ( i
= 0; i
< choices
.Number(); i
++)
194 strings
[i
] = (char *)choices
.Nth(i
)->Data();
196 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
201 bool wxSingleChoiceDialog::Create( wxWindow
*WXUNUSED(parent
), const wxString
& message
,
202 const wxString
& WXUNUSED(caption
), int n
,
203 const wxString
*choices
, char **clientData
, long style
,
204 const wxPoint
& WXUNUSED(pos
) )
206 m_dialogStyle
= style
;
208 m_stringSelection
= "";
213 wxSizer
*topSizer
= new wxSizer(this, wxSizerShrink
);
214 topSizer
->SetBorder(10, 10);
216 wxRowColSizer
*messageSizer
= new wxRowColSizer(topSizer
, wxSIZER_COLS
, 100);
217 messageSizer
->SetName("messageSizer");
219 // bool centre = ((style & wxCENTRE) == wxCENTRE);
222 wxSplitMessage2(message
, &messageList
, this, messageSizer
);
225 wxSpacingSizer
*spacingSizer
= new wxSpacingSizer(topSizer
, wxBelow
, messageSizer
, 10);
227 wxListBox
*listBox
= new wxListBox(this, wxID_LISTBOX
, wxPoint(-1, -1), wxSize(240, 160),
229 listBox
->SetSelection(m_selection
);
233 for ( i
= 0; i
< n
; i
++)
235 listBox
->SetClientData(i
, clientData
[i
]);
239 wxRowColSizer
*listBoxSizer
= new wxRowColSizer(topSizer
, wxSIZER_ROWS
);
240 listBoxSizer
->AddSizerChild(listBox
);
241 listBoxSizer
->SetName("listBoxSizer");
243 // Create constraints for the text sizer
244 wxLayoutConstraints
*textC
= new wxLayoutConstraints
;
245 textC
->left
.SameAs (messageSizer
, wxLeft
);
246 textC
->top
.Below (spacingSizer
);
247 listBoxSizer
->SetConstraints(textC
);
249 // Insert another spacer
250 wxSpacingSizer
*spacingSizer2
= new wxSpacingSizer(topSizer
, wxBelow
, listBoxSizer
, 10);
251 spacingSizer
->SetName("spacingSizer2");
253 // Insert a sizer for the buttons
254 wxRowColSizer
*buttonSizer
= new wxRowColSizer(topSizer
, wxSIZER_ROWS
);
255 buttonSizer
->SetName("buttonSizer");
257 // Specify constraints for the button sizer
258 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
261 c
->top
.Below (spacingSizer2
);
262 c
->centreX
.SameAs (listBoxSizer
, wxCentreX
);
263 buttonSizer
->SetConstraints(c
);
266 wxButton
*cancel
= NULL
;
269 ok
= new wxButton(this, wxID_OK
, _("OK"));
270 buttonSizer
->AddSizerChild(ok
);
273 if (style
& wxCANCEL
) {
274 cancel
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
275 buttonSizer
->AddSizerChild(cancel
);
293 void wxSingleChoiceDialog::SetSelection(int sel
)
295 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
298 listBox
->SetSelection(sel
);
303 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
305 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
308 m_selection
= listBox
->GetSelection();
309 m_stringSelection
= listBox
->GetStringSelection();
310 m_clientData
= listBox
->GetClientData(m_selection
);
316 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
318 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
321 m_selection
= listBox
->GetSelection();
322 m_stringSelection
= listBox
->GetStringSelection();
323 m_clientData
= listBox
->GetClientData(m_selection
);