From 51c351dc95800b46f4d3e9375612694baf7ffd67 Mon Sep 17 00:00:00 2001 From: graemeg Date: Tue, 17 Jul 2007 09:46:16 +0000 Subject: * Changed the first parameter in HandleKeyChar from a word to a string. This is in preperation for handling UTF-8 keyboard input correctly. A UTF-8 char cannot be represented in a Word type. * Reworked the keyboard events in TfpgMemo and TfpgEdit based on the previous change. Keyboard handling under x11 (Linux) works again. --- prototypes/fpgui2/source/core/gfx_widget.pas | 24 +++---- prototypes/fpgui2/source/core/x11/fpGFX2.lpk | 16 ++--- prototypes/fpgui2/source/core/x11/fpGFX2.pas | 4 +- prototypes/fpgui2/source/gui/gui_edit.pas | 75 +++++++++++--------- prototypes/fpgui2/source/gui/gui_memo.pas | 101 +++++++++++++++++---------- prototypes/fpgui2/tests/edittest.dpr | 2 +- 6 files changed, 130 insertions(+), 92 deletions(-) (limited to 'prototypes/fpgui2') diff --git a/prototypes/fpgui2/source/core/gfx_widget.pas b/prototypes/fpgui2/source/core/gfx_widget.pas index 62e339c4..5cb5281d 100644 --- a/prototypes/fpgui2/source/core/gfx_widget.pas +++ b/prototypes/fpgui2/source/core/gfx_widget.pas @@ -56,7 +56,7 @@ type procedure HandlePaint; virtual; procedure HandleResize(awidth, aheight: TfpgCoord); virtual; procedure HandleMove(x, y: TfpgCoord); virtual; - procedure HandleKeyChar(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); virtual; + procedure HandleKeyChar(var AText: string; var shiftstate: TShiftState; var consumed: boolean); virtual; procedure HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); virtual; procedure HandleKeyRelease(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); virtual; procedure HandleSetFocus; virtual; @@ -202,23 +202,23 @@ end; procedure TfpgWidget.MsgKeyChar(var msg: TfpgMessageRec); var - key: word; + lText: string; ss: TShiftState; consumed: boolean; wg: TfpgWidget; begin - key := msg.params.keyboard.keycode; + lText := msg.params.keyboard.keychar; ss := msg.params.keyboard.shiftstate; consumed := False; - HandleKeyChar(key, ss, consumed); + HandleKeyChar(lText, ss, consumed); if not consumed then begin wg := Parent; while (not consumed) and (wg <> nil) do begin - wg.HandleKeyChar(key, ss, consumed); + wg.HandleKeyChar(lText, ss, consumed); wg := wg.Parent; end; end; @@ -411,7 +411,13 @@ begin // descendants will implement this. end; -procedure TfpgWidget.HandleKeyChar(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); +procedure TfpgWidget.HandleKeyChar(var AText: string; var shiftstate: TShiftState; var consumed: boolean); +begin + // descendants will implement this. +end; + +procedure TfpgWidget.HandleKeyPress(var keycode: word; var shiftstate: TShiftState; + var consumed: boolean); var wg: TfpgWidget; dir: integer; @@ -483,12 +489,6 @@ begin end; end; -procedure TfpgWidget.HandleKeyPress(var keycode: word; var shiftstate: TShiftState; - var consumed: boolean); -begin - // descendants will implement this. -end; - procedure TfpgWidget.HandleKeyRelease(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); begin // descendants will implement this. diff --git a/prototypes/fpgui2/source/core/x11/fpGFX2.lpk b/prototypes/fpgui2/source/core/x11/fpGFX2.lpk index 9af869ad..baadf4e9 100644 --- a/prototypes/fpgui2/source/core/x11/fpGFX2.lpk +++ b/prototypes/fpgui2/source/core/x11/fpGFX2.lpk @@ -90,20 +90,20 @@ - - - - - - + + - - + + + + + + diff --git a/prototypes/fpgui2/source/core/x11/fpGFX2.pas b/prototypes/fpgui2/source/core/x11/fpGFX2.pas index b48f0672..c3e31cad 100644 --- a/prototypes/fpgui2/source/core/x11/fpGFX2.pas +++ b/prototypes/fpgui2/source/core/x11/fpGFX2.pas @@ -9,8 +9,8 @@ interface uses x11_xft, x11_keyconv, gfxbase, gfxbaseinterfaces, gfx_x11, fpgfx, gfx_stdimages, gfx_imgfmt_bmp, gfx_widget, gui_form, gui_label, gui_button, - gui_edit, gui_combobox, gui_popupwindow, gui_scrollbar, gui_memo, - gfx_UTF8utils, gui_dialogs, gui_listbox; + gui_edit, gui_combobox, gui_popupwindow, gui_scrollbar, gfx_UTF8utils, + gui_dialogs, gui_listbox, gui_memo; implementation diff --git a/prototypes/fpgui2/source/gui/gui_edit.pas b/prototypes/fpgui2/source/gui/gui_edit.pas index eb2e4597..15aff0da 100644 --- a/prototypes/fpgui2/source/gui/gui_edit.pas +++ b/prototypes/fpgui2/source/gui/gui_edit.pas @@ -12,8 +12,10 @@ uses gfx_widget; type + TfpgCustomEdit = class(TfpgWidget) + end; - TfpgEdit = class(TfpgWidget) + TfpgEdit = class(TfpgCustomEdit) private FText: string; FMaxLength: integer; @@ -34,7 +36,8 @@ type procedure AdjustCursor; function GetDrawText: string; procedure HandlePaint; override; - procedure HandleKeyChar(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override; + procedure HandleKeyChar(var AText: string; var shiftstate: TShiftState; var consumed: boolean); override; + procedure HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override; procedure HandleLMouseDown(x, y: integer; shiftstate: TShiftState); override; procedure HandleMouseMove(x, y: integer; btnstate: word; shiftstate: TShiftState); override; public @@ -252,10 +255,43 @@ begin Canvas.EndDraw; end; -procedure TfpgEdit.HandleKeyChar(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); +procedure TfpgEdit.HandleKeyChar(var AText: string; + var shiftstate: TShiftState; var consumed: boolean); var - prevval: string; s: string; + prevval: string; +begin + prevval := Text; + s := AText; + consumed := False; + + // Handle only printable characters + // Note: This is not UTF-8 compliant! + if (Ord(AText[1]) > 31) and (Ord(AText[1]) < 127) then + begin + if (FMaxLength <= 0) or (UTF8Length(FText) < FMaxLength) then + begin + DeleteSelection; + Insert(s, FText, FCursorPos + 1); + Inc(FCursorPos); + FSelStart := FCursorPos; + AdjustCursor; + end; + consumed := True; + end; + + if prevval <> Text then + if Assigned(OnChange) then + OnChange(self); + + if consumed then + RePaint + else + inherited HandleKeyChar(AText, shiftstate, consumed); +end; + +procedure TfpgEdit.HandleKeyPress(var keycode: word; + var shiftstate: TShiftState; var consumed: boolean); procedure StopSelection; begin @@ -264,14 +300,8 @@ var end; begin - //inherited; - //if Consumed then Exit; - - prevval := Text; - {$Note Is this UTF8 safe? } - s := char(keycode); Consumed := False; - { +{ Consumed := true; case ptkCheckClipBoardKey(keycode, shiftstate) of ckCopy: DoCopy; @@ -369,28 +399,7 @@ begin StopSelection; AdjustCursor; end; - - end; - - if not Consumed and (keycode >= 32) and (keycode < $FF00) then - begin - // printeable - if (FMaxLength <= 0) or (UTF8Length(FText) < FMaxLength) then - begin - DeleteSelection; - {$Note Is this UTF8 safe? } - Insert(s, FText, FCursorPos + 1); - Inc(FCursorPos); - FSelStart := FCursorPos; - AdjustCursor; - end; - - consumed := True; - end; - - if prevval <> Text then - if Assigned(OnChange) then - OnChange(self); + end; { if } if consumed then RePaint diff --git a/prototypes/fpgui2/source/gui/gui_memo.pas b/prototypes/fpgui2/source/gui/gui_memo.pas index 2c6d1089..ef45b4ff 100644 --- a/prototypes/fpgui2/source/gui/gui_memo.pas +++ b/prototypes/fpgui2/source/gui/gui_memo.pas @@ -62,7 +62,8 @@ type procedure SetCursorLine(aValue: integer); procedure UpdateScrollBarCoords; protected - procedure HandleKeyChar(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override; + procedure HandleKeyChar(var AText: string; var shiftstate: TShiftState; var consumed: boolean); override; + procedure HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override; procedure HandleLMouseDown(x, y: integer; shiftstate: TShiftState); override; procedure HandleMouseMove(x, y: integer; btnstate: word; shiftstate: TShiftState); override; procedure HandleResize(dwidth, dheight: integer); override; @@ -713,25 +714,77 @@ begin Canvas.EndDraw; end; -procedure TfpgMemo.HandleKeyChar(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); +procedure TfpgMemo.HandleKeyChar(var AText: string; var shiftstate: TShiftState; var consumed: boolean); var prevval: string; s: string; ls: string; - ls2: string; - cx: integer; +begin + inherited; + prevval := Text; + s := AText; + Consumed := False; +{ + Consumed := true; + case pgfCheckClipBoardKey(keycode, shiftstate) of + ckCopy: DoCopy; + ckPaste: DoPaste; + ckCut: //if FSelEndLine > 0 then + begin + DoCopy; + DeleteSelection; + end; + else + Consumed := false; + end; +} + + // Printable characters only + // Note: This is not UTF-8 compliant! + if (Ord(AText[1]) > 31) and (Ord(AText[1]) < 127) then + begin + // printeable + //FText := FText + s; + if (FMaxLength <= 0) or (UTF8Length(FLines.Text) < FMaxLength) then + begin + DeleteSelection; + ls := GetLineText(FCursorLine); + insert(s, ls, FCursorPos + 1); + SetLineText(FCursorLine, ls); + Inc(FCursorPos); + FSelStartPos := FCursorPos; + FSelStartLine := FCursorLine; + FSelEndLine := 0; + AdjustCursor; + end; + + consumed := True; + end; + + if prevval <> Text then + if Assigned(FOnChange) then + FOnChange(self); + + if consumed then + RePaint; +end; + +procedure TfpgMemo.HandleKeyPress(var keycode: word; + var shiftstate: TShiftState; var consumed: boolean); +var + cx: integer; + ls: string; + ls2: string; + procedure StopSelection; begin FSelStartLine := FCursorLine; FSelStartPos := FCursorPos; FSelEndLine := 0; end; - + begin - inherited; - prevval := Text; - s := char(keycode); Consumed := False; (* Consumed := true; @@ -808,7 +861,7 @@ begin SetCPByX(cx); end; end; - + keyHome: begin if (ssCtrl in shiftstate) then @@ -922,34 +975,10 @@ begin end; end; - {$Note This must be fixed. We change keycodes!! } - if not Consumed and (keycode >= 32) and (keycode < $FF00) then - begin - // printeable - //FText := FText + s; - - if (FMaxLength <= 0) or (UTF8Length(FLines.Text) < FMaxLength) then - begin - DeleteSelection; - ls := GetLineText(FCursorLine); - insert(s, ls, FCursorPos + 1); - SetLineText(FCursorLine, ls); - Inc(FCursorPos); - FSelStartPos := FCursorPos; - FSelStartLine := FCursorLine; - FSelEndLine := 0; - AdjustCursor; - end; - - consumed := True; - end; - - if prevval <> Text then - if Assigned(FOnChange) then - FOnChange(self); - if consumed then - RePaint; + RePaint + else + inherited; end; procedure TfpgMemo.HandleLMouseDown(x, y: integer; shiftstate: TShiftState); diff --git a/prototypes/fpgui2/tests/edittest.dpr b/prototypes/fpgui2/tests/edittest.dpr index cb328eec..a71b2982 100644 --- a/prototypes/fpgui2/tests/edittest.dpr +++ b/prototypes/fpgui2/tests/edittest.dpr @@ -124,7 +124,7 @@ type memo.Left := 250; memo.Width := 200; memo.Height := 80; - + listbox := TfpgListBox.Create(self); listbox.Top := 100; listbox.Left := 250; -- cgit v1.2.3-70-g09d2