]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/interaction.cpp
c3af263a79d7baaa60f6393784228b389f501979
[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: bookmarks.cpp 13106 2005-11-02 19:20:34Z zorglub $
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_NO, InteractionDialog::OnNo )
50     EVT_BUTTON( wxID_CLEAR, InteractionDialog::OnClear )
51     EVT_CHECKBOX( NoShow_Event, InteractionDialog::OnNoShow )
52 END_EVENT_TABLE()
53
54 /*****************************************************************************
55  * Constructor.
56  *****************************************************************************/
57 InteractionDialog::InteractionDialog( intf_thread_t *_p_intf,
58                                       wxWindow *p_parent,
59                                       interaction_dialog_t *_p_dialog )
60   : wxDialog( p_parent, -1, wxU( _p_dialog->psz_title ) )
61 {
62     /* Initializations */
63     p_intf = _p_intf;
64     p_dialog = _p_dialog;
65     SetIcon( *p_intf->p_sys->p_icon );
66
67     widgets_panel = new wxPanel( this, -1 );
68     widgets_sizer = new wxBoxSizer( wxVERTICAL );
69     widgets_panel->SetSizer( widgets_sizer );
70
71     buttons_panel = new wxPanel( this, -1 );
72     buttons_sizer = new wxBoxSizer( wxHORIZONTAL );
73     buttons_panel->SetSizer( buttons_sizer );
74
75     main_sizer = new wxBoxSizer( wxVERTICAL );
76     main_sizer->Add( widgets_panel, 1, wxEXPAND | wxALL, 5 );
77     main_sizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND  );
78     main_sizer->Add( buttons_panel, 0, wxEXPAND | wxALL, 5 );
79     SetSizer( main_sizer );
80
81     b_noshow = false;
82     Render();
83 }
84
85 InteractionDialog::~InteractionDialog()
86 {
87 }
88
89 void InteractionDialog::Update( )
90 {
91     widgets_panel->DestroyChildren();
92     /* FIXME: Needed for the spacer */
93     buttons_sizer->Remove( 1 );buttons_sizer->Remove( 2 );buttons_sizer->Remove( 3 );
94     buttons_panel->DestroyChildren();
95     input_widgets.clear();
96     Render();
97     if( b_noshow == false )
98     {
99         Show();
100     }
101 }
102
103 /// \todo Dirty - Clean that up
104 void InteractionDialog::Render()
105 {
106     wxStaticText *label;
107     wxTextCtrl   *input;
108     wxGauge      *gauge;
109
110
111     if( p_dialog->i_id == DIALOG_ERRORS )
112     {
113         wxTextCtrl *errors ; // Special case
114         label = new wxStaticText( widgets_panel, -1,
115           wxU( _("The following errors happened. More details might be "
116                  "available in the Messages window.") ) );
117         errors = new wxTextCtrl( widgets_panel, -1, wxT(""),
118                          wxDefaultPosition, wxDefaultSize,
119                          wxTE_MULTILINE | wxTE_READONLY );
120         for( int i = 0 ; i< p_dialog->i_widgets; i++ )
121         {
122             (*errors) << wxL2U( p_dialog->pp_widgets[i]->psz_text ) <<
123                            wxU( "\n" ) ;
124         }
125         widgets_sizer->Add( label );
126         widgets_sizer->Add( errors, 0, wxEXPAND|wxALL, 3 );
127     }
128     else
129     {
130         //-------------- Widgets ------------------
131         for( int i = 0 ; i< p_dialog->i_widgets; i++ )
132         {
133             user_widget_t* p_widget = p_dialog->pp_widgets[i];
134             /// \todo Merge cleanly with preferences widgets
135             switch( p_widget->i_type )
136             {
137             case WIDGET_TEXT:
138                 label = new wxStaticText( widgets_panel, -1,
139                                            wxU( p_widget->psz_text ) );
140                 widgets_sizer->Add( label );
141                 break;
142             case WIDGET_INPUT_TEXT:
143                 label = new wxStaticText( widgets_panel, -1,
144                                            wxU( p_widget->psz_text ) );
145                 input = new wxTextCtrl( widgets_panel, -1 );
146                 widgets_sizer->Add( label , 0, 0, 0);
147                 widgets_sizer->Add( input, 0, wxEXPAND , 0 );
148
149                 InputWidget widget;
150                 widget.control = input;
151                 widget.val = &p_widget->val;
152                 widget.i_type = WIDGET_INPUT_TEXT;
153                 input_widgets.push_back( widget );
154                 break;
155             case WIDGET_PROGRESS:
156                 label = new wxStaticText(widgets_panel, -1,
157                                     wxU( p_widget->psz_text ) );
158                 gauge = new wxGauge( widgets_panel, -1, 100,
159                                      wxDefaultPosition, wxDefaultSize );
160                 widgets_sizer->Add( label , 0, 0, 0);
161                 widgets_sizer->Add( gauge, 0, wxEXPAND , 0 );
162                 gauge->SetValue( (int)(p_widget->val.f_float ) );
163             }
164         }
165     }
166
167     //-------------- Buttons ------------------
168     if( p_dialog->i_flags & DIALOG_OK_CANCEL )
169     {
170         wxButton *ok = new wxButton( buttons_panel,
171                                      wxID_OK, wxU( _("OK") ) );
172         wxButton *cancel = new wxButton( buttons_panel,
173                                          wxID_CANCEL, wxU( _("Cancel") ) );
174         buttons_sizer->Add( ok, 0, wxEXPAND | wxRIGHT| wxLEFT | wxALIGN_CENTER, 5 );
175         buttons_sizer->Add( cancel, 0, wxEXPAND | wxRIGHT| wxLEFT | wxALIGN_CENTER, 5 );
176     }
177     else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
178     {
179         wxButton *yes = new wxButton( buttons_panel,
180                                       wxID_YES, wxU( _("Yes") ) );
181         wxButton *no = new wxButton( buttons_panel,
182                                      wxID_NO, wxU( _("No") ) );
183         wxButton *cancel = new wxButton( buttons_panel,
184                                          wxID_CANCEL, wxU( _("Cancel") ) );
185         buttons_sizer->Add( yes, 0, wxEXPAND | wxRIGHT| wxLEFT |
186                                     wxALIGN_CENTER, 5 );
187         buttons_sizer->Add( no, 0, wxEXPAND | wxRIGHT| wxLEFT |
188                                    wxALIGN_CENTER, 5 );
189         buttons_sizer->Add( cancel, 0, wxEXPAND | wxRIGHT| wxLEFT |
190                                        wxALIGN_CENTER, 5 );
191     }
192     else if( p_dialog->i_flags & DIALOG_CLEAR_NOSHOW )
193     {
194         wxCheckBox *noshow = new wxCheckBox( buttons_panel,
195                                          NoShow_Event, wxU( _("Don't show") ) );
196         noshow->SetValue( b_noshow );
197         wxButton *clear = new wxButton( buttons_panel,
198                                         wxID_CLEAR, wxU( _("Clear") ) );
199         buttons_sizer->Add( noshow, 0, wxEXPAND | wxRIGHT| wxLEFT | wxALIGN_LEFT, 5 );
200         buttons_sizer->Add( 0, 0, 1 );
201         buttons_sizer->Add( clear , 0, wxEXPAND | wxRIGHT| wxLEFT | wxALIGN_RIGHT, 5 );
202
203     }
204     widgets_sizer->Layout();
205     widgets_panel->SetSizerAndFit( widgets_sizer );
206     buttons_sizer->Layout();
207     buttons_panel->SetSizerAndFit( buttons_sizer );
208     main_sizer->Layout();
209     SetSizerAndFit( main_sizer );
210 }
211
212 /*****************************************************************************
213  * Private methods.
214  *****************************************************************************/
215 void InteractionDialog::OnClose( wxCloseEvent& event )
216 {
217     Finish( DIALOG_CANCELLED );
218 }
219
220 void InteractionDialog::OnCancel( wxCommandEvent& event )
221 {
222     Finish( DIALOG_CANCELLED );
223 }
224
225 void InteractionDialog::OnNo( wxCommandEvent& event )
226 {
227     Finish( DIALOG_NO );
228 }
229
230 void InteractionDialog::OnOkYes( wxCommandEvent& event )
231 {
232     Finish( DIALOG_OK_YES );
233 }
234
235 void InteractionDialog::OnClear( wxCommandEvent& event )
236 {
237     int i;
238     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
239     for( i = p_dialog->i_widgets - 1 ; i >= 0 ; i-- )
240     {
241         user_widget_t *p_widget = p_dialog->pp_widgets[i];
242         FREE( p_widget->psz_text );
243         FREE( p_widget->val.psz_string );
244         REMOVE_ELEM( p_dialog->pp_widgets, p_dialog->i_widgets, i );
245         free( p_widget );
246     }
247     widgets_panel->DestroyChildren();
248     /* FIXME: Needed for the spacer */
249     buttons_sizer->Remove( 1 );buttons_sizer->Remove( 2 );
250     buttons_sizer->Remove( 3 );
251     buttons_panel->DestroyChildren();
252     input_widgets.clear();
253     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
254     Render();
255 }
256
257 void InteractionDialog::OnNoShow( wxCommandEvent& event )
258 {
259      b_noshow = event.IsChecked();
260 }
261
262 void InteractionDialog::Finish( int i_ret )
263 {
264     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
265     vector<InputWidget>::iterator it = input_widgets.begin();
266     while ( it < input_widgets.end() )
267     {
268         if( (*it).i_type == WIDGET_INPUT_TEXT )
269             (*it).val->psz_string = strdup( (*it).control->GetValue().mb_str());
270         it++;
271     }
272     Hide();
273     p_dialog->i_status = ANSWERED_DIALOG;
274     p_dialog->i_return = i_ret;
275     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
276 }
277
278 #undef FREE