]>
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
);
48 if ( dialog
.ShowModal() == wxID_OK
)
49 return dialog
.GetStringSelection();
54 // Overloaded for backward compatibility
55 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
56 char *choices
[], wxWindow
*parent
,
57 int x
, int y
, bool centre
,
58 int width
, int height
)
60 wxString
*strings
= new wxString
[n
];
62 for ( i
= 0; i
< n
; i
++)
64 strings
[i
] = choices
[i
];
66 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
67 x
, y
, centre
, width
, height
));
72 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
73 const wxString
*choices
, wxWindow
*parent
,
74 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
75 int WXUNUSED(width
), int WXUNUSED(height
) )
77 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
78 if ( dialog
.ShowModal() == wxID_OK
)
79 return dialog
.GetSelection();
84 // Overloaded for backward compatibility
85 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
86 wxChar
*choices
[], wxWindow
*parent
,
87 int x
, int y
, bool centre
,
88 int width
, int height
)
90 wxString
*strings
= new wxString
[n
];
91 for ( int i
= 0; i
< n
; i
++)
92 strings
[i
] = choices
[i
];
93 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
94 x
, y
, centre
, width
, height
);
99 void *wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
100 const wxString
*choices
, void **client_data
, wxWindow
*parent
,
101 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
102 int WXUNUSED(width
), int WXUNUSED(height
) )
104 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
, (char **)client_data
);
105 if ( dialog
.ShowModal() == wxID_OK
)
106 return dialog
.GetSelectionClientData();
111 // Overloaded for backward compatibility
112 void *wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
113 wxChar
*choices
[], void **client_data
, wxWindow
*parent
,
114 int x
, int y
, bool centre
,
115 int width
, int height
)
117 wxString
*strings
= new wxString
[n
];
119 for ( i
= 0; i
< n
; i
++)
121 strings
[i
] = choices
[i
];
123 void *data
= wxGetSingleChoiceData(message
, caption
, n
, (const wxString
*)strings
, client_data
, parent
,
124 x
, y
, centre
, width
, height
);
130 /* Multiple choice dialog contributed by Robert Cowell
133 The new data passed are in the "int nsel" and "int * selection"
135 The idea is to make a multiple selection from list of strings.
136 The returned value is the total number selected. initialily there
137 are nsel selected, with indices stored in
138 selection[0],...,selection[nsel-1] which appear highlighted to
139 begin with. On exit with value i
140 selection[0..i-1] contains the indices of the selected items.
141 (Some prior selectecions might be deselected.)
142 Thus selection must be as big as choices, in case all items are
147 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
148 int n, const wxString *choices,
149 int nsel, int * selection,
150 wxWindow *parent , int x , int y, bool centre,
151 int width, int height)
157 // wxSingleChoiceDialog
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
)
166 #if defined(__WXMSW__) || defined(__WXMAC__)
167 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
171 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
178 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
179 const wxString
& message
,
180 const wxString
& caption
,
182 const wxString
*choices
,
186 : wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
,
187 wxCHOICEDLG_DIALOG_STYLE
)
189 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
192 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
,
193 const wxString
& message
,
194 const wxString
& caption
,
195 const wxStringList
& choices
,
199 : wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
,
200 wxCHOICEDLG_DIALOG_STYLE
)
202 Create(parent
, message
, caption
, choices
, clientData
, style
);
205 bool wxSingleChoiceDialog::Create(wxWindow
*parent
,
206 const wxString
& message
,
207 const wxString
& caption
,
208 const wxStringList
& choices
,
213 wxString
*strings
= new wxString
[choices
.Number()];
215 for ( i
= 0; i
< choices
.Number(); i
++)
217 strings
[i
] = (char *)choices
.Nth(i
)->Data();
219 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
224 bool wxSingleChoiceDialog::Create( wxWindow
*WXUNUSED(parent
),
225 const wxString
& message
,
226 const wxString
& WXUNUSED(caption
),
228 const wxString
*choices
,
231 const wxPoint
& WXUNUSED(pos
) )
235 m_dialogStyle
= style
;
239 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
242 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
245 m_listbox
= new wxListBox( this, wxID_LISTBOX
, wxDefaultPosition
, wxSize(160,100) ,
246 n
, choices
, wxLB_ALWAYS_SB
);
247 m_listbox
->SetSelection( m_selection
);
250 for (int i
= 0; i
< n
; i
++)
251 m_listbox
->SetClientData(i
, clientData
[i
]);
253 topsizer
->Add( m_listbox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
257 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
261 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
263 SetAutoLayout( TRUE
);
264 SetSizer( topsizer
);
266 topsizer
->SetSizeHints( this );
267 topsizer
->Fit( this );
271 m_listbox
->SetFocus();
279 void wxSingleChoiceDialog::SetSelection(int sel
)
281 m_listbox
->SetSelection(sel
);
285 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
287 m_selection
= m_listbox
->GetSelection();
288 m_stringSelection
= m_listbox
->GetStringSelection();
291 if ( m_listbox
->HasClientUntypedData() )
292 SetClientData(m_listbox
->GetClientData(m_selection
));
297 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
299 m_selection
= m_listbox
->GetSelection();
300 m_stringSelection
= m_listbox
->GetStringSelection();
304 if ( m_listbox
->HasClientUntypedData() )
305 SetClientData(m_listbox
->GetClientData(m_selection
));