SOL9 2.0 Class: Label

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

Source code

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


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

#include <sol\Primitive.h>

#include <sol\Resource.h>
#include <sol\BitmapFile.h>
#include <sol\Label.h>
#include <sol\Bitmap.h>
#include <sol\Icon.h>
#include <sol\PaintDC.h>
#include <sol\ClientDC.h>
#include <shellapi.h>


namespace SOL {

class Label :public Primitive {
private:
    Resource* resource;
    int        labelType;
    int        width;
    int        height;
    BOOL    recomputeSize;

protected:
    long size(Event& event)
    {
        int x, y, w, h;

        getGeometry(x, y, w, h);
        if(recomputeSize == TRUE) 
            reshape(x, y, width, height);
        return NULL;
    }

protected:
    long Label::paint(Event& event)
    {
        int x, y;
        int    w, h;

        PaintDC pdc(this);
        getGeometry(x, y, w, h);
        RECT rect;
        getWindowRect(&rect);
        HDC hdc = pdc.get();

        if(labelType == LABEL_STRING) {
            TCHAR    text[256];
            getText(text, 256);
            pdc.setBkMode(TRANSPARENT);
            Size size;

            //2008/06/26
            HFONT hfont = NULL;
            View* parent = getParent();
            Font* font = parent->getFont();

            if (font) {
                hfont = (HFONT)pdc.select(font);
            }
            DWORD ext = pdc.getTextExtent(text, strlen(text), &size);
            int hh = size.cy;    
            int ww = size.cx;    
            pdc.textOut(2, (h-hh)/2, text, strlen(text));

            pdc.select(hfont);

        }
        if(resource != NULL &&
            (labelType == LABEL_BITMAP || 
            labelType == LABEL_ICON) ) {
            resource -> draw(hdc, 0, 0);
        }
        return NULL;
    }

public: 
    enum {LABEL_STRING, LABEL_BITMAP, LABEL_ICON} LabelType;

public:
    Label():Primitive() { }


    ~Label() {
        if(resource) delete resource;
    }

public:
    Label(View* parent, const TCHAR* name, Args& args)
    :Primitive(parent, name,
        args.set(XmNstyle, (ulong)WS_GROUP)
            .set(XmNbackground, (COLOR_BTNFACE+1))
            .set(XmNclassName, "Label")
            .set(XmNuseDefaultFont, "true")
            .set(XmNclassStyle, (ulong)CS_VREDRAW|CS_HREDRAW))
    {
        addEventHandler(WM_PAINT, this,
            (Handler)&Label::paint, NULL);
        addEventHandler(WM_SIZE, this,
            (Handler)&Label::size, NULL);
        resource      = NULL;
        recomputeSize = TRUE;
        setValues(args);
    }

public:
    virtual Boolean create(View* parent, const TCHAR* name, Args& args)
    {
        Boolean rc = Primitive::create(parent, name,
            args.set(XmNstyle, (ulong)WS_GROUP)
            .set(XmNbackground, (COLOR_BTNFACE+1))
            .set(XmNclassName, "Label")
            .set(XmNuseDefaultFont, "true")
            .set(XmNclassStyle, (ulong)CS_VREDRAW|CS_HREDRAW));

        addEventHandler(WM_PAINT, this,
            (Handler)&Label::paint, NULL);
        addEventHandler(WM_SIZE, this,
            (Handler)&Label::size, NULL);
        resource      = NULL;
        recomputeSize = TRUE;
        setValues(args);

        return rc;
    }

public:
    void getValues(Args& args)
    {
        Primitive::getValues(args);

        int num   = args.getCount();
        Arg* arg  = args.getArgList();

        for (int j = 0; j<num; j++) {
            const TCHAR*   name  = arg[j].name;
            ulong* val  = (ulong*)arg[j].value;
            if(name == XmNrecomputeSize) {
                *val = recomputeSize;
                continue;
            }
            if(name == XmNlabelType) {
                *val = labelType;
                continue;
            }
        }
    }

public:
    void setValues(Args& args)
    {
        Primitive::setValues(args);

        TCHAR  label[256];
        getText(label, 256);
        labelType = (int) args.get(XmNlabelType);
        HICON   hicon   = (HICON)  args.get(XmNicon);
        HBITMAP hbitmap = (HBITMAP)args.get(XmNbitmap);

        if (labelType == LABEL_STRING) {
            ClientDC cdc(this);
            HFONT hfont = NULL;
            View* parent = getParent();
            if (parent) {
                Font* dfont = parent->getFont();
                if (dfont) {
                    hfont = (HFONT)cdc.select(dfont);
                }
            }
            Size size;
            cdc.getTextExtent(label, strlen(label), &size);
            if (hfont) {
                cdc.select(hfont);
            }
            height = size.cy + 4;
            width  = size.cx + 4;
        }
        if (hicon) {
            labelType = LABEL_ICON;
            if(resource) delete resource;
            resource  = new Icon(_T(""), hicon);
            resource -> getSize(&width, &height);
        }
        if (hbitmap) {
            labelType = LABEL_BITMAP;
            if(resource) delete resource;
            resource  = new Bitmap(label, hbitmap);
            resource -> getSize(&width, &height);
        }
        if (hicon == NULL && hbitmap == NULL) {
            setLabel(label);
        }
        width++;
        height++;

        ulong val;
        if (args.get(XmNrecomputeSize, &val))
            recomputeSize = (BOOL)val;
        if (recomputeSize == TRUE) 
            send(WM_SIZE, 0, 0L);

        update(NULL);
    }



public:
    void Label::setLabel(const TCHAR* label)
    {
        ClientDC cdc(this);

        labelType = LABEL_STRING;

        ulong val;
        get(XmNinstance, &val);

        TCHAR* dot = strrchr((TCHAR*)label, '.');
        TCHAR ext[80];
        ext[0] = NULL;
        if(dot) {
            strcpy_s(ext, sizeof(ext), dot);
        }
        TCHAR* name = (TCHAR*)strrchr((TCHAR*)label, (TCHAR)'\\');
        if(name) 
            name++;
        else
            name = (TCHAR*)label;

        if(strcmp(ext,_T(".ico")) == 0) {
            HICON hicon = ::ExtractIcon((HINSTANCE)val, label, 0);
            if(hicon) {
                if(resource) delete resource;
                resource = new Icon(_T(""), hicon);
                labelType = LABEL_ICON;
                resource -> getSize(&width, &height);
            }
        }
        if(strcmp(ext, _T(".bmp")) == 0) {
            BitmapFile bmpFile;
            if(bmpFile.openReadOnly(label) >= 0) {
                HBITMAP hbmp = bmpFile.extract(cdc.get());
                if(hbmp) {
                    if(resource) delete resource;
                    resource = new Bitmap(name, hbmp);
                    labelType = LABEL_BITMAP;
                    resource -> getSize(&width, &height);
                }
                bmpFile.close();
            }
        }
        if(labelType == LABEL_STRING) {
            Size size;
            cdc.getTextExtent(label, strlen(label), &size);
            height = size.cy + 4;
            width  = size.cx + 4;
        }
    }


    void    getSize(int* width1, int* height1) {
            *width1  = width;
            *height1 = height;
    }
};

}



Last modified: 1 Feb 2012

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