summaryrefslogtreecommitdiff
path: root/src/ui/documentwidget.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/documentwidget.c')
-rw-r--r--src/ui/documentwidget.c122
1 files changed, 62 insertions, 60 deletions
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c
index bacf8ff7..e87b1413 100644
--- a/src/ui/documentwidget.c
+++ b/src/ui/documentwidget.c
@@ -55,81 +55,40 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
55#include <ctype.h> 55#include <ctype.h>
56#include <errno.h> 56#include <errno.h>
57 57
58iDeclareClass(MediaRequest)
59
60struct Impl_MediaRequest {
61 iObject object;
62 iDocumentWidget *doc;
63 iGmLinkId linkId;
64 iGmRequest * req;
65};
66
67static void updated_MediaRequest_(iAnyObject *obj) {
68 iMediaRequest *d = obj;
69 postCommandf_App("media.updated link:%u request:%p", d->linkId, d);
70}
71
72static void finished_MediaRequest_(iAnyObject *obj) {
73 iMediaRequest *d = obj;
74 postCommandf_App("media.finished link:%u request:%p", d->linkId, d);
75}
76
77void init_MediaRequest(iMediaRequest *d, iDocumentWidget *doc, iGmLinkId linkId, const iString *url) {
78 d->doc = doc;
79 d->linkId = linkId;
80 d->req = new_GmRequest(certs_App());
81 setUrl_GmRequest(d->req, url);
82 iConnect(GmRequest, d->req, updated, d, updated_MediaRequest_);
83 iConnect(GmRequest, d->req, finished, d, finished_MediaRequest_);
84 submit_GmRequest(d->req);
85}
86
87void deinit_MediaRequest(iMediaRequest *d) {
88 iDisconnect(GmRequest, d->req, updated, d, updated_MediaRequest_);
89 iDisconnect(GmRequest, d->req, finished, d, finished_MediaRequest_);
90 iRelease(d->req);
91}
92
93iDefineObjectConstructionArgs(MediaRequest,
94 (iDocumentWidget *doc, iGmLinkId linkId, const iString *url),
95 doc, linkId, url)
96iDefineClass(MediaRequest)
97
98/*----------------------------------------------------------------------------------------------*/ 58/*----------------------------------------------------------------------------------------------*/
99 59
100iDeclareType(Model) 60iDeclareType(PersistentDocumentState)
101iDeclareTypeConstruction(Model) 61iDeclareTypeConstruction(PersistentDocumentState)
102iDeclareTypeSerialization(Model) 62iDeclareTypeSerialization(PersistentDocumentState)
103 63
104struct Impl_Model { 64struct Impl_PersistentDocumentState {
105 /* state that persists across sessions */
106 iHistory *history; 65 iHistory *history;
107 iString * url; 66 iString * url;
108}; 67};
109 68
110void init_Model(iModel *d) { 69void init_PersistentDocumentState(iPersistentDocumentState *d) {
111 d->history = new_History(); 70 d->history = new_History();
112 d->url = new_String(); 71 d->url = new_String();
113} 72}
114 73
115void deinit_Model(iModel *d) { 74void deinit_PersistentDocumentState(iPersistentDocumentState *d) {
116 delete_String(d->url); 75 delete_String(d->url);
117 delete_History(d->history); 76 delete_History(d->history);
118} 77}
119 78
120void serialize_Model(const iModel *d, iStream *outs) { 79void serialize_PersistentDocumentState(const iPersistentDocumentState *d, iStream *outs) {
121 serialize_String(d->url, outs); 80 serialize_String(d->url, outs);
122 write16_Stream(outs, 0 /*d->zoomPercent*/); 81 write16_Stream(outs, 0 /*d->zoomPercent*/);
123 serialize_History(d->history, outs); 82 serialize_History(d->history, outs);
124} 83}
125 84
126void deserialize_Model(iModel *d, iStream *ins) { 85void deserialize_PersistentDocumentState(iPersistentDocumentState *d, iStream *ins) {
127 deserialize_String(d->url, ins); 86 deserialize_String(d->url, ins);
128 /*d->zoomPercent =*/ read16_Stream(ins); 87 /*d->zoomPercent =*/ read16_Stream(ins);
129 deserialize_History(d->history, ins); 88 deserialize_History(d->history, ins);
130} 89}
131 90
132iDefineTypeConstruction(Model) 91iDefineTypeConstruction(PersistentDocumentState)
133 92
134/*----------------------------------------------------------------------------------------------*/ 93/*----------------------------------------------------------------------------------------------*/
135 94
@@ -167,7 +126,7 @@ enum iDocumentWidgetFlag {
167struct Impl_DocumentWidget { 126struct Impl_DocumentWidget {
168 iWidget widget; 127 iWidget widget;
169 enum iRequestState state; 128 enum iRequestState state;
170 iModel mod; 129 iPersistentDocumentState mod;
171 int flags; 130 int flags;
172 iString * titleUser; 131 iString * titleUser;
173 iGmRequest * request; 132 iGmRequest * request;
@@ -215,7 +174,7 @@ void init_DocumentWidget(iDocumentWidget *d) {
215 init_Widget(w); 174 init_Widget(w);
216 setId_Widget(w, "document000"); 175 setId_Widget(w, "document000");
217 setFlags_Widget(w, hover_WidgetFlag, iTrue); 176 setFlags_Widget(w, hover_WidgetFlag, iTrue);
218 init_Model(&d->mod); 177 init_PersistentDocumentState(&d->mod);
219 d->flags = 0; 178 d->flags = 0;
220 iZap(d->certExpiry); 179 iZap(d->certExpiry);
221 d->certFlags = 0; 180 d->certFlags = 0;
@@ -285,7 +244,7 @@ void deinit_DocumentWidget(iDocumentWidget *d) {
285 deinit_PtrArray(&d->visibleLinks); 244 deinit_PtrArray(&d->visibleLinks);
286 delete_String(d->certSubject); 245 delete_String(d->certSubject);
287 delete_String(d->titleUser); 246 delete_String(d->titleUser);
288 deinit_Model(&d->mod); 247 deinit_PersistentDocumentState(&d->mod);
289} 248}
290 249
291static void requestUpdated_DocumentWidget_(iAnyObject *obj) { 250static void requestUpdated_DocumentWidget_(iAnyObject *obj) {
@@ -1757,6 +1716,48 @@ static iBool processPlayerEvents_DocumentWidget_(iDocumentWidget *d, const SDL_E
1757 return iFalse; 1716 return iFalse;
1758} 1717}
1759 1718
1719static size_t linkOrdinalFromKey_(int key) {
1720 if (key >= '1' && key <= '9') {
1721 return key - '1';
1722 }
1723 if (key < 'a' || key > 'z') {
1724 return iInvalidPos;
1725 }
1726 int ord = key - 'a' + 9;
1727#if defined (iPlatformApple)
1728 /* Skip keys that would conflict with default system shortcuts: hide, minimize, quit, close. */
1729 if (key == 'h' || key == 'm' || key == 'q' || key == 'w') {
1730 return iInvalidPos;
1731 }
1732 if (key > 'h') ord--;
1733 if (key > 'm') ord--;
1734 if (key > 'q') ord--;
1735 if (key > 'w') ord--;
1736#endif
1737 return ord;
1738}
1739
1740static iChar linkOrdinalChar_(size_t ord) {
1741 if (ord < 9) {
1742 return 0x278a + ord;
1743 }
1744#if defined (iPlatformApple)
1745 if (ord < 9 + 22) {
1746 int key = 'a' + ord - 9;
1747 if (key >= 'h') key++;
1748 if (key >= 'm') key++;
1749 if (key >= 'q') key++;
1750 if (key >= 'w') key++;
1751 return 0x24b6 + key - 'a';
1752 }
1753#else
1754 if (ord < 9 + 26) {
1755 return 0x24b6 + ord - 9;
1756 }
1757#endif
1758 return 0;
1759}
1760
1760static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *ev) { 1761static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *ev) {
1761 iWidget *w = as_Widget(d); 1762 iWidget *w = as_Widget(d);
1762 if (ev->type == SDL_USEREVENT && ev->user.code == command_UserEventCode) { 1763 if (ev->type == SDL_USEREVENT && ev->user.code == command_UserEventCode) {
@@ -1787,12 +1788,12 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e
1787 } 1788 }
1788 } 1789 }
1789 if (ev->type == SDL_KEYDOWN) { 1790 if (ev->type == SDL_KEYDOWN) {
1790 const int mods = keyMods_Sym(ev->key.keysym.mod); 1791 const int key = ev->key.keysym.sym;
1791 const int key = ev->key.keysym.sym;
1792 if ((d->flags & showLinkNumbers_DocumentWidgetFlag) && 1792 if ((d->flags & showLinkNumbers_DocumentWidgetFlag) &&
1793 ((key >= '1' && key <= '9') || (key >= 'a' && key <= 'z'))) { 1793 ((key >= '1' && key <= '9') || (key >= 'a' && key <= 'z'))) {
1794 const size_t ord = isdigit(key) ? key - SDLK_1 : (key - 'a' + 9); 1794 const size_t ord = linkOrdinalFromKey_(key);
1795 iConstForEach(PtrArray, i, &d->visibleLinks) { 1795 iConstForEach(PtrArray, i, &d->visibleLinks) {
1796 if (ord == iInvalidPos) break;
1796 const iGmRun *run = i.ptr; 1797 const iGmRun *run = i.ptr;
1797 if (run->flags & decoration_GmRunFlag && 1798 if (run->flags & decoration_GmRunFlag &&
1798 visibleLinkOrdinal_DocumentWidget_(d, run->linkId) == ord) { 1799 visibleLinkOrdinal_DocumentWidget_(d, run->linkId) == ord) {
@@ -1954,6 +1955,7 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e
1954 if (processPlayerEvents_DocumentWidget_(d, ev)) { 1955 if (processPlayerEvents_DocumentWidget_(d, ev)) {
1955 return iTrue; 1956 return iTrue;
1956 } 1957 }
1958 /* The left mouse button. */
1957 switch (processEvent_Click(&d->click, ev)) { 1959 switch (processEvent_Click(&d->click, ev)) {
1958 case started_ClickResult: 1960 case started_ClickResult:
1959 iChangeFlags(d->flags, selecting_DocumentWidgetFlag, iFalse); 1961 iChangeFlags(d->flags, selecting_DocumentWidgetFlag, iFalse);
@@ -2204,8 +2206,8 @@ static void drawRun_DrawContext_(void *context, const iGmRun *run) {
2204 else { 2206 else {
2205 if (d->showLinkNumbers && run->linkId && run->flags & decoration_GmRunFlag) { 2207 if (d->showLinkNumbers && run->linkId && run->flags & decoration_GmRunFlag) {
2206 const size_t ord = visibleLinkOrdinal_DocumentWidget_(d->widget, run->linkId); 2208 const size_t ord = visibleLinkOrdinal_DocumentWidget_(d->widget, run->linkId);
2207 if (ord < 9 + 26) { 2209 const iChar ordChar = linkOrdinalChar_(ord);
2208 const iChar ordChar = ord < 9 ? 0x278a + ord : (0x24b6 + ord - 9); 2210 if (ordChar) {
2209 drawString_Text(run->font, 2211 drawString_Text(run->font,
2210 init_I2(d->viewPos.x - gap_UI / 3, visPos.y), 2212 init_I2(d->viewPos.x - gap_UI / 3, visPos.y),
2211 fg, 2213 fg,
@@ -2656,11 +2658,11 @@ const iString *bookmarkTitle_DocumentWidget(const iDocumentWidget *d) {
2656} 2658}
2657 2659
2658void serializeState_DocumentWidget(const iDocumentWidget *d, iStream *outs) { 2660void serializeState_DocumentWidget(const iDocumentWidget *d, iStream *outs) {
2659 serialize_Model(&d->mod, outs); 2661 serialize_PersistentDocumentState(&d->mod, outs);
2660} 2662}
2661 2663
2662void deserializeState_DocumentWidget(iDocumentWidget *d, iStream *ins) { 2664void deserializeState_DocumentWidget(iDocumentWidget *d, iStream *ins) {
2663 deserialize_Model(&d->mod, ins); 2665 deserialize_PersistentDocumentState(&d->mod, ins);
2664 parseUser_DocumentWidget_(d); 2666 parseUser_DocumentWidget_(d);
2665 updateFromHistory_DocumentWidget_(d); 2667 updateFromHistory_DocumentWidget_(d);
2666} 2668}