'====================================================================
' Listview example, nested modeless dialog.
'====================================================================


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

'% review

uses dialogs


'Identifiers
#define IDD_DLG1 1000
#define IDC_LSV1 1001
#define IDM_EXIT        104
#define IDM_ABOUT       105
#define IDI_LOGO        106


macro ListView_InsertColumn(hwnd,iCol,pcol) {SendMessage(hwnd, LVM_INSERTCOLUMN, iCol, pcol)}
macro ListView_SetColumnWidth(hwnd,iCol,cx) {SendMessage(hwnd, LVM_SETCOLUMNWIDTH, iCol, cx)}
macro ListView_InsertItem(hwnd,pitem) {SendMessage(hwnd, LVM_INSERTITEM,0, pitem)}
macro ListView_SetItem(hwnd,pitem) {SendMessage(hwnd, LVM_SETITEM,0, pitem)}

% DS_CENTER=0x0800
% DS_MODALFRAME=0x80
% SS_CENTERIMAGE=0x200
% LVS_LIST  0x0003
% LVS_REPORT  0x0001
% LVS_EX_GRIDLINES 1
% LVS_EX_CHECKBOXES 4
% LVS_EX_FULLROWSELECT  0x0020
% LVSCW_AUTOSIZE  -1
% LVSCW_AUTOSIZE_USEHEADER  -2
% LVM_INSERTCOLUMN=4123
% LVM_SETCOLUMNWIDTH=4126
% LVM_INSERTITEM=4103
% LVM_SETITEM=4102
% LVCF_FMT 1
% LVCF_WIDTH 2
% LVCF_TEXT=4
% LVCF_SUBITEM 8
% LVIF_TEXT=1
% LVM_SETEXTENDEDLISTVIEWSTYLE 0x1036
% LVN_COLUMNCLICK = -108
% LVN_ITEMCHANGED = -101
% LR_LOADFROMFILE=0x0010
% IMAGE_ICON=1
% STM_SETIMAGE=0x172
% SWP_NOZORDER=4 

type LVCOLUMN 
  uint  mask 
  int   fmt 
  int   cx 
  char* pszText 
  int   cchTextMax 
  int   iSubItem 
  int   iImage
  int   iOrder
  int   cxMin
  int   cxDefault
  int   cxIdeal  
end type 
typedef LVCOLUMN LV_COLUMN

type LVITEM  
  uint   mask 
  int    iItem 
  int    iSubItem 
  uint   state 
  uint   stateMask 
  char*  pszText 
  int    cchTextMax 
  int    iImage       // index of the list view item's icon 
  sys    lParam       // 32-bit value to associate with item 
  int    iIndent
  int    iGroupId
  uint   cColumns
  uint   *puColumns
  int    *piColFmt
  int    iGroup
end type
typedef LVITEM LV_ITEM

% LVROWS = 200   ' Number of rows in the ListView
% LVCOLS = 3     ' Number of columns in the ListView

! GetDlgItem lib "user32.dll" (sys hDlg, int nIDDlgItem) as sys
! IsDialogMessage lib "user32.dll" alias"IsDialogMessageA" (sys hDlg, sys lpMsg) as bool
! IsWindow lib "user32.dll" (sys hWnd) as bool


sys AboutDlg
'string o2dir = "c:\oxygenbasic\"
string o2dir = "..\..\"
string fullname


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

  select case uMsg

    case WM_INITDIALOG
      fullname = o2dir + "tools\OxideIcon.ico"
      sys hIcon = LoadImage( 0, fullname, IMAGE_ICON,0,0, LR_LOADFROMFILE )
      if hIcon=null then mbox "Cannot load " + fullname +"!"
      SendMessage (GetDlgItem(hDlg,IDI_LOGO), STM_SETIMAGE, IMAGE_ICON, hIcon)

    case WM_COMMAND
      if loword(wParam) = IDCANCEL or
         loword(wParam) = IDOK then
        DestroyWindow( hDlg )
      end if

    case WM_CLOSE
      DestroyWindow( hDlg )

      'Reset so main can determine if nested dialog is open.
      AboutDlg = 0

  end select

  return 0
