]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/choicdlg.cpp
5aeb8dec861b4e83a47c24d40b4b177d07abb637
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/gtk/choicdlg.h"
40 static void wxSplitMessage2( const wxString
&message
, wxWindow
*parent
, wxSizer
* sizer
)
43 for (size_t pos
= 0; pos
< message
.Len(); pos
++)
45 if (message
[pos
] == _T('\n'))
49 wxStaticText
*s1
= new wxStaticText( parent
, -1, line
);
60 // remaining text behind last '\n'
63 wxStaticText
*s2
= new wxStaticText( parent
, -1, line
);
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
)
76 return dialog
.GetStringSelection();
81 // Overloaded for backward compatibility
82 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
83 char *choices
[], wxWindow
*parent
,
84 int x
, int y
, bool centre
,
85 int width
, int height
)
87 wxString
*strings
= new wxString
[n
];
89 for ( i
= 0; i
< n
; i
++)
91 strings
[i
] = choices
[i
];
93 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
94 x
, y
, centre
, width
, height
));
99 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
100 const wxString
*choices
, 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
);
105 if ( dialog
.ShowModal() == wxID_OK
)
106 return dialog
.GetSelection();
111 // Overloaded for backward compatibility
112 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
113 wxChar
*choices
[], wxWindow
*parent
,
114 int x
, int y
, bool centre
,
115 int width
, int height
)
117 wxString
*strings
= new wxString
[n
];
118 for ( int i
= 0; i
< n
; i
++)
119 strings
[i
] = choices
[i
];
120 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
121 x
, y
, centre
, width
, height
);
126 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
127 const wxString
*choices
, char **client_data
, wxWindow
*parent
,
128 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
129 int WXUNUSED(width
), int WXUNUSED(height
) )
131 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
, client_data
);
132 if ( dialog
.ShowModal() == wxID_OK
)
133 return (wxChar
*)dialog
.GetSelectionClientData();
138 // Overloaded for backward compatibility
139 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
140 wxChar
*choices
[], char **client_data
, wxWindow
*parent
,
141 int x
, int y
, bool centre
,
142 int width
, int height
)
144 wxString
*strings
= new wxString
[n
];
146 for ( i
= 0; i
< n
; i
++)
148 strings
[i
] = choices
[i
];
150 wxChar
*data
= wxGetSingleChoiceData(message
, caption
, n
, (const wxString
*)strings
, client_data
, parent
,
151 x
, y
, centre
, width
, height
);
157 /* Multiple choice dialog contributed by Robert Cowell
160 The new data passed are in the "int nsel" and "int * selection"
162 The idea is to make a multiple selection from list of strings.
163 The returned value is the total number selected. initialily there
164 are nsel selected, with indices stored in
165 selection[0],...,selection[nsel-1] which appear highlighted to
166 begin with. On exit with value i
167 selection[0..i-1] contains the indices of the selected items.
168 (Some prior selectecions might be deselected.)
169 Thus selection must be as big as choices, in case all items are
174 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
175 int n, const wxString *choices,
176 int nsel, int * selection,
177 wxWindow *parent , int x , int y, bool centre,
178 int width, int height)
184 // wxSingleChoiceDialog
186 #if !USE_SHARED_LIBRARY
187 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
188 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
189 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
192 IMPLEMENT_CLASS(wxSingleChoiceDialog
, wxDialog
)
195 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
196 int n
, const wxString
*choices
, char **clientData
, long style
, const wxPoint
& pos
):
197 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
|wxDIALOG_MODAL
|wxTAB_TRAVERSAL
)
199 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
202 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
203 const wxStringList
& choices
, char **clientData
, long style
, const wxPoint
& pos
):
204 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
206 Create(parent
, message
, caption
, choices
, clientData
, style
);
209 bool wxSingleChoiceDialog::Create(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
210 const wxStringList
& choices
, char **clientData
, long style
, const wxPoint
& pos
)
212 wxString
*strings
= new wxString
[choices
.Number()];
214 for ( i
= 0; i
< choices
.Number(); i
++)
216 strings
[i
] = (char *)choices
.Nth(i
)->Data();
218 bool ans
= Create(parent
, message
, caption
, choices
.Number(), strings
, clientData
, style
, pos
);
223 bool wxSingleChoiceDialog::Create( wxWindow
*WXUNUSED(parent
), const wxString
& message
,
224 const wxString
& WXUNUSED(caption
), int n
,
225 const wxString
*choices
, char **clientData
, long style
,
226 const wxPoint
& WXUNUSED(pos
) )
228 m_dialogStyle
= style
;
230 m_stringSelection
= _T("");
235 wxBox
*topsizer
= new wxBox( wxVERTICAL
);
238 wxBox
*textsizer
= new wxBox( wxVERTICAL
);
239 wxSplitMessage2( message
, this, textsizer
);
240 topsizer
->Add( textsizer
, 0, wxALL
, 10 );
243 wxListBox
*listBox
= new wxListBox( this, wxID_LISTBOX
, wxDefaultPosition
, wxSize(160,100) ,
244 n
, choices
, wxLB_ALWAYS_SB
);
245 listBox
->SetSelection( m_selection
);
248 for (int i
= 0; i
< n
; i
++)
249 listBox
->SetClientData(i
, clientData
[i
]);
251 topsizer
->Add( listBox
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 15 );
256 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 3 );
261 wxBox
*buttonsizer
= new wxBox( wxHORIZONTAL
);
263 wxButton
*ok
= (wxButton
*) NULL
;
266 ok
= new wxButton( this, wxID_OK
, _("OK") );
267 buttonsizer
->Add( ok
, 0, wxLEFT
|wxRIGHT
, 10 );
270 wxButton
*cancel
= (wxButton
*) NULL
;
271 if (style
& wxCANCEL
)
273 cancel
= new wxButton( this, wxID_CANCEL
, _("Cancel") );
274 buttonsizer
->Add( cancel
, 0, wxLEFT
|wxRIGHT
, 10 );
277 topsizer
->Add( buttonsizer
, 0, wxCENTRE
| wxALL
, 10 );
279 topsizer
->SetSizeHints( this );
280 topsizer
->Fit( this );
281 SetSizer( topsizer
);
282 SetAutoLayout( TRUE
);
297 void wxSingleChoiceDialog::SetSelection(int sel
)
299 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
302 listBox
->SetSelection(sel
);
307 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
309 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
312 m_selection
= listBox
->GetSelection();
313 m_stringSelection
= listBox
->GetStringSelection();
314 m_clientData
= listBox
->GetClientData(m_selection
);
320 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
322 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
325 m_selection
= listBox
->GetSelection();
326 m_stringSelection
= listBox
->GetStringSelection();
327 m_clientData
= listBox
->GetClientData(m_selection
);