]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/interaction.cpp
* : svn:keywords
[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_id == DIALOG_ERRORS )
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_OK_CANCEL )
170     {
171         wxButton *ok = new wxButton( buttons_panel, wxID_OK );
172         wxButton *cancel = new wxButton( buttons_panel, wxID_CANCEL );
173         buttons_sizer->AddButton( ok );
174         buttons_sizer->AddButton( cancel );
175     }
176     else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
177     {
178         wxButton *yes = new wxButton( buttons_panel, wxID_YES );
179         wxButton *no = new wxButton( buttons_panel, wxID_NO );
180         wxButton *cancel = new wxButton( buttons_panel, wxID_CANCEL );
181         buttons_sizer->AddButton( yes );
182         buttons_sizer->AddButton( no );
183         buttons_sizer->AddButton( cancel );
184     }
185     else if( p_dialog->i_flags & DIALOG_CLEAR_NOSHOW )
186     {
187         wxCheckBox *noshow = new wxCheckBox( buttons_panel,
188                       NoShow_Event, wxU( _("Don't show further errors") ) );
189         noshow->SetValue( b_noshow );
190         wxButton *clear = new wxButton( buttons_panel, wxID_CLEAR );
191         wxButton *close = new wxButton( buttons_panel, wxID_CLOSE );
192         close->SetDefault();
193         buttons_sizer->Add( noshow, 0, wxEXPAND | wxRIGHT|
194                                        wxLEFT | wxALIGN_LEFT, 5 );
195         buttons_sizer->Add( 0, 0, 1 );
196         buttons_sizer->AddButton( clear );
197         buttons_sizer->SetNegativeButton( clear );
198         buttons_sizer->AddButton( close );
199         buttons_sizer->SetAffirmativeButton( close );
200     }
201     widgets_sizer->Layout();
202     widgets_panel->SetSizerAndFit( widgets_sizer );
203     buttons_sizer->Realize();
204     buttons_panel->SetSizerAndFit( buttons_sizer );
205     main_sizer->Layout();
206     SetSizerAndFit( main_sizer );
207 }
208
209 /*****************************************************************************
210  * Private methods.
211  *****************************************************************************/
212 void InteractionDialog::OnClose( wxCloseEvent& event )
213 {
214     Finish( DIALOG_CANCELLED );
215 }
216
217 void InteractionDialog::OnCancel( wxCommandEvent& event )
218 {
219     Finish( DIALOG_CANCELLED );
220 }
221
222 void InteractionDialog::OnNo( wxCommandEvent& event )
223 {
224     Finish( DIALOG_NO );
225 }
226
227 void InteractionDialog::OnOkYes( wxCommandEvent& event )
228 {
229     Finish( DIALOG_OK_YES );
230 }
231
232 void InteractionDialog::OnClear( wxCommandEvent& event )
233 {
234     int i;
235     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
236     for( i = p_dialog->i_widgets - 1 ; i >= 0 ; i-- )
237     {
238         user_widget_t *p_widget = p_dialog->pp_widgets[i];
239         FREE( p_widget->psz_text );
240         FREE( p_widget->val.psz_string );
241         REMOVE_ELEM( p_dialog->pp_widgets, p_dialog->i_widgets, i );
242         free( p_widget );
243     }
244     widgets_panel->DestroyChildren();
245     /* FIXME: Needed for the spacer */
246     buttons_sizer->Remove( 1 );buttons_sizer->Remove( 2 );
247     buttons_sizer->Remove( 3 );
248     buttons_panel->DestroyChildren();
249     input_widgets.clear();
250     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
251     Render();
252 }
253
254 void InteractionDialog::OnNoShow( wxCommandEvent& event )
255 {
256      b_noshow = event.IsChecked();
257 }
258
259 void InteractionDialog::Finish( int i_ret )
260 {
261     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
262     vector<InputWidget>::iterator it = input_widgets.begin();
263     while ( it < input_widgets.end() )
264     {
265         if( (*it).i_type == WIDGET_INPUT_TEXT )
266             (*it).val->psz_string = strdup( (*it).control->GetValue().mb_str());
267         it++;
268     }
269     Hide();
270     p_dialog->i_status = ANSWERED_DIALOG;
271     p_dialog->i_return = i_ret;
272     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
273 }
274
275 #undef FREE