$ filename "Demo7Dlg.exe"

'uses rtl32
'uses rtl64

'% review
uses dialogs

uses autosizing

% TCS_TABS=0
% TCIF_PARAM=8
% TCIF_TEXT=1
% TCM_GETITEM=0x1305
% TCM_INSERTITEM=0x1307
% TCM_GETCURSEL=0x130B
% TCN_SELCHANGE= -551
% TCN_SELCHANGING= -552

% HINST_COMMCTRL= -1
% IDB_STD_SMALL_COLOR=0
% IDB_STD_LARGE_COLOR=1

% TBSTYLE_FLAT=0x800
% TBSTATE_ENABLED=4
% TBSTYLE_BUTTON=0
% TB_ADDBITMAP=0x413
% TB_ADDBUTTONS=0x414
% TB_BUTTONSTRUCTSIZE=0x41E


type TCITEM int mask,dwState,dwStateMask, char* pszText, int cchTextMax,iImage, sys lParam

typedef TCITEM TC_ITEM

type TBADDBITMAP , sys hInst , sys  nID

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

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

enum  Control_ids
   ID_TC=1001
   ID_Btn1A 
   ID_Btn1B 
   ID_Btn1C 
   ID_Btn1D
   ID_Gr2 
   ID_Btn2A 
   ID_Btn2B 
   ID_Btn2C
   ID_BtnOpen
   ID_SBar
   ID_Toolbar
end enum

% ID_MENU0=9000
% ID_MENU1=9001
% ID_MENU2=9002
% ID_MENU3=9003
% ID_MENU4=9004
% ID_About=9005


declare sub AddMenu(sys hwnd)
declare sub AddToolbar(sys hwnd)
declare sub OpenWindow(sys hParent)

'============================================================
init_common_controls()

Dialog(0,0,225,255, "Autosize Demo 7",
       WS_OVERLAPPEDWINDOW Or DS_CENTER or WS_CLIPSIBLINGS or DS_SETFONT, 10, "MS Sans Serif" )
   Control( "", ID_Toolbar, "ToolbarWindow32", TBSTYLE_FLAT , 0, 0, 0, 0 )', WS_EX_DLGMODALFRAME )
   LTEXT "A TabControl with two pages, and a second Window", -1, 40,20, 200,20
   Control( "", ID_TC,  WC_TABCONTROL, WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPSIBLINGS | TCS_TABS , 10, 40, 180, 150 )
   Control( "", ID_SBar, STATUSCLASSNAME, 0, 0, 0, 0, 0 )
   PushButton("E&xit", IDCANCEL, 95,205, 40,15)
CreateModalDialog( null, @DlgProc, 0 )
'============================================================


function Tab1Proc( sys hDlg, uint uMsg, sys wParam, lParam) as sys callback
    select case uMsg
       case WM_COMMAND
          if loword(wParam)=ID_BtnOpen then          
            SendMessage(GetParent(hDlg), WM_COMMAND, ID_MENU1, lParam)
          end if  
    end select       
    return FALSE
end function

function Tab2Proc( sys hDlg, uint uMsg, sys wParam, lParam) as sys callback
    return FALSE
end function


