summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gui/chat.cpp78
-rw-r--r--src/gui/chat.h10
2 files changed, 88 insertions, 0 deletions
diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp
index 0f298436..c97f4b65 100644
--- a/src/gui/chat.cpp
+++ b/src/gui/chat.cpp
@@ -28,6 +28,11 @@
#include "../main.h"
#include <iostream>
+/** History */
+#define HIST_LEN 10
+static char *buf[HIST_LEN];
+static int f,s,t;
+
ChatWindow::ChatWindow(const char *logfile, int item_num):
Window("")
{
@@ -371,3 +376,76 @@ std::string ChatWindow::cut_string(std::string& value, unsigned int maximumLengt
return std::string("");
}
+
+void ChatWindow::update_history(const char *ptr)
+{
+ f = t;
+ if(*ptr == 0) return;
+
+ // prevent duplicates
+ if(f != s && strcmp(ptr, buf[(f + HIST_LEN -1) % HIST_LEN]) == 0) return;
+
+ buf[f] = strdup(ptr);
+ f = ( f + 1) % HIST_LEN;
+
+ if(f == s) {
+ free(buf[f]);
+ buf[s] = 0;
+ s = (s + 1) % HIST_LEN;
+ }
+
+ t = f;
+}
+
+void ChatWindow::arrow_up(void)
+{
+ const char *ptr;
+
+ ptr = chatInput->getText().c_str();
+
+ if(*ptr) {
+ if(t == f || strcmp(ptr, buf[t]) != 0) {
+ update_history(ptr);
+ t = (f + HIST_LEN -1) % HIST_LEN;
+ }
+ }
+
+ if(t != s)
+ t = (t + HIST_LEN -1) % HIST_LEN;
+ if(buf[t])
+ update_history(buf[t]);
+ else
+ update_history("");
+}
+
+void ChatWindow::arrow_down(void)
+{
+ const char *ptr;
+
+ ptr = chatInput->getText().c_str();
+
+ if(*ptr) {
+ if(t == f || strcmp(ptr, buf[t]) != 0) {
+ update_history(ptr);
+ t = (f + HIST_LEN -1) % HIST_LEN;
+ }
+ }
+
+ if(t != f)
+ t = (t + 1) % HIST_LEN;
+
+ if(buf[t])
+ update_history(buf[t]);
+ else
+ update_history("");
+}
+
+void ChatWindow::keyPress(const gcn::Key& key)
+{
+ if(key.getValue() == key.DOWN)
+ arrow_down();
+ else if(key.getValue() == key.UP)
+ arrow_up();
+
+ chatInput->setText(std::string(buf[t]));
+}
diff --git a/src/gui/chat.h b/src/gui/chat.h
index 53891f7d..574accab 100644
--- a/src/gui/chat.h
+++ b/src/gui/chat.h
@@ -25,6 +25,7 @@
#define _TMW_CHAT_H
#include <guichan.hpp>
+#include <guichan/key.hpp>
#include "../resources/image.h"
#include "../net/network.h"
#include "window.h"
@@ -169,6 +170,14 @@ class ChatWindow : public Window, public gcn::ActionListener {
*/
char *chat_send(std::string nick, std::string msg);
+ /** History */
+ void update_history(const char *ptr);
+ void arrow_up(void);
+ void arrow_down(void);
+
+ /** Called when key is pressed */
+ void keyPress(const gcn::Key& key);
+
private :
std::ofstream chatlog_file;
@@ -201,6 +210,7 @@ class ChatWindow : public Window, public gcn::ActionListener {
gcn::TextField *chatInput;
gcn::TextBox *textOutput;
ScrollArea *scrollArea;
+
};
#endif