diff --git a/include/windows.h b/include/windowsBack.h similarity index 63% rename from include/windows.h rename to include/windowsBack.h index f500aa0..1625552 100644 --- a/include/windows.h +++ b/include/windowsBack.h @@ -1,20 +1,23 @@ -#ifndef WINDOWS_H -#define WINDOWS_H +#ifndef WINDOWSBACK_H +#define WINDOWSBACK_H #include #include #include #include -int runMainMenu(); -void runOptionsMenu(); +WINDOW *create_newwin(int height, int width, int starty, int startx); +void destroy_win(WINDOW *local_win); + +void wwrap_print(WINDOW *win, int start_y, int start_x, const char *str); +int uiToggleSwitch(WINDOW *win, int y, int x, int state); +void uiTextInput(WINDOW *win, int y, int x, char *input, int max_len); +int uiSelectMenu(WINDOW *win, int start_y, int start_x, char **options, int n_options); +void print_menu(WINDOW *menu_win, int y, int x, int highlight); + void warningf(const char *fmt, ...); void warning(char *string); void statusf(const char *fmt, ...); void status(char *msg); -WINDOW *create_newwin(int height, int width, int starty, int startx); -void destroy_win(WINDOW *local_win); -void print_menu(WINDOW *menu_win, int y, int x, int highlight); -void wwrap_print(WINDOW *win, int start_y, int start_x, const char *str); #endif diff --git a/include/windowsDeclaration.h b/include/windowsDeclaration.h new file mode 100644 index 0000000..08961bd --- /dev/null +++ b/include/windowsDeclaration.h @@ -0,0 +1,13 @@ +#ifndef WINDOWSDECLARATION_H +#define WINDOWSDECLARATION_H + +#include +#include +#include +#include + +int runMainMenu(); +void runOptionsMenu(); +void example_window(); + +#endif diff --git a/src/main.c b/src/main.c index 0f87a0d..37d34d3 100644 --- a/src/main.c +++ b/src/main.c @@ -19,8 +19,9 @@ #include #include -#include -#include +#include "../include/windowsDeclaration.h" +#include "../include/windowsBack.h" +#include "../include/logic.h" void printLogoBG(); @@ -49,6 +50,7 @@ int main(){ int running = 0; while(running != 6){ + printLogoBG(); refresh(); running = runMainMenu(); switch(running) { @@ -60,7 +62,8 @@ int main(){ case 3: break; case 4: - break; + example_window(); + break; case 5: runOptionsMenu(); break; diff --git a/src/testLogic.c b/src/testLogic.c index ccbf9c7..e91df6e 100644 --- a/src/testLogic.c +++ b/src/testLogic.c @@ -7,11 +7,9 @@ #include "../include/globals.h" int main() { - /* Working printf("%d", existsFile("qwestoeuntest")); createFile("qwestoeuntest"); printf("%d", existsFile("qwestoeuntest")); - */ return 0; } diff --git a/src/windows.c b/src/windows.c deleted file mode 100644 index 550bc05..0000000 --- a/src/windows.c +++ /dev/null @@ -1,276 +0,0 @@ -#include -#include -#include -#include - -#include "../include/windows.h" -#include "../include/logic.h" -#include "../include/globals.h" -#define MAX_LEN 64 - -char *mainMenu[] = { - "Segna vendita", - "Vedi vendite", - "Vedi prodotti", - "Gestisci prodotti", - "Opzioni", - "Exit" -}; -int n_mainMenu = sizeof(mainMenu) / sizeof(char *); - - -int runMainMenu(){ - WINDOW *mainWin; - int startx, starty, width, height; - height = LINES / 2; - width = COLS / 2; - starty = 0; - startx = 0; - mainWin = create_newwin(height, width, starty, startx); - - int highlight = 1; - int choice = 0; - int c; - - keypad(mainWin, TRUE); - wwrap_print(mainWin, 1, 2, "Usa le FRECCETTE ed INVIO per scegliere un'opzione,\nin alternativa ci sono i movement di vim e SPAZIO."); - refresh(); - print_menu(mainWin, height / 2 - 3, width / 2 - 9, highlight); - while(1) - { c = wgetch(mainWin); - switch(c) - { case KEY_UP: - case KEY_LEFT: - case 'h': - case 'k': - if(highlight == 1) - highlight = n_mainMenu; - else - --highlight; - break; - case KEY_DOWN: - case KEY_RIGHT: - case 'l': - case 'j': - if(highlight == n_mainMenu) - highlight = 1; - else - ++highlight; - break; - case 10: - case 32: - choice = highlight; - break; - default: - statusf("Il tasto '%c' non e' un tasto valido", c); - refresh(); - break; - } - print_menu(mainWin, height / 2 - 3, width / 2 - 9, highlight); - if(choice != 0) /* User did a choice come out of the infinite loop */ - break; - } - statusf("Avvio opzione '%s'", mainMenu[choice - 1]); - clrtoeol(); - refresh(); - destroy_win(mainWin); - - return choice; -} - -void runOptionsMenu(){ - WINDOW *optionsWin; - int startx, starty, width, height; - height = LINES / 2; - width = COLS / 2; - starty = 0; - startx = COLS / 2; - optionsWin = create_newwin(height, width, starty, startx); - - char input[MAX_LEN + 1]; - strcpy(input, productPricesFileLocation); - int ch, pos = strlen(input); - - mvwprintw(optionsWin, 1, 2, "Posizione file prodotti:"); - wattron(optionsWin, COLOR_PAIR(2)); - for (int i = 0; i < MAX_LEN; i++) { - mvwaddch(optionsWin, 2, 2 + i, ' '); - } - mvwprintw(optionsWin, 2, 2, "%s", input); - wmove(optionsWin, 2, 2 + strlen(input)); - wrefresh(optionsWin); - - pos = strlen(input); - - // Input nella finestra - curs_set(1); - while ((ch = wgetch(optionsWin)) != '\n') { - if (ch == KEY_BACKSPACE || ch == 127 || ch == '\b') { - if (pos > 0) { - pos--; - input[pos] = '\0'; - mvwaddch(optionsWin, 2, 2 + pos, ' '); - wmove(optionsWin, 2, 2 + pos); - } - } else if (pos < MAX_LEN && ch >= 32 && ch <= 126) { - input[pos++] = ch; - input[pos] = '\0'; - mvwaddch(optionsWin, 2, 2 + pos - 1, ch); - } - wrefresh(optionsWin); - } - curs_set(0); - wattroff(optionsWin, COLOR_PAIR(2)); - - if (existsFile(input)) { - statusf("Il file '%s' esiste e verra' utilizzato al posto di '%s'", input, productPricesFileLocation); - strcpy(productPricesFileLocation, input); - } - else { - warningf("Il file '%s' non esiste, utilizzo il vecchio file '%s'", input, productPricesFileLocation); - } - - werase(optionsWin); - destroy_win(optionsWin); -} - -void warningf(const char *fmt, ...) { - char buf[256]; - va_list args; - va_start(args, fmt); - vsnprintf(buf, sizeof(buf), fmt, args); - va_end(args); - warning(buf); -} - -void warning(char *string){ - WINDOW *warningWin; - int startx, starty, width, height; - height = LINES / 2 - 2; - width = COLS / 2; - starty = LINES / 2; - startx = 0; - warningWin = create_newwin(height, width, starty, startx); - wbkgd(warningWin, COLOR_PAIR(1)); - - mvwprintw(warningWin, 2, 2, "ATTENZIONE!!! E' avvenuto un errore! "); - wwrap_print(warningWin, 4, 3, string); - mvwprintw(warningWin, height - 1, 3, " Premi un tasto per continuare . . . "); - - wrefresh(warningWin); - getch(); - werase(warningWin); - destroy_win(warningWin); -} - - -void statusf(const char *fmt, ...) { - char buf[256]; - va_list args; - va_start(args, fmt); - vsnprintf(buf, sizeof(buf), fmt, args); - va_end(args); - status(buf); -} - -void status(char *msg) { - attron(COLOR_PAIR(3)); - mvprintw(LINES -2, 0, "%-*s", COLS, msg); - attroff(COLOR_PAIR(3)); - refresh(); -} - - -WINDOW *create_newwin(int height, int width, int starty, int startx) -{ WINDOW *local_win; - - local_win = newwin(height, width, starty, startx); - box(local_win, 0 , 0); - wrefresh(local_win); - - return local_win; -} - -void destroy_win(WINDOW *local_win) -{ - wborder(local_win, ':', ':', '.', '.', '.', '.', '.', '.'); - wrefresh(local_win); - delwin(local_win); -} - -void print_menu(WINDOW *menu_win, int y, int x, int highlight) -{ - int i; - box(menu_win, 0, 0); - for(i = 0; i < n_mainMenu; ++i) - { if(highlight == i + 1) /* High light the present choice */ - { wattron(menu_win, A_REVERSE); - mvwprintw(menu_win, y, x, "%s", mainMenu[i]); - wattroff(menu_win, A_REVERSE); - } - else - mvwprintw(menu_win, y, x, "%s", mainMenu[i]); - ++y; - } - wrefresh(menu_win); -} - -void wwrap_print(WINDOW *win, int start_y, int start_x, const char *str) { - int max_width = getmaxx(win) - (start_y + 1); - int y = start_y; - int x = start_x; - int line_length = 0; - int word_length = 0; - const char *word_start = str; - - while (*str) { - // Trova la prossima parola - if (*str != ' ' && *str != '\n' && *str != '\t') { - str++; - continue; - } - - // Calcola lunghezza parola corrente - word_length = str - word_start; - - // Gestione wrap - if (line_length + word_length > max_width) { - y++; - x = start_x; - line_length = 0; - } - - // Stampa parola - for (int i = 0; i < word_length; i++) { - mvwaddch(win, y, x++, word_start[i]); - } - line_length += word_length; - - // Gestione spazi - if (*str == ' ' || *str == '\t') { - if (line_length < max_width) { - mvwaddch(win, y, x++, ' '); - line_length++; - } else { - y++; - x = start_x; - line_length = 0; - } - } else if (*str == '\n') { - y++; - x = start_x; - line_length = 0; - } - - word_start = ++str; - } - - // Stampa ultima parola - word_length = strlen(word_start); - if (line_length + word_length > max_width) { - y++; - x = start_x; - } - mvwprintw(win, y, x, "%s", word_start); -} - diff --git a/src/windowsBack.c b/src/windowsBack.c new file mode 100644 index 0000000..bf68c47 --- /dev/null +++ b/src/windowsBack.c @@ -0,0 +1,196 @@ +#include +#include +#include +#include + +#include "../include/windowsBack.h" +#include "../include/logic.h" +#include "../include/globals.h" +#define MAX_LEN 64 + +void warningf(const char *fmt, ...) { + char buf[256]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + warning(buf); +} + +void warning(char *string){ + WINDOW *warningWin; + int startx, starty, width, height; + height = LINES / 2 - 2; + width = COLS / 2; + starty = LINES / 2; + startx = 0; + warningWin = create_newwin(height, width, starty, startx); + wbkgd(warningWin, COLOR_PAIR(1)); + + mvwprintw(warningWin, 2, 2, "ATTENZIONE!!! E' avvenuto un errore! "); + wwrap_print(warningWin, 4, 3, string); + mvwprintw(warningWin, height - 1, 3, " Premi un tasto per continuare . . . "); + + wrefresh(warningWin); + getch(); + werase(warningWin); + destroy_win(warningWin); +} + +void statusf(const char *fmt, ...) { + char buf[256]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + status(buf); +} + +void status(char *msg) { + attron(COLOR_PAIR(3)); + mvprintw(LINES -2, 0, "%-*s", COLS, msg); + attroff(COLOR_PAIR(3)); + refresh(); +} + +WINDOW *create_newwin(int height, int width, int starty, int startx) +{ WINDOW *local_win; + + local_win = newwin(height, width, starty, startx); + box(local_win, 0 , 0); + wrefresh(local_win); + + return local_win; +} + +void destroy_win(WINDOW *local_win) +{ + //wborder(local_win, ':', ':', '.', '.', '.', '.', '.', '.'); + wrefresh(local_win); + delwin(local_win); +} + +void wwrap_print(WINDOW *win, int start_y, int start_x, const char *str) { + int max_width = getmaxx(win) - (start_y + 1); + int y = start_y; + int x = start_x; + int line_length = 0; + int word_length = 0; + const char *word_start = str; + + while (*str) { + // Trova la prossima parola + if (*str != ' ' && *str != '\n' && *str != '\t') { + str++; + continue; + } + + // Calcola lunghezza parola corrente + word_length = str - word_start; + + // Gestione wrap + if (line_length + word_length > max_width) { + y++; + x = start_x; + line_length = 0; + } + + // Stampa parola + for (int i = 0; i < word_length; i++) { + mvwaddch(win, y, x++, word_start[i]); + } + line_length += word_length; + + // Gestione spazi + if (*str == ' ' || *str == '\t') { + if (line_length < max_width) { + mvwaddch(win, y, x++, ' '); + line_length++; + } else { + y++; + x = start_x; + line_length = 0; + } + } else if (*str == '\n') { + y++; + x = start_x; + line_length = 0; + } + + word_start = ++str; + } + + // Stampa ultima parola + word_length = strlen(word_start); + if (line_length + word_length > max_width) { + y++; + x = start_x; + } + mvwprintw(win, y, x, "%s", word_start); +} + +int uiSelectMenu(WINDOW *win, int start_y, int start_x, char **options, int n_options) { + int highlight = 1, choice = 0, c; + keypad(win, TRUE); + while(1) { + for(int i = 0; i < n_options; ++i) { + if(highlight == i + 1) wattron(win, A_REVERSE); + mvwprintw(win, start_y + i, start_x, "%s", options[i]); + if(highlight == i + 1) wattroff(win, A_REVERSE); + } + wrefresh(win); + c = wgetch(win); + switch(c) { + case KEY_UP: case 'k': highlight = (highlight == 1) ? n_options : highlight - 1; break; + case KEY_DOWN: case 'j': highlight = (highlight == n_options) ? 1 : highlight + 1; break; + case 10: case 32: choice = highlight; break; + } + if(choice != 0) break; + } + return choice; +} + +void uiTextInput(WINDOW *win, int y, int x, char *input, int max_len) { + int ch, pos = strlen(input); + wattron(win, COLOR_PAIR(2)); + curs_set(1); + for(int i = 0; i < max_len; i++) mvwaddch(win, y, x + i, ' '); + mvwprintw(win, y, x, "%s", input); + wmove(win, y, x + pos); + wrefresh(win); + while ((ch = wgetch(win)) != '\n') { + if (ch == KEY_BACKSPACE || ch == 127 || ch == '\b') { + if (pos > 0) { + pos--; + input[pos] = '\0'; + mvwaddch(win, y, x + pos, ' '); + wmove(win, y, x + pos); + } + } else if (pos < max_len && ch >= 32 && ch <= 126) { + input[pos++] = ch; + input[pos] = '\0'; + mvwaddch(win, y, x + pos - 1, ch); + } + wrefresh(win); + } + curs_set(0); + wattroff(win, COLOR_PAIR(2)); +} + +int uiToggleSwitch(WINDOW *win, int y, int x, int state) { + int c; + keypad(win, TRUE); + while(1) { + wattron(win, A_REVERSE); + mvwprintw(win, y, x, state ? "[ ON ] " : "[ OFF ]"); + wattroff(win, A_REVERSE); + wrefresh(win); + c = wgetch(win); + if (c == 10 || c == 32) break; + if (c == KEY_LEFT || c == KEY_RIGHT || c == 'h' || c == 'j' || c == 'k' || c == 'l') state = !state; + } + mvwprintw(win, y, x, state ? "[ ON ] " : "[ OFF ]"); + wrefresh(win); + return state; +} + diff --git a/src/windowsDeclaration.c b/src/windowsDeclaration.c new file mode 100644 index 0000000..5802349 --- /dev/null +++ b/src/windowsDeclaration.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +#include "../include/windowsDeclaration.h" +#include "../include/windowsBack.h" +#include "../include/logic.h" +#include "../include/globals.h" +#define MAX_LEN 64 + +void example_window() { + WINDOW *win; + int height = LINES / 2 -2; + int width = COLS / 2; + int starty = LINES / 2; + int startx = COLS / 2; + win = create_newwin(height, width, starty, startx); + mvwprintw(win, 1, 2, "Esempio Interfaccia"); + + char *opts[] = {"Opzione 1", "Opzione 2", "Opzione 3"}; + mvwprintw(win, 3, 2, "Scegli dal menu:"); + int menu_res = uiSelectMenu(win, 4, 4, opts, 3); + + char txt_buf[64] = "Testo base"; + mvwprintw(win, 9, 2, "Inserisci testo:"); + uiTextInput(win, 9, 19, txt_buf, 30); + + mvwprintw(win, 11, 2, "Attiva opzione:"); + int switch_res = uiToggleSwitch(win, 11, 18, 0); + + mvwprintw(win, 13, 2, "Scelto: M=%d, T=%s, S=%d", menu_res, txt_buf, switch_res); + wrefresh(win); + wgetch(win); + werase(win); + destroy_win(win); +} + +int runMainMenu(){ + char *mainMenu[] = { + "Segna vendita", + "Vedi vendite", + "Vedi prodotti", + "Gestisci prodotti", + "Opzioni", + "Exit" + }; + int n_mainMenu = sizeof(mainMenu) / sizeof(char *); + + WINDOW *mainWin; + int height = LINES / 2; + int width = COLS / 2; + int starty = 0; + int startx = 0; + mainWin = create_newwin(height, width, starty, startx); + + wwrap_print(mainWin, 1, 2, "Usa le FRECCETTE ed INVIO per scegliere un'opzione,\nin alternativa ci sono i movement di vim e SPAZIO."); + refresh(); + + int choice = uiSelectMenu(mainWin, height / 2 - 3, width / 2 - 9, mainMenu, n_mainMenu); + statusf("Avvio opzione '%s'", mainMenu[choice - 1]); + + clrtoeol(); + refresh(); + + destroy_win(mainWin); + return choice; +} + +void runOptionsMenu(){ + WINDOW *optionsWin; + optionsWin = create_newwin(LINES/2, COLS/2, 0, COLS/2); + + char input[MAX_LEN + 1]; + strcpy(input, productPricesFileLocation); + + mvwprintw(optionsWin, 1, 2, "Posizione file prodotti:"); + uiTextInput(optionsWin, 2, 2, input, MAX_LEN); + + if (existsFile(input)) { + statusf("Il file '%s' esiste e verra' utilizzato al posto di '%s'", input, productPricesFileLocation); + strcpy(productPricesFileLocation, input); + } else { + warningf("Il file '%s' non esiste, utilizzo il vecchio file '%s'", input, productPricesFileLocation); + } + + werase(optionsWin); + destroy_win(optionsWin); +}