]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/messages.cpp
f9b5b9d59ce3afb506d7dbd61523e6abefa7fbf9
[vlc] / modules / gui / wxwindows / messages.cpp
1 /*****************************************************************************
2  * playlist.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: messages.cpp,v 1.12 2003/07/12 13:33:10 gbazin Exp $
6  *
7  * Authors: Olivier Teulière <ipkiss@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/textctrl.h>
45
46 #include <vlc/intf.h>
47
48 #if defined MODULE_NAME_IS_skins
49 #   include "../skins/src/skin_common.h"
50 #endif
51
52 #include "wxwindows.h"
53
54 /*****************************************************************************
55  * Event Table.
56  *****************************************************************************/
57
58 /* IDs for the controls and the menu commands */
59 enum
60 {
61     Close_Event,
62     Verbose_Event,
63     Clear_Event,
64     Save_Log_Event
65 };
66
67 BEGIN_EVENT_TABLE(Messages, wxFrame)
68     /* Button events */
69     EVT_BUTTON(wxID_OK, Messages::OnClose)
70     EVT_CHECKBOX(Verbose_Event, Messages::OnVerbose)
71     EVT_BUTTON(wxID_CLEAR, Messages::OnClear)
72     EVT_BUTTON(wxID_SAVEAS, Messages::OnSaveLog)
73
74     /* Special events : we don't want to destroy the window when the user
75      * clicks on (X) */
76     EVT_CLOSE(Messages::OnClose)
77 END_EVENT_TABLE()
78
79 /*****************************************************************************
80  * Constructor.
81  *****************************************************************************/
82 Messages::Messages( intf_thread_t *_p_intf, wxWindow *p_parent ):
83     wxFrame( p_parent, -1, wxU(_("Messages")), wxDefaultPosition,
84              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
85 {
86     /* Initializations */
87     p_intf = _p_intf;
88     SetIcon( *p_intf->p_sys->p_icon );
89     save_log_dialog = NULL;
90
91     /* Create a panel to put everything in */
92     wxPanel *messages_panel = new wxPanel( this, -1 );
93     messages_panel->SetAutoLayout( TRUE );
94
95     /* Create the textctrl and some text attributes */
96     textctrl = new wxTextCtrl( messages_panel, -1, wxT(""), wxDefaultPosition,
97         wxSize::wxSize( 400, 500 ), wxTE_MULTILINE | wxTE_READONLY |
98                                     wxTE_RICH | wxTE_NOHIDESEL );
99     info_attr = new wxTextAttr( wxColour::wxColour( 0, 128, 0 ) );
100     err_attr = new wxTextAttr( *wxRED );
101     warn_attr = new wxTextAttr( *wxBLUE );
102     dbg_attr = new wxTextAttr( *wxBLACK );
103
104     /* Create the OK button */
105     wxButton *ok_button = new wxButton( messages_panel, wxID_OK,
106                                         wxU(_("Close")));
107     ok_button->SetDefault();
108
109     /* Create the Clear button */
110     wxButton *clear_button = new wxButton( messages_panel, wxID_CLEAR,
111                                            wxU(_("Clear")));
112     clear_button->SetDefault();
113
114     /* Create the Save Log button */
115      wxButton *save_log_button = new wxButton( messages_panel, wxID_SAVEAS,
116                                                wxU(_("Save As...")));
117      save_log_button->SetDefault();
118
119     /* Create the Verbose checkbox */
120     wxCheckBox *verbose_checkbox =
121         new wxCheckBox( messages_panel, Verbose_Event, wxU(_("Verbose")) );
122     b_verbose = p_intf->p_libvlc->i_verbose > 0;
123     verbose_checkbox->SetValue( b_verbose );
124
125     /* Place everything in sizers */
126     wxBoxSizer *buttons_sizer = new wxBoxSizer( wxHORIZONTAL );
127     buttons_sizer->Add( ok_button, 0, wxEXPAND |wxALIGN_LEFT| wxALL, 5 );
128     buttons_sizer->Add( clear_button, 0, wxEXPAND |wxALIGN_LEFT| wxALL, 5 );
129     buttons_sizer->Add( save_log_button, 0, wxEXPAND |wxALIGN_LEFT| wxALL, 5 );
130     buttons_sizer->Add( new wxPanel( this, -1 ), 1, wxALL, 5 );
131     buttons_sizer->Add( verbose_checkbox, 0, wxEXPAND|wxALIGN_RIGHT|wxALL, 5 );
132     buttons_sizer->Layout();
133     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
134     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
135     panel_sizer->Add( textctrl, 1, wxEXPAND | wxALL, 5 );
136     panel_sizer->Add( buttons_sizer, 0, wxEXPAND | wxALL, 5 );
137     panel_sizer->Layout();
138     messages_panel->SetSizerAndFit( panel_sizer );
139     main_sizer->Add( messages_panel, 1, wxGROW, 0 );
140     main_sizer->Layout();
141     SetSizerAndFit( main_sizer );
142 }
143
144 Messages::~Messages()
145 {
146 /* Clean up */
147     if( save_log_dialog ) delete save_log_dialog;
148 }
149
150 void Messages::UpdateLog()
151 {
152     msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
153     int i_start;
154
155     vlc_mutex_lock( p_sub->p_lock );
156     int i_stop = *p_sub->pi_stop;
157     vlc_mutex_unlock( p_sub->p_lock );
158
159     if( p_sub->i_start != i_stop )
160     {
161         for( i_start = p_sub->i_start;
162              i_start != i_stop;
163              i_start = (i_start+1) % VLC_MSG_QSIZE )
164         {
165
166             if( !b_verbose &&
167                 VLC_MSG_ERR != p_sub->p_msg[i_start].i_type &&
168                 VLC_MSG_INFO != p_sub->p_msg[i_start].i_type )
169                 continue;
170
171             /* Append all messages to log window */
172             textctrl->SetDefaultStyle( *dbg_attr );
173             (*textctrl) << wxU(p_sub->p_msg[i_start].psz_module);
174
175             switch( p_sub->p_msg[i_start].i_type )
176             {
177             case VLC_MSG_INFO:
178                 (*textctrl) << wxT(": ");
179                 textctrl->SetDefaultStyle( *info_attr );
180                 break;
181             case VLC_MSG_ERR:
182                 (*textctrl) << wxT(" error: ");
183                 textctrl->SetDefaultStyle( *err_attr );
184                 break;
185             case VLC_MSG_WARN:
186                 (*textctrl) << wxT(" warning: ");
187                 textctrl->SetDefaultStyle( *warn_attr );
188                 break;
189             case VLC_MSG_DBG:
190             default:
191                 (*textctrl) << wxT(" debug: ");
192                 break;
193             }
194
195             /* Add message */
196             (*textctrl) << wxU(p_sub->p_msg[i_start].psz_msg) << wxT("\n");
197         }
198
199         vlc_mutex_lock( p_sub->p_lock );
200         p_sub->i_start = i_start;
201         vlc_mutex_unlock( p_sub->p_lock );
202     }
203 }
204
205 /*****************************************************************************
206  * Private methods.
207  *****************************************************************************/
208 void Messages::OnClose( wxCommandEvent& WXUNUSED(event) )
209 {
210     Hide();
211 }
212
213 void Messages::OnClear( wxCommandEvent& WXUNUSED(event) )
214 {
215     textctrl->Clear();
216 }
217
218 void Messages::OnSaveLog( wxCommandEvent& WXUNUSED(event) )
219 {
220     if( save_log_dialog == NULL )
221         save_log_dialog = new wxFileDialog( this,
222             wxU(_("Save Messages As a file...")),
223             wxT(""), wxT("messages"), wxT("*"), wxSAVE | wxOVERWRITE_PROMPT );
224     
225     if( save_log_dialog && save_log_dialog->ShowModal() == wxID_OK )
226     {
227         if( !textctrl->SaveFile( save_log_dialog->GetPath() ) )
228         {
229             // [FIX ME] should print an error message
230         }
231     }
232 }
233
234 void Messages::OnVerbose( wxCommandEvent& event )
235 {
236     b_verbose = event.IsChecked();
237 }