summaryrefslogtreecommitdiffstats
path: root/kkbswitch/kbswitchtrayicon.cpp
blob: 335f90622146e25f645fcb79e15e29d02e91349a (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
/***************************************************************************
                          kbswitchtrayicon.cpp  -  description
                             -------------------
    begin                : Wed Jul 4 2001
    copyright            : (C) 2001 by Leonid Zeitlin
    email                : lz@europe.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tdeversion.h>
 
#undef USE_BOLD_MENUITEM
 
#include "kbswitchtrayicon.h"
#include "xkeyboard.h"
#ifdef USE_BOLD_MENUITEM
  #include "boldmenuitem.h"
#endif  
#include <tdepopupmenu.h>
#if TDE_VERSION_MAJOR >= 3
  #include <kstandarddirs.h>
#else  
  #include <kstddirs.h>
#endif  
#include <tdelocale.h>
#include <kstdaction.h>
#include <tdeaction.h>
#include <tdeaboutapplication.h>
#if TDE_VERSION_MAJOR >= 3
  #include <tdeapplication.h>
#else
  #include <kapp.h>
#endif
#include <kiconloader.h>
#include <kdebug.h>
#include <tdeversion.h>
#include <ntqtooltip.h>
#include <ntqstyle.h>
#include <ntqpainter.h>

#ifdef USE_BOLD_MENUITEM
static TQColor getActiveTextColor(TDEPopupMenu *menu)
{
	int id = menu->insertItem("test text");
	TQMenuItem *item = menu->findItem(id);
	TQStyleOption styleopt = TQStyleOption(item);
	TQPainter painter(menu);
	TQColorGroup &cg = menu->colorGroup();
	TDEApplication::style().drawControl(TQStyle::CE_PopupMenuItem, &painter, menu,
	  menu->contentsRect(), cg, TQStyle::Style_Enabled | TQStyle::Style_Active,
	  styleopt);
	menu->removeItem(id);
	return painter.pen().color();  
}
#endif

KBSwitchTrayIcon::KBSwitchTrayIcon(KBConfig *conf){
  TQPixmap pix;
#if TDE_VERSION_MAJOR >= 3
  TDEActionCollection *actions = new TDEActionCollection(this);
  #define ACTION_PARENT actions
#else
  #define ACTION_PARENT this
#endif

  m_kbconf = conf;
  //TQObject::connect(conf, SIGNAL(changed()), this, SLOT(updateMenuIcons()));
  TDEPopupMenu * menu = contextMenu();
  addLayoutItems(menu, false);
  TQObject::connect(menu, SIGNAL(activated(int)), this, SLOT(slotMenuActivated(int)));

  menu->insertSeparator();
  TDEAction *pref = KStdAction::preferences(this, SIGNAL(preferencesSelected()), ACTION_PARENT);
  pref->plug(menu);
  TDEAction *help = KStdAction::help(this, SLOT(slotHelp()), ACTION_PARENT);
  help->plug(menu);
  TDEAction *about = KStdAction::aboutApp(this, SLOT(slotAbout()), ACTION_PARENT);
  about->plug(menu);

  /*TQString path = locate("icon", "hicolor/16x16/apps/locale.png");
  if (!path.isEmpty()) pix.load(path);*/
  pix = kapp->iconLoader()->loadIcon("locale", TDEIcon::Small);
  menu->changeTitle(menu->idAt(0), pix, i18n("Keyboard Switch"));
  setAlignment(TQt::AlignHCenter | TQt::AlignCenter);
}

KBSwitchTrayIcon::~KBSwitchTrayIcon(){
}

/** No descriptions */
void KBSwitchTrayIcon::slotMenuActivated(int id){
  if (id >= 0 && id < m_kbconf->groupCount()) emit groupSelected(id);
}

/** No descriptions */
void KBSwitchTrayIcon::setActiveGroup(int groupno){
  int i;
  TDEPopupMenu *menu = contextMenu();
  for (i = 0; i < m_kbconf->groupCount(); i++)
    menu->setItemChecked(i, false);
  menu->setItemChecked(groupno, true);
}

/** No descriptions */
void KBSwitchTrayIcon::setToggleGroups(int group1, int group2){
  int i;
  bool toggling;
  TDEPopupMenu *menu = contextMenu();

  for (i = 0; i < m_kbconf->groupCount(); i++) {
    toggling = (i == group1 || i == group2);
#ifdef USE_BOLD_MENUITEM
		TQMenuItem *item = menu->findItem(i);
		BoldMenuItem *bolditem = dynamic_cast<BoldMenuItem*>(item->custom());
		bolditem->setBold(toggling);
#else
    if (toggling)
      menu->changeItem(i, m_kbconf->getGroup(i)->getName() + "*");
    else menu->changeItem(i, m_kbconf->getGroup(i)->getName());
#endif
  }
}

/** No descriptions */
void KBSwitchTrayIcon::mouseReleaseEvent(TQMouseEvent *event){
  if (event->button() == LeftButton) {
    emit clicked();
  }
}

/** No descriptions */
void KBSwitchTrayIcon::slotAbout(){
  TDEAboutApplication about;
  about.exec();
}

/** No descriptions */
void KBSwitchTrayIcon::updateMenuIcons(){
  TDEPopupMenu *menu = contextMenu();
  for (int i = 0; i < m_kbconf->groupCount(); i++) {
    menu->changeItem(i, m_kbconf->getGroup(i)->getPixmap(), menu->text(i));
  }
}

/** No descriptions */
void KBSwitchTrayIcon::addLayoutItems(TDEPopupMenu *menu, bool clearOld) {
  KBGroup *group;
  int index;
#ifdef USE_BOLD_MENUITEM
  TQColor active_text_color = getActiveTextColor(menu);
#endif  

  if (clearOld)
    for (int i = 0; i < XKeyboard::MaxNumKbdGroups; i++)
      if ((index = menu->indexOf(i)) >= 0) menu->removeItemAt(index);

  for (int i = 0; i < m_kbconf->groupCount(); i++) {
    group = m_kbconf->getGroup(i);
#ifdef USE_BOLD_MENUITEM
    /* the work on BoldMenuItems suspended: see comments in boldmenuitem.h */
    menu->insertItem(group->getPixmap(),
      new BoldMenuItem(group->getName(), active_text_color, false), i);
#else    
    menu->insertItem(group->getPixmap(), group->getName(), i, i + 1);
#endif    
  }
}

/** No descriptions */
void KBSwitchTrayIcon::reconfigure(){
  addLayoutItems(contextMenu(), true);
}

/** Update the tray icon display for the given group */
void KBSwitchTrayIcon::updateTrayIcon(int groupno){
  const TQPixmap& pix = m_kbconf->getGroup(groupno)->getPixmap();
  setPixmap(pix);
  setActiveGroup(groupno);
  TQToolTip::remove(this);
  TQToolTip::add(this, m_kbconf->getGroup(groupno)->getName());
}

/** Update menu and tray icons after configuration has changed */
void KBSwitchTrayIcon::slotUpdateIcons(){
  updateTrayIcon(XKeyboard::self()->getGroupNo());
  updateMenuIcons();
}

/** Display help */
void KBSwitchTrayIcon::slotHelp(){
  kapp->invokeHelp();
}