forked from ProgerXP/Notepad2e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhl_helper.cpp
More file actions
225 lines (222 loc) · 8.82 KB
/
hl_helper.cpp
File metadata and controls
225 lines (222 loc) · 8.82 KB
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
#include <windows.h>
#include <commctrl.h>
#include <shlwapi.h>
#include <commdlg.h>
#include <string>
#include <Shlobj.h>
#include <algorithm>
#include <cassert>
extern "C"
{
extern UINT _hl_ctx_menu_type ;
extern VOID HL_Trace ( const char *fmt , ... );
#if 0
BOOL HL_Explorer_cxt_menu ( LPCWSTR path, void *parentWindow )
{
ITEMIDLIST *id = 0;
std::wstring windowsPath = path;
std::replace ( windowsPath.begin(), windowsPath.end(), '/', '\\' );
HRESULT result = SHParseDisplayName ( windowsPath.c_str(), 0, &id, 0, 0 );
if ( !SUCCEEDED ( result ) || !id ) {
return false;
}
bool out = true;
IShellFolder *ifolder = 0;
LPCITEMIDLIST idChild = 0;
result = SHBindToParent ( id, IID_IShellFolder, ( void ** ) &ifolder, &idChild );
if ( !SUCCEEDED ( result ) || !ifolder ) {
out = false;
}
if ( out ) {
IContextMenu2 *imenu = 0;
result = ifolder->GetUIObjectOf ( ( HWND ) parentWindow
, 1
, ( const ITEMIDLIST ** ) &idChild
, IID_IContextMenu
, 0
, ( void ** ) &imenu );
if ( !SUCCEEDED ( result ) || !ifolder ) {
out = false;
}
if ( out ) {
HMENU hMenu = CreatePopupMenu();
if ( !hMenu ) {
out = false;
}
if ( out && SUCCEEDED ( imenu->QueryContextMenu ( hMenu, 0, 1, 0x7FFF, _hl_ctx_menu_type ) ) ) {
POINT pt ;
GetCursorPos ( &pt );
int iCmd = TrackPopupMenuEx ( hMenu, TPM_RETURNCMD, pt.x, pt.y, ( HWND ) parentWindow, NULL );
if ( iCmd > 0 ) {
CMINVOKECOMMANDINFOEX info = { 0 };
info.cbSize = sizeof ( info );
info.fMask = CMIC_MASK_UNICODE;
info.hwnd = ( HWND ) parentWindow;
info.lpVerb = MAKEINTRESOURCEA ( iCmd - 1 );
info.lpVerbW = MAKEINTRESOURCEW ( iCmd - 1 );
info.nShow = SW_SHOWNORMAL;
imenu->InvokeCommand ( ( LPCMINVOKECOMMANDINFO ) &info );
}
}
if ( hMenu ) {
DestroyMenu ( hMenu );
}
}
if ( imenu ) {
imenu->Release();
}
}
if ( ifolder ) {
ifolder->Release();
}
return out;
}
#else
LPCONTEXTMENU2 g_IContext2 = NULL;
LPCONTEXTMENU3 g_IContext3 = NULL;
//
VOID Invoke ( int cmd , LPCONTEXTMENU menu , HWND win )
{
if ( cmd > 0 ) {
CMINVOKECOMMANDINFOEX info = { 0 };
info.cbSize = sizeof ( info );
info.fMask = CMIC_MASK_UNICODE;
info.hwnd = win;
info.lpVerb = MAKEINTRESOURCEA ( cmd - 1 );
info.lpVerbW = MAKEINTRESOURCEW ( cmd - 1 );
info.nShow = SW_SHOWNORMAL;
menu->InvokeCommand ( ( LPCMINVOKECOMMANDINFO ) &info );
}
}
//
LRESULT CALLBACK HookWndProc ( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
switch ( message ) {
case WM_MENUCHAR: // only supported by IContextMenu3
if ( g_IContext3 ) {
LRESULT lResult = 0;
g_IContext3->HandleMenuMsg2 ( message, wParam, lParam, &lResult );
return ( lResult );
}
break;
case WM_DRAWITEM:
case WM_MEASUREITEM:
if ( wParam ) {
break; // if wParam != 0 then the message is not menu-related
}
case WM_INITMENUPOPUP:
if ( g_IContext2 ) {
g_IContext2->HandleMenuMsg ( message, wParam, lParam );
} else { // version 3
g_IContext3->HandleMenuMsg ( message, wParam, lParam );
}
return ( message == WM_INITMENUPOPUP ? 0 : TRUE ); // inform caller that
// we handled WM_INITPOPUPMENU by ourself
break;
default:
break;
}
// call original WndProc of window to prevent undefined bevhaviour
//
/*of window
return ::CallWindowProc ( ( WNDPROC ) GetProp ( hWnd, TEXT ( "OldWndProc" ) ),
hWnd, message, wParam, lParam );
*/
return DefWindowProc ( hWnd, message,
wParam, lParam );
}
BOOL GetContextMenu ( LPCWSTR path, void **ppContextMenu, int &iMenuType )
{
*ppContextMenu = NULL;
LPCONTEXTMENU icm1 = NULL;
// first we retrieve the normal IContextMenu
// interface (every object should have it)
LPCITEMIDLIST idChild = 0;
IShellFolder *ifolder = 0;
ITEMIDLIST *id = 0;
std::wstring windowsPath = path;
std::replace ( windowsPath.begin(), windowsPath.end(), '/', '\\' );
HRESULT result = SHParseDisplayName ( windowsPath.c_str(), 0, &id, 0, 0 );
if ( !SUCCEEDED ( result ) || !id ) {
return false;
}
result = SHBindToParent ( id, IID_IShellFolder, ( void ** ) &ifolder, &idChild );
ifolder->GetUIObjectOf ( NULL,
1,
( LPCITEMIDLIST * ) &idChild,
IID_IContextMenu,
NULL,
( void ** ) &icm1 );
if ( icm1 ) {
// since we got an IContextMenu interface we can
// now obtain the higher version interfaces via that
if ( icm1->QueryInterface ( IID_IContextMenu3, ppContextMenu ) == NOERROR ) {
iMenuType = 3;
} else if ( icm1->QueryInterface ( IID_IContextMenu2,
ppContextMenu ) == NOERROR ) {
iMenuType = 2;
}
if ( *ppContextMenu ) {
icm1->Release(); // we can now release version 1 interface,
}
// cause we got a higher one
else {
iMenuType = 1;
*ppContextMenu = icm1; // since no higher versions were found
} // redirect ppContextMenu to version 1 interface
} else {
return ( FALSE ); // something went wrong
}
return ( TRUE ); // success
}
//UINT ShowContextMenu(CWnd *pWnd, CPoint pt)
BOOL HL_Explorer_cxt_menu ( LPCWSTR path, void *parentWindow )
{
int iMenuType = 0;
// to know which version of IContextMenu is supported
LPCONTEXTMENU pContextMenu;
// common pointer to IContextMenu and higher version interface
if ( !GetContextMenu ( path , ( void ** ) &pContextMenu, iMenuType ) ) {
return FALSE; // something went wrong
}
//
HMENU h_menu = CreatePopupMenu();
// lets fill the our popupmenu
pContextMenu->QueryContextMenu ( h_menu ,
0, 1, 0x7FFF, _hl_ctx_menu_type );
WNDPROC OldWndProc = NULL;
//
OSVERSIONINFOEX osvi;
ZeroMemory ( &osvi, sizeof ( OSVERSIONINFOEX ) );
osvi.dwOSVersionInfoSize = sizeof ( OSVERSIONINFOEX );
GetVersionEx ( ( OSVERSIONINFO * ) &osvi );
BOOL bIsWindowsXPorLater =
( ( osvi.dwMajorVersion > 5 ) ||
( ( osvi.dwMajorVersion == 5 ) && ( osvi.dwMinorVersion >= 1 ) ) );
HL_Trace ( "win version %d (%d - %d) . XP ? : %d" , WINVER , osvi.dwMajorVersion , osvi.dwMinorVersion , !bIsWindowsXPorLater );
#if 1
if ( iMenuType > 1 ) { // only version 2 and 3 supports menu messages
OldWndProc = ( WNDPROC ) SetWindowLong ( ( HWND ) parentWindow,
GWL_WNDPROC, ( DWORD ) HookWndProc );
if ( iMenuType == 2 ) {
g_IContext2 = ( LPCONTEXTMENU2 ) pContextMenu;
} else { // version 3
g_IContext3 = ( LPCONTEXTMENU3 ) pContextMenu;
}
} else {
OldWndProc = NULL;
}
#endif
POINT pt ;
GetCursorPos ( &pt );
int iCmd = TrackPopupMenuEx ( h_menu, TPM_RETURNCMD, pt.x, pt.y, ( HWND ) parentWindow, NULL );
Invoke ( iCmd , pContextMenu, ( HWND ) parentWindow );
if ( OldWndProc ) {
SetWindowLong ( ( HWND ) parentWindow, GWL_WNDPROC, ( DWORD ) OldWndProc );
}
pContextMenu->Release();
return ( TRUE );
}
#endif
}