summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaromil <jaromil@dyne.org>2009-02-07 11:35:47 (GMT)
committer Jaromil <jaromil@dyne.org>2009-02-07 11:35:47 (GMT)
commita2fd902600d7e7ac352a2e13454f6758a6f9bcca (patch)
treec0861dd32b776749757065559543a345ea5e74fd
parent7246c6d520dbe23fed70650db92cbb429fffa0cc (diff)
slang widget for logging
plus some fixes and an uncomplete prompt widget
-rw-r--r--slw/Makefile10
-rw-r--r--slw/abs_text_console.cpp1
-rw-r--r--slw/slw_log.cpp211
-rw-r--r--slw/slw_log.h54
-rw-r--r--slw/slw_prompt.cpp95
-rw-r--r--slw/slw_prompt.h47
-rw-r--r--slw/slw_text.h3
-rw-r--r--slw/testlog.cpp52
8 files changed, 467 insertions, 6 deletions
diff --git a/slw/Makefile b/slw/Makefile
index c019a16..f9e793a 100644
--- a/slw/Makefile
+++ b/slw/Makefile
@@ -34,7 +34,8 @@ LIBS = -lslang
#CXXFLAGS = -Wall -ggdb -I. -I$(SLANGPATH)/src
#LIBS = $(SLANGPATH)/src/objs/libslang.a -ltermcap
-DEPS = slw_console.o slw.o slw_popup.o abs_text_console.o slw_text.o ../linklist.o ../jutils.o
+DEPS = slw_console.o slw.o slw_popup.o slw_log.o slw_prompt.o \
+ abs_text_console.o slw_text.o ../linklist.o ../jutils.o
# generic make rules
#%.o: %.cpp
@@ -42,7 +43,7 @@ DEPS = slw_console.o slw.o slw_popup.o abs_text_console.o slw_text.o ../linklist
#%: %.cpp
# $(CXX) $(CXXFLAGS) -o $@ $< $(DEPS) $(LIBS)
-all: libslw.a testkey
+all: libslw.a
depend:
mkdep $(CXXFLAGS) linklist.cpp jutils.cpp slw.cpp slw_console.cpp slw_text.cpp slw_popup.cpp abs_text_console.cpp
@@ -50,9 +51,10 @@ depend:
libslw.a: $(DEPS)
ar cru libslw.a $(DEPS)
-testkey: $(DEPS) testkey.o
+test: $(DEPS) testkey.o testlog.o
$(CXX) -o testkey $(DEPS) testkey.o $(LIBS)
+ $(CXX) -o testlog $(DEPS) testlog.o $(LIBS)
clean:
- rm -rf *.o *! libslw.a testkey
+ rm -rf *.o *! libslw.a testkey testlog
diff --git a/slw/abs_text_console.cpp b/slw/abs_text_console.cpp
index 6aefd8c..053fb90 100644
--- a/slw/abs_text_console.cpp
+++ b/slw/abs_text_console.cpp
@@ -29,6 +29,7 @@
#include <keycodes.h>
#include <abs_text_console.h>
+#define ROWCHUNK 128 // chunk to add/sub when modifying the text line
TextConsole::TextConsole() {
diff --git a/slw/slw_log.cpp b/slw/slw_log.cpp
new file mode 100644
index 0000000..22cf6dc
--- /dev/null
+++ b/slw/slw_log.cpp
@@ -0,0 +1,211 @@
+/* S-Lang console widgets
+ *
+ * (C) Copyright 2004-2008 Denis Rojo <jaromil@dyne.org>
+ *
+ * This source code is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Public License as published
+ * by the Free Software Foundation; either version 2 of the License,
+ * or (at your option) any later version.
+ *
+ * This source code is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * Please refer to the GNU Public License for more details.
+ *
+ * You should have received a copy of the GNU Public License along with
+ * this source code; if not, write to:
+ * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <jutils.h>
+
+#include <slw_log.h>
+
+
+SLW_Log::SLW_Log()
+ : SLangWidget() {
+
+ set_name("log");
+ textconsole = NULL;
+ scrolling = false;
+
+}
+
+
+SLW_Log::~SLW_Log() {
+ if(textconsole)
+ delete textconsole;
+}
+
+bool SLW_Log::init() {
+
+ if(!console) {
+ ::error("can't initialize widget '%s': not placed on console", name);
+ return false;
+ }
+
+ // default widget settings
+ initialized = true;
+ visible = true;
+ cursor = false;
+ can_focus = true;
+
+
+ // create the private structure where to hold text
+ if(textconsole) delete textconsole;
+ textconsole = new SLW_TextConsole();
+ textconsole->widget = this;
+ textconsole->w = w;
+ textconsole->h = h;
+ textconsole->cur_x = 0;
+ textconsole->cur_y = 0;
+
+
+ refresh();
+
+ return true;
+}
+
+bool SLW_Log::feed(int key) {
+ // interprets a keycode and perform the action (or write a letter)
+ switch(key) {
+ case 0: break;
+ case KEY_PAGE_UP:
+ if(textconsole->vis_row_in->prev) {
+ textconsole->cur_row = (Row*) textconsole->cur_row->prev;
+ textconsole->vis_row_in = (Row*) textconsole->vis_row_in->prev;
+ scrolling = true;
+ refresh();
+ }
+ break;
+ case KEY_PAGE_DOWN:
+ if(textconsole->cur_row->next) {
+ textconsole->cur_row = (Row*) textconsole->cur_row->next;
+ textconsole->vis_row_in = (Row*) textconsole->vis_row_in->next;
+ if(!textconsole->cur_row->next) scrolling = false;
+ refresh();
+ }
+ break;
+ default:
+ func("key [ %c : %u ] fed to log", key, key);
+ refresh();
+ }
+ // TODO: scrolling pg-up down
+ return true;
+}
+
+bool SLW_Log::refresh() {
+ Row *r;
+ register int c;
+
+ if(!textconsole->vis_row_in) return false;
+ else r = textconsole->vis_row_in;
+
+ // tell the renderer to blank the surface for a refresh
+ // this is a pure virtual function here
+ blank();
+
+ for(c = 0; c < h; c++ ) {
+
+ if(r->text)
+ putnch(r->text, 0, c, r->len);
+
+ if(!r->next) break;
+ else r = (Row*) r->next;
+
+ }
+
+ if(textconsole->cur_row != r) {
+ putnch((char*)"|",w-1,c-2,1);
+ putnch((char*)"V",w-1,c-1,1);
+ }
+ return true;
+}
+
+void SLW_Log::append(const char *text) {
+ int len = strlen(text);
+ Row *r = new Row();
+ r->insert_string((char*)text, len);
+ textconsole->rows.append( (Entry*) r );
+ if(!scrolling) textconsole->cur_row = r;
+ if(textconsole->cur_y < textconsole->h)
+ textconsole->cur_y++;
+ if(!scrolling & (textconsole->cur_y >= textconsole->h) )
+ textconsole->vis_row_in = (Row*) textconsole->vis_row_in->next;
+}
+
+
+void SLW_Log::notice(const char *text, ...) {
+char msg[255];
+ va_list arg;
+ va_start(arg, text);
+
+ // TODO: colors
+ vsnprintf(msg, 254, text, arg);
+
+ append(msg);
+
+ va_end(arg);
+}
+
+void SLW_Log::error(const char *text, ...) {
+char msg[255];
+ va_list arg;
+ va_start(arg, text);
+
+ // TODO: colors
+ vsnprintf(msg, 254, text, arg);
+
+ append(msg);
+
+ va_end(arg);
+}
+
+void SLW_Log::act(const char *text, ...) {
+char msg[255];
+ va_list arg;
+ va_start(arg, text);
+
+ // TODO: colors
+ vsnprintf(msg, 254, text, arg);
+
+ append(msg);
+
+ va_end(arg);
+}
+
+
+void SLW_Log::warn(const char *text, ...) {
+char msg[255];
+ va_list arg;
+ va_start(arg, text);
+
+ // TODO: colors
+ vsnprintf(msg, 254, text, arg);
+
+ append(msg);
+
+ va_end(arg);
+}
+
+void SLW_Log::func(const char *text, ...) {
+char msg[255];
+ va_list arg;
+ va_start(arg, text);
+
+ // TODO: colors
+ vsnprintf(msg, 254, text, arg);
+
+ append(msg);
+
+ va_end(arg);
+}
+
+
+
+
diff --git a/slw/slw_log.h b/slw/slw_log.h
new file mode 100644
index 0000000..fb94319
--- /dev/null
+++ b/slw/slw_log.h
@@ -0,0 +1,54 @@
+/* S-Lang console widgets
+ *
+ * (C) Copyright 2004-2008 Denis Rojo <jaromil@dyne.org>
+ *
+ * This source code is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Public License as published
+ * by the Free Software Foundation; either version 2 of the License,
+ * or (at your option) any later version.
+ *
+ * This source code is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * Please refer to the GNU Public License for more details.
+ *
+ * You should have received a copy of the GNU Public License along with
+ * this source code; if not, write to:
+ * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#ifndef __SLW_LOG_H__
+#define __SLW_LOG_H__
+
+#include <slw.h>
+
+#include <slw_text.h>
+
+
+class SLW_Log : public SLangWidget {
+
+ public:
+ SLW_Log();
+ ~SLW_Log();
+
+ // pure virtual functions from parent class
+ bool init();
+ bool feed(int key);
+ bool refresh();
+ ////////////////////////
+
+ void notice(const char *text, ...);
+ void error(const char *text, ...);
+ void act(const char *text, ...);
+ void warn(const char *text, ...);
+ void func(const char *text, ...);
+
+ private:
+ SLW_TextConsole *textconsole;
+ void append(const char *text);
+ bool scrolling;
+
+};
+
+#endif
diff --git a/slw/slw_prompt.cpp b/slw/slw_prompt.cpp
new file mode 100644
index 0000000..9c24e52
--- /dev/null
+++ b/slw/slw_prompt.cpp
@@ -0,0 +1,95 @@
+/* S-Lang console widgets
+ *
+ * (C) Copyright 2004-2008 Denis Rojo <jaromil@dyne.org>
+ *
+ * This source code is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Public License as published
+ * by the Free Software Foundation; either version 2 of the License,
+ * or (at your option) any later version.
+ *
+ * This source code is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * Please refer to the GNU Public License for more details.
+ *
+ * You should have received a copy of the GNU Public License along with
+ * this source code; if not, write to:
+ * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <jutils.h>
+
+#include <slw_prompt.h>
+
+
+SLW_Prompt::SLW_Prompt()
+ : SLangWidget() {
+
+ set_name("prompt");
+ textconsole = NULL;
+
+}
+
+
+SLW_Prompt::~SLW_Prompt() {
+ if(textconsole)
+ delete textconsole;
+}
+
+bool SLW_Prompt::init() {
+
+ if(!console) {
+ ::error("can't initialize widget '%s': not placed on console", name);
+ return false;
+ }
+
+ // default widget settings
+ initialized = true;
+ visible = true;
+ cursor = false;
+ can_focus = true;
+
+
+ // create the private structure where to hold text
+ if(textconsole) delete textconsole;
+ textconsole = new SLW_TextConsole();
+ textconsole->widget = this;
+ textconsole->w = w;
+ textconsole->h = h;
+ textconsole->cur_x = 0;
+ textconsole->cur_y = 0;
+
+
+ refresh();
+
+ return true;
+}
+
+bool SLW_Prompt::feed(int key) {
+ switch(key) {
+ case 0: return false;
+ default:
+ textconsole->cur_row->insert_char(key);
+ textconsole->cur_x++;
+ }
+ refresh();
+ return true;
+}
+
+bool SLW_Prompt::refresh() {
+ Row *r = textconsole->cur_row;
+
+ blank_row(1);
+
+ putnch(r->text, 0, 0, r->len);
+
+ return true;
+}
+
+
+
diff --git a/slw/slw_prompt.h b/slw/slw_prompt.h
new file mode 100644
index 0000000..263177e
--- /dev/null
+++ b/slw/slw_prompt.h
@@ -0,0 +1,47 @@
+/* S-Lang console widgets
+ *
+ * (C) Copyright 2004-2008 Denis Rojo <jaromil@dyne.org>
+ *
+ * This source code is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Public License as published
+ * by the Free Software Foundation; either version 2 of the License,
+ * or (at your option) any later version.
+ *
+ * This source code is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * Please refer to the GNU Public License for more details.
+ *
+ * You should have received a copy of the GNU Public License along with
+ * this source code; if not, write to:
+ * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#ifndef __SLW_PROMPT_H__
+#define __SLW_PROMPT_H__
+
+#include <slw.h>
+
+#include <slw_text.h>
+
+
+class SLW_Prompt : public SLangWidget {
+
+ public:
+ SLW_Prompt();
+ ~SLW_Prompt();
+
+ // pure virtual functions from parent class
+ bool init();
+ bool feed(int key);
+ bool refresh();
+ ////////////////////////
+
+ private:
+ SLW_TextConsole *textconsole;
+ void append(const char *text);
+
+};
+
+#endif
diff --git a/slw/slw_text.h b/slw/slw_text.h
index c52b93e..132771c 100644
--- a/slw/slw_text.h
+++ b/slw/slw_text.h
@@ -24,7 +24,6 @@
#include <slw.h>
#include <abs_text_console.h>
-#define ROWCHUNK 128
class SLW_Text;
@@ -39,7 +38,7 @@ class SLW_TextConsole : public TextConsole {
void blank();
void blank_row(int r);
- SLW_Text *widget;
+ SLangWidget *widget;
};
class SLW_Text : public SLangWidget {
diff --git a/slw/testlog.cpp b/slw/testlog.cpp
new file mode 100644
index 0000000..1b568ee
--- /dev/null
+++ b/slw/testlog.cpp
@@ -0,0 +1,52 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+#include <slw.h>
+#include <slw_console.h>
+#include <slw_log.h>
+#include <slw_prompt.h>
+
+#include <jutils.h>
+
+
+SLangConsole con;
+SLW_Log log;
+SLW_Prompt prompt;
+
+int main(int argc, char **argv) {
+ int key;
+ bool quit;
+
+ assert( con.init() );
+
+ SLtt_set_cursor_visibility(0);
+
+ assert( con.place(&log, 1, 0, con.w, con.h-2) );
+ assert( log.init() );
+
+ assert( con.place(&prompt, 0, con.h-1, con.w, con.h) );
+ assert( prompt.init() );
+
+ con.focused = &prompt;
+
+ quit = false;
+
+ while(!quit) {
+
+ key = con.getkey();
+
+ prompt.feed(key);
+ log.feed(key);
+
+ if( ! con.refresh() ) quit = true;
+ }
+
+ con.close();
+
+ exit(1);
+}
+
+