feat: add common dialogs, i.e. open/save dialog
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "pdfviewer.h"
|
||||
#include <gui/dialogs.hpp>
|
||||
#include <gui/standalone.hpp>
|
||||
|
||||
// ============================================================================
|
||||
@@ -37,14 +38,15 @@ char g_status_msg[128] = {};
|
||||
// Toolbar hit-testing constants
|
||||
// ============================================================================
|
||||
|
||||
// Toolbar buttons: Open | < > | - +
|
||||
// Toolbar buttons: Open | Print | < > | - +
|
||||
static constexpr int TB_OPEN_X0 = 4, TB_OPEN_X1 = 40;
|
||||
// separator at 48
|
||||
static constexpr int TB_PREV_X0 = 56, TB_PREV_X1 = 80;
|
||||
static constexpr int TB_NEXT_X0 = 84, TB_NEXT_X1 = 108;
|
||||
// separator at 116
|
||||
static constexpr int TB_ZOUT_X0 = 124, TB_ZOUT_X1 = 148;
|
||||
static constexpr int TB_ZIN_X0 = 152, TB_ZIN_X1 = 176;
|
||||
static constexpr int TB_PRINT_X0 = 44, TB_PRINT_X1 = 80;
|
||||
// separator at 88
|
||||
static constexpr int TB_PREV_X0 = 96, TB_PREV_X1 = 120;
|
||||
static constexpr int TB_NEXT_X0 = 124, TB_NEXT_X1 = 148;
|
||||
// separator at 156
|
||||
static constexpr int TB_ZOUT_X0 = 164, TB_ZOUT_X1 = 188;
|
||||
static constexpr int TB_ZIN_X0 = 192, TB_ZIN_X1 = 216;
|
||||
|
||||
// ============================================================================
|
||||
// Helpers
|
||||
@@ -66,6 +68,15 @@ static void clamp_scroll() {
|
||||
if (g_scroll_y > ms) g_scroll_y = ms;
|
||||
}
|
||||
|
||||
static const char* file_basename(const char* path) {
|
||||
if (!path || !path[0]) return "";
|
||||
const char* name = path;
|
||||
for (int i = 0; path[i]; i++) {
|
||||
if (path[i] == '/') name = path + i + 1;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
static void go_to_page(int page) {
|
||||
if (page < 0) page = 0;
|
||||
if (page >= g_doc.page_count) page = g_doc.page_count - 1;
|
||||
@@ -99,6 +110,40 @@ static void open_file(const char* path) {
|
||||
}
|
||||
}
|
||||
|
||||
static void open_inline_pathbar(const char* initial_text) {
|
||||
g_pathbar_open = true;
|
||||
if (!initial_text) initial_text = "";
|
||||
str_cpy(g_pathbar_text, initial_text, sizeof(g_pathbar_text));
|
||||
g_pathbar_len = str_len(g_pathbar_text);
|
||||
g_pathbar_cursor = g_pathbar_len;
|
||||
}
|
||||
|
||||
static void open_file_dialog() {
|
||||
char path[256] = {};
|
||||
if (dialogs::open_file("Open PDF", g_filepath, path, sizeof(path), g_status_msg, sizeof(g_status_msg))) {
|
||||
open_file(path);
|
||||
} else if (g_status_msg[0]) {
|
||||
open_inline_pathbar(g_filepath);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_file_dialog() {
|
||||
if (!g_filepath[0]) {
|
||||
str_cpy(g_status_msg, "Open a PDF before printing", sizeof(g_status_msg));
|
||||
return;
|
||||
}
|
||||
|
||||
char job_id[64] = {};
|
||||
char msg[128] = {};
|
||||
if (dialogs::print_file("Print PDF", g_filepath, file_basename(g_filepath),
|
||||
job_id, sizeof(job_id), msg, sizeof(msg))) {
|
||||
if (msg[0]) str_cpy(g_status_msg, msg, sizeof(g_status_msg));
|
||||
else str_cpy(g_status_msg, "Print job queued", sizeof(g_status_msg));
|
||||
} else if (msg[0]) {
|
||||
str_cpy(g_status_msg, msg, sizeof(g_status_msg));
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Entry point
|
||||
// ============================================================================
|
||||
@@ -126,9 +171,7 @@ extern "C" void _start() {
|
||||
// Build window title
|
||||
char title[64] = "PDF Viewer";
|
||||
if (g_filepath[0]) {
|
||||
const char* fname = g_filepath;
|
||||
for (int i = 0; g_filepath[i]; i++)
|
||||
if (g_filepath[i] == '/') fname = g_filepath + i + 1;
|
||||
const char* fname = file_basename(g_filepath);
|
||||
snprintf(title, 64, "%s - PDF Viewer", fname);
|
||||
}
|
||||
|
||||
@@ -211,10 +254,12 @@ extern "C" void _start() {
|
||||
}
|
||||
// Ctrl+O: open file
|
||||
else if (key.ctrl && (key.ascii == 'o' || key.ascii == 'O' || key.ascii == 15)) {
|
||||
g_pathbar_open = true;
|
||||
g_pathbar_text[0] = '\0';
|
||||
g_pathbar_len = 0;
|
||||
g_pathbar_cursor = 0;
|
||||
open_file_dialog();
|
||||
redraw = true;
|
||||
}
|
||||
// Ctrl+P: print file
|
||||
else if (key.ctrl && (key.ascii == 'p' || key.ascii == 'P' || key.ascii == 16)) {
|
||||
print_file_dialog();
|
||||
redraw = true;
|
||||
}
|
||||
// Page Down / Right arrow: next page
|
||||
@@ -288,10 +333,10 @@ extern "C" void _start() {
|
||||
|
||||
if (clicked && my < TOOLBAR_H && my >= TB_BTN_Y && my < TB_BTN_Y + TB_BTN_SIZE) {
|
||||
if (mx >= TB_OPEN_X0 && mx < TB_OPEN_X1) {
|
||||
g_pathbar_open = true;
|
||||
g_pathbar_text[0] = '\0';
|
||||
g_pathbar_len = 0;
|
||||
g_pathbar_cursor = 0;
|
||||
open_file_dialog();
|
||||
redraw = true;
|
||||
} else if (mx >= TB_PRINT_X0 && mx < TB_PRINT_X1) {
|
||||
print_file_dialog();
|
||||
redraw = true;
|
||||
} else if (mx >= TB_PREV_X0 && mx < TB_PREV_X1 && g_doc.valid) {
|
||||
go_to_page(g_current_page - 1);
|
||||
|
||||
@@ -180,8 +180,9 @@ void render(uint32_t* pixels) {
|
||||
bx += 8;
|
||||
};
|
||||
|
||||
// Open
|
||||
// Open / Print
|
||||
tb_btn(36, false, "Open");
|
||||
tb_btn(36, false, "Print");
|
||||
tb_sep();
|
||||
|
||||
// Navigation
|
||||
|
||||
Reference in New Issue
Block a user