2 * Copyright (c) 2004 Gunnar Schmi Dt <gunnar@schmi-dt.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
20 #include <qdrawutil.h>
23 #include <q3popupmenu.h>
25 #include <QMouseEvent>
26 #include <QTimerEvent>
27 #include <QResizeEvent>
29 #include <kaboutapplication.h>
31 #include <kaboutdata.h>
34 #include <kglobalsettings.h>
35 #include <kapplication.h>
36 #include <kiconloader.h>
37 #include <kiconeffect.h>
38 #include <kshortcut.h>
39 #include <kdemacros.h>
41 #include "kdeexportfix.h"
43 #include "kbstate.moc"
49 #include <X11/Xutil.h>
50 #include <X11/XKBlib.h>
53 #include <X11/keysymdef.h>
54 #include <X11/extensions/XKB.h>
55 #include <ktoolinvocation.h>
57 KDE_EXPORT KPanelApplet
* init(QWidget
*parent
, const QString
& configFile
)
59 KGlobal::locale()->insertCatalog("kbstateapplet");
60 KbStateApplet
*applet
= new KbStateApplet(configFile
, Plasma::Normal
, Plasma::About
, parent
, "kbstateapplet");
66 const unsigned int mask
;
71 const bool isModifier
;
74 static ModifierKey modifierKeys
[] = {
75 { ShiftMask
, 0, I18N_NOOP("Shift"), "shiftkey", "", true },
76 { ControlMask
, 0, I18N_NOOP("Control"), "controlkey", "", true },
77 { 0, XK_Alt_L
, I18N_NOOP("Alt"), "altkey", "", true },
78 { 0, 0, I18N_NOOP("Win"), "superkey", "", true },
79 { 0, XK_Meta_L
, I18N_NOOP("Meta"), "metakey", "", true },
80 { 0, XK_Super_L
, I18N_NOOP("Super"), "superkey", "", true },
81 { 0, XK_Hyper_L
, I18N_NOOP("Hyper"), "hyperkey", "", true },
82 { 0, 0, I18N_NOOP("Alt Graph"), "", I18N_NOOP("æ"), true },
83 { 0, XK_Num_Lock
, I18N_NOOP("Num Lock"), "lockkey", I18N_NOOP("Num"), false },
84 { LockMask
, 0, I18N_NOOP("Caps Lock"), "capskey", "", false },
85 { 0, XK_Scroll_Lock
, I18N_NOOP("Scroll Lock"), "lockkey", I18N_NOOP("Scroll"), false },
86 { 0, 0, "", "", "", false }
90 /********************************************************************/
92 KbStateApplet::KbStateApplet(const QString
& configFile
, Plasma::Type t
, int actions
,
93 QWidget
*parent
, const char *name
)
94 : KPanelApplet( configFile
, t
, actions
, parent
)
96 for (int i
= 0; i
< 8; i
++) {
99 instance
= new KInstance ("kbstateapplet");
102 mouse
= new MouseIcon (instance
, this, "mouse");
103 sticky
= new TimeoutIcon (instance
, "", "kbstate_stickykeys", this, "sticky");
104 slow
= new TimeoutIcon (instance
, "", "kbstate_slowkeys", this, "slow");
105 bounce
= new TimeoutIcon (instance
, "", "", this, "bounce");
107 xkb
= XkbGetMap(QX11Info::display(), 0, XkbUseCoreKbd
);
110 XkbGetControls (QX11Info::display(), XkbAllControlsMask
, xkb
);
112 accessxFeatures
= xkb
->ctrls
->enabled_ctrls
;
119 //startTimer(100); // ten times a second
120 connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), SLOT(paletteChanged()));
122 kapp
->installX11EventFilter (this);
123 int opcode_rtn
, error_rtn
;
124 XkbQueryExtension (QX11Info::display(), &opcode_rtn
, &xkb_base_event_type
, &error_rtn
, NULL
, NULL
);
125 XkbSelectEvents (QX11Info::display(), XkbUseCoreKbd
, XkbAllEventsMask
,
131 KbStateApplet::~KbStateApplet() {
132 kapp
->removeX11EventFilter (this);
138 // Builds, connects _popup menu
139 void KbStateApplet::buildPopupMenu()
141 sizePopup
=new KMenu(this);
142 sizePopup
->setCheckable( true );
143 sizePopup
->insertItem(i18n("Small"), 13);
144 sizePopup
->insertItem(i18n("Medium"), 20);
145 sizePopup
->insertItem(i18n("Large"), 26);
146 connect(sizePopup
,SIGNAL(activated(int)), this, SLOT(setIconDim(int)));
148 showPopup
=new KMenu(this);
149 showPopup
->setCheckable( true );
150 modifierItem
=showPopup
->insertItem(i18n("Modifier Keys"), this, SLOT(toggleModifier()));
151 lockkeysItem
=showPopup
->insertItem(i18n("Lock Keys"), this, SLOT(toggleLockkeys()));
152 mouseItem
=showPopup
->insertItem(i18n("Mouse Status"), this, SLOT(toggleMouse()));
153 accessxItem
=showPopup
->insertItem(i18n("AccessX Status"), this, SLOT(toggleAccessX()));
155 popup
= new KMenu(this);
156 popup
->setCheckable( true );
157 popup
->addTitle(i18n("Keyboard Status Applet"));
158 popup
->insertItem(i18n("Set Icon Size"),sizePopup
);
159 fillSpaceItem
= popup
->insertItem(i18n("Fill Available Space"),
160 this, SLOT(toggleFillSpace()));
161 popup
->insertItem(i18n("Show"),showPopup
);
162 popup
->insertItem(i18n("Configure AccessX Features..."), this, SLOT(configureAccessX()));
163 popup
->insertItem(i18n("Configure Keyboard..."), this, SLOT(configureKeyboard()));
164 popup
->insertItem(i18n("Configure Mouse..."), this, SLOT(configureMouse()));
165 popup
->insertSeparator();
166 popup
->insertItem(i18n("About"), this, SLOT(about()));
167 setCustomMenu(popup
);
171 void KbStateApplet::updateMenu()
173 showPopup
->setItemChecked (modifierItem
, showModifiers
);
174 showPopup
->setItemChecked (lockkeysItem
, showLockkeys
);
175 showPopup
->setItemChecked (mouseItem
, showMouse
);
176 showPopup
->setItemChecked (accessxItem
, showAccessX
);
177 popup
->setItemChecked (fillSpaceItem
, fillSpace
);
178 sizePopup
->setItemChecked(13, size
== 13);
179 sizePopup
->setItemChecked(20, size
== 20);
180 sizePopup
->setItemChecked(26, size
== 26);
184 void calculateSizes (int space
, int modifiers
, int lockkeys
, int accessx
,
185 bool showMouse
, int &lines
, int &length
, int &size
)
186 // Calculates the layout based on a given number of modifiers, lockkeys and
189 // lines: number of lines
190 // length: number of icons per line
191 // size: size of the icons
193 // Calculate lines and length
197 lines
= space
>=size
? space
/size
: 1;
198 length
= modifiers
+ lockkeys
+ accessx
;
200 if (length
> 0 && lines
>= 2) {
201 length
= (length
+ lines
-1)/lines
;
203 // As we want to have some line breaks, we need to do some corrections:
204 // Calculate the number of lines that are really needed:
205 int linesNeeded
= (modifiers
+length
-1)/length
+ (lockkeys
+length
-1)/length
;
206 int tmp1
= modifiers
%length
== 0 ? 0 : length
- modifiers
%length
;
207 int tmp2
= lockkeys
%length
== 0 ? 0 : length
- lockkeys
%length
;
208 if ((tmp1
+ tmp2
) < accessx
)
209 linesNeeded
= (modifiers
+lockkeys
+accessx
+ length
-1)/length
;
211 // If we need more lines than we have, try with more icons per line:
212 while (linesNeeded
> lines
) {
214 linesNeeded
= (modifiers
+length
-1)/length
+ (lockkeys
+length
-1)/length
;
215 int tmp1
= modifiers
%length
== 0 ? 0 : length
- modifiers
%length
;
216 int tmp2
= lockkeys
%length
== 0 ? 0 : length
- lockkeys
%length
;
217 if ((tmp1
+ tmp2
) < accessx
)
218 linesNeeded
= (modifiers
+lockkeys
+accessx
+ length
-1)/length
;
224 int KbStateApplet::widthForHeight(int h
) const {
226 int size
= this->size
;
229 if ((accessxFeatures
& XkbStickyKeysMask
) != 0)
231 if ((accessxFeatures
& XkbSlowKeysMask
) != 0)
233 if ((accessxFeatures
& XkbBounceKeysMask
) != 0)
236 calculateSizes (h
, showModifiers
?modifiers
.count():0,
237 showLockkeys
?lockkeys
.count():0,
238 showAccessX
?accessx
:0,
239 showMouse
, lines
, length
, size
);
247 int KbStateApplet::heightForWidth(int w
) const {
249 int size
= this->size
;
252 if ((accessxFeatures
& XkbStickyKeysMask
) != 0)
254 if ((accessxFeatures
& XkbSlowKeysMask
) != 0)
256 if ((accessxFeatures
& XkbBounceKeysMask
) != 0)
259 calculateSizes (w
, showModifiers
?modifiers
.count():0,
260 showLockkeys
?lockkeys
.count():0,
261 showAccessX
?accessx
:0,
262 showMouse
, lines
, length
, size
);
270 void KbStateApplet::mousePressEvent(QMouseEvent
*e
) {
271 if (e
->button() == Qt::RightButton
)
272 popup
->popup(e
->globalPos());
275 void KbStateApplet::setIconDim (int size
) {
284 void KbStateApplet::resizeEvent( QResizeEvent
*e
) {
285 QWidget::resizeEvent(e
);
289 void KbStateApplet::toggleFillSpace() {
290 fillSpace
= !fillSpace
;
298 void KbStateApplet::layout() {
299 int size
= this->size
;
301 int lines
, length
, x
,y
,dx
,dy
, ldx
,ldy
;
302 int modifierCount
= showModifiers
?modifiers
.count():0;
303 int lockkeyCount
= showLockkeys
?lockkeys
.count():0;
304 int accessxCount
= 0;
307 if ((accessxFeatures
& XkbStickyKeysMask
) != 0)
309 if ((accessxFeatures
& XkbSlowKeysMask
) != 0)
311 if ((accessxFeatures
& XkbBounceKeysMask
) != 0)
315 if (orientation() == Qt::Vertical
) {
316 calculateSizes (width(), modifierCount
, lockkeyCount
, accessxCount
,
317 showMouse
, lines
, length
, size
);
320 size
= width()/lines
;
322 x
= (width()-lines
*size
)/2;
330 calculateSizes (height(), modifierCount
, lockkeyCount
, accessxCount
,
331 showMouse
, lines
, length
, size
);
334 size
= height()/lines
;
337 y
= (height()-lines
*size
)/2;
346 for (icon
= modifiers
.first(); icon
; icon
= modifiers
.next()) {
348 icon
->setGeometry (x
, y
, size
, size
);
351 item
++; x
+=dx
; y
+=dy
;
353 x
= x
- length
*dx
+ ldx
;
354 y
= y
- length
*dy
+ ldy
;
362 int lockkeyLines
= (lockkeyCount
+length
-1)/length
;
363 int accessxLines
= lines
- (modifierCount
+length
-1)/length
- lockkeyLines
;
368 if (lockkeyLines
*length
+ accessxLines
*length
369 >= lockkeyCount
+ accessxCount
)
371 if (lines
> 1 && item
> 1) {
372 x
= x
- (item
-1)*dx
+ ldx
;
373 y
= y
- (item
-1)*dy
+ ldy
;
381 if (showMouse
&& showAccessX
&& accessxLines
> 0) {
382 mouse
->setGeometry (x
, y
, size
, size
);
386 item
++; x
+=dx
; y
+=dy
;
388 x
= x
- length
*dx
+ ldx
;
389 y
= y
- length
*dy
+ ldy
;
397 if ((accessxFeatures
& XkbStickyKeysMask
) != 0
398 && showAccessX
&& accessxLines
> 0)
400 sticky
->setGeometry (x
, y
, size
, size
);
404 item
++; x
+=dx
; y
+=dy
;
406 x
= x
- length
*dx
+ ldx
;
407 y
= y
- length
*dy
+ ldy
;
415 if ((accessxFeatures
& XkbSlowKeysMask
) != 0
416 && showAccessX
&& accessxLines
> 0)
418 slow
->setGeometry (x
, y
, size
, size
);
422 item
++; x
+=dx
; y
+=dy
;
424 x
= x
- length
*dx
+ ldx
;
425 y
= y
- length
*dy
+ ldy
;
433 if ((accessxFeatures
& XkbBounceKeysMask
) != 0
434 && showAccessX
&& accessxLines
> 0)
436 bounce
->setGeometry (x
, y
, size
, size
);
440 item
++; x
+=dx
; y
+=dy
;
442 x
= x
- length
*dx
+ ldx
;
443 y
= y
- length
*dy
+ ldy
;
453 x
= x
- (item
-1)*dx
+ ldx
;
454 y
= y
- (item
-1)*dy
+ ldy
;
458 for (icon
= lockkeys
.first(); icon
; icon
= lockkeys
.next()) {
460 icon
->setGeometry (x
, y
, size
, size
);
463 item
++; x
+=dx
; y
+=dy
;
465 x
= x
- length
*dx
+ ldx
;
466 y
= y
- length
*dy
+ ldy
;
474 if ((accessxFeatures
& XkbBounceKeysMask
) != 0
475 && showAccessX
&& accessxCount
> 0)
477 bounce
->setGeometry (x
, y
, size
, size
);
480 item
++; x
+=dx
; y
+=dy
;
484 if ((accessxFeatures
& XkbSlowKeysMask
) != 0
485 && showAccessX
&& accessxCount
> 0)
487 slow
->setGeometry (x
, y
, size
, size
);
490 item
++; x
+=dx
; y
+=dy
;
494 if ((accessxFeatures
& XkbStickyKeysMask
) != 0
495 && showAccessX
&& accessxCount
> 0)
497 sticky
->setGeometry (x
, y
, size
, size
);
500 item
++; x
+=dx
; y
+=dy
;
504 if (showMouse
&& accessxCount
> 0)
506 mouse
->setGeometry (x
, y
, size
, size
);
509 item
++; x
+=dx
; y
+=dy
;
514 void KbStateApplet::paletteChanged() {
515 for (int i
= 0; i
< 8; i
++) {
517 icons
[i
]->updateImages();
525 void KbStateApplet::initMasks() {
526 for (int i
= 0; i
< 8; i
++) {
533 for (int i
= 0; strcmp(modifierKeys
[i
].name
, "") != 0; i
++) {
534 int mask
= modifierKeys
[i
].mask
;
536 if (modifierKeys
[i
].keysym
!= 0)
537 mask
= XkbKeysymToModifiers (this->x11Display(), modifierKeys
[i
].keysym
);
539 #warning "kde4: how to port it ?"
542 else if (strcmp(modifierKeys
[i
].name
, "Win") == 0)
543 mask
= KKeyNative::modXWin();
546 mask
= XkbKeysymToModifiers (this->x11Display(), XK_Mode_switch
)
547 | XkbKeysymToModifiers (this->x11Display(), XK_ISO_Level3_Shift
)
548 | XkbKeysymToModifiers (this->x11Display(), XK_ISO_Level3_Latch
)
549 | XkbKeysymToModifiers (this->x11Display(), XK_ISO_Level3_Lock
);
552 for (map
= 0; map
< 8; map
++) {
553 if ((mask
& (1 << map
)) != 0)
556 if ((map
<= 7) && !(icons
[map
])) {
557 icons
[map
] = new KeyIcon (i
, instance
, this, modifierKeys
[i
].name
);
558 icons
[map
]->setToolTip( i18n (modifierKeys
[i
].name
));
559 connect (icons
[map
], SIGNAL(stateChangeRequest (KeyIcon
*,bool,bool)),
560 SLOT(stateChangeRequest (KeyIcon
*,bool,bool)));
561 if (modifierKeys
[i
].isModifier
)
562 modifiers
.append(icons
[map
]);
564 lockkeys
.append(icons
[map
]);
569 bool KbStateApplet::x11Event (XEvent
*evt
) {
570 if (evt
->type
== xkb_base_event_type
+ XkbEventCode
) {
571 XkbEvent
*kbevt
= (XkbEvent
*)evt
;
572 switch (kbevt
->any
.xkb_type
) {
576 mouse
->setState (kbevt
->state
.ptr_buttons
);
578 case XkbAccessXNotify
:
579 switch (kbevt
->accessx
.detail
) {
581 slow
->setGlyth(i18nc("a (the first letter in the alphabet)", "a"));
582 slow
->setImage("unlatched");
584 case XkbAXN_SKAccept
:
585 slow
->setImage("keypressok");
587 case XkbAXN_SKRelease
:
589 slow
->setImage("kbstate_slowkeys");
591 case XkbAXN_SKReject
:
592 slow
->setImage("keypressno", kbevt
->accessx
.sk_delay
>150?kbevt
->accessx
.sk_delay
:150);
594 case XkbAXN_BKAccept
:
595 slow
->setGlyth(i18nc("a (the first letter in the alphabet)", "a"));
596 bounce
->setImage("keypressok", kbevt
->accessx
.sk_delay
>150?kbevt
->accessx
.sk_delay
:150);
598 case XkbAXN_BKReject
:
599 slow
->setGlyth(i18nc("a (the first letter in the alphabet)", "a"));
600 bounce
->setImage("keypressno", kbevt
->accessx
.sk_delay
>150?kbevt
->accessx
.sk_delay
:150);
604 case XkbControlsNotify
: {
605 XkbControlsNotifyEvent
* event
= (XkbControlsNotifyEvent
*)evt
;
606 accessxFeatures
= event
->enabled_ctrls
;
608 if ((accessxFeatures
& XkbMouseKeysMask
) != 0) {
609 XkbGetControls (QX11Info::display(), XkbMouseKeysMask
, xkb
);
610 if (xkb
->ctrls
->mk_dflt_btn
< 1)
611 mouse
->setActiveKey (1);
612 else if (xkb
->ctrls
->mk_dflt_btn
> 3)
613 mouse
->setActiveKey (1);
615 mouse
->setActiveKey (xkb
->ctrls
->mk_dflt_btn
);
618 mouse
->setActiveKey (0);
625 case XkbExtensionDeviceNotify
:
626 /* This is a hack around the fact that XFree86's XKB doesn't give AltGr notifications */
635 void KbStateApplet::timerEvent(QTimerEvent
*) {
636 XkbStateRec state_return
;
637 XkbGetState (this->x11Display(), XkbUseCoreKbd
, &state_return
);
638 unsigned char latched
= XkbStateMods (&state_return
);
639 unsigned char locked
= XkbModLocks (&state_return
);
640 int mods
= ((int)latched
)<<8 | locked
;
644 for (int i
= 0; i
< 8; i
++) {
646 icons
[i
]->setState ((latched
&(1<<i
)) != 0, (locked
&(1<<i
)) != 0);
651 void KbStateApplet::stateChangeRequest (KeyIcon
*source
, bool latched
, bool locked
) {
652 for (int i
= 0; i
< 8; i
++) {
653 if (icons
[i
] == source
) {
655 XkbLockModifiers (this->x11Display(), XkbUseCoreKbd
, 1<<i
, 1<<i
);
657 XkbLockModifiers (this->x11Display(), XkbUseCoreKbd
, 1<<i
, 0);
658 XkbLatchModifiers (this->x11Display(), XkbUseCoreKbd
, 1<<i
, 1<<i
);
661 XkbLockModifiers (this->x11Display(), XkbUseCoreKbd
, 1<<i
, 0);
662 XkbLatchModifiers (this->x11Display(), XkbUseCoreKbd
, 1<<i
, 0);
669 void KbStateApplet::toggleModifier() {
670 showModifiers
= !showModifiers
;
677 void KbStateApplet::toggleLockkeys() {
678 showLockkeys
= !showLockkeys
;
685 void KbStateApplet::toggleMouse() {
686 showMouse
= !showMouse
;
693 void KbStateApplet::toggleAccessX() {
694 showAccessX
= !showAccessX
;
701 void KbStateApplet::configureAccessX() {
702 KToolInvocation::startServiceByDesktopName("kcmaccess");
705 void KbStateApplet::configureKeyboard() {
706 // The modulename "keyboard" is ambiguous on SuSE systems
707 // as there is also a YaST-module called "keyboard".
710 proc
<< "kde/keyboard";
711 proc
.start(KProcess::DontCare
);
715 void KbStateApplet::configureMouse() {
716 KToolInvocation::startServiceByDesktopName("mouse");
719 void KbStateApplet::about() {
720 KAboutData
about("kbstateapplet", I18N_NOOP("Keyboard Status Applet"), "0.2",
721 I18N_NOOP("Panel applet that shows the state of the modifier keys"), KAboutData::License_GPL_V2
, "(C) 2004 Gunnar Schmi Dt");
722 KAboutApplication
a(&about
, this);
726 void KbStateApplet::loadConfig()
728 KConfig
*c
= config();
729 c
->setGroup("General");
730 size
= c
->readEntry("IconDim", 20);
731 fillSpace
= c
->readEntry("fill space", true);
732 showModifiers
= c
->readEntry("Modifierkeys visible", true);
733 showLockkeys
= c
->readEntry("Lockkeys visible", true);
734 showMouse
= c
->readEntry("Mouse status visible", true);
735 showAccessX
= c
->readEntry("Slowkeys status visible", true);
736 showAccessX
= c
->readEntry("AccessX status visible", showAccessX
);
739 void KbStateApplet::saveConfig()
741 KConfig
*c
= config();
742 c
->setGroup("General");
743 c
->writeEntry("IconDim", size
);
744 c
->writeEntry("fill space", fillSpace
);
745 c
->writeEntry("Modifierkeys visible", showModifiers
);
746 c
->writeEntry("Lockkeys visible", showLockkeys
);
747 c
->writeEntry("Mouse status visible", showMouse
);
748 c
->writeEntry("AccessX status visible", showAccessX
);
752 /********************************************************************/
754 KeyIcon::KeyIcon (int keyId
, KInstance
*instance
,
755 QWidget
*parent
, const char *name
)
756 : StatusIcon (modifierKeys
[keyId
].name
, parent
, name
) {
757 this->instance
= instance
;
759 this->tristate
= (modifierKeys
[keyId
].isModifier
);
763 connect (this, SIGNAL(clicked()), SLOT(clickedSlot()));
766 KeyIcon::~KeyIcon () {
769 void KeyIcon::setState (bool latched
, bool locked
) {
770 latched
= latched
| locked
;
777 void KeyIcon::clickedSlot () {
779 emit
stateChangeRequest (this, !isLocked
, isLatched
&!isLocked
);
781 emit
stateChangeRequest (this, false, !isLocked
);
785 void KeyIcon::resizeEvent( QResizeEvent
*e
)
787 QWidget::resizeEvent(e
);
791 void KeyIcon::updateImages () {
792 int size
= width()<height() ? width() : height();
794 locked
= instance
->iconLoader()->loadIcon("lock_overlay", K3Icon::Panel
, size
-4);
795 if (strcmp(modifierKeys
[keyId
].icon
, "")) {
796 latched
= instance
->iconLoader()->loadIcon(modifierKeys
[keyId
].icon
, K3Icon::NoGroup
, size
-4);
797 unlatched
= instance
->iconLoader()->loadIcon(modifierKeys
[keyId
].icon
, K3Icon::NoGroup
, size
-4);
799 QImage img
= latched
.convertToImage();
800 KIconEffect::colorize(img
, KGlobalSettings::highlightedTextColor(), 1.0);
801 latched
.convertFromImage (img
);
803 img
= unlatched
.convertToImage();
804 KIconEffect::colorize(img
, KGlobalSettings::textColor(), 1.0);
805 unlatched
.convertFromImage (img
);
811 void KeyIcon::drawButton (QPainter
*p
) {
814 int x
= (width()-locked
.width())/2;
815 int y
= (height()-locked
.height())/2;
817 if (isLocked
|| isLatched
) {
818 qDrawShadePanel (p
, 0, 0, width(), height(), colorGroup(), true, 1, NULL
);
819 p
->fillRect (1,1,width()-2,height()-2, KGlobalSettings::highlightColor());
820 if (strcmp(modifierKeys
[keyId
].icon
, ""))
821 p
->drawPixmap (x
+1,y
+1, latched
);
822 black
= KGlobalSettings::highlightedTextColor();
826 qDrawShadePanel (p
, 0, 0, width(), height(), colorGroup(), false, 1, NULL
);
827 if (strcmp(modifierKeys
[keyId
].icon
, ""))
828 p
->drawPixmap (x
,y
, unlatched
);
829 black
= KGlobalSettings::textColor();
832 QString text
= i18n(modifierKeys
[keyId
].text
);
833 if (!text
.isEmpty() && !text
.isNull()) {
834 QFont font
= KGlobalSettings::generalFont();
835 font
.setWeight(QFont::Black
);
836 QFontMetrics
metrics(font
);
837 QRect rect
= metrics
.boundingRect (text
);
839 if (!strcmp(modifierKeys
[keyId
].name
, "Alt Graph"))
840 size
= rect
.width()>rect
.height()?rect
.width():rect
.height();
842 size
= rect
.width()>12*rect
.height()/5?rect
.width():12*rect
.height()/5;
844 if (font
.pixelSize() != -1)
845 font
.setPixelSize (font
.pixelSize()*width()*19/size
/32);
847 font
.setPointSizeFloat (font
.pointSizeFloat()*width()*19/size
/32);
851 if (!strcmp(modifierKeys
[keyId
].name
, "Alt Graph"))
852 p
->drawText (o
,o
, width(), height(), Qt::AlignCenter
, text
);
854 p
->drawText (o
,o
, width(), height()*(251)/384, Qt::AlignCenter
, text
);
856 if (tristate
&& isLocked
) {
857 p
->drawPixmap(x
+o
,y
+o
, locked
);
861 /********************************************************************/
863 MouseIcon::MouseIcon (KInstance
*instance
, QWidget
*parent
, const char *name
)
864 : StatusIcon ("", parent
, name
)
866 this->instance
= instance
;
870 connect (this, SIGNAL(clicked()), SLOT(clickedSlot()));
873 MouseIcon::~MouseIcon () {
876 void MouseIcon::setState (int state
) {
882 void MouseIcon::setActiveKey (int activekey
) {
883 this->activekey
= activekey
;
888 void MouseIcon::resizeEvent( QResizeEvent
*e
)
890 QWidget::resizeEvent(e
);
894 QPixmap
loadIcon(KInstance
*instance
, int size
, QColor color
, QString name
) {
895 KIconLoader
*loader
= instance
->iconLoader();
896 QPixmap result
= loader
->loadIcon(name
, K3Icon::NoGroup
, size
);
898 QImage img
= result
.convertToImage();
899 KIconEffect::colorize(img
, color
, 1.0);
900 result
.convertFromImage (img
);
905 void MouseIcon::updateImages () {
906 int size
= width()<height() ? width() : height();
908 QColor textcolor
= KGlobalSettings::textColor();
909 QColor basecolor
= KGlobalSettings::baseColor();
910 mouse
= loadIcon (instance
, size
, textcolor
, "kbstate_mouse");
911 leftSelected
= loadIcon (instance
, size
, textcolor
,
912 "kbstate_mouse_left_selected");
913 middleSelected
= loadIcon (instance
, size
, textcolor
,
914 "kbstate_mouse_mid_selected");
915 rightSelected
= loadIcon (instance
, size
, textcolor
,
916 "kbstate_mouse_right_selected");
917 leftDot
= loadIcon (instance
, size
, textcolor
, "kbstate_mouse_left");
918 middleDot
= loadIcon (instance
, size
, textcolor
, "kbstate_mouse_mid");
919 rightDot
= loadIcon (instance
, size
, textcolor
, "kbstate_mouse_right");
920 leftDotSelected
= loadIcon (instance
, size
, basecolor
,
921 "kbstate_mouse_left");
922 middleDotSelected
= loadIcon (instance
, size
, basecolor
,
923 "kbstate_mouse_mid");
924 rightDotSelected
= loadIcon (instance
, size
, basecolor
,
925 "kbstate_mouse_right");
930 void MouseIcon::drawButton (QPainter
*p
) {
931 p
->drawPixmap(0,0, mouse
);
932 if ((state
& Button1Mask
) != 0)
933 p
->drawPixmap(0,0, leftSelected
);
934 if ((state
& Button2Mask
) != 0)
935 p
->drawPixmap(0,0, middleSelected
);
936 if ((state
& Button3Mask
) != 0)
937 p
->drawPixmap(0,0, rightSelected
);
942 if ((state
& Button1Mask
) != 0)
943 p
->drawPixmap(0,0, leftDotSelected
);
945 p
->drawPixmap(0,0, leftDot
);
948 if ((state
& Button2Mask
) != 0)
949 p
->drawPixmap(0,0, middleDotSelected
);
951 p
->drawPixmap(0,0, middleDot
);
954 if ((state
& Button3Mask
) != 0)
955 p
->drawPixmap(0,0, rightDotSelected
);
957 p
->drawPixmap(0,0, rightDot
);
962 /********************************************************************/
964 TimeoutIcon::TimeoutIcon (KInstance
*instance
, const QString
&text
,
965 const QString
&featurename
,
966 QWidget
*parent
, const char *name
)
967 : StatusIcon (text
, parent
, name
) {
968 this->instance
= instance
;
969 this->featurename
= featurename
;
971 setImage (featurename
);
972 connect (&timer
, SIGNAL(timeout()), this, SLOT(timeout()));
975 TimeoutIcon::~TimeoutIcon () {
978 void TimeoutIcon::update () {
979 int size
= width()<height() ? width() : height();
980 if (pixmap
.width() != size
)
981 pixmap
= instance
->iconLoader()->loadIcon(iconname
, K3Icon::NoGroup
, size
);
983 QImage img
= pixmap
.convertToImage();
984 KIconEffect::colorize(img
, KGlobalSettings::textColor(), 1.0);
985 pixmap
.convertFromImage (img
);
991 void TimeoutIcon::setGlyth (const QString
&glyth
) {
995 QImage img
= pixmap
.convertToImage();
996 KIconEffect::colorize(img
, KGlobalSettings::textColor(), 1.0);
997 pixmap
.convertFromImage (img
);
1003 void TimeoutIcon::setImage (const QString
&name
, int timeout
) {
1006 if (!name
.isNull() && !name
.isEmpty()) {
1007 int size
= width()<height() ? width() : height();
1008 pixmap
= instance
->iconLoader()->loadIcon(iconname
, K3Icon::NoGroup
, size
);
1010 QImage img
= pixmap
.convertToImage();
1011 KIconEffect::colorize(img
, KGlobalSettings::textColor(), 1.0);
1012 pixmap
.convertFromImage (img
);
1018 timer
.start (timeout
, true);
1021 void TimeoutIcon::timeout () {
1023 setImage(featurename
);
1027 void TimeoutIcon::drawButton (QPainter
*p
) {
1028 QString text
= glyth
;
1032 if (!iconname
.isNull() && !iconname
.isEmpty())
1033 p
->drawPixmap(0,0, image
);
1034 else if (glyth
== " ") {
1035 text
= i18nc("a (the first letter in the alphabet)", "a");
1040 QFont font
= KGlobalSettings::generalFont();
1041 font
.setWeight(QFont::Black
);
1042 QFontMetrics
metrics(font
);
1043 QRect rect
= metrics
.boundingRect (text
);
1044 int size
= count
*rect
.width() > rect
.height()
1045 ? count
*rect
.width() : rect
.height();
1046 if (font
.pixelSize() != -1)
1047 font
.setPixelSize (font
.pixelSize()*width()*factor
/size
/64);
1049 font
.setPointSizeFloat (font
.pointSizeFloat()*width()*factor
/size
/64);
1053 p
->setPen (KGlobalSettings::textColor());
1054 p
->drawText (0,0, width()/2, height()/2, Qt::AlignCenter
, text
);
1057 QColor t
= KGlobalSettings::textColor();
1058 QColor b
= KGlobalSettings::baseColor();
1059 p
->setPen (QColor ((2*t
.red()+3*b
.red())/5,
1060 (2*t
.green()+3*b
.green())/5,
1061 (2*t
.blue()+3*b
.blue())/5));
1062 p
->drawText (width()/2,0, width()/2, height(), Qt::AlignCenter
, text
);
1063 p
->setPen (QColor ((2*t
.red()+b
.red())/3,
1064 (2*t
.green()+b
.green())/3,
1065 (2*t
.blue()+b
.blue())/3));
1066 p
->drawText (0,0, width(), height(), Qt::AlignCenter
, text
);
1067 p
->setPen (KGlobalSettings::textColor());
1068 p
->drawText (0,0, width()/2, height(), Qt::AlignCenter
, text
);
1072 /********************************************************************/
1074 StatusIcon::StatusIcon (const QString
&text
, QWidget
*parent
, const char *name
)
1075 : QPushButton (text
, parent
, name
) {
1076 setSizePolicy(QSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Ignored
));
1079 StatusIcon::~StatusIcon () {
1082 QSize
StatusIcon::minimumSizeHint () const {