Recent

Author Topic: How to shorten QuestionDlg calls?  (Read 509 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 1900
    • UVviewsoft
How to shorten QuestionDlg calls?
« on: July 19, 2022, 07:29:07 pm »
I want to shorten this, by using variable for "array of const" param.
And only single QuestionDlg call.
But I don't know of what type this var must be.
Code: Pascal  [Select][+][-]
  1.   case AFlags and $0F of
  2.     MB_OK:
  3.       Res:= QuestionDlg(msgTitle, AText, Typ,
  4.             [mrOk, msgButtonOk],
  5.             0);
  6.     MB_OKCANCEL:
  7.       Res:= QuestionDlg(msgTitle, AText, Typ,
  8.             [mrOk, msgButtonOk,
  9.             mrCancel, msgButtonCancel],
  10.             0);
  11.     MB_ABORTRETRYIGNORE:
  12.       Res:= QuestionDlg(msgTitle, AText, Typ,
  13.             [mrAbort, msgButtonAbort,
  14.             mrRetry, msgButtonRetry,
  15.             mrIgnore, msgButtonIgnore],
  16.             0);
  17.     MB_YESNO:
  18.       Res:= QuestionDlg(msgTitle, AText, Typ,
  19.             [mrYes, msgButtonYes,
  20.             mrNo, msgButtonNo],
  21.             0);
  22.     MB_YESNOCANCEL:
  23.       Res:= QuestionDlg(msgTitle, AText, Typ,
  24.             [mrYes, msgButtonYes,
  25.             mrNo, msgButtonNo,
  26.             mrCancel, msgButtonCancel],
  27.             0);
  28.     MB_RETRYCANCEL:
  29.       Res:= QuestionDlg(msgTitle, AText, Typ,
  30.             [mrRetry, msgButtonRetry,
  31.             mrCancel, msgButtonCancel],
  32.             0);
  33.     else
  34.       exit(ID_CANCEL);
  35.  

Using FPC 3.2 fixes.

y.ivanov

  • Hero Member
  • *****
  • Posts: 508
Re: How to shorten QuestionDlg calls?
« Reply #1 on: July 20, 2022, 08:08:35 am »
Unfortunately I didn't found an easy way to work with array of const. I wrote a utility class for that according to this. It encapsulates array of TVarRec and have Append/Prepend functionality.
As TVarRec needs some additional housekeeping for copying, it needs Create/Free pair, but now with advanced records t can be overhauled.

Currently it may be used like this:
Code: Pascal  [Select][+][-]
  1.   CA := TConstArrayAccess.Create([]);
  2.   case AFlags and $0F of
  3.     MB_OK:
  4.       begin
  5.         CA.Append([mrOk, msgButtonOk]);
  6.         Res:= QuestionDlg(msgTitle, AText, Typ, CA.AsConstArray, 0);
  7.       end;
  8.     MB_OKCANCEL:
  9.       ...
  10.   end;
  11.   CA.Free;

If that seems OK with you I can extract that class in a separate unit (because it is in a bulk with a lot more unrelated stuff) and I can share it.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 5161
Re: How to shorten QuestionDlg calls?
« Reply #2 on: July 20, 2022, 09:45:19 am »
So what is the actual parameter type there? Because it looks as though it should be a set rather than an array of const.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thausand

  • Sr. Member
  • ****
  • Posts: 291
Re: How to shorten QuestionDlg calls?
« Reply #3 on: July 20, 2022, 09:57:31 am »
So what is the actual parameter type there?
Array of const

Quote
Because it looks as though it should be a set rather than an array of const.
Looks not important, documentation more important: https://lazarus-ccr.sourceforge.io/docs/lcl/dialogs/questiondlg.html  ;)

y.ivanov

  • Hero Member
  • *****
  • Posts: 508
Re: How to shorten QuestionDlg calls?
« Reply #4 on: July 20, 2022, 10:34:32 am »
So what is the actual parameter type there? Because it looks as though it should be a set rather than an array of const.

MarkMLl
Code: Pascal  [Select][+][-]
  1.   TConstArray = array of TVarRec;
  2.  
  3.   TConstArrayAccess = class
  4.   private
  5.     A: TConstArray;
  6.     //...
  7.   public
  8.     constructor Create(const AArray: array of const);
  9.     destructor Destroy; override;
  10.  
  11.     procedure Append(const AArray: array of const);
  12.     procedure Prepend(const AArray: array of const);
  13.  
  14.     property AsInteger[I: Integer]: Integer read GetAsInteger;
  15.     property AsInt64[I: Integer]: Int64 read GetAsInt64;
  16.     property AsChar[I: Integer]: Char read GetAsChar;
  17.     property AsAnsiString[I: Integer]: AnsiString read GetAsAnsiString;
  18.     property AsBoolean[I: Integer]: Boolean read GetAsBoolean;
  19.     property AsExtended[I: Integer]: Extended read GetAsExtended;
  20.     property AsDateTime[I: Integer]: TDateTime read GetAsDateTime;
  21.     property Item[I: Integer]: TVarRec read GetItem; default;
  22.     property Count: Integer read GetCount;
  23.  
  24.     property AsConstArray: TConstArray read A;
  25.   end;  
« Last Edit: July 20, 2022, 10:36:44 am by y.ivanov »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

AlexTP

  • Hero Member
  • *****
  • Posts: 1900
    • UVviewsoft
Re: How to shorten QuestionDlg calls?
« Reply #5 on: July 20, 2022, 12:16:18 pm »
Okay, so there is no an easy way to solve it.

MarkMLl

  • Hero Member
  • *****
  • Posts: 5161
Re: How to shorten QuestionDlg calls?
« Reply #6 on: July 20, 2022, 12:19:18 pm »
So what is the actual parameter type there?
Array of const

Quote
Because it looks as though it should be a set rather than an array of const.
Looks not important, documentation more important: https://lazarus-ccr.sourceforge.io/docs/lcl/dialogs/questiondlg.html  ;)

So why not just wrap it in another function that takes a set as a parameter rather than an array of const?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thausand

  • Sr. Member
  • ****
  • Posts: 291
Re: How to shorten QuestionDlg calls?
« Reply #7 on: July 20, 2022, 12:33:02 pm »
So why not just wrap it in another function that takes a set as a parameter rather than an array of const?
And how that work ?

Quote
Buttons is a list of TModalResult (defined as constants [mrNone..MrYesToAll] in Controls) and strings.
For each TModalResult a button is created.
To set a custom caption, add a string after a button. You can use the special string parameters 'isdefault' and 'iscancel' to setup the default and cancel buttons.

Array of const is variable length because consist 1) button type, 2) button text then 3) optional button default. And 2 can be optional to (not sure) ?

