Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

pgmaskedit.cpp

Go to the documentation of this file.
00001 /*
00002     ParaGUI - crossplatform widgetset
00003     Copyright (C) 2000,2001,2002  Alexander Pipelka
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     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  
00019     Alexander Pipelka
00020     pipelka@teleweb.at
00021  
00022     Last Update:      $Author: mbickel $
00023     Update Date:      $Date: 2007-04-13 16:16:03 $
00024     Source File:      $Source: /home/cvspsrv/cvsroot/games/asc/source/libs/paragui/src/widgets/pgmaskedit.cpp,v $
00025     CVS/RCS Revision: $Revision: 1.2 $
00026     Status:           $State: Exp $
00027 */
00028 
00029 
00030 #include "pgmaskedit.h"
00031 #include "pgstring.h"
00032 
00033 PG_MaskEdit::PG_MaskEdit(PG_Widget* parent, const PG_Rect& r, const std::string& style) : PG_LineEdit(parent, r, style) {
00034         my_spacer = '_';
00035 }
00036 
00037 void PG_MaskEdit::SetText(const std::string& new_text) {
00038         PG_LineEdit::SetText(my_displaymask);
00039 
00040         // set the displaymask if the string is empty
00041         if(new_text.empty()) {
00042                 return;
00043         }
00044 
00045         SetCursorPos(0);
00046 
00047         // merge the text with the displaymask
00048         for(Uint32 i=0; i<new_text.length(); i++) {
00049                 InsertChar(new_text[i]);
00050         }
00051 }
00052 
00053 void PG_MaskEdit::SetMask(const std::string& mask) {
00054         my_mask = mask;
00055         my_displaymask = mask;
00056 
00057         for(Uint32 i=0; i<my_displaymask.size(); i++) {
00058                 if(my_displaymask[i] == '#') {
00059                         my_displaymask[i] = my_spacer;
00060                 }
00061         }
00062 
00063         PG_LineEdit::SetText(my_displaymask);
00064 }
00065 
00066 const PG_String& PG_MaskEdit::GetMask() {
00067         return my_mask;
00068 }
00069 
00070 void PG_MaskEdit::SetSpacer(const PG_Char& c) {
00071         my_spacer = c;
00072 }
00073 
00074 PG_Char PG_MaskEdit::GetSpacer() {
00075         return my_spacer;
00076 }
00077 
00078 void PG_MaskEdit::InsertChar(const PG_Char& c) {
00079         /*if (!c)
00080                 return;*/
00081 
00082         // check if we are on a valid position
00083         while(((Uint32)my_cursorPosition < my_mask.length()) && (my_mask[my_cursorPosition] != '#')) {
00084                 my_cursorPosition++;
00085         }
00086 
00087         if((Uint32)my_cursorPosition == my_mask.length()) {
00088                 return;
00089         }
00090 
00091         my_text[my_cursorPosition++] = c;
00092 
00093         while((my_cursorPosition < (int)my_mask.size()) &&
00094                 (my_mask[my_cursorPosition] != '#')) {
00095                 my_cursorPosition++;
00096         }
00097 
00098         SetCursorPos(my_cursorPosition);
00099 
00100 }
00101 
00102 void PG_MaskEdit::DeleteChar(Uint16 pos) {
00103         if(my_mask[pos] == '#') {
00104                 my_text[pos] = my_spacer;
00105         }
00106 }
00107 
00108 bool PG_MaskEdit::eventMouseButtonDown(const SDL_MouseButtonEvent* button) {
00109 
00110         // let the parent first process the event
00111         if(!PG_LineEdit::eventMouseButtonDown(button)) {
00112                 return false;
00113         }
00114 
00115         // check the cursorposition and find a valid position
00116         while((my_text[my_cursorPosition] == my_spacer) || (my_mask[my_cursorPosition] != '#')) {
00117                 my_cursorPosition--;
00118                 if(my_cursorPosition < 0) {
00119                         break;
00120                 }
00121         }
00122 
00123         if(my_cursorPosition >= 0) {
00124                 my_cursorPosition++;
00125         }
00126 
00127         SetCursorPos(my_cursorPosition);
00128 
00129         return true;
00130 }

Generated on Tue Jun 24 01:27:48 2008 for Advanced Strategic Command by  doxygen 1.4.2