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