1 // ----------------------------------------------------------------------------
2 //
3 //  Copyright (C) 2008-2015 Fons Adriaensen <fons@linuxaudio.org>
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 //
18 // ----------------------------------------------------------------------------
19 
20 
21 #include "button.h"
22 
23 
24 int PushButton::_keymod = 0;
25 int PushButton::_button = 0;
26 
27 
PushButton(X_window * parent,X_callback * cbobj,int cbind,XImage * image,int xp,int yp,int xs,int ys)28 PushButton::PushButton (X_window     *parent,
29                         X_callback   *cbobj,
30 			int           cbind,
31                         XImage       *image,
32 		        int  xp,
33                         int  yp,
34 			int  xs,
35 			int  ys) :
36     X_window (parent, xp, yp, xs, ys, 0),
37     _cbobj (cbobj),
38     _cbind (cbind),
39     _image (image),
40     _state (0),
41     _xs (xs),
42     _ys (ys)
43 {
44     x_add_events (ExposureMask | ButtonPressMask | ButtonReleaseMask);
45 }
46 
47 
~PushButton(void)48 PushButton::~PushButton (void)
49 {
50 }
51 
52 
init(X_display * disp)53 void PushButton::init (X_display *disp)
54 {
55 }
56 
57 
fini(void)58 void PushButton::fini (void)
59 {
60 }
61 
62 
handle_event(XEvent * E)63 void PushButton::handle_event (XEvent *E)
64 {
65     switch (E->type)
66     {
67     case Expose:
68 	render ();
69 	break;
70 
71     case ButtonPress:
72 	bpress ((XButtonEvent *) E);
73 	break;
74 
75     case ButtonRelease:
76 	brelse ((XButtonEvent *) E);
77 	break;
78 
79     default:
80 	fprintf (stderr, "PushButton: event %d\n", E->type );
81     }
82 }
83 
84 
bpress(XButtonEvent * E)85 void PushButton::bpress (XButtonEvent *E)
86 {
87     int r = 0;
88 
89     if (E->button < 4)
90     {
91         _keymod = E->state;
92         _button = E->button;
93         r = handle_press ();
94     }
95     render ();
96     if (r) callback (r);
97 }
98 
99 
brelse(XButtonEvent * E)100 void PushButton::brelse (XButtonEvent *E)
101 {
102     int r = 0;
103 
104     if (E->button < 4)
105     {
106         _keymod = E->state;
107         _button = E->button;
108         r = handle_relse ();
109     }
110     render ();
111     if (r) callback (r);
112 }
113 
114 
set_state(int s)115 void PushButton::set_state (int s)
116 {
117     if (_state != s)
118     {
119         _state = s;
120         render ();
121     }
122 }
123 
124 
render(void)125 void PushButton::render (void)
126 {
127     XSetFunction (dpy (), dgc (), GXcopy);
128     XPutImage (dpy (), win (), dgc (), _image, 0, _state * _ys, 0, 0, _xs, _ys);
129 }
130 
131 
132