GLICT/textbox.cpp

00001 /*
00002         GLICT - Graphics Library Interface Creation Toolkit
00003         Copyright (C) 2006-2007 OBJECT Networks
00004 
00005         This library is free software; you can redistribute it and/or
00006         modify it under the terms of the GNU Library General Public
00007         License as published by the Free Software Foundation; either
00008         version 2 of the License, or (at your option) any later version.
00009 
00010         This library is distributed in the hope that it will be useful,
00011         but WITHOUT ANY WARRANTY; without even the implied warranty of
00012         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013         Library General Public License for more details.
00014 
00015         You should have received a copy of the GNU Library General Public
00016         License along with this library; if not, write to the Free
00017         Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021 #include "textbox.h"
00022 #include "globals.h"
00023 #include "fonts.h"
00024 glictTextbox::glictTextbox() {
00025         this->bgcolor.r = .7;
00026         this->bgcolor.g = .7;
00027         this->bgcolor.b = .7;
00028         this->bgcolor.a = 1.0;
00029         strcpy(this->objtype, "Textbox");
00030 
00031         this->parent = NULL;
00032         //printf("Textbox generated.\n");
00033 
00034         this->focusable = true;
00035 
00036         this->SetHeight(14);
00037         this->SetWidth(100);
00038 
00039         this->passprotectchar = 0;
00040         this->allowedchr = "";
00041 
00042         caption = "";
00043 }
00044 glictTextbox::~glictTextbox() {
00045 
00046 }
00047 void glictTextbox::Paint() {
00048         if (!GetVisible()) return;
00049 
00050 
00051     if (!glictGlobals.textboxSkin) {
00052                 glictGlobals.PaintRect(this->x+glictGlobals.translation.x, this->x+this->width+glictGlobals.translation.x,
00053                                         this->y+glictGlobals.translation.y, this->y+this->height+glictGlobals.translation.y, bgcolor);
00054 
00055 
00056 /*        glBegin(GL_QUADS);
00057         glVertex2f(this->x+glictGlobals.translation.x,this->y+glictGlobals.translation.y);
00058         glVertex2f(this->x+glictGlobals.translation.x,this->y+this->height+glictGlobals.translation.y);
00059         glVertex2f(this->x+this->width+glictGlobals.translation.x,this->y+this->height+glictGlobals.translation.y);
00060         glVertex2f(this->x+this->width+glictGlobals.translation.x,this->y+glictGlobals.translation.y);
00061         glEnd();*/
00062     } else {
00063         glictSize s = {this->width, this->height};
00064 
00065         glictGlobals.Translatef(this->x, this->y, 0);
00066         glictGlobals.textboxSkin->Paint(&s);
00067         glictGlobals.Translatef(-this->x, -this->y, 0);
00068 
00069 
00070     }
00071 
00072 
00073         std::string oldcaption;
00074         char* asterisked=NULL;
00075         if (passprotectchar) {
00076                 asterisked = (char*)malloc(caption.size() + 1);
00077                 for (int i = 0; i < caption.size() ; i++) {
00078                         asterisked[i] = passprotectchar;
00079                 }
00080                 asterisked[caption.size()] = 0;
00081         }
00082         if (glictGlobals.topFocused==this) {
00083                 oldcaption = caption;
00084                 if (asterisked)
00085                         this->SetCaption((std::string)(asterisked) + "_");
00086                 else
00087                         this->SetCaption(caption + "_");
00088         }
00089 
00090 
00091 
00092 
00093         glictGlobals.SetColor(glictGlobals.textboxTextColor.r, glictGlobals.textboxTextColor.g, glictGlobals.textboxTextColor.b, glictGlobals.textboxTextColor.a);
00094         if (asterisked && glictGlobals.topFocused != this)
00095                 glictFontRender(asterisked, fontname.c_str(), fontsize, x+(glictGlobals.textboxSkin ? glictGlobals.textboxSkin->GetLeftSize()->w : 0) +glictGlobals.translation.x , y+(glictGlobals.textboxSkin ? glictGlobals.textboxSkin->GetTopSize()->h : 0) +glictGlobals.translation.y);
00096         else
00097                 glictFontRender(this->caption.c_str(), fontname.c_str(), fontsize, x+(glictGlobals.textboxSkin ? glictGlobals.textboxSkin->GetLeftSize()->w : 0) +glictGlobals.translation.x, y+(glictGlobals.textboxSkin ? glictGlobals.textboxSkin->GetTopSize()->h : 0) + glictGlobals.translation.y);
00098     glictGlobals.SetColor(1., 1., 1., 1.);
00099 
00100 
00101         if (glictGlobals.topFocused==this) {
00102                 caption = oldcaption;
00103         }
00104         if (asterisked) free(asterisked);
00105         this->CPaint();
00106 
00107 }
00108 void glictTextbox::SetBGColor(float r, float g, float b, float a) {
00109         this->bgcolor.r = r;
00110         this->bgcolor.g = g;
00111         this->bgcolor.b = b;
00112         this->bgcolor.a = a;
00113 }
00118 // Copypastable to other widgets.
00119 bool glictTextbox::CastEvent(glictEvents evt, void* wparam, long lparam, void* returnvalue) {
00120         if (!GetVisible() || !GetEnabled()) return false;
00121         switch (evt) {
00122                 case GLICT_MOUSEUP:
00123                 case GLICT_MOUSEDOWN:
00124                 case GLICT_MOUSECLICK:
00125                         if (((glictPos*)wparam)->x > this->clipleft &&
00126                                 ((glictPos*)wparam)->x < this->clipright &&
00127                                 ((glictPos*)wparam)->y > this->cliptop &&
00128                                 ((glictPos*)wparam)->y < this->clipbottom) {
00129 
00130 
00131                                 //if (evt==GLICT_MOUSECLICK) MessageBox(0,caption.c_str(),NULL,0);
00132 
00133                                 // if a child caught click, we dont handle it otherwise
00134                                 return DefaultCastEvent(evt, wparam, lparam, returnvalue);
00135                                 // otherwise we could handle it mroe but we'll simply tell we didnt proces it
00136 
00137                         }
00138                         //It occured outside the textbox, ignored.
00139                         break;
00140                 case GLICT_KEYPRESS:
00141                         switch (*((char*)wparam)) {
00142                                 default:
00143                                         if (allowedchr.size() == 0 || (allowedchr.find(*((char*)wparam))<allowedchr.size())) { // FIXME (ivucica#1#) one of the conditions concerning if character is contained in allowedchr is redundant, its needed to see what exactly happens if char is not in there and optimize
00144                                                 this->SetCaption(caption + *((char*)wparam));
00145                                         }
00146                                         break;
00147                                 case 8:
00148                                         if (caption.size()) {
00149                                                 char* Title = (char*)malloc(caption.size()+1);
00150                                                 strcpy(Title, caption.c_str());
00151                                                 Title[strlen(Title)-1]=0;
00152                                                 caption = Title;
00153                                                 free(Title);
00154 
00155                                         }
00156                                         break;
00157                                 case 9:
00158                                         if (next) 
00159                                                 next->Focus(NULL);
00160                                         break;
00161                         }
00162                         break;
00163         }
00164 
00165         return false;
00166 }
00175 void glictTextbox::SetPassProtectCharacter(char asterisk) {
00176         passprotectchar = asterisk;
00177 }
00178 
00188 void glictTextbox::SetAllowedChars (std::string s) {
00189         allowedchr = s;
00190 }

SourceForge.net Logo
generated with doxygen 1.5.3 on Mon Oct 29 18:09:26 2007