init
[qstardict] / qstardict / selection.cpp
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
20 #include "selection.h"
21
22 #ifdef Q_WS_WIN
23 #include <windows.h>
24
25 namespace
26 {
27
28 QString currentSelection()
29 {
30     POINT Point;
31     HWND hWindow;
32     DWORD dwStart, dwEnd;
33     char szWindowText[256];
34
35     if (! GetCursorPos(&Point))
36         return QString();
37
38     if( ! (hWindow = WindowFromPoint(Point)))
39         return QString();
40
41     SendMessage(hWindow, WM_GETTEXT, 256, (LPARAM)szWindowText);
42     SendMessage(hWindow, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
43
44     return QString::fromLocal8Bit(szWindowText);
45 }
46
47 }
48
49 #else // Q_WS_WIN
50
51 #include <QApplication>
52 #include <QClipboard>
53
54 namespace
55 {
56
57 inline QString currentSelection()
58 { return QApplication::clipboard()->text(QClipboard::Selection); }
59
60 }
61
62 #endif // Q_WS_WIN
63
64 namespace QStarDict
65 {
66
67 Selection::Selection(QObject *parent)
68     : QObject(parent)
69 {
70     m_scan = false;
71     m_timerId = 0;
72 }
73
74 void Selection::setScan(bool scan)
75 {
76     if (m_scan == scan)
77         return;
78
79     m_scan = scan;
80     if (m_scan)
81     {
82         m_lastState = currentSelection();
83         m_timerId = startTimer(300);
84     }
85     else
86         killTimer(m_timerId);
87 }
88
89 void Selection::timerEvent(QTimerEvent*)
90 {
91     if (m_lastState != currentSelection())
92     {
93         m_lastState = currentSelection();
94         emit changed(m_lastState);
95     }
96 }
97
98 }
99
100 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
101