home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1996 November
/
VPR9611A.ISO
/
ols
/
win31
/
hmize120
/
hmize120.lzh
/
SOURCE.LZH
/
EDITHOOK.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-15
|
17KB
|
667 lines
#include <windows.h>
extern "C" {
#include <ime.h>
}
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "edithook.h"
#include "editedit.h"
#include "listhook.h"
#include "..\hidemaru\menu.h"
BOOL fSelecting;
BOOL fMenuFilterInstalled;
static DWORD dw;
void SendVKey( int vkey ) {
BYTE bCtrl = abKeyState[ VK_CONTROL ];
abKeyState[ VK_CONTROL ] = 0;
SetKeyboardState( (BYTE FAR*)abKeyState );
SendMessage( hwndTarget, WM_KEYDOWN, vkey, 1L );
abKeyState[ VK_CONTROL ] = bCtrl;
SetKeyboardState( (BYTE FAR*)abKeyState );
}
void SendChar( int ch ) {
BYTE bCtrl = abKeyState[ VK_CONTROL ];
abKeyState[ VK_CONTROL ] = 0;
SetKeyboardState( (BYTE FAR*)abKeyState );
SendMessage( hwndTarget, WM_CHAR, ch, 1L );
abKeyState[ VK_CONTROL ] = bCtrl;
SetKeyboardState( (BYTE FAR*)abKeyState );
}
void SendCtrlVKey( int vkey ) {
BYTE bCtrl = abKeyState[ VK_CONTROL ];
abKeyState[ VK_CONTROL ] = 0x80;
SetKeyboardState( (BYTE FAR*)abKeyState );
SendMessage( hwndTarget, WM_KEYDOWN, vkey, 1L );
abKeyState[ VK_CONTROL ] = bCtrl;
SetKeyboardState( (BYTE FAR*)abKeyState );
}
void SendCtrlShiftVKey( int vkey ) {
BYTE bCtrl = abKeyState[ VK_CONTROL ];
BYTE bShift = abKeyState[ VK_SHIFT ];
abKeyState[ VK_CONTROL ] = 0x80;
abKeyState[ VK_SHIFT ] = 0x80;
SetKeyboardState( (BYTE FAR*)abKeyState );
SendMessage( hwndTarget, WM_KEYDOWN, vkey, 1L );
abKeyState[ VK_CONTROL ] = bCtrl;
abKeyState[ VK_SHIFT ] = bShift;
SetKeyboardState( (BYTE FAR*)abKeyState );
}
void SendShiftVKey( int vkey ) {
BYTE bShift = abKeyState[ VK_SHIFT ];
BYTE bCtrl = abKeyState[ VK_CONTROL ];
abKeyState[ VK_CONTROL ] = 0;
abKeyState[ VK_SHIFT ] = 0x80;
SetKeyboardState( (BYTE FAR*)abKeyState );
SendMessage( hwndTarget, WM_KEYDOWN, vkey, 1L );
abKeyState[ VK_SHIFT ] = bShift;
abKeyState[ VK_CONTROL ] = bCtrl;
SetKeyboardState( (BYTE FAR*)abKeyState );
}
void SendSelectingVKey( int vkey ) {
if( fSelecting ) {
SendShiftVKey( vkey );
} else {
SendVKey( vkey );
}
}
void SendSelectingCtrlVKey( int vkey ) {
BYTE bCtrl = abKeyState[ VK_CONTROL ];
BYTE bShift = abKeyState[ VK_SHIFT ];
abKeyState[ VK_CONTROL ] = 0x80;
if( fSelecting ) {
abKeyState[VK_SHIFT] = 0x80;
}
SetKeyboardState( (BYTE FAR*)abKeyState );
SendMessage( hwndTarget, WM_KEYDOWN, vkey, 1L );
abKeyState[ VK_CONTROL ] = bCtrl;
abKeyState[ VK_SHIFT ] = bShift;
SetKeyboardState( (BYTE FAR*)abKeyState );
}
static void EndSelection( void ) {
fSelecting = FALSE;
dw = SendMessage( hwndTarget, EM_GETSEL, 0, 0L );
if( HIWORD(dw) != LOWORD(dw) ) {
SendMessage( hwndTarget, EM_SETSEL, TRUE, MAKELONG( LOWORD(dw), LOWORD(dw) ) );
}
}
/************************************************************/
static char szCollon[] = ":";
char* GetCommandString( int cmd ) {
static char szResult[100];
LoadString( hInstance, cmd, szResult, sizeof(szResult) );
if( cmd >= CMD_MENU_1 && cmd <= CMD_MENU_8 ) {
if( pshare->key.aszMenuTitle[cmd - CMD_MENU_1][0] != '\0' ) {
lstrcat( szResult, szCollon );
lstrcat( szResult, pshare->key.aszMenuTitle[cmd - CMD_MENU_1] );
}
} else if( (cmd >= CMD_MACRO_1 && cmd <= CMD_MACRO_15)
|| (cmd >= CMD_MACRO_16 && cmd <= CMD_MACRO_30) ) {
LPSTR pszTitle;
if( cmd < CMD_MACRO_16 ) {
pszTitle = pshare->amacro[cmd - CMD_MACRO_1].szTitle;
} else {
pszTitle = pshare->amacro2[cmd - CMD_MACRO_16].szTitle;
}
if( pszTitle[0] != '\0' ) {
lstrcat( szResult, szCollon );
lstrcat( szResult, pszTitle );
}
}
return szResult;
}
static KEYMENU FAR* pKeyMenu;
static int cMenuItem;
static FARPROC pfnOldHook;
static DWORD timeButtonDown;
extern "C" void FAR PASCAL _export MenuFilter( int nCode, WPARAM wParam, LPARAM lParam ) {
if( nCode >= 0 ) {
MSG FAR* pmsg = (MSG FAR*)lParam;
if( pmsg->message == WM_RBUTTONUP ) {
if( GetCurrentTime() - timeButtonDown <= 350 ) {
pmsg->message = WM_MOUSEMOVE;
}
} else if( pmsg->message == WM_CHAR ) {
if( pmsg->wParam <= 0x1A ) {
pmsg->wParam += '@';
}
}
} else {
DefHookProc( nCode, wParam, lParam, &pfnOldHook );
}
}
static BOOL fRightButton;
static void near CheckMacroTitle( char* psz, int cmd ) {
if( cmd >= CMD_MACRO_1 && cmd <= CMD_MACRO_15 ) {
lstrcpy( psz + 3, pshare->amacro[cmd - CMD_MACRO_1].szTitle );
} else if( cmd >= CMD_MACRO_16 && cmd <= CMD_MACRO_30 ) {
lstrcpy( psz + 3, pshare->amacro2[cmd - CMD_MACRO_16].szTitle );
}
}
static HMENU near CreatePopupSub( int cmd ) {
HMENU hPopup = CreatePopupMenu();
KEYMENU FAR* pKeyMenu = & pshare->key.amenu[ cmd - CMD_MENU_1 ];
int cMenuItem = 0;
while( pKeyMenu->acmd[ cMenuItem ] != 0 ) {
cMenuItem ++;
if( cMenuItem >= KEYMENU_NUM ) {
break;
}
}
char sz[50];
sz[0] = '&';
sz[2] = '\t';
for( int i = 0; i < cMenuItem; i++ ) {
if( pKeyMenu->avkey[i] == (BYTE)-1 ) {
AppendMenu( hPopup, MF_SEPARATOR, 0, NULL );
} else {
sz[1] = pKeyMenu->avkey[i];
strcpy( sz + 3, GetCommandString( pKeyMenu->acmd[i] ) );
if( pKeyMenu->acmd[i] >= CMD_MENU_1 && pKeyMenu->acmd[i] <= CMD_MENU_8 ) {
if( pKeyMenu->acmd[i] != cmd ) {
AppendMenu( hPopup, MF_POPUP
, CreatePopupSub( pKeyMenu->acmd[i] )
, sz + fRightButton * 3 );
}
} else {
CheckMacroTitle( sz, pKeyMenu->acmd[i] );
AppendMenu( hPopup, MF_BYCOMMAND | MF_ENABLED | MF_STRING
, pKeyMenu->acmd[i]
, sz + fRightButton * 3 );
}
}
}
return hPopup;
}
static WPARAM ConvertWParam( WPARAM wParam ) {
if( wParam <= 'Z' - '@' + 1 + 5 ) {
wParam += '@';
} else if( wParam >= 'a' && wParam <= 'z' ) {
wParam = wParam - 'a' + 'A';
}
return wParam;
}
static void ProcessCommand( int cmd ) {
GetKeyboardState( (BYTE FAR*)abKeyState );
if( wndclass == CLASS_EDIT ) {
DebugOut( "EditCommand\x0d\x0a" );
ProcessEditCommand( cmd );
} else {
DebugOut( "ListBoxCommand\x0d\x0a" );
ProcessListBoxCommand( cmd );
}
}
#ifndef VK_KANA
#define VK_KANA 0x15
#endif
void CommandKeyMenu( WORD cmd ) {
pKeyMenu = & pshare->key.amenu[ cmd - CMD_MENU_1 ];
cMenuItem = 0;
while( pKeyMenu->acmd[ cMenuItem ] != 0 ) {
cMenuItem ++;
if( cMenuItem >= KEYMENU_NUM ) {
break;
}
}
timeButtonDown = GetCurrentTime();
fRightButton = (GetKeyState( VK_RBUTTON ) & 0x8000) != 0;
BOOL fClipbrdMenu;
if( wndclass == CLASS_EDIT ) {
dw = SendMessage( hwndTarget, EM_GETSEL, 0, 0L );
fClipbrdMenu = fRightButton && (HIWORD(dw) != LOWORD(dw));
} else {
fClipbrdMenu = FALSE;
}
if( cMenuItem == 0 && !fRightButton ) {
MessageBeep(0);
return;
}
{
MSG msg;
PeekMessage( &msg, NULL, WM_CHAR, WM_CHAR, PM_REMOVE );
}
HMENU hPopup = CreatePopupMenu();
int cyMenu = GetSystemMetrics( SM_CYMENU );
int cySeparator = cyMenu / 2 - 2;
int cyPopup = cyMenu / 4;
int cyMenuUp = 0;
int i;
cyMenu += 1;
if( fClipbrdMenu ) {
BOOL fLastSeparator = FALSE;
int cMenu = 0;
for( i = 0; i < KEYMENU_NUM; i++ ) {
if( pshare->menuRightButton.acmd[i] == 0 ) {
break;
}
if( pshare->menuRightButton.avkey[i] == (BYTE)-1 ) {
fLastSeparator = TRUE;
AppendMenu( hPopup, MF_SEPARATOR, 0, NULL );
cyPopup += cySeparator;
cMenu ++;
} else {
int cmd = pshare->menuRightButton.acmd[i];
fLastSeparator = FALSE;
AppendMenu( hPopup, MF_BYCOMMAND | MF_ENABLED | MF_STRING
, cmd
, GetCommandString( pshare->menuRightButton.acmd[i] ) );
cyPopup += cyMenu;
if( cmd == CMD_COPY ) {
cyMenuUp = cyPopup - cyMenu;
}
cMenu ++;
}
}
if( fLastSeparator ) {
DeleteMenu( hPopup, cMenu - 1, MF_BYPOSITION );
cyPopup -= cySeparator;
}
} else {
char sz[50];
if( pshare->key.fTitledMenu ) {
sz[0] = '\t';
if( pshare->key.aszMenuTitle[cmd - CMD_MENU_1][0] == '\0' ) {
LoadString( hInstance, cmd, sz + 1, sizeof(sz) - 1 );
} else {
lstrcpy( sz + 1, pshare->key.aszMenuTitle[cmd - CMD_MENU_1] );
}
AppendMenu( hPopup, MF_BYCOMMAND | MF_ENABLED | MF_STRING
, 0, sz + fRightButton );
AppendMenu( hPopup, MF_SEPARATOR, 0, NULL );
cyPopup += cyMenu + cySeparator;
}
sz[0] = '&';
sz[2] = '\t';
for( int i = 0; i < cMenuItem; i++ ) {
if( pKeyMenu->avkey[i] == (BYTE)-1 ) {
AppendMenu( hPopup, MF_SEPARATOR, 0, NULL );
cyPopup += cySeparator;
} else {
sz[1] = pKeyMenu->avkey[i];
strcpy( sz + 3, GetCommandString( pKeyMenu->acmd[i] ) );
if( pKeyMenu->acmd[i] >= CMD_MENU_1 && pKeyMenu->acmd[i] <= CMD_MENU_8 ) {
AppendMenu( hPopup, MF_POPUP
, CreatePopupSub( pKeyMenu->acmd[i] )
, sz + fRightButton * 3 );
} else {
CheckMacroTitle( sz, pKeyMenu->acmd[i] );
AppendMenu( hPopup, MF_BYCOMMAND | MF_ENABLED | MF_STRING
, pKeyMenu->acmd[i]
, sz + fRightButton * 3 );
}
cyPopup += cyMenu;
}
}
}
int x,y;
BOOL fDelay = FALSE;
if( fRightButton ) {
POINT pt;
GetCursorPos( &pt );
x = pt.x;
y = pt.y;
y -= cyMenuUp;
} else {
RECT rc;
GetWindowRect( hwndTarget, &rc );
x = rc.left;
y = rc.top;
if( GetKeyState( VK_CONTROL ) & 0x8000 ) fDelay = TRUE;
}
{
int cyScreen = GetSystemMetrics( SM_CYSCREEN );
if( y + cyPopup > cyScreen ) {
y = cyScreen - cyPopup;
}
}
MSG msg;
if( fDelay && pshare->key.adelay[ cmd - CMD_MENU_1 ] != 0 ) {
// ImeSaveMode();
DWORD timeStart = GetCurrentTime();
while( GetCurrentTime() - timeStart < (WORD)pshare->key.adelay[ cmd - CMD_MENU_1 ] * 100 ) {
if( PeekMessage( &msg, NULL, NULL, NULL, PM_REMOVE ) ) {
TranslateMessage( &msg );
if( msg.message == WM_KEYUP
|| msg.message == WM_COMMAND ) {
// nothing to do
} else if( msg.message == WM_KEYDOWN
|| msg.message == WM_CHAR ) {
if( msg.wParam == VK_ESCAPE ) {
PeekMessage( &msg, NULL, WM_CHAR, WM_CHAR, PM_REMOVE );
DestroyMenu( hPopup );
return;
}
msg.wParam = ConvertWParam( msg.wParam );
for( int i = 0; i < cMenuItem; i++ ) {
if( pKeyMenu->avkey[i] == msg.wParam ) {
PeekMessage( &msg, NULL, WM_CHAR, WM_CHAR, PM_REMOVE );
ProcessCommand( pKeyMenu->acmd[i] );
DestroyMenu( hPopup );
return;
}
}
} else {
DispatchMessage( &msg );
}
}
}
}
pfnOldHook = SetWindowsHook( WH_GETMESSAGE, (FARPROC)MenuFilter );
HWND hwndMenuParent = CreateWindow( "Edit", "", WS_CHILD, 0, 0, 0, 0
, hwndTarget, 1, hInstance, NULL );
if( hwndMenuParent == NULL ) {
DebugOut( "MenuParent creation failed.\x0d\x0a" );
}
TrackPopupMenu( hPopup, TPM_LEFTALIGN | (fRightButton ? TPM_RIGHTBUTTON : TPM_LEFTBUTTON), x, y, 0, hwndMenuParent, NULL );
UnhookWindowsHook( WH_GETMESSAGE, (FARPROC)MenuFilter );
DestroyMenu( hPopup );
if( PeekMessage( &msg, hwndMenuParent, WM_COMMAND, WM_COMMAND, PM_REMOVE ) ) {
ProcessCommand( msg.wParam );
} else {
DebugOut( "WM_COMMAND not found after TrackPopupMenu\x0d\x0a" );
}
DestroyWindow( hwndMenuParent );
}
/*********************/
static int xInput;
BOOL ProcessEditCommand( int cmd ) {
switch( cmd ) {
case CMD_CURSOR_UP:
case CMD_FAST_UP:
SendSelectingVKey( VK_UP );
break;
case CMD_CURSOR_DOWN:
case CMD_FAST_DOWN:
SendSelectingVKey( VK_DOWN );
break;
case CMD_CURSOR_RIGHT:
SendSelectingVKey( VK_RIGHT );
break;
case CMD_CURSOR_LEFT:
SendSelectingVKey( VK_LEFT );
break;
case CMD_WORD_RIGHT:
SendSelectingCtrlVKey( VK_RIGHT );
break;
case CMD_WORD_LEFT:
SendSelectingCtrlVKey( VK_LEFT );
break;
case CMD_GO_LINE_TOP:
case CMD_GO_LINE_TOP2:
SendSelectingVKey( VK_HOME );
break;
case CMD_GO_LINE_END:
case CMD_GO_LINE_END2:
SendSelectingVKey( VK_END );
break;
case CMD_GO_FILE_TOP:
SendSelectingCtrlVKey( VK_HOME );
break;
case CMD_GO_FILE_END:
SendSelectingCtrlVKey( VK_END );
break;
case CMD_NEXT_PAGE:
case CMD_HALF_NEXT_PAGE:
SendSelectingVKey( VK_NEXT );
break;
case CMD_PREV_PAGE:
case CMD_HALF_PREV_PAGE:
SendSelectingVKey( VK_PRIOR );
break;
case CMD_BEGINSELECTION:
fSelecting = TRUE;
break;
case CMD_COPY:
DebugOut( "CMD_COPY\x0d\x0a" );
SendMessage( hwndTarget, WM_COPY, 0, 0L );
EndSelection();
break;
case CMD_PASTE:
SendMessage( hwndTarget, WM_PASTE, 0, 0L );
fSelecting = FALSE;
break;
case CMD_CUT:
DebugOut( "CMD_CUT\x0d\x0a" );
SendMessage( hwndTarget, WM_CUT, 0, 0L );
fSelecting = FALSE;
break;
case CMD_DELETE:
SendVKey( VK_DELETE );
fSelecting = FALSE;
break;
case CMD_DELETE_WORD:
SendCtrlShiftVKey( VK_RIGHT );
SendVKey( VK_DELETE );
break;
case CMD_DELETE_WORD_ALL:
SendCtrlVKey( VK_LEFT );
SendCtrlShiftVKey( VK_RIGHT );
SendVKey( VK_DELETE );
break;
case CMD_DELETE_WORD_FRONT:
SendCtrlShiftVKey( VK_RIGHT );
SendVKey( VK_DELETE );
break;
case CMD_DELETE_LINE:
SendVKey( VK_HOME );
SendShiftVKey( VK_DOWN );
SendVKey( VK_DELETE );
break;
case CMD_DELETE_AFTER:
SendShiftVKey( VK_END );
SendVKey( VK_DELETE );
break;
case CMD_DELETE_BEFORE:
SendShiftVKey( VK_HOME );
SendVKey( VK_DELETE );
break;
case CMD_RETURN:
SendVKey( VK_RETURN );
break;
case CMD_TAB:
SendVKey( VK_TAB );
break;
case CMD_BACKSPACE:
SendChar( VK_BACK );
break;
case CMD_UNDO:
SendMessage( hwndTarget, WM_UNDO, 0, 0L );
break;
case CMD_TOUPPER:
ToUpper();
break;
case CMD_TOLOWER:
ToLower();
break;
case CMD_TOHANKAKU:
ToHankaku();
break;
case CMD_TOZENKAKUHIRA:
ToZenkakuHira();
break;
case CMD_TOZENKAKUKATA:
ToZenkakuKata();
break;
case CMD_CASE_CHANGE:
CaseChange();
break;
case CMD_SELECT_ALL:
SendMessage( hwndTarget, EM_SETSEL, TRUE, MAKELONG( 0, -1 ) );
break;
case CMD_CAPSLOCK_FORGOT:
GetKeyboardState( (BYTE FAR*)abKeyState );
abKeyState[ VK_CAPITAL ] ^= 1;
SetKeyboardState( (BYTE FAR*)abKeyState );
if( xInput != -1 ) {
CapsLockForget( xInput );
xInput = -1;
DebugOut( "xInput reset\x0d\x0a" );
}
break;
default:
if( cmd >= CMD_MENU_1 && cmd <= CMD_MENU_8 ) {
CommandKeyMenu( cmd );
break;
}
MessageBeep(0);
return FALSE;
}
return TRUE;
}
BOOL EditHookProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam ) {
static int cmd;
switch( message ) {
case WM_KEYDOWN:
DebugOut( "WM_KEYDOWN " );
{
static char sz[20];
itoa( wParam, sz, 10 );
DebugOut( sz );
DebugOut( "\x0d\x0a" );
}
if( wParam == VK_DELETE ) {
fSelecting = FALSE;
} else if( wParam >= VK_PRIOR && wParam <= VK_DOWN ) {
if( fSelecting ) {
SendSelectingVKey( wParam );
xInput = -1;
DebugOut( "xInput reset\x0d\x0a" );
return TRUE;
}
} else if( wParam >= VK_F1 ) {
cmd = GetCommand( wParam );
if( cmd != 0 ) {
if( ProcessEditCommand( cmd ) ) {
return TRUE;
}
}
}
if( wParam == VK_BACK
|| (wParam >= VK_CAPITAL && wParam <= VK_HELP)
|| wParam >= VK_F1 && wParam <= VK_F24 ) {
xInput = -1;
DebugOut( "xInput reset\x0d\x0a" );
}
break;
case WM_CHAR:
DebugOut( "WM_CHAR " );
{
static char sz[20];
itoa( wParam, sz, 10 );
DebugOut( sz );
DebugOut( "\x0d\x0a" );
}
if( wParam < ' ' ) {
if( (afConfig & FLAG_HOOK_CTRL_XCV) == 0 ) {
if( wParam == 'X' - '@'
|| wParam == 'C' - '@'
|| wParam == 'V' - '@' ) {
break;
}
}
cmd = GetCommand( wParam + '@' );
if( cmd != 0 ) {
if( ProcessEditCommand( cmd ) ) {
return TRUE;
}
}
xInput = -1;
DebugOut( "xInput reset\x0d\x0a" );
} else {
if( xInput == -1 ) {
dw = SendMessage( hwnd, EM_GETSEL, 0, 0L );
xInput = (int)(WORD)dw;
DebugOut( "xInput set\x0d\x0a" );
}
}
fSelecting = FALSE;
break;
case WM_RBUTTONDOWN:
if( GetFocus() == hwnd ) {
PostMessage( hwndClient, WM_USER, (WPARAM)hwnd, 0 );
}
break;
case WM_LBUTTONDOWN:
xInput = -1;
break;
}
return FALSE;
}
void EditHookRButtonDown( HWND hwnd ) {
if( GetFocus() == hwnd ) {
if( afConfig & FLAG_HOOK_RBUTTON ) {
GetKeyboardState( (BYTE FAR*)abKeyState );
dw = SendMessage( hwnd, EM_GETSEL, 0, 0L );
if( HIWORD(dw) != LOWORD(dw) ) {
CommandKeyMenu( CMD_MENU_1 );
} else {
ProcessEditCommand( pshare->key.mouse[1] );
}
}
}
}