Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

CSTextfield.cpp

Go to the documentation of this file.
00001 #ifdef WIN32
00002 #pragma warning(disable : 4786 )
00003 #endif
00004 
00005 #include "CSTextfield.h"
00006 #include "CSFont.h"
00007 #include "CSDesktop.h"
00008 #include "CSBorder.h"
00009 #include "CSLAF.h"
00010 
00011 const char *CSTextfield::CLASS = "CSTextfield";
00012 
00013 CSTextfield::CSTextfield() : CSGrafikElement(0, 0)
00014 {
00015     static char *functionName="CSTextfield";
00016     LOG_ENTER 
00017     init("");
00018     LOG_EXIT
00019 }
00020 
00021 CSTextfield::CSTextfield(int width) : CSGrafikElement(0, 0)
00022 {
00023     static char *functionName="CSTextfield";
00024     LOG_ENTER 
00025     setWidth(width);
00026     init("");
00027     LOG_EXIT
00028 }
00029 
00030 CSTextfield::CSTextfield(const std::string &text) : CSGrafikElement(0, 0)
00031 {
00032     static char *functionName="CSTextfield";
00033     LOG_ENTER 
00034     init(text);
00035     LOG_EXIT
00036 }
00037 
00038 CSTextfield::init(const std::string &text)
00039 {
00040     static char *functionName="init";
00041     LOG_ENTER 
00042     CSLAF *laf = CSLAF::getCurrentLAF();
00043     mCursorRate = laf->getCursorRate(); // 500 milliseconds
00044 
00045     mString = text;
00046     mTickTimeNext = 0;
00047     mCursorShown = false;
00048     MESSAGE_TEXT_CHANGED.setSubtype(TEXT_CHANGED_MESSAGE);
00049     MESSAGE_TEXT_CHANGED.sender = this;
00050     setEnabled(false);
00051     layoutSetup();
00052     LOG_EXIT
00053 }
00054 
00055 void CSTextfield::putString(SDL_Surface *destination, SDL_Rect *parentViewport, int x1, int y1, const std::string &string)
00056 {
00057     static char *functionName="putString";
00058     x1 += parentViewport->x + getX();
00059     y1 += parentViewport->y + getY();
00060     getFont()->putString(destination,x1,y1,getTextColor(), string);
00061 }
00062 
00063 //! \todo maybe one can inherit from textAreà? That way we would only have
00064 //! one editing event loop...
00065 void CSTextfield::paint(SDL_Surface *destination, SDL_Rect *parentViewport)
00066 {
00067     static char *functionName="paint";
00068     int xOffset = getInset().getSizeWest() + getBorder()->getSizeWest();
00069     int yOffset = getInset().getSizeNorth() + getBorder()->getSizeNorth();
00070 
00071     if (getFocused())
00072     {
00073         float currentTick = SDL_GetTicks() + mCursorRate;
00074         if (currentTick > mTickTimeNext)
00075         {
00076             mTickTimeNext = currentTick + mCursorRate;
00077             mCursorShown = !mCursorShown;
00078         }
00079         
00080         putString(destination, parentViewport, xOffset, yOffset, mWorkingString);
00081         if (mCursorShown)
00082         {
00083             int witdh = getFont()->getWidth(mWorkingString);
00084             putString(destination, parentViewport, xOffset + witdh, yOffset, "_");
00085         }
00086     }
00087     else
00088     {
00089         putString(destination, parentViewport, xOffset, yOffset, mString);
00090     }
00091 }
00092 
00093 void CSTextfield::reactOnMessage(CSMessage *message)
00094 {
00095     reactOnMessageTextField(message);
00096 }
00097 
00098 void CSTextfield::reactOnMessageTextField(CSMessage *message)
00099 {
00100     static char *functionName="reactOnMessage";
00101     if (message->mIsHandled)
00102     {
00103         reactOnMessageGrafikElement(message);
00104         return;
00105     }
00106     switch (message->getType())
00107     {
00108         case GUI_MESSAGE:
00109         {
00110             GuiMessage *gm = (GuiMessage *) message;
00111             if (gm->receiver != this)
00112             {
00113                 break;
00114             }
00115             else // receiver == this
00116             {
00117                 switch (message->getSubtype())
00118                 {
00119                     case FOCUS_GAINED_MESSAGE:
00120                     {
00121                         setEnabled(true);
00122                         mWorkingString = mString;
00123                         gm->mIsHandled = true;
00124                         break;
00125                     }
00126                     case FOCUS_LOST_MESSAGE:
00127                     {
00128                         setEnabled(false);
00129                         mString = mWorkingString;
00130                         gm->mIsHandled = true;
00131                         break;
00132                     }
00133                     case KEY_RELEASED_MESSAGE:
00134                     {
00135                         gm->mIsHandled = false;
00136                         break;
00137                     }
00138                     case KEY_PRESSED_MESSAGE:
00139                     {
00140                         int ch=gm->keyUnicodeChar;
00141                         if ((gm->keySymbolic == SDLK_RETURN) || 
00142                             (gm->keySymbolic == SDLK_KP_ENTER) || 
00143                             (gm->keySymbolic == SDLK_TAB))
00144                         {
00145                             getDesktop()->focusedNextComponent();
00146                             gm->mIsHandled = true;
00147                             MESSAGE_TEXT_CHANGED.mIsHandled = false;
00148                             MESSAGE_TEXT_CHANGED.actionId = mActionId;
00149 
00150                             sendMessage(MESSAGE_TEXT_CHANGED);
00151                             break;
00152                         }
00153                         if (((ch>31)||(ch=='\b')) && (ch<128)) 
00154                         {
00155                             int len = mWorkingString.length();
00156                             if (ch=='\b')
00157                             {
00158                                 if (len>0)
00159                                 {
00160                                     mWorkingString.resize(len-1);
00161                                 }
00162                             }
00163                             else
00164                             {
00165                                 mWorkingString += (char) ch;
00166                                 
00167                                 len = mWorkingString.length();
00168                                 int witdh = getFont()->getWidth(mWorkingString) + getFont()->getWidth("_");
00169                                 if (witdh > mWidth - (getInset().getTotalWidth() + getBorder()->getTotalWidth()))
00170                                 {
00171                                     mWorkingString.resize(len-1);
00172                                 }
00173                             }
00174                             gm->mIsHandled = true;
00175                         }
00176                         break;
00177                     }
00178                 }
00179             }                       
00180         }
00181     }
00182     reactOnMessageGrafikElement(message);
00183 }
00184 
00185 void CSTextfield::layoutSetupTextfield()
00186 {
00187     static char *functionName="layoutSetupTextfield";
00188     LOG_ENTER 
00189     CSLAF *laf = CSLAF::getCurrentLAF();
00190 
00191     if (!mWidthSet)
00192     {
00193         mWidth =    getFont()->getWidth(mString) 
00194                     + getBorder()->getTotalWidth()
00195                     + getInset().getTotalWidth();
00196         mMinWidth = getFont()->getWidth("12345");
00197     }
00198     else
00199     {
00200         mMinWidth = mWidth - getBorder()->getTotalWidth() - getInset().getTotalWidth();
00201     }
00202     mHeight =   getFont()->getHeight() 
00203                 + getBorder()->getTotalHeight()
00204                 + getInset().getTotalHeight();
00205     mMinHeight = getFont()->getHeight();
00206 
00207     LOG_EXIT
00208 }
00209 

Generated on Wed Jul 14 00:43:42 2004 for CSLib by doxygen 1.3.6