File: wordPickUI.cpp

package info (click to toggle)
stardict 3.0.1-9.3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,916 kB
  • ctags: 10,187
  • sloc: cpp: 45,541; sh: 9,360; xml: 4,151; ansic: 2,090; makefile: 552
file content (283 lines) | stat: -rw-r--r-- 8,263 bytes parent folder | download | duplicates (3)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*********************************************************************
 * 
 * This file part of WordPick - A Adobe Acrobat/Reader plugin for text
 * extraction by mouse click.
 * 2006 Dewolf Xue <deWolf_maTri_X@msn.com>
 * 2006 Hu Zheng <huzheng_001@163.com>
 *
 * This plugin is special for StarDict (http://stardict.sourceforge.net)
 *
 * Compile under Acrobat SDK 7 + M$ VS 2003.
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
*********************************************************************/
//#include <windows.h>
//#include <process.h>
#include "PIHeaders.h"
#include "PIRequir.h"
#include "PIMain.h" // for gHINSTANCE
#include "wordPickUI.h"
#include "ThTypes.h"


/*-------------------------------------------------------
	Constants/Declarations
-------------------------------------------------------*/

char buffer[128];
static bool boolPickWords = false;

//extern ASAtom WordPick_K;
extern AVToolButton handButton;

static AVToolRec WordPickTool;
static AVIdleProc idleProc;
static AVPageViewCursorProc cursorProc;
//ASBool gWordPickToolSelected;
//static AVCursor WordPickCursor;
//static AVCursor orgSysCursor;
static AVToolButton wordPickToolButton;

static AVMenuItem menuItem = NULL;

// UI Callbacks
static AVExecuteProc		cbActivateTool;
static AVComputeEnabledProc	cbIsEnabled;
static AVComputeMarkedProc	cbIsMarked;

// For mac
#define CURSWordPickCursor			150
#define SICNWordPickToolIcon		150
#define cicnWordPickIcon			150

// For windows
#define IDC_CURSOR1                 101
#define IDI_ICON1                   102
#define IDB_BITMAP1                 103
#define IDB_BITMAP2                 104

const int MOUSEOVER_INTERVAL = 300;
const int WM_MY_SHOW_TRANSLATION = WM_USER + 300;
	
static AVDevCoord oldxpoint=-1, oldypoint=-1;
static AVCursor HandCursor=NULL;

/*-------------------------------------------------------
	Utility Methods
-------------------------------------------------------*/

void *GetToolButtonIcon(void)
{
	return (AVCursor)LoadBitmapA(gHINSTANCE, MAKEINTRESOURCE(IDB_BITMAP1));
}
static ACCB1 ASBool ACCB2 RedrawCurrentPage (AVDoc doc, void* clientData)
{
	AVPageView page = AVDocGetPageView(doc);
	AVPageViewInvalidateRect (page, NULL);
	AVPageViewDrawNow(page);

	return true;
}

static void RedrawAllVisibleAnnots()
{
	AVAppEnumDocs (RedrawCurrentPage, NULL);
}

/*-------------------------------------------------------
	AVTool Callbacks
-------------------------------------------------------*/
static ACCB1 ASBool ACCB2 PDTextSelectEnumTextProcCB(void* procObj, PDFont font, ASFixed size, 
											   PDColorValue color, char* text, ASInt32 textLen)
{
	if (strlen(buffer) + strlen(text) < 127)
	{
		strcat(buffer, text);
	}
	return true;
}

static ACCB1 ASBool ACCB2 IsEnabled (void *permRequired)
{
	AVDoc avDoc = AVAppGetActiveDoc();
	if (!avDoc)
		return false;
	else
	{
		PDPerms docPerms = PDDocGetPermissions(AVDocGetPDDoc(avDoc));
		return (!permRequired || (((PDPerms)permRequired & docPerms) != 0));
	}
}

static ACCB1 ASBool ACCB2 IsMarked (void *clientData)
{
	return boolPickWords;
}

static ACCB1 void ACCB2 avtiveWordPickButton(void *clientData)
{
	AVCursor tmp = AVSysGetCursor();
	AVSysSetCursor (AVSysGetStandardCursor(HAND_CURSOR));
	HandCursor = AVSysGetCursor();
	AVSysSetCursor (tmp);

	boolPickWords = !boolPickWords;
}

static void SetUpToolButton(void)
{
	void *WordPickIcon = GetToolButtonIcon();
	AVToolBar toolBar = AVAppGetToolBar();
	AVToolButton separator = AVToolBarGetButtonByName (toolBar, ASAtomFromString("Hand"));

	wordPickToolButton = AVToolButtonNew (ASAtomFromString("ADBE:WordPick"), WordPickIcon, true, false);
	AVToolButtonSetExecuteProc (wordPickToolButton, ASCallbackCreateProto(AVExecuteProc, avtiveWordPickButton), NULL);
	AVToolButtonSetComputeEnabledProc (wordPickToolButton, cbIsEnabled, (void *)pdPermEdit);
	AVToolButtonSetComputeMarkedProc (wordPickToolButton, cbIsMarked, NULL);
	AVToolButtonSetHelpText (wordPickToolButton, "Toggle StarDict");

	AVToolBarAddButton(toolBar, wordPickToolButton, true, separator);
}

void SetUpUI(void)
{
	idleProc = ASCallbackCreateProto(AVIdleProc, wordPickAVAppRegisterForPageViewIdleProc);
	AVAppRegisterIdleProc (idleProc, NULL, 30);
	
	cbIsEnabled		= ASCallbackCreateProto (AVComputeEnabledProc,	&IsEnabled);
	cbIsMarked		= ASCallbackCreateProto (AVComputeMarkedProc,	&IsMarked);

	if (ASGetConfiguration(ASAtomFromString("CanEdit")))
		SetUpToolButton();
}

void CleanUpUI(void)
{
	AVAppUnregisterIdleProc(idleProc, NULL);

	if(wordPickToolButton)
		AVToolButtonDestroy (wordPickToolButton);
}

ACCB1 void ACCB2 wordPickAVAppRegisterForPageViewIdleProc(void * clientData)
{
	if (boolPickWords)
	{
		//_beginthread(getCurWords, 0, NULL);
		getCurWords(NULL);
	}
	return;
}

static bool bIsPureEnglish(const char *str) 
{ 
	for (int i=0; str[i]!=0; i++) 
		if (!isascii(str[i]))
			return false;
	return true;	
}

void getCurWords(PVOID parm)
{
	POINT Pt;
	GetCursorPos(&Pt);
	HWND WND = WindowFromPoint(Pt);
	TCHAR wClassName[64];
	if (GetClassName(WND, wClassName, sizeof(wClassName) / sizeof(TCHAR))) 
	{
		if (strcmp(wClassName, "AVL_AVView")!=0)
			return;
	}
	AVDoc avDoc = AVAppGetActiveDoc();
	if (!avDoc)
	{
		return;
	}
	AVPageView pageView = AVDocGetPageView(avDoc);
	if (!pageView)
	{
		return;
	}
	AVCursor acur = AVSysGetCursor ();
	if (HandCursor != acur)
	{
		return;
	}
	
	AVDevCoord xpoint, ypoint;
	AVPageViewGetMousePosition (pageView, &xpoint, &ypoint);

	if (oldxpoint == xpoint && oldypoint == ypoint)
        return;
	oldxpoint = xpoint;
	oldypoint = ypoint;

	PDDoc pdDoc = AVDocGetPDDoc(avDoc);

	buffer[0] = 0;
	if (AVPageViewGetPageNum(pageView) > PDBeforeFirstPage) 
	{
		// First of all U have to capture mouse position x,y and do the following code.
		AVDevRect resultRect;
		resultRect.left   = xpoint-10;
		resultRect.right  = xpoint+10;
		resultRect.top    = ypoint;
		resultRect.bottom = ypoint-1;
		AVPageViewDrawRectOutlineWithHandles(pageView, &resultRect, true, true, NULL, NULL);

		ASFixedRect fixedRect;
		AVPageViewDeviceRectToPage(pageView,&resultRect,&fixedRect);
		PDTextSelect pdtext=PDDocCreateTextSelect (pdDoc,AVPageViewGetPageNum(pageView),&fixedRect);
		
		AVPageViewInvalidateRect (pageView, NULL);
		AVPageViewDrawNow(pageView);
		//AVDocSetSelection(avDoc,ASAtomFromString("Text"),(void *)pdtext, true);
		//AVDocShowSelection(avDoc);
		if (PDTextSelectGetRangeCount(pdtext) > 0)
		{
			PDTextSelectEnumTextProc enumProc 
				= ASCallbackCreateProto(PDTextSelectEnumTextProc, &PDTextSelectEnumTextProcCB);
			// Enumerate the text runs in PDText
			PDTextSelectEnumText(pdtext, enumProc, NULL);
			ASCallbackDestroy(enumProc);
			HWND stardictWND = FindWindow((LPCTSTR)"StarDictMouseover", NULL);
			if (stardictWND == NULL)
			{
				//didn't find the window, barf here...
				//AVAlertNote("The dict is down.\nWordPick's doing stuff.");
			}
			else
			{
				strcpy(GlobalData->CurMod.MatchedWord, buffer);
				if (bIsPureEnglish(buffer))
				{
					char *p = strchr(buffer, ' ');
					if (p)
						GlobalData->CurMod.BeginPos = p-buffer;
					else
						GlobalData->CurMod.BeginPos = 0;
				} else
				{
					GlobalData->CurMod.BeginPos = 0;
				}

				DWORD SendMsgAnswer;
				SendMessageTimeout(stardictWND, WM_MY_SHOW_TRANSLATION, 0, 0, SMTO_ABORTIFHUNG, MOUSEOVER_INTERVAL, &SendMsgAnswer);
			}
		}
	}
	return;
}