]> git.sesse.net Git - vlc/blob - modules/gui/wince/messages.cpp
* modules/gui/wince: some more code cleanup + fixes.
[vlc] / modules / gui / wince / messages.cpp
1 /*****************************************************************************
2  * messages.cpp : WinCE gui plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Marodon Cedric <cedric_marodon@yahoo.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33
34 #include "wince.h"
35
36 #include <winuser.h>
37 #include <windows.h>
38 #include <windowsx.h>
39 #include <commctrl.h>
40 #include <commdlg.h>
41
42 #ifndef NMAXFILE
43 #define NMAXFILE 512 // at least 256
44 #endif
45
46 #ifndef TEXTMAXBUF
47 #define TEXTMAXBUF 512 // at least 500
48 #endif
49
50 /*****************************************************************************
51  * Constructor.
52  *****************************************************************************/
53
54 Messages::Messages( intf_thread_t *_p_intf, HINSTANCE _hInst )
55 {
56     /* Initializations */
57     p_intf = _p_intf;
58     hInst = _hInst;
59     hListView = NULL;
60     b_verbose = VLC_FALSE;
61 }
62
63 /***********************************************************************
64
65 FUNCTION: 
66   WndProc
67
68 PURPOSE: 
69   Processes messages sent to the main window.
70
71 ***********************************************************************/
72 LRESULT Messages::WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
73 {
74     SHINITDLGINFO shidi;
75
76     TCHAR psz_text[TEXTMAXBUF];
77     OPENFILENAME ofn;
78     int i_dummy;
79     HANDLE fichier;
80
81     switch( msg )
82     {
83     case WM_INITDIALOG: 
84         shidi.dwMask = SHIDIM_FLAGS;
85         shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN |
86             SHIDIF_FULLSCREENNOMENUBAR;//SHIDIF_SIZEDLGFULLSCREEN;
87         shidi.hDlg = hwnd;
88         SHInitDialog( &shidi );
89
90         RECT rect; 
91         GetClientRect( hwnd, &rect );
92         hListView = CreateWindow( WC_LISTVIEW, NULL,
93                                   WS_VISIBLE | WS_CHILD | LVS_REPORT |
94                                   LVS_SHOWSELALWAYS | WS_VSCROLL | WS_HSCROLL |
95                                   WS_BORDER /*| LVS_NOCOLUMNHEADER */,
96                                   rect.left + 20, rect.top + 50, 
97                                   rect.right - rect.left - ( 2 * 20 ), 
98                                   rect.bottom - rect.top - 50 - 20, 
99                                   hwnd, NULL, hInst, NULL );            
100         ListView_SetExtendedListViewStyle( hListView, LVS_EX_FULLROWSELECT );
101
102         LVCOLUMN lv;
103         lv.mask = LVCF_WIDTH | LVCF_FMT | LVCF_TEXT;
104         lv.fmt = LVCFMT_LEFT ;
105         GetClientRect( hwnd, &rect );
106         lv.cx = rect.right - rect.left;
107         lv.pszText = _T("Messages");
108         lv.cchTextMax = 9;
109         ListView_InsertColumn( hListView, 0, &lv);
110
111         SetTimer( hwnd, 1, 500 /*milliseconds*/, NULL );
112
113         SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
114         break;
115
116     case WM_TIMER:
117         UpdateLog();
118         break;
119
120     case WM_CLOSE:
121         EndDialog( hwnd, LOWORD( wp ) );
122         break;
123
124     case WM_COMMAND:
125         switch( LOWORD(wp) )
126         {
127         case IDOK:
128             EndDialog( hwnd, LOWORD( wp ) );
129             break;
130
131         case IDCLEAR:
132             ListView_DeleteAllItems( hListView );
133             break;
134
135         case IDSAVEAS:  
136             memset( &(ofn), 0, sizeof(ofn) );
137             ofn.lStructSize = sizeof(ofn);
138             ofn.hwndOwner = hwnd;
139             ofn.lpstrFile = _T("");
140             ofn.nMaxFile = NMAXFILE;    
141             ofn.lpstrFilter = _T("Text (*.txt)\0*.txt\0");
142             ofn.lpstrTitle = _T("Save File As");
143             ofn.Flags = OFN_HIDEREADONLY; 
144             ofn.lpstrDefExt = _T("txt");
145
146             if( GetSaveFileName( (LPOPENFILENAME)&ofn ) )
147             {
148                 fichier = CreateFile( ofn.lpstrFile, GENERIC_WRITE,
149                                       FILE_SHARE_READ|FILE_SHARE_WRITE,
150                                       NULL, CREATE_ALWAYS,
151                                       FILE_ATTRIBUTE_NORMAL, NULL );
152
153                 if( fichier != INVALID_HANDLE_VALUE )
154                 {
155                     int n;
156
157                     //SetFilePointer( fichier, 0, NULL, FILE_END );
158                     for( n = 0; n < ListView_GetItemCount( hListView ); n++ )
159                     {
160                         ListView_GetItemText( hListView, n, 0, psz_text,
161                                               TEXTMAXBUF );
162                         string text_out = (string)_TOMB(psz_text) + "\n";
163                         WriteFile( fichier, text_out.c_str(), text_out.size(),
164                                    (LPDWORD)&i_dummy, NULL );
165                     }
166                     FlushFileBuffers( fichier );
167                     CloseHandle(fichier);
168                 }
169             }
170
171             SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
172             break;
173
174         default:
175             break;
176         }
177
178     default:
179         break;
180     }
181
182     return FALSE;
183 }
184
185 void Messages::UpdateLog()
186 {
187     msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
188     string debug;
189     int i_start, i_stop;
190
191     vlc_mutex_lock( p_sub->p_lock );
192     i_stop = *p_sub->pi_stop;
193     vlc_mutex_unlock( p_sub->p_lock );
194
195     if( p_sub->i_start != i_stop )
196     {
197         for( i_start = p_sub->i_start; i_start != i_stop;
198              i_start = (i_start+1) % VLC_MSG_QSIZE )
199         {
200             if( !b_verbose && VLC_MSG_ERR != p_sub->p_msg[i_start].i_type )
201                 continue;
202
203             /* Append all messages to log window */
204             debug = p_sub->p_msg[i_start].psz_module;
205         
206             switch( p_sub->p_msg[i_start].i_type )
207             {
208             case VLC_MSG_INFO:
209                 debug += ": ";
210                 break;
211             case VLC_MSG_ERR:
212                 debug += " error: ";
213                 break;
214             case VLC_MSG_WARN:
215                 debug += " warning: ";
216                 break;
217             case VLC_MSG_DBG:
218             default:
219                 debug += " debug: ";
220                 break;
221             }
222
223             /* Add message */
224             debug += p_sub->p_msg[i_start].psz_msg;
225
226             LVITEM lv;
227             lv.mask = LVIF_TEXT;
228             lv.pszText = TEXT("");
229             lv.cchTextMax = 1;
230             lv.iSubItem = 0;
231             lv.iItem = ListView_GetItemCount( hListView );
232             ListView_InsertItem( hListView, &lv );
233             ListView_SetItemText( hListView, lv.iItem, 0,
234                                   (TCHAR *)_FROMMB(debug.c_str()) );
235         }
236
237         vlc_mutex_lock( p_sub->p_lock );
238         p_sub->i_start = i_start;
239         vlc_mutex_unlock( p_sub->p_lock );
240     }
241 }