SmartPDF - lightweight pdf viewer app for rosapps
[reactos.git] / rosapps / smartpdf / src / DisplayState.cc
1 /* Copyright Krzysztof Kowalczyk 2006-2007
2 License: GPLv2 */
3 #include "DisplayState.h"
4 #include "str_util.h"
5 #include "dstring.h"
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 void normalizeRotation(int *rotation)
12 {
13 assert(rotation);
14 if (!rotation) return;
15 while (*rotation < 0)
16 *rotation += 360;
17 while (*rotation >= 360)
18 *rotation -= 360;
19 }
20
21 BOOL validRotation(int rotation)
22 {
23 normalizeRotation(&rotation);
24 if ((0 == rotation) || (90 == rotation) ||
25 (180 == rotation) || (270 == rotation))
26 return TRUE;
27 return FALSE;
28 }
29
30 BOOL ValidZoomVirtual(double zoomVirtual)
31 {
32 if ((ZOOM_FIT_PAGE == zoomVirtual) || (ZOOM_FIT_WIDTH == zoomVirtual))
33 return TRUE;
34 if ((zoomVirtual < ZOOM_MIN) || (zoomVirtual > ZOOM_MAX)) {
35 DBG_OUT("ValidZoomVirtual() invalid zoom: %.4f\n", zoomVirtual);
36 return FALSE;
37 }
38 return TRUE;
39 }
40
41 #define STR_FROM_ENUM(val) \
42 if (val == var) \
43 return val##_STR;
44
45 const char *DisplayModeNameFromEnum(DisplayMode var)
46 {
47 STR_FROM_ENUM(DM_SINGLE_PAGE)
48 STR_FROM_ENUM(DM_FACING)
49 STR_FROM_ENUM(DM_CONTINUOUS)
50 STR_FROM_ENUM(DM_CONTINUOUS_FACING)
51 return NULL;
52 }
53
54 #define IS_STR_ENUM(enumName) \
55 if (str_eq(txt, enumName##_STR)) { \
56 *resOut = enumName; \
57 return TRUE; \
58 }
59
60 BOOL DisplayModeEnumFromName(const char *txt, DisplayMode *resOut)
61 {
62 IS_STR_ENUM(DM_SINGLE_PAGE)
63 IS_STR_ENUM(DM_FACING)
64 IS_STR_ENUM(DM_CONTINUOUS)
65 IS_STR_ENUM(DM_CONTINUOUS_FACING)
66 assert(0);
67 return FALSE;
68 }
69
70 void DisplayState_Init(DisplayState *ds)
71 {
72 memzero(ds, sizeof(DisplayState));
73 ds->displayMode = DM_SINGLE_PAGE;
74 ds->visible = FALSE;
75 ds->fullScreen = FALSE;
76 ds->pageNo = 1;
77 ds->zoomVirtual = 100.0;
78 ds->rotation = 0;
79 }
80
81 void DisplayState_Free(DisplayState *ds)
82 {
83 free((void*)ds->filePath);
84 DisplayState_Init(ds);
85 }
86
87 BOOL DisplayState_Serialize(DisplayState *ds, DString *strOut)
88 {
89 const char * displayModeName = NULL;
90
91 DStringSprintf(strOut, " %s: %s\n", FILE_STR, ds->filePath);
92
93 displayModeName = DisplayModeNameFromEnum(ds->displayMode);
94 if (displayModeName)
95 DStringSprintf(strOut, " %s: %s\n", DISPLAY_MODE_STR, displayModeName);
96 else
97 DStringSprintf(strOut, " %s: %s\n", DISPLAY_MODE_STR, DisplayModeNameFromEnum(DM_SINGLE_PAGE));
98
99 DStringSprintf(strOut, " %s: %d\n", VISIBLE_STR, ds->visible);
100 DStringSprintf(strOut, " %s: %d\n", PAGE_NO_STR, ds->pageNo);
101 DStringSprintf(strOut, " %s: %.4f\n", ZOOM_VIRTUAL_STR, ds->zoomVirtual);
102 DStringSprintf(strOut, " %s: %d\n", ROTATION_STR, ds->rotation);
103 DStringSprintf(strOut, " %s: %d\n", FULLSCREEN_STR, (int)ds->fullScreen);
104
105 DStringSprintf(strOut, " %s: %d\n", SCROLL_X_STR, ds->scrollX);
106 DStringSprintf(strOut, " %s: %d\n", SCROLL_Y_STR, ds->scrollY);
107
108 DStringSprintf(strOut, " %s: %d\n", WINDOW_X_STR, ds->windowX);
109 DStringSprintf(strOut, " %s: %d\n", WINDOW_Y_STR, ds->windowY);
110 DStringSprintf(strOut, " %s: %d\n", WINDOW_DX_STR, ds->windowDx);
111 DStringSprintf(strOut, " %s: %d\n", WINDOW_DY_STR, ds->windowDy);
112 return TRUE;
113 }
114