]> git.sesse.net Git - vlc/blob - modules/gui/skins2/win32/win32_window.cpp
Uniformize source files encoding
[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         HWND hParent = pParentWindow->getHandle();
58         m_hWnd = CreateWindowEx( WS_EX_TOOLWINDOW, "SkinWindowClass",
59             "default name", WS_CHILD, CW_USEDEFAULT, CW_USEDEFAULT,
60             CW_USEDEFAULT, CW_USEDEFAULT, hParent, 0, hInst, NULL );
61     }
62     else
63     {
64         // Normal window
65         m_hWnd = CreateWindowEx( WS_EX_TOOLWINDOW, "SkinWindowClass",
66             "default name", WS_POPUP | WS_CLIPCHILDREN,
67             CW_USEDEFAULT, CW_USEDEFAULT,
68             CW_USEDEFAULT, CW_USEDEFAULT, hParentWindow, 0, hInst, NULL );
69     }
70
71     if( !m_hWnd )
72     {
73         msg_Err( getIntf(), "CreateWindow failed" );
74         return;
75     }
76
77     // Store a pointer to the GenericWindow in a map
78     Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( getIntf() );
79     pFactory->m_windowMap[m_hWnd] = &rWindow;
80
81     // Drag & drop
82     if( m_dragDrop )
83     {
84         m_pDropTarget = (LPDROPTARGET) new Win32DragDrop( getIntf(),
85                                                           playOnDrop );
86         // Register the window as a drop target
87         RegisterDragDrop( m_hWnd, m_pDropTarget );
88     }
89
90     // Set this window as a vout
91     if( m_pParent )
92     {
93         VlcProc::instance( getIntf() )->registerVoutWindow( (void*)m_hWnd );
94     }
95 }
96
97
98 Win32Window::~Win32Window()
99 {
100     if( m_pParent )
101     {
102         VlcProc::instance( getIntf() )->unregisterVoutWindow( (void*)m_hWnd );
103     }
104
105     Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( getIntf() );
106     pFactory->m_windowMap[m_hWnd] = NULL;
107
108     if( m_hWnd )
109     {
110         if( m_dragDrop )
111         {
112             // Remove the window from the list of drop targets
113             RevokeDragDrop( m_hWnd );
114             m_pDropTarget->Release();
115         }
116
117         DestroyWindow( m_hWnd );
118     }
119 }
120
121
122 void Win32Window::show( int left, int top ) const
123 {
124     ShowWindow( m_hWnd, SW_SHOW );
125 }
126
127
128 void Win32Window::hide() const
129 {
130     ShowWindow( m_hWnd, SW_HIDE );
131 }
132
133
134 void Win32Window::moveResize( int left, int top, int width, int height ) const
135 {
136     MoveWindow( m_hWnd, left, top, width, height, true );
137 }
138
139
140 void Win32Window::raise() const
141 {
142 //     SetWindowPos( m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
143     SetForegroundWindow( m_hWnd );
144 }
145
146
147 void Win32Window::setOpacity( uint8_t value ) const
148 {
149     Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( getIntf() );
150
151     if( value == 255 )
152     {
153         // If the window is opaque, we remove the WS_EX_LAYERED attribute
154         // which slows down resizing for nothing
155         if( m_isLayered )
156         {
157             SetWindowLongPtr( m_hWnd, GWL_EXSTYLE,
158                 GetWindowLong( m_hWnd, GWL_EXSTYLE ) & ~WS_EX_LAYERED );
159
160             // Redraw the window, otherwise we may end up with a grey rectangle
161             // for some strange reason
162             RedrawWindow(m_hWnd, NULL, NULL,
163                 RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
164
165             m_isLayered = false;
166         }
167     }
168     else
169     {
170         if( pFactory->SetLayeredWindowAttributes )
171         {
172             if( ! m_isLayered )
173             {
174                 // (Re)Add the WS_EX_LAYERED attribute.
175                 // Resizing will be very slow, now :)
176                 SetWindowLongPtr( m_hWnd, GWL_EXSTYLE,
177                     GetWindowLong( m_hWnd, GWL_EXSTYLE ) | WS_EX_LAYERED );
178
179                 // Redraw the window, otherwise we may end up with a grey
180                 // rectangle for some strange reason
181                 RedrawWindow(m_hWnd, NULL, NULL,
182                     RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
183
184                 m_isLayered = true;
185             }
186
187             // Change the opacity
188             pFactory->SetLayeredWindowAttributes(
189                 m_hWnd, 0, value, LWA_ALPHA|LWA_COLORKEY );
190         }
191     }
192 }
193
194
195 void Win32Window::toggleOnTop( bool onTop ) const
196 {
197     if( onTop )
198     {
199         // Set the window on top
200         SetWindowPos( m_hWnd, HWND_TOPMOST, 0, 0, 0, 0,
201                       SWP_NOSIZE | SWP_NOMOVE );
202     }
203     else
204     {
205         // Set the window not on top
206         SetWindowPos( m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0,
207                       SWP_NOSIZE | SWP_NOMOVE );
208     }
209 }
210
211
212 #endif