diff options
author | Mateusz Kaduk <mateusz.kaduk@gmail.com> | 2005-04-28 20:39:57 +0000 |
---|---|---|
committer | Mateusz Kaduk <mateusz.kaduk@gmail.com> | 2005-04-28 20:39:57 +0000 |
commit | 120e0cb592d289132f504778f471195c12a196d5 (patch) | |
tree | 3221cdccacccc1cb6158f8c6c9edc75815cba259 /src | |
parent | fd193321df49fe33697a77f6721b4e1fc13fe05b (diff) | |
download | mana-120e0cb592d289132f504778f471195c12a196d5.tar.gz mana-120e0cb592d289132f504778f471195c12a196d5.tar.bz2 mana-120e0cb592d289132f504778f471195c12a196d5.tar.xz mana-120e0cb592d289132f504778f471195c12a196d5.zip |
First step to chat history
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/chat.cpp | 78 | ||||
-rw-r--r-- | src/gui/chat.h | 10 |
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 |