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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
{
*****************************************************************************
* Win32WSSpin.pp *
* -------------- *
* *
* *
*****************************************************************************
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit WinCEWSSpin;
{$mode objfpc}{$H+}
interface
uses
// Libs
commctrl,
Windows,
// LCL
Spin, Controls, StdCtrls, LCLType, LazUTF8,
// Widgetset
WSSpin, WSLCLClasses, WinCEInt, WinCEProc,
WinCEWSStdCtrls, WinCEWSControls;
type
{ TWinCEWSCustomFloatSpinEdit }
TWinCEWSCustomFloatSpinEdit = class(TWSCustomFloatSpinEdit)
published
class procedure AdaptBounds(const AWinControl: TWinControl;
var Left, Top, Width, Height: integer; var SuppressMove: boolean); override;
class function CreateHandle(const AWinControl: TWinControl;
const AParams: TCreateParams): HWND; override;
class function GetSelStart(const ACustomEdit: TCustomEdit): integer; override;
class function GetSelLength(const ACustomEdit: TCustomEdit): integer; override;
class function GetText(const AWinControl: TWinControl; var AText: String): Boolean; override;
class function GetValue(const ACustomFloatSpinEdit: TCustomFloatSpinEdit): Double; override;
class procedure SetReadOnly(const ACustomEdit: TCustomEdit; NewReadOnly: boolean); override;
class procedure SetSelStart(const ACustomEdit: TCustomEdit; NewStart: integer); override;
class procedure SetSelLength(const ACustomEdit: TCustomEdit; NewLength: integer); override;
class procedure SetText(const AWinControl: TWinControl; const AText: string); override;
class procedure ShowHide(const AWinControl: TWinControl); override;
class procedure UpdateControl(const ACustomFloatSpinEdit: TCustomFloatSpinEdit); override;
end;
// Prototypes for helper routines
function GetBuddyWindow(AHandle: HWND): HWND;
function SpinWindowProc(Window: HWnd; Msg: UInt; WParam: Windows.WParam;
LParam: Windows.LParam): LResult; cdecl;
function SpinBuddyWindowProc(Window: HWnd; Msg: UInt; WParam: Windows.WParam;
LParam: Windows.LParam): LResult; cdecl;
procedure UpdateFloatSpinEditControl(const Handle: HWND;
const AFloatSpinEdit: TCustomFloatSpinEdit);
procedure UpdateFloatSpinEditText(const ASpinEdit: TCustomFloatSpinEdit;
const ANewValue: Double);
implementation
uses
SysUtils;
{ TWinCEWSCustomFloatSpinEdit }
function GetBuddyWindow(AHandle: HWND): HWND;
begin
Result := SendMessage(AHandle, UDM_GETBUDDY, 0, 0)
end;
function SpinWindowProc(Window: HWnd; Msg: UInt; WParam: Windows.WParam;
LParam: Windows.LParam): LResult; cdecl;
var
BuddyWindow: HWND;
begin
Result := WindowProc(Window, Msg, WParam, LParam);
if Msg = WM_SETFOCUS then
begin
BuddyWindow := GetBuddyWindow(Window);
Windows.SetFocus(BuddyWindow);
// don't select text in edit, if user clicked on the up down and the edit
// was already focused
if HWND(WPARAM)<>BuddyWindow then ;
// for LCL controls this is done in win32callback.inc
Windows.SendMessage(BuddyWindow, EM_SETSEL, 0, -1);
end;
end;
function SpinBuddyWindowProc(Window: HWnd; Msg: UInt; WParam: Windows.WParam;
LParam: Windows.LParam): LResult; cdecl;
var
AWindowInfo: PWindowInfo;
begin
Result := WindowProc(Window, Msg, WParam, LParam);
if Msg = WM_KILLFOCUS then
begin
AWindowInfo := GetWindowInfo(Window);
if AWindowInfo^.AWinControl is TCustomFloatSpinEdit then
UpdateFloatSpinEditControl(AWindowInfo^.AWinControl.Handle,
TCustomFloatSpinEdit(AWindowInfo^.AWinControl));
end;
end;
procedure UpdateFloatSpinEditControl(const Handle: HWND;
const AFloatSpinEdit: TCustomFloatSpinEdit);
var
lWindowInfo: PWindowInfo;
begin
lWindowInfo := GetWindowInfo(Handle);
if lWindowInfo <> @DefaultWindowInfo then
begin
lWindowInfo^.spinValue := AFloatSpinEdit.Value;
UpdateFloatSpinEditText(AFloatSpinEdit, AFloatSpinEdit.Value);
end;
end;
procedure UpdateFloatSpinEditText(const ASpinEdit: TCustomFloatSpinEdit;
const ANewValue: Double);
var
editHandle: HWND;
newValueText: widestring;
begin
editHandle := GetBuddyWindow(ASpinEdit.Handle);
newValueText := UTF8Decode(ASpinEdit.ValueToStr(ANewValue));
Windows.SendMessageW(editHandle, WM_SETTEXT, 0, Windows.LPARAM(PWideChar(newValueText)));
end;
class function TWinCEWSCustomFloatSpinEdit.CreateHandle(const AWinControl: TWinControl;
const AParams: TCreateParams): HWND;
var
Params: TCreateWindowExParams;
begin
// general initialization of Params
PrepareCreateWindow(AWinControl, AParams, Params);
// customization of Params
Params.SubClassWndProc := @SpinWindowProc;
with Params do
begin
Buddy := CreateWindowExW(WS_EX_CLIENTEDGE, 'EDIT',
PWideChar(UTF8Decode(StrCaption)),
Flags Or ES_AUTOHSCROLL,
Left, Top, Width, Height,
Parent, HMENU(Nil), HInstance, Nil);
Window := CreateUpDownControl(Flags or DWORD(WS_BORDER or UDS_ALIGNRIGHT or UDS_ARROWKEYS),
0, 0, // pos - ignored for buddy
0, 0, // size - ignored for buddy
Parent, 0, HInstance, Buddy,
1000, 0, 500);
end;
// create window
FinishCreateWindow(AWinControl, Params, true);
UpdateFloatSpinEditControl(Params.Window, TCustomFloatSpinEdit(AWinControl));
// init buddy
Params.SubClassWndProc := @SpinBuddyWindowProc;
WindowCreateInitBuddy(AWinControl, Params);
Params.BuddyWindowInfo^.isChildEdit := true;
// make possible LCL Wincontrol identification by Buddy handle
// TODO: should move to widget specific SetProp method
SetProp(Params.Buddy, 'WinControl', PtrUInt(AWinControl));
Result := Params.Window;
end;
class procedure TWinCEWSCustomFloatSpinEdit.AdaptBounds(const AWinControl: TWinControl;
var Left, Top, Width, Height: integer; var SuppressMove: boolean);
var
WinHandle, BuddyHandle: HWND;
R: TRect;
WindowWidth: Integer;
begin
// Felipe: With this code, the edit part gets invisible
// Is the same as win32 code, need to check why
WinHandle := AWinControl.Handle;
BuddyHandle := Windows.SendMessage(WinHandle, UDM_GETBUDDY, 0, 0);
GetWindowRect(WinHandle, @R);
WindowWidth := R.Right - R.Left;
MoveWindow(BuddyHandle, Left, Top, Width - WindowWidth + 2, Height, True);
MoveWindow(WinHandle, Left + Width - WindowWidth, Top, WindowWidth, Height, True);
SuppressMove := True;
end;
class function TWinCEWSCustomFloatSpinEdit.GetSelStart(const ACustomEdit: TCustomEdit): integer;
begin
Result := EditGetSelStart(GetBuddyWindow(ACustomEdit.Handle));
end;
class function TWinCEWSCustomFloatSpinEdit.GetSelLength(const ACustomEdit: TCustomEdit): integer;
begin
Result := EditGetSelLength(GetBuddyWindow(ACustomEdit.Handle));
end;
class function TWinCEWSCustomFloatSpinEdit.GetText(const AWinControl: TWinControl;
var AText: string): boolean;
begin
Result := AWinControl.HandleAllocated;
if not Result then
exit;
AText := GetControlText(GetBuddyWindow(AWinControl.Handle));
end;
class function TWinCEWSCustomFloatSpinEdit.GetValue(
const ACustomFloatSpinEdit: TCustomFloatSpinEdit): Double;
begin
Result := GetWindowInfo(ACustomFloatSpinEdit.Handle)^.spinValue;
end;
class procedure TWinCEWSCustomFloatSpinEdit.SetReadOnly(
const ACustomEdit: TCustomEdit; NewReadOnly: boolean);
begin
Windows.SendMessage(GetBuddyWindow(ACustomEdit.Handle), EM_SETREADONLY,
Windows.WPARAM(NewReadOnly), 0);
end;
class procedure TWinCEWSCustomFloatSpinEdit.SetSelStart(const ACustomEdit: TCustomEdit;
NewStart: integer);
begin
EditSetSelStart(GetBuddyWindow(ACustomEdit.Handle), NewStart);
end;
class procedure TWinCEWSCustomFloatSpinEdit.SetSelLength(const ACustomEdit: TCustomEdit;
NewLength: integer);
begin
EditSetSelLength(GetBuddyWindow(ACustomEdit.Handle), NewLength);
end;
class procedure TWinCEWSCustomFloatSpinEdit.SetText(
const AWinControl: TWinControl; const AText: string);
begin
Windows.SetWindowTextW(
GetBuddyWindow(AWinControl.Handle),
PWideChar(UTF8ToUTF16(AText)));
end;
class procedure TWinCEWSCustomFloatSpinEdit.ShowHide(const AWinControl: TWinControl);
var
Buddy: HWND;
begin
// call inherited
TWinCEWSWinControl.ShowHide(AWinControl);
Buddy := GetBuddyWindow(AWinControl.Handle);
if AWinControl.HandleObjectShouldBeVisible then
ShowWindow(Buddy, SW_SHOW)
else
ShowWindow(Buddy, SW_HIDE);
end;
class procedure TWinCEWSCustomFloatSpinEdit.UpdateControl(
const ACustomFloatSpinEdit: TCustomFloatSpinEdit);
begin
UpdateFloatSpinEditControl(ACustomFloatSpinEdit.Handle, ACustomFloatSpinEdit);
end;
end.
|