]> git.sesse.net Git - vlc/blob - modules/gui/skins/controls/button.cpp
* Fixed dewtructor
[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.9 2003/04/16 21:40:07 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 "../src/bitmap.h"
32 #include "../src/banks.h"
33 #include "generic.h"
34 #include "button.h"
35 #include "../src/event.h"
36 #include "../src/theme.h"
37 #include "../src/window.h"
38 #include "../src/skin_common.h"
39
40 //---------------------------------------------------------------------------
41 // Control Button
42 //---------------------------------------------------------------------------
43 ControlButton::ControlButton(
44     string id,
45     bool visible,
46     int x, int y,
47     string Up, string Down, string Disabled,
48     string onclick, string onmouseover, string onmouseout,
49     string tooltiptext, string help,
50     Window *Parent ) : GenericControl( id, visible, help, Parent )
51 {
52     // General
53     Left            = x;
54     Top             = y;
55     State           = 1;                   // 1 = up - 0 = down
56     Selected        = false;
57     Enabled         = true;
58     CursorIn        = false;
59     this->Up        = Up;
60     this->Down      = Down;
61     this->Disabled  = Disabled;
62
63     // Actions
64     ClickActionName     = onclick;
65     MouseOverActionName = onmouseover;
66     MouseOutActionName  = onmouseout;
67
68     // Texts
69     ToolTipText = tooltiptext;
70 }
71 //---------------------------------------------------------------------------
72 ControlButton::~ControlButton()
73 {
74 }
75 //---------------------------------------------------------------------------
76 void ControlButton::Init()
77 {
78     // Init bitmaps
79     Img = new (Bitmap*)[3];
80     Img[0] = p_intf->p_sys->p_theme->BmpBank->Get( Up );
81     Img[1] = p_intf->p_sys->p_theme->BmpBank->Get( Down );
82     if( Disabled == "none" )
83         Img[2] = p_intf->p_sys->p_theme->BmpBank->Get( Up );
84     else
85         Img[2] = p_intf->p_sys->p_theme->BmpBank->Get( Disabled );
86
87     // Get size of control
88     Img[0]->GetSize( Width, Height );
89
90     // Create script
91     ClickAction     = new Action( p_intf, ClickActionName );
92     MouseOverAction = new Action( p_intf, MouseOverActionName );
93     MouseOutAction  = new Action( p_intf, MouseOutActionName );
94 }
95 //---------------------------------------------------------------------------
96 bool ControlButton::ProcessEvent( Event *evt )
97 {
98     switch( evt->GetMessage() )
99     {
100         case CTRL_ENABLED:
101             Enable( (Event*)evt->GetParam1(), (bool)evt->GetParam2() );
102             break;
103     }
104     return false;
105 }
106 //---------------------------------------------------------------------------
107 void ControlButton::MoveRelative( int xOff, int yOff )
108 {
109     Left += xOff;
110     Top  += yOff;
111 }
112 //---------------------------------------------------------------------------
113 void ControlButton::Draw( int x, int y, int w, int h, Graphics *dest )
114 {
115     if( !Visible )
116         return;
117
118     int xI, yI, wI, hI;
119     if( GetIntersectRgn( x,y,w,h,Left,Top,Width,Height, xI, yI, wI, hI ) )
120     {
121         // Button is in down state
122         if( State == 0 && Enabled )
123             Img[1]->DrawBitmap( xI-Left, yI-Top, wI, hI, xI-x, yI-y, dest );
124
125         // Button is in up state
126         if( State == 1 && Enabled )
127             Img[0]->DrawBitmap( xI-Left, yI-Top, wI, hI, xI-x, yI-y, dest );
128
129         // Button is disabled
130         if( !Enabled )
131             Img[2]->DrawBitmap( xI-Left, yI-Top, wI, hI, xI-x, yI-y, dest );
132     }
133 }
134 //---------------------------------------------------------------------------
135 bool ControlButton::MouseUp( int x, int y, int button )
136 {
137     // If hit in the button
138     if( Img[1]->Hit( x - Left, y - Top ) )
139     {
140         if( !Enabled )
141             return true;
142
143         if( button == 1 && Selected )
144         {
145             State = 1;
146             Selected = false;
147             ClickAction->SendEvent();
148             ParentWindow->Refresh( Left, Top, Width, Height );
149             return true;
150         }
151     }
152
153     if( button == 1 )
154         Selected = false;
155
156     return false;
157 }
158 //---------------------------------------------------------------------------
159 bool ControlButton::MouseDown( int x, int y, int button )
160 {
161     if( Img[0]->Hit( x - Left, y - Top ) )
162     {
163         if( !Enabled )
164             return true;
165
166         if( button == 1 )
167         {
168             State = 0;
169             Selected = true;
170             ParentWindow->Refresh( Left, Top, Width, Height );
171             return true;
172         }
173     }
174
175     return false;
176 }
177 //---------------------------------------------------------------------------
178 bool ControlButton::MouseMove( int x, int y, int button )
179 {
180     if( !Enabled )
181         return false;
182
183
184     if( MouseOver( x, y ) && !CursorIn )
185     {
186         if( button == 1 && Selected )
187         {
188             State = 0;
189             ParentWindow->Refresh( Left, Top, Width, Height );
190         }
191
192         if( MouseOverActionName != "none" )
193         {
194             MouseOverAction->SendEvent();
195         }
196
197         CursorIn = true;
198         return true;
199     }
200     else if( !MouseOver( x, y ) & CursorIn )
201     {
202
203         if( button == 1 && Selected )
204         {
205             State = 1;
206             ParentWindow->Refresh( Left, Top, Width, Height );
207         }
208
209         if( MouseOutActionName != "none" )
210         {
211             MouseOutAction->SendEvent();
212         }
213
214         CursorIn = false;
215         return true;
216     }
217
218     return false;
219 }
220 //---------------------------------------------------------------------------
221 bool ControlButton::MouseOver( int x, int y )
222 {
223     if( Img[1 - State]->Hit( x - Left, y - Top ) )
224     {
225         return true;
226     }
227     else
228     {
229         return false;
230     }
231 }
232 //---------------------------------------------------------------------------
233 bool ControlButton::ToolTipTest( int x, int y )
234 {
235     if( MouseOver( x, y ) && Enabled )
236     {
237         ParentWindow->ChangeToolTipText( ToolTipText );
238         return true;
239     }
240     return false;
241 }
242 //---------------------------------------------------------------------------
243 void ControlButton::Enable( Event *event, bool enabled )
244 {
245     if( !ClickAction->MatchEvent( event, ACTION_MATCH_ONE ) )
246         return;
247
248     if( enabled != Enabled )
249     {
250         Enabled = enabled;
251
252         // If cursor is in, send mouse out event
253         if( !Enabled && CursorIn )
254         {
255             if( MouseOutActionName != "none" )
256                 MouseOutAction->SendEvent();
257             CursorIn = false;
258         }
259
260         ParentWindow->Refresh( Left, Top, Width, Height );
261     }
262 }
263 //---------------------------------------------------------------------------
264