]> git.sesse.net Git - vlc/blob - modules/gui/skins/win32/win32_window.cpp
* modules/gui/skins/*: Added a "playondrop" attribute to the "Window"
[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.14 2003/10/22 19:12:56 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, bool playondrop )
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( playondrop );
105         // register the listview as a drop target
106         RegisterDragDrop( hWnd, DropTarget );
107     }
108 }
109 //---------------------------------------------------------------------------
110 Win32Window::~Win32Window()
111 {
112     delete CursorPos;
113     delete WindowPos;
114
115     if( hWnd != NULL )
116     {
117         DestroyWindow( hWnd );
118     }
119     if( ToolTipWindow != NULL )
120     {
121         DestroyWindow( ToolTipWindow );
122     }
123     if( DragDrop )
124     {
125         // Remove the listview from the list of drop targets
126         RevokeDragDrop( hWnd );
127         DropTarget->Release();
128         // Uninitialize the OLE library
129         OleUninitialize();
130     }
131 }
132 //---------------------------------------------------------------------------
133 bool Win32Window::ProcessOSEvent( Event *evt )
134 {
135     unsigned int msg = evt->GetMessage();
136     unsigned int p1  = evt->GetParam1();
137     int          p2  = evt->GetParam2();
138
139     switch( msg )
140     {
141         case WM_PAINT:
142             HDC DC;
143             PAINTSTRUCT Infos;
144             DC = BeginPaint( hWnd , &Infos );
145             EndPaint( hWnd , &Infos );
146             RefreshFromImage( 0, 0, Width, Height );
147             return true;
148
149         case WM_MOUSEMOVE:
150             TRACKMOUSEEVENT TrackEvent;
151             TrackEvent.cbSize      = sizeof( TRACKMOUSEEVENT );
152             TrackEvent.dwFlags     = TME_LEAVE;
153             TrackEvent.hwndTrack   = hWnd;
154             TrackEvent.dwHoverTime = 1;
155             TrackMouseEvent( &TrackEvent );
156             if( p1 == MK_LBUTTON )
157                 MouseMove( LOWORD( p2 ), HIWORD( p2 ), 1 );
158             else if( p1 == MK_RBUTTON )
159                 MouseMove( LOWORD( p2 ), HIWORD( p2 ), 2 );
160             else
161                 MouseMove( LOWORD( p2 ), HIWORD( p2 ), 0 );
162
163             return true;
164
165         case WM_LBUTTONDOWN:
166             SetCapture( hWnd );
167             MouseDown( LOWORD( p2 ), HIWORD( p2 ), 1 );
168             return true;
169
170         case WM_LBUTTONUP:
171             ReleaseCapture();
172             MouseUp( LOWORD( p2 ), HIWORD( p2 ), 1 );
173             return true;
174
175         case WM_RBUTTONDOWN:
176             MouseDown( LOWORD( p2 ), HIWORD( p2 ), 2 );
177             return true;
178
179         case WM_RBUTTONUP:
180             MouseUp( LOWORD( p2 ), HIWORD( p2 ), 2 );
181             return true;
182
183         case WM_LBUTTONDBLCLK:
184             MouseDblClick( LOWORD( p2 ), HIWORD( p2 ), 1 );
185             return true;
186
187         case WM_MOUSELEAVE:
188             OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
189             return true;
190
191         case WM_MOUSEWHEEL:
192             if( GET_WHEEL_DELTA_WPARAM( p1 ) > 0 )
193                 MouseScroll( LOWORD( p2 ) - Left, HIWORD( p2 ) - Top,
194                     MOUSE_SCROLL_UP );
195             else if( GET_WHEEL_DELTA_WPARAM( p1 ) < 0 )
196                 MouseScroll( LOWORD( p2 ) - Left, HIWORD( p2 ) - Top,
197                     MOUSE_SCROLL_DOWN );
198             return true;
199
200         default:
201             return false;
202     }
203 }
204 //---------------------------------------------------------------------------
205 void Win32Window::ToggleOnTop()
206 {
207     Win32Theme *winTheme = (Win32Theme *)p_intf->p_sys->p_theme;
208     HMENU hMenu = GetSystemMenu( winTheme->GetParentWindow(), false );
209     Event *event = p_intf->p_sys->p_theme->EvtBank->Get( "on_top" );
210
211     if( !p_intf->p_sys->b_on_top )
212     {
213         // Set the window on top
214         SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0,
215                       SWP_NOSIZE | SWP_NOMOVE );
216         // Check the menu entry (FIXME: we shouldn't do that here...)
217         CheckMenuItem( hMenu, (unsigned int)event,
218                        MF_BYCOMMAND | MFS_CHECKED );
219     }
220     else
221     {
222         // Set the window not on top
223         SetWindowPos( hWnd, HWND_NOTOPMOST, 0, 0, 0, 0,
224                       SWP_NOSIZE | SWP_NOMOVE );
225         // Uncheck the menu entry (FIXME: we shouldn't do that here...)
226         CheckMenuItem( hMenu, (unsigned int)event,
227                        MF_BYCOMMAND | MFS_UNCHECKED );
228     }
229 }
230 //---------------------------------------------------------------------------
231 void Win32Window::OSShow( bool show )
232 {
233     if( show )
234     {
235         ShowWindow( hWnd, SW_SHOW );
236     }
237     else
238     {
239         ShowWindow( hWnd, SW_HIDE );
240     }
241 }
242 //---------------------------------------------------------------------------
243 void Win32Window::SetTransparency( int Value )
244 {
245     if( Value > -1 )
246         Alpha = Value;
247
248     if( p_intf->p_sys->SetLayeredWindowAttributes )
249         p_intf->p_sys->SetLayeredWindowAttributes( hWnd, 0, Alpha,
250                                                    LWA_ALPHA | LWA_COLORKEY );
251
252     UpdateWindow( hWnd );
253 }
254 //---------------------------------------------------------------------------
255 void Win32Window::RefreshFromImage( int x, int y, int w, int h )
256 {
257     // Initialize painting
258     HDC DC = GetWindowDC( hWnd );
259
260     // Draw image on window
261     BitBlt( DC, x, y, w, h, ( (Win32Graphics *)Image )->GetImageHandle(),
262             x, y, SRCCOPY );
263
264     // Release window device context
265     ReleaseDC( hWnd, DC );
266 }
267 //---------------------------------------------------------------------------
268 void Win32Window::WindowManualMove()
269 {
270     // Get mouse cursor position
271     LPPOINT NewPos = new POINT;
272     GetCursorPos( NewPos );
273
274     // Move window and chek for magnetism
275     p_intf->p_sys->p_theme->MoveSkinMagnet( this,
276         WindowPos->x + NewPos->x - CursorPos->x,
277         WindowPos->y + NewPos->y - CursorPos->y );
278
279     // Free memory
280     delete[] NewPos;
281 }
282 //---------------------------------------------------------------------------
283 void Win32Window::WindowManualMoveInit()
284 {
285     GetCursorPos( CursorPos );
286     WindowPos->x = Left;
287     WindowPos->y = Top;
288 }
289 //---------------------------------------------------------------------------
290 void Win32Window::Move( int left, int top )
291 {
292     Left = left;
293     Top  = top;
294     //SetWindowPos( hWnd, HWND_TOP, Left, Top, Width, Height,
295     //              SWP_NOSIZE|SWP_NOREDRAW|SWP_NOZORDER );
296     MoveWindow( hWnd, Left, Top, Width, Height, false );
297 }
298 //---------------------------------------------------------------------------
299 void Win32Window::Size( int width, int height )
300 {
301     Width  = width;
302     Height = height;
303     SetWindowPos( hWnd, HWND_TOP, Left, Top, Width, Height,
304                   SWP_NOMOVE|SWP_NOREDRAW|SWP_NOZORDER );
305 }
306 //---------------------------------------------------------------------------
307 void Win32Window::ChangeToolTipText( string text )
308 {
309     if( text == "none" )
310     {
311         if( ToolTipText != "none" )
312         {
313             ToolTipText = "none";
314             ToolTipInfo.lpszText = NULL;
315             SendMessage( ToolTipWindow, TTM_ACTIVATE, 0 , 0 );
316         }
317     }
318     else
319     {
320         if( text != ToolTipText )
321         {
322             ToolTipText = text;
323             ToolTipInfo.lpszText = (char *)ToolTipText.c_str();
324             SendMessage( ToolTipWindow, TTM_ACTIVATE, 1 , 0 );
325             SendMessage( ToolTipWindow, TTM_UPDATETIPTEXT, 0,
326                              (LPARAM)(LPTOOLINFO)&ToolTipInfo );
327         }
328     }
329 }
330 //---------------------------------------------------------------------------
331
332 #endif