]> git.sesse.net Git - vlc/blob - modules/gui/skins/controls/button.cpp
Keep quiet, explanations will follow.
[vlc] / modules / gui / skins / controls / button.cpp
1 /*****************************************************************************
2  * button.cpp: Button control
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: button.cpp,v 1.1 2003/03/18 02:21:47 ipkiss 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 "bitmap.h"
32 #include "banks.h"
33 #include "generic.h"
34 #include "button.h"
35 #include "event.h"
36 #include "theme.h"
37 #include "window.h"
38 #include "skin_common.h"
39
40
41
42 //---------------------------------------------------------------------------
43 // Control Button
44 //---------------------------------------------------------------------------
45 ControlButton::ControlButton( string id, bool visible, int x, int y, string Up,
46     string Down, string Disabled, string click, string tooltiptext, string help,
47     Window *Parent ) : GenericControl( id, visible, help, Parent )
48 {
49     Left            = x;
50     Top             = y;
51     State           = 1;                   // 1 = up - 0 = down
52     Selected        = false;
53     Enabled         = true;
54     ClickActionName = click;
55     this->Up        = Up;
56     this->Down      = Down;
57     this->Disabled  = Disabled;
58
59     ToolTipText = tooltiptext;
60 }
61 //---------------------------------------------------------------------------
62 ControlButton::~ControlButton()
63 {
64 }
65 //---------------------------------------------------------------------------
66 void ControlButton::Init()
67 {
68     // Init bitmaps
69     Img = new (Bitmap*)[3];
70     Img[0] = p_intf->p_sys->p_theme->BmpBank->Get( Up );
71     Img[1] = p_intf->p_sys->p_theme->BmpBank->Get( Down );
72     if( Disabled == "none" )
73         Img[2] = p_intf->p_sys->p_theme->BmpBank->Get( Up );
74     else
75         Img[2] = p_intf->p_sys->p_theme->BmpBank->Get( Disabled );
76
77     // Get size of control
78     Img[0]->GetSize( Width, Height );
79
80     // Create script
81     ClickAction = new Action( p_intf, ClickActionName );
82 }
83 //---------------------------------------------------------------------------
84 bool ControlButton::ProcessEvent( Event *evt )
85 {
86     switch( evt->GetMessage() )
87     {
88         case CTRL_ENABLED:
89             Enable( (Event*)evt->GetParam1(), (bool)evt->GetParam2() );
90             break;
91     }
92     return false;
93 }
94 //---------------------------------------------------------------------------
95 void ControlButton::MoveRelative( int xOff, int yOff )
96 {
97     Left += xOff;
98     Top  += yOff;
99 }
100 //---------------------------------------------------------------------------
101 void ControlButton::Draw( int x, int y, int w, int h, Graphics *dest )
102 {
103     if( !Visible )
104         return;
105
106     int xI, yI, wI, hI;
107     if( GetIntersectRgn( x,y,w,h,Left,Top,Width,Height, xI, yI, wI, hI ) )
108     {
109         // Button is in down state
110         if( State == 0 && Enabled )
111             Img[1]->DrawBitmap( xI-Left, yI-Top, wI, hI, xI-x, yI-y, dest );
112
113         // Button is in up state
114         if( State == 1 && Enabled )
115             Img[0]->DrawBitmap( xI-Left, yI-Top, wI, hI, xI-x, yI-y, dest );
116
117         // Button is disabled
118         if( !Enabled )
119             Img[2]->DrawBitmap( xI-Left, yI-Top, wI, hI, xI-x, yI-y, dest );
120     }
121 }
122 //---------------------------------------------------------------------------
123 bool ControlButton::MouseUp( int x, int y, int button )
124 {
125     if( !Enabled )
126         return false;
127
128     if( Img[1]->Hit( x - Left, y - Top ) && button == 1 && Selected )
129     {
130         State = 1;
131         Selected = false;
132         ClickAction->SendEvent();
133         ParentWindow->Refresh( Left, Top, Width, Height );
134         return true;
135     }
136
137     if( button == 1 )
138         Selected = false;
139
140     return false;
141 }
142 //---------------------------------------------------------------------------
143 bool ControlButton::MouseDown( int x, int y, int button )
144 {
145     if( !Enabled )
146         return false;
147
148     if( Img[0]->Hit( x - Left, y - Top ) && button == 1 )
149     {
150         State = 0;
151         Selected = true;
152         ParentWindow->Refresh( Left, Top, Width, Height );
153         return true;
154     }
155     return false;
156 }
157 //---------------------------------------------------------------------------
158 bool ControlButton::MouseMove( int x, int y, int button )
159 {
160     if( !Enabled || !Selected || !button )
161         return false;
162
163     if( MouseOver( x, y ) )
164     {
165         if( State == 1 )
166         {
167             State = 0;
168             ParentWindow->Refresh( Left, Top, Width, Height );
169         }
170     }
171     else
172     {
173         if( State == 0 )
174         {
175             State = 1;
176             ParentWindow->Refresh( Left, Top, Width, Height );
177         }
178     }
179     return true;
180 }
181 //---------------------------------------------------------------------------
182 bool ControlButton::MouseOver( int x, int y )
183 {
184     if( Img[1 - State]->Hit( x - Left, y - Top ) )
185     {
186         return true;
187     }
188     else
189     {
190         return false;
191     }
192 }
193 //---------------------------------------------------------------------------
194 bool ControlButton::ToolTipTest( int x, int y )
195 {
196     if( MouseOver( x, y ) && Enabled )
197     {
198         ParentWindow->ChangeToolTipText( ToolTipText );
199         return true;
200     }
201     return false;
202 }
203 //---------------------------------------------------------------------------
204 void ControlButton::Enable( Event *event, bool enabled )
205 {
206     if( !ClickAction->MatchEvent( event, ACTION_MATCH_ONE ) )
207         return;
208
209     if( enabled != Enabled )
210     {
211         Enabled = enabled;
212         ParentWindow->Refresh( Left, Top, Width, Height );
213     }
214 }
215 //---------------------------------------------------------------------------
216