]>
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 // Split message, using constraints to position controls
37 static void wxSplitMessage2(const char *message
, wxList
*messageList
, wxWindow
*parent
, wxRowColSizer
*sizer
)
39 char *copyMessage
= copystring(message
);
41 size_t len
= strlen(copyMessage
);
42 char *currentMessage
= copyMessage
;
44 // wxWindow *lastWindow = parent;
47 while ((i
< len
) && (copyMessage
[i
] != '\n')) i
++;
48 if (i
< len
) copyMessage
[i
] = 0;
49 wxStaticText
*mess
= new wxStaticText(parent
, -1, currentMessage
);
52 wxLayoutConstraints *c = new wxLayoutConstraints;
53 c->left.SameAs (parent, wxLeft, 10);
54 c->top.SameAs (lastWindow, wxBottom, 5);
58 mess->SetConstraints(c);
60 sizer
->AddSizerChild(mess
);
62 messageList
->Append(mess
);
64 currentMessage
= copyMessage
+ i
+ 1;
69 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
70 const wxString
*choices
, wxWindow
*parent
,
71 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
72 int WXUNUSED(width
), int WXUNUSED(height
) )
74 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
75 if ( dialog
.ShowModal() == wxID_OK
)
77 return dialog
.GetStringSelection();
83 // Overloaded for backward compatibility
84 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
85 char *choices
[], wxWindow
*parent
,
86 int x
, int y
, bool centre
,
87 int width
, int height
)
89 wxString
*strings
= new wxString
[n
];
91 for ( i
= 0; i
< n
; i
++)
93 strings
[i
] = choices
[i
];
95 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
96 x
, y
, centre
, width
, height
));
101 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
102 const wxString
*choices
, wxWindow
*parent
,
103 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
104 int WXUNUSED(width
), int WXUNUSED(height
) )
106 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
107 if ( dialog
.ShowModal() == wxID_OK
)
109 return dialog
.GetSelection();
115 // Overloaded for backward compatibility
116 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
117 char *choices
[], wxWindow
*parent
,
118 int x
, int y
, bool centre
,
119 int width
, int height
)
121 wxString
*strings
= new wxString
[n
];
123 for ( i
= 0; i
< n
; i
++)
125 strings
[i
] = choices
[i
];
127 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
128 x
, y
, centre
, width
, height
);
133 char *wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
134 const wxString
*choices
, char **client_data
, wxWindow
*parent
,
135 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
136 int WXUNUSED(width
), int WXUNUSED(height
) )
138 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
, client_data
);
139 if ( dialog
.ShowModal() == wxID_OK
)
141 return dialog
.GetSelectionClientData();
147 // Overloaded for backward compatibility
148 char *wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
149 char *choices
[], char **client_data
, wxWindow
*parent
,
150 int x
, int y
, bool centre
,
151 int width
, int height
)
153 wxString
*strings
= new wxString
[n
];
155 for ( i
= 0; i
< n
; i
++)
157 strings
[i
] = choices
[i
];
159 char *data
= wxGetSingleChoiceData(message
, caption
, n
, (const wxString
*)strings
, client_data
, parent
,
160 x
, y
, centre
, width
, height
);
166 /* Multiple choice dialog contributed by Robert Cowell
169 The new data passed are in the "int nsel" and "int * selection"
171 The idea is to make a multiple selection from list of strings.
172 The returned value is the total number selected. initialily there
173 are nsel selected, with indices stored in
174 selection[0],...,selection[nsel-1] which appear highlighted to
175 begin with. On exit with value i
176 selection[0..i-1] contains the indices of the selected items.
177 (Some prior selectecions might be deselected.)
178 Thus selection must be as big as choices, in case all items are
183 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
184 int n, const wxString *choices,
185 int nsel, int * selection,
186 wxWindow *parent , int x , int y, bool centre,
187 int width, int height)
193 // wxSingleChoiceDialog
195 #if !USE_SHARED_LIBRARY
196 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
197 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
198 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
201 IMPLEMENT_CLASS(wxSingleChoiceDialog
, wxDialog
)
204 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
205 int n
, const wxString
*choices
, char **clientData
, long style
, const wxPoint
& pos
):
206 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
|wxTAB_TRAVERSAL
)
208 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
211 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
212 const wxStringList
& choices
, char **clientData
, long style
, const wxPoint
& pos
):
213 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
215 Create(parent
, message
, caption
, choices
, clientData
, style
);
218 bool wxSingleChoiceDialog::Create(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
219 const wxStringList
& choices
, char **clientData
, long style
, const wxPoint
& pos
)
221 wxString
*strings
= new wxString
[choices
.Number()];
223 for ( i
= 0; i
< choices
.Number(); i
++)
225 strings
[i
] = (char *)choices
.Nth(i
)->Data();
227 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
232 bool wxSingleChoiceDialog::Create( wxWindow
*WXUNUSED(parent
), const wxString
& message
,
233 const wxString
& WXUNUSED(caption
), int n
,
234 const wxString
*choices
, char **clientData
, long style
,
235 const wxPoint
& WXUNUSED(pos
) )
237 m_dialogStyle
= style
;
239 m_stringSelection
= "";
244 wxSizer
*topSizer
= new wxSizer(this, wxSizerShrink
);
245 topSizer
->SetBorder(10, 10);
247 wxRowColSizer
*messageSizer
= new wxRowColSizer(topSizer
, wxSIZER_COLS
, 100);
248 messageSizer
->SetName("messageSizer");
250 // bool centre = ((style & wxCENTRE) == wxCENTRE);
253 wxSplitMessage2(message
, &messageList
, this, messageSizer
);
256 wxSpacingSizer
*spacingSizer
= new wxSpacingSizer(topSizer
, wxBelow
, messageSizer
, 10);
258 wxListBox
*listBox
= new wxListBox(this, wxID_LISTBOX
, wxPoint(-1, -1), wxSize(240, 160),
260 listBox
->SetSelection(m_selection
);
264 for ( i
= 0; i
< n
; i
++)
266 listBox
->SetClientData(i
, clientData
[i
]);
270 wxRowColSizer
*listBoxSizer
= new wxRowColSizer(topSizer
, wxSIZER_ROWS
);
271 listBoxSizer
->AddSizerChild(listBox
);
272 listBoxSizer
->SetName("listBoxSizer");
274 // Create constraints for the text sizer
275 wxLayoutConstraints
*textC
= new wxLayoutConstraints
;
276 textC
->left
.SameAs (messageSizer
, wxLeft
);
277 textC
->top
.Below (spacingSizer
);
278 listBoxSizer
->SetConstraints(textC
);
280 // Insert another spacer
281 wxSpacingSizer
*spacingSizer2
= new wxSpacingSizer(topSizer
, wxBelow
, listBoxSizer
, 10);
282 spacingSizer
->SetName("spacingSizer2");
284 // Insert a sizer for the buttons
285 wxRowColSizer
*buttonSizer
= new wxRowColSizer(topSizer
, wxSIZER_ROWS
);
286 buttonSizer
->SetName("buttonSizer");
287 buttonSizer
->SetSpacing(12,0);
289 // Specify constraints for the button sizer
290 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
293 c
->top
.Below (spacingSizer2
);
294 c
->centreX
.SameAs (listBoxSizer
, wxCentreX
);
295 buttonSizer
->SetConstraints(c
);
298 wxButton
*cancel
= NULL
;
301 ok
= new wxButton(this, wxID_OK
, _("OK"), wxDefaultPosition
, wxSize(75,-1) );
302 buttonSizer
->AddSizerChild(ok
);
305 if (style
& wxCANCEL
) {
306 cancel
= new wxButton(this, wxID_CANCEL
, _("Cancel"), wxDefaultPosition
, wxSize(75,-1));
307 buttonSizer
->AddSizerChild(cancel
);
325 void wxSingleChoiceDialog::SetSelection(int sel
)
327 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
330 listBox
->SetSelection(sel
);
335 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
337 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
340 m_selection
= listBox
->GetSelection();
341 m_stringSelection
= listBox
->GetStringSelection();
342 m_clientData
= (char*)listBox
->GetClientData(m_selection
);
348 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
350 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
353 m_selection
= listBox
->GetSelection();
354 m_stringSelection
= listBox
->GetStringSelection();
355 m_clientData
= (char*)listBox
->GetClientData(m_selection
);