diff options
author | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2007-07-14 12:38:29 +0000 |
---|---|---|
committer | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2007-07-14 12:38:29 +0000 |
commit | c39790ae56441cb41f1aa97e016f7dfcc30557fd (patch) | |
tree | d944232e43ec566af534a0cebc93a6f859bc1a80 /prototypes/fpgui2/source | |
parent | 337ee2c72452d9ad5520d463c355d6d6dd1f6c5d (diff) | |
download | fpGUI-c39790ae56441cb41f1aa97e016f7dfcc30557fd.tar.xz |
* fpgui2: Added a new gui_dialogs unit. This is where most dialogs will
be defined.
* Implemented a ShowMessage() method with a TfpgMessageBox window. It wraps long lines, but a few
more improvements need to be made.
Diffstat (limited to 'prototypes/fpgui2/source')
-rw-r--r-- | prototypes/fpgui2/source/core/x11/fpGFX2.lpk | 6 | ||||
-rw-r--r-- | prototypes/fpgui2/source/core/x11/fpGFX2.pas | 2 | ||||
-rw-r--r-- | prototypes/fpgui2/source/gui/gui_dialogs.pas | 203 |
3 files changed, 209 insertions, 2 deletions
diff --git a/prototypes/fpgui2/source/core/x11/fpGFX2.lpk b/prototypes/fpgui2/source/core/x11/fpGFX2.lpk index e72e5abb..f45af8c2 100644 --- a/prototypes/fpgui2/source/core/x11/fpGFX2.lpk +++ b/prototypes/fpgui2/source/core/x11/fpGFX2.lpk @@ -24,7 +24,7 @@ <License Value="Modified LGPL "/> <Version Minor="1"/> - <Files Count="19"> + <Files Count="20"> <Item1> <Filename Value="x11_xft.pas"/> <UnitName Value="x11_xft"/> @@ -101,6 +101,10 @@ <Filename Value="../resample.pas"/> <UnitName Value="resample"/> </Item19> + <Item20> + <Filename Value="../../gui/gui_dialogs.pas"/> + <UnitName Value="gui_dialogs"/> + </Item20> </Files> <RequiredPkgs Count="1"> <Item1> diff --git a/prototypes/fpgui2/source/core/x11/fpGFX2.pas b/prototypes/fpgui2/source/core/x11/fpGFX2.pas index 6e261b09..5e8fe166 100644 --- a/prototypes/fpgui2/source/core/x11/fpGFX2.pas +++ b/prototypes/fpgui2/source/core/x11/fpGFX2.pas @@ -10,7 +10,7 @@ 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, resample; + gfx_UTF8utils, resample, gui_dialogs; implementation diff --git a/prototypes/fpgui2/source/gui/gui_dialogs.pas b/prototypes/fpgui2/source/gui/gui_dialogs.pas new file mode 100644 index 00000000..4d6586a7 --- /dev/null +++ b/prototypes/fpgui2/source/gui/gui_dialogs.pas @@ -0,0 +1,203 @@ +unit gui_dialogs; + +{$mode objfpc}{$H+} + +interface + +uses + Classes, SysUtils, fpgfx, gui_form, gui_button, gui_label; + +type + + { TfpgMessageBox } + + TfpgMessageBox = class(TfpgForm) + private + FLines: TStringList; + FFont: TfpgFont; + FTextY: integer; + FLineHeight: integer; + FMaxLineWidth: integer; + FButton: TfpgButton; + procedure ButtonClick(Sender: TObject); + protected + procedure HandleKeyPress(var keycode: word; var shiftstate: word; var consumed: boolean); override; + procedure HandlePaint; override; + public + constructor Create(AOwner : TComponent); override; + destructor Destroy; override; + procedure SetMessage(AMessage: string); + end; + + +procedure ShowMessage(AMessage, ATitle: string); overload; +procedure ShowMessage(AMessage: string); overload; + + +implementation + +uses + gfxbase, gfx_utf8utils; + + +procedure ShowMessage(AMessage, ATitle: string); +var + frm: TfpgMessageBox; +begin + frm := TfpgMessageBox.Create(nil); + try + frm.WindowTitle := ATitle; + frm.SetMessage(AMessage); + frm.ShowModal; + finally + frm.Free; + end; +end; + +procedure ShowMessage(AMessage: string); +begin + ShowMessage(AMessage, 'Message'); +end; + + +{ TfpgMessageBox } + +procedure TfpgMessageBox.ButtonClick(Sender: TObject); +begin + ModalResult := 1; +end; + +procedure TfpgMessageBox.HandleKeyPress(var keycode: word; + var shiftstate: word; var consumed: boolean); +begin + inherited HandleKeyPress(keycode, shiftstate, consumed); + if keycode = KEY_ESC then + begin + Close; + end; +end; + +procedure TfpgMessageBox.HandlePaint; +var + n, y: integer; + tw: integer; +begin + Canvas.BeginDraw; + inherited HandlePaint; + +// canvas.Clear(FBackgroundColor); + Canvas.SetFont(FFont); + + y := FTextY; + for n := 0 to FLines.Count-1 do + begin + tw := FFont.TextWidth(FLines[n]); + Canvas.DrawString(Width div 2 - tw div 2, y, FLines[n]); + Inc(y, FLineHeight); + end; + Canvas.EndDraw; +end; + +constructor TfpgMessageBox.Create(AOwner: TComponent); +begin + inherited Create(AOwner); + WindowAttributes := [waAutoPos]; + + FLines := TStringList.Create; + FFont := fpgGetFont('#Label1'); + FTextY := 10; + FLineHeight := FFont.Height + 4; + MinWidth := 200; + FMaxLineWidth := 500; + + FButton := TfpgButton.Create(self); + FButton.text := 'OK'; // We must localize this + FButton.Width := 75; + FButton.OnClick := @ButtonClick; + +end; + +destructor TfpgMessageBox.Destroy; +begin + FFont.Free; + FLines.Free; + inherited Destroy; +end; + +procedure TfpgMessageBox.SetMessage(AMessage: string); +var + maxw : integer; + n : integer; + s, s16 : string; + c : char; + + procedure AddLine(all : boolean); + var + w : integer; + m : integer; + begin + s16 := s; + w := FFont.TextWidth(s16); + if w > FMaxLineWidth then + begin + while w > FMaxLineWidth do + begin + m := UTF8Length(s); + repeat + dec(m); + s16 := UTF8Copy(s,1,m); + w := FFont.TextWidth(s16); + until w <= FMaxLineWidth; + if w > maxw then maxw := w; + FLines.Add(s16); + s := Copy(s,m+1,length(s)); + s16 := s; + w := FFont.TextWidth(s16); + end; + if all then + begin + FLines.Add(s16); + s := ''; + end; + end + else + begin + FLines.Add(s16); + s := ''; + end; + + if w > maxw then maxw := w; + end; + +begin + s := ''; + FLines.Clear; + n := 1; + maxw := 0; + while n <= length(AMessage) do + begin + c := AMessage[n]; + if (c = #13) or (c = #10) then + begin + AddLine(false); + if (c = #13) and (n < length(AMessage)) and (AMessage[n+1] = #10) then inc(n); + end + else s := s + c; + inc(n); + end; + AddLine(true); + + width := maxw + 2*10; + + if width < FMinWidth then width := FMinWidth; + + FButton.Top := FTextY + FLineHeight*FLines.Count + FTextY; + + FButton.Left := (Width div 2) - (FButton.Width div 2); + + height := FButton.Top + FButton.Height + FTextY; +end; + + +end. + |