fixes and improves tab navigation mechanism.
This commit is contained in:
@@ -6,3 +6,4 @@ TODO:
|
|||||||
- [ ] Hotkeys for buttons
|
- [ ] Hotkeys for buttons
|
||||||
- [ ] Simpler refresh on typing / entering in input
|
- [ ] Simpler refresh on typing / entering in input
|
||||||
- [ ] Group elements as lists
|
- [ ] Group elements as lists
|
||||||
|
- [ ] Process arrows
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef COMMON_H
|
||||||
|
#define COMMON_H
|
||||||
|
#include <stdint.h>
|
||||||
|
typedef struct {
|
||||||
|
uint32_t cur_y, cur_x;
|
||||||
|
uint32_t max_y, max_x;
|
||||||
|
} coordinates_t;
|
||||||
|
#endif
|
||||||
+4
-13
@@ -41,20 +41,11 @@ void clear_params(params_t *params) {
|
|||||||
params->uname = NULL;
|
params->uname = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_missing_params(params_t *params) {
|
void get_ip_port(params_t *params, char *ip, char *port) {
|
||||||
size_t lsize;
|
|
||||||
char *bufptr = NULL;
|
|
||||||
|
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
|
inet_aton(ip, &(addr.sin_addr));
|
||||||
if (!params->addr) {
|
params->addr = addr.sin_addr.s_addr;
|
||||||
printf("host> ");
|
params->port = htons(atoi(port));
|
||||||
getline(&bufptr, &lsize, stdin);
|
|
||||||
inet_aton(bufptr, &(addr.sin_addr));
|
|
||||||
params->addr = addr.sin_addr.s_addr;
|
|
||||||
free(bufptr);
|
|
||||||
bufptr = NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void close_session(int sd) { close(sd); }
|
void close_session(int sd) { close(sd); }
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
void connect_to_server(int sd, params_t *params);
|
void connect_to_server(int sd, params_t *params);
|
||||||
void init_params(params_t *restrict);
|
void init_params(params_t *restrict);
|
||||||
void clear_params(params_t *params);
|
void clear_params(params_t *params);
|
||||||
void get_missing_params(params_t *restrict);
|
void get_ip_port(params_t *params, char *ip, char *port);
|
||||||
void close_session(int sd);
|
void close_session(int sd);
|
||||||
int init_client();
|
int init_client();
|
||||||
void analyze_args(int argc, char *argv[], params_t *restrict);
|
void analyze_args(int argc, char *argv[], params_t *restrict);
|
||||||
|
|||||||
+7
-7
@@ -5,13 +5,13 @@ file_processor.o: src/file_processor.c src/client.h src/main.h \
|
|||||||
src/types.h src/query.h
|
src/types.h src/query.h
|
||||||
main.o: src/main.c src/connection.h src/main.h src/types.h src/ui/app.h \
|
main.o: src/main.c src/connection.h src/main.h src/types.h src/ui/app.h \
|
||||||
src/ui/widget/dialogue.h src/ui/widget/../widget.h \
|
src/ui/widget/dialogue.h src/ui/widget/../widget.h \
|
||||||
src/ui/widget/../../main.h src/ui/widget/button.h \
|
src/ui/widget/../../main.h src/ui/widget/../../common.h \
|
||||||
src/ui/widget/../../main.h src/ui/widget/group.h \
|
src/ui/widget/button.h src/ui/widget/../../main.h src/ui/widget/group.h \
|
||||||
src/ui/modals/ask_server_addr.h src/ui/modals/../app.h \
|
src/ui/../common.h src/ui/modals/ask_server_addr.h \
|
||||||
src/ui/modals/../widget/dialogue.h src/ui/modals/login.h src/ui/widget.h \
|
src/ui/modals/../app.h src/ui/modals/../widget/dialogue.h \
|
||||||
src/ui/widget/dialogue.h
|
src/ui/modals/login.h src/ui/widget.h src/ui/widget/dialogue.h
|
||||||
query.o: src/query.c src/file_processor.h src/main.h src/types.h \
|
query.o: src/query.c src/file_processor.h src/main.h src/types.h \
|
||||||
src/connection.h src/client.h src/server.h
|
src/connection.h src/client.h src/server.h
|
||||||
server.o: src/server.c src/client.h src/main.h src/types.h \
|
server.o: src/server.c src/client.h src/main.h src/types.h src/query.h \
|
||||||
src/connection.h src/query.h src/ui.h
|
src/ui.h
|
||||||
ui.o: src/ui.c
|
ui.o: src/ui.c
|
||||||
|
|||||||
+1
-2
@@ -1,5 +1,4 @@
|
|||||||
#include "client.h"
|
#include "client.h"
|
||||||
#include "connection.h"
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "query.h"
|
#include "query.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
@@ -69,7 +68,7 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) {
|
|||||||
free(params->pass);
|
free(params->pass);
|
||||||
params->uname = NULL;
|
params->uname = NULL;
|
||||||
params->pass = NULL;
|
params->pass = NULL;
|
||||||
get_missing_params(params);
|
// get_missing_params(params);
|
||||||
write(q_args->sd, params->uname, strlen(params->uname));
|
write(q_args->sd, params->uname, strlen(params->uname));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-7
@@ -13,10 +13,10 @@ app_t *init_app() {
|
|||||||
app_t *_app = calloc(1, sizeof(app_t));
|
app_t *_app = calloc(1, sizeof(app_t));
|
||||||
|
|
||||||
// associate values for screen size
|
// associate values for screen size
|
||||||
_app->cur_y = y_max;
|
_app->coordinates.max_x = _app->coordinates.cur_x = x_max;
|
||||||
_app->cur_x = x_max;
|
_app->coordinates.max_y = _app->coordinates.cur_y = y_max;
|
||||||
|
|
||||||
_app->win = newwin(_app->cur_y, _app->cur_x, 0, 0);
|
_app->win = newwin(_app->coordinates.cur_y, _app->coordinates.cur_x, 0, 0);
|
||||||
|
|
||||||
// define the width for each sub window
|
// define the width for each sub window
|
||||||
int menu_w_x = x_max / 6;
|
int menu_w_x = x_max / 6;
|
||||||
@@ -79,18 +79,18 @@ void print_bars(app_t *app) {
|
|||||||
wattrset(app->win, A_REVERSE);
|
wattrset(app->win, A_REVERSE);
|
||||||
|
|
||||||
// print top and bottom bars
|
// print top and bottom bars
|
||||||
for (int i = 1; i < app->cur_x - 1; i++) {
|
for (int i = 1; i < app->coordinates.cur_x - 1; i++) {
|
||||||
mvwprintw(app->win, 1, i, " ");
|
mvwprintw(app->win, 1, i, " ");
|
||||||
mvwprintw(app->win, app->cur_y - 2, i, " ");
|
mvwprintw(app->win, app->coordinates.cur_y - 2, i, " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
// add content to the top bar
|
// add content to the top bar
|
||||||
mvwprintw(app->win, 1, 2, "example cprint_barsurses app | ");
|
mvwprintw(app->win, 1, 2, "example cprint_barsurses app | ");
|
||||||
mvwprintw(app->win, 1, sizeof "example cprint_barsurses app | " + 1,
|
mvwprintw(app->win, 1, sizeof "example cprint_barsurses app | " + 1,
|
||||||
"size %d, %d", app->cur_x, app->cur_y);
|
"size %d, %d", app->coordinates.cur_x, app->coordinates.cur_y);
|
||||||
|
|
||||||
// add content to the bottom bar
|
// add content to the bottom bar
|
||||||
mvwprintw(app->win, app->cur_y - 2, 2, "F1 - Help | F9 - Quit");
|
mvwprintw(app->win, app->coordinates.cur_y - 2, 2, "F1 - Help | F9 - Quit");
|
||||||
|
|
||||||
wattroff(app->win, A_REVERSE);
|
wattroff(app->win, A_REVERSE);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -1,8 +1,9 @@
|
|||||||
#ifndef N_APP_H
|
#ifndef N_APP_H
|
||||||
#define N_APP_H
|
#define N_APP_H
|
||||||
|
|
||||||
#include <ncurses.h>
|
|
||||||
#include "widget/dialogue.h"
|
#include "widget/dialogue.h"
|
||||||
|
#include "../common.h"
|
||||||
|
#include <ncurses.h>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
MODAL_COLOR_PAIR = 3,
|
MODAL_COLOR_PAIR = 3,
|
||||||
@@ -23,8 +24,8 @@ typedef struct {
|
|||||||
dialogue_e type;
|
dialogue_e type;
|
||||||
dialogue_t dialogue;
|
dialogue_t dialogue;
|
||||||
} modal;
|
} modal;
|
||||||
int cur_y, cur_x;
|
|
||||||
params_t *params;
|
params_t *params;
|
||||||
|
coordinates_t coordinates;
|
||||||
} app_t;
|
} app_t;
|
||||||
|
|
||||||
app_t *init_app();
|
app_t *init_app();
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
|
#include "../../connection.h"
|
||||||
#include "../app.h"
|
#include "../app.h"
|
||||||
#include "../widget/dialogue.h"
|
#include "../widget/dialogue.h"
|
||||||
#include "../widget/group.h"
|
#include "../widget/group.h"
|
||||||
|
#include "../widget/input.h"
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -9,20 +13,27 @@ typedef struct {
|
|||||||
enum w_type type;
|
enum w_type type;
|
||||||
} group_bt_t;
|
} group_bt_t;
|
||||||
|
|
||||||
void asa_modal_group_callback(callback_args_t *args) {
|
void asa_modal_dialogue_callback(callback_args_t *args) {
|
||||||
group_resp_t *response = (group_resp_t *)args->resp_data;
|
int32_t response;
|
||||||
callback_args_t ch_args;
|
callback_args_t d_args;
|
||||||
memcpy(&ch_args, args, sizeof(callback_args_t));
|
app_t *app = args->app;
|
||||||
ch_args.app = NULL;
|
dialogue_t *d = (dialogue_t *)args->widget;
|
||||||
group_default_callback(&ch_args);
|
memcpy(&d_args, args, sizeof(callback_args_t));
|
||||||
if (response->value > -1) {
|
d_args.app = NULL;
|
||||||
switch (response->value) {
|
d_args.resp_data = &response;
|
||||||
|
input_t *in_ip = d->g_content->elements[0].element;
|
||||||
|
input_t *in_port = d->g_content->elements[1].element;
|
||||||
|
dialogue_default_callback(&d_args);
|
||||||
|
if (response > -1) {
|
||||||
|
switch (response) {
|
||||||
case 0:
|
case 0:
|
||||||
// exit(10);
|
get_ip_port(app->params, in_ip->value, in_port->value);
|
||||||
|
destroy_dialogue(d);
|
||||||
|
app->modal.type = none;
|
||||||
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
// exit(11);
|
destroy_app(app);
|
||||||
case 2:
|
exit(0);
|
||||||
// exit(12);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,19 +46,18 @@ dialogue_t *init_asa_modal(app_t *app) {
|
|||||||
{.type = w_input, .label = "Port", .length = 5},
|
{.type = w_input, .label = "Port", .length = 5},
|
||||||
{.type = w_end}};
|
{.type = w_end}};
|
||||||
|
|
||||||
group_el_init_t actions[] = {{.type = w_button, .label = "Connect"},
|
group_el_init_t actions[] = {
|
||||||
{.type = w_button, .label = "Cancel"},
|
{.type = w_button, .label = "Connect", .is_default = 1},
|
||||||
{.type = w_end}};
|
{.type = w_button, .label = "Cancel", .is_default = 0},
|
||||||
|
{.type = w_end}};
|
||||||
|
|
||||||
init_dialogue(&(app->modal.dialogue), "Connect to server",
|
init_dialogue(&(app->modal.dialogue), "Connect to server",
|
||||||
"There is information needed", app->cur_y, app->cur_x);
|
"There is information needed", &(app->coordinates));
|
||||||
dialogue_t *d = &(app->modal.dialogue);
|
dialogue_t *d = &(app->modal.dialogue);
|
||||||
|
|
||||||
|
d->w.callback = asa_modal_dialogue_callback;
|
||||||
d->g_content = init_group(&(d->win), &(d->w), content, horizontal);
|
d->g_content = init_group(&(d->win), &(d->w), content, horizontal);
|
||||||
d->g_content->w.callback = asa_modal_group_callback;
|
|
||||||
|
|
||||||
d->g_action = init_group(&(d->win), &(d->w), actions, horizontal);
|
d->g_action = init_group(&(d->win), &(d->w), actions, horizontal);
|
||||||
d->g_action->w.callback = asa_modal_group_callback;
|
|
||||||
|
|
||||||
dialogue_init_active_id(d);
|
dialogue_init_active_id(d);
|
||||||
|
|
||||||
|
|||||||
+16
-14
@@ -2,6 +2,7 @@
|
|||||||
#include "../widget/dialogue.h"
|
#include "../widget/dialogue.h"
|
||||||
#include "../widget/group.h"
|
#include "../widget/group.h"
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -10,20 +11,21 @@ typedef struct {
|
|||||||
} group_bt_t;
|
} group_bt_t;
|
||||||
|
|
||||||
void login_modal_group_callback(callback_args_t *args) {
|
void login_modal_group_callback(callback_args_t *args) {
|
||||||
group_resp_t *response = (group_resp_t *)args->resp_data;
|
int32_t response;
|
||||||
callback_args_t ch_args;
|
callback_args_t d_args;
|
||||||
memcpy(&ch_args, args, sizeof(callback_args_t));
|
app_t *app = args->app;
|
||||||
ch_args.app = NULL;
|
dialogue_t *d = (dialogue_t *)args->widget;
|
||||||
group_default_callback(&ch_args);
|
memcpy(&d_args, args, sizeof(callback_args_t));
|
||||||
if (response->value > -1) {
|
d_args.app = NULL;
|
||||||
// TODO: finish actions
|
d_args.resp_data = &response;
|
||||||
switch (response->value) {
|
dialogue_default_callback(&d_args);
|
||||||
|
if (response > -1) {
|
||||||
|
switch (response) {
|
||||||
case 0:
|
case 0:
|
||||||
// exit(10);
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
// exit(11);
|
// exit(0);
|
||||||
case 2:
|
break;
|
||||||
// exit(12);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,11 +41,11 @@ dialogue_t *init_login_modal(app_t *app) {
|
|||||||
init_dialogue(&(app->modal.dialogue), "Login",
|
init_dialogue(&(app->modal.dialogue), "Login",
|
||||||
"Select your login "
|
"Select your login "
|
||||||
"option",
|
"option",
|
||||||
app->cur_y, app->cur_x);
|
&(app->coordinates));
|
||||||
app->modal.dialogue.g_content =
|
app->modal.dialogue.g_content =
|
||||||
init_group(&(app->modal.dialogue.win), &(app->modal.dialogue.w), children,
|
init_group(&(app->modal.dialogue.win), &(app->modal.dialogue.w), children,
|
||||||
horizontal);
|
horizontal);
|
||||||
app->modal.dialogue.g_content->w.callback = login_modal_group_callback;
|
app->modal.dialogue.w.callback = login_modal_group_callback;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -19,6 +19,7 @@ typedef struct {
|
|||||||
void *widget;
|
void *widget;
|
||||||
void *data;
|
void *data;
|
||||||
void *resp_data;
|
void *resp_data;
|
||||||
|
uint32_t active_id;
|
||||||
} callback_args_t;
|
} callback_args_t;
|
||||||
|
|
||||||
enum rsize {
|
enum rsize {
|
||||||
|
|||||||
@@ -9,10 +9,12 @@
|
|||||||
|
|
||||||
button_t *init_button(WINDOW **win, widget_t *w_parent, char *label) {
|
button_t *init_button(WINDOW **win, widget_t *w_parent, char *label) {
|
||||||
button_t *btn = malloc(sizeof(button_t));
|
button_t *btn = malloc(sizeof(button_t));
|
||||||
init_widget(&(btn->w), w_parent, win, label);
|
init_widget(&(btn->w), w_parent, win, label);
|
||||||
btn->is_disabled = 0;
|
btn->is_disabled = 0;
|
||||||
btn->w.x = strlen(btn->w.title) + 6; // + "[ ]"
|
btn->w.x = strlen(btn->w.title) + 6; // + "[ ]"
|
||||||
btn->w.y = 1; // TODO: detect new line
|
btn->w.y = 1; // TODO: detect new line
|
||||||
|
btn->w.cur.y = 0;
|
||||||
|
btn->w.cur.x = 0;
|
||||||
return btn;
|
return btn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,6 +33,8 @@ int32_t draw_button(button_t *btn, uint32_t active_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mvwprintw(win, margin_y, margin_x, "%s", title);
|
mvwprintw(win, margin_y, margin_x, "%s", title);
|
||||||
|
btn->w.cur.y = margin_y;
|
||||||
|
btn->w.cur.x = margin_x;
|
||||||
|
|
||||||
wattroff(win, A_BOLD | A_REVERSE);
|
wattroff(win, A_BOLD | A_REVERSE);
|
||||||
return strlen(title);
|
return strlen(title);
|
||||||
|
|||||||
+45
-27
@@ -7,54 +7,71 @@
|
|||||||
#include "dialogue.h"
|
#include "dialogue.h"
|
||||||
#include "group.h"
|
#include "group.h"
|
||||||
|
|
||||||
|
#define INCR_ACTIVE_ID(d, current, next) \
|
||||||
|
if (d->active.id != d->current->last_id) { \
|
||||||
|
d->active.id = d->active.id + 1; \
|
||||||
|
} else if (d->next != NULL) { \
|
||||||
|
d->active.id = d->next->first_id; \
|
||||||
|
d->active.type = next; \
|
||||||
|
} else { \
|
||||||
|
d->active.id = d->current->first_id; \
|
||||||
|
}
|
||||||
|
|
||||||
void dialogue_default_callback(callback_args_t *args) {
|
void dialogue_default_callback(callback_args_t *args) {
|
||||||
dialogue_t *d = (void *)args->widget;
|
dialogue_t *d = (void *)args->widget;
|
||||||
char key = *((char *)args->data);
|
char key = *((char *)args->data);
|
||||||
app_t *app = (app_t *)args->app;
|
app_t *app = (app_t *)args->app;
|
||||||
callback_args_t new_args;
|
callback_args_t new_args;
|
||||||
group_resp_t response;
|
int32_t *resp_value = (int32_t *)args->resp_data;
|
||||||
|
group_el_t *default_element = NULL;
|
||||||
memcpy(&new_args, args, sizeof(callback_args_t));
|
memcpy(&new_args, args, sizeof(callback_args_t));
|
||||||
new_args.widget = d->g_content;
|
new_args.active_id = d->active.id;
|
||||||
new_args.resp_data = &response;
|
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case '\t':
|
case '\t':
|
||||||
// TODO: active task
|
|
||||||
if (d->active.type == g_content) {
|
if (d->active.type == g_content) {
|
||||||
if (d->active.id != d->g_content->last_id) {
|
INCR_ACTIVE_ID(d, g_content, g_action);
|
||||||
d->active.id = d->active.id + 1;
|
} else if (d->active.type == g_action) {
|
||||||
} else {
|
INCR_ACTIVE_ID(d, g_action, g_content);
|
||||||
d->active.id = d->g_content->first_id;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
*resp_value = -1;
|
||||||
|
draw_dialogue(d);
|
||||||
|
break;
|
||||||
|
case '\n':
|
||||||
if (d->active.type == g_action) {
|
if (d->active.type == g_action) {
|
||||||
if (d->active.id != d->g_action->last_id) {
|
new_args.widget = new_args.widget = d->g_action;
|
||||||
d->active.id = d->active.id + 1;
|
group_default_callback(&new_args);
|
||||||
} else {
|
} else {
|
||||||
d->active.id = d->g_action->first_id;
|
for (int i = 0; i < d->g_action->count; i++) {
|
||||||
|
if (d->g_action->elements[i].is_default) {
|
||||||
|
*resp_value = i;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
draw_dialogue(d);
|
draw_dialogue(d);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
d->g_content->w.callback(&new_args);
|
if (d->active.type == g_content) {
|
||||||
if (response.value > -1) {
|
new_args.widget = new_args.widget = d->g_content;
|
||||||
d->is_initiated = 0;
|
group_default_callback(&new_args);
|
||||||
destroy_dialogue(d);
|
|
||||||
app->modal.type = none;
|
|
||||||
} else {
|
} else {
|
||||||
|
new_args.widget = new_args.widget = d->g_action;
|
||||||
|
group_default_callback(&new_args);
|
||||||
|
}
|
||||||
|
if (*resp_value == -1) {
|
||||||
draw_dialogue(d);
|
draw_dialogue(d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_dialogue(dialogue_t *dialogue, const char title[], const char text[],
|
void init_dialogue(dialogue_t *dialogue, const char title[], const char text[],
|
||||||
uint32_t cur_y, uint32_t cur_x) {
|
coordinates_t *p_coordinates) {
|
||||||
dialogue->win = 0;
|
dialogue->win = 0;
|
||||||
dialogue->g_content = NULL;
|
dialogue->g_content = NULL;
|
||||||
dialogue->w.x = 0;
|
dialogue->w.x = 0;
|
||||||
dialogue->w.y = 0;
|
dialogue->w.y = 0;
|
||||||
dialogue->cur_y = cur_y;
|
dialogue->p_coordinates = p_coordinates;
|
||||||
dialogue->cur_x = cur_x;
|
|
||||||
dialogue->w.callback = dialogue_default_callback;
|
dialogue->w.callback = dialogue_default_callback;
|
||||||
dialogue->is_initiated = 1;
|
dialogue->is_initiated = 1;
|
||||||
strcpy(dialogue->w.title, title);
|
strcpy(dialogue->w.title, title);
|
||||||
@@ -123,12 +140,10 @@ int32_t draw_dialogue(dialogue_t *d) {
|
|||||||
|
|
||||||
/* space for action block */
|
/* space for action block */
|
||||||
|
|
||||||
y += 2;
|
|
||||||
|
|
||||||
d->w.x = x;
|
d->w.x = x;
|
||||||
d->w.y = y;
|
d->w.y = y;
|
||||||
d->w.m_y = (d->cur_y - y) / 2;
|
d->w.m_y = (d->p_coordinates->max_y - y) / 2;
|
||||||
d->w.m_x = (d->cur_x - x) / 2;
|
d->w.m_x = (d->p_coordinates->max_x - x) / 2;
|
||||||
|
|
||||||
/* render window */
|
/* render window */
|
||||||
if (d->win == NULL) {
|
if (d->win == NULL) {
|
||||||
@@ -156,10 +171,10 @@ int32_t draw_dialogue(dialogue_t *d) {
|
|||||||
print_multiline_text(d->win, d->text, d->w.x, 2, 1, PMT_ALIGN_CENTER);
|
print_multiline_text(d->win, d->text, d->w.x, 2, 1, PMT_ALIGN_CENTER);
|
||||||
wattroff(d->win, A_REVERSE);
|
wattroff(d->win, A_REVERSE);
|
||||||
if (d->g_content != NULL) {
|
if (d->g_content != NULL) {
|
||||||
draw_group(d->win, d->g_content, d->active.id);
|
draw_group(d->win, d->g_content, d->active.id, &(d->w));
|
||||||
}
|
}
|
||||||
if (d->g_action != NULL) {
|
if (d->g_action != NULL) {
|
||||||
draw_group(d->win, d->g_action, d->active.id);
|
draw_group(d->win, d->g_action, d->active.id, &(d->w));
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
@@ -168,6 +183,9 @@ void destroy_dialogue(dialogue_t *d) {
|
|||||||
if (d->g_content) {
|
if (d->g_content) {
|
||||||
destroy_group(d->g_content);
|
destroy_group(d->g_content);
|
||||||
}
|
}
|
||||||
|
if (d->g_action) {
|
||||||
|
destroy_group(d->g_action);
|
||||||
|
}
|
||||||
delwin(d->win);
|
delwin(d->win);
|
||||||
d->win = NULL;
|
d->win = NULL;
|
||||||
d->is_initiated = 0;
|
d->is_initiated = 0;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define DIALOGUE_H
|
#define DIALOGUE_H
|
||||||
|
|
||||||
#include "../widget.h"
|
#include "../widget.h"
|
||||||
|
#include "../../common.h"
|
||||||
#include "button.h"
|
#include "button.h"
|
||||||
#include "group.h"
|
#include "group.h"
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
@@ -17,16 +18,14 @@ typedef struct {
|
|||||||
struct {
|
struct {
|
||||||
int32_t id;
|
int32_t id;
|
||||||
enum g_type type;
|
enum g_type type;
|
||||||
uint32_t y, x;
|
|
||||||
} active;
|
} active;
|
||||||
uint32_t cur_y;
|
|
||||||
uint32_t cur_x;
|
|
||||||
char text[DIALOGUE_TEXT];
|
char text[DIALOGUE_TEXT];
|
||||||
uint32_t is_initiated : 1;
|
uint32_t is_initiated : 1;
|
||||||
|
coordinates_t *p_coordinates;
|
||||||
} dialogue_t;
|
} dialogue_t;
|
||||||
|
|
||||||
void init_dialogue(dialogue_t *d, const char title[], const char text[],
|
void init_dialogue(dialogue_t *d, const char title[], const char text[],
|
||||||
uint32_t cur_x, uint32_t cur_y);
|
coordinates_t *p_coordinates);
|
||||||
int32_t draw_dialogue(dialogue_t *d);
|
int32_t draw_dialogue(dialogue_t *d);
|
||||||
void destroy_dialogue(dialogue_t *d);
|
void destroy_dialogue(dialogue_t *d);
|
||||||
void dialogue_default_callback(callback_args_t *args);
|
void dialogue_default_callback(callback_args_t *args);
|
||||||
|
|||||||
+29
-38
@@ -9,36 +9,23 @@
|
|||||||
|
|
||||||
#define MAKE_RESPONSE_M1(args, resp_data, response) \
|
#define MAKE_RESPONSE_M1(args, resp_data, response) \
|
||||||
if (args->resp_data != NULL) { \
|
if (args->resp_data != NULL) { \
|
||||||
response->active_id = g->active_id; \
|
*response = -1; \
|
||||||
response->value = -1; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define FIND_ELEMENT(args, element_ptr, element_idx) \
|
|
||||||
if (args->resp_data != NULL) { \
|
|
||||||
for (int i = 0; i < g->count; i++) { \
|
|
||||||
if (g->elements[i].id == g->active_id) { \
|
|
||||||
element_ptr = &(g->elements[i]); \
|
|
||||||
element_idx = i; \
|
|
||||||
break; \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void group_default_callback(callback_args_t *args) {
|
void group_default_callback(callback_args_t *args) {
|
||||||
group_t *g = (group_t *)args->widget;
|
group_t *g = (group_t *)args->widget;
|
||||||
char key = *((char *)args->data);
|
char key = *((char *)args->data);
|
||||||
group_resp_t *response = (group_resp_t *)args->resp_data;
|
int32_t *response = (int32_t *)args->resp_data;
|
||||||
input_t *input;
|
input_t *input;
|
||||||
group_el_t *element_ptr;
|
group_el_t *element_ptr;
|
||||||
int32_t element_idx = -1;
|
int32_t element_idx = -1;
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case '\n': /* Enter */
|
case '\n': /* Enter */
|
||||||
FIND_ELEMENT(args, element_ptr, element_idx);
|
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
|
||||||
response->active_id = g->active_id;
|
*response = element_idx;
|
||||||
response->value = element_idx;
|
|
||||||
break;
|
break;
|
||||||
case '\177': /* DEL */
|
case '\177': /* DEL */
|
||||||
FIND_ELEMENT(args, element_ptr, element_idx);
|
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
|
||||||
if (element_ptr->type == w_input) {
|
if (element_ptr->type == w_input) {
|
||||||
input = (input_t *)element_ptr->element;
|
input = (input_t *)element_ptr->element;
|
||||||
if (input->value_len) {
|
if (input->value_len) {
|
||||||
@@ -48,7 +35,7 @@ void group_default_callback(callback_args_t *args) {
|
|||||||
MAKE_RESPONSE_M1(args, resp_data, response);
|
MAKE_RESPONSE_M1(args, resp_data, response);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
FIND_ELEMENT(args, element_ptr, element_idx);
|
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
|
||||||
if (element_ptr->type == w_input) {
|
if (element_ptr->type == w_input) {
|
||||||
input = (input_t *)element_ptr->element;
|
input = (input_t *)element_ptr->element;
|
||||||
if (input->max_len > input->value_len) {
|
if (input->max_len > input->value_len) {
|
||||||
@@ -76,12 +63,13 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
|||||||
}
|
}
|
||||||
group->elements = calloc(group->count, sizeof(group_el_t));
|
group->elements = calloc(group->count, sizeof(group_el_t));
|
||||||
group_el_t *elements = group->elements;
|
group_el_t *elements = group->elements;
|
||||||
for (int i = 0; i < group->count; i++) {
|
for (int32_t i = 0; i < group->count; i++) {
|
||||||
elements[i].type = children[i].type;
|
elements[i].type = children[i].type;
|
||||||
|
elements[i].is_default = children[i].is_default;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* init child elements */
|
/* init child elements */
|
||||||
for (int i = 0; i < group->count; i++) {
|
for (int32_t i = 0; i < group->count; i++) {
|
||||||
widget_t *w;
|
widget_t *w;
|
||||||
switch (elements[i].type) {
|
switch (elements[i].type) {
|
||||||
case w_button:
|
case w_button:
|
||||||
@@ -101,7 +89,7 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
|||||||
// set dimensions
|
// set dimensions
|
||||||
elements[i].id = w->id;
|
elements[i].id = w->id;
|
||||||
if (direction == horizontal) {
|
if (direction == horizontal) {
|
||||||
w->m_x = group->w.x;
|
w->m_x = group->w.x + 1;
|
||||||
w->m_y = group->w.m_y;
|
w->m_y = group->w.m_y;
|
||||||
group->w.x += w->x + 1;
|
group->w.x += w->x + 1;
|
||||||
if (group->w.y < w->y)
|
if (group->w.y < w->y)
|
||||||
@@ -109,7 +97,6 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
|||||||
}
|
}
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
group->first_id = w->id;
|
group->first_id = w->id;
|
||||||
group->active_id = w->id;
|
|
||||||
} else {
|
} else {
|
||||||
group->last_id = w->id;
|
group->last_id = w->id;
|
||||||
}
|
}
|
||||||
@@ -118,8 +105,11 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
|||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_group(WINDOW *win, group_t *group, int32_t active_id) {
|
void draw_group(WINDOW *win, group_t *group, int32_t active_id,
|
||||||
|
widget_t *dialog_w) {
|
||||||
group_el_t *children = group->elements;
|
group_el_t *children = group->elements;
|
||||||
|
group_el_t *active_element = NULL;
|
||||||
|
uint32_t active_idx;
|
||||||
for (int i = 0; i < group->count; i++) {
|
for (int i = 0; i < group->count; i++) {
|
||||||
group_el_t *el = &children[i];
|
group_el_t *el = &children[i];
|
||||||
switch (el->type) {
|
switch (el->type) {
|
||||||
@@ -135,22 +125,23 @@ void draw_group(WINDOW *win, group_t *group, int32_t active_id) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < group->count; i++) {
|
/* move cursor */
|
||||||
if (group->elements[i].id == group->active_id &&
|
FIND_ACTIVE_ELEMENT(group, active_id, active_element, active_idx);
|
||||||
group->elements[i].type == w_input) {
|
|
||||||
input_t *input = group->elements[i].element;
|
if (active_element != NULL && active_element->id == active_id &&
|
||||||
uint32_t margin_y = input->w.m_y + input->w.w_parent->m_y;
|
active_element->type == w_input) {
|
||||||
uint32_t margin_x = input->w.m_x + input->w.w_parent->m_x + 1;
|
input_t *input = active_element->element;
|
||||||
if (input->max_len == input->value_len) {
|
dialog_w->cur.y = input->w.cur.y;
|
||||||
wmove(win, margin_y + 1, margin_x + input->value_len);
|
if (input->max_len == input->value_len) {
|
||||||
} else {
|
dialog_w->cur.x = input->w.cur.x + input->value_len - 1;
|
||||||
wmove(win, margin_y + 1,
|
} else {
|
||||||
margin_x + input->value_len +
|
dialog_w->cur.x = input->w.cur.x + input->value_len;
|
||||||
1); // x: +1: margin left from the border
|
|
||||||
}
|
|
||||||
curs_set(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (dialog_w->cur.y || dialog_w->cur.x) {
|
||||||
|
wmove(win, dialog_w->cur.y, dialog_w->cur.x);
|
||||||
|
curs_set(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_group(group_t *group) {
|
void destroy_group(group_t *group) {
|
||||||
|
|||||||
+11
-6
@@ -13,10 +13,12 @@ typedef struct {
|
|||||||
uint32_t id;
|
uint32_t id;
|
||||||
void *element;
|
void *element;
|
||||||
enum w_type type;
|
enum w_type type;
|
||||||
|
uint32_t is_default : 1;
|
||||||
} group_el_t;
|
} group_el_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
enum w_type type;
|
enum w_type type;
|
||||||
|
uint32_t is_default : 1;
|
||||||
char label[DIALOGUE_TITLE];
|
char label[DIALOGUE_TITLE];
|
||||||
uint32_t length;
|
uint32_t length;
|
||||||
} group_el_init_t;
|
} group_el_init_t;
|
||||||
@@ -24,20 +26,23 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
widget_t w;
|
widget_t w;
|
||||||
group_el_t *elements;
|
group_el_t *elements;
|
||||||
uint32_t active_id; // TODO: needs to be removed
|
|
||||||
enum g_direction direction;
|
enum g_direction direction;
|
||||||
uint32_t count;
|
uint32_t count;
|
||||||
uint32_t first_id, last_id;
|
uint32_t first_id, last_id;
|
||||||
} group_t;
|
} group_t;
|
||||||
|
|
||||||
typedef struct {
|
#define FIND_ACTIVE_ELEMENT(g, active_id, element_ptr, element_idx) \
|
||||||
int32_t value;
|
for (int i = 0; i < g->count; i++) { \
|
||||||
uint32_t active_id;
|
if (g->elements[i].id == active_id) { \
|
||||||
} group_resp_t;
|
element_ptr = &(g->elements[i]); \
|
||||||
|
element_idx = i; \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
||||||
enum g_direction dir);
|
enum g_direction dir);
|
||||||
void draw_group(WINDOW *win, group_t *group, int32_t active_id);
|
void draw_group(WINDOW *win, group_t *group, int32_t active_id, widget_t *dialog_w);
|
||||||
void destroy_group(group_t *group);
|
void destroy_group(group_t *group);
|
||||||
void group_default_callback(callback_args_t *args);
|
void group_default_callback(callback_args_t *args);
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ input_t *init_input(WINDOW **win, widget_t *w_parent, char *label,
|
|||||||
init_widget(&(input->w), w_parent, win, label);
|
init_widget(&(input->w), w_parent, win, label);
|
||||||
input->is_disabled = 0;
|
input->is_disabled = 0;
|
||||||
input->w.y = 3; // with borders
|
input->w.y = 3; // with borders
|
||||||
input->w.x = length + 2; // with borders
|
input->w.x = length + 2; // with borders
|
||||||
uint32_t t_len = strlen(input->w.title) + 4; // with borders and space
|
uint32_t t_len = strlen(input->w.title) + 4; // with borders and space
|
||||||
if (input->w.x < t_len)
|
if (input->w.x < t_len)
|
||||||
input->w.x = t_len;
|
input->w.x = t_len;
|
||||||
@@ -27,7 +27,7 @@ input_t *init_input(WINDOW **win, widget_t *w_parent, char *label,
|
|||||||
int32_t draw_input(input_t *input, uint32_t active_id) {
|
int32_t draw_input(input_t *input, uint32_t active_id) {
|
||||||
WINDOW *win = *(input->w.parent_win);
|
WINDOW *win = *(input->w.parent_win);
|
||||||
uint32_t margin_y = input->w.m_y + input->w.w_parent->m_y;
|
uint32_t margin_y = input->w.m_y + input->w.w_parent->m_y;
|
||||||
uint32_t margin_x = input->w.m_x + input->w.w_parent->m_x + 1; // +1: margin left from the border
|
uint32_t margin_x = input->w.m_x + input->w.w_parent->m_x;
|
||||||
|
|
||||||
wattrset(win, COLOR_PAIR(MODAL_COLOR_PAIR));
|
wattrset(win, COLOR_PAIR(MODAL_COLOR_PAIR));
|
||||||
|
|
||||||
@@ -41,9 +41,9 @@ int32_t draw_input(input_t *input, uint32_t active_id) {
|
|||||||
mvwvline(win, margin_y + 1, margin_x, 0, 1);
|
mvwvline(win, margin_y + 1, margin_x, 0, 1);
|
||||||
mvwvline(win, margin_y + 2, margin_x, ACS_LLCORNER, 1);
|
mvwvline(win, margin_y + 2, margin_x, ACS_LLCORNER, 1);
|
||||||
// right
|
// right
|
||||||
mvwvline(win, margin_y, margin_x + input->w.x-1, ACS_URCORNER, 1);
|
mvwvline(win, margin_y, margin_x + input->w.x - 1, ACS_URCORNER, 1);
|
||||||
mvwvline(win, margin_y + 1, margin_x + input->w.x-1, 0, 1);
|
mvwvline(win, margin_y + 1, margin_x + input->w.x - 1, 0, 1);
|
||||||
mvwvline(win, margin_y + 2, margin_x + input->w.x-1, ACS_LRCORNER, 1);
|
mvwvline(win, margin_y + 2, margin_x + input->w.x - 1, ACS_LRCORNER, 1);
|
||||||
|
|
||||||
// TODO: correct sizes (x)
|
// TODO: correct sizes (x)
|
||||||
|
|
||||||
@@ -57,7 +57,8 @@ int32_t draw_input(input_t *input, uint32_t active_id) {
|
|||||||
|
|
||||||
uint32_t x_c = 0;
|
uint32_t x_c = 0;
|
||||||
mvwprintw(win, margin_y + 1, margin_x, "%s", input->value);
|
mvwprintw(win, margin_y + 1, margin_x, "%s", input->value);
|
||||||
|
input->w.cur.y = margin_y + 1;
|
||||||
|
input->w.cur.x = margin_x;
|
||||||
for (int i = 0; i < input->w.x - input->value_len - 2; i++) {
|
for (int i = 0; i < input->w.x - input->value_len - 2; i++) {
|
||||||
mvwprintw(win, margin_y + 1, margin_x + i + input->value_len, " ");
|
mvwprintw(win, margin_y + 1, margin_x + i + input->value_len, " ");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user