interaction.cpp 9.61 KB
Newer Older
zorglub's avatar
zorglub committed
1
2
3
4
/*****************************************************************************
 * interaction.cpp: wxWidgets handling of interaction dialogs
 *****************************************************************************
 * Copyright (C) 2000-2004 the VideoLAN team
Christophe Mutricy's avatar
Christophe Mutricy committed
5
 * $Id$
zorglub's avatar
zorglub committed
6
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
zorglub's avatar
zorglub committed
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
zorglub's avatar
zorglub committed
22
23
24
25
26
27
28
29
30
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "dialogs/interaction.hpp"

#include <wx/statline.h>

31
32
#define FREE( i ) { if( i ) free( i ); i = NULL; }

zorglub's avatar
zorglub committed
33
34
35
36
37
38
39
40
/*****************************************************************************
 * Event Table.
 *****************************************************************************/

/* IDs for the controls and the menu commands */
enum
{
     No_Event,
41
     NoShow_Event
zorglub's avatar
zorglub committed
42
43
};

zorglub's avatar
zorglub committed
44
BEGIN_EVENT_TABLE( InteractionDialog, wxDialog)
zorglub's avatar
zorglub committed
45
    EVT_CLOSE( InteractionDialog::OnClose )
zorglub's avatar
zorglub committed
46
47
48
    EVT_BUTTON( wxID_OK, InteractionDialog::OnOkYes )
    EVT_BUTTON( wxID_YES, InteractionDialog::OnOkYes )
    EVT_BUTTON( wxID_CANCEL, InteractionDialog::OnCancel)
49
    EVT_BUTTON( wxID_CLOSE, InteractionDialog::OnCancel)
zorglub's avatar
Fix no    
zorglub committed
50
    EVT_BUTTON( wxID_NO, InteractionDialog::OnNo )
zorglub's avatar
zorglub committed
51
    EVT_BUTTON( wxID_CLEAR, InteractionDialog::OnClear )
52
    EVT_CHECKBOX( NoShow_Event, InteractionDialog::OnNoShow )
zorglub's avatar
zorglub committed
53
54
55
56
57
58
59
60
END_EVENT_TABLE()

/*****************************************************************************
 * Constructor.
 *****************************************************************************/
InteractionDialog::InteractionDialog( intf_thread_t *_p_intf,
                                      wxWindow *p_parent,
                                      interaction_dialog_t *_p_dialog )
zorglub's avatar
zorglub committed
61
  : wxDialog( p_parent, -1, wxU( _p_dialog->psz_title ) )
zorglub's avatar
zorglub committed
62
63
64
65
66
67
68
69
70
71
72
{
    /* Initializations */
    p_intf = _p_intf;
    p_dialog = _p_dialog;
    SetIcon( *p_intf->p_sys->p_icon );

    widgets_panel = new wxPanel( this, -1 );
    widgets_sizer = new wxBoxSizer( wxVERTICAL );
    widgets_panel->SetSizer( widgets_sizer );

    buttons_panel = new wxPanel( this, -1 );
73
    buttons_sizer = new wxStdDialogButtonSizer;
zorglub's avatar
zorglub committed
74
75
76
    buttons_panel->SetSizer( buttons_sizer );

    main_sizer = new wxBoxSizer( wxVERTICAL );
77
    main_sizer->Add( widgets_panel, 1, wxEXPAND | wxALL, 5 );
zorglub's avatar
zorglub committed
78
79
80
81
    main_sizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND  );
    main_sizer->Add( buttons_panel, 0, wxEXPAND | wxALL, 5 );
    SetSizer( main_sizer );

82
    b_noshow = false;
zorglub's avatar
zorglub committed
83
84
85
86
87
88
89
90
91
92
    Render();
}

InteractionDialog::~InteractionDialog()
{
}

