Planeshift

pawstextbox.h

Go to the documentation of this file.
00001 /*
00002  * pawstextbox.h - Author: Andrew Craig
00003  *
00004  * Copyright (C) 2003 Atomic Blue ([email protected], http://www.atomicblue.org)
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program 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
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 // pawstextbox.h: interface for the pawsTextBox class.
00020 //
00022 
00023 #ifndef PAWS_TEXT_BOX_HEADER
00024 #define PAWS_TEXT_BOX_HEADER
00025 
00026 struct iVirtualClock;
00027 
00028 #include "pawswidget.h"
00029 #include <csutil/parray.h>
00030 #include <ivideo/fontserv.h>
00031 #include <iutil/virtclk.h>
00032 
00033 #include <ispellchecker.h>
00034 
00042 class pawsTextBox : public pawsWidget
00043 {
00044 public:
00048     enum pawsVertAdjust
00049     {
00050         vertTOP,    
00051         vertBOTTOM, 
00052         vertCENTRE  
00053     };
00054 
00058     enum pawsHorizAdjust
00059     {
00060         horizLEFT,  
00061         horizRIGHT, 
00062         horizCENTRE 
00063     };
00064 
00068     pawsTextBox();
00069 
00073     virtual ~pawsTextBox();
00074 
00078     pawsTextBox(const pawsTextBox &origin);
00079 
00092     bool Setup(iDocumentNode* node);
00098     bool SelfPopulate(iDocumentNode* node);
00099 
00101     void Draw();
00102 
00107     void SetText(const char* text);
00108 
00113     void FormatText(const char* fmt, ...);
00114 
00119     const char* GetText() const
00120     {
00121         return text;
00122     }
00123 
00128     void SetVertical(bool vertical);
00129 
00135     void Adjust(pawsHorizAdjust horiz, pawsVertAdjust vert);
00140     void HorizAdjust(pawsHorizAdjust horiz);
00145     void VertAdjust(pawsVertAdjust vert);
00146 
00152     void SetSizeByText(int padX,int padY);
00153 
00158     virtual bool OnGainFocus(bool)
00159     {
00160         return false;
00161     }
00162 
00167     virtual int GetBorderStyle()
00168     {
00169         return BORDER_SUNKEN;
00170     }
00171 
00176     inline void Grayed(bool g)
00177     {
00178         grayed = g;
00179     }
00180 
00185     virtual int GetFontColour();
00186 
00193     virtual void OnUpdateData(const char* dataname,PAWSData &data);
00194 
00202     static int CountCodePoints(const char* text, int start = 0, int len = -1);
00203 
00211     static int RewindCodePoints(const char* text, int start, int count);
00212 
00220     static int SkipCodePoints(const char* text, int start, int count);
00221 
00222 protected:
00223 
00227     void CalcTextPos();
00228 
00234     void CalcTextSize(int &width, int &height);
00235 
00239     void CalcLetterSizes();
00240 
00242     csString text;
00243 
00245     int colour;
00246 
00248     bool grayed;
00249 
00251     pawsHorizAdjust horizAdjust;
00252 
00254     pawsVertAdjust  vertAdjust;
00255 
00257     bool vertical;
00258 
00260     int textX;
00261 
00263     int textY;
00264 
00266     int textWidth;
00267 
00269     psPoint* letterSizes;
00270 
00271 };
00272 
00273 //--------------------------------------------------------------------------
00274 CREATE_PAWS_FACTORY(pawsTextBox);
00275 
00276 
00277 
00278 //--------------------------------------------------------------------------
00279 
00280 #define MESSAGE_TEXTBOX_MOUSE_SCROLL_AMOUNT 3
00281 
00285 class pawsMessageTextBox : public pawsWidget
00286 {
00287 public:
00288     struct MessageSegment
00289     {
00290         int x;
00291         csString text;
00292         int colour;
00293         int size;
00294 
00295         MessageSegment() : x(0), text(""), colour(0), size(0) { }
00296     };
00297     struct MessageLine
00298     {
00299         csString text;
00300         int colour;
00301         int size;
00302         csArray<MessageSegment> segments;
00303 
00304         MessageLine()
00305         {
00306             text = "";
00307             colour = 0;
00308             size = 0;
00309         }
00310         MessageLine(const MessageLine &origin):text(origin.text),colour(origin.colour),size(origin.size)
00311         {
00312             for(unsigned int i = 0 ; i < origin.segments.GetSize(); i++)
00313                 segments.Push(origin.segments[i]);
00314         }
00315     };
00316 
00317     pawsMessageTextBox();
00318     pawsMessageTextBox(const pawsMessageTextBox &origin);
00319     virtual ~pawsMessageTextBox();
00320     virtual bool Setup(iDocumentNode* node);
00321     virtual bool Setup(void);
00322     virtual bool PostSetup();
00323 
00329     bool SelfPopulate(iDocumentNode* node);
00330 
00331     virtual void Draw();
00332 
00338     void AddMessage(const char* data, int colour = -1);
00339 
00340     void AppendLastMessage(const char* data);
00341     void ReplaceLastMessage(const char* data);
00342 
00343     virtual void OnResize();
00344     virtual void Resize();
00345     virtual bool OnScroll(int direction, pawsScrollBar* widget);
00346     virtual bool OnMouseDown(int button, int modifiers, int x, int y);
00347 
00348     void Clear();
00349 
00350     virtual int GetBorderStyle()
00351     {
00352         return BORDER_SUNKEN;
00353     }
00354 
00356     void ResetScroll();
00357 
00359     void FullScroll();
00360 
00361     virtual void OnUpdateData(const char* dataname,PAWSData &data);
00362 
00363     bool OnKeyDown(utf32_char code, utf32_char key, int modifiers);
00364 
00365 
00366 protected:
00368     int RenderMessage(const char* data, int lineStart, int colour);
00369 
00371     csPDelArray<MessageLine> messages;
00372 
00373     //void AdjustMessages();
00374 
00375     void SplitMessage(const char* newText, int colour, int size, MessageLine* &msgLine, int &startPosition);
00376 
00378     void CalcLineHeight();
00379 
00384     pawsScrollBar* GetScrollBar();
00385 
00386     csPDelArray<MessageLine> adjusted;
00387 
00388     int lineHeight;
00389     size_t maxLines;
00390 
00391     int topLine;
00392 
00393     pawsScrollBar* scrollBar;
00394     int scrollBarWidth;
00395 
00396 private:
00397     static const int INITOFFSET = 20;
00398     void WriteMessageLine(MessageLine* &msgLine, csString text, int colour);
00399     void WriteMessageSegment(MessageLine* &msgLine, csString text, int colour, int startPosition);
00400     csString FindStringThatFits(csString stringBuffer, int canDrawLength);
00401 };
00402 
00403 CREATE_PAWS_FACTORY(pawsMessageTextBox);
00404 
00405 #define EDIT_TEXTBOX_MOUSE_SCROLL_AMOUNT 3
00406 #define BLINK_TICKS  1000
00407 
00409 class pawsEditTextBox : public pawsWidget
00410 {
00411 public:
00412     pawsEditTextBox();
00413     virtual ~pawsEditTextBox();
00414     pawsEditTextBox(const pawsEditTextBox &origin);
00415 
00416     virtual void Draw();
00417 
00418     bool Setup(iDocumentNode* node);
00419 
00420     bool OnKeyDown(utf32_char code, utf32_char key, int modifiers);
00421 
00422     const char* GetText()
00423     {
00424         return text.GetDataSafe();
00425     }
00426 
00427     void SetMultiline(bool multi);
00428     void SetPassword(bool pass); //displays astrices instead of text
00429 
00436     void SetText(const char* text, bool publish = true);
00437 
00438     virtual void OnUpdateData(const char* dataname,PAWSData &data);
00439 
00443     void SetSizeByText();
00444 
00445     void Clear();
00446 
00447     virtual bool OnMouseDown(int button, int modifiers, int x, int y);
00448 
00454     virtual bool OnClipboard(const csString &content);
00455 
00456     virtual int GetBorderStyle()
00457     {
00458         return BORDER_SUNKEN;
00459     }
00460 
00466     bool SelfPopulate(iDocumentNode* node);
00467 
00471     unsigned int GetTopLine()
00472     {
00473         return topLine;
00474     }
00475     void SetTopLine(unsigned int newTopLine)
00476     {
00477         topLine = newTopLine;
00478     }
00479 
00480     void SetCursorPosition(size_t pos)
00481     {
00482         cursorPosition = pos;
00483     }
00484 
00485     virtual bool GetFocusOverridesControls() const
00486     {
00487         return true;
00488     }
00489 
00494     void SetMaxLength(unsigned int maxlen);
00499     inline unsigned int GetMaxLength() const
00500     {
00501         return maxLen;
00502     }
00507     inline unsigned int GetRemainingChars() const
00508     {
00509         if(maxLen)
00510             return (unsigned int)(maxLen - text.Length());
00511         return UINT_MAX;
00512     }
00516     inline bool getSpellChecked() const
00517     {
00518         return spellChecked;
00519     };
00524     inline void setSpellChecked(const bool check)
00525     {
00526         spellChecked = check;
00527     };
00531     inline void toggleSpellChecked()
00532     {
00533         spellChecked = !spellChecked;
00534     };
00537     unsigned int getTypoColour()
00538     {
00539         return typoColour;
00540     };
00544     void setTypoColour(unsigned int col)
00545     {
00546         typoColour = col;
00547     };
00548 protected:
00551     void checkSpelling();
00554     struct Word
00555     {
00556         bool correct;
00557         int endPos;
00558     };
00559 
00560     bool password;
00561     csRef<iVirtualClock> clock;
00562 
00564     int start;
00565 
00567     size_t cursorPosition;
00568     unsigned int cursorLine;
00569 
00571     bool blink;
00572 
00574     csTicks blinkTicks;
00575 
00577     csString text;
00578 
00579     bool multiLine;
00580 
00581     int lineHeight;
00582     size_t maxLines;
00583 
00584     // This value will always be one less than the number of used lines
00585     size_t usedLines;
00586 
00587     unsigned int topLine;
00589     unsigned int maxLen;
00590 
00591     pawsScrollBar* vScrollBar;
00594     csRef<iSpellChecker> spellChecker;
00597     bool spellChecked;
00600     unsigned int typoColour;
00603     csArray<Word> words;
00604 };
00605 
00606 CREATE_PAWS_FACTORY(pawsEditTextBox);
00607 
00608 
00609 //---------------------------------------------------------------------------------------
00610 
00611 
00612 #define MULTILINE_TEXTBOX_MOUSE_SCROLL_AMOUNT 3
00613 class pawsMultiLineTextBox : public pawsWidget
00614 {
00615 public:
00616     pawsMultiLineTextBox();
00617     pawsMultiLineTextBox(const pawsMultiLineTextBox &origin);
00618     virtual ~pawsMultiLineTextBox();
00619 
00620     bool Setup(iDocumentNode* node);
00621     bool PostSetup();
00622 
00623     void Draw();
00624 
00625     void SetText(const char* text);
00626     const char* GetText()
00627     {
00628         return text;
00629     }
00630     void Resize();
00631     bool OnScroll(int direction, pawsScrollBar* widget);
00632     virtual bool OnMouseDown(int button, int modifiers, int x, int y);
00633 
00634     // Normal text boxes should not be focused.
00635     virtual bool OnGainFocus(bool /*notifyParent*/ = true)
00636     {
00637         return false;
00638     }
00639     virtual void OnUpdateData(const char* dataname,PAWSData &data);
00640 
00641 protected:
00642 
00643     void OrganizeText(const char* text);
00644 
00649     pawsScrollBar* GetScrollBar();
00650 
00652     csString text;
00654     csArray<csString> lines;
00655 
00656     //where our scrollbar at?
00657     size_t startLine;
00658     //Number of lines that we can fit in the box
00659     size_t canDrawLines;
00660     bool usingScrollBar;
00661     int maxWidth;
00662     int maxHeight;
00663     pawsScrollBar* scrollBar;
00664 };
00665 
00666 //--------------------------------------------------------------------------
00667 CREATE_PAWS_FACTORY(pawsMultiLineTextBox);
00668 
00669 
00670 class pawsMultiPageTextBox : public pawsWidget
00671 {
00672 public:
00673     pawsMultiPageTextBox();
00674     pawsMultiPageTextBox(const pawsMultiPageTextBox &origin);
00675     virtual ~pawsMultiPageTextBox();
00676 
00677     bool Setup(iDocumentNode* node);
00678     bool PostSetup();
00679 
00680 
00681 
00682 
00683     void Draw();
00684 
00685     void SetText(const char* text);
00686     const char* GetText()
00687     {
00688         return text;
00689     }
00690     void Resize();
00691     bool OnScroll(int direction, pawsScrollBar* widget);
00692 
00693     virtual bool OnMouseDown(int button, int modifiers, int x, int y);
00694 
00695     // Normal text boxes should not be focused.
00696     virtual bool OnGainFocus(bool /*notifyParent*/ = true)
00697     {
00698         return false;
00699     }
00700     virtual void OnUpdateData(const char* dataname,PAWSData &data);
00701 
00702     // Added these member functions for dealing with pages
00703     int GetNumPages();
00704     void SetCurrentPageNum(int n);
00705     int GetCurrentPageNum();
00706 protected:
00707 
00708     // splits the given text in separate lines filling the 'lines' variable
00709     void OrganizeText(const char* text);
00710 
00715     pawsScrollBar* GetScrollBar();
00716 
00717 
00719     csString text;
00721     csArray<csString> lines;
00722 
00723     //where our scrollbar at?and that d
00724     size_t startLine;
00725     //Number of lines that we can fit in the box
00726     size_t canDrawLines;
00727     bool usingScrollBar;
00728     int maxWidth;
00729     int maxHeight;
00730     pawsScrollBar* scrollBar;
00731     int currentPageNum;
00732     int numPages;
00733 };
00734 
00735 //--------------------------------------------------------------------------
00736 CREATE_PAWS_FACTORY(pawsMultiPageTextBox);
00737 
00738 
00739 /* An edit box widget */
00740 class pawsMultilineEditTextBox : public pawsWidget
00741 {
00742 public:
00743     pawsMultilineEditTextBox();
00744     virtual ~pawsMultilineEditTextBox();
00745     pawsMultilineEditTextBox(const pawsMultilineEditTextBox &origin);
00746     struct MessageLine
00747     {
00748         size_t lineLength; //Stores length of the line.
00749         size_t breakLine; //Stores real text position of where break occurs.
00750         int lineExtra; //Stores 1 if line ends with a \n, or 0 if it does not.
00751     };
00752 
00753     bool Setup(iDocumentNode* node);
00754 
00755     virtual void Draw();
00756     void DrawWidgetText(const char* text, size_t x, size_t y, int style, int fg, int visLine);
00757 
00764     void SetText(const char* text, bool publish = true);
00765     const char* GetText();
00766     void UpdateScrollBar();
00767     virtual void SetupScrollBar();
00768 
00769     bool OnScroll(int direction, pawsScrollBar* widget);
00770     virtual void OnResize();
00771     virtual bool OnMouseUp(int button, int modifiers, int x, int y);
00772     virtual bool OnMouseDown(int button, int modifiers, int x, int y);
00777     virtual bool OnClipboard(const csString &content);
00778     virtual void OnUpdateData(const char* dataname,PAWSData &data);
00779     virtual bool OnKeyDown(utf32_char code, utf32_char key, int modifiers);
00780     virtual void CalcMouseClick(int x, int y, size_t &cursorLine, size_t &cursorChar);
00781 
00782     virtual bool GetFocusOverridesControls() const
00783     {
00784         return true;
00785     }
00786 
00787     void PushLineInfo(size_t lineLength, size_t lineBreak, int lineExtra);
00788 
00789     virtual int GetBorderStyle()
00790     {
00791         return BORDER_SUNKEN;
00792     }
00793     void LayoutText();
00794 
00800     bool SelfPopulate(iDocumentNode* node);
00801 
00805     size_t GetTopLine()
00806     {
00807         return topLine;
00808     }
00809     void SetTopLine(size_t newTopLine)
00810     {
00811         topLine = newTopLine;
00812     }
00813     size_t GetCursorPosition();
00814     size_t GetCursorPosition(size_t destLine, size_t destCursor);
00815     void SetCursorPosition(size_t pos)
00816     {
00817         cursorPosition = pos;
00818     }
00819     void Clear()
00820     {
00821         SetText("");
00822     }
00823     csString GetLine(size_t line);
00824     int GetLineWidth(int lineNumber);
00825     void GetLinePos(size_t lineNumber, size_t &start, size_t &end);
00826     void GetLineRelative(size_t pos, size_t &start, size_t &end);
00827     bool EndsWithNewLine(int lineNumber);
00828     void GetCursorLocation(size_t pos, size_t &destLine, size_t &destCursor);
00829     char GetAt(size_t destLine, size_t destCursor);
00830 
00835     void SetMaxLength(unsigned int maxlen);
00840     inline unsigned int GetMaxLength() const
00841     {
00842         return maxLen;
00843     }
00848     inline unsigned int GetRemainingChars() const
00849     {
00850         if(maxLen)
00851             return (unsigned int)(maxLen - text.Length());
00852         return UINT_MAX;
00853     }
00857     inline bool getSpellChecked() const
00858     {
00859         return spellChecked;
00860     };
00865     inline void setSpellChecked(const bool check)
00866     {
00867         spellChecked = check;
00868     };
00872     inline void toggleSpellChecked()
00873     {
00874         spellChecked = !spellChecked;
00875     };
00878     unsigned int getTypoColour()
00879     {
00880         return typoColour;
00881     };
00885     void setTypoColour(unsigned int col)
00886     {
00887         typoColour = col;
00888     };
00889 
00890     int GetLinesPerPage()
00891     {
00892         return canDrawLines;
00893     }
00894 protected:
00898     void checkSpelling();
00907     void drawTextSpellChecked(iFont* font, int x, int y, int fg, csString str, int visLine);
00908 
00909 
00912     struct Word
00913     {
00914         bool correct;
00915         int endPos;
00916     };
00917 
00918     csRef<iVirtualClock> clock;
00919 
00920     // The position of the cursor blink
00921     size_t cursorPosition;
00922 
00923     // The position of the cursor in an array (Used by draw method).
00924     size_t cursorLine;
00925     size_t cursorLoc;
00926 
00928     bool blink;
00929 
00931     csTicks blinkTicks;
00932 
00934     csString text;
00935     csPDelArray<MessageLine> lineInfo;
00936     int lineHeight;
00937 
00938     // Width of the vertical scroll bar
00939     int vScrollBarWidth;
00940     size_t canDrawLines;
00941 
00942     size_t topLine;
00943     pawsScrollBar* vScrollBar;
00944 
00945     bool usingScrollBar;
00946 
00947     int maxWidth;
00948     int maxHeight;
00949 
00950     int yPos;
00951     csString tmp;
00952 
00954     unsigned int maxLen;
00955     /* PS spellChecker plugin
00956      */
00957     csRef<iSpellChecker> spellChecker;
00958     /* Should the text be checked by the spellchecker plugin (if available)
00959      */
00960     bool spellChecked;
00963     unsigned int typoColour;
00964 
00965     csArray<csArray<Word> > lineTypos;
00966 };
00967 
00968 CREATE_PAWS_FACTORY(pawsMultilineEditTextBox);
00969 
00970 
00971 
00973 #define FADE_TIME              1000 // Time for the onscreen message to fade away
00974 #define MESG_TIME              3000 // Time for the onscreen message to be shown ( not including fading)
00975 
00976 class pawsFadingTextBox : public pawsWidget
00977 {
00978 public:
00979     pawsFadingTextBox();
00980     virtual ~pawsFadingTextBox() {};
00981     pawsFadingTextBox(const pawsFadingTextBox &origin);
00982     bool Setup(iDocumentNode* /*node*/)
00983     {
00984         return true;   // Shouldn't be created in XML, only by code
00985     }
00986 
00987     void Draw();
00988 
00989     void Fade(); // Force fading
00990     void SetText(const char* newtext, iFont* font1,iFont* font2, int color,int time=MESG_TIME,int fadetime=FADE_TIME);
00991 
00992     void GetSize(int &w, int &h);
00993 private:
00994     void DrawBorderText(const char* text, iFont* font,int x);
00995 
00996     csString text;
00997     csString first;
00998 
00999     csRef<iFont> firstFont; // used for the first letter
01000     csRef<iFont> font;      // used for the rest
01001 
01002     int org_color;
01003     int color;
01004     int scolor;
01005     csTicks start;
01006 
01007     int ymod;
01008 
01009     int time,fadetime;
01010 };
01011 
01012 CREATE_PAWS_FACTORY(pawsFadingTextBox);
01014 struct PictureInfo
01015 {
01016     unsigned int width; 
01017     unsigned int height;
01018     csString srcString;
01019     unsigned int align;
01020     unsigned int padding[4];
01021 };
01022 class pawsDocumentView: public pawsMultiLineTextBox
01023 {
01024 public:
01025     pawsDocumentView();
01026     pawsDocumentView(const pawsDocumentView &origin);
01027     virtual ~pawsDocumentView();
01040     void SetText(const char* text);
01044     void Draw();
01045     void Resize();
01046 protected:
01054     void OrganizeContent(const char* text);
01055 
01064     unsigned int ProcessPictureInfo(iDocumentNode* node);
01065 
01066     csArray<PictureInfo> picsInfo;
01067 };
01068 CREATE_PAWS_FACTORY(pawsDocumentView);
01069 
01070 
01071 class pawsMultiPageDocumentView: public pawsMultiPageTextBox
01072 {
01073 public:
01074     pawsMultiPageDocumentView();
01075     pawsMultiPageDocumentView(const pawsMultiPageDocumentView &origin);
01076     virtual ~pawsMultiPageDocumentView();
01090     void SetText(const char* text);
01091 
01095     void Draw();
01096 
01097     void Resize();
01098 protected:
01106     void OrganizeContent(const char* text);
01107 
01116     unsigned int ProcessPictureInfo(iDocumentNode* node);
01117     csArray<PictureInfo> picsInfo;
01118 };
01119 CREATE_PAWS_FACTORY(pawsMultiPageDocumentView);
01120 
01123 #endif
01124