Dillo v3.2.0
Loading...
Searching...
No Matches
plain.cc
Go to the documentation of this file.
1/*
2 * File: plain.cc
3 *
4 * Copyright (C) 2005-2007 Jorge Arellano Cid <jcid@dillo.org>
5 * Copyright (C) 2024 Rodrigo Arias Mallo <rodarima@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
18#include "msg.h"
19#include "prefs.h"
20#include "cache.h"
21#include "bw.h"
22#include "web.hh"
23#include "misc.h"
24#include "styleengine.hh"
25
26#include "uicmd.hh"
27
28#include "dw/core.hh"
29#include "dw/textblock.hh"
30
31// Dw to Textblock
32#define DW2TB(dw) ((Textblock*)dw)
33
34using namespace dw;
35using namespace dw::core;
36
37
38class DilloPlain {
39private:
40 class PlainLinkReceiver: public dw::core::Layout::LinkReceiver {
41 public:
42 DilloPlain *plain;
43 bool press(dw::core::Widget *widget, int link, int img, int x, int y,
45 };
46 PlainLinkReceiver plainReceiver;
47
48 void addLine(char *Buf, uint_t BufSize);
49
50public:
51 BrowserWindow *bw;
52
53 Widget *dw;
54 style::Style *widgetStyle;
55 size_t Start_Ofs; /* Offset of where to start reading next */
56 int state;
57 long currentLine;
58
59 DilloPlain(BrowserWindow *bw);
60 ~DilloPlain();
61
62 void write(void *Buf, uint_t BufSize, int Eof);
63};
64
65/* FSM states */
66enum {
69 ST_Eof
70};
71
72/*
73 * Exported function with C linkage.
74 */
75extern "C" {
76void *a_Plain_text(const char *type, void *P, CA_Callback_t *Call,void **Data);
77}
78
79/*
80 * Forward declarations
81 */
82static void Plain_callback(int Op, CacheClient_t *Client);
83void a_Plain_free(void *data);
84
85
89DilloPlain::DilloPlain(BrowserWindow *p_bw)
90{
91 /* Init event receiver */
92 plainReceiver.plain = this;
93
94 /* Init internal variables */
95 bw = p_bw;
97 Start_Ofs = 0;
98 state = ST_SeekingEol;
99 currentLine = 0L;
100
101 Layout *layout = (Layout*) bw->render_layout;
102 // TODO (1x) No URL?
103 StyleEngine styleEngine (layout, NULL, NULL, bw->zoom);
104
105 styleEngine.startElement ("body", bw);
106 styleEngine.startElement ("pre", bw);
107 widgetStyle = styleEngine.wordStyle (bw);
108 widgetStyle->ref ();
109
110 /* The context menu */
111 layout->connectLink (&plainReceiver);
112
113 /* Hook destructor to the dw delete call */
114 dw->setDeleteCallback(a_Plain_free, this);
115}
116
120DilloPlain::~DilloPlain()
121{
122 _MSG("::~DilloPlain()\n");
123 widgetStyle->unref();
124}
125
129bool DilloPlain::PlainLinkReceiver::press (Widget *widget, int, int, int, int,
130 EventButton *event)
131{
132 _MSG("DilloPlain::PlainLinkReceiver::buttonPress\n");
133
134 if (event->button == 3) {
135 a_UIcmd_page_popup(plain->bw, FALSE, NULL);
136 return true;
137 }
138 return false;
139}
140
141void DilloPlain::addLine(char *Buf, uint_t BufSize)
142{
143 int len;
144 char buf[129];
145 char *end = Buf + BufSize;
146
147 currentLine++; /* Start at 1 */
148 sprintf(buf, "L%ld", currentLine); /* Always fits */
149 DW2TB(dw)->addAnchor(buf, widgetStyle);
150
151 if (BufSize > 0) {
152 // Limit word length to avoid X11 coordinate
153 // overflow with extremely long lines.
154 while ((len = a_Misc_expand_tabs(&Buf, end, buf, sizeof(buf) - 1))) {
155 assert ((uint_t)len < sizeof(buf));
156 buf[len] = '\0';
157 DW2TB(dw)->addText(buf, len, widgetStyle);
158 }
159 } else {
160 // Add dummy word for empty lines - otherwise the parbreak is ignored.
161 DW2TB(dw)->addText("", 0, widgetStyle);
162 }
163
164 DW2TB(dw)->addParbreak(0, widgetStyle);
165}
166
171void DilloPlain::write(void *Buf, uint_t BufSize, int Eof)
172{
173 char *Start;
174 uint_t i, len, MaxBytes;
175
176 _MSG("DilloPlain::write Eof=%d\n", Eof);
177
178 Start = (char*)Buf + Start_Ofs;
179 MaxBytes = BufSize - Start_Ofs;
180 i = len = 0;
181 while ( i < MaxBytes ) {
182 switch ( state ) {
183 case ST_SeekingEol:
184 if (Start[i] == '\n' || Start[i] == '\r')
185 state = ST_Eol;
186 else {
187 ++i; ++len;
188 }
189 break;
190 case ST_Eol:
191 addLine(Start + i - len, len);
192 if (Start[i] == '\r' && Start[i + 1] == '\n') ++i;
193 if (i < MaxBytes) ++i;
194 state = ST_SeekingEol;
195 len = 0;
196 break;
197 }
198 }
199 Start_Ofs += i - len;
200 if (Eof && len) {
201 addLine(Start + i - len, len);
202 Start_Ofs += len;
203 }
204
205 DW2TB(dw)->flush();
206}
207
211void *a_Plain_text(const char *type, void *P, CA_Callback_t *Call, void **Data)
212{
213 DilloWeb *web = (DilloWeb*)P;
214 DilloPlain *plain = new DilloPlain(web->bw);
215
217 *Data = (void*)plain;
218
219 return (void*)plain->dw;
220}
221
222void a_Plain_free(void *data)
223{
224 _MSG("a_Plain_free! %p\n", data);
225 delete ((DilloPlain *)data);
226}
227
231static void Plain_callback(int Op, CacheClient_t *Client)
232{
233 DilloPlain *plain = (DilloPlain*)Client->CbData;
234
235 if (Op) {
236 /* Do the last line: */
237 plain->write(Client->Buf, Client->BufSize, 1);
238 /* remove this client from our active list */
239 a_Bw_close_client(plain->bw, Client->Key);
240 } else {
241 plain->write(Client->Buf, Client->BufSize, 0);
242 }
243}
244
#define _MSG(...)
Definition bookmarks.c:45
void a_Bw_close_client(BrowserWindow *bw, int ClientKey)
Close a cache-client upon successful retrieval.
Definition bw.c:167
void(* CA_Callback_t)(int Op, CacheClient_t *Client)
Callback type for cache clients.
Definition cache.h:43
This class provides the glue between HTML parser and CSS subsystem.
A Widget for rendering text blocks, i.e.
Definition textblock.hh:206
Represents a button press or release event.
Definition events.hh:58
The central class for managing and drawing a widget tree.
Definition layout.hh:17
void connectLink(LinkReceiver *receiver)
Definition layout.hh:259
The base class of all dillo widgets.
Definition widget.hh:44
unsigned int uint_t
Definition d_size.h:20
#define FALSE
Definition dlib.h:19
static Style * widgetStyle
static Layout * layout
int a_Misc_expand_tabs(char **start, char *end, char *buf, int buflen)
Takes a string and converts any tabs to spaces.
Definition misc.c:54
The core of Dw is defined in this namespace.
Definition core.hh:23
Dw is in this namespace, or sub namespaces of this one.
@ ST_SeekingEol
Definition plain.cc:67
@ ST_Eol
Definition plain.cc:68
@ ST_Eof
Definition plain.cc:69
void * a_Plain_text(const char *type, void *P, CA_Callback_t *Call, void **Data)
Set callback function and callback data for "text/" MIME major-type.
Definition plain.cc:211
#define DW2TB(dw)
Definition plain.cc:32
void a_Plain_free(void *data)
Definition plain.cc:222
static void Plain_callback(int Op, CacheClient_t *Client)
This function is a cache client.
Definition plain.cc:231
DilloPrefs prefs
Global Data.
Definition prefs.c:33
Contains the specific data for a single window.
Definition bw.h:27
Data structure for cache clients.
Definition cache.h:48
int Key
Primary Key for this client.
Definition cache.h:49
void * CbData
Client function data.
Definition cache.h:55
uint_t BufSize
Valid size of cache-data.
Definition cache.h:53
void * Buf
Pointer to cache-data.
Definition cache.h:52
bool_t limit_text_width
Definition prefs.h:71
BrowserWindow * bw
The requesting browser window [reference].
Definition web.hh:28
void a_UIcmd_page_popup(void *vbw, bool_t has_bugs, void *v_cssUrls)
Definition uicmd.cc:1262