selection.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*****************************************************************************
  2. * selection.cpp - QStarDict, a StarDict clone written with using Qt *
  3. * Copyright (C) 2007 Alexander Rodin *
  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 2 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 along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  18. *****************************************************************************/
  19. #include "selection.h"
  20. #ifdef Q_WS_WIN
  21. #include <windows.h>
  22. namespace
  23. {
  24. QString currentSelection()
  25. {
  26. POINT Point;
  27. HWND hWindow;
  28. DWORD dwStart, dwEnd;
  29. char szWindowText[256];
  30. if (! GetCursorPos(&Point))
  31. return QString();
  32. if( ! (hWindow = WindowFromPoint(Point)))
  33. return QString();
  34. SendMessage(hWindow, WM_GETTEXT, 256, (LPARAM)szWindowText);
  35. SendMessage(hWindow, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
  36. return QString::fromLocal8Bit(szWindowText);
  37. }
  38. }
  39. #else // Q_WS_WIN
  40. #include <QApplication>
  41. #include <QClipboard>
  42. namespace
  43. {
  44. inline QString currentSelection()
  45. { return QApplication::clipboard()->text(QClipboard::Selection); }
  46. }
  47. #endif // Q_WS_WIN
  48. namespace QStarDict
  49. {
  50. Selection::Selection(QObject *parent)
  51. : QObject(parent)
  52. {
  53. m_scan = false;
  54. m_timerId = 0;
  55. }
  56. void Selection::setScan(bool scan)
  57. {
  58. if (m_scan == scan)
  59. return;
  60. m_scan = scan;
  61. if (m_scan)
  62. {
  63. m_lastState = currentSelection();
  64. m_timerId = startTimer(300);
  65. }
  66. else
  67. killTimer(m_timerId);
  68. }
  69. void Selection::timerEvent(QTimerEvent*)
  70. {
  71. if (m_lastState != currentSelection())
  72. {
  73. m_lastState = currentSelection();
  74. emit changed(m_lastState);
  75. }
  76. }
  77. }
  78. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc