Video4Linux / PVR / KFIR unified panel
[vlc.git] / modules / gui / wxwindows / v4l.cpp
1 /*****************************************************************************
2  * v4l.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: v4l.cpp,v 1.1 2003/08/19 21:16:09 adn Exp $
6  *
7  * Authors: Mohammed Adnène Trojette <adn@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44 #include <wx/notebook.h>
45 #include <wx/textctrl.h>
46 #include <wx/combobox.h>
47 #include <wx/spinctrl.h>
48 #include <wx/statline.h>
49
50 #include <vlc/intf.h>
51
52 #include "wxwindows.h"
53
54 #ifndef wxRB_SINGLE
55 #   define wxRB_SINGLE 0
56 #endif
57
58 enum
59 {
60     AUDIO_DEV=0,
61     AUDIO_CHAN,
62     SIZE,
63     NORM,
64     FREQUENCY
65 };
66
67 /*****************************************************************************
68  * Event Table.
69  *****************************************************************************/
70
71 /* IDs for the controls and the menu commands */
72 enum
73 {
74     MRL_Event,
75
76     AudioDevice_Event, ADevLocation, AudioChannel_Event,
77
78     Size_Event,
79     Norm_Event,
80     Frequency_Event
81 };
82
83 BEGIN_EVENT_TABLE(V4LDialog, wxDialog)
84     /* Button events */
85     EVT_BUTTON(wxID_OK, V4LDialog::OnOk)
86     EVT_BUTTON(wxID_CANCEL, V4LDialog::OnCancel)
87
88     /* Events generated by the audio panel */
89     EVT_CHECKBOX(AudioDevice_Event, V4LDialog::OnAudioEnable)
90     EVT_TEXT(ADevLocation, V4LDialog::OnAudioEnable)
91     EVT_SPINCTRL(AudioChannel_Event, V4LDialog::OnAudioChannel)
92
93     /* Events generated by the audio panel */
94     EVT_CHECKBOX(Size_Event, V4LDialog::OnSizeEnable)
95     EVT_COMBOBOX(Size_Event, V4LDialog::OnSize)
96     EVT_CHECKBOX(Norm_Event, V4LDialog::OnNormEnable)
97     EVT_COMBOBOX(Norm_Event, V4LDialog::OnNorm)
98     EVT_CHECKBOX(Frequency_Event, V4LDialog::OnFrequencyEnable)
99     EVT_COMBOBOX(Frequency_Event, V4LDialog::OnFrequency)
100
101 END_EVENT_TABLE()
102
103 /*****************************************************************************
104  * Constructor.
105  *****************************************************************************/
106 V4LDialog::V4LDialog( intf_thread_t *_p_intf, wxWindow* _p_parent ):
107     wxDialog( _p_parent, -1, wxU(_("Video Device Advanced Options")),
108              wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE )
109 {
110     /* Initializations */
111     p_intf = _p_intf;
112     p_parent = _p_parent;
113     SetIcon( *p_intf->p_sys->p_icon );
114
115     /* Create a panel to put everything in */
116     wxPanel *panel = new wxPanel( this, -1 );
117     panel->SetAutoLayout( TRUE );
118
119     /* Create MRL combobox */
120     wxBoxSizer *mrl_sizer_sizer = new wxBoxSizer( wxHORIZONTAL );
121     wxStaticBox *mrl_box = new wxStaticBox( panel, -1,
122                                wxU(_("Video Device MRL")) );
123     wxStaticBoxSizer *mrl_sizer = new wxStaticBoxSizer( mrl_box,
124                                                         wxHORIZONTAL );
125     wxStaticText *mrl_label = new wxStaticText( panel, -1,
126                                                 wxU(_("Destination Target:")));
127     mrl_combo = new wxComboBox( panel, MRL_Event, wxT(""),
128                                 wxPoint(20,25), wxSize(200, -1), 0, NULL );
129     mrl_combo->SetToolTip( wxU(_("You can use this field directly by typing "
130         "the full MRL you want to open.\n""Alternatively, the field will be "
131         "filled automatically when you use the controls below")) );
132
133     mrl_sizer->Add( mrl_label, 0, wxALL | wxALIGN_CENTER, 5 );
134     mrl_sizer->Add( mrl_combo, 1, wxALL | wxALIGN_CENTER, 5 );
135     mrl_sizer_sizer->Add( mrl_sizer, 1, wxEXPAND | wxALL, 5 );
136
137     /* Create the audio panel */
138     wxPanel *audio_panel = AudioPanel( panel );
139
140     /* Create the size panel */
141     wxPanel *common_panel = CommonPanel( panel );
142
143     /* Separation */
144     wxStaticLine *static_line = new wxStaticLine( panel, wxID_OK );
145
146     /* Create the buttons */
147     wxButton *ok_button = new wxButton( panel, wxID_OK, wxU(_("OK")) );
148     ok_button->SetDefault();
149     wxButton *cancel_button = new wxButton( panel, wxID_CANCEL,
150                                             wxU(_("Cancel")) );
151
152     /* Place everything in sizers */
153     wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
154     button_sizer->Add( ok_button, 0, wxALL, 5 );
155     button_sizer->Add( cancel_button, 0, wxALL, 5 );
156     button_sizer->Layout();
157     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
158     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
159     panel_sizer->Add( mrl_sizer_sizer, 0, wxEXPAND, 5 );
160     panel_sizer->Add( common_panel, 1, wxEXPAND | wxALL, 5 );
161     panel_sizer->Add( audio_panel, 1, wxEXPAND | wxALL, 5 );
162     panel_sizer->Add( static_line, 0, wxEXPAND | wxALL, 5 );
163     panel_sizer->Add( button_sizer, 0, wxALIGN_LEFT | wxALIGN_BOTTOM |
164                       wxALL, 5 );
165     panel_sizer->Layout();
166     panel->SetSizerAndFit( panel_sizer );
167     main_sizer->Add( panel, 1, wxGROW, 0 );
168     main_sizer->Layout();
169     SetSizerAndFit( main_sizer );
170 }
171
172 V4LDialog::~V4LDialog()
173 {
174 }
175
176 wxArrayString V4LDialog::GetOptions()
177 {
178    return SeparateEntries( mrl_combo->GetValue() );
179 }
180
181 /*****************************************************************************
182  * Private methods.
183  *****************************************************************************/
184 void V4LDialog::UpdateMRL()
185 {
186     wxString audio;
187     if( audio_checkbox->IsChecked() )
188         {
189             audio = wxT(":adev=")
190                     + audio_device->GetLineText(0)
191                     + wxString::Format( wxT(":audio=%d"),
192                         audio_channel->GetValue() );
193         }
194
195     wxString common;
196     {
197         if( size_checkbox->IsChecked() )
198         {
199         common += wxT(":size=")
200                 + size_combo->GetValue();
201         }
202
203         if( norm_checkbox->IsChecked() )
204         {
205             common += wxT(":norm=")
206                     + norm_combo->GetValue();
207         }
208
209         if( frequency_checkbox->IsChecked() )
210         {
211             common += wxString::Format( wxT(":frequency=%d"),
212                                         frequency->GetValue() );
213         }
214
215     }
216
217     if( !audio.IsEmpty() || !common.IsEmpty() )
218         mrl_combo->SetValue( audio + common );
219     else
220         mrl_combo->SetValue( wxT("") );
221 }
222
223 wxPanel *V4LDialog::CommonPanel( wxWindow* parent )
224 {
225     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
226                                   wxSize(200, 200) );
227
228     wxStaticBox *panel_box = new wxStaticBox( panel, -1,
229                                    wxU(_("Common Options")) );
230     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
231                                                           wxVERTICAL );
232
233     wxFlexGridSizer *subpanel_sizer;
234
235     common_subpanel = new wxPanel( panel, -1 );
236
237     subpanel_sizer = new wxFlexGridSizer( 2, 3, 20 );
238
239     static const wxString size_array[] =
240     {
241         wxT("subqcif"),
242         wxT("qsif"),
243         wxT("qcif"),
244         wxT("sif"),
245         wxT("cif"),
246         wxT("vga")
247     };
248
249     size_checkbox = new wxCheckBox( common_subpanel,
250                                     Size_Event,
251                                     wxU(_("Size")) );
252     size_combo = new wxComboBox( common_subpanel, Size_Event, wxT(""),
253                                  wxPoint(20,25), wxSize( 120, -1),
254                                  WXSIZEOF(size_array), size_array,
255                                  wxCB_READONLY );
256     size_combo->SetSelection(5);
257
258     subpanel_sizer->Add( size_checkbox, 0, wxALIGN_LEFT |
259                          wxALIGN_CENTER_VERTICAL );
260     subpanel_sizer->Add( size_combo, 0, wxALIGN_RIGHT |
261                          wxALIGN_CENTER_VERTICAL );
262
263     static const wxString norm_array[] =
264     {
265         wxT("pal"),
266         wxT("secam"),
267         wxT("ntsc")
268     };
269
270     norm_checkbox = new wxCheckBox( common_subpanel,
271                                      Norm_Event,
272                                      wxU(_("Norm")) );
273     norm_combo = new wxComboBox( common_subpanel, Norm_Event, wxT(""),
274                                  wxPoint(20,25), wxSize( 120, -1),
275                                  WXSIZEOF(norm_array), norm_array,
276                                  wxCB_READONLY );
277     norm_combo->SetSelection(1);
278
279     subpanel_sizer->Add( norm_checkbox, 0, wxALIGN_LEFT |
280                          wxALIGN_CENTER_VERTICAL );
281     subpanel_sizer->Add( norm_combo, 0, wxALIGN_RIGHT |
282                          wxALIGN_CENTER_VERTICAL );
283
284     frequency_checkbox = new wxCheckBox( common_subpanel,
285                                          Frequency_Event,
286                                          wxU(_("Frequency")) );
287     frequency = new wxSpinCtrl( common_subpanel, Frequency_Event );
288     frequency->SetToolTip( wxU(_("The frequency in KHz" )) );
289
290     subpanel_sizer->Add( frequency_checkbox, 0, wxALIGN_LEFT |
291                          wxALIGN_CENTER_VERTICAL );
292     subpanel_sizer->Add( frequency, 0, wxALIGN_RIGHT |
293                          wxALIGN_CENTER_VERTICAL );
294
295     common_subpanel->SetSizerAndFit( subpanel_sizer );
296
297     /* Stuff everything into the main panel */
298     panel_sizer->Add( common_subpanel, 1,
299                       wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 5 );
300
301     panel->SetSizerAndFit( panel_sizer );
302
303     /* Update panel */
304     size_combo->Disable();
305     norm_combo->Disable();
306     frequency->Disable();
307
308     return panel;
309 }
310
311 wxPanel *V4LDialog::AudioPanel( wxWindow* parent )
312 {
313     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
314                                   wxSize(200, 200) );
315
316     wxStaticBox *panel_box = new wxStaticBox( panel, -1,
317                                    wxU(_("Audio Options")) );
318     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
319                                                           wxVERTICAL );
320
321     wxStaticText *label;
322     wxFlexGridSizer *subpanel_sizer;
323
324     audio_subpanel = new wxPanel( panel, -1 );
325
326     subpanel_sizer = new wxFlexGridSizer( 2, 2, 20 );
327
328     audio_checkbox = new wxCheckBox( audio_subpanel,
329                                      AudioDevice_Event,
330                                      wxU(_("Audio Device")) );
331     audio_device = new wxTextCtrl( audio_subpanel, AudioDevice_Event,
332                                    wxT("/dev/dsp"),
333                                    wxDefaultPosition, wxSize( 120, -1 ),
334                                    wxTE_PROCESS_ENTER);
335     label = new wxStaticText( audio_subpanel, -1, wxU(_("Channel")) );
336     audio_channel = new wxSpinCtrl( audio_subpanel, AudioChannel_Event );
337
338     subpanel_sizer->Add( audio_checkbox, 0, wxALIGN_RIGHT |
339                                             wxALIGN_CENTER_VERTICAL );
340     subpanel_sizer->Add( audio_device, 1, wxALIGN_RIGHT );
341     subpanel_sizer->Add( label, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
342     subpanel_sizer->Add( audio_channel, 0, wxALIGN_RIGHT |
343                          wxALIGN_CENTER_VERTICAL );
344
345     audio_subpanel->SetSizerAndFit( subpanel_sizer );
346
347     /* Stuff everything into the main panel */
348     panel_sizer->Add( audio_subpanel, 1,
349                       wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 5 );
350
351     panel->SetSizerAndFit( panel_sizer );
352
353     /* Update panel */
354     audio_device->Disable();
355     audio_channel->Disable();
356
357     return panel;
358 }
359
360
361 /*****************************************************************************
362  * Events methods.
363  *****************************************************************************/
364 void V4LDialog::OnOk( wxCommandEvent& WXUNUSED(event) )
365 {
366     mrl_combo->Append( mrl_combo->GetValue() );
367     EndModal( wxID_OK );
368 }
369
370 void V4LDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
371 {
372     EndModal( wxID_CANCEL );
373 }
374
375 void V4LDialog::OnMRLChange( wxCommandEvent& event )
376 {
377     //mrl = event.GetString();
378 }
379
380 /*****************************************************************************
381  * Audio panel event methods.
382  *****************************************************************************/
383 void V4LDialog::OnAudioEnable( wxCommandEvent& event )
384 {
385     audio_device->SetValue( wxU( "/dev/dsp" ) );
386     audio_channel->SetRange( 0, 255 );
387     audio_channel->SetValue( 0 );
388
389     audio_device->Enable( event.GetInt() );
390     audio_channel->Enable( event.GetInt() );
391     UpdateMRL();
392 }
393
394 void V4LDialog::OnAudioChannel( wxCommandEvent& event )
395 {
396     UpdateMRL();
397 }
398
399 /******************************************************************************
400  * Common panel event methods.
401  ******************************************************************************/
402 void V4LDialog::OnSizeEnable( wxCommandEvent& event )
403 {
404     UpdateMRL();
405     size_combo->Enable( event.GetInt() );
406 }
407
408 void V4LDialog::OnSize( wxCommandEvent& event )
409 {
410     UpdateMRL();
411 }
412
413 void V4LDialog::OnNormEnable( wxCommandEvent& event )
414 {
415     norm_combo->Enable( event.GetInt() );
416     UpdateMRL();
417 }
418
419 void V4LDialog::OnNorm( wxCommandEvent& event )
420 {
421     UpdateMRL();
422 }
423
424 void V4LDialog::OnFrequencyEnable( wxCommandEvent& event )
425 {
426     frequency->SetRange( 0, 42000 );
427     frequency->SetValue( 7668 );
428     frequency->Enable( event.GetInt() );
429     UpdateMRL();
430 }
431
432 void V4LDialog::OnFrequency( wxCommandEvent& event )
433 {
434     UpdateMRL();
435 }
436