From a523b8e3d725295613888ba460d26f39274583db Mon Sep 17 00:00:00 2001 From: Abs62 Date: Wed, 8 Jun 2011 20:12:19 +0400 Subject: [PATCH] Add interface for other programs to send to GD a word under cursor --- mouseover_win32/GDDataTranfer.h | 46 +++++++++++++++++++++++++++++++++ mouseover_win32/TextOutSpy.c | 35 ++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 mouseover_win32/GDDataTranfer.h diff --git a/mouseover_win32/GDDataTranfer.h b/mouseover_win32/GDDataTranfer.h new file mode 100644 index 00000000..84bc483d --- /dev/null +++ b/mouseover_win32/GDDataTranfer.h @@ -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 diff --git a/mouseover_win32/TextOutSpy.c b/mouseover_win32/TextOutSpy.c index 967879b1..416b3f63 100644 --- a/mouseover_win32/TextOutSpy.c +++ b/mouseover_win32/TextOutSpy.c @@ -1,8 +1,9 @@ #include "TextOutSpy.h" #include "ThTypes.h" - +#include "GDDataTranfer.h" const int MOUSEOVER_INTERVAL = 300; +const int REQUEST_MESSAGE_INTERVAL = 500; const int WM_MY_SHOW_TRANSLATION = WM_USER + 301; HINSTANCE g_hInstance = NULL; @@ -11,19 +12,45 @@ HINSTANCE hGetWordLib = 0; UINT_PTR TimerID = 0; typedef void (*GetWordProc_t)(TCurrentMode *); GetWordProc_t GetWordProc = NULL; +GDDataStruct gds; +UINT uGdAskMessage; +WCHAR Buffer[256]; -static HWND GetWindowFromPoint(POINT pt) { +static HWND GetWindowFromPoint(POINT pt) +{ HWND WndParent,WndChild; WndParent = WindowFromPoint(pt); if(WndParent == NULL) return WndParent; ScreenToClient(WndParent, &pt); - WndChild=RealChildWindowFromPoint(WndParent, pt); + WndChild = RealChildWindowFromPoint(WndParent, pt); if(WndChild == NULL) return WndParent; return WndChild; }; 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) { hGetWordLib = LoadLibrary(GlobalData->LibName); if (hGetWordLib) { @@ -38,7 +65,6 @@ static void SendWordToServer() GlobalData->CurMod.Pt = GlobalData->LastPt; GetWordProc(&(GlobalData->CurMod)); if (GlobalData->CurMod.WordLen > 0) { - DWORD 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); } ThTypes_Init(); + uGdAskMessage = RegisterWindowMessage(GD_MESSAGE_NAME); break; case DLL_PROCESS_DETACH: