summaryrefslogtreecommitdiff
path: root/src/ui/documentwidget.c
blob: 7fe30f1e7a817db27a1bbe188e2b09c581095aeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "documentwidget.h"
#include "paint.h"
#include "util.h"
#include "app.h"
#include "../gemini.h"
#include "../gmdocument.h"

#include <the_Foundation/file.h>
#include <the_Foundation/regexp.h>
#include <the_Foundation/tlsrequest.h>

enum iDocumentState {
    blank_DocumentState,
    fetching_DocumentState,
    layout_DocumentState,
    ready_DocumentState,
};

struct Impl_DocumentWidget {
    iWidget widget;
    enum iDocumentState state;
    iString *url;
    iTlsRequest *request;
    int statusCode;
    iString *newSource;
    iGmDocument *doc;
    int pageMargin;
    int scrollY;
};

iDeclareType(Url)

struct Impl_Url {
    iRangecc protocol;
    iRangecc host;
    iRangecc port;
    iRangecc path;
    iRangecc query;
};

void init_Url(iUrl *d, const iString *text) {
    iRegExp *pattern =
        new_RegExp("(.+)://([^/:?]*)(:[0-9]+)?([^?]*)(\\?.*)?", caseInsensitive_RegExpOption);
    iRegExpMatch m;
    if (matchString_RegExp(pattern, text, &m)) {
        capturedRange_RegExpMatch(&m, 1, &d->protocol);
        capturedRange_RegExpMatch(&m, 2, &d->host);
        capturedRange_RegExpMatch(&m, 3, &d->port);
        if (!isEmpty_Range(&d->port)) {
            /* Don't include the colon. */
            d->port.start++;
        }
        capturedRange_RegExpMatch(&m, 4, &d->path);
        capturedRange_RegExpMatch(&m, 5, &d->query);
    }
    else {
        iZap(*d);
    }
    iRelease(pattern);
}

iDefineObjectConstruction(DocumentWidget)

void init_DocumentWidget(iDocumentWidget *d) {
    iWidget *w = as_Widget(d);
    init_Widget(w);
    setBackgroundColor_Widget(w, gray25_ColorId);
    d->state = blank_DocumentState;
    d->url = new_String();
    d->statusCode = 0;
    d->request = NULL;
    d->newSource = new_String();
    d->doc = new_GmDocument();
    d->pageMargin = 5;
    d->scrollY = 0;
    setUrl_DocumentWidget(d, collectNewCStr_String("file:///home/jaakko/test.gmi"));
}

void deinit_DocumentWidget(iDocumentWidget *d) {
    delete_String(d->url);
    delete_String(d->newSource);
    iRelease(d->doc);
}

static int documentWidth_DocumentWidget_(const iDocumentWidget *d) {
    const iWidget *w = constAs_Widget(d);
    const iRect bounds = bounds_Widget(w);
    return bounds.size.x - gap_UI * d->pageMargin * 2;
}

void setSource_DocumentWidget(iDocumentWidget *d, const iString *source) {
    /* TODO: lock source during update */
    setSource_GmDocument(d->doc, source, documentWidth_DocumentWidget_(d));
    d->state = ready_DocumentState;
}

static iRangecc getLine_(iRangecc text) {
    iRangecc line = { text.start, text.start };
    for (; *line.end != '\n' && line.end != text.end; line.end++) {}
    return line;
}

static void requestFinished_DocumentWidget_(iAnyObject *obj) {
    iDocumentWidget *d = obj;
    iBlock *response = readAll_TlsRequest(d->request);
    iRangecc responseRange = { constBegin_Block(response), constEnd_Block(response) };
    iRangecc respLine = getLine_(responseRange);
    responseRange.start = respLine.end + 1;
    /* First line is the status code. */ {
        iString *line = newRange_String(respLine);
        trim_String(line);
        d->statusCode = toInt_String(line);
        printf("response (%02d): %s\n", d->statusCode, cstr_String(line));
        /* TODO: post a command with the status code */
        delete_String(line);
    }
    setCStrN_String(d->newSource, responseRange.start, size_Range(&responseRange));
    delete_Block(response);
    iReleaseLater(d->request);
    d->request = NULL;
    fflush(stdout);
    postRefresh_App();
}

