'====================================================================
' Test program for custom control Progressbar include file: PGBAR3D.inc
'====================================================================

$ filename "Pgbar3d.exe"
'
'uses rtl32
'uses rtl64

'% review
uses dialogs
uses pgbar3d


%ID_BUTNSTART   = 101
%ID_BUTNREVERSE = 102
%ID_CHKTXT      = 110
%ID_OPTFAST     = 120
%ID_OPTSLOW     = 121
%ID_BARLEFT     = 201
%ID_BARTOP      = 202
%ID_BARRIGHT    = 203
%ID_BARBOTTOM   = 204
%ID_BARMID      = 205


INITCOMMONCONTROLSEXt icce
'Load the common controls library...
icce.dwSize = sizeof(INITCOMMONCONTROLSEXt)
icce.dwICC = 0xffff
InitCommonControlsEx(&icce)
'print sizeof INITCOMMONCONTROLSEXt

char* cmdline
&cmdline=GetCommandLine()
sys hInstance = GetModuleHandle(null)

sub winmain()

   Dialog( 0, 0, 200, 138, "PGBAR3D v2 demo", WS_CAPTION or WS_SYSMENU or DS_CENTER or DS_SETFONT,8, "MS Sans Serif" )
   PushButton( "Start" ,   ID_BUTNSTART,   100, 52, 75, 14 )
   PushButton( "Reverse" , ID_BUTNREVERSE,  25, 52, 75, 14 )
   RadioButton("Fast",     ID_OPTFAST,      30, 28, 40, 10 )
   RadioButton("Slow",     ID_OPTSLOW,      30, 38, 40, 10 )
   AutoCheckBox("Text on/off", ID_CHKTXT,   97, 28, 50, 10 )
   CreateModalDialog( null, @DialogProc, 0)
end sub

winmain()

'---------------------------------------------------------------------------------

sub doEvents()
   MSG tMsg   
   while PeekMessage(&tMsg, null, 0,0, PM_NOREMOVE)
      sys bRet = GetMessage (&tMsg, null, 0, 0)
      if bRet = 0 then   'WM_QUIT
        PostQuitMessage(tMsg.wParam)
        exit while 
      elseif bRet = -1 then
        'show an error message
        print "Error in doEvents"
      else
        TranslateMessage(&tMsg)
        DispatchMessage(&tMsg)
      end if
   wend
end sub


function DialogProc( sys hDlg, uint uMsg, sys wParam, lParam ) as sys callback
  select case uMsg
    case WM_INITDIALOG
        int st3d=WS_CHILD OR WS_VISIBLE
        SendMessage(GetDlgItem(hDlg, ID_OPTFAST), BM_SETCHECK, true, 0)
        EnableWindow(GetDlgItem(hDlg, ID_BUTNREVERSE), false)
