DarkForm Version 1.2.0 (Revision Date: Feb 20, 2010)
Fixed many bugs.
Added Textarea (which is for output only).
/////////////////////////////////////////////////////
// DarkForm.h Version 1.2.0 //
// by Paul Chin (Feb 18, 2010) //
// Last Updated: Feb 20, 2010 //
// * Free to modify and use for any purpose * //
/////////////////////////////////////////////////////
#include "DarkGDK.h"
//--------- FORM ----------
class Form{
private:
int color;
char title[128];
public:
Form(int _color,char* _title);
};
Form::Form(int _color,char* _title){
dbSyncOn();
dbSyncRate(60);
color = _color;
memset(title,0,128);
strcpy(title,_title);
dbColorBackdrop (dbRGB(color,color,color));
dbBackdropOn();
dbSetWindowTitle(title);
}
//--------- LABEL ----------
class Label{
private:
int x,y;
char text[128];
public:
Label(int _x, int _y, char* _text);
void update();
};
Label::Label(int _x, int _y, char* _text){
x = _x;
y = _y;
memset(text,0,128);
strcpy(text, _text);
dbInk(0,0);
dbSetTextSize(12);
}
void Label::update(){
dbInk(0,0);
dbSetTextSize(12);
dbText(x,y,text);
}
//--------- TEXTBOX ----------
class Textbox {
private:
int xLeft,yTop,xRight,yBottom,length;
bool selected;
char buffer[256];
bool isBackspaceKeyDown;
public:
Textbox(int _xLeft,int _yTop, int _length);
void update();
char* getText();
void setText(char* text);
void clear();
};
Textbox::Textbox(int _xLeft,int _yTop, int _length){
xLeft = _xLeft;
yTop = _yTop;
length = _length;
xRight = xLeft + length*9;
yBottom = _yTop + 20;
selected=isBackspaceKeyDown=false;
memset(buffer,0,256);
}
void Textbox::update(){
static bool isBackspaceKeyDown=false;
static int counter=0; //backspace press & hold count
int mc = dbMouseClick();
int mx = dbMouseX();
int my = dbMouseY();
dbInk(dbRGB(255,255,255),dbRGB(255,255,255));
dbBox(xLeft,yTop,xRight,yBottom);
if (mc == 1){
if ((mx>xLeft+5 && mx<xRight-5) && (my>yTop && my<yBottom)) selected = true;
else{
dbClearEntryBuffer( );
selected = false;
}
}
if(selected==true){ //selected look
dbInk(0,0);
dbLine(xLeft,yBottom,xLeft,yTop); //left black edge
dbLine(xLeft,yTop,xRight,yTop); //top black edge
dbLine(xRight,yTop,xRight,yBottom); //right black edge
dbLine(xRight,yBottom,xLeft,yBottom); //bottom black edge
}else{
dbInk(dbRGB(128,128,128),dbRGB(128,128,128));
dbLine(xLeft,yBottom,xLeft,yTop); //left gray edge
dbLine(xLeft,yTop,xRight,yTop); //left gray edge
dbLine(xRight,yTop,xRight,yBottom); //left gray edge
dbLine(xRight,yBottom,xLeft,yBottom); //left gray edge
dbInk(dbRGB(255,255,255),dbRGB(255,255,255));
dbLine(xLeft-1,yBottom+1,xLeft-1,yTop-1); //left white edge
dbLine(xLeft-1,yTop-1,xRight+1,yTop-1); //left white edge
dbLine(xRight+1,yTop-1,xRight+1,yBottom+1); //left white edge
dbLine(xRight+1,yBottom+1,xLeft-1,yBottom+1); //left white edge
}
if (selected==true && dbGetEntry()>0 && strlen(buffer)<length && dbScanCode()!=14){
strcat(buffer, dbGetEntry() );
}
if(selected==true && strlen(buffer)>0 && dbScanCode()==14){//backspace
counter++;
if(isBackspaceKeyDown==false) {
char *str = dbLeft(buffer,dbLen(buffer)-1);
memset(buffer,0,128);
strcpy(buffer,str);
isBackspaceKeyDown = true;
counter=0;
}
if(counter>20){ //backspace press and hold
memset(buffer,0,128);
counter=0;
dbClearEntryBuffer( );
}
}
if(dbKeyState(14)==0) {
isBackspaceKeyDown = false;
counter=0;
}
dbInk(0,0);
dbSetTextSize(12);
dbText(xLeft+2,yTop+2,buffer);
if(selected==true)dbClearEntryBuffer ( );
}
char* Textbox::getText(){
return buffer;
}
void Textbox::setText(char* text){
strcpy(buffer,text);
}
void Textbox::clear(){
memset(buffer,0,256);
}
// -------- BUTTON ----------
class Button{
private:
int xLeft,yTop,xRight,yBottom;
bool selected;
char caption[128];
int isClicked;
int width;
public:
Button(int _xLeft,int _yTop, char* _caption);
void update();
int getClickState();
};
Button::Button(int _xLeft,int _yTop, char* _caption){
xLeft = _xLeft;
yTop = _yTop;
width = strlen(_caption)*20;;
xRight = xLeft + width;
yBottom = yTop + 20;
isClicked=0;
memset(caption,0,128);
strcpy(caption,_caption);
}
void Button::update(){
int mc = dbMouseClick();
int mx = dbMouseX();
int my = dbMouseY();
dbInk(dbRGB(190,190,190),dbRGB(190,190,190));
dbBox(xLeft,yTop,xRight,yBottom);
dbInk(dbRGB(255,255,255),dbRGB(255,255,255));
dbLine(xLeft-1,yBottom+1,xLeft-1,yTop-1); //left white edge
dbLine(xLeft-1,yTop-1,xRight+1,yTop-1); //top white edge
dbInk(0,0);
dbLine(xRight+1,yTop-1,xRight+1,yBottom+1); //right black edge
dbLine(xRight+1,yBottom+1,xLeft-1,yBottom+1); //bottom black edge
dbInk(0,0);
dbCenterText(xLeft+width/2,yTop+2,caption);
if (mc == 1){
if ((mx > xLeft+5 && mx < xRight-5) && (my > yTop && my < yBottom)){
dbInk(dbRGB(160,160,160),dbRGB(160,160,160));
dbBox(xLeft+1,yTop+1,xRight-1,yBottom-1);
dbInk(0,0);
dbCenterText(xLeft+width/2,yTop+2,caption);
isClicked = 1;
}
else isClicked = 0;
}
}
int Button::getClickState(){
if(isClicked==1 && dbMouseClick()==0){
isClicked=0;
return 1;
}else return 0;
}
//--------- STATUSBAR ----------
class Statusbar{
private:
int xLeft,yTop,xRight,yBottom;
char text[256];
int width;
public:
Statusbar();
void update();
void setText(char* text);
};
Statusbar::Statusbar(){
xLeft=0;
yTop=dbScreenHeight()-30;
xRight=dbScreenWidth();
yBottom=dbScreenHeight();
memset(text,0,256);
}
void Statusbar::update(){
dbInk(dbRGB(128,128,128),dbRGB(128,128,128));
dbLine(xLeft,yTop-2,xRight,yTop-2);
dbInk(dbRGB(190,190,190),dbRGB(190,190,190));
dbBox(xLeft,yTop,xRight,yBottom);
dbInk(0,0);
dbSetTextSize(12);
dbText(xLeft+10,yTop+5,text);
}
void Statusbar::setText(char* _text){
memset(text,0,256);
strcpy(text, _text);
}
//--------- TEXTAREA ----------
class Textarea{
private:
int xLeft,yTop,xRight,yBottom;
char buffer[100][300];
int width,height;
int line;
int yMinline, yMaxline;
int xMincolumn, xMaxcolumn;
public:
Textarea(int _xLeft,int _yTop,int _width,int _height);
void update();
void appendText(char* text);
void clear();
};
Textarea::Textarea(int _xLeft,int _yTop,int _width,int _height){
xLeft = _xLeft;
yTop = _yTop;
width = _width;
height = _height;
xRight = xLeft + width*9;
yBottom = yTop + height*13 + 10;
memset(buffer,0,2048);
yMinline = yTop + 2;
yMaxline = yBottom - 5;
xMincolumn = xLeft + 5;
xMaxcolumn = xRight - 5;
line = 0;
}
void Textarea::update(){
dbInk(dbRGB(240,240,240),dbRGB(240,240,240));
dbBox(xLeft,yTop,xRight,yBottom);
dbInk(0,0);
dbSetTextSize(12);
for(int i=0; i<height; i++){
if(strlen(buffer[i])>width){
char* str = dbLeft(buffer[i],width);
dbText(xMincolumn,yMinline + i*13, str);
}else{
dbText(xMincolumn,yMinline + i*13, buffer[i]);
}
}
dbInk(dbRGB(128,128,128),dbRGB(128,128,128));
dbLine(xLeft,yBottom,xLeft,yTop); //left black edge
dbLine(xLeft,yTop,xRight,yTop); //top black edge
dbLine(xRight,yTop,xRight,yBottom); //right black edge
dbLine(xRight,yBottom,xLeft,yBottom); //bottom black edge
dbInk(dbRGB(255,255,255),dbRGB(255,255,255));
dbLine(xLeft-1,yBottom+1,xLeft-1,yTop-1); //left black edge
dbLine(xLeft-1,yTop-1,xRight+1,yTop-1); //top black edge
dbLine(xRight+1,yTop-1,xRight+1,yBottom+1); //right black edge
dbLine(xRight+1,yBottom+1,xLeft-1,yBottom+1); //bottom black edge
}
void Textarea::appendText(char* text){
if(line < height&& strlen(text)>0){
strcat(buffer[line],text);
line++;
}else if(line >= height && strlen(text)>0) {
line=0;
memset(buffer,0,sizeof(buffer));
strcat(buffer[line],text);
line++;
}
}
void Textarea::clear(){
memset(buffer,0,sizeof(buffer));
line=0;
}
Another example usage is shown below:
//////////////////////////////////////
// DarkForm Demo: Simple Textarea //
// by Paul Chin //
// Feb 20, 2010 //
//////////////////////////////////////
#include "DarkGDK.h"
#include "DarkForm.h"
void DarkGDK ( void )
{
Form form(220,"Simple Textarea: Demo of DarkForm Version 1.2.0");
Label lblInstruction(8,15,"Enter some text and click add");
Textbox txtboxInput(12,40,65);
Button btnAdd(20,400,"Add");
Button btnClearAll(100,400,"Clear All");
Button btnExit(450,400,"Exit");
Statusbar statusbar;
Textarea txtarea(12,80,65,20);
while ( LoopGDK ( ) ){
lblInstruction.update();
txtboxInput.update();
btnAdd.update();
btnClearAll.update();
btnExit.update();
statusbar.update();
txtarea.update();
if(btnAdd.getClickState()==1){
txtarea.appendText(txtboxInput.getText());
statusbar.setText(txtboxInput.getText());
}
if(btnClearAll.getClickState()==1){
txtboxInput.clear();
txtarea.clear();
statusbar.setText("All Cleared");
}
if(btnExit.getClickState()==1){
int response = MessageBox(0,"Click OK to Exit","Confirm",MB_OKCANCEL);
if(response==IDOK) exit(0);
}
dbSync ( );
}
return;
}
Screen shot:
[img]
[/img]