summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/chat.cpp74
-rw-r--r--src/gui/chat.h11
-rw-r--r--src/gui/gui.h1
-rw-r--r--src/gui/npc_text.cpp29
-rw-r--r--src/gui/textbox.cpp73
-rw-r--r--src/gui/textbox.h5
6 files changed, 90 insertions, 103 deletions
diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp
index 84a12585..a0b027ae 100644
--- a/src/gui/chat.cpp
+++ b/src/gui/chat.cpp
@@ -41,6 +41,8 @@ ChatWindow::ChatWindow(const std::string &logfile):
chatInput = new ChatInput();
textOutput->setEditable(false);
scrollArea = new ScrollArea(textOutput);
+ scrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
+ scrollArea->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS);
scrollArea->setDimension(gcn::Rectangle(
2, 0, 596, 98 - chatInput->getHeight() - 5));
scrollArea->setOpaque(false);
@@ -101,42 +103,10 @@ void ChatWindow::chat_log(std::string line, int own)
line = tmp.nick + line;
- // A try to get text sentences no too long...
- bool finished = false;
- unsigned int maxLength = 97;
-
- while (!finished)
- {
- std::string tempText = line;
-
- if (line.length() > maxLength)
- {
- if (line.length() > maxLength) {
- line = cutString(tempText, maxLength);
- }
-
- //tmp.text = tempText;
-
- //chatlog_file << tmp.nick << tmp.text << "\n";
- //chatlog_file.flush();
-
- //chatlog.push_front(tmp);
- }
- else // Normal message
- {
- //tmp.text = line;
- //chatlog_file << tmp.nick << tmp.text << "\n";
- //chatlog_file.flush();
-
- //chatlog.push_front(tmp);
- finished = true;
- }
-
- textOutput->setText(
- textOutput->getText() + std::string("\n") + tempText);
- scrollArea->setVerticalScrollAmount(
- scrollArea->getVerticalMaxScroll());
- }
+ textOutput->setText(
+ textOutput->getText() + std::string("\n") + line);
+ scrollArea->setVerticalScrollAmount(
+ scrollArea->getVerticalMaxScroll());
}
void ChatWindow::chat_log(CHATSKILL action)
@@ -356,38 +326,6 @@ std::string ChatWindow::const_msg(CHATSKILL action)
return msg;
}
-std::string ChatWindow::cutString(std::string& value, unsigned int maximumLength)
-{
- // If the string exceeds the maximum length
- if (value.length() > maximumLength)
- {
- unsigned int index = 0;
- unsigned int lastSpace = 0;
- std::string cutOff = "";
-
- for (index = 0; index < maximumLength; index++) {
- if (value.at(index) == ' ') {
- lastSpace = index;
- }
- }
-
- // If the last space is at the beginning of the string
- if (lastSpace == 0) {
- // Just cut it right off from the end
- cutOff = value.substr(maximumLength);
- value = value.substr(0, maximumLength);
- } else {
- // Cut it off from the last space forward
- cutOff = value.substr(lastSpace + 1);
- value = value.substr(0, lastSpace);
- }
-
- return cutOff;
- }
-
- return std::string("");
-}
-
void ChatWindow::keyPress(const gcn::Key &key)
{
if (key.getValue() == key.DOWN && curHist != history.end())
diff --git a/src/gui/chat.h b/src/gui/chat.h
index f242e3ee..28955ecf 100644
--- a/src/gui/chat.h
+++ b/src/gui/chat.h
@@ -196,17 +196,6 @@ class ChatWindow : public Window, public gcn::ActionListener,
/** Constructs failed messages for actions */
std::string const_msg(CHATSKILL);
- /**
- * Cuts a string into two on a per word basis.
- *
- * @param value The string to be cut, it may be modified
- * in the function.
- * @param maximumLength The length after which the string
- * should be cut.
- * @return The cut off section of the string
- */
- std::string cutString(std::string& value, unsigned int maximumLength);
-
gcn::TextField *chatInput; /**< Input box for typing chat messages */
gcn::TextBox *textOutput; /**< Text box for displaying chat history */
ScrollArea *scrollArea; /**< Scroll area around text output */
diff --git a/src/gui/gui.h b/src/gui/gui.h
index 057753d4..1c8ccb34 100644
--- a/src/gui/gui.h
+++ b/src/gui/gui.h
@@ -77,7 +77,6 @@ class Gui : public gcn::Gui, public gcn::MouseListener
#endif
gcn::ImageLoader *imageLoader; /**< For loading images */
gcn::ImageFont *guiFont; /**< The global GUI font */
- gcn::FocusHandler *focusHandler; /**< The focus handler */
};
extern Gui *gui; /**< The GUI system */
diff --git a/src/gui/npc_text.cpp b/src/gui/npc_text.cpp
index b6bd962f..7fc59343 100644
--- a/src/gui/npc_text.cpp
+++ b/src/gui/npc_text.cpp
@@ -37,6 +37,8 @@ NpcTextDialog::NpcTextDialog():
okButton = new Button("OK");
setContentSize(260, 175);
+ scrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
+ scrollArea->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS);
scrollArea->setDimension(gcn::Rectangle(
5, 5, 250, 160 - okButton->getHeight()));
okButton->setPosition(
@@ -61,32 +63,13 @@ NpcTextDialog::~NpcTextDialog()
void NpcTextDialog::setText(const char *text)
{
- std::string tmp = "";
- int lineWidth = 0;
- int w = scrollArea->getWidth();
- for (unsigned int i = 0; i < strlen(text); i++)
- {
- if (text[i] != '\n')
- {
- std::string tmpChar = ""; tmpChar += text[i];
- lineWidth += getFont()->getWidth(tmpChar);
- if (lineWidth > w) {
- tmp += '\n';
- lineWidth = 0;
- }
- } else
- {
- lineWidth = 0;
- }
- tmp += text[i];
- }
- textBox->setText(tmp);
+ textBox->setText(text);
}
void NpcTextDialog::addText(const char *text)
{
- std::string tmp = textBox->getText() + std::string(text) + std::string("\n");
- setText(tmp.c_str());
+ textBox->setText(
+ textBox->getText() + std::string(text) + std::string("\n"));
}
void NpcTextDialog::action(const std::string& eventId)
@@ -96,5 +79,5 @@ void NpcTextDialog::action(const std::string& eventId)
WFIFOSET(6);
setText("");
setVisible(false);
- current_npc = 0;
+ current_npc = 0;
}
diff --git a/src/gui/textbox.cpp b/src/gui/textbox.cpp
index a620f29f..06ee3598 100644
--- a/src/gui/textbox.cpp
+++ b/src/gui/textbox.cpp
@@ -22,6 +22,7 @@
*/
#include "textbox.h"
+#include <sstream>
TextBox::TextBox():
gcn::TextBox()
@@ -36,3 +37,75 @@ TextBox::TextBox(const std::string& text):
setOpaque(false);
setBorderSize(0);
}
+
+void TextBox::setText(const std::string &text)
+{
+ // Make sure parent scroll area sets width of this widget
+ if (getParent())
+ {
+ getParent()->logic();
+ }
+
+ std::stringstream wrappedStream;
+ std::string::size_type newlinePos, lastNewlinePos = 0;
+
+ do
+ {
+ // Determine next piece of string to wrap
+ newlinePos = text.find("\n", lastNewlinePos);
+
+ if (newlinePos == std::string::npos)
+ {
+ newlinePos = text.size();
+ }
+
+ std::string line =
+ text.substr(lastNewlinePos, newlinePos - lastNewlinePos);
+ std::string::size_type spacePos, lastSpacePos = 0;
+ int xpos = 0;
+
+ do
+ {
+ spacePos = line.find(" ", lastSpacePos);
+
+ if (spacePos == std::string::npos)
+ {
+ spacePos = line.size();
+ }
+
+ std::string word =
+ line.substr(lastSpacePos, spacePos - lastSpacePos);
+
+ int width = getFont()->getWidth(word);
+
+ if (xpos != 0 && xpos + width < getWidth())
+ {
+ xpos += width + getFont()->getWidth(" ");
+ wrappedStream << " " << word;
+ }
+ else if (lastSpacePos == 0)
+ {
+ xpos += width;
+ wrappedStream << word;
+ }
+ else
+ {
+ xpos = width;
+ wrappedStream << "\n" << word;
+ }
+
+ lastSpacePos = spacePos + 1;
+ }
+ while (spacePos != line.size());
+
+ if (text.find("\n", lastNewlinePos) != std::string::npos)
+ {
+ wrappedStream << "\n";
+ }
+
+ lastNewlinePos = newlinePos + 1;
+ }
+ while (newlinePos != text.size());
+
+ gcn::TextBox::setText(wrappedStream.str());
+}
diff --git a/src/gui/textbox.h b/src/gui/textbox.h
index 8f5d910f..5c315d15 100644
--- a/src/gui/textbox.h
+++ b/src/gui/textbox.h
@@ -44,6 +44,11 @@ class TextBox : public gcn::TextBox {
* Constructor.
*/
TextBox(const std::string& text);
+
+ /**
+ * Sets the text after wrapping it to the current width of the widget.
+ */
+ void setText(const std::string &text);
};
#endif