]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/interaction.cpp
Completely replace all ANSI CP dependend mb_str()
[vlc] / modules / gui / wxwidgets / dialogs / interaction.cpp
1 /*****************************************************************************
2  * interaction.cpp: wxWidgets handling of interaction dialogs
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "dialogs/interaction.hpp"
28
29 #include <wx/statline.h>
30
31 /*****************************************************************************
32  * Event Table.
33  *****************************************************************************/
34
35 /* IDs for the controls and the menu commands */
36 enum
37 {
38      No_Event,
39      NoShow_Event
40 };
41
42 BEGIN_EVENT_TABLE( InteractionDialog, wxDialog)
43     EVT_CLOSE( InteractionDialog::OnClose )
44     EVT_BUTTON( wxID_OK, InteractionDialog::OnOkYes )
45     EVT_BUTTON( wxID_YES, InteractionDialog::OnOkYes )
46     EVT_BUTTON( wxID_CANCEL, InteractionDialog::OnCancel)
47     EVT_BUTTON( wxID_CLOSE, InteractionDialog::OnCancel)
48     EVT_BUTTON( wxID_NO, InteractionDialog::OnNo )
49     EVT_BUTTON( wxID_CLEAR, InteractionDialog::OnClear )
50     EVT_CHECKBOX( NoShow_Event, InteractionDialog::OnNoShow )
51 END_EVENT_TABLE()
52
53 /*****************************************************************************
54  * Constructor.
55  *****************************************************************************/
56 InteractionDialog::InteractionDialog( intf_thread_t *_p_intf,
57                                       wxWindow *p_parent,
58                                       interaction_dialog_t *_p_dialog )
59   : wxDialog( p_parent, -1, wxU( _p_dialog->psz_title ) )
60 {
61     /* Initializations */
62     p_intf = _p_intf;
63     p_dialog = _p_dialog;
64     SetIcon( *p_intf->p_sys->p_icon );
65
66     widgets_panel = new wxPanel( this, -1 );
67     widgets_sizer = new wxBoxSizer( wxVERTICAL );
68     widgets_panel->SetSizer( widgets_sizer );
69
70     buttons_panel = new wxPanel( this, -1 );
71     buttons_sizer = new wxStdDialogButtonSizer;
72     buttons_panel->SetSizer( buttons_sizer );
73
74     main_sizer = new wxBoxSizer( wxVERTICAL );
75     main_sizer->Add( widgets_panel, 1, wxEXPAND | wxALL, 5 );
76     main_sizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND  );
77     main_sizer->Add( buttons_panel, 0, wxEXPAND | wxALL, 5 );
78     SetSizer( main_sizer );
79
80     b_noshow = false;
81     Render();
82 }
83
84 InteractionDialog::~InteractionDialog()
85 {
86 }
87
88 void InteractionDialog::Update( )
89 {
90     widgets_panel->DestroyChildren();
91     /* FIXME: Needed for the spacer */
92     buttons_sizer->Remove( 1 );buttons_sizer->Remove( 2 );buttons_sizer->Remove( 3 );
93     buttons_panel->DestroyChildren();
94     input_widgets.clear();
95     Render();
96     if( b_noshow == false )
97     {
98         Show();
99     }
100 }
101
102 /// \todo Dirty - Clean that up
103 void InteractionDialog::Render()
104 {
105     wxStaticText *label;
106     wxTextCtrl   *input;
107     wxGauge      *gauge;
108
109
110     if( p_dialog->i_flags == DIALOG_BLOCKING_ERROR || p_dialog->i_flags == DIALOG_NONBLOCKING_ERROR )
111     {
112         wxTextCtrl *errors ; // Special case
113         label = new wxStaticText( widgets_panel, -1,
114           wxU( _("The following errors occurred. More details might be "
115                  "available in the Messages window.") ) );
116         errors = new wxTextCtrl( widgets_panel, -1, wxT(""),
117                          wxDefaultPosition, wxDefaultSize,
118                          wxTE_MULTILINE | wxTE_READONLY );
119         for( int i = 0 ; i< p_dialog->i_widgets; i++ )
120         {
121             (*errors) << wxL2U( p_dialog->pp_widgets[i]->psz_text ) <<
122                            wxU( "\n" ) ;
123         }
124         widgets_sizer->Add( label );
125         widgets_sizer->Add( errors, 0, wxEXPAND|wxALL, 3 );
126     }
127     else
128     {
129         //-------------- Widgets ------------------
130         for( int i = 0 ; i< p_dialog->i_widgets; i++ )
131         {
132             user_widget_t* p_widget = p_dialog->pp_widgets[i];
133             /// \todo Merge cleanly with preferences widgets
134             switch( p_widget->i_type )
135             {
136             case WIDGET_TEXT:
137                 label = new wxStaticText( widgets_panel, -1,
138                                            wxU( p_widget->psz_text ) );
139                 widgets_sizer->Add( label );
140                 break;
141             case WIDGET_INPUT_TEXT:
142                 label = new wxStaticText( widgets_panel, -1,
143                                            wxU( p_widget->psz_text ) );
144                 input = new wxTextCtrl( widgets_panel, -1 );
145                 widgets_sizer->Add( label , 0, 0, 0);
146                 widgets_sizer->Add( input, 0, wxEXPAND , 0 );
147
148                 InputWidget widget;
149                 widget.control = input;
150                 widget.val = &p_widget->val;
151                 widget.i_type = WIDGET_INPUT_TEXT;
152                 input_widgets.push_back( widget );
153                 break;
154             case WIDGET_PROGRESS:
155                 label = new wxStaticText(widgets_panel, -1,
156                                     wxU( p_widget->psz_text ) );
157                 gauge = new wxGauge( widgets_panel, -1, 100,
158                                      wxDefaultPosition, wxDefaultSize );
159                 widgets_sizer->Add( label , 0, 0, 0);
160                 widgets_sizer->Add( gauge, 0, wxEXPAND , 0 );
161                 gauge->SetValue( (int)(p_widget->val.f_float ) );
162             }
163         }
164     }
165
166     //-------------- Buttons ------------------
167     if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
168     {
169         wxButton *yes = new wxButton( buttons_panel,
170                                       wxID_YES, wxU( _("&Yes") ) );
171         wxButton *no = new wxButton( buttons_panel,
172                                      wxID_NO, wxU( _("&No") ) );
173         wxButton *cancel = new wxButton( buttons_panel,
174                                          wxID_CANCEL, wxU( _("&Cancel") ) );
175         buttons_sizer->AddButton( yes );
176         buttons_sizer->AddButton( no );
177         buttons_sizer->AddButton( cancel );
178     }
179 #if 0
180     else if( p_dialog->i_flags & DIALOG_CLEAR_NOSHOW )
181     {
182         wxCheckBox *noshow = new wxCheckBox( buttons_panel,
183                       NoShow_Event, wxU( _("Don't show further errors") ) );
184         noshow->SetValue( b_noshow );
185         wxButton *clear = new wxButton( buttons_panel,
186                                         wxID_CLEAR, wxU( _("&Clear") ) );
187         wxButton *close = new wxButton( buttons_panel, wxID_CLOSE,
188                                          wxU( _("&Close") ) );
189         close->SetDefault();
190         buttons_sizer->Add( noshow, 0, wxEXPAND | wxRIGHT|
191                                        wxLEFT | wxALIGN_LEFT, 5 );
192         buttons_sizer->Add( 0, 0, 1 );
193         buttons_sizer->AddButton( clear );
194         buttons_sizer->SetNegativeButton( clear );
195         buttons_sizer->AddButton( close );
196         buttons_sizer->SetAffirmativeButton( close );
197     }
198 #endif
199     widgets_sizer->Layout();
200     widgets_panel->SetSizerAndFit( widgets_sizer );
201     buttons_sizer->Realize();
202     buttons_panel->SetSizerAndFit( buttons_sizer );
203     main_sizer->Layout();
204     SetSizerAndFit( main_sizer );
205 }
206
207 /*****************************************************************************
208  * Private methods.
209  *****************************************************************************/
210 void InteractionDialog::OnClose( wxCloseEvent& event )
211 {
212     Finish( DIALOG_CANCELLED );
213 }
214
215 void InteractionDialog::OnCancel( wxCommandEvent& event )
216 {
217     Finish( DIALOG_CANCELLED );
218 }
219
220 void InteractionDialog::OnNo( wxCommandEvent& event )
221 {
222     Finish( DIALOG_NO );
223 }
224
225 void InteractionDialog::OnOkYes( wxCommandEvent& event )
226 {
227     Finish( DIALOG_OK_YES );
228 }
229
230 void InteractionDialog::OnClear( wxCommandEvent& event )
231 {
232     int i;
233     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
234     for( i = p_dialog->i_widgets - 1 ; i >= 0 ; i-- )
235     {
236         user_widget_t *p_widget = p_dialog->pp_widgets[i];
237         FREENULL( p_widget->psz_text );
238         FREENULL( p_widget->val.psz_string );
239         REMOVE_ELEM( p_dialog->pp_widgets, p_dialog->i_widgets, i );
240         free( p_widget );
241     }
242     widgets_panel->DestroyChildren();
243     /* FIXME: Needed for the spacer */
244     buttons_sizer->Remove( 1 );buttons_sizer->Remove( 2 );
245     buttons_sizer->Remove( 3 );
246     buttons_panel->DestroyChildren();
247     input_widgets.clear();
248     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
249     Render();
250 }
251
252 void InteractionDialog::OnNoShow( wxCommandEvent& event )
253 {
254      b_noshow = event.IsChecked();
255 }
256
257 void InteractionDialog::Finish( int i_ret )
258 {
259     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
260     vector<InputWidget>::iterator it = input_widgets.begin();
261     while ( it < input_widgets.end() )
262     {
263         if( (*it).i_type == WIDGET_INPUT_TEXT )
264             (*it).val->psz_string = strdup( (*it).control->GetValue().mb_str(wxConvUTF8));
265         it++;
266     }
267     Hide();
268     p_dialog->i_status = ANSWERED_DIALOG;
269     p_dialog->i_return = i_ret;
270     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
271 }