summaryrefslogtreecommitdiff
path: root/plugins/HistoryPlusPlus/hpp_mescatcher.pas
blob: bb91be9aa65afd216fbb9884371a48288c2eb987 (plain)
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
(*
    History++ plugin for Miranda IM: the free IM client for Microsoft* Windows*

    Copyright (C) 2006-2009 theMIROn, 2003-2006 Art Fedorov.
    History+ parts (C) 2001 Christian Kastner

    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
*)

{-----------------------------------------------------------------------------
 hpp_mescatcher (historypp project)

 Version:   1.0
 Created:   09.12.2006
 Author:    theMIROn

 [ Description ]

 Hidden window, used for catching WM messages and hotkeys

 [ History ]

 1.0 (09.12.2006)
   First version

 [ Modifications ]
 none

 [ Known Issues ]
 none

 Contributors: theMIROn
-----------------------------------------------------------------------------}

unit hpp_mescatcher;

interface

{$I compilers.inc}
{.$DEFINE USE_CUSTOMIDLEHOOK}

uses
  Windows, Messages, Classes, Controls;

const
  hppWindowClassName = 'History++ MainWindow';

var
  hppMainWindow: HWND = 0;

procedure hppWakeMainThread(Sender: TObject);
function hppRegisterMainWindow: Boolean;
function hppUnregisterMainWindow: Boolean;

implementation

uses Forms, Themes;

{$IFDEF USE_CUSTOMIDLEHOOK}
type
  THackApplication = class(TComponent)
  protected
    FxxxxxxxxxHandle: HWnd;
    FxxxxxxxxxBiDiMode: TBiDiMode;
    FxxxxxxxxxBiDiKeyboard: AnsiString;
    FxxxxxxxxxNonBiDiKeyboard: AnsiString;
    FxxxxxxxxxObjectInstance: Pointer;
    FxxxxxxxxxMainForm: TForm;
    FMouseControl: TControl;
  end;
{$ENDIF}

var
  SavedWakeMainThread: TNotifyEvent = nil;
  SavedCheckIniChange: function (var Message: TMessage): Boolean of object = nil;
  {$IFDEF USE_CUSTOMIDLEHOOK}
  ForegroundIdleHook: HHOOK;
  {$ENDIF}

function MainWindowWndProc(hwndDlg: HWND; uMsg: uint; wParam: WPARAM; lParam: LPARAM): lresult; stdcall;
var
  Message: TMessage;
begin
  Result := 0;
  if Assigned(SavedCheckIniChange) then
  begin
    Message.Msg := uMsg;
    SavedCheckIniChange(Message);
  end;
  case uMsg of
    //WM_HOTKEY:
    // place for global hotkeys :)
    //if wParam = Hotkey then
    //  CallService(MS_HPP_SHOWGLOBALSEARCH,0,0);
    CM_WINDOWHOOK: begin
      if (wParam = 0) and not Assigned(SavedCheckIniChange) then
        SavedCheckIniChange := TWindowHook(Pointer(LParam)^);
    end;
    WM_SETTINGCHANGE: begin
      // workaround to force vcl notice mouse setting changed
      if wParam = SPI_SETWHEELSCROLLLINES then
        Mouse.SettingChanged(SPI_GETWHEELSCROLLLINES)
      else
        Mouse.SettingChanged(wParam);
      Result := DefWindowProc(hwndDlg, uMsg, wParam, lParam);
    end;
    WM_FONTCHANGE: begin
      Screen.ResetFonts;
      Result := DefWindowProc(hwndDlg, uMsg, wParam, lParam);
    end;
    WM_THEMECHANGED:
      StyleServices.ApplyThemeChange;
    WM_NULL:
      CheckSynchronize;
    else
      Result := DefWindowProc(hwndDlg, uMsg, wParam, lParam);
  end;
end;

{$IFDEF USE_CUSTOMIDLEHOOK}
function IdleHookProc(code: Integer; wParam: WPARAM; lParam: LPARAM): Integer; stdcall;
var
  Control: TControl;
  MouseControl: TControl;
  CaptureControl: TControl;
  P: TPoint;
begin
  if code < 0 then
  begin
    Result := CallNextHookEx(ForegroundIdleHook,code,wParam,lParam);
    exit;
  end;
  GetCursorPos(P);
  Control := FindDragTarget(P, True);
  MouseControl := THackApplication(Application).FMouseControl;
  CaptureControl := GetCaptureControl;
  if MouseControl <> Control then
  begin
    if ((MouseControl <> nil) and (CaptureControl = nil)) or
       ((CaptureControl <> nil) and (MouseControl = CaptureControl)) then
      MouseControl.Perform(CM_MOUSELEAVE, 0, 0);
    MouseControl := Control;
    if ((MouseControl <> nil) and (CaptureControl = nil)) or
       ((CaptureControl <> nil) and (MouseControl = CaptureControl)) then
      MouseControl.Perform(CM_MOUSEENTER, 0, 0);
    THackApplication(Application).FMouseControl := MouseControl;
  end;
  if Application.ShowHint and (MouseControl = nil) then
    Application.CancelHint;
  Result := 1;
end;
{$ENDIF}

function hppRegisterMainWindow: Boolean;
var
  WndClass: TWNDCLASS;
begin
  Result := False;
  ZeroMemory(@WndClass,SizeOf(WndClass));
  WndClass.lpfnWndProc   := @MainWindowWndProc;
  WndClass.hInstance     := GetModuleHandle(nil);
  WndClass.lpszClassName := hppWindowClassName;
  if Windows.RegisterClass(WndClass) = 0 then exit;
  hppMainWindow := CreateWindow(hppWindowClassName,hppWindowClassName,WS_DISABLED,
                             0,0,0,0,0,0,WndClass.hInstance,nil);
  Result := (hppMainWindow <> 0);
  if Result then
  begin
    // assign Application.CheckIniChange function
    Application.Handle := hppMainWindow;
    Application.Handle := 0;
    SavedWakeMainThread := Classes.WakeMainThread;
    @Classes.WakeMainThread := @hppWakeMainThread;
  end;
  {$IFDEF USE_CUSTOMIDLEHOOK}
  ForegroundIdleHook := SetWindowsHookEx(WH_FOREGROUNDIDLE,
                             @IdleHookProc,0,GetCurrentThreadID);
  {$ENDIF}
end;

function hppUnregisterMainWindow: Boolean;
begin
  if hppMainWindow <> 0 then
  begin
    DestroyWindow(hppMainWindow);
    hppMainWindow := 0;
  end;
  Result := Boolean(Windows.UnregisterClass(hppWindowClassName,GetModuleHandle(nil)));
  Classes.WakeMainThread := SavedWakeMainThread;
  {$IFDEF USE_CUSTOMIDLEHOOK}
  if ForegroundIdleHook <> 0 then UnhookWindowsHookEx(ForegroundIdleHook);
  {$ENDIF}
end;

procedure hppWakeMainThread(Sender: TObject);
begin
  PostMessage(hppMainWindow, WM_NULL, 0, 0);
  if Assigned(SavedWakeMainThread) then
    SavedWakeMainThread(Sender);
end;

end.