diff options
author | Paul Gardiner <paulg.artifex@glidos.net> | 2012-08-02 16:04:44 +0100 |
---|---|---|
committer | Paul Gardiner <paulg.artifex@glidos.net> | 2012-08-02 16:04:44 +0100 |
commit | 901f3ea1bb6ffc55f297a52b7b4c72138962a986 (patch) | |
tree | dca67928ce95e4458a76c4dff0c58b1758db0626 | |
parent | 8068b7ba7ac0f103f4770736518fe7e545ceba30 (diff) | |
download | mupdf-901f3ea1bb6ffc55f297a52b7b4c72138962a986.tar.xz |
Forms: add support for save on exit to the windows app
-rw-r--r-- | apps/jstest_main.c | 10 | ||||
-rw-r--r-- | apps/pdfapp.c | 43 | ||||
-rw-r--r-- | apps/pdfapp.h | 5 | ||||
-rw-r--r-- | apps/win_main.c | 49 | ||||
-rw-r--r-- | apps/x11_main.c | 12 |
5 files changed, 117 insertions, 2 deletions
diff --git a/apps/jstest_main.c b/apps/jstest_main.c index 01493f47..283d5e93 100644 --- a/apps/jstest_main.c +++ b/apps/jstest_main.c @@ -64,6 +64,16 @@ void winclose(pdfapp_t *app) exit(0); } +int winsavequery(pdfapp_t *app) +{ + return DISCARD; +} + +int wingetsavepath(pdfapp_t *app, char *buf, int len) +{ + return 0; +} + void wincursor(pdfapp_t *app, int curs) { } diff --git a/apps/pdfapp.c b/apps/pdfapp.c index 92ebe3a8..bc47cf9a 100644 --- a/apps/pdfapp.c +++ b/apps/pdfapp.c @@ -7,6 +7,9 @@ #define ZOOMSTEP 1.142857 #define BEYOND_THRESHHOLD 40 +#ifndef PATH_MAX +#define PATH_MAX (1024) +#endif enum panning { @@ -195,6 +198,46 @@ void pdfapp_close(pdfapp_t *app) fz_flush_warnings(app->ctx); } +int pdfapp_preclose(pdfapp_t *app) +{ + fz_interactive *idoc = fz_interact(app->doc); + + if (idoc && fz_has_unsaved_changes(idoc)) + { + char buf[PATH_MAX]; + + switch (winsavequery(app)) + { + case DISCARD: + return 1; + + case CANCEL: + return 0; + + case SAVE: + if (wingetsavepath(app, buf, PATH_MAX)) + { + fz_write_options opts; + + opts.do_ascii = 1; + opts.do_expand = 0; + opts.do_garbage = 1; + opts.do_linear = 0; + + fz_write_document(app->doc, buf, &opts); + + return 1; + } + else + { + return 0; + } + } + } + + return 1; +} + static fz_matrix pdfapp_viewctm(pdfapp_t *app) { fz_matrix ctm; diff --git a/apps/pdfapp.h b/apps/pdfapp.h index 30b3d667..070eb157 100644 --- a/apps/pdfapp.h +++ b/apps/pdfapp.h @@ -16,6 +16,8 @@ typedef struct pdfapp_s pdfapp_t; enum { ARROW, HAND, WAIT, CARET }; +enum { DISCARD, SAVE, CANCEL }; + extern void winwarn(pdfapp_t*, char *s); extern void winerror(pdfapp_t*, char *s); extern void wintitle(pdfapp_t*, char *title); @@ -32,6 +34,8 @@ extern void windrawstring(pdfapp_t*, int x, int y, char *s); extern void winclose(pdfapp_t*); extern void winhelp(pdfapp_t*); extern void winfullscreen(pdfapp_t*, int state); +extern int winsavequery(pdfapp_t*); +extern int wingetsavepath(pdfapp_t*, char *buf, int len); struct pdfapp_s { @@ -107,6 +111,7 @@ struct pdfapp_s void pdfapp_init(fz_context *ctx, pdfapp_t *app); void pdfapp_open(pdfapp_t *app, char *filename, int reload); void pdfapp_close(pdfapp_t *app); +int pdfapp_preclose(pdfapp_t *app); char *pdfapp_version(pdfapp_t *app); char *pdfapp_usage(pdfapp_t *app); diff --git a/apps/win_main.c b/apps/win_main.c index 71cf9203..a76fe053 100644 --- a/apps/win_main.c +++ b/apps/win_main.c @@ -99,6 +99,16 @@ void winerror(pdfapp_t *app, char *msg) exit(1); } +int winsavequery(pdfapp_t *app) +{ + switch(MessageBoxA(hwndframe, "File has unsaved changes. Do you want to save", "MuPDF", MB_YESNOCANCEL)) + { + case IDYES: return SAVE; + case IDNO: return DISCARD; + default: return CANCEL; + } +} + int winfilename(wchar_t *buf, int len) { OPENFILENAME ofn; @@ -115,6 +125,34 @@ int winfilename(wchar_t *buf, int len) return GetOpenFileNameW(&ofn); } +int wingetsavepath(pdfapp_t *app, char *buf, int len) +{ + OPENFILENAMEA ofn; + buf[0] = 0; + if (strlen(filename) < (unsigned int)len) + strcpy(buf, filename); + memset(&ofn, 0, sizeof(OPENFILENAME)); + ofn.lStructSize = sizeof(OPENFILENAME); + ofn.hwndOwner = hwndframe; + ofn.lpstrFile = buf; + ofn.nMaxFile = len; + ofn.lpstrInitialDir = NULL; + ofn.lpstrTitle = "MuPDF: Save PDF file"; + ofn.lpstrFilter = "Documents (*.pdf;*.xps;*.cbz;*.zip)\0*.zip;*.cbz;*.xps;*.pdf\0PDF Files (*.pdf)\0*.pdf\0XPS Files (*.xps)\0*.xps\0CBZ Files (*.cbz;*.zip)\0*.zip;*.cbz\0All Files\0*\0\0"; + ofn.Flags = OFN_HIDEREADONLY; + if (GetSaveFileNameA(&ofn)) + { + if (strlen(buf) < sizeof(filename)) + strcpy(filename, buf); + + return 1; + } + else + { + return 0; + } +} + static char pd_filename[256] = "The file is encrypted."; static char pd_password[256] = ""; static char td_textinput[1024] = ""; @@ -411,8 +449,11 @@ void winopen() void winclose(pdfapp_t *app) { - pdfapp_close(app); - exit(0); + if (pdfapp_preclose(app)) + { + pdfapp_close(app); + exit(0); + } } void wincursor(pdfapp_t *app, int curs) @@ -740,6 +781,10 @@ frameproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_NOTIFY: case WM_COMMAND: return SendMessage(hwndview, message, wParam, lParam); + + case WM_CLOSE: + if (!pdfapp_preclose(&gapp)) + return 0; } return DefWindowProc(hwnd, message, wParam, lParam); diff --git a/apps/x11_main.c b/apps/x11_main.c index ec78695d..0f1aeb7a 100644 --- a/apps/x11_main.c +++ b/apps/x11_main.c @@ -226,6 +226,18 @@ void winclose(pdfapp_t *app) closing = 1; } +int winsavequery(pdfapp_t *app) +{ + /* FIXME: temporary dummy implementation */ + return DISCARD; +} + +int wingetsavepath(pdfapp_t *app, char *buf, int len) +{ + /* FIXME: temporary dummy implementation */ + return 0; +} + void cleanup(pdfapp_t *app) { fz_context *ctx = app->ctx; |