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