Then need 3 wrapper functions. In end must be array of const to make fit QuestionDlg ?
« Last Edit: July 20, 2022, 12:37:51 pm by Thausand »

y.ivanov

  • Hero Member
  • *****
  • Posts: 508
Re: How to shorten QuestionDlg calls?
« Reply #8 on: July 21, 2022, 10:44:11 am »
I have assembled a small helper unit:
https://gist.github.com/alpinistbg/6f96130c5c032833ce8d2dabae656ffa

For the QuestionDlg it can be used that way:
Code: Pascal  [Select][+][-]
  1. var
  2.   CA: TConstArray;
  3.  
  4.   case AFlags and $0F of
  5.     MB_OK:
  6.       CA := [mrOk, msgButtonOk];
  7.     MB_OKCANCEL:
  8.       CA := [mrOk, msgButtonOk, mrCancel, msgButtonCancel];
  9.     MB_ABORTRETRYIGNORE:
  10.       CA := [mrAbort, msgButtonAbort, mrRetry, msgButtonRetry,
  11.             mrIgnore, msgButtonIgnore];
  12.     MB_YESNO:
  13.       CA := [mrYes, msgButtonYes, mrNo, msgButtonNo],
  14.     ...
  15.     else
  16.       exit(ID_CANCEL);
  17.   end;
  18.   Res := QuestionDlg(msgTitle, AText, Typ, CA.AsArray, 0);
  19.   ...

TConstArray predefined := and + operators, so + can be used to concatenate two const arrays:
Code: Pascal  [Select][+][-]
  1.   CA := [1, 2, 'three'];
  2.   CA := CA + [4, 5]; // [1,2,'three',4,5]
  3.   CA := [-1, 0] + CA; // [-1,0,1,2,'three',4,5]
  4.   // etc.

i.e. some buttons can be easily appended afterwards, which is more important for the way I use it.

Hope it can be useful.

"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018