]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/window.cpp
402058923508bfa6342c24cb9eb3476cc28846bd
[vlc] / modules / gui / skins / src / window.cpp
1 /*****************************************************************************
2  * window.cpp: Window class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: window.cpp,v 1.3 2003/03/19 18:14:48 karibu 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
27 //--- VLC -------------------------------------------------------------------
28 #include <vlc/intf.h>
29
30 //--- SKIN ------------------------------------------------------------------
31 #include "anchor.h"
32 #include "generic.h"
33 #include "window.h"
34 #include "event.h"
35 #include "os_api.h"
36 #include "graphics.h"
37 #include "os_graphics.h"
38 #include "banks.h"
39 #include "theme.h"
40 #include "skin_common.h"
41
42
43
44 //---------------------------------------------------------------------------
45 // Skinable Window
46 //---------------------------------------------------------------------------
47 Window::Window( intf_thread_t *_p_intf, int x, int y, bool visible,
48     int transition, int normalalpha, int movealpha, bool dragdrop )
49 {
50     p_intf = _p_intf;
51
52     // Set position parameters
53     Left         = x;
54     Top          = y;
55     Width        = 0;
56     Height       = 0;
57     WindowMoving = false;
58     Moved        = false;
59
60     // Set transparency
61     Transition   = transition;
62     if( Transition < 1 )
63         Transition = 1;
64     NormalAlpha  = normalalpha;
65     MoveAlpha    = movealpha;
66     Alpha        = normalalpha;
67     StartAlpha   = 0;
68     EndAlpha     = 0;
69     StartTime    = 0;
70     EndTime      = 0;
71     Lock         = 0;
72
73     // Visible parameters
74     Image               = NULL;
75     Hidden              = true;
76     Changing            = false;
77     OnStartThemeVisible = visible;
78
79     // Drag & drop
80     DragDrop = dragdrop;
81
82     // ToolTip
83     ToolTipText = "none";
84 }
85 //---------------------------------------------------------------------------
86 Window::~Window()
87 {
88     // Destroy the controls
89     for( unsigned int i = 0; i < ControlList.size(); i++ )
90         delete ControlList[i];
91 }
92 //---------------------------------------------------------------------------
93 void Window::Open()
94 {
95     if( !Hidden )
96         return;
97
98     Changing = true;
99
100     if( Transition )
101     {
102         SetTransparency( 0 );
103         OSAPI_PostMessage( this, WINDOW_SHOW, 0, 0 );
104         Fade( NormalAlpha, Transition );
105     }
106     else
107     {
108         OSAPI_PostMessage( this, WINDOW_SHOW, 0, 0 );
109     }
110 }
111 //---------------------------------------------------------------------------
112 void Window::Close()
113 {
114     Changing = true;
115
116     if( Transition )
117         Fade( 0, Transition, WINDOW_HIDE );
118     else
119         OSAPI_PostMessage( this, WINDOW_FADE, WINDOW_HIDE, 1242 );
120 }
121 //---------------------------------------------------------------------------
122 void Window::Show()
123 {
124     Changing = false;
125     Hidden   = false;
126     OSShow( true );
127 }
128 //---------------------------------------------------------------------------
129 void Window::Hide()
130 {
131     if( Hidden )
132         return;
133
134     Changing = false;
135     Hidden   = true;
136     OSShow( false );
137     OSAPI_PostMessage( NULL, VLC_TEST_ALL_CLOSED, 0, 0 );
138 }
139 //---------------------------------------------------------------------------
140 void Window::Fade( int To, int Time, unsigned int evt )
141 {
142     StartAlpha = Alpha;
143     EndAlpha   = To;
144     StartTime  = OSAPI_GetTime();
145     EndTime    = StartTime + Time;
146     Lock++;
147
148     OSAPI_PostMessage( this, WINDOW_FADE, evt, Lock );
149 }
150 //---------------------------------------------------------------------------
151 bool Window::ProcessEvent( Event *evt )
152 {
153     unsigned int i;
154     unsigned int msg = evt->GetMessage();
155     unsigned int p1  = evt->GetParam1();
156     int          p2  = evt->GetParam2();
157
158     // Send event to control if necessary
159     if( msg > VLC_CONTROL )
160     {
161         for( i = 0; i < ControlList.size(); i++ )
162             ControlList[i]->GenericProcessEvent( evt );
163         return true;
164     }
165
166     // Message processing
167     switch( msg )
168     {
169         case WINDOW_FADE:
170             if( Lock == p2 && ChangeAlpha( OSAPI_GetTime() ) )
171             {
172                 OSAPI_PostMessage( this, WINDOW_FADE, p1, p2 );
173             }
174             else
175             {
176                 OSAPI_PostMessage( this, p1, 0, 0 );
177             }
178             return true;
179
180         case WINDOW_MOVE:
181             WindowManualMoveInit();
182             WindowMoving = true;
183             if( MoveAlpha )
184                 Fade( MoveAlpha, 100 );
185             return true;
186
187         case WINDOW_OPEN:
188             switch( p1 )
189             {
190                 case 0:
191                     Close();
192                     break;
193                 case 1:
194                     Open();
195                     break;
196                 case 2:
197                     if( Hidden )
198                         Open();
199                     else
200                         Close();
201                     break;
202             }
203             return true;
204
205         case WINDOW_CLOSE:
206             switch( p1 )
207             {
208                 case 0:
209                     Open();
210                     break;
211                 case 1:
212                     Close();
213                     break;
214                 case 2:
215                     if( Hidden )
216                         Open();
217                     else
218                         Close();
219                     break;
220             }
221             return true;
222
223         case WINDOW_SHOW:
224             Show();
225             return true;
226
227         case WINDOW_HIDE:
228             Hide();
229             return true;
230
231         default:
232             // OS specific messages processing
233             return ProcessOSEvent( evt );
234     }
235 }
236 //---------------------------------------------------------------------------
237 bool Window::ChangeAlpha( int time )
238 {
239     if( time >= EndTime )
240     {
241         if( Lock )
242         {
243             SetTransparency( EndAlpha );
244             Lock = 0;
245         }
246         return false;
247     }
248
249     int NewAlpha = StartAlpha + (EndAlpha - StartAlpha) * (time - StartTime)
250         / (EndTime - StartTime);
251     if( NewAlpha != Alpha )
252         SetTransparency( NewAlpha );
253     if( NewAlpha == EndAlpha )
254     {
255         Lock = 0;
256         return false;
257     }
258     return true;
259 }
260 //---------------------------------------------------------------------------
261 void Window::RefreshImage( int x, int y, int w, int h )
262 {
263     unsigned int i;
264
265     // Create Bitmap Buffer
266     Graphics *Buffer = (Graphics *)new OSGraphics( w, h, this );
267
268     // Draw every control
269         for( i = 0; i < ControlList.size(); i++ )
270             ControlList[i]->Draw( x, y, w, h, Buffer );
271
272     // Copy buffer in Image
273     Image->CopyFrom( x, y, w, h, Buffer, 0, 0, SRC_COPY );
274
275     // Free memory
276     delete Buffer;
277 }
278 //---------------------------------------------------------------------------
279 void Window::Refresh( int x, int y, int w, int h )
280 {
281     if( Image == NULL )
282         return;
283
284     // Refresh buffer image
285     RefreshImage( x, y, w, h );
286
287     if( Hidden )
288         return;
289
290     // And copy buffer to window
291     RefreshFromImage( x, y, w, h );
292
293 }
294 //---------------------------------------------------------------------------
295 void Window::RefreshAll()
296 {
297     Refresh( 0, 0, Width, Height );
298 }
299 //---------------------------------------------------------------------------
300 void Window::MouseDown( int x, int y, int button )
301 {
302     // Checking event in controls
303
304     for( int i = ControlList.size() - 1; i >= 0 ; i-- )
305     {
306         if( ControlList[i]->MouseDown( x, y, button ) )
307         {
308             return;
309         }
310     }
311 }
312 //---------------------------------------------------------------------------
313 void Window::MouseMove( int x, int y, int button  )
314 {
315     int i;
316
317     // Move window if selected !
318     if( WindowMoving )
319     {
320         WindowManualMove();
321     }
322
323     // Checking event in controls
324     for( i = ControlList.size() - 1; i >= 0 ; i-- )
325     {
326         ControlList[i]->MouseMove( x, y, button );
327     }
328
329     // Checking help text
330     for( i = ControlList.size() - 1; i >= 0 ; i-- )
331     {
332         if( ControlList[i]->MouseOver( x, y ) )
333         {
334             if( ControlList[i]->SendNewHelpText() )
335             {
336                 break;
337             }
338         }
339     }
340
341     // If help text not found, change it to ""
342     if( i == -1 )
343     {
344         p_intf->p_sys->p_theme->EvtBank->Get( "help" )
345             ->PostTextMessage( " " );
346     }
347
348     // Checking for change in Tool Tip
349     for( i = ControlList.size() - 1; i >= 0 ; i-- )
350     {
351         if( ControlList[i]->ToolTipTest( x, y ) )
352             break;
353     }
354
355     // If no change, delete tooltip text
356     if( i == -1 )
357         ChangeToolTipText( "none" );
358 }
359 //---------------------------------------------------------------------------
360 void Window::MouseUp( int x, int y, int button )
361 {
362     int i;
363
364     // Move window if selected !
365     if( WindowMoving )
366     {
367         if( MoveAlpha )
368             Fade( NormalAlpha, 100 );
369
370         // Check for magnetism
371         p_intf->p_sys->p_theme->CheckAnchors();
372
373         WindowMoving = false;
374     }
375
376     // Checking event in controls
377     for( i = ControlList.size() - 1; i >= 0 ; i-- )
378     {
379         if( ControlList[i]->MouseUp( x, y, button ) )
380             return;
381     }
382 }
383 //---------------------------------------------------------------------------
384 void Window::MouseDblClick( int x, int y, int button )
385 {
386     int i;
387
388     // Checking event in controls
389     for( i = ControlList.size() - 1; i >= 0 ; i-- )
390     {
391         if( ControlList[i]->MouseDblClick( x, y, button ) )
392             return;
393     }
394 }
395 //---------------------------------------------------------------------------
396 void Window::Init()
397 {
398     // Get size of window
399     ReSize();
400
401     // Refresh Image buffer
402     RefreshImage( 0, 0, Width, Height );
403
404     // Move window as it hasn't been moved yet
405     Move( Left, Top );
406 }
407 //---------------------------------------------------------------------------
408 void Window::ReSize()
409 {
410     // Initialization
411     unsigned int i;
412     int w    = 0;
413     int h    = 0;
414     int MinX = 10000000;
415     int MinY = 10000000;
416
417     // Search size of window and negative values to move all
418     for( i = 0; i < ControlList.size(); i++ )
419     {
420 #define min(a,b) ((a)<(b))?(a):(b)
421 #define max(a,b) ((a)>(b))?(a):(b)
422         w    = max( w,    ControlList[i]->Left + ControlList[i]->Width );
423         h    = max( h,    ControlList[i]->Top + ControlList[i]->Height );
424         MinX = min( MinX, ControlList[i]->Left );
425         MinY = min( MinY, ControlList[i]->Top );
426 #undef max
427 #undef min
428     }
429
430     // Correct values
431     w = w - MinX;
432     h = h - MinY;
433     if( w <= 0 )
434         w = 1;
435     if( h <= 0 )
436         h = 1;
437
438     // Move window and controls !
439     if( MinX != 0 || MinY != 0 )
440     {
441         Move( Left + MinX, Top + MinY );
442         for( i = 0; i < ControlList.size(); i++ )
443             ControlList[i]->MoveRelative( -MinX, -MinY );
444     }
445
446     // Create buffer image for repainting if size has changed
447     if( w != Width || h != Height )
448     {
449         // Change image buffer
450         if( Image != NULL )
451             delete (OSGraphics *)Image;
452         Image = (Graphics *)new OSGraphics( w, h, this );
453
454
455         Size( w, h );
456     }
457
458 }
459 //---------------------------------------------------------------------------
460 void Window::GetSize( int &w, int &h )
461 {
462     w = Width;
463     h = Height;
464 }
465 //---------------------------------------------------------------------------
466 void Window::GetPos( int &x, int &y )
467 {
468     x = Left;
469     y = Top;
470 }
471 //---------------------------------------------------------------------------
472