Add interface for other programs to send to GD a word under cursor

This commit is contained in:
Abs62 2011-06-08 20:12:19 +04:00
parent e87dce57ad
commit a523b8e3d7
2 changed files with 77 additions and 4 deletions

View file

@ -0,0 +1,46 @@
#ifndef __GDDATATRANSFER_H_
#define __GDDATATRANSFER_H_
/*
Header for other program to interact with GoldenDict
When GD needs to retrieve word under mouse cursor it can ask for this a target program
by sending a message.
GD_MESSAGE_NAME - name of the this message
Message number must be retrieved through RegisterWindowMessage function
Message parameters:
WPARAM - 0, not used
LPARAM - pointer to GDDataStruct structure
GDDataStruct fields:
dwSize - Structure size
hWnd - Window with requested word
Pt - Request coordinates (in screen coordinates, device units)
dwMaxLength - Maximum word length to transfer (buffer size in unicode symbols)
cwData - Buffer for requested word in UNICODE
If program process this message it must fill cwData and return TRUE
Otherwise GD will work by old technique
*/
#ifdef UNICODE
#define GD_MESSAGE_NAME L"GOLDENDICT_GET_WORD_IN_COORDINATES"
#else
#define GD_MESSAGE_NAME "GOLDENDICT_GET_WORD_IN_COORDINATES"
#endif
#pragma pack(push,1)
typedef struct {
DWORD dwSize;
HWND hWnd;
POINT Pt;
DWORD dwMaxLength;
WCHAR *cwData;
} GDDataStruct, *LPGDDataStruct;
#pragma pack(pop)
#endif

View file

@ -1,8 +1,9 @@
#include "TextOutSpy.h" #include "TextOutSpy.h"
#include "ThTypes.h" #include "ThTypes.h"
#include "GDDataTranfer.h"
const int MOUSEOVER_INTERVAL = 300; const int MOUSEOVER_INTERVAL = 300;
const int REQUEST_MESSAGE_INTERVAL = 500;
const int WM_MY_SHOW_TRANSLATION = WM_USER + 301; const int WM_MY_SHOW_TRANSLATION = WM_USER + 301;
HINSTANCE g_hInstance = NULL; HINSTANCE g_hInstance = NULL;
@ -11,19 +12,45 @@ HINSTANCE hGetWordLib = 0;
UINT_PTR TimerID = 0; UINT_PTR TimerID = 0;
typedef void (*GetWordProc_t)(TCurrentMode *); typedef void (*GetWordProc_t)(TCurrentMode *);
GetWordProc_t GetWordProc = NULL; GetWordProc_t GetWordProc = NULL;
GDDataStruct gds;
UINT uGdAskMessage;
WCHAR Buffer[256];
static HWND GetWindowFromPoint(POINT pt) { static HWND GetWindowFromPoint(POINT pt)
{
HWND WndParent,WndChild; HWND WndParent,WndChild;
WndParent = WindowFromPoint(pt); WndParent = WindowFromPoint(pt);
if(WndParent == NULL) return WndParent; if(WndParent == NULL) return WndParent;
ScreenToClient(WndParent, &pt); ScreenToClient(WndParent, &pt);
WndChild=RealChildWindowFromPoint(WndParent, pt); WndChild = RealChildWindowFromPoint(WndParent, pt);
if(WndChild == NULL) return WndParent; if(WndChild == NULL) return WndParent;
return WndChild; return WndChild;
}; };
static void SendWordToServer() static void SendWordToServer()
{ {
DWORD SendMsgAnswer;
if(uGdAskMessage) {
LRESULT lr;
int n;
gds.dwSize = sizeof(gds);
gds.cwData = Buffer;
gds.dwMaxLength = sizeof(Buffer) / sizeof(Buffer[0]);
Buffer[0] = 0;
gds.hWnd = GlobalData->LastWND;
gds.Pt = GlobalData->LastPt;
lr = SendMessageTimeout(gds.hWnd, uGdAskMessage, 0, (LPARAM)&gds, SMTO_ABORTIFHUNG, REQUEST_MESSAGE_INTERVAL, &SendMsgAnswer);
if(lr != 0 && SendMsgAnswer != 0) {
n = WideCharToMultiByte(CP_UTF8, 0, gds.cwData, lstrlenW(gds.cwData), GlobalData->CurMod.MatchedWord, sizeof(GlobalData->CurMod.MatchedWord) - 1, 0, 0);
GlobalData->CurMod.MatchedWord[n] = 0;
GlobalData->CurMod.WordLen = n;
GlobalData->CurMod.BeginPos = 0;
if(n > 0) {
SendMessageTimeout(GlobalData->ServerWND, WM_MY_SHOW_TRANSLATION, 0, 0, SMTO_ABORTIFHUNG, MOUSEOVER_INTERVAL, &SendMsgAnswer);
}
return;
}
}
if (hGetWordLib == 0) { if (hGetWordLib == 0) {
hGetWordLib = LoadLibrary(GlobalData->LibName); hGetWordLib = LoadLibrary(GlobalData->LibName);
if (hGetWordLib) { if (hGetWordLib) {
@ -38,7 +65,6 @@ static void SendWordToServer()
GlobalData->CurMod.Pt = GlobalData->LastPt; GlobalData->CurMod.Pt = GlobalData->LastPt;
GetWordProc(&(GlobalData->CurMod)); GetWordProc(&(GlobalData->CurMod));
if (GlobalData->CurMod.WordLen > 0) { if (GlobalData->CurMod.WordLen > 0) {
DWORD SendMsgAnswer;
SendMessageTimeout(GlobalData->ServerWND, WM_MY_SHOW_TRANSLATION, 0, 0, SMTO_ABORTIFHUNG, MOUSEOVER_INTERVAL, &SendMsgAnswer); SendMessageTimeout(GlobalData->ServerWND, WM_MY_SHOW_TRANSLATION, 0, 0, SMTO_ABORTIFHUNG, MOUSEOVER_INTERVAL, &SendMsgAnswer);
} }
} }
@ -142,6 +168,7 @@ BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
if(hSynhroMutex==0) return(FALSE); if(hSynhroMutex==0) return(FALSE);
} }
ThTypes_Init(); ThTypes_Init();
uGdAskMessage = RegisterWindowMessage(GD_MESSAGE_NAME);
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH: