]>
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/button.h"
28 #include "wx/listbox.h"
29 #include "wx/stattext.h"
34 #include "wx/statline.h"
37 #include "wx/generic/choicdgg.h"
39 #define wxID_LISTBOX 3000
41 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
42 const wxString
*choices
, wxWindow
*parent
,
43 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
44 int WXUNUSED(width
), int WXUNUSED(height
) )
46 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
47 if ( dialog
.ShowModal() == wxID_OK
)
48 return dialog
.GetStringSelection();
53 // Overloaded for backward compatibility
54 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
55 char *choices
[], wxWindow
*parent
,
56 int x
, int y
, bool centre
,
57 int width
, int height
)
59 wxString
*strings
= new wxString
[n
];
61 for ( i
= 0; i
< n
; i
++)
63 strings
[i
] = choices
[i
];
65 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
66 x
, y
, centre
, width
, height
));
71 int wxGetSingleChoiceIndex( 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
)
78 return dialog
.GetSelection();
83 // Overloaded for backward compatibility
84 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
85 wxChar
*choices
[], wxWindow
*parent
,
86 int x
, int y
, bool centre
,
87 int width
, int height
)
89 wxString
*strings
= new wxString
[n
];
90 for ( int i
= 0; i
< n
; i
++)
91 strings
[i
] = choices
[i
];
92 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
93 x
, y
, centre
, width
, height
);
98 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
99 const wxString
*choices
, wxChar
**client_data
, wxWindow
*parent
,
100 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
101 int WXUNUSED(width
), int WXUNUSED(height
) )
103 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
, client_data
);
104 if ( dialog
.ShowModal() == wxID_OK
)
105 return dialog
.GetSelectionClientData();
110 // Overloaded for backward compatibility
111 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
112 wxChar
*choices
[], wxChar
**client_data
, wxWindow
*parent
,
113 int x
, int y
, bool centre
,
114 int width
, int height
)
116 wxString
*strings
= new wxString
[n
];
118 for ( i
= 0; i
< n
; i
++)
120 strings
[i
] = choices
[i
];
122 wxChar
*data
= wxGetSingleChoiceData(message
, caption
, n
, (const wxString
*)strings
, client_data
, parent
,
123 x
, y
, centre
, width
, height
);
129 /* Multiple choice dialog contributed by Robert Cowell
132 The new data passed are in the "int nsel" and "int * selection"
134 The idea is to make a multiple selection from list of strings.
135 The returned value is the total number selected. initialily there
136 are nsel selected, with indices stored in
137 selection[0],...,selection[nsel-1] which appear highlighted to
138 begin with. On exit with value i
139 selection[0..i-1] contains the indices of the selected items.
140 (Some prior selectecions might be deselected.)
141 Thus selection must be as big as choices, in case all items are
146 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
147 int n, const wxString *choices,
148 int nsel, int * selection,
149 wxWindow *parent , int x , int y, bool centre,
150 int width, int height)
156 // wxSingleChoiceDialog
158 #if !USE_SHARED_LIBRARY
159 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
160 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
161 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
164 IMPLEMENT_CLASS(wxSingleChoiceDialog
, wxDialog
)
167 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
171 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
172 const wxString
& message
,
173 const wxString
& caption
,
175 const wxString
*choices
,
179 : wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
,
180 wxCHOICEDLG_DIALOG_STYLE
)
182 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
185 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
186 const wxString
& message
,
187 const wxString
& caption
,
188 const wxStringList
& choices
,
192 : wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
,
193 wxCHOICEDLG_DIALOG_STYLE
)
195 Create(parent
, message
, caption
, choices
, clientData
, style
);
198 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
199 const wxString
& message
,
200 const wxString
& caption
,
201 const wxStringList
& choices
,
206 wxString
*strings
= new wxString
[choices
.Number()];
208 for ( i
= 0; i
< choices
.Number(); i
++)
210 strings
[i
] = (char *)choices
.Nth(i
)->Data();
212 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
217 bool wxSingleChoiceDialog::Create( wxWindow
*WXUNUSED(parent
),
218 const wxString
& message
,
219 const wxString
& WXUNUSED(caption
),
221 const wxString
*choices
,
224 const wxPoint
& WXUNUSED(pos
) )
229 // calc the message size
230 // ---------------------
233 wxSize sizeText
= SplitTextMessage(message
, &lines
);
234 long heightTextMax
= sizeText
.GetHeight(),
235 widthTextMax
= sizeText
.GetWidth();
236 size_t nLineCount
= lines
.Count();
237 long hTotalMsg
= heightTextMax
*nLineCount
;
239 // calc the button size
240 // --------------------
242 // always create the OK button - the code below supposes we do have buttons
243 // and besides the user should have some way to close this dialog
244 wxASSERT_MSG( style
& wxOK
, _T("this dialog should have OK button") );
246 bool hasCancel
= (style
& wxCANCEL
) != 0;
248 wxSize sizeButtons
= GetStandardButtonSize(hasCancel
);
250 long wButton
= sizeButtons
.GetWidth(),
251 hButton
= sizeButtons
.GetHeight();
253 long wTotalButtons
= wButton
;
256 wTotalButtons
*= 2; // second button
257 wTotalButtons
+= MARGIN_BETWEEN_BUTTONS
; // margin between the 2
260 // listbox and stat line
261 // ---------------------
263 // make the listbox at least as tall as the message - otherwise it looks
264 // ugly (the lower limit of 300 for the width is arbitrary OTOH)
266 // NB: we write "n + 2" because the horiz. scrollbar also takes some place
267 long hListbox
= wxMax((n
+ 2) * heightTextMax
, hTotalMsg
),
268 wListbox
= wxMax(300, wxMax(wTotalButtons
, widthTextMax
));
271 long hStatLine
= wxStaticLine::GetDefaultSize();
274 // now the complete dialog size
275 // ----------------------------
277 long hDialog
= 2*LAYOUT_Y_MARGIN
+ // top margin
278 hTotalMsg
+ // message
279 2*LAYOUT_Y_MARGIN
+ // margin between text and listbox
280 hListbox
+ // listbox
282 LAYOUT_Y_MARGIN
+ // margin
283 hStatLine
+ // separator line
285 2*LAYOUT_Y_MARGIN
+ // margin between listbox and buttons
286 hButton
+ // button(s)
287 LAYOUT_Y_MARGIN
; // bottom margin
289 long wDialog
= wxMax(wListbox
, wxMax(wTotalButtons
, widthTextMax
)) +
290 4*LAYOUT_X_MARGIN
; // 2 from each side
292 // create the controls
293 // -------------------
297 int y
= 2*LAYOUT_Y_MARGIN
;
298 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ )
300 text
= new wxStaticText(this, -1, lines
[nLine
],
301 wxPoint(2*LAYOUT_X_MARGIN
, y
),
302 wxSize(widthTextMax
, heightTextMax
));
306 y
+= 2*LAYOUT_X_MARGIN
;
309 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
310 wxPoint(2*LAYOUT_X_MARGIN
, y
),
311 wxSize(wListbox
, hListbox
),
318 for (int i
= 0; i
< n
; i
++)
319 m_listbox
->SetClientData(i
, clientData
[i
]);
324 (void) new wxStaticLine( this, -1,
325 wxPoint(2*LAYOUT_X_MARGIN
, y
+ LAYOUT_Y_MARGIN
),
326 wxSize(wDialog
- 4*LAYOUT_X_MARGIN
, hStatLine
) );
328 y
+= LAYOUT_Y_MARGIN
+ hStatLine
;
333 y
+= 2*LAYOUT_X_MARGIN
;
335 CreateStandardButtons(wDialog
, y
, wButton
, hButton
, hasCancel
);
337 SetClientSize( wDialog
, hDialog
);
345 void wxSingleChoiceDialog::SetSelection(int sel
)
347 m_listbox
->SetSelection(sel
);
351 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
353 m_selection
= m_listbox
->GetSelection();
354 m_stringSelection
= m_listbox
->GetStringSelection();
355 m_clientData
= m_listbox
->GetClientData(m_selection
);
360 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
362 m_selection
= m_listbox
->GetSelection();
363 m_stringSelection
= m_listbox
->GetStringSelection();
364 m_clientData
= m_listbox
->GetClientData(m_selection
);