#include "main.h"
#include "screen.h"
-/* Global data */
-int win_rows = 0;
-int win_cols = 0;
-
/* Static data */
-static WINDOW *win = NULL;
static FILE *debug_fd = NULL;
/* Control-C handler, so we don't hose the therminal */
exit(0);
}
-/* Window change */
-static void update(void)
-{
- getmaxyx(win, win_rows, win_cols);
- win_rows++;
- win_cols++;
- screen_draw();
-}
-
/* Window change */
static void on_sigwinch(int signum)
{
endwin();
refresh();
- update();
+ screen_draw();
}
/* Debugging functions */
{
int rval;
va_list ap;
+
+ /* Log to debug file */
va_start(ap, fmt);
vfprintf(debug_fd, "debug: ", ap);
rval = vfprintf(debug_fd, fmt, ap);
+
+ /* Log to status bar */
+ va_start(ap, fmt);
+ mvhline(LINES-2, 0, ACS_HLINE, COLS);
+ move(LINES-1, 0);
+ attron(COLOR_PAIR(COLOR_ERROR));
+ vwprintw(stdscr, fmt, ap);
+ attroff(COLOR_PAIR(COLOR_ERROR));
+ clrtoeol();
+
va_end(ap);
return rval;
}
debug("sigwinch error\n");
/* Curses setup */
- win = initscr();
+ initscr();
cbreak();
noecho();
+ keypad(stdscr, TRUE);
start_color();
curs_set(false);
+ mousemask(ALL_MOUSE_EVENTS, NULL);
+ init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK);
+ init_pair(COLOR_ERROR, COLOR_RED, COLOR_BLACK);
screen_init();
/* Run */
while (1) {
+ MEVENT btn;
int chr = getch();
+ if (chr == KEY_MOUSE)
+ if (getmouse(&btn) != OK)
+ continue;
if (chr == 'q')
break;
+ if (KEY_MOUSE)
+ //debug("mouse xyz=%d,%d,%d id=%hd state=%lx\n",
+ // btn.x, btn.y, btn.z, btn.id, btn.bstate);
switch (chr) {
case 'L':
clear();
case 'l':
- update();
+ screen_draw();
break;
default:
- screen_run(chr);
+ screen_run(chr, btn.bstate, btn.y, btn.x);
break;
}
}
+#define COLOR_TITLE 1
+#define COLOR_ERROR 2
+
#define N_ELEMENTS(x) (sizeof(x)/sizeof((x)[0]))
int debug(char *fmt, ...);
-
-extern int win_rows;
-extern int win_cols;
+#include <string.h>
#include <ncurses.h>
#include "main.h"
#include "screen.h"
char *name;
void (*init)(void);
void (*draw)(void);
- int (*run)(int);
+ int (*run)(int,mmask_t,int,int);
int keys[8];
} view_t;
{ "Help", help_init, help_draw, help_run, {KEY_F(8), '8', 'h', '?'} },
};
-view_t *active = &views[0];
+int active = 0;
/* Local functions */
void draw_header(void)
{
move(0, 0);
- attron(COLOR_PAIR(1));
+ attron(COLOR_PAIR(COLOR_TITLE));
for (int i = 0; i < N_ELEMENTS(views); i++) {
- if (active == &views[i])
+ if (i == active)
attron(A_BOLD);
printw(" %s", views[i].name);
- if (active == &views[i])
+ if (i == active)
attroff(A_BOLD);
}
- attroff(COLOR_PAIR(1));
- mvhline(1, 0, ACS_HLINE, win_cols);
+ attroff(COLOR_PAIR(COLOR_TITLE));
+ mvhline(1, 0, ACS_HLINE, COLS);
}
/* Screen init */
void screen_init(void)
{
- init_pair(1, COLOR_GREEN, COLOR_BLACK);
}
-/* Scren draw */
+/* Screen draw */
void screen_draw(void)
{
draw_header();
- active->draw();
+ views[active].draw();
+}
+
+/* Screen set */
+int screen_set(int num)
+{
+ if (active != num) {
+ active = num;
+ screen_draw();
+ }
+ return 1;
}
/* Screen run */
-int screen_run(int chr)
+int screen_run(int key, mmask_t btn, int row, int col)
{
+ /* Check for mouse events */
+ if (key == KEY_MOUSE && row == 0) {
+ int start = 1;
+ for (int i = 0; i < N_ELEMENTS(views); i++) {
+ int end = start + strlen(views[i].name) - 1;
+ if (start <= col && col <= end)
+ return screen_set(i);
+ start = end + 2;
+ }
+ }
+
/* Check for view change */
for (int i = 0; i < N_ELEMENTS(views); i++) {
- view_t *view = &views[i];
- if (view == active)
+ if (i == active)
continue;
- for (int j = 0; j < N_ELEMENTS(view->keys); j++)
- if (view->keys[j] == chr) {
- active = view;
- screen_draw();
- }
+ for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
+ if (views[i].keys[j] == key)
+ return screen_set(i);
+ }
+
+ /* Shift windows */
+ int num = active;
+ int shift = key == KEY_RIGHT ? +1 :
+ key == KEY_LEFT ? -1 : 0;
+ while (shift) {
+ num += shift;
+ num += N_ELEMENTS(views);
+ num %= N_ELEMENTS(views);
+ if (views[num].run)
+ return screen_set(num);
}
/* Pass key to active view */
- return active->run(chr);
+ return views[active].run(key, btn, row, col);
}
/* Screen functions */
void screen_init(void);
void screen_draw(void);
-int screen_run(int);
+int screen_run(int key, mmask_t btn, int row, int col);
/* View init functions */
void day_init(void);
void help_draw(void);
/* View run functions */
-int day_run(int);
-int week_run(int);
-int month_run(int);
-int year_run(int);
-int todo_run(int);
-int notes_run(int);
-int settings_run(int);
-int help_run(int);
+int day_run(int,mmask_t,int,int);
+int week_run(int,mmask_t,int,int);
+int month_run(int,mmask_t,int,int);
+int year_run(int,mmask_t,int,int);
+int todo_run(int,mmask_t,int,int);
+int notes_run(int,mmask_t,int,int);
+int settings_run(int,mmask_t,int,int);
+int help_run(int,mmask_t,int,int);
+#include <ncurses.h>
+
/* day init */
void day_init(void)
{
}
/* day run */
-int day_run(int chr)
+int day_run(int key, mmask_t btn, int row, int col)
{
return 0;
}
+#include <ncurses.h>
+
/* help init */
void help_init(void)
{
}
/* help run */
-int help_run(int chr)
+int help_run(int key, mmask_t btn, int row, int col)
{
return 0;
}
name=${file%.c}
tab=" "
cat > $file <<-EOF
+ #include <ncurses.h>
+
/* $name init */
void ${name}_init(void)
{
}
/* $name run */
- int ${name}_run(int chr)
+ int ${name}_run(int key, mmask_t btn, int row, int col)
{
${tab}return 0;
}
+#include <ncurses.h>
+
/* month init */
void month_init(void)
{
}
/* month run */
-int month_run(int chr)
+int month_run(int key, mmask_t btn, int row, int col)
{
return 0;
}
+#include <ncurses.h>
+
/* notes init */
void notes_init(void)
{
}
/* notes run */
-int notes_run(int chr)
+int notes_run(int key, mmask_t btn, int row, int col)
{
return 0;
}
+#include <ncurses.h>
+
/* settings init */
void settings_init(void)
{
}
/* settings run */
-int settings_run(int chr)
+int settings_run(int key, mmask_t btn, int row, int col)
{
return 0;
}
+#include <ncurses.h>
+
/* todo init */
void todo_init(void)
{
}
/* todo run */
-int todo_run(int chr)
+int todo_run(int key, mmask_t btn, int row, int col)
{
return 0;
}
+#include <ncurses.h>
+
/* week init */
void week_init(void)
{
}
/* week run */
-int week_run(int chr)
+int week_run(int key, mmask_t btn, int row, int col)
{
return 0;
}
+#include <ncurses.h>
+
/* year init */
void year_init(void)
{
}
/* year run */
-int year_run(int chr)
+int year_run(int key, mmask_t btn, int row, int col)
{
return 0;
}