Main Page   Packages   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals  

TextBox.cpp

Go to the documentation of this file.
00001 /*
00002  * $Id: TextBox.cpp,v 1.9 2002/08/08 10:00:26 virtualcasino Exp $
00003  */
00004 #include<TextBox.h>
00005 #include<Font.h>
00006 #include<SDL.h>
00007 #include<Resource.h>
00008 #include<string.h>
00009 #include<SDL_gfxPrimitives.h>
00010 
00011 extern SDL_Event globalEvent;
00012 #ifdef DO_BLINK
00013 static int doblink;
00014 #endif
00015 
00016 namespace clawsoft{
00017 
00018         TextBox::TextBox(){
00019                 setClassName("TextBox");
00020                 release();
00021                 border = 8;
00022         }
00023 
00024         TextBox::TextBox(Rect &r):Widget(r){
00025                 setClassName("TextBox");
00026                 release();
00027                 border = 8;
00028         }
00029 
00030         void TextBox::grab(){
00031                 grabbed = true;
00032         }
00033 
00034         void TextBox::release(){
00035                 grabbed = false;
00036         }
00037 
00038         void TextBox::draw(){
00039                 SDL_Surface *surf;
00040                 SDL_Rect src, dest;
00041                 int temp;
00042                 dest.x = 0;
00043                 dest.y = 0;
00044                 dest.w = W();
00045                 dest.h = H();
00046                 //SDL_FillRect(Screen, &dest, 0);
00047                 drawBorder();
00048                 if(text.size() <= 0 || text.toCharPtr() == 0){
00049                         if((grabbed || mouse.inFocus(*this))){
00050 #ifdef DO_BLINK
00051                                 if((doblink++)%10 == 0){
00052 #endif
00053                                         surf = SystemFont[MINI_FONT].renderText("_");
00054 #ifdef DO_BLINK
00055                                 }
00056                                 else
00057                                         surf = SystemFont[MINI_FONT].renderText(" ");
00058 #endif
00059                         }
00060                         else
00061                                 surf = SystemFont[MINI_FONT].renderText(" ");
00062                 }
00063                 else{
00064                         char *tmp;
00065                         tmp = new char[text.size() + 2];
00066                         strcpy(tmp, text.toCharPtr());
00067                         if(grabbed || mouse.inFocus(*this)){
00068 #ifdef DO_BLINK
00069                                 if((doblink++)%10 == 0){
00070 #endif
00071                                         tmp[text.size()] = '_';
00072                                         tmp[text.size() + 1] = '\0';
00073 #ifdef DO_BLINK
00074                                 }
00075 #endif
00076                         }
00077                         surf = SystemFont[MINI_FONT].renderText(tmp);
00078                         delete[] tmp;
00079                 }
00080                 src.x = 0;
00081                 src.y = 0;
00082                 temp = border * 2;
00083                 if(surf->w > (W() - temp)){
00084                         src.w = W() - temp;
00085                         src.x = surf->w - src.w;
00086                 }
00087                 else{
00088                         src.w = surf->w;
00089                 }
00090                 if(surf->h > (H() - temp)){
00091                         src.h = H() - temp;
00092                 }
00093                 else{
00094                         src.h = surf->h;
00095                 }
00096                 dest.x = border + X();
00097                 dest.y = border + Y();
00098                 SDL_BlitSurface(surf, &src, Screen, &dest);
00099                 SDL_FreeSurface(surf);
00100         }
00101 
00102         void TextBox::drawBorder(){
00103                 int temp;
00104                 if(grabbed || mouse.inFocus(*this)){
00105                         temp = 100;
00106                 }
00107                 else
00108                         temp = 0;
00109                 rectangleRGBA(Screen, X(), Y(), getRight(), getBottom(), temp, 255, 0, 255);
00110         }
00111 
00112         bool TextBox::onReturn(){
00113                 return false;
00114         }
00115 
00116         bool TextBox::checkKeyEvents(){
00117                 int key;
00118                 char cc[2];
00119                 cc[1] = 0;
00120                 if(grabbed || mouse.inFocus(*this)){
00121                         key = globalEvent.key.keysym.sym;
00122                         if(key >= SDLK_SPACE && key <= SDLK_z){
00123                                 cc[0] = (const char)globalEvent.key.keysym.sym;
00124                                 text += cc;
00125                                 return true;
00126                         }
00127                         else if(key == SDLK_BACKSPACE){
00128                                 if(text.size() > 0)
00129                                         text = text.deleteLast();
00130                                 return true;
00131                         }
00132                         else if(key == SDLK_RETURN){
00133                                 if(onReturn())
00134                                         return true;
00135                                 if(text.size() > 1)
00136                                         text += "\n";
00137                                 return true;
00138                         }
00139                 }
00140                 return false;
00141         }
00142 
00143         bool TextBox::checkMouseEvents(){
00144                 if(mouse.inFocus(*this) && mouse.leftPressed()){
00145                         grab();
00146                         return true;
00147                 }
00148                 return false;
00149         }
00150 
00151         String &TextBox::getText(){
00152                 return text;
00153         }
00154 }
00155 

Powered by:

SourceForge Logo