]> git.sesse.net Git - vlc/blob - modules/gui/skins/win32/win32_window.cpp
edcafa53c61a37283d3373bcf216a08131aa9378
[vlc] / modules / gui / skins / win32 / win32_window.cpp
1 /*****************************************************************************
2  * win32_window.cpp: Win32 implementation of the Window class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: win32_window.cpp,v 1.13 2003/10/17 18:17:28 ipkiss Exp $
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Emmanuel Puig    <karibu@via.ecp.fr>
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,
23  * USA.
24  *****************************************************************************/
25
26 #ifdef WIN32
27
28 /* For TrackMouseEvent, WM_MOUSEWHEEL and GET_WHEEL_DELTA_WPARAM */
29 #undef WINVER
30 #undef _WIN32_WINNT
31 #define _WIN32_WINNT 0x0500
32 #define WINVER 0x0500
33
34 //--- GENERAL ---------------------------------------------------------------
35 //#include <math.h>
36
37 //--- VLC -------------------------------------------------------------------
38 #include <vlc/intf.h>
39
40 //--- WIN32 -----------------------------------------------------------------
41 #include <windows.h>
42
43 //--- SKIN ------------------------------------------------------------------
44 #include "../os_api.h"
45 #include "../src/anchor.h"
46 #include "../controls/generic.h"
47 #include "../src/window.h"
48 #include "../os_window.h"
49 #include "../src/event.h"
50 #include "../os_event.h"
51 #include "../src/graphics.h"
52 #include "../os_graphics.h"
53 #include "../src/skin_common.h"
54 #include "../src/theme.h"
55 #include "../os_theme.h"
56 #include "../src/banks.h"
57
58 //---------------------------------------------------------------------------
59 // Fading API
60 //---------------------------------------------------------------------------
61 #define LWA_COLORKEY  0x00000001
62 #define LWA_ALPHA     0x00000002
63
64 //---------------------------------------------------------------------------
65 // Skinable Window
66 //---------------------------------------------------------------------------
67 Win32Window::Win32Window( intf_thread_t *p_intf, HWND hwnd, int x, int y,
68     bool visible, int transition, int normalalpha, int movealpha,
69     bool dragdrop )
70     : SkinWindow( p_intf, x, y, visible, transition, normalalpha, movealpha,
71               dragdrop )
72 {
73     // Set handles
74     hWnd           = hwnd;
75
76     // Set position parameters
77     CursorPos    = new POINT;
78     WindowPos    = new POINT;
79
80     // Create Tool Tip Window
81     ToolTipWindow = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
82         WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
83         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
84         hWnd, 0, GetModuleHandle( NULL ), 0);
85
86     // Create Tool Tip infos
87     ToolTipInfo.cbSize = sizeof(TOOLINFO);
88     ToolTipInfo.uFlags = TTF_SUBCLASS|TTF_IDISHWND;
89     ToolTipInfo.hwnd = hWnd;
90     ToolTipInfo.hinst = GetModuleHandle( NULL );
91     ToolTipInfo.uId = (unsigned int)hWnd;
92     ToolTipInfo.lpszText = NULL;
93     ToolTipInfo.rect.left = ToolTipInfo.rect.top = 0;
94         ToolTipInfo.rect.right = ToolTipInfo.rect.bottom = 0;
95
96     SendMessage( ToolTipWindow, TTM_ADDTOOL, 0,
97                     (LPARAM)(LPTOOLINFO) &ToolTipInfo );
98
99     // Drag & drop
100     if( DragDrop )
101     {
102         // Initialize the OLE library
103         OleInitialize( NULL );
104         DropTarget = (LPDROPTARGET) new Win32DropObject();
105         // register the listview as a drop target
106         RegisterDragDrop( hWnd, DropTarget );
107     }
108
109 }
110 //---------------------------------------------------------------------------
111 Win32Window::~Win32Window()
112 {
113     delete CursorPos;
114     delete WindowPos;
115
116     if( hWnd != NULL )
117     {
118         DestroyWindow( hWnd );
119     }
120     if( ToolTipWindow != NULL )
121     {
122         DestroyWindow( ToolTipWindow );
123     }
124     if( DragDrop )
125     {
126         // Remove the listview from the list of drop targets
127         RevokeDragDrop( hWnd );
128         DropTarget->Release();
129         // Uninitialize the OLE library
130         OleUninitialize();
131     }
132
133 }
134 //---------------------------------------------------------------------------
135 bool Win32Window::ProcessOSEvent( Event *evt )
136 {
137     unsigned int msg = evt->GetMessage();
138     unsigned int p1  = evt->GetParam1();
139     int          p2  = evt->GetParam2();
140
141     switch( msg )
142     {
143         case WM_PAINT:
144             HDC DC;
145             PAINTSTRUCT Infos;
146             DC = BeginPaint( hWnd , &Infos );
147             EndPaint( hWnd , &Infos );
148             RefreshFromImage( 0, 0, Width, Height );
149             return true;
150
151         case WM_MOUSEMOVE:
152             TRACKMOUSEEVENT TrackEvent;
153             TrackEvent.cbSize      = sizeof( TRACKMOUSEEVENT );
154             TrackEvent.dwFlags     = TME_LEAVE;
155             TrackEvent.hwndTrack   = hWnd;
156             TrackEvent.dwHoverTime = 1;
157             TrackMouseEvent( &TrackEvent );
158             if( p1 == MK_LBUTTON )
159                 MouseMove( LOWORD( p2 ), HIWORD( p2 ), 1 );
160             else if( p1 == MK_RBUTTON )
161                 MouseMove( LOWORD( p2 ), HIWORD( p2 ), 2 );
162             else
163                 MouseMove( LOWORD( p2 ), HIWORD( p2 ), 0 );
164
165             return true;
166
167         case WM_LBUTTONDOWN:
168             SetCapture( hWnd );
169             MouseDown( LOWORD( p2 ), HIWORD( p2 ), 1 );
170             return true;
171
172         case WM_LBUTTONUP:
173             ReleaseCapture();
174             MouseUp( LOWORD( p2 ), HIWORD( p2 ), 1 );
175             return true;
176
177         case WM_RBUTTONDOWN:
178             MouseDown( LOWORD( p2 ), HIWORD( p2 ), 2 );
179             return true;
180
181         case WM_RBUTTONUP:
182             MouseUp( LOWORD( p2 ), HIWORD( p2 ), 2 );
183             return true;
184
185         case WM_LBUTTONDBLCLK:
186             MouseDblClick( LOWORD( p2 ), HIWORD( p2 ), 1 );
187             return true;
188
189         case WM_MOUSELEAVE:
190             OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
191             return true;
192
193         case WM_MOUSEWHEEL:
194             if( GET_WHEEL_DELTA_WPARAM( p1 ) > 0 )
195                 MouseScroll( LOWORD( p2 ) - Left, HIWORD( p2 ) - Top,
196                     MOUSE_SCROLL_UP );
197             else if( GET_WHEEL_DELTA_WPARAM( p1 ) < 0 )
198                 MouseScroll( LOWORD( p2 ) - Left, HIWORD( p2 ) - Top,
199                     MOUSE_SCROLL_DOWN );
200             return true;
201
202         default:
203             return false;
204     }
205 }
206 //---------------------------------------------------------------------------
207 void Win32Window::ToggleOnTop()
208 {
209     Win32Theme *winTheme = (Win32Theme *)p_intf->p_sys->p_theme;
210     HMENU hMenu = GetSystemMenu( winTheme->GetParentWindow(), false );
211     Event *event = p_intf->p_sys->p_theme->EvtBank->Get( "on_top" );
212
213     if( !p_intf->p_sys->b_on_top )
214     {
215         // Set the window on top
216         SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0,
217                       SWP_NOSIZE | SWP_NOMOVE );
218         // Check the menu entry (FIXME: we shouldn't do that here...)
219         CheckMenuItem( hMenu, (unsigned int)event,
220                        MF_BYCOMMAND | MFS_CHECKED );
221     }
222     else
223     {
224         // Set the window not on top
225         SetWindowPos( hWnd, HWND_NOTOPMOST, 0, 0, 0, 0,
226                       SWP_NOSIZE | SWP_NOMOVE );
227         // Uncheck the menu entry (FIXME: we shouldn't do that here...)
228         CheckMenuItem( hMenu, (unsigned int)event,
229                        MF_BYCOMMAND | MFS_UNCHECKED );
230     }
231 }
232 //---------------------------------------------------------------------------
233 void Win32Window::OSShow( bool show )
234 {
235     if( show )
236     {
237         ShowWindow( hWnd, SW_SHOW );
238     }
239     else
240     {
241         ShowWindow( hWnd, SW_HIDE );
242     }
243 }
244 //---------------------------------------------------------------------------
245 void Win32Window::SetTransparency( int Value )
246 {
247     if( Value > -1 )
248         Alpha = Value;
249
250     if( p_intf->p_sys->SetLayeredWindowAttributes )
251         p_intf->p_sys->SetLayeredWindowAttributes( hWnd, 0, Alpha,
252                                                    LWA_ALPHA | LWA_COLORKEY );
253
254     UpdateWindow( hWnd );
255 }
256 //---------------------------------------------------------------------------
257 void Win32Window::RefreshFromImage( int x, int y, int w, int h )
258 {
259     // Initialize painting
260     HDC DC = GetWindowDC( hWnd );
261
262     // Draw image on window
263     BitBlt( DC, x, y, w, h, ( (Win32Graphics *)Image )->GetImageHandle(),
264             x, y, SRCCOPY );
265
266     // Release window device context
267     ReleaseDC( hWnd, DC );
268
269 }
270 //---------------------------------------------------------------------------
271 void Win32Window::WindowManualMove()
272 {
273     // Get mouse cursor position
274     LPPOINT NewPos = new POINT;
275     GetCursorPos( NewPos );
276
277     // Move window and chek for magnetism
278     p_intf->p_sys->p_theme->MoveSkinMagnet( this,
279         WindowPos->x + NewPos->x - CursorPos->x,
280         WindowPos->y + NewPos->y - CursorPos->y );
281
282     // Free memory
283     delete[] NewPos;
284
285 }
286 //---------------------------------------------------------------------------
287 void Win32Window::WindowManualMoveInit()
288 {
289     GetCursorPos( CursorPos );
290     WindowPos->x = Left;
291     WindowPos->y = Top;
292 }
293 //---------------------------------------------------------------------------
294 void Win32Window::Move( int left, int top )
295 {
296     Left = left;
297     Top  = top;
298     //SetWindowPos( hWnd, HWND_TOP, Left, Top, Width, Height,
299     //              SWP_NOSIZE|SWP_NOREDRAW|SWP_NOZORDER );
300     MoveWindow( hWnd, Left, Top, Width, Height, false );
301 }
302 //---------------------------------------------------------------------------
303 void Win32Window::Size( int width, int height )
304 {
305     Width  = width;
306     Height = height;
307     SetWindowPos( hWnd, HWND_TOP, Left, Top, Width, Height,
308                   SWP_NOMOVE|SWP_NOREDRAW|SWP_NOZORDER );
309 }
310 //---------------------------------------------------------------------------
311 void Win32Window::ChangeToolTipText( string text )
312 {
313     if( text == "none" )
314     {
315         if( ToolTipText != "none" )
316         {
317             ToolTipText = "none";
318             ToolTipInfo.lpszText = NULL;
319             SendMessage( ToolTipWindow, TTM_ACTIVATE, 0 , 0 );
320         }
321     }
322     else
323     {
324         if( text != ToolTipText )
325         {
326             ToolTipText = text;
327             ToolTipInfo.lpszText = (char *)ToolTipText.c_str();
328             SendMessage( ToolTipWindow, TTM_ACTIVATE, 1 , 0 );
329             SendMessage( ToolTipWindow, TTM_UPDATETIPTEXT, 0,
330                              (LPARAM)(LPTOOLINFO)&ToolTipInfo );
331         }
332     }
333
334 }
335 //---------------------------------------------------------------------------
336
337 #endif