/*
* GradientLabelGadget.h
* Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL++2000
// 2008/07/01
#pragma once
#include <sol\PaintDC.h>
#include <sol\Brush.h>
#include <sol\StockObject.h>
#include <sol\Pen.h>
#include <sol\ClientDC.h>
#include <sol\Font.h>
#include <sol\Gadget.h>
#include <sol\String.h>
namespace SOL {
class GradientLabelGadget :public Gadget {
private:
//char* text;
String text;
Font* font;
COLORREF textColor;
int margin;
private:
int baseColor;
int width;
int height;
public:
enum {GRADIENT_BASECOLOR_RED,
GRADIENT_BASECOLOR_GREEN,
GRADIENT_BASECOLOR_BLUE} GradientBaseColorType;
public:
GradientLabelGadget(): Gadget() {
//text = NULL;
font = NULL;
textColor=0;
margin =0;
baseColor =0;
width = 0;
height =0;
}
public:
GradientLabelGadget(View* parent, const TCHAR* label, Args& args)
:Gadget(parent, label, args)
{
GradientLabelGadget::create(parent, label, args);
}
public:
/**
*/
~GradientLabelGadget() {
//delete [] text;
}
public:
Boolean create(View* parent, const TCHAR* label, Args& args)
{
int margin = 4;
setMargin(margin);
int width = 0;
int height = 0;
font = NULL;
Boolean rc = Gadget::create(parent, label, args);
textColor = ::GetSysColor(COLOR_BTNTEXT);
const TCHAR* ptr = (const TCHAR*)args.get(XmNlabelString);
if(ptr == NULL) {
ptr = label;
}
if(ptr == NULL) {
ptr = _T("");
}
this->text = ptr;
ClientDC dc(parent);
if (args.get(XmNwidth) == 0 || args.get(XmNheight) == 0) {
HGDIOBJ hfont = NULL;
if(font) {
hfont = dc.select(font);
} else {
View* parent = getParent();
if (parent) {
Font* dfont = parent->getFont();
if (dfont) {
hfont = dc.select(dfont);
}
}
}
const TCHAR* ctext = (const TCHAR*)text;
RECT r;
dc.drawText(ctext, strlen(ctext), &r, DT_CALCRECT);
width = r.right - r.left;
height = r.bottom - r.top;
if (hfont) {
dc.select(hfont);
}
}
this->baseColor = GRADIENT_BASECOLOR_BLUE;
ulong color = 0;
if (args.get(XmNgradientBaseColor, &color)) {
this->baseColor = (int)color;
}
if (!(this->baseColor ==GRADIENT_BASECOLOR_RED ||
this->baseColor ==GRADIENT_BASECOLOR_GREEN ||
this->baseColor ==GRADIENT_BASECOLOR_BLUE)) {
this->baseColor = GRADIENT_BASECOLOR_BLUE;
}
if (width<255) {
width = 255;
}
setSize(width+margin*2, height+margin*2);
return rc;
}
public:
void draw(DC* dc)
{
draw(dc, 0, 0);
}
public:
void draw(DC* cdc, int x, int y)
{
cdc->setBkMode(TRANSPARENT);
StockObject pen(NULL_PEN);
cdc->select(&pen);
//hfont = a previous original font-handle
HGDIOBJ hfont = NULL;
if(font) {
hfont = cdc->select(font);
} else {
View* parent = getParent();
if (parent) {
Font* dfont = parent->getFont();
if (dfont) {
hfont = cdc->select(dfont);
}
}
}
int width, height;
getSize(width, height);
cdc -> setTextColor(textColor);
const TCHAR* ctext = (const TCHAR*)text;
RECT r;
cdc->drawText(ctext, strlen(ctext), &r, DT_CALCRECT);
int w = r.right - r.left;
int h = r.bottom - r.top;
if (w >width) {
width = w;
}
int p = width/255+1;
for(int i = 0; i < 255; i++) {
Brush* brush = NULL;
if (baseColor==GRADIENT_BASECOLOR_RED) {
brush = new Brush(RGB(255, 128+i/2,128+i/2) );
}
if (baseColor==GRADIENT_BASECOLOR_GREEN) {
brush = new Brush(RGB(128+i/2, 255, 128+i/2) );
}
if (baseColor==GRADIENT_BASECOLOR_BLUE) {
brush = new Brush(RGB(128+i/2, 128+i/2, 255) );
}
HGDIOBJ prev = cdc->select(brush);
cdc->rectangle(x+i*p, y, x+20+i*p, y+ h+ margin*2);
cdc->select(prev);
delete brush;
}
r.left = x + margin;
r.right = r.left + width+margin*2;
r.top = y + margin;
r.bottom = r.top + h +margin*2;
cdc->drawText(ctext, strlen(ctext), &r, 0);
if(hfont) {
//back to original hfont
cdc->select(hfont);
}
}
void setHighLightTextColor() {
textColor = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
}
void setMargin(int val) {
margin = val;
}
void setFont(Font* f) {
font = f;
}
void setFont(Font& f) {
font = &f;
}
};
}
|