kkbswitch
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kbswitchtrayicon.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /***************************************************************************
  2. kbswitchtrayicon.cpp - description
  3. -------------------
  4. begin : Wed Jul 4 2001
  5. copyright : (C) 2001 by Leonid Zeitlin
  6. email : lz@europe.com
  7. ***************************************************************************/
  8. /***************************************************************************
  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. ***************************************************************************/
  16. #include <tdeversion.h>
  17. #undef USE_BOLD_MENUITEM
  18. #include "kbswitchtrayicon.h"
  19. #include "xkeyboard.h"
  20. #ifdef USE_BOLD_MENUITEM
  21. #include "boldmenuitem.h"
  22. #endif
  23. #include <tdepopupmenu.h>
  24. #if TDE_VERSION_MAJOR >= 3
  25. #include <kstandarddirs.h>
  26. #else
  27. #include <kstddirs.h>
  28. #endif
  29. #include <tdelocale.h>
  30. #include <kstdaction.h>
  31. #include <tdeaction.h>
  32. #include <tdeaboutapplication.h>
  33. #if TDE_VERSION_MAJOR >= 3
  34. #include <tdeapplication.h>
  35. #else
  36. #include <kapp.h>
  37. #endif
  38. #include <kiconloader.h>
  39. #include <kdebug.h>
  40. #include <tdeversion.h>
  41. #include <ntqtooltip.h>
  42. #include <ntqstyle.h>
  43. #include <ntqpainter.h>
  44. #ifdef USE_BOLD_MENUITEM
  45. static TQColor getActiveTextColor(TDEPopupMenu *menu)
  46. {
  47. int id = menu->insertItem("test text");
  48. TQMenuItem *item = menu->findItem(id);
  49. TQStyleOption styleopt = TQStyleOption(item);
  50. TQPainter painter(menu);
  51. TQColorGroup &cg = menu->colorGroup();
  52. TDEApplication::style().drawControl(TQStyle::CE_PopupMenuItem, &painter, menu,
  53. menu->contentsRect(), cg, TQStyle::Style_Enabled | TQStyle::Style_Active,
  54. styleopt);
  55. menu->removeItem(id);
  56. return painter.pen().color();
  57. }
  58. #endif
  59. KBSwitchTrayIcon::KBSwitchTrayIcon(KBConfig *conf){
  60. TQPixmap pix;
  61. #if TDE_VERSION_MAJOR >= 3
  62. TDEActionCollection *actions = new TDEActionCollection(this);
  63. #define ACTION_PARENT actions
  64. #else
  65. #define ACTION_PARENT this
  66. #endif
  67. m_kbconf = conf;
  68. //TQObject::connect(conf, SIGNAL(changed()), this, SLOT(updateMenuIcons()));
  69. TDEPopupMenu * menu = contextMenu();
  70. addLayoutItems(menu, false);
  71. TQObject::connect(menu, SIGNAL(activated(int)), this, SLOT(slotMenuActivated(int)));
  72. menu->insertSeparator();
  73. TDEAction *pref = KStdAction::preferences(this, SIGNAL(preferencesSelected()), ACTION_PARENT);
  74. pref->plug(menu);
  75. TDEAction *help = KStdAction::help(this, SLOT(slotHelp()), ACTION_PARENT);
  76. help->plug(menu);
  77. TDEAction *about = KStdAction::aboutApp(this, SLOT(slotAbout()), ACTION_PARENT);
  78. about->plug(menu);
  79. /*TQString path = locate("icon", "hicolor/16x16/apps/locale.png");
  80. if (!path.isEmpty()) pix.load(path);*/
  81. pix = kapp->iconLoader()->loadIcon("locale", TDEIcon::Small);
  82. menu->changeTitle(menu->idAt(0), pix, i18n("Keyboard Switch"));
  83. setAlignment(TQt::AlignHCenter | TQt::AlignCenter);
  84. }
  85. KBSwitchTrayIcon::~KBSwitchTrayIcon(){
  86. }
  87. /** No descriptions */
  88. void KBSwitchTrayIcon::slotMenuActivated(int id){
  89. if (id >= 0 && id < m_kbconf->groupCount()) emit groupSelected(id);
  90. }
  91. /** No descriptions */
  92. void KBSwitchTrayIcon::setActiveGroup(int groupno){
  93. int i;
  94. TDEPopupMenu *menu = contextMenu();
  95. for (i = 0; i < m_kbconf->groupCount(); i++)
  96. menu->setItemChecked(i, false);
  97. menu->setItemChecked(groupno, true);
  98. }
  99. /** No descriptions */
  100. void KBSwitchTrayIcon::setToggleGroups(int group1, int group2){
  101. int i;
  102. bool toggling;
  103. TDEPopupMenu *menu = contextMenu();
  104. for (i = 0; i < m_kbconf->groupCount(); i++) {
  105. toggling = (i == group1 || i == group2);
  106. #ifdef USE_BOLD_MENUITEM
  107. TQMenuItem *item = menu->findItem(i);
  108. BoldMenuItem *bolditem = dynamic_cast<BoldMenuItem*>(item->custom());
  109. bolditem->setBold(toggling);
  110. #else
  111. if (toggling)
  112. menu->changeItem(i, m_kbconf->getGroup(i)->getName() + "*");
  113. else menu->changeItem(i, m_kbconf->getGroup(i)->getName());
  114. #endif
  115. }
  116. }
  117. /** No descriptions */
  118. void KBSwitchTrayIcon::mouseReleaseEvent(TQMouseEvent *event){
  119. if (event->button() == LeftButton) {
  120. emit clicked();
  121. }
  122. }
  123. /** No descriptions */
  124. void KBSwitchTrayIcon::slotAbout(){
  125. TDEAboutApplication about;
  126. about.exec();
  127. }
  128. /** No descriptions */
  129. void KBSwitchTrayIcon::updateMenuIcons(){
  130. TDEPopupMenu *menu = contextMenu();
  131. for (int i = 0; i < m_kbconf->groupCount(); i++) {
  132. menu->changeItem(i, m_kbconf->getGroup(i)->getPixmap(), menu->text(i));
  133. }
  134. }
  135. /** No descriptions */
  136. void KBSwitchTrayIcon::addLayoutItems(TDEPopupMenu *menu, bool clearOld) {
  137. KBGroup *group;
  138. int index;
  139. #ifdef USE_BOLD_MENUITEM
  140. TQColor active_text_color = getActiveTextColor(menu);
  141. #endif
  142. if (clearOld)
  143. for (int i = 0; i < XKeyboard::MaxNumKbdGroups; i++)
  144. if ((index = menu->indexOf(i)) >= 0) menu->removeItemAt(index);
  145. for (int i = 0; i < m_kbconf->groupCount(); i++) {
  146. group = m_kbconf->getGroup(i);
  147. #ifdef USE_BOLD_MENUITEM
  148. /* the work on BoldMenuItems suspended: see comments in boldmenuitem.h */
  149. menu->insertItem(group->getPixmap(),
  150. new BoldMenuItem(group->getName(), active_text_color, false), i);
  151. #else
  152. menu->insertItem(group->getPixmap(), group->getName(), i, i + 1);
  153. #endif
  154. }
  155. }
  156. /** No descriptions */
  157. void KBSwitchTrayIcon::reconfigure(){
  158. addLayoutItems(contextMenu(), true);
  159. }
  160. /** Update the tray icon display for the given group */
  161. void KBSwitchTrayIcon::updateTrayIcon(int groupno){
  162. const TQPixmap& pix = m_kbconf->getGroup(groupno)->getPixmap();
  163. setPixmap(pix);
  164. setActiveGroup(groupno);
  165. TQToolTip::remove(this);
  166. TQToolTip::add(this, m_kbconf->getGroup(groupno)->getName());
  167. }
  168. /** Update menu and tray icons after configuration has changed */
  169. void KBSwitchTrayIcon::slotUpdateIcons(){
  170. updateTrayIcon(XKeyboard::self()->getGroupNo());
  171. updateMenuIcons();
  172. }
  173. /** Display help */
  174. void KBSwitchTrayIcon::slotHelp(){
  175. kapp->invokeHelp();
  176. }
  177. #include "kbswitchtrayicon.moc"