end function
'====================================================================

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

  sys hMenu
  sys hListview = GetDlgItem(hDlg, IDC_LSV1)  
  int i,j
  LV_COLUMN lvc
  LV_ITEM lvi
  select case uMsg

     case WM_INITDIALOG
        MENU(hMenu)
        
        BEGIN
          POPUP "&File"
          BEGIN
            MENUITEM "E&xit",IDM_EXIT
          ENDMenu
          POPUP "&Help"
          BEGIN
            MENUITEM "&About...",IDM_ABOUT
          ENDMenu
        ENDMenu

        if SetMenu( hDlg, hMenu ) = 0 then
          mbox "SetMenu hMenu failed!"
        end if

        string text
        'Column Headers
        For i = 0 To LVCOLS
           lvc.mask = LVCF_FMT OR LVCF_WIDTH OR LVCF_TEXT OR LVCF_SUBITEM 'LVCF_TEXT
           text="Column #" & str(i+1)
           lvc.pszText = text                
           ListView_InsertColumn(hListview, i, &lvC)
        Next i

        'Rows
        For i=1 To LVROWS
           'First column
           lvi.mask      =  LVIF_TEXT
           text = "Row #" & str(200-i+1) ", Col # 1"
           lvi.pszText   = text
           lvi.iSubItem  =  0
           ListView_InsertItem(hListview, &lvi)
           'Remaining columns
           for j=2 to LVCOLS
              text = "Row #" & str(200-i+1) ", Col # " & str(j)
              lvi.pszText   = text
              lvi.iSubItem  =  j-1
              ListView_SetItem(hListview, &lvi)
           next j
        Next i

        for i = 0 to LVCOLS
           ListView_SetColumnWidth(hListview,i,LVSCW_AUTOSIZE_USEHEADER)
        next i
        
        SendMessage(hListview, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,  LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES)
'        SendMessage(hListview, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,  LVS_EX_GRIDLINES)
'        SendMessage(hListview, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,  LVS_EX_CHECKBOXES)
          
    case WM_COMMAND

       select case loword(wParam)
          case IDM_ABOUT
            'Open nested dialog only if not already open.
            if IsWindow( AboutDlg ) = 0 then            
              Dialog( 0,0,144,77, "About Listview Sample",
                      WS_POPUP|WS_VISIBLE|WS_CAPTION|DS_MODALFRAME or DS_SETFONT or DS_CENTER,
                      8,"MS Sans Serif" )
              CONTROL "OK",IDOK,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,54,51,40,14
              CONTROL "Creating a Listview Control",-1,"Static",WS_CHILDWINDOW|WS_VISIBLE|WS_GROUP, 50,17,92,8
              CONTROL "in OxygenBasic",-1,"Static",WS_CHILDWINDOW|WS_VISIBLE|WS_GROUP,65,27,70,8
              Control(  "",IDI_LOGO, "STATIC", SS_ICON|SS_CENTERIMAGE, 0, 0, 40, 40 )
              AboutDlg = CreateModelessDialog( hDlg, @AboutDlgProc, 0 )
            end if

        case IDCANCEL, IDM_EXIT
           DestroyWindow( hDlg )
        end select

    case WM_NOTIFY
       NMHDR pnm at lParam
       
       if pnm.hwndFrom = hListview then      'ListView         
            select case pnm.code
              case LVN_COLUMNCLICK
mbox "LVN_COLUMNCLICK"                           
              case LVN_ITEMCHANGED
#ifdef review
  printl "LVN_ITEMCHANGED"
  printl
#endif              
            end select
       end if
  
      case WM_SIZE      
         RECT rcClient
      
         // Calculate remaining height and size edit
         GetClientRect(hDlg, &rcClient)
         SetWindowPos(hListview, NULL, 0, rcClient.top, rcClient.right, rcClient.bottom, SWP_NOZORDER)

    case WM_CLOSE
      DestroyWindow( hDlg )

    case WM_DESTROY
      PostQuitMessage( null )

  end select

  return 0
end function

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

sys hDlg
MSG wMsg

Dialog( 10,10,280,150, "Listview example",
        WS_OVERLAPPEDWINDOW or DS_CENTER or WS_VISIBLE,
        8,"MS Sans Serif" )

CONTROL "",IDC_LSV1,"SysListView32", _
        WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP|WS_BORDER|LVS_REPORT, _
        3,3,273,141,WS_EX_CLIENTEDGE

hDlg = CreateModelessDialog( 0, @DlgProc, 0 )

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