]>
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"
28 #include "wx/dialog.h"
29 #include "wx/listbox.h"
30 #include "wx/button.h"
31 #include "wx/stattext.h"
32 #include "wx/layout.h"
36 #include "wx/generic/choicdgg.h"
38 // Split message, using constraints to position controls
39 static void wxSplitMessage2(const wxChar
*message
, wxList
*messageList
, wxWindow
*parent
, wxRowColSizer
*sizer
)
41 wxChar
*copyMessage
= copystring(message
);
43 size_t len
= wxStrlen(copyMessage
);
44 wxChar
*currentMessage
= copyMessage
;
46 // wxWindow *lastWindow = parent;
49 while ((i
< len
) && (copyMessage
[i
] != _T('\n'))) i
++;
50 if (i
< len
) copyMessage
[i
] = 0;
51 wxStaticText
*mess
= new wxStaticText(parent
, -1, currentMessage
);
54 wxLayoutConstraints *c = new wxLayoutConstraints;
55 c->left.SameAs (parent, wxLeft, 10);
56 c->top.SameAs (lastWindow, wxBottom, 5);
60 mess->SetConstraints(c);
62 sizer
->AddSizerChild(mess
);
64 messageList
->Append(mess
);
66 currentMessage
= copyMessage
+ i
+ 1;
71 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
72 const wxString
*choices
, wxWindow
*parent
,
73 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
74 int WXUNUSED(width
), int WXUNUSED(height
) )
76 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
77 if ( dialog
.ShowModal() == wxID_OK
)
79 return dialog
.GetStringSelection();
85 // Overloaded for backward compatibility
86 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
87 char *choices
[], wxWindow
*parent
,
88 int x
, int y
, bool centre
,
89 int width
, int height
)
91 wxString
*strings
= new wxString
[n
];
93 for ( i
= 0; i
< n
; i
++)
95 strings
[i
] = choices
[i
];
97 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
98 x
, y
, centre
, width
, height
));
103 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
104 const wxString
*choices
, wxWindow
*parent
,
105 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
106 int WXUNUSED(width
), int WXUNUSED(height
) )
108 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
109 if ( dialog
.ShowModal() == wxID_OK
)
111 return dialog
.GetSelection();
117 // Overloaded for backward compatibility
118 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
119 wxChar
*choices
[], wxWindow
*parent
,
120 int x
, int y
, bool centre
,
121 int width
, int height
)
123 wxString
*strings
= new wxString
[n
];
125 for ( i
= 0; i
< n
; i
++)
127 strings
[i
] = choices
[i
];
129 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
130 x
, y
, centre
, width
, height
);
135 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
136 const wxString
*choices
, wxChar
**client_data
, wxWindow
*parent
,
137 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
138 int WXUNUSED(width
), int WXUNUSED(height
) )
140 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
, client_data
);
141 if ( dialog
.ShowModal() == wxID_OK
)
143 return dialog
.GetSelectionClientData();
149 // Overloaded for backward compatibility
150 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
151 wxChar
*choices
[], wxChar
**client_data
, wxWindow
*parent
,
152 int x
, int y
, bool centre
,
153 int width
, int height
)
155 wxString
*strings
= new wxString
[n
];
157 for ( i
= 0; i
< n
; i
++)
159 strings
[i
] = choices
[i
];
161 wxChar
*data
= wxGetSingleChoiceData(message
, caption
, n
, (const wxString
*)strings
, client_data
, parent
,
162 x
, y
, centre
, width
, height
);
168 /* Multiple choice dialog contributed by Robert Cowell
171 The new data passed are in the "int nsel" and "int * selection"
173 The idea is to make a multiple selection from list of strings.
174 The returned value is the total number selected. initialily there
175 are nsel selected, with indices stored in
176 selection[0],...,selection[nsel-1] which appear highlighted to
177 begin with. On exit with value i
178 selection[0..i-1] contains the indices of the selected items.
179 (Some prior selectecions might be deselected.)
180 Thus selection must be as big as choices, in case all items are
185 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
186 int n, const wxString *choices,
187 int nsel, int * selection,
188 wxWindow *parent , int x , int y, bool centre,
189 int width, int height)
195 // wxSingleChoiceDialog
197 #if !USE_SHARED_LIBRARY
198 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
199 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
200 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
203 IMPLEMENT_CLASS(wxSingleChoiceDialog
, wxDialog
)
206 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
207 int n
, const wxString
*choices
, wxChar
**clientData
, long style
, const wxPoint
& pos
):
208 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
|wxTAB_TRAVERSAL
)
210 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
213 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
214 const wxStringList
& choices
, wxChar
**clientData
, long style
, const wxPoint
& pos
):
215 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
217 Create(parent
, message
, caption
, choices
, clientData
, style
);
220 bool wxSingleChoiceDialog::Create(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
221 const wxStringList
& choices
, wxChar
**clientData
, long style
, const wxPoint
& pos
)
223 wxString
*strings
= new wxString
[choices
.Number()];
225 for ( i
= 0; i
< choices
.Number(); i
++)
227 strings
[i
] = (char *)choices
.Nth(i
)->Data();
229 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
234 bool wxSingleChoiceDialog::Create( wxWindow
*WXUNUSED(parent
), const wxString
& message
,
235 const wxString
& WXUNUSED(caption
), int n
,
236 const wxString
*choices
, wxChar
**clientData
, long style
,
237 const wxPoint
& WXUNUSED(pos
) )
239 m_dialogStyle
= style
;
241 m_stringSelection
= _T("");
246 wxSizer
*topSizer
= new wxSizer(this, wxSizerShrink
);
247 topSizer
->SetBorder(10, 10);
249 wxRowColSizer
*messageSizer
= new wxRowColSizer(topSizer
, wxSIZER_COLS
, 100);
250 messageSizer
->SetName(_T("messageSizer"));
252 // bool centre = ((style & wxCENTRE) == wxCENTRE);
255 wxSplitMessage2(message
, &messageList
, this, messageSizer
);
258 wxSpacingSizer
*spacingSizer
= new wxSpacingSizer(topSizer
, wxBelow
, messageSizer
, 10);
260 wxListBox
*listBox
= new wxListBox(this, wxID_LISTBOX
, wxPoint(-1, -1), wxSize(240, 160),
262 listBox
->SetSelection(m_selection
);
266 for ( i
= 0; i
< n
; i
++)
268 listBox
->SetClientData(i
, clientData
[i
]);
272 wxRowColSizer
*listBoxSizer
= new wxRowColSizer(topSizer
, wxSIZER_ROWS
);
273 listBoxSizer
->AddSizerChild(listBox
);
274 listBoxSizer
->SetName(_T("listBoxSizer"));
276 // Create constraints for the text sizer
277 wxLayoutConstraints
*textC
= new wxLayoutConstraints
;
278 textC
->left
.SameAs (messageSizer
, wxLeft
);
279 textC
->top
.Below (spacingSizer
);
280 listBoxSizer
->SetConstraints(textC
);
282 // Insert another spacer
283 wxSpacingSizer
*spacingSizer2
= new wxSpacingSizer(topSizer
, wxBelow
, listBoxSizer
, 10);
284 spacingSizer
->SetName(_T("spacingSizer2"));
286 // Insert a sizer for the buttons
287 wxRowColSizer
*buttonSizer
= new wxRowColSizer(topSizer
, wxSIZER_ROWS
);
288 buttonSizer
->SetName(_T("buttonSizer"));
289 buttonSizer
->SetSpacing(12,0);
291 // Specify constraints for the button sizer
292 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
295 c
->top
.Below (spacingSizer2
);
296 c
->centreX
.SameAs (listBoxSizer
, wxCentreX
);
297 buttonSizer
->SetConstraints(c
);
300 wxButton
*cancel
= NULL
;
303 ok
= new wxButton(this, wxID_OK
, _("OK"), wxDefaultPosition
, wxSize(75,-1) );
304 buttonSizer
->AddSizerChild(ok
);
307 if (style
& wxCANCEL
) {
308 cancel
= new wxButton(this, wxID_CANCEL
, _("Cancel"), wxDefaultPosition
, wxSize(75,-1));
309 buttonSizer
->AddSizerChild(cancel
);
327 void wxSingleChoiceDialog::SetSelection(int sel
)
329 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
332 listBox
->SetSelection(sel
);
337 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
339 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
342 m_selection
= listBox
->GetSelection();
343 m_stringSelection
= listBox
->GetStringSelection();
344 m_clientData
= (wxChar
*)listBox
->GetClientData(m_selection
);
350 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
352 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
355 m_selection
= listBox
->GetSelection();
356 m_stringSelection
= listBox
->GetStringSelection();
357 m_clientData
= (wxChar
*)listBox
->GetClientData(m_selection
);
363 #endif // wxUSE_CONSTRAINTS