; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Common dialogs procedures library.
;
;  Target OS: Any
;
;  Dependencies:
;
;  Notes:
;_________________________________________________________________________________________
module "Common dialogs library"


iglobal
   getfile _DlgIconError,       '%lib%/gui/images/error.png'
   getfile _DlgIconInformation, '%lib%/gui/images/information.png'
   getfile _DlgIconQuestion,    '%lib%/gui/images/question.png'
   getfile _DlgIconWarning,     '%lib%/gui/images/warning.png'

   if used _CommonDialogTemplate
    _CommonDialogTemplate:
        ObjTemplate  tfParent or tfEnd, TForm, frmCommonDialog,  x = 200, y = 100, width = 480, height = 100, border = borderModal
           ObjTemplate tfChild, TImageLabel, .imgIcon, x = 4, y = 4, width = 70, height = 70, Visible = TRUE, ImageAlign = iaCenter or iaMiddle
           ObjTemplate tfChild, TLabel,  .LblText,   x = 74, y = 4, width = 10, height = 10, TextAlign = dtfAlignLeft or dtfAlignMiddle or dtfWordWrap or dtfCRLF, Visible = TRUE

           ObjTemplate tfChild, TButton, .btnOK,     x = 0, y = 0, width = 64, height = 24, TextAlign = dtfAlignCenter  or dtfAlignMiddle, Caption = 'OK',     ModalResult = mrOK
           ObjTemplate tfChild, TButton, .btnCancel, x = 0, y = 0, width = 64, height = 24, TextAlign = dtfAlignCenter  or dtfAlignMiddle, Caption = 'Cancel', ModalResult = mrCancel
           ObjTemplate tfChild, TButton, .btnAbort,  x = 0, y = 0, width = 64, height = 24, TextAlign = dtfAlignCenter  or dtfAlignMiddle, Caption = 'Abort',  ModalResult = mrAbort
           ObjTemplate tfChild, TButton, .btnRetry,  x = 0, y = 0, width = 64, height = 24, TextAlign = dtfAlignCenter  or dtfAlignMiddle, Caption = 'Retry',  ModalResult = mrRetry
           ObjTemplate tfChild, TButton, .btnIgnore, x = 0, y = 0, width = 64, height = 24, TextAlign = dtfAlignCenter  or dtfAlignMiddle, Caption = 'Ignore', ModalResult = mrIgnore
           ObjTemplate tfChild, TButton, .btnYes,    x = 0, y = 0, width = 64, height = 24, TextAlign = dtfAlignCenter  or dtfAlignMiddle, Caption = 'Yes',    ModalResult = mrYes
           ObjTemplate tfChild, TButton, .btnNo,     x = 0, y = 0, width = 64, height = 24, TextAlign = dtfAlignCenter  or dtfAlignMiddle, Caption = 'No',     ModalResult = mrNo
           ObjTemplate tfChild, TButton, .btnMaybe,  x = 0, y = 0, width = 64, height = 24, TextAlign = dtfAlignCenter  or dtfAlignMiddle, Caption = 'Maybe',  ModalResult = mrMaybe
           ObjTemplate tfEnd,   TButton, .btnHelp,   x = 0, y = 0, width = 64, height = 24, TextAlign = dtfAlignCenter  or dtfAlignMiddle, Caption = 'Help',   ModalResult = mrNone
   end if
endg


; buttons flags used:
smbOK     = $01
smbCancel = $02
smbAbort  = $04
smbRetry  = $08
smbIgnore = $10
smbYes    = $20
smbNo     = $40
smbMaybe  = $80
smbHelp   = $100
smbMaxButton = smbHelp

smiError       = 0
smiInformation = 1
smiQuestion    = 2
smiWarning     = 3



proc ShowMessage, .parent, .icon, .hTitle, .hMessage, .maskButtons
.xrow dd ?
.yrow dd ?
.hicon dd ?
begin
        push    ebx ecx edx esi
        stdcall CreateFromTemplate, _CommonDialogTemplate, 0

        set     [frmCommonDialog], TWindow:_MainWindow, [.parent]

; First set the position and size of the dialog box, depending on the flags, and the size of the message.

; Set the message text:
        set     [frmCommonDialog.LblText], TLabel:Caption, [.hMessage]

; Set the message title:
        set     ebx, TForm:Caption, [.hTitle]

; arrange the buttons
        mov     eax, [.maskButtons]
        mov     ecx, 32
        xor     edx, edx
.count:
        rol     eax, 1
        adc     edx, 0
        loop    .count

        mov     ecx, edx
        imul    edx, 64         ; button width
        jecxz   @f
        dec     ecx
        imul    ecx, 16
@@:
        add     edx, ecx        ; edx contains the width of the button row in pixels.

        get     eax, ebx, TForm:width
        cmp     eax, edx
        jge     .widthok

        lea     eax, [edx+32]
        set     ebx, TForm:width, eax

.widthok:

        sub     eax, edx
        sar     eax, 1  ; eax is the begin X coordinate of the row
        mov     [.xrow], eax

        get     eax, ebx, TForm:height
        sub     eax, 24+8
        mov     [.yrow], eax

        mov     edx, .btnTable
        mov     ecx, $1

.btnLoop:
        test    [.maskButtons], ecx
        jz      @f

        mov     esi, [edx]

        set     [esi], TButton:x, [.xrow]
        set     [esi], TButton:y, [.yrow]

        set     [esi], TButton:Visible, TRUE
        add     [.xrow], 64+16

@@:
        add     edx, 4
        shl     ecx, 1
        cmp     ecx, smbMaxButton
        jbe     .btnLoop

; set the icon
        mov     eax, [.icon]
        cmp     eax, .iconCount
        jb      @f
        xor     eax, eax
@@:
        stdcall CreateImagePNG, [.iconTable+4*eax], [.iconSizes+4*eax]

        mov     [.hicon], eax
        set     [frmCommonDialog.imgIcon], TImageLabel:Image, eax

; set the text position and size.
        mov     ecx, [.yrow]
        sub     ecx, 8

        set     [frmCommonDialog.LblText], TLabel:height, ecx

        set     [frmCommonDialog.imgIcon], TImageLabel:height, ecx

        get     eax, [frmCommonDialog], TForm:width
        get     ecx, [frmCommonDialog.LblText], TLabel:x

        sub     eax, ecx
        set     [frmCommonDialog.LblText], TLabel:width, eax

;        exec    [frmCommonDialog.LblText], TWindow:Refresh
;        exec    [frmCommonDialog.imgIcon], TWindow:Refresh

        stdcall ShowModal, ebx, [.parent]

        destroy ebx

        stdcall DestroyImage, [.hicon]

        pop     esi edx ecx ebx
        return

.btnTable dd frmCommonDialog.btnOK,     \
             frmCommonDialog.btnCancel, \
             frmCommonDialog.btnAbort,  \
             frmCommonDialog.btnRetry,  \
             frmCommonDialog.btnIgnore, \
             frmCommonDialog.btnYes,    \
             frmCommonDialog.btnNo,     \
             frmCommonDialog.btnMaybe,  \
             frmCommonDialog.btnHelp

.iconTable dd _DlgIconError, _DlgIconInformation, _DlgIconQuestion, _DlgIconWarning
.iconCount = ($ - .iconTable)/4

.iconSizes dd _DlgIconError.size, _DlgIconInformation.size, _DlgIconQuestion.size, _DlgIconWarning.size

endp


proc InputString, .parent
begin


        return
endp






endmodule