]>
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"
35 #include "wx/statline.h"
38 #include "wx/generic/choicdgg.h"
40 #define wxID_LISTBOX 3000
42 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
43 const wxString
*choices
, wxWindow
*parent
,
44 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
45 int WXUNUSED(width
), int WXUNUSED(height
) )
47 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
49 if ( dialog
.ShowModal() == wxID_OK
)
50 choice
= 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
);
80 if ( dialog
.ShowModal() == wxID_OK
)
81 choice
= dialog
.GetSelection();
88 // Overloaded for backward compatibility
89 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
90 wxChar
*choices
[], wxWindow
*parent
,
91 int x
, int y
, bool centre
,
92 int width
, int height
)
94 wxString
*strings
= new wxString
[n
];
95 for ( int i
= 0; i
< n
; i
++)
96 strings
[i
] = choices
[i
];
97 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
98 x
, y
, centre
, width
, height
);
103 void *wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
104 const wxString
*choices
, void **client_data
, 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
, (char **)client_data
);
110 if ( dialog
.ShowModal() == wxID_OK
)
111 data
= dialog
.GetSelectionClientData();
118 // Overloaded for backward compatibility
119 void *wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
120 wxChar
*choices
[], void **client_data
, wxWindow
*parent
,
121 int x
, int y
, bool centre
,
122 int width
, int height
)
124 wxString
*strings
= new wxString
[n
];
126 for ( i
= 0; i
< n
; i
++)
128 strings
[i
] = choices
[i
];
130 void *data
= wxGetSingleChoiceData(message
, caption
, n
, (const wxString
*)strings
, client_data
, parent
,
131 x
, y
, centre
, width
, height
);
137 /* Multiple choice dialog contributed by Robert Cowell
140 The new data passed are in the "int nsel" and "int * selection"
142 The idea is to make a multiple selection from list of strings.
143 The returned value is the total number selected. initialily there
144 are nsel selected, with indices stored in
145 selection[0],...,selection[nsel-1] which appear highlighted to
146 begin with. On exit with value i
147 selection[0..i-1] contains the indices of the selected items.
148 (Some prior selectecions might be deselected.)
149 Thus selection must be as big as choices, in case all items are
154 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
155 int n, const wxString *choices,
156 int nsel, int * selection,
157 wxWindow *parent , int x , int y, bool centre,
158 int width, int height)
164 // wxSingleChoiceDialog
166 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
167 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
168 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
171 IMPLEMENT_CLASS(wxSingleChoiceDialog
, wxDialog
)
173 #if defined(__WXMSW__) || defined(__WXMAC__)
174 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
178 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
185 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
186 const wxString
& message
,
187 const wxString
& caption
,
189 const wxString
*choices
,
193 : wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
,
194 wxCHOICEDLG_DIALOG_STYLE
)
196 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
199 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
200 const wxString
& message
,
201 const wxString
& caption
,
202 const wxStringList
& choices
,
206 : wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
,
207 wxCHOICEDLG_DIALOG_STYLE
)
209 Create(parent
, message
, caption
, choices
, clientData
, style
);
212 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
213 const wxString
& message
,
214 const wxString
& caption
,
215 const wxStringList
& choices
,
220 wxString
*strings
= new wxString
[choices
.Number()];
222 for ( i
= 0; i
< choices
.Number(); i
++)
224 strings
[i
] = (char *)choices
.Nth(i
)->Data();
226 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
231 bool wxSingleChoiceDialog::Create( wxWindow
*WXUNUSED(parent
),
232 const wxString
& message
,
233 const wxString
& WXUNUSED(caption
),
235 const wxString
*choices
,
238 const wxPoint
& WXUNUSED(pos
) )
242 m_dialogStyle
= style
;
246 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
249 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
252 m_listbox
= new wxListBox( this, wxID_LISTBOX
, wxDefaultPosition
, wxSize(160,100) ,
253 n
, choices
, wxLB_ALWAYS_SB
);
254 m_listbox
->SetSelection( m_selection
);
257 for (int i
= 0; i
< n
; i
++)
258 m_listbox
->SetClientData(i
, clientData
[i
]);
260 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
264 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
268 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
270 SetAutoLayout( TRUE
);
271 SetSizer( topsizer
);
273 topsizer
->SetSizeHints( this );
274 topsizer
->Fit( this );
278 m_listbox
->SetFocus();
286 void wxSingleChoiceDialog::SetSelection(int sel
)
288 m_listbox
->SetSelection(sel
);
292 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
294 m_selection
= m_listbox
->GetSelection();
295 m_stringSelection
= m_listbox
->GetStringSelection();
298 if ( m_listbox
->HasClientUntypedData() )
299 SetClientData(m_listbox
->GetClientData(m_selection
));
304 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
306 m_selection
= m_listbox
->GetSelection();
307 m_stringSelection
= m_listbox
->GetStringSelection();
311 if ( m_listbox
->HasClientUntypedData() )
312 SetClientData(m_listbox
->GetClientData(m_selection
));