void InteractionDialog::Update( )
{
    widgets_panel->DestroyChildren();
93
94
    /* FIXME: Needed for the spacer */
    buttons_sizer->Remove( 1 );buttons_sizer->Remove( 2 );buttons_sizer->Remove( 3 );
zorglub's avatar
zorglub committed
95
96
97
    buttons_panel->DestroyChildren();
    input_widgets.clear();
    Render();
98
99
100
101
    if( b_noshow == false )
    {
        Show();
    }
zorglub's avatar
zorglub committed
102
103
104
105
106
107
}

/// \todo Dirty - Clean that up
void InteractionDialog::Render()
{
    wxStaticText *label;
108
109
    wxTextCtrl   *input;
    wxGauge      *gauge;
zorglub's avatar
zorglub committed
110

111
112

    if( p_dialog->i_id == DIALOG_ERRORS )
zorglub's avatar
zorglub committed
113
    {
114
115
        wxTextCtrl *errors ; // Special case
        label = new wxStaticText( widgets_panel, -1,
116
          wxU( _("The following errors occurred. More details might be "
117
                 "available in the Messages window.") ) );
118
119
120
121
        errors = new wxTextCtrl( widgets_panel, -1, wxT(""),
                         wxDefaultPosition, wxDefaultSize,
                         wxTE_MULTILINE | wxTE_READONLY );
        for( int i = 0 ; i< p_dialog->i_widgets; i++ )
zorglub's avatar
zorglub committed
122
        {
123
124
            (*errors) << wxL2U( p_dialog->pp_widgets[i]->psz_text ) <<
                           wxU( "\n" ) ;
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
        }
        widgets_sizer->Add( label );
        widgets_sizer->Add( errors, 0, wxEXPAND|wxALL, 3 );
    }
    else
    {
        //-------------- Widgets ------------------
        for( int i = 0 ; i< p_dialog->i_widgets; i++ )
        {
            user_widget_t* p_widget = p_dialog->pp_widgets[i];
            /// \todo Merge cleanly with preferences widgets
            switch( p_widget->i_type )
            {
            case WIDGET_TEXT:
                label = new wxStaticText( widgets_panel, -1,
                                           wxU( p_widget->psz_text ) );
                widgets_sizer->Add( label );
                break;
            case WIDGET_INPUT_TEXT:
                label = new wxStaticText( widgets_panel, -1,
                                           wxU( p_widget->psz_text ) );
                input = new wxTextCtrl( widgets_panel, -1 );
                widgets_sizer->Add( label , 0, 0, 0);
                widgets_sizer->Add( input, 0, wxEXPAND , 0 );

                InputWidget widget;
                widget.control = input;
                widget.val = &p_widget->val;
                widget.i_type = WIDGET_INPUT_TEXT;
                input_widgets.push_back( widget );
155
                break;
156
157
158
159
160
161
162
163
            case WIDGET_PROGRESS:
                label = new wxStaticText(widgets_panel, -1,
                                    wxU( p_widget->psz_text ) );
                gauge = new wxGauge( widgets_panel, -1, 100,
                                     wxDefaultPosition, wxDefaultSize );
                widgets_sizer->Add( label , 0, 0, 0);
                widgets_sizer->Add( gauge, 0, wxEXPAND , 0 );
                gauge->SetValue( (int)(p_widget->val.f_float ) );
164
            }
zorglub's avatar
zorglub committed
165
166
167
168
169
170
        }
    }

    //-------------- Buttons ------------------
    if( p_dialog->i_flags & DIALOG_OK_CANCEL )
    {
171
172
        wxButton *ok = new wxButton( buttons_panel, wxID_OK );
        wxButton *cancel = new wxButton( buttons_panel, wxID_CANCEL );
173
174
        buttons_sizer->AddButton( ok );
        buttons_sizer->AddButton( cancel );
175
176
177
    }
    else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
    {
178
179
180
        wxButton *yes = new wxButton( buttons_panel, wxID_YES );
        wxButton *no = new wxButton( buttons_panel, wxID_NO );
        wxButton *cancel = new wxButton( buttons_panel, wxID_CANCEL );
181
182
183
        buttons_sizer->AddButton( yes );
        buttons_sizer->AddButton( no );
        buttons_sizer->AddButton( cancel );
zorglub's avatar
zorglub committed
184
    }
185
186
187
    else if( p_dialog->i_flags & DIALOG_CLEAR_NOSHOW )
    {
        wxCheckBox *noshow = new wxCheckBox( buttons_panel,
188
                      NoShow_Event, wxU( _("Don't show further errors") ) );
189
        noshow->SetValue( b_noshow );
190
191
        wxButton *clear = new wxButton( buttons_panel, wxID_CLEAR );
        wxButton *close = new wxButton( buttons_panel, wxID_CLOSE );
192
        close->SetDefault();
193
194
        buttons_sizer->Add( noshow, 0, wxEXPAND | wxRIGHT|
                                       wxLEFT | wxALIGN_LEFT, 5 );
195
        buttons_sizer->Add( 0, 0, 1 );
196
197
198
199
        buttons_sizer->AddButton( clear );
        buttons_sizer->SetNegativeButton( clear );
        buttons_sizer->AddButton( close );
        buttons_sizer->SetAffirmativeButton( close );
200
201
202
    }
    widgets_sizer->Layout();
    widgets_panel->SetSizerAndFit( widgets_sizer );
203
    buttons_sizer->Realize();
204
205
206
    buttons_panel->SetSizerAndFit( buttons_sizer );
    main_sizer->Layout();
    SetSizerAndFit( main_sizer );
zorglub's avatar
zorglub committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
}

