]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_image.cpp
66ad5928a6ff76b4358a7c07a3f9a19e204a7032
[vlc] / modules / gui / skins2 / controls / ctrl_image.cpp
1 /*****************************************************************************
2  * ctrl_image.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 #include "ctrl_image.hpp"
26 #include "../commands/cmd_dialogs.hpp"
27 #include "../events/evt_generic.hpp"
28 #include "../src/os_factory.hpp"
29 #include "../src/os_graphics.hpp"
30 #include "../src/scaled_bitmap.hpp"
31 #include "../utils/position.hpp"
32
33
34 CtrlImage::CtrlImage( intf_thread_t *pIntf, const GenericBitmap &rBitmap,
35                       CmdGeneric &rCommand, resize_t resizeMethod,
36                       const UString &rHelp, VarBool *pVisible ):
37     CtrlFlat( pIntf, rHelp, pVisible ), m_rBitmap( rBitmap ),
38     m_rCommand( rCommand ), m_resizeMethod( resizeMethod )
39 {
40     // Create an initial unscaled image in the buffer
41     m_pImage = OSFactory::instance( pIntf )->createOSGraphics(
42                                     rBitmap.getWidth(), rBitmap.getHeight() );
43     m_pImage->drawBitmap( m_rBitmap );
44 }
45
46
47 CtrlImage::~CtrlImage()
48 {
49     delete m_pImage;
50 }
51
52
53 void CtrlImage::handleEvent( EvtGeneric &rEvent )
54 {
55     // No FSM for this simple transition
56     if( rEvent.getAsString() == "mouse:right:up:none" )
57     {
58         CmdDlgShowPopupMenu( getIntf() ).execute();
59     }
60     else if( rEvent.getAsString() == "mouse:left:up:none" )
61     {
62         CmdDlgHidePopupMenu( getIntf() ).execute();
63         CmdDlgHideVideoPopupMenu( getIntf() ).execute();
64         CmdDlgHideAudioPopupMenu( getIntf() ).execute();
65         CmdDlgHideMiscPopupMenu( getIntf() ).execute();
66     }
67     else if( rEvent.getAsString() == "mouse:left:dblclick:none" )
68     {
69         m_rCommand.execute();
70     }
71 }
72
73
74 bool CtrlImage::mouseOver( int x, int y ) const
75 {
76     if( m_resizeMethod == kMosaic &&
77         x >= 0 && x < getPosition()->getWidth() &&
78         y >= 0 && y < getPosition()->getHeight() )
79     {
80         // In mosaic mode, convert the coordinates to make them fit to the
81         // size of the original image
82         x %= m_pImage->getWidth();
83         y %= m_pImage->getHeight();
84     }
85     return m_pImage->hit( x, y );
86 }
87
88
89 void CtrlImage::draw( OSGraphics &rImage, int xDest, int yDest )
90 {
91     const Position *pPos = getPosition();
92     if( pPos )
93     {
94         int width = pPos->getWidth();
95         int height = pPos->getHeight();
96
97         if( m_resizeMethod == kScale )
98         {
99             // Use scaling method
100             if( width > 0 && height > 0 )
101             {
102                 if( width != m_pImage->getWidth() ||
103                     height != m_pImage->getHeight() )
104                 {
105                     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
106                     // Rescale the image with the actual size of the control
107                     ScaledBitmap bmp( getIntf(), m_rBitmap, width, height );
108                     delete m_pImage;
109                     m_pImage = pOsFactory->createOSGraphics( width, height );
110                     m_pImage->drawBitmap( bmp, 0, 0 );
111                 }
112                 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
113             }
114         }
115         else
116         {
117             // Use mosaic method
118             while( width > 0 )
119             {
120                 int curWidth = __MIN( width, m_pImage->getWidth() );
121                 height = pPos->getHeight();
122                 int curYDest = yDest;
123                 while( height > 0 )
124                 {
125                     int curHeight = __MIN( height, m_pImage->getHeight() );
126                     rImage.drawGraphics( *m_pImage, 0, 0, xDest, curYDest,
127                                          curWidth, curHeight );
128                     curYDest += curHeight;
129                     height -= m_pImage->getHeight();
130                 }
131                 xDest += curWidth;
132                 width -= m_pImage->getWidth();
133             }
134         }
135     }
136 }
137