1{%MainUnit ../dialogs.pp} 2 3{ TTaskDialogButtonsEnumerator } 4 5constructor TTaskDialogButtonsEnumerator.Create(ACollection: TTaskDialogButtons 6 ); 7begin 8 FCollection := ACollection; 9 FIndex := -1; 10end; 11 12function TTaskDialogButtonsEnumerator.GetCurrent: TTaskDialogBaseButtonItem; 13begin 14 Result := FCollection[FIndex]; 15end; 16 17function TTaskDialogButtonsEnumerator.MoveNext: Boolean; 18begin 19 Result := FIndex < FCollection.Count - 1; 20 if Result then 21 Inc(FIndex); 22end; 23 24{ TTaskDialogButtons } 25 26function TTaskDialogButtons.Add: TTaskDialogBaseButtonItem; 27begin 28 Result := TTaskDialogBaseButtonItem(inherited Add); 29end; 30 31function TTaskDialogButtons.FindButton(AModalResult: TModalResult 32 ): TTaskDialogBaseButtonItem; 33begin 34 for Result in Self do 35 if Result.ModalResult = AModalResult then 36 Exit; 37 38 Result := nil; 39end; 40 41function TTaskDialogButtons.GetEnumerator: TTaskDialogButtonsEnumerator; 42begin 43 Result := TTaskDialogButtonsEnumerator.Create(Self); 44end; 45 46function TTaskDialogButtons.GetItem(Index: Integer): TTaskDialogBaseButtonItem; 47begin 48 Result := TTaskDialogBaseButtonItem(inherited GetItem(Index)); 49end; 50 51procedure TTaskDialogButtons.SetDefaultButton( 52 const Value: TTaskDialogBaseButtonItem); 53begin 54 if Value <> FDefaultButton then 55 FDefaultButton := Value; 56end; 57 58procedure TTaskDialogButtons.SetItem(Index: Integer; 59 const Value: TTaskDialogBaseButtonItem); 60begin 61 inherited SetItem(Index, Value); 62end; 63 64{ TTaskDialogRadioButtonItem } 65 66constructor TTaskDialogRadioButtonItem.Create(ACollection: TCollection); 67begin 68 inherited Create(ACollection); 69 70 Caption := 'RadioButton'+IntToStr(ID+1); 71end; 72 73{ TTaskDialogButtonItem } 74 75constructor TTaskDialogButtonItem.Create(ACollection: TCollection); 76begin 77 inherited Create(ACollection); 78 79 Caption := 'Button'+IntToStr(ID+1); 80end; 81 82{ TCustomTaskDialog } 83 84constructor TCustomTaskDialog.Create(AOwner: TComponent); 85begin 86 inherited Create(AOwner); 87 88 FButtons := TTaskDialogButtons.Create(Self, TTaskDialogButtonItem); 89 FRadioButtons := TTaskDialogButtons.Create(Self, TTaskDialogRadioButtonItem); 90 91 FCommonButtons := [tcbOk, tcbCancel]; 92 FDefaultButton := tcbOk; 93 FFlags := [tfAllowDialogCancellation]; 94 FFooterIcon := tdiNone; 95 FMainIcon := tdiInformation; 96end; 97 98function TCustomTaskDialog.ButtonIDToModalResult(const AButtonID: Integer 99 ): TModalResult; 100begin 101 if AButtonID<100 then 102 begin 103 case AButtonID of 104 IDOK: Result := mrOK; 105 IDCANCEL: Result := mrCancel; 106 IDABORT: Result := mrAbort; 107 IDRETRY: Result := mrRetry; 108 IDIGNORE: Result := mrIgnore; 109 IDYES: Result := mrYes; 110 IDNO: Result := mrNo; 111 IDCLOSE: Result := mrClose; 112 else Result := AButtonID 113 end; 114 end 115 else if (AButtonID-100<Buttons.Count) then 116 Result := Buttons[AButtonID-100].ModalResult 117 else 118 Result := mrNone; 119end; 120 121destructor TCustomTaskDialog.Destroy; 122begin 123 FButtons.Free; 124 FRadioButtons.Free; 125 126 inherited Destroy; 127end; 128 129function TCustomTaskDialog.DoExecute(ParentWnd: HWND): Boolean; 130 function TD_COMMONBUTTONS(const aButtons: TTaskDialogCommonButtons): LCLTaskDialog.TCommonButtons; 131 begin 132 Result := []; 133 if tcbOk in aButtons then 134 Result := Result + [cbOK]; 135 if tcbYes in aButtons then 136 Result := Result + [cbYes]; 137 if tcbNo in aButtons then 138 Result := Result + [cbNo]; 139 if tcbCancel in aButtons then 140 Result := Result + [cbCancel]; 141 if tcbRetry in aButtons then 142 Result := Result + [cbRetry]; 143 if tcbClose in aButtons then 144 Result := Result + [cbClose]; 145 end; 146 147 function TD_FLAGS(const aTaskFlags: TTaskDialogFlags): LCLTaskDialog.TTaskDialogFlags; 148 begin 149 Result := []; 150 if tfEnableHyperlinks in aTaskFlags then 151 Result := Result + [tdfEnableHyperlinks]; 152 if tfUseHiconMain in aTaskFlags then 153 Result := Result + [tdfUseHIconMain]; 154 if tfUseHiconFooter in aTaskFlags then 155 Result := Result + [tdfUseHIconFooter]; 156 if tfAllowDialogCancellation in aTaskFlags then 157 Result := Result + [tdfAllowDialogCancellation]; 158 if tfUseCommandLinks in aTaskFlags then 159 Result := Result + [tdfUseCommandLinks]; 160 if tfUseCommandLinksNoIcon in aTaskFlags then 161 Result := Result + [tdfUseCommandLinksNoIcon]; 162 if tfExpandFooterArea in aTaskFlags then 163 Result := Result + [tdfExpandFooterArea]; 164 if tfExpandedByDefault in aTaskFlags then 165 Result := Result + [tdfExpandByDefault]; 166 if tfVerificationFlagChecked in aTaskFlags then 167 Result := Result + [tdfVerificationFlagChecked]; 168 if tfShowProgressBar in aTaskFlags then 169 Result := Result + [tdfShowProgressBar]; 170 if tfShowMarqueeProgressBar in aTaskFlags then 171 Result := Result + [tdfShowMarqueeProgressBar]; 172 if tfCallbackTimer in aTaskFlags then 173 Result := Result + [tdfCallbackTimer]; 174 if tfPositionRelativeToWindow in aTaskFlags then 175 Result := Result + [tdfPositionRelativeToWindow]; 176 if tfRtlLayout in aTaskFlags then 177 Result := Result + [tdfRtlLayout]; 178 if tfNoDefaultRadioButton in aTaskFlags then 179 Result := Result + [tdfNoDefaultRadioButton]; 180 if tfCanBeMinimized in aTaskFlags then 181 Result := Result + [tdfCanBeMinimized]; 182 end; 183 184 function TF_DIALOGICON(const aIcon: TTaskDialogIcon): LCLTaskDialog.TTaskDialogIcon; 185 begin 186 case aIcon of 187 tdiWarning: Result := LCLTaskDialog.TTaskDialogIcon.tiWarning; 188 tdiError: Result := LCLTaskDialog.TTaskDialogIcon.tiError; 189 tdiInformation: Result := LCLTaskDialog.TTaskDialogIcon.tiInformation; 190 tdiShield: Result := LCLTaskDialog.TTaskDialogIcon.tiShield; 191 tdiQuestion: Result := LCLTaskDialog.TTaskDialogIcon.tiQuestion; 192 else 193 Result := LCLTaskDialog.TTaskDialogIcon.tiBlank; 194 end; 195 end; 196 197 function TF_FOOTERICON(const aIcon: TTaskDialogIcon): LCLTaskDialog.TTaskDialogFooterIcon; 198 begin 199 case aIcon of 200 tdiWarning: Result := LCLTaskDialog.TTaskDialogFooterIcon.tfiWarning; 201 tdiError: Result := LCLTaskDialog.TTaskDialogFooterIcon.tfiError; 202 tdiInformation: Result := LCLTaskDialog.TTaskDialogFooterIcon.tfiInformation; 203 tdiShield: Result := LCLTaskDialog.TTaskDialogFooterIcon.tfiShield; 204 tdiQuestion: Result := LCLTaskDialog.TTaskDialogFooterIcon.tfiQuestion; 205 else 206 Result := LCLTaskDialog.TTaskDialogFooterIcon.tfiBlank; 207 end; 208 end; 209var 210 TaskDlg: LCLTaskDialog.TTaskDialog; 211 DefRB, DefBtn: TModalResult; 212 B: TTaskDialogBaseButtonItem; 213 ButtonID: Integer; 214const 215 TD_BTNMOD: array[TTaskDialogCommonButton] of Integer = ( 216 mrOk, mrYes, mrNo, mrCancel, mrRetry, mrAbort); 217begin 218 FillChar(TaskDlg, SizeOf(LCLTaskDialog.TTaskDialog), 0); 219 220 if RadioButtons.DefaultButton<> nil then 221 DefRB := RadioButtons.DefaultButton.ModalResult 222 else 223 DefRB := 0; 224 if Buttons.DefaultButton<>nil then 225 DefBtn := Buttons.DefaultButton.ModalResult 226 else 227 DefBtn := TD_BTNMOD[DefaultButton]; 228 229 for B in Buttons do 230 TaskDlg.Buttons := TaskDlg.Buttons + B.Caption + #10; 231 for B in RadioButtons do 232 TaskDlg.Radios := TaskDlg.Radios + B.Caption + #10; 233 234 TaskDlg.Title := Caption; 235 TaskDlg.Inst := Title; 236 TaskDlg.Content := Text; 237 TaskDlg.InfoCollapse := ExpandButtonCaption; 238 TaskDlg.Info := ExpandedText; 239 TaskDlg.Footer := FooterText; 240 TaskDlg.Verify := VerificationText; 241 242 ButtonID := TaskDlg.Execute(TD_COMMONBUTTONS(CommonButtons), DefBtn, TD_FLAGS(Flags), TF_DIALOGICON(MainIcon), TF_FOOTERICON(FooterIcon), 243 DefRB, 0, ParentWnd, False, False, @DoOnButtonClickedHandler); 244 Result := ButtonID>=0; 245 FModalResult := ButtonIDToModalResult(ButtonID); 246 247 if (TaskDlg.RadioRes>=200) and (TaskDlg.RadioRes-200<RadioButtons.Count) then 248 FRadioButton := RadioButtons[TaskDlg.RadioRes-200] as TTaskDialogRadioButtonItem 249 else 250 FRadioButton := nil; 251 if TaskDlg.VerifyChecked then 252 Include(FFlags, tfVerificationFlagChecked) 253 else 254 Exclude(FFlags, tfVerificationFlagChecked) 255end; 256 257procedure TCustomTaskDialog.DoOnButtonClicked(AModalResult: Integer; 258 var ACanClose: Boolean); 259begin 260 if Assigned(FOnButtonClicked) then 261 FOnButtonClicked(Self, AModalResult, ACanClose); 262end; 263 264procedure TCustomTaskDialog.DoOnButtonClickedHandler(Sender: PTaskDialog; 265 AButtonID: integer; var ACanClose: Boolean); 266begin 267 DoOnButtonClicked(ButtonIDToModalResult(AButtonID), ACanClose) 268end; 269 270function TCustomTaskDialog.Execute(ParentWnd: HWND): Boolean; 271begin 272 FModalResult := 0; 273 Result := DoExecute(ParentWnd); 274end; 275 276function TCustomTaskDialog.Execute: Boolean; 277begin 278 Result := Execute(0); 279end; 280 281procedure TCustomTaskDialog.SetButtons(const Value: TTaskDialogButtons); 282begin 283 if FButtons=Value then Exit; 284 FButtons.Assign(Value); 285end; 286 287procedure TCustomTaskDialog.SetRadioButtons(const Value: TTaskDialogButtons); 288begin 289 if FRadioButtons=Value then Exit; 290 FRadioButtons.Assign(Value); 291end; 292 293{ TTaskDialogBaseButtonItem } 294 295constructor TTaskDialogBaseButtonItem.Create(ACollection: TCollection); 296begin 297 inherited Create(ACollection); 298 299 FClient := Collection.Owner as TCustomTaskDialog; 300 FModalResult := 100 + ID; 301end; 302 303function TTaskDialogBaseButtonItem.GetDefault: Boolean; 304begin 305 Result := TaskButtonCollection.DefaultButton = Self; 306end; 307 308function TTaskDialogBaseButtonItem.GetDisplayName: TTranslateString; 309begin 310 if FCaption <> '' then 311 Result := FCaption 312 else 313 Result := inherited GetDisplayName; 314end; 315 316procedure TTaskDialogBaseButtonItem.SetCaption(const ACaption: TTranslateString); 317begin 318 if FCaption = ACaption then Exit; 319 FCaption := ACaption; 320end; 321 322procedure TTaskDialogBaseButtonItem.SetDefault(const Value: Boolean); 323begin 324 if Value then 325 TaskButtonCollection.DefaultButton := Self 326 else if TTaskDialogButtons(Collection).DefaultButton = Self then 327 TaskButtonCollection.DefaultButton := nil; 328end; 329 330function TTaskDialogBaseButtonItem.TaskButtonCollection: TTaskDialogButtons; 331begin 332 Result := TTaskDialogButtons(Collection); 333end; 334