-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdialog.c
112 lines (89 loc) · 2.21 KB
/
dialog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "dialog.h"
#include <string.h>
/* This supports only a single dialog instance to reduce programming clutter.
It is designed for simple LCD displays. */
typedef struct
{
uint8_t rows;
uint8_t cols;
void (*display)(uint8_t row, const char *str);
dialog_t *current;
} dialog_data_t;
static dialog_data_t dialog_data;
void
dialog_display_options (dialog_t *dialog, int rows)
{
int i;
int cols;
char *str;
char buffer[64];
for (; rows < dialog_data.rows - 1; rows++)
dialog_data.display (rows, "\n");
cols = dialog_data.cols
- (strlen (dialog->left_name) + strlen (dialog->right_name));
str = buffer;
for (i = 0; dialog->left_name[i]; i++)
*str++ = dialog->left_name[i];
for (i = 0; i < cols; i++)
*str++ = ' ';
for (i = 0; dialog->right_name[i]; i++)
*str++ = dialog->right_name[i];
*str = '\0';
dialog_data.display (rows - 1, buffer);
dialog_data.current = dialog;
}
/* Display a new dialog, saving previous dialog to return to later. */
void
dialog_display (dialog_t *dialog, const char *string)
{
int i;
int rows;
int cols;
rows = 1;
cols = 0;
for (i = 0; string[i]; i++)
{
if (string[i] == '\n')
{
rows++;
cols = 0;
}
else
{
cols++;
if (cols > dialog_data.cols)
{
rows++;
cols = 0;
}
}
}
if (string[i - 1] == '\n')
rows--;
dialog_data.display (0, string);
dialog_display_options (dialog, rows);
}
/** Return non-zero if the dialog has finished. */
bool
dialog_right (void)
{
if (!dialog_data.current->right_action)
return 1;
return dialog_data.current->right_action ();
}
/** Return non-zero if the dialog has finished. */
bool
dialog_left (void)
{
if (!dialog_data.current->left_action)
return 1;
return dialog_data.current->left_action ();
}
void
dialog_init (uint8_t rows, uint8_t cols,
void (*display)(uint8_t row, const char *message))
{
dialog_data.rows = rows;
dialog_data.cols = cols;
dialog_data.display = display;
}