Paste: send-to-trash

Author: mrjbq7
Mode: factor
Date: Mon, 9 Aug 2010 23:37:16
Plain Text |
LIBRARY: shell32

STRUCT: SHFILEOPSTRUCTW
    { hwnd HWND }
    { wFunc UINT }
    { pFrom LPCWSTR }
    { pTo LPCWSTR }
    { fFlags FILEOP_FLAGS }
    { fAnyOperationsAborted BOOL }
    { hNameMappings LPVOID }
    { lpszProgressTitle LPCWSTR } ;

FUNCTION: int SHFileOperationW (
    SHFILEOPSTRUCTW lpFileOp
) ;

CONSTANT: FO_MOVE HEX: 0001
CONSTANT: FO_COPY HEX: 0002
CONSTANT: FO_DELETE HEX: 0003
CONSTANT: FO_RENAME HEX: 0004

CONSTANT: FOF_MULTIDESTFILES HEX: 0001
CONSTANT: FOF_CONFIRMMOUSE HEX: 0002
CONSTANT: FOF_SILENT HEX: 0004
CONSTANT: FOF_RENAMEONCOLLISION HEX: 0008
CONSTANT: FOF_NOCONFIRMATION HEX: 0010
CONSTANT: FOF_WANTMAPPINGHANDLE HEX: 0020
CONSTANT: FOF_ALLOWUNDO HEX: 0040
CONSTANT: FOF_FILESONLY HEX: 0080
CONSTANT: FOF_SIMPLEPROGRESS HEX: 0100
CONSTANT: FOF_NOCONFIRMMKDIR HEX: 0200
CONSTANT: FOF_NOERRORUI HEX: 0400
CONSTANT: FOF_NOCOPYSECURITYATTRIBS HEX: 0800
CONSTANT: FOF_NORECURSION HEX: 1000
CONSTANT: FOF_NO_CONNECTED_ELEMENTS HEX: 2000
CONSTANT: FOF_WANTNUKEWARNING HEX: 4000
CONSTANT: FOF_NORECURSEREPARSE HEX: 8000

M: windows send-to-trash ( path -- )
    SHFILEOPSTRUCTW <struct>
        f >>hwnd
        FO_DELETE >>wFunc
        swap >>pFrom
        f >>pTo
        FOF_ALLOWUNDO
        FOF_NOCONFIRMATION bitor
        FOF_NOERRORUI bitor
        FOF_SILENT bitor >>flags
    SHFileOperationW win32-error=0/f ;

Annotation: "not responding"

Author: mrjbq7
Mode: factor
Date: Tue, 10 Aug 2010 03:09:59
Plain Text |
! Copyright (C) 2010 John Benediktsson
! See http://factorcode.org/license.txt for BSD license

USING: accessors alien.c-types alien.data alien.strings
alien.syntax classes.struct destructors io.encodings.string
io.encodings.utf16n io.pathnames kernel libc math math.parser
sequences system trash windows windows.errors windows.types ;

IN: trash.windows

LIBRARY: shell32

TYPEDEF: WORD FILEOP_FLAGS

STRUCT: SHFILEOPSTRUCTW
    { hwnd HWND }
    { wFunc UINT }
    { pFrom LPCWSTR }
    { pTo LPCWSTR }
    { fFlags FILEOP_FLAGS }
    { fAnyOperationsAborted BOOL }
    { hNameMappings LPVOID }
    { lpszProgressTitle LPCWSTR } ;

FUNCTION: int SHFileOperationW (
    SHFILEOPSTRUCTW lpFileOp
) ;


CONSTANT: FO_MOVE HEX: 0001
CONSTANT: FO_COPY HEX: 0002
CONSTANT: FO_DELETE HEX: 0003
CONSTANT: FO_RENAME HEX: 0004

CONSTANT: FOF_MULTIDESTFILES HEX: 0001
CONSTANT: FOF_CONFIRMMOUSE HEX: 0002
CONSTANT: FOF_SILENT HEX: 0004
CONSTANT: FOF_RENAMEONCOLLISION HEX: 0008
CONSTANT: FOF_NOCONFIRMATION HEX: 0010
CONSTANT: FOF_WANTMAPPINGHANDLE HEX: 0020
CONSTANT: FOF_ALLOWUNDO HEX: 0040
CONSTANT: FOF_FILESONLY HEX: 0080
CONSTANT: FOF_SIMPLEPROGRESS HEX: 0100
CONSTANT: FOF_NOCONFIRMMKDIR HEX: 0200
CONSTANT: FOF_NOERRORUI HEX: 0400
CONSTANT: FOF_NOCOPYSECURITYATTRIBS HEX: 0800
CONSTANT: FOF_NORECURSION HEX: 1000
CONSTANT: FOF_NO_CONNECTED_ELEMENTS HEX: 2000
CONSTANT: FOF_WANTNUKEWARNING HEX: 4000
CONSTANT: FOF_NORECURSEREPARSE HEX: 8000


M: windows send-to-trash ( path -- )
    [
        absolute-path 

        utf16n string>alien B{ 0 0 0 0 } append
        malloc-byte-array &free

        SHFILEOPSTRUCTW <struct>
            f >>hwnd
            FO_DELETE >>wFunc
            swap >>pFrom
            f >>pTo
            FOF_ALLOWUNDO
            FOF_NOCONFIRMATION bitor
            FOF_NOERRORUI bitor
            FOF_SILENT bitor >>fFlags

        SHFileOperationW dup 0 > [
            number>string "Error: " prepend throw
        ] [ drop ] if

    ] with-destructors ;

Annotation: c++ test

Author: mrjbq7
Mode: c++
Date: Tue, 10 Aug 2010 22:34:30
Plain Text |
// cl -EHsc hello.cpp

#include <iostream>
#include <windows.h>

int main( )
{
    std::cout << "Hello, World!\n";

    std::cout << "SHFILEOPSTRUCTA: " << sizeof(SHFILEOPSTRUCTA) << "\n";
    std::cout << "SHFILEOPSTRUCTW: " << sizeof(SHFILEOPSTRUCTW) << "\n";

    std::cout << "HWND: " << sizeof(HWND) << "\n";
    std::cout << "UINT: " << sizeof(UINT) << "\n";
    std::cout << "LPCTSTR: " << sizeof(LPCTSTR) << "\n";
    std::cout << "LPCWSTR: " << sizeof(LPCWSTR) << "\n";
    std::cout << "FILEOP_FLAGS: " << sizeof(FILEOP_FLAGS) << "\n";
    std::cout << "BOOL: " << sizeof(BOOL) << "\n";
    std::cout << "LPVOID: " << sizeof(LPVOID) << "\n";

    return 0;
}

Annotation: c++ output

Author: mrjbq7
Mode: text
Date: Tue, 10 Aug 2010 22:35:37
Plain Text |
C:\>hello
Hello, World!
SHFILEOPSTRUCTA: 30
SHFILEOPSTRUCTW: 30
HWND: 4
UINT: 4
LPCTSTR: 4
LPCWSTR: 4
FILEOP_FLAGS: 2
BOOL: 4
LPVOID: 4

Annotation: factor output

Author: mrjbq7
Mode: factor
Date: Tue, 10 Aug 2010 22:36:12
Plain Text |
( scratchpad ) SHFILEOPSTRUCTW <struct> byte-length .
32

New Annotation

Summary:
Author:
Mode:
Body: