'====================================================================
' Toolbar demo, with tooltips, modeless dialog as main.
'====================================================================

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

'% review

uses dialogs


% HINST_COMMCTRL=-1
% IDB_STD_SMALL_COLOR=0
% STD_FILENEW=6
% STD_FILEOPEN=7
% STD_FILESAVE=8
% STD_FIND=12
% STD_COPY=1
% STD_CUT=0
% STD_PASTE=2
% STD_PRINT=14
% TBSTATE_ENABLED=4
% TBSTYLE_BUTTON=0
% TBSTYLE_SEP=1
% TBSTYLE_TOOLTIPS=0x100
% TBSTYLE_FLAT=0x800
% TTF_DI_SETITEM=0x8000
% TB_ADDBITMAP=0x413
% TB_ADDBUTTONS=0x414
% TB_BUTTONSTRUCTSIZE=0x41E
% TB_GETTOOLTIPS=0x423
% TTN_GETDISPINFO= -520
'% TTN_GETDISPINFO= -520
'dword TTN_GETDISPINFO= -520  '64-bit

type TBADDBITMAP
  sys       hInst
  sys  nID
end type

type TBBUTTON
  int       iBitmap
  int       idCommand
  BYTE      fsState
  BYTE      fsStyle
  dword     dwData
  sys       iString
end type

type NMTTDISPINFO
  NMHDR     hdr
  char*     lpszText
  zstring   szText[80]
  sys       hinst
  uint      uFlags
  sys       lParam
end type
typedef NMTTDISPINFO TOOLTIPTEXT  

'====================================================================

function DialogProc( sys hDlg, uint uMsg, sys wParam, lParam) as sys callback
indexbase 0

  static sys hTT
  static sys hWndTB

  select case uMsg

    case WM_INITDIALOG

      dim as TBADDBITMAP tbab
      dim as TBBUTTON tbb[6]

      hWndTB = GetDlgItem( hDlg, 100 )

      ' Specify the structure size so the system can determine
      ' which version of the common control DLL is being used.
      '
      SendMessage( hWndTB,TB_BUTTONSTRUCTSIZE,sizeof(TBBUTTON),0 )

      ' Add the system-defined bitmap button images to the
      ' list of available images.
      '
      tbab.hInst = HINST_COMMCTRL
      tbab.nID = IDB_STD_SMALL_COLOR
      SendMessage( hWndTB, TB_ADDBITMAP, 0, @tbab)

      ' For each button, specify the button image index,
      ' the associated command identifier, and the button
      ' state and style.
      '
      tbb(0).iBitmap = STD_FILENEW
      tbb(0).idCommand = 1000
      tbb(0).fsState = TBSTATE_ENABLED
      tbb(0).fsStyle = TBSTYLE_BUTTON
      tbb(1).iBitmap = STD_FILEOPEN
      tbb(1).idCommand = 1001
      tbb(1).fsState = TBSTATE_ENABLED
      tbb(1).fsStyle = TBSTYLE_BUTTON
      tbb(2).iBitmap = STD_FILESAVE
      tbb(2).idCommand = 1002
      tbb(2).fsState = TBSTATE_ENABLED
      tbb(2).fsStyle = TBSTYLE_BUTTON
      tbb(3).iBitmap = 0
      tbb(3).fsState = TBSTATE_ENABLED
      tbb(3).fsStyle = TBSTYLE_SEP
      tbb(4).iBitmap = STD_CUT
      tbb(4).idCommand = 1003
      tbb(4).fsState = TBSTATE_ENABLED
      tbb(4).fsStyle = TBSTYLE_BUTTON
      tbb(5).iBitmap = STD_COPY
      tbb(5).idCommand = 1004
      tbb(5).fsState = TBSTATE_ENABLED
      tbb(5).fsStyle = TBSTYLE_BUTTON
      tbb(6).iBitmap = STD_PASTE
      tbb(6).idCommand = 1005
      tbb(6).fsState = TBSTATE_ENABLED
      tbb(6).fsStyle = TBSTYLE_BUTTON

      ' Add the buttons to the toolbar.
      '
      SendMessage( hWndTB,TB_ADDBUTTONS,7, @tbb )

      ' Get the handle to the ToolTip control associated
      ' with the toolbar (by the TBSTYLE_TOOLTIPS style).
      '
      hTT = SendMessage( hWndTB,TB_GETTOOLTIPS,0,0 )

      return true

    case WM_COMMAND

      select case loword(wParam)
        case IDCANCEL
           DestroyWindow(hDlg)
        case 1000
          MessageBox( hDlg, "New", "", 0 )
        case 1001
          MessageBox( hDlg, "Open", "", 0 )
        case 1002
          MessageBox( hDlg, "Save", "", 0 )
        case 1003
          MessageBox( hDlg, "Cut", "", 0 )
        case 1004
          MessageBox( hDlg, "Copy", "", 0 )
        case 1005
          MessageBox( hDlg, "Paste", "", 0 )
      end select

    case WM_NOTIFY

      NMHDR pnm at lParam

      if pnm.hwndFrom = hTT then
        if pnm.code = TTN_GETDISPINFO then

          TOOLTIPTEXT pdi at lParam
'          TOOLTIPTEXT *pdi
'          @pdi = lParam 

          select case pdi.hdr.idFrom
            case 1000
              pdi.szText = "New"
            case 1001
              pdi.szText = "Open"
            case 1002
              pdi.szText = "Save"
            case 1003
              pdi.szText = "Cut"
            case 1004
              pdi.szText = "Copy"
            case 1005
              pdi.szText = "Paste"
          end select

          ' This causes the ToolTip control to retain
          ' the information after the first request.
          '
          pdi.uFlags = pdi.uFlags or TTF_DI_SETITEM

        end if
      end if

    case WM_SIZE

      RECT rcTB

      GetWindowRect( hWndTB, @rcTB )
      MoveWindow( hWndTB, 0, 0, loword(lParam), _
                  rcTB.bottom - rcTB.top + 1, false )

    case WM_CLOSE

      DestroyWindow( hDlg )

    case WM_DESTROY

      PostQuitMessage( null )

  end select

  return 0

end function

'====================================================================

sys hDlg
MSG wMsg

init_common_controls()

Dialog( 0, 0, 120, 90, "Toolbar Demo",
             WS_OVERLAPPEDWINDOW or DS_CENTER or WS_VISIBLE )

' Instead of trying to anticipate the position and/or size of
' the controls, just use zeros and set the correct values in
' the WM_SIZE handler.
'
Control( "", 100, TOOLBARCLASSNAME, TBSTYLE_TOOLTIPS or TBSTYLE_FLAT, 0, 0, 0, 0 )

hDlg = CreateModelessDialog( 0, @DialogProc, 0 )

while GetMessage( @wMsg, null, 0, 0 ) <> 0
  if IsDialogMessage( hDlg,  @wMsg ) = 0 then
    TranslateMessage( @wMsg )
    DispatchMessage( @wMsg )
  end if
wend

'====================================================================