iglobal
  Window  frmNewFile, $3, $0, 'TForm', 'New...', $00080000 or WS_TABSTOP, WS_EX_DLGMODALFRAME, 0, 201, 182, 320, 257, frmNewProc
  Window  NONE, $0, $0, LISTVIEW_CLASS, '', WS_VISIBLE or WS_CHILD or WS_TABSTOP or LVS_ICON or LVS_SINGLESEL or LVS_SHOWSELALWAYS, $200, 100, 0, 0, 312, 192, NULL
  Window  NONE, $0, $0, 'Button', 'OK', $50000001 or WS_TABSTOP, $0, MR_OK, 88, 200, 64, 25, NULL
  Window  NONE, $2, $0, 'Button', 'Cancel', $50000000 or WS_TABSTOP, $0, MR_CANCEL, 160, 200, 64, 25, NULL

  NewFilePrefix text 'Noname'
endg

uglobal
  hNewIml       dd ?
endg


;------------------------------------------
; actNewApplication action handler
;------------------------------------------
proc OnNew, .wparam, .lparam
.lvi LVITEM
.suf rb 8
begin
        stdcall CreateForm, frmNewFile, [hMainForm]
        stdcall ShowModal, ebx, MSF_CENTER
        cmp     eax, MR_OK
        jne     .finish

        invoke  SendDlgItemMessageW, ebx, 100, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
        cmp     eax, -1
        je      .finish

        mov     [.lvi.iItem], eax
        mov     [.lvi.iSubItem], 0
        mov     [.lvi.mask], LVIF_PARAM
        lea     eax, [.lvi]
        invoke  SendDlgItemMessageW, ebx, 100, LVM_GETITEMW, 0, eax
        test    eax, eax
        jz      .finish

        mov     eax, [.lvi.iItem]
        mov     eax, [FileTypeExt + 4*eax]
        mov     dword [.suf], eax
        mov     [.suf+4], 0
        lea     eax, [.suf]
        stdcall CreateUniqueName, NewFilePrefix, eax
        push    eax

        stdcall CreateFStruct, eax, [.lvi.lParam]
        mov     esi, eax

        mov     eax, [esi+TOpenFile.ptrType]

        cmp     eax, ftProject
        je      .added

        or      ecx, -1
        cmp     eax, ftMainSource
        jne     .type_ok

        xor     ecx, ecx
        cmp     [hProjManager], ecx
        jne     .type_ok

        mov     [esi+TOpenFile.ptrType], ftCommonSource
        jmp     .added

.type_ok:
        invoke  SendMessageW, [hProjManager], PMM_ADDFILE, esi, ecx

.added:
        stdcall LoadFile, esi, NULL

        stdcall StrDel ; from the stack.

.finish:
        invoke  DestroyWindow, ebx
        return
endp





proc frmNewProc, .hwnd, .wmsg, .wparam, .lparam
.lvi LVITEM
.count dd ?
begin
        cmp     [.wmsg], FM_AFTERCREATE
        je      .fmaftercreate

        cmp     [.wmsg], WM_NOTIFY
        je      .notify

.default:
        stc
        return

.fmaftercreate:
        stdcall ImageList_LoadGif, [hInstance], resBmpNewDlg, 32, FALSE
        invoke  SendDlgItemMessageW, [.hwnd], 100, LVM_SETIMAGELIST, LVSIL_NORMAL, eax

        mov     esi, FileTypeNames
        mov     edi, FileTypes
        mov     ebx, [esi]
        mov     [.count], ebx
        xor     ebx, ebx
        add     esi, 4

        invoke  SendDlgItemMessageW, [.hwnd], 100, LVM_SETITEMCOUNT, [.count], 0

.typeloop:
        mov     [.lvi.mask], LVIF_TEXT or LVIF_IMAGE or LVIF_PARAM
        lodsd   ; pointer to the string.

        stdcall utf8ToWideChar, eax
        mov     [.lvi.pszText], eax

        mov     eax, [edi+TFileType.iFileIcon]
        test    eax, eax
        jns     @f
        mov     eax, 8
@@:
        mov     [.lvi.iImage], eax
        mov     [.lvi.lParam], edi
        mov     [.lvi.iItem], ebx
        mov     [.lvi.iSubItem], 0
        lea     eax, [.lvi]
        invoke  SendDlgItemMessageW, [.hwnd], 100, LVM_INSERTITEMW, 0, eax
        stdcall FreeMem, [.lvi.pszText]

        add     edi, sizeof.TFileType
        inc     ebx
        dec     [.count]
        jnz     .typeloop

        invoke  SendDlgItemMessageW, [.hwnd], 100, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_BORDERSELECT

        mov     [.lvi.stateMask], TVIS_FOCUSED or TVIS_SELECTED
        mov     [.lvi.state], TVIS_FOCUSED or TVIS_SELECTED
        lea     eax, [.lvi]
        invoke  SendDlgItemMessageW, [.hwnd], 100, LVM_SETITEMSTATE, 1, eax

        clc
        return

.notify:
        cmp     [.wparam], 100
        jne     .default

        mov     esi, [.lparam]
        cmp     [esi+NMHDR.code], NM_DBLCLK
        jne     .default

        invoke  SetPropW, [.hwnd], [propModalResult], MR_OK

        clc
        return
endp