From 3dacb9b09622375d35d704c8ac9d7039d506026f Mon Sep 17 00:00:00 2001
From: Bjørn Lindeijer <bjorn@lindeijer.nl>
Date: Sat, 9 May 2009 19:33:22 +0200
Subject: Insert player and item names at caret position instead of at the end

An improvement based on the 0.0.28.Q version.
---
 src/gui/chat.cpp | 23 +++++++++++++++++------
 src/gui/chat.h   |  8 ++++----
 2 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp
index 3d0cb766..11a327d8 100644
--- a/src/gui/chat.cpp
+++ b/src/gui/chat.cpp
@@ -44,6 +44,8 @@
 #include <guichan/focushandler.hpp>
 #include <guichan/focuslistener.hpp>
 
+#include <sstream>
+
 /**
  * The chat input hides when it loses focus. It is also invisible by default.
  */
@@ -336,7 +338,7 @@ void ChatWindow::doPresent()
         time(&t);
 
         // Format the time string properly
-        std::stringstream timeStr;
+        std::ostringstream timeStr;
         timeStr << "[" << ((((t / 60) / 60) % 24 < 10) ? "0" : "")
             << (int) (((t / 60) / 60) % 24)
             << ":" << (((t / 60) % 60 < 10) ? "0" : "")
@@ -359,7 +361,8 @@ void ChatWindow::scroll(int amount)
         return;
 
     ChatTab *tab = getFocused();
-    if (tab) tab->scroll(amount);
+    if (tab)
+        tab->scroll(amount);
 }
 
 void ChatWindow::keyPressed(gcn::KeyEvent &event)
@@ -397,16 +400,24 @@ void ChatWindow::keyPressed(gcn::KeyEvent &event)
     }
 }
 
-void ChatWindow::addInputText(std::string input_str)
+void ChatWindow::addInputText(const std::string &text)
 {
-     mChatInput->setText(mChatInput->getText() + input_str + " ");
-     requestChatFocus();
+    const int caretPos = mChatInput->getCaretPosition();
+    const std::string inputText = mChatInput->getText();
+
+    std::ostringstream ss;
+    ss << inputText.substr(0, caretPos) << text << " ";
+    ss << inputText.substr(caretPos);
+
+    mChatInput->setText(ss.str());
+    mChatInput->setCaretPosition(caretPos + text.length() + 1);
+    requestChatFocus();
 }
 
 void ChatWindow::addItemText(const std::string &item)
 {
     std::ostringstream text;
-    text << "[" << item << "] ";
+    text << "[" << item << "]";
     addInputText(text.str());
 }
 
diff --git a/src/gui/chat.h b/src/gui/chat.h
index efd0b02e..c6e8e326 100644
--- a/src/gui/chat.h
+++ b/src/gui/chat.h
@@ -91,12 +91,12 @@ class ChatWindow : public Window,
         /**
          * Gets the focused tab.
          */
-        ChatTab* getFocused() const;
+        ChatTab *getFocused() const;
 
         /**
          * Clear the given tab.
          */
-        void clearTab(ChatTab* tab);
+        void clearTab(ChatTab *tab);
 
         /**
          * Clear the current tab.
@@ -141,8 +141,8 @@ class ChatWindow : public Window,
         /** Called when key is pressed */
         void keyPressed(gcn::KeyEvent &event);
 
-        /** Add the given text to the chat input */
-        void addInputText(std::string input_str);
+        /** Add the given text to the chat input. */
+        void addInputText(const std::string &text);
 
         /** Called to add item to chat */
         void addItemText(const std::string &item);
-- 
cgit v1.2.3-70-g09d2