чување 24d181a8a18567c7f775bcea9e11b6467cbec779
родитељ 98c022a9ac6e1af0c0ebd5f13569229a66ddae38
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sun, 6 Jun 2021 20:06:55 +0200
Added initial code for edit box
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | draw.c | | | 52 | ++++++++++++++++++++++++++++++++++++++++++++++++++-- |
| M | draw.h | | | 8 | ++++++-- |
| M | poe.c | | | 48 | +++++++++++++++++++++++------------------------- |
измењених датотека: 3, додавања: 79(+), брисања: 29(-)
diff --git a/draw.c b/draw.c
@@ -9,9 +9,10 @@
#include "draw.h"
#include "config.h"
-static const char* title = " poe - .po File Editor ";
+static const char* help_title = " poe - .po File Editor ";
static const char* help[] = {
"ESC - Close dialog, clear error",
+ "ENTER - Edit entry",
"H | F1 - Show this screen",
"g | Home - First entry",
"k | \u2191 - Previous entry",
@@ -19,6 +20,7 @@ static const char* help[] = {
"G | End - Last entry",
"q - Quit"
};
+static const char* editbox_title = " Edit translation ";
const char* errors[] = {
[ERR_UNKNOWN_KEY] = "Unknown key (press H for help)",
[ERR_NO_PREV] = "No previous entry",
@@ -35,6 +37,15 @@ const char* empty_msgstr = "*** No msgstr ***";
const char* program_name = "poe";
void
+draw_rect(const int startx, const int endx, const int starty, const int endy,
+ const uint16_t fg, const uint16_t bg)
+{
+ for (int y = starty; y < endy; y++)
+ for (int x = startx; x < endx; x++)
+ tb_change_cell(x, y, ' ', fg, bg);
+}
+
+void
draw_box(const int startx, const int endx, const int starty, const int endy,
const uint16_t fg, const uint16_t bg)
{
@@ -137,7 +148,7 @@ draw_help(const DrawState* state)
draw_box(startx, endx, starty, endy, TB_BLACK, TB_WHITE);
draw_string(startx+1, starty, TB_BLACK | TB_BOLD | TB_REVERSE, TB_WHITE,
- title, w-2, strlen(title)+2, 1, CENTER);
+ help_title, w-2, strlen(help_title)+2, 1, CENTER);
while (row < LEN(help))
{
draw_string(startx+2, starty+3+row, TB_BLACK, TB_WHITE,
@@ -147,6 +158,40 @@ draw_help(const DrawState* state)
}
void
+draw_editbox(const DrawState* state)
+{
+ if (!state)
+ return;
+
+ int startx, endx, starty, endy;
+ /* TODO: Ruler? At least output (X,Y) */
+ int w = 70;
+ int h = 20;
+ int maxx = state->maxx;
+ int maxy = state->maxy;
+
+ startx = w > maxx ? 0 : (maxx - w) / 2;
+ endx = w > maxx ? maxx-1 : maxx-startx;
+
+ starty = h > maxy-1 ? 0 : (maxy-h)/2;
+ endy = h > maxy-1 ? maxy-2 : maxy-starty;
+
+ draw_box(startx, endx, starty, endy, TB_BLACK, TB_WHITE);
+ draw_string(startx+1, starty, TB_BLACK | TB_BOLD | TB_REVERSE, TB_WHITE,
+ editbox_title, w-2, strlen(editbox_title)+2, 1, CENTER);
+
+ draw_rect(startx+2, endx-2, starty+1, endy-11, TB_BLACK | TB_BOLD,
+ TB_WHITE);
+ draw_string(startx+2, starty+1, TB_BLACK | TB_BOLD, TB_WHITE,
+ "Test msgid", w-2, strlen("Test msgid")+2, 1, LEFT);
+
+ draw_rect(startx+2, endx-2, endy-11, endy-2, TB_YELLOW | TB_BOLD,
+ TB_BLACK);
+ draw_string(startx+2, endy-11, TB_YELLOW | TB_BOLD, TB_BLACK,
+ "Test msgstr", w-2, strlen("Test msstr")+2, 1, LEFT);
+}
+
+void
draw_status(const DrawState* state)
{
if (!state)
@@ -236,6 +281,9 @@ draw(const DrawState* state)
if (state->show_help)
draw_help(state);
+ if (state->show_edit)
+ draw_editbox(state);
+
draw_status(state);
}
diff --git a/draw.h b/draw.h
@@ -29,6 +29,7 @@ typedef struct {
PoEntry* entries;
int running;
int show_help;
+ int show_edit;
int maxx;
int maxy;
long msgid_number;
@@ -45,12 +46,15 @@ typedef struct {
extern PoEntry entries[];
-void draw_box(const int startx, const int endx, const int starty, const int endy,
- const uint16_t fg, const uint16_t bg);
+void draw_rect(const int startx, const int endx, const int starty,
+ const int endy, const uint16_t fg, const uint16_t bg);
+void draw_box(const int startx, const int endx, const int starty,
+ const int endy, const uint16_t fg, const uint16_t bg);
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 DrawState* st);
+void draw_editbox(const DrawState* state);
void draw_status(const DrawState* st);
void draw_entry(const PoEntry* entry, const DrawState* st);
void draw(const DrawState* st);
diff --git a/poe.c b/poe.c
@@ -62,10 +62,12 @@ handle_key_event(const struct tb_event* ev, DrawState* state)
{
if (state->error[0])
state->error[0] = 0;
+ if (!(state->show_help || state->show_edit))
+ strcpy(state->error, errors[ERR_EXIT_KEY]);
if (state->show_help)
state->show_help = 0;
- else
- strcpy(state->error, errors[ERR_EXIT_KEY]);
+ if (state->show_edit)
+ state->show_edit = 0;
}
else if (ev->ch == 'q')
state->running = 0;
@@ -77,6 +79,11 @@ handle_key_event(const struct tb_event* ev, DrawState* state)
state->show_help = 1;
state->error[0] = 0;
}
+ else if (ev->key == TB_KEY_ENTER)
+ {
+ state->show_edit = 1;
+ state->error[0] = 0;
+ }
else if (ev->key == TB_KEY_HOME
|| ev->ch == 'g')
{
@@ -121,22 +128,17 @@ main(int argc, char** argv)
struct tb_event ev;
struct timespec sleep_spec;
char error[MAXBUF];
- int maxx, maxy;
DrawState state;
- long msgid_count = 0;
- long msgid_number = 0;
- int running = 1;
- int show_help = 0;
- FILE* input = NULL;
+ FILE* input = NULL;
char filename[MAXPATH];
/* Test */
PoEntry entries[] = {
- { flags: FL_C_FORMAT | FL_FUZZY, msgid: "First msgid: %s",
- msgstr: "Први msgid: %s" },
- { flags: FL_NONE, msgid: "Test msgid",
- msgstr: "Пробни msgstr" },
- { flags: FL_FUZZY, msgid: "Final text",
- msgstr: "Последњи текст" },
+ { flags: FL_C_FORMAT | FL_FUZZY,
+ msgid: "First msgid: %s", msgstr: "Први msgid: %s" },
+ { flags: FL_NONE,
+ msgid: "Test msgid", msgstr: "Пробни msgstr" },
+ { flags: FL_FUZZY,
+ msgid: "Final text", msgstr: "Последњи текст" },
};
filename[0] = 0;
@@ -157,25 +159,21 @@ main(int argc, char** argv)
tb_init();
error[0] = 0;
- msgid_count = LEN(entries);
- msgid_number = msgid_count > 0 ? 1 : 0;
state.error = error;
state.filename = filename;
state.entries = entries;
- state.running = running;
- state.show_help = show_help;
- state.msgid_number = msgid_number;
- state.msgid_count = msgid_count;
+ state.running = 1;
+ state.show_edit = 0;
+ state.show_help = 0;
+ state.msgid_count = LEN(entries);
+ state.msgid_number = state.msgid_count > 0 ? 1 : 0;
while (state.running)
{
tb_clear();
- maxx = tb_width();
- maxy = tb_height();
-
- state.maxx = maxx;
- state.maxy = maxy;
+ state.maxx = tb_width();
+ state.maxy = tb_height();
draw(&state);
tb_present();