function DlgProc( sys hDlg, uint uMsg, sys wParam, lParam) as sys callback
    sys id, event
        TCITEM ts
    NMHDR lpNMHDR
    sys hTab
    sys hStatus=GetDlgItem(hDlg, ID_SBar)

    select case uMsg
       case WM_INITDIALOG

          AddMenu(hDlg)
          AddToolbar(hDlg)

          'Get handle of Tabcontrol
          hTab=GetDlgItem(hDlg,ID_TC)
          ts.mask=TCIF_TEXT Or TCIF_PARAM

          'Create Tab1 child dialog
          ts.pszText="One"
          Dialog ( 10,50,200,150, "Tab1", WS_CHILD or WS_VISIBLE or DS_SETFONT, 10, "MS Sans Serif", WS_EX_DLGMODALFRAME)
            PushButton ("A", ID_Btn1A,  10, 18,15,15)
            PushButton ("B", ID_Btn1B, 175, 18,15,15)
            PushButton ("C", ID_Btn1C,  10,130,15,15)
            PushButton ("D", ID_Btn1D, 175,130,15,15)
            PushButton ("Open Window", ID_BtnOpen, 75,75,50,20)
          ts.lparam = Createmodelessdialog(hdlg,@Tab1Proc,0)
          sys page1=ts.lparam

          SendMessage(hTab,TCM_INSERTITEM,0, @ts)

          ' Create Tab2 child dialog
          ts.pszText="Two"
          Dialog ( 10,50,200,150, "Tab2",WS_CHILD or DS_SETFONT, 10, "MS Sans Serif", WS_EX_DLGMODALFRAME)
            Groupbox("Group", ID_Gr2, 80,60,100, 80)
            PushButton ("MR", ID_Btn2A,      160,72,15,15)
            PushButton ("MM", ID_Btn2B,       85,72,15,15)
            PushButton ("BM, BR", ID_Btn2C, 107,120,45,15)
          ts.lParam= Createmodelessdialog(hdlg,@Tab2Proc,0)
          sys page2=ts.lparam
 
          SendMessage(htab,TCM_INSERTITEM,1, @ts)
          
          pinCtl(hTab, hDlg, "TL","BR")

          pinCtl(page1,hDlg, "TL","BR")
          pinCtl(GetDlgItem(page1,ID_Btn1A),hDlg, "TL")
          pinCtl(GetDlgItem(page1,ID_Btn1B),hDlg, "TR")
          pinCtl(GetDlgItem(page1,ID_Btn1C),hDlg, "BL")
          pinCtl(GetDlgItem(page1,ID_Btn1D),hDlg, "BR")
          pinCtl(GetDlgItem(page1,ID_BtnOpen),hDlg, "MM")
                                        
          pinCtl(page2,hDlg, "TL","BR")
          pinCtl(GetDlgItem(page2,ID_Gr2),hDlg, "MM","BR" )
          pinCtl(GetDlgItem(page2,ID_Btn2A),hDlg, "MR")
          pinCtl(GetDlgItem(page2,ID_Btn2B),hDlg, "MM")
          pinCtl(GetDlgItem(page2,ID_Btn2C),hDlg, "BM","BR")

          pinCtl(GetDlgItem(hDlg,IDCANCEL), hDlg, "BM" )
          
          'Change Window dimensions
          MoveWindow(hDlg, 300,200, 640, 480, true)

       case WM_GETMINMAXINFO
           setMinSize 420,355  'pixels

       case WM_SIZE
          'Statusbar
          int SBheight
          RECT rcDlg, rcSB
          GetClientRect(hDlg, &rcDlg)
          GetWindowRect(hStatus, &rcSb)
          SBheight = rcSB.bottom - rcSB.top + 1
          MoveWindow( hStatus, 0, rcDlg.bottom - sbHeight ,
                      loword(lParam), rcDlg.bottom - sbHeight, true )
          SendMessage(hStatus, SB_SETTEXT, 0, "StatusBar" )
 
          'Toolbar
          RECT rcTB
          GetWindowRect( GetDlgItem(hDlg, ID_Toolbar), @rcTB )
          MoveWindow( GetDlgItem(hDlg, ID_Toolbar), 0, 0, loword(lParam), _
                      rcTB.bottom - rcTB.top + SBHeight + 1, true )
          
          ' Controls 
          resizeControls(hDlg)

       case WM_NOTIFY
          NMHDR lpNMHDR at lParam
          if lpNMHDR.code=TCN_SELCHANGING Then
            ' Hide the currently selected dialog
            id=SendMessage(lpNMHDR.hwndFrom,TCM_GETCURSEL,0,0)
            ts.mask=TCIF_PARAM
            SendMessage(lpNMHDR.hwndFrom,TCM_GETITEM,id, @ts)
            ShowWindow(ts.lParam,SW_HIDE)
          elseif lpNMHDR.code=TCN_SELCHANGE Then
            ' Show the currently selected dialog
            id=SendMessage(lpNMHDR.hwndFrom,TCM_GETCURSEL,0,0)
            ts.mask=TCIF_PARAM
            SendMessage(lpNMHDR.hwndFrom,TCM_GETITEM,id, @ts)
            ShowWindow(ts.lParam,SW_SHOW)
          endif

       case WM_CLOSE
          EndDialog(hDlg, 0)

       case WM_COMMAND
          id=loword(wParam)
          event=hiword(wParam)
          select case id
             case ID_MENU1
                OpenWindow(hDlg)
             case ID_About
                mbox "Autosizing Demo with Oxygenbasic"   
             case ID_MENU4, IDCANCEL
                EndDialog(hDlg, 0)
          end select

       case else
          return FALSE

    end select

    return TRUE
