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