'print hdlg cr ID_BARLEFT cr WS_EX_CLIENTEDGE
        CreatePGBar3D (hDlg, ID_BARLEFT, "", 5, 20, 14, 98, st3d, WS_EX_CLIENTEDGE,1)      'LEFT BAR, VERTICAL
        SendMessage(GetDlgItem(hDlg , ID_BARLEFT), PGB_SETMAX, 100, 0)                      'max number of steps
        SendMessage(GetDlgItem(hDlg, ID_BARLEFT), PGB_SETBARDIR, 2, 0)                     'bar direction, bottom - top
        SendMessage(GetDlgItem(hDlg, ID_BARLEFT), PGB_SETGRADIENTDIR, 1, 0)                'vertical gradient
        SendMessage(GetDlgItem(hDlg, ID_BARLEFT), PGB_SETBARCOL, PGB_GOLD, 0)              'bar color scheme
        SendMessage(GetDlgItem(hDlg, ID_BARLEFT), PGB_SETTXTCOLBKG, RGB(255, 255, 0), 0)   'backgound text color
        SendMessage(GetDlgItem(hDlg, ID_BARLEFT), PGB_SETTXTCOLBAR, RGB(0, 0, 255), 0)     'bar text color
        SendMessage(GetDlgItem(hDlg, ID_BARLEFT), PGB_SETTXTANGLE, 90, 0)                  'text angle, vertical up

        CreatePGBar3D (hDlg, ID_BARTOP, "", 5, 4, 190, 14, st3d, WS_EX_CLIENTEDGE,1)       'TOP BAR, HORIZONTAL
        SendMessage(GetDlgItem(hDlg, ID_BARTOP), PGB_SETMAX, 100, 0)                       'max number of steps
        SendMessage(GetDlgItem(hDlg, ID_BARTOP), PGB_SETBARDIR, 0, 0)                      'bar direction, left - right
        SendMessage(GetDlgItem(hDlg, ID_BARTOP), PGB_SETGRADIENTDIR, 0, 0)                 'horizontal gradient
        SendMessage(GetDlgItem(hDlg, ID_BARTOP), PGB_SETBARCOL, PGB_CYAN, 0)               'bar color scheme
        SendMessage(GetDlgItem(hDlg, ID_BARTOP), PGB_SETTXTCOLBKG, RGB(0,255,255), 0)      'backgound text color
        SendMessage(GetDlgItem(hDlg, ID_BARTOP), PGB_SETTXTCOLBAR, RGB(0,0,0), 0)          'bar text color

        CreatePGBar3D (hDlg, ID_BARRIGHT, "", 181, 20, 14, 98, st3d, WS_EX_CLIENTEDGE,1)   'RIGHT BAR, VERTICAL
        SendMessage(GetDlgItem(hDlg, ID_BARRIGHT), PGB_SETMAX, 100, 0)                     'max number of steps
        SendMessage(GetDlgItem(hDlg, ID_BARRIGHT), PGB_SETBARDIR, 3, 0)                    'bar direction, top - bottom
        SendMessage(GetDlgItem(hDlg, ID_BARRIGHT), PGB_SETGRADIENTDIR, 1, 0)               'vertical gradient
        SendMessage(GetDlgItem(hDlg, ID_BARRIGHT), PGB_SETBARCOL, PGB_RED, 0)              'bar edge color
        SendMessage(GetDlgItem(hDlg, ID_BARRIGHT), PGB_SETCOLBKG, RGB(191,191,191), 0)     'background color
        SendMessage(GetDlgItem(hDlg, ID_BARRIGHT), PGB_SETTXTCOLBKG, RGB(255,0,0), 0)      'backgound text color
        SendMessage(GetDlgItem(hDlg, ID_BARRIGHT), PGB_SETTXTCOLBAR, RGB(255,255,0), 0)    'bar text color
        SendMessage(GetDlgItem(hDlg, ID_BARRIGHT), PGB_SETTXTANGLE, 270, 0)                'text angle, vertical down

        CreatePGBar3D (hDlg, ID_BARBOTTOM, "", 5, 120, 190, 14,st3d, WS_EX_CLIENTEDGE,1)   'BOTTOM BAR, HORIZONTAL
        SendMessage(GetDlgItem(hDlg, ID_BARBOTTOM), PGB_SETMAX, 100, 0)                    'max number of steps
        SendMessage(GetDlgItem(hDlg, ID_BARBOTTOM), PGB_SETBARDIR, 1, 0)                   'bar direction, right - left
        SendMessage(GetDlgItem(hDlg, ID_BARBOTTOM), PGB_SETGRADIENTDIR, 0, 0)              'horizontal gradient
        SendMessage(GetDlgItem(hDlg, ID_BARBOTTOM), PGB_SETCOLBKG, RGB(191,191,191), 0)    'background color
        SendMessage(GetDlgItem(hDlg, ID_BARBOTTOM), PGB_SETBARCOL, PGB_BLUE, 0)            'bar mid color
        SendMessage(GetDlgItem(hDlg, ID_BARBOTTOM), PGB_SETTXTCOLBKG, RGB(0,0,196), 0)     'backgound text color
        SendMessage(GetDlgItem(hDlg, ID_BARBOTTOM), PGB_SETTXTCOLBAR, RGB(255,255,255), 0) 'bar text color
        SendMessage(GetDlgItem(hDlg, ID_BARBOTTOM), PGB_SETTXTANGLE, 180, 0)               'text angle, upside down

        CreatePGBar3D (hDlg, ID_BARMID, "", 22, 70, 157, 48, st3d, WS_EX_CLIENTEDGE,1)     'MID BAR, HORIZONTAL
        SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETMAX, 100, 0)                       'max number of steps
        SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETBARDIR, 2, 0)                      'bar direction, bottom - top
        SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETGRADIENTDIR, 0, 0)                 'horizontal gradient
        SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETCOLBKG, RGB(0,0,0), 0)             'background color
        SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETBARCOL, PGB_SILVER, 0)             'bar edge color
        SendMessage(GetDlgItem(hDlg, ID_BARMID),  PGB_SETTXTCOLBKG, RGB(0,255,255), 0)     'backgound text color


        string txt
        txt = "Click Start to see action"                                  'set background text in middle progressbar
        SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETTXTBKG, txt, 0)
        txt = "Hope you'll find it useful!"                                'set bar text in middle progressbar
        SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETTXTBAR, txt, 0)
        SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETTXTON, 0, 2)       'tell control to use custom text

        SendMessage(GetDlgItem(hDlg, ID_BARLEFT),   PGB_BUILDBARS, 0, 0)   'finally, build the bars - IMPORTANT!
        SendMessage(GetDlgItem(hDlg, ID_BARTOP),    PGB_BUILDBARS, 0, 0)
        SendMessage(GetDlgItem(hDlg, ID_BARRIGHT),  PGB_BUILDBARS, 0, 0)
        SendMessage(GetDlgItem(hDlg, ID_BARBOTTOM), PGB_BUILDBARS, 0, 0)
        SendMessage(GetDlgItem(hDlg, ID_BARMID),    PGB_BUILDBARS, 0, 0)
    case WM_COMMAND
       sys id=loword(wParam)
       sys event=hiword(wParam)
       int I,J,K, slow
       static int allSteps
       string txt
    
       if event=BN_CLICKED then
          select case id           
            case ID_BUTNSTART
               allSteps = SendMessage(GetDlgItem(hDlg, ID_BARTOP), PGB_GETMAX, 0, 0 )  'get number of steps, usually 100             
               EnableWindow(GetDlgItem(hDlg, ID_BUTNSTART), false)                     '<- disable start button
               txt = "Please wait.."                                             '<- set new text in middle progressbar
               SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETTXTBKG, txt, 1)
               for K = 0 to 4                                                    '<- run one control at the time                
                  for I = 1 to allSteps
                     slow = SendMessage(GetDlgItem(hDlg, ID_OPTSLOW), BM_GETCHECK, 0, 0)     '<- check settings for slow/fast
                     SendMessage(GetDlgItem(hDlg, ID_BARLEFT + K), PGB_STEPUP, 0, 0)
                    if mod(I,2) = 0 then doEvents()                              '<- to avoid dialog "freeze" in loop..
                    Sleep 1
                    if slow then Sleep 39                                        '<- if "slow" has been selected
                  next
               next
               EnableWindow(GetDlgItem(hDlg, ID_BUTNREVERSE), true)              '<- enable reverse button

            case ID_BUTNREVERSE              
               EnableWindow(GetDlgItem(hDlg, ID_BUTNREVERSE), false)
               for K = 0 to 4                  
                  for I = 1 to allSteps
                    slow = SendMessage(GetDlgItem(hDlg, ID_OPTSLOW), BM_GETCHECK, 0, 0)
                     SendMessage(GetDlgItem(hDlg, (ID_BARMID - K)), PGB_STEPDN, 0, 0)
                    if mod(I,2) = 0 then doEvents() 
                    Sleep 1
                    if slow then SLEEP 39                                         'if "slow" has been selected
                  next
               next
               txt = "Click Start to see action"                                  'set new text in middle progressbar
               SendMessage(GetDlgItem(hDlg, ID_BARMID), PGB_SETTXTBKG, txt, 1)
               EnableWindow(GetDlgItem(hDlg, ID_BUTNSTART), true)                 '<- enable start button

            case ID_CHKTXT 'text on/off
               J=SendMessage(GetDlgItem(hDlg, ID_CHKTXT), BM_GETCHECK, 0, 0)      '<- set auto text (%) on/off
               for K = 0 to 3
                  SendMessage(GetDlgItem(hDlg, (ID_BARLEFT + K)), PGB_SETTXTON, 0, J)
                  SendMessage(GetDlgItem(hDlg, (ID_BARLEFT + K)), PGB_REFRESH, 0, 0)
               next

          end select
       end if
       
    case WM_CLOSE
       EndDialog( hDlg, null )              
  end select

  return 0
end function