static void fetch_DocumentWidget_(iDocumentWidget *d) {
    iAssert(!d->request);
    d->state = fetching_DocumentState;
    d->statusCode = 0;
    iUrl url;
    init_Url(&url, d->url);
    if (!cmpCStrSc_Rangecc(&url.protocol, "file", &iCaseInsensitive)) {
        iFile *f = new_File(collect_String(newRange_String(url.path)));
        if (open_File(f, readOnly_FileMode)) {
            setBlock_String(d->newSource, collect_Block(readAll_File(f)));
            postRefresh_App();
        }
        iRelease(f);
        return;
    }
    d->request = new_TlsRequest();
    uint16_t port = toInt_String(collect_String(newRange_String(url.port)));
    if (port == 0) {
        port = 1965; /* default Gemini port */
    }
    setUrl_TlsRequest(d->request, collect_String(newRange_String(url.host)), port);
    /* The request string is an UTF-8 encoded absolute URL. */
    iString *content = collectNew_String();
    append_String(content, d->url);
    appendCStr_String(content, "\r\n");
    setContent_TlsRequest(d->request, utf8_String(content));
    iConnect(TlsRequest, d->request, finished, d, requestFinished_DocumentWidget_);
    submit_TlsRequest(d->request);
}

void setUrl_DocumentWidget(iDocumentWidget *d, const iString *url) {
    clear_String(d->url);
    if (indexOfCStr_String(url, "://") == iInvalidPos && !startsWithCase_String(url, "gemini:")) {
        /* Prepend default protocol. */
        setCStr_String(d->url, "gemini://");
    }
    append_String(d->url, url);
    fetch_DocumentWidget_(d);
}

static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *ev) {
    iWidget *w = as_Widget(d);
    if (isResize_UserEvent(ev)) {
        setWidth_GmDocument(d->doc, documentWidth_DocumentWidget_(d));
    }
    if (ev->type == SDL_KEYDOWN) {
        const int mods = keyMods_Sym(ev->key.keysym.mod);
        const int key = ev->key.keysym.sym;
        if (mods == KMOD_PRIMARY && key == 'r') {
            fetch_DocumentWidget_(d);
            return iTrue;
        }
    }
    else if (ev->type == SDL_MOUSEWHEEL) {
        d->scrollY -= 3 * ev->wheel.y * lineHeight_Text(default_FontId);
        postRefresh_App();
        return iTrue;
    }
    return processEvent_Widget(w, ev);
}

iDeclareType(DrawContext)

struct Impl_DrawContext {
    const iDocumentWidget *widget;
    iRect bounds;
    iPaint paint;
};

static void drawRun_DrawContext_(void *context, const iGmRun *run) {
    iDrawContext *d = context;
    iString text;
    /* TODO: making a copy is unnecessary; the text routines should accept Rangecc */
    initRange_String(&text, run->text);
    iInt2 origin = addY_I2(d->bounds.pos, -d->widget->scrollY);
    drawString_Text(run->font, add_I2(run->bounds.pos, origin), run->color, &text);
//    drawRect_Paint(&d->paint, moved_Rect(run->bounds, origin), red_ColorId);
    deinit_String(&text);
}

static void draw_DocumentWidget_(const iDocumentWidget *d) {
    const iWidget *w = constAs_Widget(d);
    draw_Widget(w);
    /* Update the document? */
    if (!isEmpty_String(d->newSource)) {
        /* TODO: Do this in the background. However, that requires a text metrics calculator
           that does not try to cache the glyph bitmaps. */
        setSource_GmDocument(d->doc, d->newSource, documentWidth_DocumentWidget_(d));
        clear_String(d->newSource);
        iConstCast(iDocumentWidget *, d)->state = ready_DocumentState;
    }
    if (d->state != ready_DocumentState) return;
    iDrawContext ctx = {.widget = d, .bounds = bounds_Widget(w) };
    shrink_Rect(&ctx.bounds, init1_I2(gap_UI * d->pageMargin));
    init_Paint(&ctx.paint);
    drawRect_Paint(&ctx.paint, ctx.bounds, teal_ColorId);
    render_GmDocument(d->doc,
                      (iRangei){ d->scrollY, d->scrollY + height_Rect(ctx.bounds) },
                      drawRun_DrawContext_,
                      &ctx);
}

iBeginDefineSubclass(DocumentWidget, Widget)
    .processEvent = (iAny *) processEvent_DocumentWidget_,
    .draw         = (iAny *) draw_DocumentWidget_,
iEndDefineSubclass(DocumentWidget)