end function


=======================================================

% ID_MLE1=1020
% ID_MLE2=1021
% ID_Exit=1022

sub OpenWindow(sys hParent)
    Dialog( 0, 0, 200, 100, "Second Window",
            WS_OVERLAPPEDWINDOW or DS_CENTER )
       MultiLineText("MLE 1", ID_MLE1,   5,5, 95,70)
       MultiLineText("MLE 2", ID_MLE2, 100,5, 95,70)        
       PushButton( "Close" , ID_Exit, 80, 80, 40, 12 )
    CreateModalDialog( hParent, @DialogProcNewWin, 0)
end sub

function DialogProcNewWin( sys hDlg, uint uMsg, sys wParam, lParam ) as sys callback

    select case uMsg
  
    case WM_INITDIALOG
       pinCtl(GetDlgItem(hDlg,ID_Exit), hDlg, "BM")
       pinCtl(GetDlgItem(hDlg,ID_MLE1), hDlg, "TL", "BM")
       pinCtl(GetDlgItem(hDlg,ID_MLE2), hDlg, "TM", "BR")

    case WM_SIZE
       resizeControls(hDlg)

    case WM_COMMAND
      select case loword(wParam)
      case ID_Exit
         EndDialog( hDlg, null )
      end select
      
    case WM_CLOSE
       EndDialog( hDlg, null )
                
    end select

    return 0
end function

=======================================================

sub AddMenu(sys hwnd)
    sys MainMenu
    MENU(MainMenu)
    BEGIN
      POPUP "&File"
      BEGIN
        MENUITEM "&Open Window", ID_MENU1
        MENUITEM "&Save", ID_MENU1, MF_GRAYED
        MENUITEM "&Exit" tab "Alt+F4", ID_MENU4
      ENDMenu
      int x
      for x=2 to 11
         POPUP "Topic " x
         BEGIN
         ENDMenu
      next   
      POPUP "&Help"
      BEGIN
        MENUITEM "&About", ID_About
      ENDMenu
    ENDMenu
    ' activate menu
    if not SetMenu(hwnd, MainMenu) then
      mbox "Error: Cannot create Menu"
    end if
end sub

sub AddToolbar(sys hwnd)   
    sys hwndTB=GetDlgItem(hwnd, ID_Toolbar)

    dim as TBADDBITMAP tbab
    dim as TBBUTTON tbb[19]

    SendMessage( hWndTB,TB_BUTTONSTRUCTSIZE,sizeof(TBBUTTON),0 )

    tbab.hInst = HINST_COMMCTRL
    'tbab.nID = IDB_STD_SMALL_COLOR
    tbab.nID = IDB_STD_LARGE_COLOR
    SendMessage( hWndTB, TB_ADDBITMAP, 0, @tbab)

    int x
    for x = 1 to 14 
       tbb(x).iBitmap = x
       tbb(x).fsState = TBSTATE_ENABLED
       tbb(x).fsStyle = TBSTYLE_BUTTON
    next x      
    for x = 15 to 19  
       tbb(x).iBitmap = x-15
       tbb(x).fsState = TBSTATE_ENABLED
       tbb(x).fsStyle = TBSTYLE_BUTTON
    next x      

    ' Add the buttons to the toolbar.
    SendMessage( hWndTB,TB_ADDBUTTONS,19, @tbb )
end sub