]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/messages.cpp
* fixed a segfault
[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.3 2003/04/06 16:30:43 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 #include "wxwindows.h"
49
50 /*****************************************************************************
51  * Event Table.
52  *****************************************************************************/
53
54 /* IDs for the controls and the menu commands */
55 enum
56 {
57     Close_Event,
58     Verbose_Event
59 };
60
61 BEGIN_EVENT_TABLE(Messages, wxFrame)
62     /* Button events */
63     EVT_BUTTON(wxID_OK, Messages::OnClose)
64     EVT_CHECKBOX(Verbose_Event, Messages::OnVerbose)
65
66     /* Special events : we don't want to destroy the window when the user
67      * clicks on (X) */
68     EVT_CLOSE(Messages::OnClose)
69 END_EVENT_TABLE()
70
71 /*****************************************************************************
72  * Constructor.
73  *****************************************************************************/
74 Messages::Messages( intf_thread_t *_p_intf, Interface *_p_main_interface ):
75     wxFrame( _p_main_interface, -1, "Messages", wxDefaultPosition,
76              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
77 {
78     /* Initializations */
79     p_intf = _p_intf;
80     p_main_interface = _p_main_interface;
81     b_verbose = VLC_FALSE;
82     SetIcon( *p_intf->p_sys->p_icon );
83
84     /* Create a panel to put everything in */
85     wxPanel *messages_panel = new wxPanel( this, -1 );
86     messages_panel->SetAutoLayout( TRUE );
87
88     /* Create the textctrl and some text attributes */
89     textctrl = new wxTextCtrl( messages_panel, -1, "", wxDefaultPosition,
90         wxSize::wxSize( 400, 500 ), wxTE_MULTILINE | wxTE_READONLY |
91                                     wxTE_RICH | wxTE_NOHIDESEL );
92     info_attr = new wxTextAttr( wxColour::wxColour( 0, 128, 0 ) );
93     err_attr = new wxTextAttr( *wxRED );
94     warn_attr = new wxTextAttr( *wxBLUE );
95     dbg_attr = new wxTextAttr( *wxBLACK );
96
97     /* Create the OK button */
98     wxButton *ok_button = new wxButton( messages_panel, wxID_OK, _("OK") );
99     ok_button->SetDefault();
100
101     /* Create the Verbose checkbox */
102     wxCheckBox *verbose_checkbox =
103         new wxCheckBox( messages_panel, Verbose_Event, _("Verbose") );
104
105     /* Place everything in sizers */
106     wxBoxSizer *buttons_sizer = new wxBoxSizer( wxHORIZONTAL );
107     buttons_sizer->Add( ok_button, 0, wxEXPAND |wxALIGN_LEFT| wxALL, 5 );
108     buttons_sizer->Add( new wxPanel( this, -1 ), 1, wxALL, 5 );
109     buttons_sizer->Add( verbose_checkbox, 0, wxEXPAND |wxALIGN_RIGHT | wxALL, 5 );
110     buttons_sizer->Layout();
111     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
112     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
113     panel_sizer->Add( textctrl, 1, wxEXPAND | wxALL, 5 );
114     panel_sizer->Add( buttons_sizer, 0, wxEXPAND | wxALL, 5 );
115     panel_sizer->Layout();
116     messages_panel->SetSizerAndFit( panel_sizer );
117     main_sizer->Add( messages_panel, 1, wxGROW, 0 );
118     main_sizer->Layout();
119     SetSizerAndFit( main_sizer );
120 }
121
122 Messages::~Messages()
123 {
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     if( p_sub->i_start != i_stop )
136     {
137         for( i_start = p_sub->i_start;
138              i_start != i_stop;
139              i_start = (i_start+1) % VLC_MSG_QSIZE )
140         {
141
142             if( !b_verbose &&
143                 VLC_MSG_ERR != p_sub->p_msg[i_start].i_type &&
144                 VLC_MSG_INFO != p_sub->p_msg[i_start].i_type )
145                 continue;
146
147             /* Append all messages to log window */
148             textctrl->SetDefaultStyle( *dbg_attr );
149             (*textctrl) << p_sub->p_msg[i_start].psz_module;
150
151             switch( p_sub->p_msg[i_start].i_type )
152             {
153             case VLC_MSG_INFO:
154                 (*textctrl) << ": ";
155                 textctrl->SetDefaultStyle( *info_attr );
156                 break;
157             case VLC_MSG_ERR:
158                 (*textctrl) << " error: ";
159                 textctrl->SetDefaultStyle( *err_attr );
160                 break;
161             case VLC_MSG_WARN:
162                 (*textctrl) << " warning: ";
163                 textctrl->SetDefaultStyle( *warn_attr );
164                 break;
165             case VLC_MSG_DBG:
166             default:
167                 (*textctrl) << " debug: ";
168                 break;
169             }
170
171             /* Add message */
172             (*textctrl) << p_sub->p_msg[i_start].psz_msg << "\n";
173         }
174
175         vlc_mutex_lock( p_sub->p_lock );
176         p_sub->i_start = i_start;
177         vlc_mutex_unlock( p_sub->p_lock );
178     }
179 }
180
181 /*****************************************************************************
182  * Private methods.
183  *****************************************************************************/
184 void Messages::OnClose( wxCommandEvent& WXUNUSED(event) )
185 {
186     Hide();
187 }
188
189 void Messages::OnVerbose( wxCommandEvent& event )
190 {
191     b_verbose = event.IsChecked();
192 }