summaryrefslogtreecommitdiffstats
path: root/src/VButton.cpp
blob: c0ce411808689fb6643199128a8895624fc33c5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "VButton.h"
#include <tqvbox.h>
#include <tqfont.h>
#include <tdeconfig.h>
#include <tdeapplication.h>
#include "Xutils.h"
#include <X11/XKBlib.h>


double VButton::pw=552.0;
double VButton::ph=235.0;

VButton::VButton(TQWidget *parent, const char *name): TQPushButton (parent,name)
{
	TDEConfig *cfg = TDEApplication::kApplication()->config();
	TQString keysC = cfg->readEntry("keysColor", "#f0f0f0");
	setColor(TQColor(keysC));
	setFocusPolicy(TQ_NoFocus);
	resize(30,30);
	press=false;
}

VButton::~VButton()
{
}

void VButton::setupTexts(Display *display)
{
	// normal text
	KeySym keysym_c = XkbKeycodeToKeysym(display, keycode, 0, 0);
	TQChar nc((uint)keysym2ucs(keysym_c));
	if (nc == '&')
	{
		setText("&&");
	}
	else
	{
		setText(nc);
	}

	// shift text
	keysym_c = XkbKeycodeToKeysym(display, keycode, 0, 1);
	TQChar sc = (uint)keysym2ucs(keysym_c);
	if (sc == '&')
	{
		setShiftText("&&");
	}
	else
	{
		setShiftText(sc);
	}

	// altGr text
	keysym_c = XkbKeycodeToKeysym(display, keycode, 0, 2);
	TQChar c = (uint)keysym2ucs(keysym_c);
	if (c == " ")
	{
		// use normal text in case altGr does not provide extra characters.
		// This is required at least on US keyboards, where altGr works as Alt.
		c = nc;
	}
	if (c == '&')
	{
		altGrText = "&&";
	}
	else
	{
		altGrText = c;
	}

	// altGr + shift text
	keysym_c = XkbKeycodeToKeysym(display, keycode, 0, 3);
	c = (uint)keysym2ucs(keysym_c);
	if (c == " ")
	{
		// use shift text in case altGr does not provide extra characters.
		// This is required at least on US keyboards, where altGr works as Alt.
		c = sc;
	}
	if (c == '&')
	{
		altGrShiftText = "&&";
	}
	else
	{
		altGrShiftText = c;
	}
}

void VButton::shiftCapsAltGrPressed(bool shift, bool caps, bool altGrState)
{
	if (altGrState)
	{
		// If Caps is on, displayed alpha characters should be toggled from the AltGr case
		if (caps)
		{
			if (shift)
			{
				TQPushButton::setText(altGrShiftText.lower());
			}
			else
			{
				TQPushButton::setText(altGrText.upper());
			}
		}
		else if (shift)
		{
			TQPushButton::setText(altGrShiftText);
		}
		else
		{
			TQPushButton::setText(altGrText);
		}
	}
	else
	{
		if (isAlpha)
		{
			// Alpha button, both shift and caps affect its state
			if (caps && shift)
			{
				TQPushButton::setText(capsShiftText);
			}
			else if (caps)
			{
				TQPushButton::setText(capsText);
			}
			else if (shift)
			{
				TQPushButton::setText(shiftText);
			}
			else
			{
				TQPushButton::setText(normalText);
			}
		}
		else
		{
			// Non alpha button, only shift affects its state
			if (shift)
			{
				TQPushButton::setText(shiftText);
			}
			else
			{
				TQPushButton::setText(normalText);
			}
		}
	}
}

void VButton::setText(const TQString& text)
{
	// Need to set the pushbutton text to correctly handle those buttons which usually
	// don't change (tab, shift, caps, Fn, ...)
	TQPushButton::setText(text);
	normalText = text;
	capsText = text.upper();
	isAlpha = text.length() == 1 && capsText != normalText;
}

void VButton::setShiftText(const TQString& text)
{
	shiftText = text;
	// 'text' is normally uppercase here, so in most cases we need to set capsShiftText
	// to text.lower() to handle caps+shift correctly. Special cases like ç and Ç are
	// handled by using text.upper()
	if (text.lower() != text)
	{
		capsShiftText = text.lower();
	}
	else
	{
		capsShiftText = text.upper();
	}
}

void VButton::setColor(const TQColor &color)
{
	setPaletteBackgroundColor(color);
	// Need to set TQColorGroup::Button color as well, otherwise the actual
	// color of the key does not change until the next restart of the application.
	TQPalette plt = palette();
	plt.setColor(TQPalette::Active,   TQColorGroup::Button, color);
	plt.setColor(TQPalette::Inactive, TQColorGroup::Button, color);
	plt.setColor(TQPalette::Disabled, TQColorGroup::Button, color);
	setPalette(plt);
}

void VButton::setKeyCode(unsigned int keycode)
{
	this->keycode=keycode;
}

unsigned int VButton::getKeyCode()
{
	return this->keycode;
}

void VButton::sendKey()
{
	emit keyClick(keycode);
}

void VButton::reposition(int width, int height)
{
	double dx=pw/orig_size.x();
	double dy=ph/orig_size.y();
	double sdx=pw/orig_size.width();
	double sdy=ph/orig_size.height();
	move((int)(width/dx),(int)(height/dy));
	resize((int)(width/sdx), (int)(height/sdy));
}

void VButton::res()
{
	orig_size=geometry();
}

void VButton::enterEvent(TQEvent *e)
{
	TQPushButton::enterEvent(e);
	inside = true;
}

void VButton::leaveEvent(TQEvent *e)
{
	TQPushButton::leaveEvent(e);
	inside = false;
}

void VButton::mousePressEvent(TQMouseEvent *e)
{
	press=true;
	TQPushButton::mousePressEvent(e);
	if (!isToggleButton())
	{
		// non toggle buttons need to send the key here
		sendKey();
		startTimer(500);
	}
}

void VButton::mouseReleaseEvent(TQMouseEvent *e)
{
	press=false;
	TQPushButton::mouseReleaseEvent(e);
	if (hitButton(e->pos()) && isToggleButton())
	{
		// toggle buttons need to send the key here because the underlying pushbutton state
		// is only updated on mouse release
		sendKey();
	}
	killTimers();
	inrpt=false;                                                                        
}

void VButton::timerEvent(TQTimerEvent *)
{
	if (!press)
	{
		inrpt=false;
		return;
	}
	if (press && !inrpt)
	{
		inrpt=true;
		startTimer(120);
		return;
	}
	if (inside)
	{
		// Send key only if the mouse is still inside the button rectangle
		sendKey();
	}
}

#include "VButton.moc"