/*****************************************************************************
 * Private methods.
 *****************************************************************************/
void InteractionDialog::OnClose( wxCloseEvent& event )
{
    Finish( DIALOG_CANCELLED );
}

void InteractionDialog::OnCancel( wxCommandEvent& event )
{
    Finish( DIALOG_CANCELLED );
}

void InteractionDialog::OnNo( wxCommandEvent& event )
{
    Finish( DIALOG_NO );
}

void InteractionDialog::OnOkYes( wxCommandEvent& event )
{
    Finish( DIALOG_OK_YES );
}

232
233
234
235
236
237
238
239
240
241
242
243
244
245
void InteractionDialog::OnClear( wxCommandEvent& event )
{
    int i;
    vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
    for( i = p_dialog->i_widgets - 1 ; i >= 0 ; i-- )
    {
        user_widget_t *p_widget = p_dialog->pp_widgets[i];
        FREE( p_widget->psz_text );
        FREE( p_widget->val.psz_string );
        REMOVE_ELEM( p_dialog->pp_widgets, p_dialog->i_widgets, i );
        free( p_widget );
    }
    widgets_panel->DestroyChildren();
    /* FIXME: Needed for the spacer */
zorglub's avatar
zorglub committed
246
247
    buttons_sizer->Remove( 1 );buttons_sizer->Remove( 2 );
    buttons_sizer->Remove( 3 );
248
249
250
251
252
253
254
255
256
257
258
    buttons_panel->DestroyChildren();
    input_widgets.clear();
    vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
    Render();
}

void InteractionDialog::OnNoShow( wxCommandEvent& event )
{
     b_noshow = event.IsChecked();
}

zorglub's avatar
zorglub committed
259
260
void InteractionDialog::Finish( int i_ret )
{
261
    vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
zorglub's avatar
zorglub committed
262
263
264
    vector<InputWidget>::iterator it = input_widgets.begin();
    while ( it < input_widgets.end() )
    {
zorglub's avatar
zorglub committed
265
        if( (*it).i_type == WIDGET_INPUT_TEXT )
zorglub's avatar
zorglub committed
266
            (*it).val->psz_string = strdup( (*it).control->GetValue().mb_str());
zorglub's avatar
zorglub committed
267
268
269
270
271
272
273
        it++;
    }
    Hide();
    p_dialog->i_status = ANSWERED_DIALOG;
    p_dialog->i_return = i_ret;
    vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
}
274
275

#undef FREE