]> git.sesse.net Git - vlc/blob - modules/gui/skins2/win32/win32_window.cpp
skins2 vout manager
[vlc] / modules / gui / skins2 / win32 / win32_window.cpp
1 /*****************************************************************************
2  * win32_window.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef WIN32_SKINS
26
27 #include "../src/generic_window.hpp"
28 #include "../src/vlcproc.hpp"
29 #include "win32_window.hpp"
30 #include "win32_dragdrop.hpp"
31 #include "win32_factory.hpp"
32
33
34 /// Fading API
35 #ifndef LWA_COLORKEY
36 #   define LWA_COLORKEY  0x00000001
37 #   define LWA_ALPHA     0x00000002
38 #endif
39
40
41 // XXX layered windows are supposed to work only with at least win2k
42 #ifndef WS_EX_LAYERED
43 #   define WS_EX_LAYERED 0x00080000
44 #endif
45
46 Win32Window::Win32Window( intf_thread_t *pIntf, GenericWindow &rWindow,
47                           HINSTANCE hInst, HWND hParentWindow,
48                           bool dragDrop, bool playOnDrop,
49                           Win32Window *pParentWindow ):
50     OSWindow( pIntf ), m_dragDrop( dragDrop ), m_isLayered( false ),
51     m_pParent( pParentWindow )
52 {
53     // Create the window
54     if( pParentWindow )
55     {
56         // Child window (for vout)
57         m_hWnd_parent = pParentWindow->getHandle();
58         m_hWnd = CreateWindowEx( WS_EX_TOOLWINDOW, "SkinWindowClass",
59             "default name", WS_CHILD, CW_USEDEFAULT, CW_USEDEFAULT,
60             CW_USEDEFAULT, CW_USEDEFAULT, m_hWnd_parent, 0, hInst, NULL );
61     }
62     else
63     {
64         // Normal window
65         m_hWnd_parent = hParentWindow;
66         m_hWnd = CreateWindowEx( WS_EX_TOOLWINDOW, "SkinWindowClass",
67             "default name", WS_POPUP | WS_CLIPCHILDREN,
68             CW_USEDEFAULT, CW_USEDEFAULT,
69             CW_USEDEFAULT, CW_USEDEFAULT, m_hWnd_parent, 0, hInst, NULL );
70     }
71
72     if( !m_hWnd )
73     {
74         msg_Err( getIntf(), "CreateWindow failed" );
75         return;
76     }
77
78     // Store a pointer to the GenericWindow in a map
79     Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( getIntf() );
80     pFactory->m_windowMap[m_hWnd] = &rWindow;
81
82     // Drag & drop
83     if( m_dragDrop )
84     {
85         m_pDropTarget = (LPDROPTARGET) new Win32DragDrop( getIntf(),
86                                                           playOnDrop );
87         // Register the window as a drop target
88         RegisterDragDrop( m_hWnd, m_pDropTarget );
89     }
90 }
91
92
93 Win32Window::~Win32Window()
94 {
95     Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( getIntf() );
96     pFactory->m_windowMap[m_hWnd] = NULL;
97
98     if( m_hWnd )
99     {
100         if( m_dragDrop )
101         {
102             // Remove the window from the list of drop targets
103             RevokeDragDrop( m_hWnd );
104             m_pDropTarget->Release();
105         }
106
107         DestroyWindow( m_hWnd );
108     }
109 }
110
111
112 void Win32Window::reparent( void* OSHandle, int x, int y, int w, int h )
113 {
114     // Reparent the window
115     SetParent( m_hWnd, (HWND)OSHandle );
116     MoveWindow( m_hWnd, x, y, w, h, true );
117 }
118
119
120 void Win32Window::show( int left, int top ) const
121 {
122     ShowWindow( m_hWnd, SW_SHOW );
123 }
124
125
126 void Win32Window::hide() const
127 {
128     ShowWindow( m_hWnd, SW_HIDE );
129 }
130
131
132 void Win32Window::moveResize( int left, int top, int width, int height ) const
133 {
134     MoveWindow( m_hWnd, left, top, width, height, true );
135 }
136
137
138 void Win32Window::raise() const
139 {
140 //     SetWindowPos( m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
141     SetForegroundWindow( m_hWnd );
142 }
143
144
145 void Win32Window::setOpacity( uint8_t value ) const
146 {
147     Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( getIntf() );
148
149     if( value == 255 )
150     {
151         // If the window is opaque, we remove the WS_EX_LAYERED attribute
152         // which slows down resizing for nothing
153         if( m_isLayered )
154         {
155             SetWindowLongPtr( m_hWnd, GWL_EXSTYLE,
156                 GetWindowLong( m_hWnd, GWL_EXSTYLE ) & ~WS_EX_LAYERED );
157
158             // Redraw the window, otherwise we may end up with a grey rectangle
159             // for some strange reason
160             RedrawWindow(m_hWnd, NULL, NULL,
161                 RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
162
163             m_isLayered = false;
164         }
165     }
166     else
167     {
168         if( pFactory->SetLayeredWindowAttributes )
169         {
170             if( ! m_isLayered )
171             {
172                 // (Re)Add the WS_EX_LAYERED attribute.
173                 // Resizing will be very slow, now :)
174                 SetWindowLongPtr( m_hWnd, GWL_EXSTYLE,
175                     GetWindowLong( m_hWnd, GWL_EXSTYLE ) | WS_EX_LAYERED );
176
177                 // Redraw the window, otherwise we may end up with a grey
178                 // rectangle for some strange reason
179                 RedrawWindow(m_hWnd, NULL, NULL,
180                     RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
181
182                 m_isLayered = true;
183             }
184
185             // Change the opacity
186             pFactory->SetLayeredWindowAttributes(
187                 m_hWnd, 0, value, LWA_ALPHA|LWA_COLORKEY );
188         }
189     }
190 }
191
192
193 void Win32Window::toggleOnTop( bool onTop ) const
194 {
195     if( onTop )
196     {
197         // Set the window on top
198         SetWindowPos( m_hWnd, HWND_TOPMOST, 0, 0, 0, 0,
199                       SWP_NOSIZE | SWP_NOMOVE );
200     }
201     else
202     {
203         // Set the window not on top
204         SetWindowPos( m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0,
205                       SWP_NOSIZE | SWP_NOMOVE );
206     }
207 }
208
209
210 #endif