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