SOL9 2.0 Class: ApplicationView

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

Source code

/*
 * ApplicationView.h 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2008/07/13
// 2008/09/16 Added Profile instance variable to store user's application setting
// such as a window's geomery, a file folder selected in FileDialog.
//    Two methods saveFileFolder/restoreFileFolder, and modified savePlacement and restorePlacement
// methods not to take any arguments.

#pragma once

#include <sol\Application.h>
#include <sol\Composite.h>
#include <sol\MenuBar.h>
#include <sol\Accelerator.h>
#include <sol\ToolBar.h>
#include <sol\StatusBar.h>
#include <sol\DefaultLayout.h>
//2008/09/16
#include <sol\Profile.h>

//2009/10/08
#include <sol/AppEntry.h>



/**
 * ApplicationView class.
 */
namespace SOL {

class ApplicationView :public Composite {
private:
    Application&     application;
    MenuBar*         menuBar;
    Accelerator*     accel;
    ToolBar*        toolBar;
    StatusBar*        statusBar;
    DefaultLayout*    defaultLayout;
    //2008/09/16
    Profile            profile;

protected:
//      virtual long size(Event& event);
    virtual long size(Event& event)
    {
        int    w, h;
        event.getSize(w, h);

        int    top = 0;
        if (toolBar) {
            toolBar -> send(event.getMessage(), 
                event.getWParam(), event.getLParam());
            top = toolBar -> getHeight();
        }

        if(statusBar) {
            statusBar -> send(event.getMessage(),
                event.getWParam(), event.getLParam());
            h = h - top - statusBar -> getHeight();
        }
        justifyScrollRange();
        layout(0, top, w, h);
        return 0L;
    }


public:
    ApplicationView(Application& applet, const TCHAR* name,
                Args& args)
    :Composite(NULL, name,
        args.set(XmNx,      (ulong)CW_USEDEFAULT)
            .set(XmNy,      (ulong)CW_USEDEFAULT)
            .set(XmNwidth,  (ulong)CW_USEDEFAULT)
            .set(XmNheight, (ulong)CW_USEDEFAULT)
            .set(XmNstyle,  (ulong)WS_OVERLAPPEDWINDOW)
          //  .set(XmNpredefined,  False)
            .set(XmNclassName, "ApplicationView")),
        application(applet),
        menuBar(null),
        accel(null),
        toolBar(null),
        statusBar(null),
        defaultLayout(null)
    {
        defaultLayout = new DefaultLayout();
        setLayout(defaultLayout);
        applet.setToplevel(this);

        ulong val;
        const TCHAR* menuName = name;
        if (args.get(XmNmenuName, &val)) {
            menuName = (const TCHAR*)val;
        }
        menuBar = new MenuBar(this, menuName);

        const TCHAR* accelName = name;
        if (args.get(XmNacceleratorName, &val)) {
            accelName = (const TCHAR*)val;
        }
        accel   = new Accelerator(accelName);

        Args ar;
        if (args.get(XmNdefaultToolBar)) {
            ulong id = args.get(XmNtoolBarBitmapID);
            ar.set(XmNtoolBarBitmapID,  id);
            toolBar = new ToolBar(this, _T(""), ar);
        }
        if(args.get(XmNdefaultStatusBar)) {
            ar.reset();
            statusBar = new StatusBar(this, _T(""), ar);    
        }

        //<added date="2009/09/08">
        const TCHAR* iconName = name;
        if (args.get(XmNiconName, &val)) {
            iconName = (const TCHAR*)val;
        }
        int iconId = 0;
        if (args.get(XmNiconId, &val)) {
            iconId = (int)val;
        }
        
        if (setIcon(iconName) == false) {
            setIcon(iconId);
        }
        //</added>

    }

public:
    ~ApplicationView() 
    { 
        // Don't delete the application
        delete defaultLayout;
        delete accel;    
        delete menuBar; 
        delete toolBar;
        delete statusBar;
    }
    
public:
    // For compatibility Oz++ class library on Motif.
    void    ApplicationView::realize()
    {
        int cmdShow = SW_SHOWNORMAL;
    
        cmdShow = application.getShowCommand();
        show(cmdShow);
    }

public:
    Boolean    translate(MSG* msg)
    {
        HACCEL haccel = NULL;
        if(accel) haccel = accel->get();
        if(haccel && ::TranslateAccelerator(getWindow(), haccel, msg)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

public:
    void getClientRect(RECT* r)
    {
        View::getClientRect(r);
        if(toolBar) {
            RECT rt;
            toolBar -> getClientRect(&rt);
            r->top = (rt.bottom - rt.top);            
        }
        if(statusBar) {
            RECT rs;
            statusBar -> getClientRect(&rs);
            r->bottom -= (rs.bottom - rs.top);            
        }
    }

public:
    int    showMessageDialog(const TCHAR* message, int flag = MB_OK)
    {
        if(isIconic()) {
            show(SW_NORMAL);
        }

        TCHAR path[_MAX_PATH];
        ::GetModuleFileName(NULL, path, _MAX_PATH);
        Folder folder(path);
        String progname = _T("");
        folder.getName(progname);
        int rc = ::MessageBox(getWindow(), message, (const TCHAR*)progname, flag);
        return rc;
    }
// 2009/10/19
public:
    int showMessageDialog(const wchar_t* title, const wchar_t* message, int flag = MB_OK)
    {
        if(isIconic()) {
            show(SW_NORMAL);
        }
        return ::MessageBoxW(getWindow(), message, title,  flag);
    }


public:
    int showErrorDialog(const wchar_t* title, const wchar_t* message, int flag = MB_OK)
    {
        if(isIconic()) {
            show(SW_NORMAL);
        }
        return ::MessageBoxW(getWindow(), message, title, MB_ICONERROR|flag);
    }

public:
    int showInformationDialog(const wchar_t* title, const wchar_t* message, int flag = MB_OK)
    {
        if(isIconic()) {
            show(SW_NORMAL);
        }
        return ::MessageBoxW(getWindow(), message, title, MB_ICONINFORMATION|flag);
    }

public:
    int showQuestionDialog(const wchar_t* title, const wchar_t* message, int flag = MB_OK)
    {
        if(isIconic()) {
            show(SW_NORMAL);
        }
        return ::MessageBoxW(getWindow(), message, title, MB_ICONQUESTION|flag);    
    }

// 2009/10/19
public:
    int showMessageDialog(const char* title, const char* message, int flag = MB_OK)
    {
        if(isIconic()) {
            show(SW_NORMAL);
        }
        return ::MessageBoxA(getWindow(), message, title,  flag);
    }


public:
    int showErrorDialog(const char* title, const char* message, int flag = MB_OK)
    {
        if(isIconic()) {
            show(SW_NORMAL);
        }
        return ::MessageBoxA(getWindow(), message, title, MB_ICONERROR|flag);
    }

public:
    int showInformationDialog(const char* title, const char* message, int flag = MB_OK)
    {
        if(isIconic()) {
            show(SW_NORMAL);
        }
        return ::MessageBoxA(getWindow(), message, title, MB_ICONINFORMATION|flag);
    }

public:
    int showQuestionDialog(const char* title, const char* message, int flag = MB_OK)
    {
        if(isIconic()) {
            show(SW_NORMAL);
        }
        return ::MessageBoxA(getWindow(), message, title, MB_ICONQUESTION|flag);    
    }


    //
public:
    // 1996.12.01 to-arai
    void wait(LONG interval) 
    {
        DWORD startedTime = ::GetTickCount();
        while(::GetTickCount() - startedTime < (DWORD)interval){
            MSG msg;
            if (::PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){
                ::TranslateMessage(&msg);
                ::DispatchMessage(&msg);
            }
        }
    }


public:
    void restorePlacement()
    {
        //Profile profile(name);
        WINDOWPLACEMENT pl;
        memset(&pl, 0, sizeof(pl));
        pl.length = sizeof(pl); 

        int x, y, w, h;
        profile.getGeometry(&x, &y, &w, &h);

        RECT r;
        r.left     = x;
        r.right    = x+ w;
        r.top      = y;
        r.bottom   = y+h;
        pl.showCmd = SW_SHOWNORMAL; 
        pl.rcNormalPosition = r;

        if(x > 0 && y > 0) {
            setPlacement(&pl);
        }
    }

public:
    void savePlacement()
    {
        //Profile profile(name);
        WINDOWPLACEMENT pl;

        //    memset(&pl, 0, sizeof(pl));
        pl.length = sizeof(pl); 

        getPlacement(&pl);
        RECT r = pl.rcNormalPosition;
        profile.setGeometry(&r);
    }

public:
    bool restoreFileFolder(TCHAR* folder, size_t size)
    {
        bool rc = false;
        if (folder != NULL && size >0) {
            rc = profile.getFileFolder(folder, size);
        }
        return rc;
    }

public:
    bool saveFileFolder(const TCHAR* filePath)
    {
        bool rc = false;
        if (filePath != NULL && strlen(filePath)>0) {
        
            Folder folder(filePath);
            String dirOnly = _T("");
        
            folder.getDirectory(dirOnly);
            const TCHAR* dir = (const TCHAR*)dirOnly;
            if (dir) {
                rc = profile.setFileFolder(dir);
            }
        }
        return rc;
    }


    void      exit(Action& action) {
        //2009/10/14
        savePlacement();
            post(WM_CLOSE, 0, 0L);
    }


    StatusBar* getStatusBar() { return statusBar; }

    StatusBar& getStatusBarRef() { return *statusBar; }

    ToolBar*   getToolBar() { return toolBar; }
    ToolBar&   getToolBarRef() { return *toolBar; }

    MenuBar*  getMenuBar()  { return menuBar; }

    BOOL    isIconic() { return ::IsIconic(getWindow()); }

    BOOL    isZoomed() { return ::IsZoomed(getWindow()); }

//    void    realize();

    void    setToolBar(ToolBar* bar)   { toolBar = bar; }
    void    setStatusBar(StatusBar* bar) { statusBar = bar; }


    Profile& getProfile() { return profile; }

public:
    //2009/11/16
    void  waitForThread(HANDLE hThread){
        HANDLE handle[1];
        handle[0] = hThread;

        bool looping = true;        
        while (looping)  { 
        
            if (MsgWaitForMultipleObjects(1, handle, FALSE,   
                INFINITE, QS_ALLINPUT|QS_ALLEVENTS) == WAIT_OBJECT_0+1 ) {
                MSG msg;

                while(PeekMessage (&msg,NULL,0,0,PM_REMOVE)) {
                    SleepEx(0, TRUE);
                    if (msg.message == WM_QUIT) {
                        //Repost WM_QUIT message to your application message queue.
                        PostMessage(NULL, WM_QUIT, 0, 0);
                        looping = false;
                        break;
                    }    
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }

            }else {
                break;   
            }
        }
    }

};

}



Last modified: 19 Dec 2009

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