]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choicdlg.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/gtk/choicdlg.h"
39 /* Split message, using constraints to position controls */
40 static wxSize
wxSplitMessage2( const wxString
&message
, wxWindow
*parent
)
44 wxString
line( _T("") );
45 for (size_t pos
= 0; pos
< message
.Len(); pos
++)
47 if (message
[pos
] == _T('\n'))
51 wxStaticText
*s1
= new wxStaticText( parent
, -1, line
, wxPoint(15,y
) );
52 wxSize
size1( s1
->GetSize() );
53 if (size1
.x
> w
) w
= size1
.x
;
66 wxStaticText
*s2
= new wxStaticText( parent
, -1, line
, wxPoint(15,y
) );
67 wxSize
size2( s2
->GetSize() );
68 if (size2
.x
> w
) w
= size2
.x
;
73 return wxSize(w
+30,y
);
77 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
78 const wxString
*choices
, wxWindow
*parent
,
79 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
80 int WXUNUSED(width
), int WXUNUSED(height
) )
82 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
83 if ( dialog
.ShowModal() == wxID_OK
)
84 return dialog
.GetStringSelection();
89 // Overloaded for backward compatibility
90 wxString
wxGetSingleChoice( const wxString
& message
, const wxString
& caption
, int n
,
91 char *choices
[], wxWindow
*parent
,
92 int x
, int y
, bool centre
,
93 int width
, int height
)
95 wxString
*strings
= new wxString
[n
];
97 for ( i
= 0; i
< n
; i
++)
99 strings
[i
] = choices
[i
];
101 wxString
ans(wxGetSingleChoice(message
, caption
, n
, (const wxString
*)strings
, parent
,
102 x
, y
, centre
, width
, height
));
107 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
108 const wxString
*choices
, wxWindow
*parent
,
109 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
110 int WXUNUSED(width
), int WXUNUSED(height
) )
112 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
);
113 if ( dialog
.ShowModal() == wxID_OK
)
114 return dialog
.GetSelection();
119 // Overloaded for backward compatibility
120 int wxGetSingleChoiceIndex( const wxString
& message
, const wxString
& caption
, int n
,
121 wxChar
*choices
[], wxWindow
*parent
,
122 int x
, int y
, bool centre
,
123 int width
, int height
)
125 wxString
*strings
= new wxString
[n
];
126 for ( int i
= 0; i
< n
; i
++)
127 strings
[i
] = choices
[i
];
128 int ans
= wxGetSingleChoiceIndex(message
, caption
, n
, (const wxString
*)strings
, parent
,
129 x
, y
, centre
, width
, height
);
134 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
135 const wxString
*choices
, char **client_data
, wxWindow
*parent
,
136 int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(centre
),
137 int WXUNUSED(width
), int WXUNUSED(height
) )
139 wxSingleChoiceDialog
dialog(parent
, message
, caption
, n
, choices
, client_data
);
140 if ( dialog
.ShowModal() == wxID_OK
)
141 return dialog
.GetSelectionClientData();
146 // Overloaded for backward compatibility
147 wxChar
*wxGetSingleChoiceData( const wxString
& message
, const wxString
& caption
, int n
,
148 wxChar
*choices
[], char **client_data
, wxWindow
*parent
,
149 int x
, int y
, bool centre
,
150 int width
, int height
)
152 wxString
*strings
= new wxString
[n
];
154 for ( i
= 0; i
< n
; i
++)
156 strings
[i
] = choices
[i
];
158 wxChar
*data
= wxGetSingleChoiceData(message
, caption
, n
, (const wxString
*)strings
, client_data
, parent
,
159 x
, y
, centre
, width
, height
);
165 /* Multiple choice dialog contributed by Robert Cowell
168 The new data passed are in the "int nsel" and "int * selection"
170 The idea is to make a multiple selection from list of strings.
171 The returned value is the total number selected. initialily there
172 are nsel selected, with indices stored in
173 selection[0],...,selection[nsel-1] which appear highlighted to
174 begin with. On exit with value i
175 selection[0..i-1] contains the indices of the selected items.
176 (Some prior selectecions might be deselected.)
177 Thus selection must be as big as choices, in case all items are
182 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
183 int n, const wxString *choices,
184 int nsel, int * selection,
185 wxWindow *parent , int x , int y, bool centre,
186 int width, int height)
192 // wxSingleChoiceDialog
194 #if !USE_SHARED_LIBRARY
195 BEGIN_EVENT_TABLE(wxSingleChoiceDialog
, wxDialog
)
196 EVT_BUTTON(wxID_OK
, wxSingleChoiceDialog::OnOK
)
197 EVT_LISTBOX_DCLICK(wxID_LISTBOX
, wxSingleChoiceDialog::OnListBoxDClick
)
200 IMPLEMENT_CLASS(wxSingleChoiceDialog
, wxDialog
)
203 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
204 int n
, const wxString
*choices
, char **clientData
, long style
, const wxPoint
& pos
):
205 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
|wxTAB_TRAVERSAL
)
207 Create(parent
, message
, caption
, n
, choices
, clientData
, style
);
210 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
211 const wxStringList
& choices
, char **clientData
, long style
, const wxPoint
& pos
):
212 wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
214 Create(parent
, message
, caption
, choices
, clientData
, style
);
217 bool wxSingleChoiceDialog::Create(wxWindow
*parent
, const wxString
& message
, const wxString
& caption
,
218 const wxStringList
& choices
, char **clientData
, long style
, const wxPoint
& pos
)
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
), const wxString
& message
,
232 const wxString
& WXUNUSED(caption
), int n
,
233 const wxString
*choices
, char **clientData
, long style
,
234 const wxPoint
& WXUNUSED(pos
) )
236 m_dialogStyle
= style
;
238 m_stringSelection
= _T("");
243 wxSize
message_size( wxSplitMessage2( message
, this ) );
245 wxButton
*ok
= (wxButton
*) NULL
;
246 wxButton
*cancel
= (wxButton
*) NULL
;
249 int y
= message_size
.y
+ 15;
251 int listbox_height
= 100;
253 wxListBox
*listBox
= new wxListBox( this, wxID_LISTBOX
, wxPoint(10, y
), wxSize(240, listbox_height
),
254 n
, choices
, wxLB_ALWAYS_SB
);
255 listBox
->SetSelection( m_selection
);
258 for (int i
= 0; i
< n
; i
++)
259 listBox
->SetClientData(i
, clientData
[i
]);
262 y
+= listbox_height
+ 35;
266 ok
= new wxButton( this, wxID_OK
, _("OK"), wxPoint(-1,y
), wxSize(80,-1) );
267 m_buttons
.Append( ok
);
270 if (style
& wxCANCEL
)
272 cancel
= new wxButton( this, wxID_CANCEL
, _("Cancel"), wxPoint(-1,y
), wxSize(80,-1) );
273 m_buttons
.Append( cancel
);
282 int w
= m_buttons
.GetCount() * 100;
283 if (message_size
.x
> w
) w
= message_size
.x
;
284 int space
= w
/ (m_buttons
.GetCount()*2);
286 listBox
->SetSize( 20, -1, w
-10, listbox_height
);
289 wxNode
*node
= m_buttons
.First();
292 wxWindow
*win
= (wxWindow
*)node
->Data();
293 int x
= (m
*2+1)*space
- 40 + 15;
300 (void) new wxStaticLine( this, -1, wxPoint(0,y
-20), wxSize(w
+30, 5) );
303 SetSize( w
+30, y
+40 );
313 void wxSingleChoiceDialog::SetSelection(int sel
)
315 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
318 listBox
->SetSelection(sel
);
323 void wxSingleChoiceDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
325 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
328 m_selection
= listBox
->GetSelection();
329 m_stringSelection
= listBox
->GetStringSelection();
330 m_clientData
= (char*)listBox
->GetClientData(m_selection
);
336 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent
& WXUNUSED(event
))
338 wxListBox
*listBox
= (wxListBox
*)FindWindow(wxID_LISTBOX
);
341 m_selection
= listBox
->GetSelection();
342 m_stringSelection
= listBox
->GetStringSelection();
343 m_clientData
= (char*)listBox
->GetClientData(m_selection
);