]> Pileus Git - lackey/blob - src/screen.c
Add some mouse support
[lackey] / src / screen.c
1 #include <string.h>
2 #include <ncurses.h>
3 #include "main.h"
4 #include "screen.h"
5
6 /* Types */
7 typedef struct {
8         char  *name;
9         void (*init)(void);
10         void (*draw)(void);
11         int  (*run)(int,mmask_t,int,int);
12         int    keys[8];
13 } view_t;
14
15 /* Data */
16 view_t views[] = {
17         { "Day",      day_init,      day_draw,      day_run,      {KEY_F(1), '1', 'd',    } },
18         { "Week",     week_init,     week_draw,     week_run,     {KEY_F(2), '2', 'w',    } },
19         { "Month",    month_init,    month_draw,    month_run,    {KEY_F(3), '3', 'm',    } },
20         { "Year",     year_init,     year_draw,     year_run,     {KEY_F(4), '4', 'y',    } },
21         { "|",        NULL,          NULL,          NULL,         {                       } },
22         { "Todo",     todo_init,     todo_draw,     todo_run,     {KEY_F(5), '5', 't',    } },
23         { "Notes",    notes_init,    notes_draw,    notes_run,    {KEY_F(6), '6', 'n',    } },
24         { "|",        NULL,          NULL,          NULL,         {                       } },
25         { "Settings", settings_init, settings_draw, settings_run, {KEY_F(7), '7', 's',    } },
26         { "Help",     help_init,     help_draw,     help_run,     {KEY_F(8), '8', 'h', '?'} },
27 };
28
29 int active = 0;
30
31 /* Local functions */
32 void draw_header(void)
33 {
34         move(0, 0);
35         attron(COLOR_PAIR(COLOR_TITLE));
36         for (int i = 0; i < N_ELEMENTS(views); i++) {
37                 if (i == active)
38                         attron(A_BOLD);
39                 printw(" %s", views[i].name);
40                 if (i == active)
41                         attroff(A_BOLD);
42         }
43         attroff(COLOR_PAIR(COLOR_TITLE));
44         mvhline(1, 0, ACS_HLINE, COLS);
45 }
46
47 /* Screen init */
48 void screen_init(void)
49 {
50 }
51
52 /* Screen draw */
53 void screen_draw(void)
54 {
55         draw_header();
56         views[active].draw();
57 }
58
59 /* Screen set */
60 int screen_set(int num)
61 {
62         if (active != num) {
63                 active = num;
64                 screen_draw();
65         }
66         return 1;
67 }
68
69 /* Screen run */
70 int screen_run(int key, mmask_t btn, int row, int col)
71 {
72         /* Check for mouse events */
73         if (key == KEY_MOUSE && row == 0) {
74                 int start = 1;
75                 for (int i = 0; i < N_ELEMENTS(views); i++) {
76                         int end = start + strlen(views[i].name) - 1;
77                         if (start <= col && col <= end)
78                                 return screen_set(i);
79                         start = end + 2;
80                 }
81         }
82
83         /* Check for view change */
84         for (int i = 0; i < N_ELEMENTS(views); i++) {
85                 if (i == active)
86                         continue;
87                 for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
88                         if (views[i].keys[j] == key)
89                                 return screen_set(i);
90         }
91
92         /* Shift windows */
93         int num   = active;
94         int shift = key == KEY_RIGHT ? +1 :
95                     key == KEY_LEFT  ? -1 : 0;
96         while (shift) {
97                 num += shift;
98                 num += N_ELEMENTS(views);
99                 num %= N_ELEMENTS(views);
100                 if (views[num].run)
101                         return screen_set(num);
102         }
103
104         /* Pass key to active view */
105         return views[active].run(key, btn, row, col);
106 }