summaryrefslogtreecommitdiffstats
path: root/src/ui
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2013-02-25 22:33:35 +0100
committerSebastian Reichel <sre@ring0.de>2013-02-25 22:33:35 +0100
commit21e7d9260ebf66a70dc09cd912a0a5aa32abcebd (patch)
treeae15bbc2879a17a0c6c8c76413b71ad46d8659a9 /src/ui
parent72c29aef1e7ac67149e49d362093fea956edd6bf (diff)
downloadserial-barcode-scanner-21e7d9260ebf66a70dc09cd912a0a5aa32abcebd.tar.bz2
new curses user interface
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/clock.vala61
-rw-r--r--src/ui/curses-ui.vala68
-rw-r--r--src/ui/dialog.vala36
-rw-r--r--src/ui/logo.vala30
-rw-r--r--src/ui/message_box.vala45
-rw-r--r--src/ui/numbers.vala98
-rw-r--r--src/ui/status.vala24
7 files changed, 362 insertions, 0 deletions
diff --git a/src/ui/clock.vala b/src/ui/clock.vala
new file mode 100644
index 0000000..e38ad9c
--- /dev/null
+++ b/src/ui/clock.vala
@@ -0,0 +1,61 @@
+using Curses;
+
+public class ClockWindow {
+ AsciiNumbers ascii;
+ Window win;
+
+ public ClockWindow() {
+ ascii = new AsciiNumbers();
+ win = new Window(6, 18, 1, COLS-2-18);
+ win.bkgdset(COLOR_PAIR(0) | Attribute.BOLD);
+
+ win.clrtobot();
+ win.box(0, 0);
+
+ win.refresh();
+ }
+
+ public void update() {
+ string[] x;
+ var now = new DateTime.now_local();
+
+ x = ascii.get('0' + (char) (now.get_hour() / 10));
+ win.mvaddstr(1,1, x[0]);
+ win.mvaddstr(2,1, x[1]);
+ win.mvaddstr(3,1, x[2]);
+
+ x = ascii.get('0' + (char) (now.get_hour() % 10));
+ win.mvaddstr(1,4, x[0]);
+ win.mvaddstr(2,4, x[1]);
+ win.mvaddstr(3,4, x[2]);
+
+ x = ascii.get(':');
+ win.mvaddstr(1,7, x[0]);
+ win.mvaddstr(2,7, x[1]);
+ win.mvaddstr(3,7, x[2]);
+
+ x = ascii.get('0' + (char) (now.get_minute() / 10));
+ win.mvaddstr(1,10, x[0]);
+ win.mvaddstr(2,10, x[1]);
+ win.mvaddstr(3,10, x[2]);
+
+ x = ascii.get('0' + (char) (now.get_minute() % 10));
+ win.mvaddstr(1,13, x[0]);
+ win.mvaddstr(2,13, x[1]);
+ win.mvaddstr(3,13, x[2]);
+
+
+ win.clrtobot();
+ win.box(0, 0);
+
+ win.mvaddstr(5,4, now.format("%Y-%m-%d"));
+
+ win.refresh();
+ }
+
+ public void redraw() {
+ win.touchwin();
+ win.refresh();
+ }
+
+}
diff --git a/src/ui/curses-ui.vala b/src/ui/curses-ui.vala
new file mode 100644
index 0000000..6676eea
--- /dev/null
+++ b/src/ui/curses-ui.vala
@@ -0,0 +1,68 @@
+public class CursesUI {
+ MessageBox messages;
+ Dialog dialog;
+ Logo banner;
+ ClockWindow clkwin;
+ StatusPanel statuswin;
+
+ public CursesUI() {
+ /* unicode support */
+ Intl.setlocale(LocaleCategory.CTYPE, "");
+
+ /* initialize curses */
+ Curses.initscr();
+
+ /* disable cursor */
+ Curses.curs_set(0);
+
+ /* initialize color mode and define color pairs */
+ Curses.start_color();
+ Curses.init_pair(0, Curses.Color.WHITE, Curses.Color.BLACK);
+ Curses.init_pair(1, Curses.Color.GREEN, Curses.Color.BLACK);
+ Curses.init_pair(2, Curses.Color.WHITE, Curses.Color.RED);
+
+ /* initialize widgets */
+ banner = new Logo();
+ statuswin = new StatusPanel();
+ messages = new MessageBox();
+ clkwin = new ClockWindow();
+
+ clkwin.update();
+
+ Timeout.add_seconds(10, update_time);
+ }
+
+ ~CursesUI() {
+ exit();
+ }
+
+ public void exit() {
+ /* Reset the terminal mode */
+ Curses.endwin();
+ }
+
+ bool update_time() {
+ clkwin.update();
+ return true;
+ }
+
+ public void status(string message) {
+ statuswin.set(message);
+ }
+
+ public void log(string message) {
+ messages.add(message);
+ }
+
+ public void dialog_open(string title, string message) {
+ dialog = new Dialog(message, title);
+ }
+
+ public void dialog_close() {
+ dialog = null;
+ messages.redraw();
+ banner.redraw();
+ clkwin.redraw();
+ statuswin.redraw();
+ }
+}
diff --git a/src/ui/dialog.vala b/src/ui/dialog.vala
new file mode 100644
index 0000000..29782e5
--- /dev/null
+++ b/src/ui/dialog.vala
@@ -0,0 +1,36 @@
+using Curses;
+
+public class Dialog {
+ Window win;
+ Window subwin;
+
+ public Dialog(string message, string title = "KtT Shopsystem Error", int h=16, int w=60)
+ requires (title.length <= w-4)
+ {
+ int y = LINES/2-h/2;
+ int x = COLS/2-w/2;
+
+ int title_x = (w-title.length)/2;
+
+ win = new Window(h, w, y, x);
+
+ /* make the dialog white on red */
+ win.bkgdset(COLOR_PAIR(2) | Attribute.BOLD);
+ win.clrtobot();
+
+ /* message subwindow */
+ subwin = win.derwin(h-4, w-4, 2, 2);
+ subwin.clrtobot();
+ subwin.printw(message);
+ subwin.refresh();
+
+ /* dialog title */
+ win.box(0,0);
+ win.mvaddstr(0, title_x, title);
+ win.mvaddch(0, title_x-2, Acs.RTEE);
+ win.mvaddch(0, title_x-1, ' ');
+ win.mvaddch(0, title_x+title.length, ' ');
+ win.mvaddch(0, title_x+title.length+1, Acs.LTEE);
+ win.refresh();
+ }
+}
diff --git a/src/ui/logo.vala b/src/ui/logo.vala
new file mode 100644
index 0000000..46fbaf7
--- /dev/null
+++ b/src/ui/logo.vala
@@ -0,0 +1,30 @@
+using Curses;
+
+public class Logo {
+ Window win;
+
+ public Logo() {
+ win = new Window(8, COLS - 2, 0, 1);
+ win.bkgdset(COLOR_PAIR(1) | Attribute.BOLD);
+
+ win.addstr("\n");
+ win.addstr(" _ ___ _____ ____ _ _ \n");
+ win.addstr(" | |/ / ||_ _| / ___|| |__ ___ _ __ ___ _ _ ___| |_ ___ _ __ ___ \n");
+ win.addstr(" | ' /| __|| | \\___ \\| '_ \\ / _ \\| '_ \\/ __| | | / __| __/ _ \\ '_ ` _ \\ \n");
+ win.addstr(" | . \\| |_ | | ___) | | | | (_) | |_) \\__ \\ |_| \\__ \\ || __/ | | | | |\n");
+ win.addstr(" |_|\\_\\\\__||_| |____/|_| |_|\\___/| .__/|___/\\__, |___/\\__\\___|_| |_| |_|\n");
+ win.addstr(" |_| |___/ \n");
+
+ win.clrtobot();
+
+ win.box(0, 0);
+
+ win.refresh();
+ }
+
+ public void redraw() {
+ win.touchwin();
+ win.refresh();
+ }
+
+}
diff --git a/src/ui/message_box.vala b/src/ui/message_box.vala
new file mode 100644
index 0000000..4f9c3f6
--- /dev/null
+++ b/src/ui/message_box.vala
@@ -0,0 +1,45 @@
+using Curses;
+
+public class MessageBox {
+ Window win;
+ Window subwin;
+ DateTime last;
+
+ public MessageBox() {
+ win = new Window(LINES-9, COLS - 2, 8, 1);
+ win.bkgdset(COLOR_PAIR(0));
+
+ win.clrtobot();
+ win.box(0, 0);
+ win.refresh();
+
+ subwin = win.derwin(LINES-11, COLS-4, 1, 1);
+ subwin.scrollok(true);
+ subwin.clrtobot();
+ subwin.refresh();
+
+ last = new DateTime.from_unix_utc(0);
+ }
+
+ public void add(string msg) {
+ var now = new DateTime.now_local();
+
+ if(now.get_day_of_year() != last.get_day_of_year() || now.get_year() != last.get_year()) {
+ string curtime = now.format("%Y-%m-%d");
+ subwin.addstr("\nDate Changed: " + curtime);
+ }
+
+ last = now;
+
+ string curtime = now.format("%H:%M:%S");
+ subwin.addstr("\n[" + curtime + "] " + msg);
+ subwin.refresh();
+ }
+
+ public void redraw() {
+ win.touchwin();
+ win.refresh();
+ subwin.touchwin();
+ subwin.refresh();
+ }
+}
diff --git a/src/ui/numbers.vala b/src/ui/numbers.vala
new file mode 100644
index 0000000..8ee00d5
--- /dev/null
+++ b/src/ui/numbers.vala
@@ -0,0 +1,98 @@
+public class AsciiNumbers {
+
+ public string[] zero = {
+ " _ ",
+ "/ \\",
+ "\\_/"
+ };
+
+ public string[] one = {
+ " ",
+ " /|",
+ " |"
+ };
+
+ public string[] two = {
+ "__ ",
+ " _)",
+ "(__"
+ };
+
+ public string[] three = {
+ "__ ",
+ " _)",
+ "__)"
+ };
+
+ public string[] four = {
+ " ",
+ "|_|",
+ " |"
+ };
+
+ public string[] five = {
+ " __",
+ "|_ ",
+ "__)"
+ };
+
+ public string[] six = {
+ " _ ",
+ "/_ ",
+ "\\_)"
+ };
+
+ public string[] seven = {
+ "___",
+ " /",
+ " / "
+ };
+
+ public string[] eight = {
+ " _ ",
+ "(_)",
+ "(_)"
+ };
+
+ public string[] nine = {
+ " _ ",
+ "(_\\",
+ " _/"
+ };
+
+ public string[] colon = {
+ " ",
+ " o ",
+ " o "
+ };
+
+ public string[] get(char c) {
+ switch(c) {
+ case '0':
+ return zero;
+ case '1':
+ return one;
+ case '2':
+ return two;
+ case '3':
+ return three;
+ case '4':
+ return four;
+ case '5':
+ return five;
+ case '6':
+ return six;
+ case '7':
+ return seven;
+ case '8':
+ return eight;
+ case '9':
+ return nine;
+ case ':':
+ return colon;
+ default:
+ return {};
+ }
+ }
+
+}
diff --git a/src/ui/status.vala b/src/ui/status.vala
new file mode 100644
index 0000000..db33820
--- /dev/null
+++ b/src/ui/status.vala
@@ -0,0 +1,24 @@
+using Curses;
+
+public class StatusPanel {
+ Window win;
+
+ public StatusPanel() {
+ win = new Window(1, COLS - 2, LINES-1, 1);
+ win.bkgdset(COLOR_PAIR(2) | Attribute.BOLD);
+
+ win.clrtobot();
+ win.refresh();
+ }
+
+ public void set(string msg) {
+ win.mvaddstr(0,1, msg);
+ win.clrtobot();
+ win.refresh();
+ }
+
+ public void redraw() {
+ win.touchwin();
+ win.refresh();
+ }
+}