чување ad30031da824dfa6c88d00a403734d13e5a58036
родитељ d30b66b093ef7248f6653076dbdd982f876fe41e
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Fri, 11 Jun 2021 22:27:25 +0200
Added list rendering and selection
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | draw.c | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++++++---- |
| M | draw.h | | | 19 | +++++++++++++++---- |
| M | poe.c | | | 3 | +++ |
измењених датотека: 3, додавања: 67(+), брисања: 8(-)
diff --git a/draw.c b/draw.c
@@ -223,6 +223,45 @@ draw_status(const struct DrawState* state)
}
}
+void
+draw_list_visible(const struct PoEntry* start, const struct DrawState* state)
+{
+ if (!state)
+ return;
+
+ const struct PoEntry* current = start;
+ int max_screen_entries = state->maxy-2;
+ int cy = 0;
+ size_t i = state->first_shown_msgid;
+
+ if (!start)
+ {
+ draw_string(0, cy, LIST_MSGID_FG, LIST_MSGID_BG, empty_msgid,
+ state->maxx/2, state->maxx/2, 2, CENTER);
+ draw_string(state->maxx/2, cy, LIST_MSGSTR_FG, LIST_MSGSTR_BG,
+ empty_msgstr, state->maxx/2, state->maxx/2,
+ 2, CENTER);
+ return;
+ }
+
+ while (i <= state->msgid_count && cy < max_screen_entries)
+ {
+ int sel = i == state->msgid_number;
+ draw_string(0, cy, sel ? LIST_MSGID_SEL_FG : LIST_MSGID_FG,
+ sel ? LIST_MSGID_SEL_BG : LIST_MSGID_BG,
+ current->msgid,
+ state->maxx/2, state->maxx/2, 2, LEFT);
+ draw_string(state->maxx/2, cy,
+ sel ? LIST_MSGSTR_SEL_FG : LIST_MSGSTR_FG,
+ sel ? LIST_MSGSTR_SEL_BG : LIST_MSGSTR_BG,
+ current->msgstr, state->maxx/2, state->maxx/2,
+ 2, LEFT);
+ current++;
+ i++;
+ cy++;
+ }
+}
+
void
draw_entry(const struct PoEntry* entry, const struct DrawState* state)
{
@@ -272,11 +311,17 @@ draw(const struct DrawState* state)
if (!state)
return;
- if (state->msgid_number > 0
- && state->msgid_number < state->msgid_count+1)
- draw_entry(&state->entries[state->msgid_number-1], state);
+ if (state->first_shown_msgid > 0)
+ draw_list_visible(&state->entries[state->first_shown_msgid-1], state);
else
- draw_entry(NULL, state);
+ draw_list_visible(NULL, state);
+ /*
+ *if (state->msgid_number > 0
+ * && state->msgid_number < state->msgid_count+1)
+ * draw_entry(&state->entries[state->msgid_number-1], state);
+ *else
+ * draw_entry(NULL, state);
+ */
if (state->show_help)
draw_help(state);
diff --git a/draw.h b/draw.h
@@ -10,6 +10,14 @@
#define ERROR_BG TB_RED
#define FLAG_FG TB_YELLOW | TB_BOLD
#define FLAG_BG TB_DEFAULT
+#define LIST_MSGID_FG TB_WHITE
+#define LIST_MSGID_BG TB_DEFAULT
+#define LIST_MSGSTR_FG TB_GREEN
+#define LIST_MSGSTR_BG TB_DEFAULT
+#define LIST_MSGID_SEL_FG TB_WHITE
+#define LIST_MSGID_SEL_BG TB_BLUE
+#define LIST_MSGSTR_SEL_FG TB_GREEN
+#define LIST_MSGSTR_SEL_BG TB_BLUE
#define MSGID_FG TB_GREEN
#define MSGID_BG TB_DEFAULT
#define MSGSTR_FG TB_WHITE
@@ -34,6 +42,7 @@ struct DrawState {
int maxy;
long msgid_number;
long msgid_count;
+ long first_shown_msgid;
};
struct StatusSegment {
@@ -53,9 +62,11 @@ void draw_box(const int startx, const int endx, const int starty,
void draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
const char* s, const int max_cols, const int fill_cols,
const int padding, const Alignment align);
-void draw_help(const struct DrawState* st);
+void draw_help(const struct DrawState* state);
void draw_editbox(const struct DrawState* state);
-void draw_status(const struct DrawState* st);
-void draw_entry(const struct PoEntry* entry, const struct DrawState* st);
-void draw(const struct DrawState* st);
+void draw_status(const struct DrawState* state);
+void draw_list_visible(const struct PoEntry* start,
+ const struct DrawState* state);
+void draw_entry(const struct PoEntry* entry, const struct DrawState* state);
+void draw(const struct DrawState* state);
diff --git a/poe.c b/poe.c
@@ -171,12 +171,15 @@ main(int argc, char** argv)
state.show_help = 0;
state.msgid_count = LEN(entries);
state.msgid_number = state.msgid_count > 0 ? 1 : 0;
+ state.first_shown_msgid = state.msgid_number;
while (state.running)
{
tb_clear();
state.maxx = tb_width();
state.maxy = tb_height();
+ if (state.msgid_number - state.first_shown_msgid > state.maxx-2)
+ state.first_shown_msgid = state.msgid_number;
draw(&state);
tb_present();