]> git.sesse.net Git - vlc/blob - modules/gui/wince/messages.cpp
Factorize.
[vlc] / modules / gui / wince / messages.cpp
1 /*****************************************************************************
2  * messages.cpp : WinCE gui plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_interface.h>
34
35 #include "wince.h"
36
37 #include <winuser.h>
38 #include <windows.h>
39 #include <windowsx.h>
40 #include <commctrl.h>
41 #include <commdlg.h>
42
43 #ifndef NMAXFILE
44 #define NMAXFILE 512 // at least 256
45 #endif
46
47 #ifndef TEXTMAXBUF
48 #define TEXTMAXBUF 512 // at least 500
49 #endif
50
51 /*****************************************************************************
52  * Constructor.
53  *****************************************************************************/
54
55 Messages::Messages( intf_thread_t *p_intf, CBaseWindow *p_parent,
56                     HINSTANCE h_inst )
57   :  CBaseWindow( p_intf, p_parent, h_inst )
58 {
59     /* Initializations */
60     hListView = NULL;
61
62     hWnd = CreateWindow( _T("VLC WinCE"), _T("Messages"),
63                          WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX,
64                          0, 0, /*CW_USEDEFAULT*/300, /*CW_USEDEFAULT*/300,
65                          p_parent->GetHandle(), NULL, h_inst, (void *)this );
66         // Suscribe to messages bank
67     cb_data = new msg_cb_data_t;
68     cb_data->self = this;
69     sub = msg_Subscribe( p_intf->p_libvlc, sinkMessage, cb_data );
70 }
71
72 Messages::~Messages()
73 {
74     msg_Unsubscribe(sub);
75     delete cb_data;
76 }
77
78 /***********************************************************************
79 FUNCTION:
80   WndProc
81
82 PURPOSE:
83   Processes messages sent to the main window.
84 ***********************************************************************/
85 LRESULT Messages::WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
86 {
87     SHINITDLGINFO shidi;
88
89     TCHAR psz_text[TEXTMAXBUF];
90     OPENFILENAME ofn;
91     int i_dummy;
92     HANDLE fichier;
93
94     switch( msg )
95     {
96     case WM_CREATE:
97         shidi.dwMask = SHIDIM_FLAGS;
98         shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN |
99             SHIDIF_FULLSCREENNOMENUBAR;//SHIDIF_SIZEDLGFULLSCREEN;
100         shidi.hDlg = hwnd;
101         SHInitDialog( &shidi );
102
103         hListView = CreateWindow( WC_LISTVIEW, NULL,
104                                   WS_VISIBLE | WS_CHILD | LVS_REPORT |
105                                   LVS_SHOWSELALWAYS | WS_VSCROLL | WS_HSCROLL |
106                                   WS_BORDER | LVS_NOCOLUMNHEADER, 0, 0, 0, 0,
107                                   hwnd, NULL, hInst, NULL );
108         ListView_SetExtendedListViewStyle( hListView, LVS_EX_FULLROWSELECT );
109
110         LVCOLUMN lv;
111         lv.mask = LVCF_FMT;
112         lv.fmt = LVCFMT_LEFT ;
113         ListView_InsertColumn( hListView, 0, &lv );
114
115         SetTimer( hwnd, 1, 500 /*milliseconds*/, NULL );
116         break;
117
118     case WM_WINDOWPOSCHANGED:
119         {
120             RECT rect;
121             if( !GetClientRect( hwnd, &rect ) ) break;
122             SetWindowPos( hListView, 0, 0, 0,
123                           rect.right - rect.left, rect.bottom - rect.top, 0 );
124
125             LVCOLUMN lv;
126             lv.cx = rect.right - rect.left;
127             lv.mask = LVCF_WIDTH;
128             ListView_SetColumn( hListView, 0, &lv );
129         }
130         break;
131
132     case WM_SETFOCUS:
133         SHSipPreference( hwnd, SIP_DOWN );
134         SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
135         break;
136
137     case WM_CLOSE:
138         Show( FALSE );
139         return TRUE;
140
141     case WM_COMMAND:
142         switch( LOWORD(wp) )
143         {
144         case IDOK:
145             Show( FALSE );
146             break;
147
148         case IDCLEAR:
149             ListView_DeleteAllItems( hListView );
150             break;
151
152         case IDSAVEAS:
153             memset( &(ofn), 0, sizeof(ofn) );
154             ofn.lStructSize = sizeof(ofn);
155             ofn.hwndOwner = hwnd;
156             ofn.lpstrFile = _T("");
157             ofn.nMaxFile = NMAXFILE;
158             ofn.lpstrFilter = _T("Text (*.txt)\0*.txt\0");
159             ofn.lpstrTitle = _T("Save File As");
160             ofn.Flags = OFN_HIDEREADONLY;
161             ofn.lpstrDefExt = _T("txt");
162
163             if( GetSaveFileName( (LPOPENFILENAME)&ofn ) )
164             {
165                 fichier = CreateFile( ofn.lpstrFile, GENERIC_WRITE,
166                                       FILE_SHARE_READ|FILE_SHARE_WRITE,
167                                       NULL, CREATE_ALWAYS,
168                                       FILE_ATTRIBUTE_NORMAL, NULL );
169
170                 if( fichier != INVALID_HANDLE_VALUE )
171                 {
172                     int n;
173
174                     //SetFilePointer( fichier, 0, NULL, FILE_END );
175                     for( n = 0; n < ListView_GetItemCount( hListView ); n++ )
176                     {
177                         ListView_GetItemText( hListView, n, 0, psz_text,
178                                               TEXTMAXBUF );
179                         string text_out = (string)_TOMB(psz_text) + "\n";
180                         WriteFile( fichier, text_out.c_str(), text_out.size(),
181                                    (LPDWORD)&i_dummy, NULL );
182                     }
183                     FlushFileBuffers( fichier );
184                     CloseHandle(fichier);
185                 }
186             }
187             break;
188
189         default:
190             break;
191         }
192
193     default:
194         break;
195     }
196
197     return DefWindowProc( hwnd, msg, wp, lp );
198 }
199
200 void Messages::sinkMessage (msg_cb_data_t *data, msg_item_t *item,
201                                   unsigned overruns)
202 {
203     Messages *self = data->self;
204
205     self->sinkMessage (item, overruns);
206 }
207
208 void Messages::sinkMessage (msg_item_t *item, unsigned overruns)
209 {
210     vlc_value_t val;
211     var_Get( p_intf->p_libvlc, "verbose", &val );
212
213
214     /* Append all messages to log window */
215     string debug = item->psz_module;
216
217     switch( item->i_type )
218     {
219         case VLC_MSG_INFO: debug += ": "; break;
220         case VLC_MSG_ERR: debug += " error: "; break;
221         case VLC_MSG_WARN: debug += " warning: "; break;
222         default: debug += " debug: "; break;
223     }
224
225     /* Add message */
226     debug += item->psz_msg;
227
228     LVITEM lv;
229     lv.mask = LVIF_TEXT;
230     lv.pszText = TEXT("");
231     lv.cchTextMax = 1;
232     lv.iSubItem = 0;
233     lv.iItem = ListView_GetItemCount( hListView );
234     ListView_InsertItem( hListView, &lv );
235     ListView_SetItemText( hListView, lv.iItem, 0,
236                           (TCHAR *)_FROMMB(debug.c_str()) );
237 }
238