summaryrefslogtreecommitdiff
path: root/src/gui/chat.cpp
diff options
context:
space:
mode:
authorMateusz Kaduk <mateusz.kaduk@gmail.com>2005-04-28 20:39:57 +0000
committerMateusz Kaduk <mateusz.kaduk@gmail.com>2005-04-28 20:39:57 +0000
commit120e0cb592d289132f504778f471195c12a196d5 (patch)
tree3221cdccacccc1cb6158f8c6c9edc75815cba259 /src/gui/chat.cpp
parentfd193321df49fe33697a77f6721b4e1fc13fe05b (diff)
downloadmana-120e0cb592d289132f504778f471195c12a196d5.tar.gz
mana-120e0cb592d289132f504778f471195c12a196d5.tar.bz2
mana-120e0cb592d289132f504778f471195c12a196d5.tar.xz
mana-120e0cb592d289132f504778f471195c12a196d5.zip
First step to chat history
Diffstat (limited to 'src/gui/chat.cpp')
-rw-r--r--src/gui/chat.cpp78
1 files changed, 78 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]));
+}