{ /*************************************************************************** dialogs.pp ---------- Component Library Standard dialogs Controls ***************************************************************************/ ***************************************************************************** * * * This file is part of the Lazarus Component Library (LCL) * * * * See the file COPYING.LCL, included in this distribution, * * for details about the copyright. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * * ***************************************************************************** } { @author(TMyClass - Michael A. Hess ) @author(TMyOtherClass - Other Author Name ) @created() @lastmod() Detailed description of the Unit. } unit Dialogs; {$mode objfpc}{$H+} interface uses Classes, SysUtils, LCLIntf, InterfaceBase, FileUtil, LCLStrConsts, LCLType, LMessages, LCLProc, Forms, Controls, GraphType, Graphics, Buttons, StdCtrls, LCLClasses; type TMsgDlgType = (mtWarning, mtError, mtInformation, mtConfirmation, mtCustom); TMsgDlgBtn = (mbYes, mbNo, mbOK, mbCancel, mbAbort, mbRetry, mbIgnore, mbAll, mbNoToAll, mbYesToAll, mbHelp, mbClose); TMsgDlgButtons = set of TMsgDlgBtn; const mbYesNoCancel = [mbYes, mbNo, mbCancel]; mbYesNo = [mbYes, mbNo]; mbOKCancel = [mbOK, mbCancel]; mbAbortRetryIgnore = [mbAbort, mbRetry, mbIgnore]; MsgDlgBtnToBitBtnKind: array[TMsgDlgBtn] of TBitBtnKind = ( bkYes, bkNo, bkOK, bkCancel, bkAbort, bkRetry, bkIgnore, bkAll, bkNoToAll, bkYesToAll, bkHelp, bkClose ); BitBtnKindToMsgDlgBtn: array[TBitBtnKind] of TMsgDlgBtn = ( mbOk, mbOK, mbCancel, mbHelp, mbYes, mbNo, mbClose, mbAbort, mbRetry, mbIgnore, mbAll, mbNoToALl, mbYesToAll ); type { TCommonDialog } TCommonDialog = class(TLCLComponent) private FHandle : THandle; FHeight: integer; FWidth: integer; FOnCanClose: TCloseQueryEvent; FOnShow, FOnClose : TNotifyEvent; FTitle : string; FUserChoice: integer; FHelpContext: THelpContext; procedure SetHandle(const AValue: THandle); procedure SetHeight(const AValue: integer); procedure SetWidth(const AValue: integer); protected function DoExecute : boolean; virtual; public FCompStyle : LongInt; constructor Create(TheOwner: TComponent); override; function Execute: boolean; virtual; property Handle: THandle read FHandle write SetHandle; property UserChoice: integer read FUserChoice write FUserChoice; procedure Close; virtual; procedure DoShow; virtual; procedure DoClose; virtual; function HandleAllocated: boolean; published property OnClose: TNotifyEvent read FOnClose write FOnClose; property OnCanClose: TCloseQueryEvent read FOnCanClose write FOnCanClose; property OnShow: TNotifyEvent read FOnShow write FOnShow; property HelpContext: THelpContext read FHelpContext write FHelpContext default 0; property Width: integer read FWidth write SetWidth; property Height: integer read FHeight write SetHeight; property Title: string read FTitle write FTitle; end; { TFileDialog } TFileDialog = class(TCommonDialog) private FDefaultExt: string; FFileName : String; FFiles: TStrings; FFilter: String; FFilterIndex: Integer; FHistoryList: TStrings; FInitialDir: string; FOldWorkingDir: string; FOnHelpClicked: TNotifyEvent; procedure SetDefaultExt(const AValue: string); protected function DoExecute: boolean; override; procedure SetFileName(const Value: String); virtual; procedure SetFilter(const Value: String); virtual; procedure SetHistoryList(const AValue: TStrings); virtual; public constructor Create(TheOwner: TComponent); override; destructor Destroy; override; function Execute: boolean; override; property Files: TStrings read FFiles; property HistoryList: TStrings read FHistoryList write SetHistoryList; published property Title; property DefaultExt: string read FDefaultExt write SetDefaultExt; property FileName: String read FFileName write SetFileName; property Filter: String read FFilter write SetFilter; property FilterIndex: Integer read FFilterIndex write FFilterIndex default 1; property InitialDir: string read FInitialDir write FInitialDir; property OnHelpClicked: TNotifyEvent read FOnHelpClicked write FOnHelpClicked; end; { TOpenDialog } TOpenOption = ( ofReadOnly, ofOverwritePrompt, // if selected file exists shows a message, that file // will be overwritten ofHideReadOnly, // hide read only file ofNoChangeDir, // do not change current directory ofShowHelp, // show a help button ofNoValidate, ofAllowMultiSelect,// allow multiselection ofExtensionDifferent, ofPathMustExist, // shows an error message if selected path does not exist ofFileMustExist, // shows an error message if selected file does not exist ofCreatePrompt, ofShareAware, ofNoReadOnlyReturn,// do not return filenames that are readonly ofNoTestFileCreate, ofNoNetworkButton, ofNoLongNames, ofOldStyleDialog, ofNoDereferenceLinks,// do not expand filenames ofEnableIncludeNotify, ofEnableSizing, // dialog can be resized, e.g. via the mouse ofDontAddToRecent, // do not add the path to the history list ofForceShowHidden, // show hidden files ofViewDetail, // details are OS and interface dependent ofAutoPreview // details are OS and interface dependent ); TOpenOptions = set of TOpenOption; const DefaultOpenDialogOptions = [ofEnableSizing, ofViewDetail]; type TOpenDialog = class(TFileDialog) private FOnFolderChange: TNotifyEvent; FOnSelectionChange: TNotifyEvent; FOptions: TOpenOptions; FLastSelectionChangeFilename: string; protected procedure DereferenceLinks; virtual; function CheckFile(var AFilename: string): boolean; virtual; function CheckAllFiles: boolean; virtual; function DoExecute: boolean; override; public constructor Create(TheOwner: TComponent); override; procedure DoFolderChange; virtual; procedure DoSelectionChange; virtual; published property Options: TOpenOptions read FOptions write FOptions default DefaultOpenDialogOptions; property OnFolderChange: TNotifyEvent read FOnFolderChange write FOnFolderChange; property OnSelectionChange: TNotifyEvent read FOnSelectionChange write FOnSelectionChange; end; { TSaveDialog } TSaveDialog = class(TOpenDialog) public constructor Create(AOwner: TComponent); override; end; { TSelectDirectoryDialog } TSelectDirectoryDialog = class(TOpenDialog) protected function CheckFile(var AFilename: string): boolean; override; public constructor Create(AOwner: TComponent); override; end; { TColorDialog } TColorDialog = class(TCommonDialog) private FColor: TColor; public constructor Create(TheOwner: TComponent); override; published property Title; property Color: TColor read FColor write FColor; end; { TColorButton } TColorButton = class(TGraphicControl) private FBorderWidth: integer; FButtonColor: TColor; FColorDialog: TColorDialog; FOnColorChanged: TNotifyEvent; procedure SetBorderWidth(const AValue: integer); protected procedure Click; override; procedure Paint; override; procedure SetButtonColor(Value: TColor); procedure ShowColorDialog; virtual; public constructor Create(AnOwner: TComponent); override; destructor Destroy; Override; published property Align; property Anchors; property BorderSpacing; property BorderWidth: integer read FBorderWidth write SetBorderWidth; property ButtonColor:TColor read FButtonColor write SetButtonColor; property Hint; property OnChangeBounds; property OnColorChanged: TNotifyEvent read FOnColorChanged write FOnColorChanged; property OnMouseDown; property OnMouseMove; property OnMouseUp; property OnPaint; property OnResize; property ParentShowHint; property PopupMenu; property ShowHint; property Visible; end; { TFontDialog } TFontDialogOption = (fdAnsiOnly, fdTrueTypeOnly, fdEffects, fdFixedPitchOnly, fdForceFontExist, fdNoFaceSel, fdNoOEMFonts, fdNoSimulations, fdNoSizeSel, fdNoStyleSel, fdNoVectorFonts, fdShowHelp, fdWysiwyg, fdLimitSize, fdScalableOnly, fdApplyButton); TFontDialogOptions = set of TFontDialogOption; TFontDialog = class(TCommonDialog) private FFont: TFont; FMaxFontSize: Integer; FMinFontSize: Integer; FOnApplyClicked: TNotifyEvent; FOptions: TFontDialogOptions; FPreviewText: string; procedure SetFont(const AValue: TFont); public procedure ApplyClicked; virtual; constructor Create (AOwner : TComponent); override; destructor Destroy; override; published property Title; property Font: TFont read FFont write SetFont; property MinFontSize: Integer read FMinFontSize write FMinFontSize; property MaxFontSize: Integer read FMaxFontSize write FMaxFontSize; property Options: TFontDialogOptions read FOptions write FOptions default [fdEffects]; property OnApplyClicked: TNotifyEvent read FOnApplyClicked write FOnApplyClicked; property PreviewText: string read FPreviewText write FPreviewText; end; { TFindDialog } TFindOption = (frDown, frFindNext, frHideMatchCase, frHideWholeWord, frHideUpDown, frMatchCase, frDisableMatchCase, frDisableUpDown, frDisableWholeWord, frReplace, frReplaceAll, frWholeWord, frShowHelp); TFindOptions = set of TFindOption; { MessageDlg } function MessageDlg(const aMsg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer; function MessageDlg(const aCaption, aMsg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer; function MessageDlgPos(const aMsg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer): Integer; function MessageDlgPosHelp(const aMsg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer; const HelpFileName: string): Integer; function QuestionDlg(const aCaption, aMsg: string; DlgType: TMsgDlgType; Buttons: array of const; HelpCtx: Longint): TModalResult; procedure ShowMessage(const aMsg: string); procedure ShowMessageFmt(const aMsg: string; Params: array of const); procedure ShowMessagePos(const aMsg: string; X, Y: Integer); Function InputQuery(const ACaption, APrompt : String; MaskInput : Boolean; var Value : String) : Boolean; Function InputQuery(const ACaption, APrompt : String; var Value : String) : Boolean; Function InputBox(const ACaption, APrompt, ADefault : String) : String; Function PasswordBox(const ACaption, APrompt : String) : String; type TSelectDirOpt = (sdAllowCreate, sdPerformCreate, sdPrompt); TSelectDirOpts = set of TSelectDirOpt; function SelectDirectory(const Caption, InitialDirectory: string; var Directory: string): boolean; function SelectDirectory(const Caption, InitialDirectory: string; var Directory: string; ShowHidden: boolean; HelpCtx: Longint = 0): boolean; function SelectDirectory(var Directory: string; Options: TSelectDirOpts; HelpCtx: Longint): Boolean; procedure Register; implementation uses Math, WSDialogs; const // //TODO: all the constants below should be replaced in the future // their only purpose is to overcome some current design flaws & // missing features in the GTK libraries // cBitmapX = 10; // x-position for bitmap in messagedialog cBitmapY = 10; // y-position for bitmap in messagedialog cLabelSpacing = 10; // distance between icon & label procedure Register; begin RegisterComponents('Dialogs',[TOpenDialog,TSaveDialog,TSelectDirectoryDialog, TColorDialog,TFontDialog]); RegisterComponents('Misc',[TColorButton]); end; function ShowMessageBox(Text, Caption : PChar; Flags : Longint) : Integer; var DlgType : TMsgDlgType; Buttons : TMsgDlgButtons; begin //This uses TMessageBox class in MessageDialogs.inc if (Flags and MB_RETRYCANCEL) = MB_RETRYCANCEL then Buttons := [mbREtry, mbCancel] else if (Flags and MB_YESNO) = MB_YESNO then Buttons := [mbYes, mbNo] else if (Flags and MB_YESNOCANCEL) = MB_YESNOCANCEL then Buttons := [mbYes, mbNo, mbCancel] else if (Flags and MB_ABORTRETRYIGNORE) = MB_ABORTRETRYIGNORE then Buttons := [mbAbort, mbRetry, mbIgnore] else if (Flags and MB_OKCANCEL) = MB_OKCANCEL then Buttons := [mbOK,mbCancel] else if (Flags and MB_OK) = MB_OK then Buttons := [mbOK] else Buttons := [mbOK]; if (Flags and MB_ICONQUESTION) = MB_ICONQUESTION then DlgTYpe := mtConfirmation else if (Flags and MB_ICONINFORMATION) = MB_ICONINFORMATION then DlgTYpe := mtInformation else if (Flags and MB_ICONERROR) = MB_ICONERROR then DlgTYpe := mtError else if (Flags and MB_ICONWARNING) = MB_ICONWARNING then DlgTYpe := mtWarning else DlgTYpe := mtCustom; Result := MessageDlg(Caption,Text,DlgType,Buttons,0); end; {$I colordialog.inc} {$I commondialog.inc} {$I filedialog.inc} {$I fontdialog.inc} {$I inputdialog.inc} {$I messagedialogs.inc} {$I promptdialog.inc} {$I colorbutton.inc} initialization Forms.MessageBoxFunction:=@ShowMessageBox; InterfaceBase.InputDialogFunction:=@ShowInputDialog; InterfaceBase.PromptDialogFunction:=@ShowPromptDialog; finalization end.