]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/gui/intlGUIEditBox.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / gui / intlGUIEditBox.cpp
index 8b0f10721551393654d602df818ec84b236fc510..e917f73c1ab8abed2d8ddbf2ebaf1d5eb3808c31 100644 (file)
 #include <util/numeric.h>
 #include "intlGUIEditBox.h"
 
-#if defined(_IRR_COMPILE_WITH_GUI_) && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
-
 #include "IGUISkin.h"
 #include "IGUIEnvironment.h"
 #include "IGUIFont.h"
 #include "IVideoDriver.h"
-//#include "rect.h"
 //#include "irrlicht/os.cpp"
 #include "porting.h"
 //#include "Keycodes.h"
@@ -62,7 +59,7 @@ namespace gui
 intlGUIEditBox::intlGUIEditBox(const wchar_t* text, bool border,
                IGUIEnvironment* environment, IGUIElement* parent, s32 id,
                const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
-       : IGUIEditBox(environment, parent, id, rectangle),
+       : GUIEditBox(environment, parent, id, rectangle),
        Border(border), FrameRect(rectangle),
        m_scrollbar_width(0), m_vscrollbar(NULL), m_writable(writable)
 {
@@ -111,57 +108,11 @@ intlGUIEditBox::intlGUIEditBox(const wchar_t* text, bool border,
 //! destructor
 intlGUIEditBox::~intlGUIEditBox()
 {
-       if (OverrideFont)
-               OverrideFont->drop();
-
        if (Operator)
                Operator->drop();
-}
-
-
-//! Sets another skin independent font.
-void intlGUIEditBox::setOverrideFont(IGUIFont* font)
-{
-       if (OverrideFont == font)
-               return;
-
-       if (OverrideFont)
-               OverrideFont->drop();
-
-       OverrideFont = font;
-
-       if (OverrideFont)
-               OverrideFont->grab();
-
-       breakText();
-}
-
-IGUIFont * intlGUIEditBox::getOverrideFont() const
-{
-       return OverrideFont;
-}
 
-//! Get the font which is used right now for drawing
-IGUIFont* intlGUIEditBox::getActiveFont() const
-{
-       if ( OverrideFont )
-               return OverrideFont;
-       IGUISkin* skin = Environment->getSkin();
-       if (skin)
-               return skin->getFont();
-       return 0;
-}
-
-//! Sets another color for the text.
-void intlGUIEditBox::setOverrideColor(video::SColor color)
-{
-       OverrideColor = color;
-       OverrideColorEnabled = true;
-}
-
-video::SColor intlGUIEditBox::getOverrideColor() const
-{
-       return OverrideColor;
+       if (m_vscrollbar)
+               m_vscrollbar->drop();
 }
 
 //! Turns the border on or off
@@ -175,26 +126,6 @@ void intlGUIEditBox::setDrawBackground(bool draw)
 {
 }
 
-//! Sets if the text should use the overide color or the color in the gui skin.
-void intlGUIEditBox::enableOverrideColor(bool enable)
-{
-       OverrideColorEnabled = enable;
-}
-
-bool intlGUIEditBox::isOverrideColorEnabled() const
-{
-       _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
-       return OverrideColorEnabled;
-}
-
-//! Enables or disables word wrap
-void intlGUIEditBox::setWordWrap(bool enable)
-{
-       WordWrap = enable;
-       breakText();
-}
-
-
 void intlGUIEditBox::updateAbsolutePosition()
 {
     core::rect<s32> oldAbsoluteRect(AbsoluteRect);
@@ -205,30 +136,6 @@ void intlGUIEditBox::updateAbsolutePosition()
        }
 }
 
-
-//! Checks if word wrap is enabled
-bool intlGUIEditBox::isWordWrapEnabled() const
-{
-       _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
-       return WordWrap;
-}
-
-
-//! Enables or disables newlines.
-void intlGUIEditBox::setMultiLine(bool enable)
-{
-       MultiLine = enable;
-}
-
-
-//! Checks if multi line editing is enabled
-bool intlGUIEditBox::isMultiLineEnabled() const
-{
-       _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
-       return MultiLine;
-}
-
-
 void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
 {
        PasswordBox = passwordBox;
@@ -244,7 +151,6 @@ void intlGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
 
 bool intlGUIEditBox::isPasswordBox() const
 {
-       _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
        return PasswordBox;
 }
 
@@ -277,7 +183,7 @@ bool intlGUIEditBox::OnEvent(const SEvent& event)
                        break;
                case EET_KEY_INPUT_EVENT:
         {
-#if (defined(__linux__) || defined(__FreeBSD__))
+#if (defined(__linux__) || defined(__FreeBSD__)) || defined(__DragonFly__)
             // ################################################################
                        // ValkaTR:
             // This part is the difference from the original intlGUIEditBox
@@ -354,8 +260,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
                        break;
                case KEY_KEY_X:
                        // cut to the clipboard
-                       if (!PasswordBox && Operator && MarkBegin != MarkEnd)
-                       {
+                       if (!PasswordBox && Operator && MarkBegin != MarkEnd) {
                                const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
                                const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
 
@@ -364,8 +269,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
                                sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
                                Operator->copyToClipboard(sc.c_str());
 
-                               if (IsEnabled)
-                               {
+                               if (IsEnabled && m_writable) {
                                        // delete
                                        core::stringw s;
                                        s = Text.subString(0, realmbgn);
@@ -380,7 +284,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
                        }
                        break;
                case KEY_KEY_V:
-                       if ( !IsEnabled )
+                       if (!IsEnabled || !m_writable)
                                break;
 
                        // paste from the clipboard
@@ -470,7 +374,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
        case KEY_END:
                {
                        s32 p = Text.size();
-                       if (WordWrap || MultiLine)
+                       if (m_word_wrap || m_multiline)
                        {
                                p = getLineFromPos(CursorPos);
                                p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
@@ -498,7 +402,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
                {
 
                        s32 p = 0;
-                       if (WordWrap || MultiLine)
+                       if (m_word_wrap || m_multiline)
                        {
                                p = getLineFromPos(CursorPos);
                                p = BrokenTextPositions[p];
@@ -520,7 +424,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
                }
                break;
        case KEY_RETURN:
-               if (MultiLine)
+               if (m_multiline)
                {
                        inputChar(L'\n');
                        return true;
@@ -573,7 +477,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
                BlinkStartTime = porting::getTimeMs();
                break;
        case KEY_UP:
-               if (MultiLine || (WordWrap && BrokenText.size() > 1) )
+               if (m_multiline || (m_word_wrap && BrokenText.size() > 1) )
                {
                        s32 lineNo = getLineFromPos(CursorPos);
                        s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
@@ -604,7 +508,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
                }
                break;
        case KEY_DOWN:
-               if (MultiLine || (WordWrap && BrokenText.size() > 1) )
+               if (m_multiline || (m_word_wrap && BrokenText.size() > 1) )
                {
                        s32 lineNo = getLineFromPos(CursorPos);
                        s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
@@ -636,7 +540,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
                break;
 
        case KEY_BACK:
-               if ( !this->IsEnabled )
+               if (!this->IsEnabled || !m_writable)
                        break;
 
                if (!Text.empty()) {
@@ -675,7 +579,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
                }
                break;
        case KEY_DELETE:
-               if ( !this->IsEnabled )
+               if (!this->IsEnabled || !m_writable)
                        break;
 
                if (!Text.empty()) {
@@ -797,8 +701,8 @@ void intlGUIEditBox::draw()
 
        // draw the text
 
-       IGUIFont* font = OverrideFont;
-       if (!OverrideFont)
+       IGUIFont* font = m_override_font;
+       if (!m_override_font)
                font = skin->getFont();
 
        s32 cursorLine = 0;
@@ -819,7 +723,7 @@ void intlGUIEditBox::draw()
                core::stringw s, s2;
 
                // get mark position
-               const bool ml = (!PasswordBox && (WordWrap || MultiLine));
+               const bool ml = (!PasswordBox && (m_word_wrap || m_multiline));
                const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
                const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
                const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
@@ -828,14 +732,14 @@ void intlGUIEditBox::draw()
 
                // Save the override color information.
                // Then, alter it if the edit box is disabled.
-               const bool prevOver = OverrideColorEnabled;
-               const video::SColor prevColor = OverrideColor;
+               const bool prevOver = m_override_color_enabled;
+               const video::SColor prevColor = m_override_color;
 
                if (!Text.empty()) {
-                       if (!IsEnabled && !OverrideColorEnabled)
+                       if (!IsEnabled && !m_override_color_enabled)
                        {
-                               OverrideColorEnabled = true;
-                               OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
+                               m_override_color_enabled = true;
+                               m_override_color = skin->getColor(EGDC_GRAY_TEXT);
                        }
 
                        for (s32 i=0; i < lineCount; ++i)
@@ -876,7 +780,7 @@ void intlGUIEditBox::draw()
 
                                // draw normal text
                                font->draw(txtLine->c_str(), CurrentTextRect,
-                                       OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
+                                       m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
                                        false, true, &localClipRect);
 
                                // draw mark and marked text
@@ -920,20 +824,20 @@ void intlGUIEditBox::draw()
 
                                        if (!s.empty())
                                                font->draw(s.c_str(), CurrentTextRect,
-                                                       OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
+                                                       m_override_color_enabled ? m_override_color : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
                                                        false, true, &localClipRect);
 
                                }
                        }
 
                        // Return the override color information to its previous settings.
-                       OverrideColorEnabled = prevOver;
-                       OverrideColor = prevColor;
+                       m_override_color_enabled = prevOver;
+                       m_override_color = prevColor;
                }
 
                // draw cursor
 
-               if (WordWrap || MultiLine)
+               if (m_word_wrap || m_multiline)
                {
                        cursorLine = getLineFromPos(CursorPos);
                        txtLine = &BrokenText[cursorLine];
@@ -949,7 +853,7 @@ void intlGUIEditBox::draw()
                                CurrentTextRect.UpperLeftCorner.X += charcursorpos;
 
                                font->draw(L"_", CurrentTextRect,
-                                       OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
+                                       m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
                                        false, true, &localClipRect);
                        }
                }
@@ -971,23 +875,6 @@ void intlGUIEditBox::setText(const wchar_t* text)
 }
 
 
-//! Enables or disables automatic scrolling with cursor position
-//! \param enable: If set to true, the text will move around with the cursor position
-void intlGUIEditBox::setAutoScroll(bool enable)
-{
-       AutoScroll = enable;
-}
-
-
-//! Checks to see if automatic scrolling is enabled
-//! \return true if automatic scrolling is enabled, false if not
-bool intlGUIEditBox::isAutoScrollEnabled() const
-{
-       _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
-       return AutoScroll;
-}
-
-
 //! Gets the area of the text in the edit box
 //! \return Returns the size in pixels of the text
 core::dimension2du intlGUIEditBox::getTextDimension()
@@ -1103,12 +990,12 @@ bool intlGUIEditBox::processMouse(const SEvent& event)
 
 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
 {
-       IGUIFont* font = OverrideFont;
+       IGUIFont* font = m_override_font;
        IGUISkin* skin = Environment->getSkin();
-       if (!OverrideFont)
+       if (!m_override_font)
                font = skin->getFont();
 
-       const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
+       const u32 lineCount = (m_word_wrap || m_multiline) ? BrokenText.size() : 1;
 
        core::stringw *txtLine = NULL;
        s32 startPos = 0;
@@ -1125,8 +1012,8 @@ s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
                // is it inside this region?
                if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y) {
                        // we've found the clicked line
-                       txtLine = (WordWrap || MultiLine) ? &BrokenText[curr_line_idx] : &Text;
-                       startPos = (WordWrap || MultiLine) ? BrokenTextPositions[curr_line_idx] : 0;
+                       txtLine = (m_word_wrap || m_multiline) ? &BrokenText[curr_line_idx] : &Text;
+                       startPos = (m_word_wrap || m_multiline) ? BrokenTextPositions[curr_line_idx] : 0;
                        break;
                }
        }
@@ -1151,14 +1038,14 @@ void intlGUIEditBox::breakText()
 {
        IGUISkin* skin = Environment->getSkin();
 
-       if ((!WordWrap && !MultiLine) || !skin)
+       if ((!m_word_wrap && !m_multiline) || !skin)
                return;
 
        BrokenText.clear(); // need to reallocate :/
        BrokenTextPositions.set_used(0);
 
-       IGUIFont* font = OverrideFont;
-       if (!OverrideFont)
+       IGUIFont* font = m_override_font;
+       if (!m_override_font)
                font = skin->getFont();
 
        if (!font)
@@ -1172,7 +1059,7 @@ void intlGUIEditBox::breakText()
        s32 lastLineStart = 0;
        s32 size = Text.size();
        s32 length = 0;
-       s32 elWidth = RelativeRect.getWidth() - 6;
+       s32 elWidth = RelativeRect.getWidth() - m_scrollbar_width - 10;
        wchar_t c;
 
        for (s32 i=0; i<size; ++i)
@@ -1197,7 +1084,7 @@ void intlGUIEditBox::breakText()
                }
 
                // don't break if we're not a multi-line edit box
-               if (!MultiLine)
+               if (!m_multiline)
                        lineBreak = false;
 
                if (c == L' ' || c == 0 || i == (size-1))
@@ -1208,7 +1095,7 @@ void intlGUIEditBox::breakText()
                                s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
                                s32 worldlgth = font->getDimension(word.c_str()).Width;
 
-                               if (WordWrap && length + worldlgth + whitelgth > elWidth)
+                               if (m_word_wrap && length + worldlgth + whitelgth > elWidth)
                                {
                                        // break to next line
                                        length = worldlgth;
@@ -1267,14 +1154,14 @@ void intlGUIEditBox::setTextRect(s32 line)
        if (!skin)
                return;
 
-       IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
+       IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
 
        if (!font)
                return;
 
        // get text dimension
-       const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
-       if (WordWrap || MultiLine)
+       const u32 lineCount = (m_word_wrap || m_multiline) ? BrokenText.size() : 1;
+       if (m_word_wrap || m_multiline)
        {
                d = font->getDimension(BrokenText[line].c_str());
        }
@@ -1335,7 +1222,7 @@ void intlGUIEditBox::setTextRect(s32 line)
 
 s32 intlGUIEditBox::getLineFromPos(s32 pos)
 {
-       if (!WordWrap && !MultiLine)
+       if (!m_word_wrap && !m_multiline)
                return 0;
 
        s32 i=0;
@@ -1351,7 +1238,7 @@ s32 intlGUIEditBox::getLineFromPos(s32 pos)
 
 void intlGUIEditBox::inputChar(wchar_t c)
 {
-       if (!IsEnabled)
+       if (!IsEnabled || !m_writable)
                return;
 
        if (c != 0)
@@ -1394,7 +1281,7 @@ void intlGUIEditBox::inputChar(wchar_t c)
 
 void intlGUIEditBox::calculateScrollPos()
 {
-       if (!AutoScroll)
+       if (!m_autoscroll)
                return;
 
        // calculate horizontal scroll position
@@ -1402,18 +1289,18 @@ void intlGUIEditBox::calculateScrollPos()
        setTextRect(cursLine);
 
        // don't do horizontal scrolling when wordwrap is enabled.
-       if (!WordWrap)
+       if (!m_word_wrap)
        {
                // get cursor position
                IGUISkin* skin = Environment->getSkin();
                if (!skin)
                        return;
-               IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
+               IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
                if (!font)
                        return;
 
-               core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
-               s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
+               core::stringw *txtLine = m_multiline ? &BrokenText[cursLine] : &Text;
+               s32 cPos = m_multiline ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
 
                s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
                        font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
@@ -1430,7 +1317,7 @@ void intlGUIEditBox::calculateScrollPos()
                // todo: adjust scrollbar
        }
 
-       if (!WordWrap && !MultiLine)
+       if (!m_word_wrap && !m_multiline)
                return;
 
        // vertical scroll position
@@ -1475,8 +1362,8 @@ void intlGUIEditBox::createVScrollBar()
 {
        s32 fontHeight = 1;
 
-       if (OverrideFont) {
-               fontHeight = OverrideFont->getDimension(L"").Height;
+       if (m_override_font) {
+               fontHeight = m_override_font->getDimension(L"").Height;
        } else {
                if (IGUISkin* skin = Environment->getSkin()) {
                        if (IGUIFont* font = skin->getFont()) {
@@ -1487,7 +1374,9 @@ void intlGUIEditBox::createVScrollBar()
 
        irr::core::rect<s32> scrollbarrect = FrameRect;
        scrollbarrect.UpperLeftCorner.X += FrameRect.getWidth() - m_scrollbar_width;
-       m_vscrollbar = Environment->addScrollBar(false, scrollbarrect, getParent(), getID());
+       m_vscrollbar = new GUIScrollBar(Environment, getParent(), -1,
+                       scrollbarrect, false, true);
+
        m_vscrollbar->setVisible(false);
        m_vscrollbar->setSmallStep(3 * fontHeight);
        m_vscrollbar->setLargeStep(10 * fontHeight);
@@ -1509,6 +1398,7 @@ void intlGUIEditBox::updateVScrollBar()
                if (scrollymax != m_vscrollbar->getMax()) {
                        // manage a newline or a deleted line
                        m_vscrollbar->setMax(scrollymax);
+                       m_vscrollbar->setPageSize(s32(getTextDimension().Height));
                        calculateScrollPos();
                } else {
                        // manage a newline or a deleted line
@@ -1521,9 +1411,10 @@ void intlGUIEditBox::updateVScrollBar()
                s32 scrollymax = getTextDimension().Height - FrameRect.getHeight();
                if (scrollymax != m_vscrollbar->getMax()) {
                        m_vscrollbar->setMax(scrollymax);
+                       m_vscrollbar->setPageSize(s32(getTextDimension().Height));
                }
 
-               if (!m_vscrollbar->isVisible() && MultiLine) {
+               if (!m_vscrollbar->isVisible() && m_multiline) {
                        AbsoluteRect.LowerRightCorner.X -= m_scrollbar_width;
 
                        m_vscrollbar->setVisible(true);
@@ -1535,6 +1426,7 @@ void intlGUIEditBox::updateVScrollBar()
                        VScrollPos = 0;
                        m_vscrollbar->setPos(0);
                        m_vscrollbar->setMax(1);
+                       m_vscrollbar->setPageSize(s32(getTextDimension().Height));
                        m_vscrollbar->setVisible(false);
                }
        }
@@ -1550,20 +1442,20 @@ void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeRea
 {
        // IGUIEditBox::serializeAttributes(out,options);
 
-       out->addBool  ("OverrideColorEnabled",OverrideColorEnabled );
-       out->addColor ("OverrideColor",       OverrideColor);
-       // out->addFont("OverrideFont",OverrideFont);
-       out->addInt   ("MaxChars",            Max);
-       out->addBool  ("WordWrap",            WordWrap);
-       out->addBool  ("MultiLine",           MultiLine);
-       out->addBool  ("AutoScroll",          AutoScroll);
-       out->addBool  ("PasswordBox",         PasswordBox);
+       out->addBool  ("OverrideColorEnabled", m_override_color_enabled );
+       out->addColor ("OverrideColor",        m_override_color);
+       // out->addFont("OverrideFont",m_override_font);
+       out->addInt   ("MaxChars",             Max);
+       out->addBool  ("WordWrap",             m_word_wrap);
+       out->addBool  ("MultiLine",            m_multiline);
+       out->addBool  ("AutoScroll",           m_autoscroll);
+       out->addBool  ("PasswordBox",          PasswordBox);
        core::stringw ch = L" ";
        ch[0] = PasswordChar;
-       out->addString("PasswordChar",        ch.c_str());
-       out->addEnum  ("HTextAlign",          HAlign, GUIAlignmentNames);
-       out->addEnum  ("VTextAlign",          VAlign, GUIAlignmentNames);
-       out->addBool  ("Writable",            m_writable);
+       out->addString("PasswordChar",         ch.c_str());
+       out->addEnum  ("HTextAlign",           HAlign, GUIAlignmentNames);
+       out->addEnum  ("VTextAlign",           VAlign, GUIAlignmentNames);
+       out->addBool  ("Writable",             m_writable);
 
        IGUIEditBox::serializeAttributes(out,options);
 }
@@ -1597,5 +1489,3 @@ void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeRe
 
 } // end namespace gui
 } // end namespace irr
-
-#endif // _IRR_COMPILE_WITH_GUI_