]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/interaction.cpp
8caf481437dead7d564c6e0ef87e86c719c8915f
[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 #define FREE( i ) { if( i ) free( i ); i = NULL; }
32
33 /*****************************************************************************
34  * Event Table.
35  *****************************************************************************/
36
37 /* IDs for the controls and the menu commands */
38 enum
39 {
40      No_Event,
41      NoShow_Event
42 };
43
44 BEGIN_EVENT_TABLE( InteractionDialog, wxDialog)
45     EVT_CLOSE( InteractionDialog::OnClose )
46     EVT_BUTTON( wxID_OK, InteractionDialog::OnOkYes )
47     EVT_BUTTON( wxID_YES, InteractionDialog::OnOkYes )
48     EVT_BUTTON( wxID_CANCEL, InteractionDialog::OnCancel)
49     EVT_BUTTON( wxID_CLOSE, InteractionDialog::OnCancel)
50     EVT_BUTTON( wxID_NO, InteractionDialog::OnNo )
51     EVT_BUTTON( wxID_CLEAR, InteractionDialog::OnClear )
52     EVT_CHECKBOX( NoShow_Event, InteractionDialog::OnNoShow )
53 END_EVENT_TABLE()
54
55 /*****************************************************************************
56  * Constructor.
57  *****************************************************************************/
58 InteractionDialog::InteractionDialog( intf_thread_t *_p_intf,
59                                       wxWindow *p_parent,
60                                       interaction_dialog_t *_p_dialog )
61   : wxDialog( p_parent, -1, wxU( _p_dialog->psz_title ) )
62 {
63     /* Initializations */
64     p_intf = _p_intf;
65     p_dialog = _p_dialog;
66     SetIcon( *p_intf->p_sys->p_icon );
67
68     widgets_panel = new wxPanel( this, -1 );
69     widgets_sizer = new wxBoxSizer( wxVERTICAL );
70     widgets_panel->SetSizer( widgets_sizer );
71
72     buttons_panel = new wxPanel( this, -1 );
73     buttons_sizer = new wxStdDialogButtonSizer;
74     buttons_panel->SetSizer( buttons_sizer );
75
76     main_sizer = new wxBoxSizer( wxVERTICAL );
77     main_sizer->Add( widgets_panel, 1, wxEXPAND | wxALL, 5 );
78     main_sizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND  );
79     main_sizer->Add( buttons_panel, 0, wxEXPAND | wxALL, 5 );
80     SetSizer( main_sizer );
81
82     b_noshow = false;
83     Render();
84 }
85
86 InteractionDialog::~InteractionDialog()
87 {
88 }
89
90 void InteractionDialog::Update( )
91 {
92     widgets_panel->DestroyChildren();
93     /* FIXME: Needed for the spacer */
94     buttons_sizer->Remove( 1 );buttons_sizer->Remove( 2 );buttons_sizer->Remove( 3 );
95     buttons_panel->DestroyChildren();
96     input_widgets.clear();
97     Render();
98     if( b_noshow == false )
99     {
100         Show();
101     }
102 }
103
104 /// \todo Dirty - Clean that up
105 void InteractionDialog::Render()
106 {
107     wxStaticText *label;
108     wxTextCtrl   *input;
109     wxGauge      *gauge;
110
111
112     if( p_dialog->i_flags == DIALOG_BLOCKING_ERROR || p_dialog->i_flags == DIALOG_NONBLOCKING_ERROR )
113     {
114         wxTextCtrl *errors ; // Special case
115         label = new wxStaticText( widgets_panel, -1,
116           wxU( _("The following errors occurred. More details might be "
117                  "available in the Messages window.") ) );
118         errors = new wxTextCtrl( widgets_panel, -1, wxT(""),
119                          wxDefaultPosition, wxDefaultSize,
120                          wxTE_MULTILINE | wxTE_READONLY );
121         for( int i = 0 ; i< p_dialog->i_widgets; i++ )
122         {
123             (*errors) << wxL2U( p_dialog->pp_widgets[i]->psz_text ) <<
124                            wxU( "\n" ) ;
125         }
126         widgets_sizer->Add( label );
127         widgets_sizer->Add( errors, 0, wxEXPAND|wxALL, 3 );
128     }
129     else
130     {
131         //-------------- Widgets ------------------
132         for( int i = 0 ; i< p_dialog->i_widgets; i++ )
133         {
134             user_widget_t* p_widget = p_dialog->pp_widgets[i];
135             /// \todo Merge cleanly with preferences widgets
136             switch( p_widget->i_type )
137             {
138             case WIDGET_TEXT:
139                 label = new wxStaticText( widgets_panel, -1,
140                                            wxU( p_widget->psz_text ) );
141                 widgets_sizer->Add( label );
142                 break;
143             case WIDGET_INPUT_TEXT:
144                 label = new wxStaticText( widgets_panel, -1,
145                                            wxU( p_widget->psz_text ) );
146                 input = new wxTextCtrl( widgets_panel, -1 );
147                 widgets_sizer->Add( label , 0, 0, 0);
148                 widgets_sizer->Add( input, 0, wxEXPAND , 0 );
149
150                 InputWidget widget;
151                 widget.control = input;
152                 widget.val = &p_widget->val;
153                 widget.i_type = WIDGET_INPUT_TEXT;
154                 input_widgets.push_back( widget );
155                 break;
156             case WIDGET_PROGRESS:
157                 label = new wxStaticText(widgets_panel, -1,
158                                     wxU( p_widget->psz_text ) );
159                 gauge = new wxGauge( widgets_panel, -1, 100,
160                                      wxDefaultPosition, wxDefaultSize );
161                 widgets_sizer->Add( label , 0, 0, 0);
162                 widgets_sizer->Add( gauge, 0, wxEXPAND , 0 );
163                 gauge->SetValue( (int)(p_widget->val.f_float ) );
164             }
165         }
166     }
167
168     //-------------- Buttons ------------------
169     if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
170     {
171         wxButton *yes = new wxButton( buttons_panel,
172                                       wxID_YES, wxU( _("&Yes") ) );
173         wxButton *no = new wxButton( buttons_panel,
174                                      wxID_NO, wxU( _("&No") ) );
175         wxButton *cancel = new wxButton( buttons_panel,
176                                          wxID_CANCEL, wxU( _("&Cancel") ) );
177         buttons_sizer->AddButton( yes );
178         buttons_sizer->AddButton( no );
179         buttons_sizer->AddButton( cancel );
180     }
181     else if( p_dialog->i_flags & DIALOG_CLEAR_NOSHOW )
182     {
183         wxCheckBox *noshow = new wxCheckBox( buttons_panel,
184                       NoShow_Event, wxU( _("Don't show further errors") ) );
185         noshow->SetValue( b_noshow );
186         wxButton *clear = new wxButton( buttons_panel,
187                                         wxID_CLEAR, wxU( _("&Clear") ) );
188         wxButton *close = new wxButton( buttons_panel, wxID_CLOSE,
189                                          wxU( _("&Close") ) );
190         close->SetDefault();
191         buttons_sizer->Add( noshow, 0, wxEXPAND | wxRIGHT|
192                                        wxLEFT | wxALIGN_LEFT, 5 );
193         buttons_sizer->Add( 0, 0, 1 );
194         buttons_sizer->AddButton( clear );
195         buttons_sizer->SetNegativeButton( clear );
196         buttons_sizer->AddButton( close );
197         buttons_sizer->SetAffirmativeButton( close );
198     }
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         FREE( p_widget->psz_text );
238         FREE( 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());
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 }
272
273 #undef FREE