]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
be19331e80c387ced83df79d8b51b76e8c8d8bb1
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"
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
36 #include "wx/statline.h"
39 #include "wx/generic/choicdgg.h"
41 #define wxID_LISTBOX 3000
43 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
44 const wxString
*choices
, wxWindow
*parent
,
45 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
46 int WXUNUSED(width
), int WXUNUSED(height
) )
48 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
49 if ( dialog
.ShowModal() == wxID_OK
)
50 return dialog
.GetStringSelection();
55 // Overloaded for backward compatibility
56 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
57 char *choices
[], wxWindow
*parent
,
58 int x
, int y
, bool centre
,
59 int width
, int height
)
61 wxString
*strings
= new wxString
[n
];
63 for ( i
= 0; i
< n
; i
++)
65 strings
[i
] = choices
[i
];
67 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
68 x
, y
, centre
, width
, height
));
73 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
74 const wxString
*choices
, wxWindow
*parent
,
75 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
76 int WXUNUSED(width
), int WXUNUSED(height
) )
78 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
79 if ( dialog
.ShowModal() == wxID_OK
)
80 return dialog
.GetSelection();
85 // Overloaded for backward compatibility
86 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
87 wxChar
*choices
[], wxWindow
*parent
,
88 int x
, int y
, bool centre
,
89 int width
, int height
)
91 wxString
*strings
= new wxString
[n
];
92 for ( int i
= 0; i
< n
; i
++)
93 strings
[i
] = choices
[i
];
94 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
95 x
, y
, centre
, width
, height
);
100 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
101 const wxString
*choices
, wxChar
**client_data
, wxWindow
*parent
,
102 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
103 int WXUNUSED(width
), int WXUNUSED(height
) )
105 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
, client_data
);
106 if ( dialog
.ShowModal() == wxID_OK
)
107 return dialog
.GetSelectionClientData();
112 // Overloaded for backward compatibility
113 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
114 wxChar
*choices
[], wxChar
**client_data
, wxWindow
*parent
,
115 int x
, int y
, bool centre
,
116 int width
, int height
)
118 wxString
*strings
= new wxString
[n
];
120 for ( i
= 0; i
< n
; i
++)
122 strings
[i
] = choices
[i
];
124 wxChar
*data
= wxGetSingleChoiceData(message
, caption
, n
, (const wxString
*)strings
, client_data
, parent
,
125 x
, y
, centre
, width
, height
);
131 /* Multiple choice dialog contributed by Robert Cowell
134 The new data passed are in the "int nsel" and "int * selection"
136 The idea is to make a multiple selection from list of strings.
137 The returned value is the total number selected. initialily there
138 are nsel selected, with indices stored in
139 selection[0],...,selection[nsel-1] which appear highlighted to
140 begin with. On exit with value i
141 selection[0..i-1] contains the indices of the selected items.
142 (Some prior selectecions might be deselected.)
143 Thus selection must be as big as choices, in case all items are
148 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
149 int n, const wxString *choices,
150 int nsel, int * selection,
151 wxWindow *parent , int x , int y, bool centre,
152 int width, int height)
158 // wxSingleChoiceDialog
160 #if !USE_SHARED_LIBRARY
161 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
162 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
163 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
166 IMPLEMENT_CLASS(wxSingleChoiceDialog
, wxDialog
)
169 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
173 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
174 const wxString
& message
,
175 const wxString
& caption
,
177 const wxString
*choices
,
181 : wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
,
182 wxCHOICEDLG_DIALOG_STYLE
)
184 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
187 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
188 const wxString
& message
,
189 const wxString
& caption
,
190 const wxStringList
& choices
,
194 : wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
,
195 wxCHOICEDLG_DIALOG_STYLE
)
197 Create(parent
, message
, caption
, choices
, clientData
, style
);
200 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
201 const wxString
& message
,
202 const wxString
& caption
,
203 const wxStringList
& choices
,
208 wxString
*strings
= new wxString
[choices
.Number()];
210 for ( i
= 0; i
< choices
.Number(); i
++)
212 strings
[i
] = (char *)choices
.Nth(i
)->Data();
214 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
219 bool wxSingleChoiceDialog::Create( wxWindow
*WXUNUSED(parent
),
220 const wxString
& message
,
221 const wxString
& WXUNUSED(caption
),
223 const wxString
*choices
,
226 const wxPoint
& WXUNUSED(pos
) )
228 m_dialogStyle
= style
;
232 // dialog layout constants
233 static const int LAYOUT_X_MARGIN
= 5;
234 static const int LAYOUT_Y_MARGIN
= 5;
235 static const int MARGIN_BETWEEN_BUTTONS
= 3*LAYOUT_X_MARGIN
;
237 // calc the message size
238 // ---------------------
240 // TODO this should be factored out to a common function (also used in
243 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
247 long height
, width
, heightTextMax
= 0, widthTextMax
= 0;
248 for ( const char *pc
= message
; ; pc
++ ) {
249 if ( *pc
== '\n' || *pc
== '\0' ) {
250 dc
.GetTextExtent(curLine
, &width
, &height
);
251 if ( width
> widthTextMax
)
252 widthTextMax
= width
;
253 if ( height
> heightTextMax
)
254 heightTextMax
= height
;
271 size_t nLineCount
= lines
.Count();
272 long hTotalMsg
= heightTextMax
*nLineCount
;
274 // calc the button size
275 // --------------------
277 bool hasCancel
= FALSE
;
279 // always create the OK button - the code below supposes we do have buttons
280 // and besides the user should have some way to close this dialog
281 wxASSERT_MSG( style
& wxOK
, _T("this dialog should have OK button") );
283 wxString
labelOk(_("OK"));
285 dc
.GetTextExtent(labelOk
, &width
, NULL
);
286 if ( width
> wButton
)
289 wxString labelCancel
;
290 if ( style
& wxCANCEL
)
292 labelCancel
= _("Cancel");
293 dc
.GetTextExtent(labelCancel
, &width
, NULL
);
294 if ( width
> wButton
)
305 long hButton
= wButton
*23/75;
306 long wTotalButtons
= wButton
;
309 wTotalButtons
*= 2; // second button
310 wTotalButtons
+= MARGIN_BETWEEN_BUTTONS
; // margin between the 2
313 // listbox and stat line
314 // ---------------------
316 // make the listbox at least as tall as the message - otherwise it looks
317 // ugly (the lower limit of 300 for the width is arbitrary OTOH)
319 // NB: we write "n + 2" because the horiz. scrollbar also takes some place
320 long hListbox
= wxMax((n
+ 2) * heightTextMax
, hTotalMsg
),
321 wListbox
= wxMax(300, wxMax(wTotalButtons
, widthTextMax
));
328 // now the complete dialog size
329 // ----------------------------
331 long hDialog
= 2*LAYOUT_Y_MARGIN
+ // top margin
332 hTotalMsg
+ // message
333 2*LAYOUT_Y_MARGIN
+ // margin between text and listbox
334 hListbox
+ // listbox
336 LAYOUT_Y_MARGIN
+ // margin
337 hStatLine
+ // separator line
339 2*LAYOUT_Y_MARGIN
+ // margin between listbox and buttons
340 hButton
+ // button(s)
341 LAYOUT_Y_MARGIN
; // bottom margin
343 long wDialog
= wxMax(wTotalButtons
, widthTextMax
) +
344 4*LAYOUT_X_MARGIN
; // 2 from each side
346 // create the controls
347 // -------------------
351 int y
= 2*LAYOUT_Y_MARGIN
;
352 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ )
354 text
= new wxStaticText(this, -1, lines
[nLine
],
355 wxPoint(2*LAYOUT_X_MARGIN
, y
),
356 wxSize(widthTextMax
, heightTextMax
));
360 y
+= 2*LAYOUT_X_MARGIN
;
363 m_listbox
= new wxListBox( this, wxID_LISTBOX
,
364 wxPoint(2*LAYOUT_X_MARGIN
, y
),
365 wxSize(wListbox
, hListbox
),
372 for (int i
= 0; i
< n
; i
++)
373 m_listbox
->SetClientData(i
, clientData
[i
]);
378 (void) new wxStaticLine( this, -1,
379 wxPoint(0, y
+ LAYOUT_Y_MARGIN
),
380 wxSize(wDialog
, hStatLine
) );
382 y
+= LAYOUT_Y_MARGIN
+ hStatLine
;
387 y
+= 2*LAYOUT_X_MARGIN
;
389 // NB: create [Ok] first to get the right tab order
391 wxButton
*ok
= (wxButton
*) NULL
;
392 wxButton
*cancel
= (wxButton
*) NULL
;
394 long x
= wDialog
/ 2;
396 x
-= MARGIN_BETWEEN_BUTTONS
/ 2 + wButton
;
400 ok
= new wxButton( this, wxID_OK
, labelOk
,
402 wxSize(wButton
, hButton
) );
406 x
+= MARGIN_BETWEEN_BUTTONS
+ wButton
;
407 cancel
= new wxButton( this, wxID_CANCEL
, labelCancel
,
409 wxSize(wButton
, hButton
) );
415 SetClientSize( wDialog
, hDialog
);
423 void wxSingleChoiceDialog::SetSelection(int sel
)
425 m_listbox
->SetSelection(sel
);
429 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
431 m_selection
= m_listbox
->GetSelection();
432 m_stringSelection
= m_listbox
->GetStringSelection();
433 m_clientData
= m_listbox
->GetClientData(m_selection
);
438 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
440 m_selection
= m_listbox
->GetSelection();
441 m_stringSelection
= m_listbox
->GetStringSelection();
442 m_clientData
= m_listbox
->GetClientData(m_selection
);