SOL9 2.0 Class: DirectoryDialog

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * DirectoryDialog.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2000.02.18
#pragma once

#include <sol\FileDialog.h>
#include <dlgs.h>
#include <sol\stdio.h>
#include <io.h>
#include <direct.h>

#define WM_DIRCHANGED   (WM_USER+100)
#define WM_DRIVECHANGED (WM_USER+101)

#define    DIR_FIELD    256

namespace SOL {


class DirectoryDialog :public FileDialog {
private:
    TCHAR*    dir;

private:
    long dirChanged(Event& event)
    {
        HWND hwnd = getWindow();
        HWND child = ::GetDlgItem(hwnd, lst2);
        int id = ::SendMessage(child, LB_GETCURSEL, 0, 0);

        TCHAR buff[_MAX_PATH];
        //ZeroMemory(buff, _MAX_PATH);
        memset(buff, (TCHAR)0, SizeOf(buff));

        TCHAR item[_MAX_PATH];
        //ZeroMemory(item, _MAX_PATH);
        memset(item, (TCHAR)0, SizeOf(item));

        buff[0] = (TCHAR)0;

        for(int i = 0; i<= id; i++) {
            ::SendMessage(child, LB_GETTEXT, i, (LPARAM)item);
            strcat_s(buff, sizeof(buff), item);

            if(i != id && strrchr(item, (TCHAR)'\\') == NULL) {
                strcat_s(buff, SizeOf(buff)-strlen(buff), _T("\\"));
            }
        }
        child = ::GetDlgItem(hwnd, DIR_FIELD);
        ::SetWindowText(child, buff);
        setDir(buff);
        return NULL;
    }

private:
    long driveChanged(Event& event)
    {
        HWND hwnd    = getWindow();
        HWND control = ::GetDlgItem(hwnd, cmb2);

        int    id = ::SendMessage(control, CB_GETCURSEL, 0, 0);
        if(id > -1) {
            TCHAR drive[10];         
            ::SendMessage(control, CB_GETLBTEXT, id, (LPARAM)drive);
        //    Printf("Drive %s \r\n", drive);
            TCHAR cdir[_MAX_PATH];
            ::GetCurrentDirectory(sizeof(cdir), cdir);
            HWND child = ::GetDlgItem(hwnd, DIR_FIELD);
            ::SetWindowText(child, cdir);
        }
        return NULL;
    }

private:
    long initDialog(Event& event)
    {
        RECT p;
        HWND hwnd = getWindow();
        getClientRect(&p);
        int width = p.right - p.left;
        int height = p.bottom - p.top;

        // Hide some controls.
        ::ShowWindow(::GetDlgItem(hwnd,stc1), SW_HIDE);
        ::ShowWindow(::GetDlgItem(hwnd, lst1), SW_HIDE);                
        ::ShowWindow(::GetDlgItem(hwnd,stc2), SW_HIDE);
        ::ShowWindow(::GetDlgItem(hwnd,stc3), SW_HIDE);
        ::ShowWindow(::GetDlgItem(hwnd,cmb1), SW_HIDE);

        RECT r;
        HWND child = ::GetDlgItem(hwnd,IDOK);
        ::GetWindowRect(child, &r);
        int w = r.right - r.left;
        int h = r.bottom - r.top;
        // Move thd FolderLabel.     
        child = ::GetDlgItem(hwnd, 0xffff);
        ::MoveWindow(child, 10, 4, width - w -40, h/2, TRUE);

        // Hide the Inputfield
        child = ::GetDlgItem(hwnd, edt1);
        ::ShowWindow(child, SW_HIDE);
        RECT e;
        ::GetWindowRect(child, &e);
        HFONT hfont = (HFONT)::SendMessage(child, WM_GETFONT, 0, 0);

        // Create a new edit control to display a directory.
        HINSTANCE hInst = (HINSTANCE)::GetWindowLong(hwnd, GWL_HINSTANCE);
        DWORD exStyle = ::GetWindowLong(child, GWL_EXSTYLE);
        OPENFILENAME* ofn = get();
        child = ::CreateWindowEx(exStyle, _T("Edit"), ofn->lpstrInitialDir, 
            WS_CHILD|WS_TABSTOP|WS_VISIBLE|ES_AUTOHSCROLL,
            10, 8+h/2, width - w -40, e.bottom - e.top, hwnd,
            (HMENU)DIR_FIELD, hInst, NULL);
        if(hfont) {
            ::SendMessage(child, WM_SETFONT, (WPARAM)hfont, TRUE);
        }
        // Move the DirListbox.
        child = ::GetDlgItem(hwnd, lst2);
        ::MoveWindow(child, 10, 8+h/2+h+4, width - w -40,height-4*h, TRUE);

        // Move the DriveLabel.
        child = ::GetDlgItem(hwnd,stc4);
        ::GetWindowRect(child, &r);
        ::MoveWindow(child, 10, height-h-8-h/2, (width-w-40), h/2, TRUE);

        // Move the DriveCombobox.
        child = ::GetDlgItem(hwnd,cmb2);
        ::MoveWindow(child, 10, height-h-4, (width-w-40), h*5, TRUE);
 
        return FALSE;
    }


private:
    long command(Event& event)
    {
        HWND hwnd    = getWindow();
        WORD notify  = (WORD)event.getNotification(); //2000/11/18
     HWND control = (HWND)event.getLParam();

        WORD controlId = LOWORD(event.getWParam());
        if(controlId == lst2 && notify == LBN_DBLCLK) {
            ::PostMessage(hwnd, WM_DIRCHANGED, 0, 0);                        
        }
        if(controlId == cmb2 && notify == CBN_SELCHANGE) {
            ::PostMessage(hwnd, WM_DRIVECHANGED, 0, 0);
        }

        if(controlId == IDOK) {
            TCHAR text[_MAX_PATH];        
            HWND child = ::GetDlgItem(hwnd, DIR_FIELD);
            ::GetWindowText(child, text, sizeof(text));
            setDir(text);
            ::EndDialog(hwnd, IDOK);
        }
        return FALSE;
    }


private:
    void setDir(TCHAR* string)
    {
        if(string) {
            delete [] dir;
            int slen = strlen(string)+1;
            dir = new TCHAR[slen];
            strcpy_s(dir, slen, string);
        }
    }


public:
    DirectoryDialog():FileDialog() { }


public:
    DirectoryDialog(View* parent, const TCHAR* name, Args& args)
    :FileDialog(parent, name, 
        args.set(XmNflags, OFN_HIDEREADONLY|OFN_LONGNAMES)
            .set(XmNhook, (ulong)&CommonDialog::hook))
    {
        dir = NULL;
        addEventHandler(WM_COMMAND, this,
            (Handler)&DirectoryDialog::command, NULL);

        addEventHandler(WM_DIRCHANGED, this,
            (Handler)&DirectoryDialog::dirChanged, NULL);
        addEventHandler(WM_DRIVECHANGED, this,
            (Handler)&DirectoryDialog::driveChanged, NULL);

        addEventHandler(WM_INITDIALOG, this,
            (Handler)&DirectoryDialog::initDialog, NULL);
    }


public:
    virtual Boolean create(View* parent, const TCHAR* name, Args& args)
    {
        Boolean rc = FileDialog::create(parent, name, 
            args.set(XmNflags, OFN_HIDEREADONLY|OFN_LONGNAMES)
                .set(XmNhook, (ulong)&CommonDialog::hook));

        dir = NULL;
        addEventHandler(WM_COMMAND, this,
            (Handler)&DirectoryDialog::command, NULL);

        addEventHandler(WM_DIRCHANGED, this,
            (Handler)&DirectoryDialog::dirChanged, NULL);
        addEventHandler(WM_DRIVECHANGED, this,
            (Handler)&DirectoryDialog::driveChanged, NULL);

        addEventHandler(WM_INITDIALOG, this,
            (Handler)&DirectoryDialog::initDialog, NULL);
        return rc;
    }

public:
    ~DirectoryDialog()
    {
        delete [] dir;
    }


public:
    TCHAR*    getDir() { return dir; }
};

}




Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.