Dillo v3.1.1-119-g140d9ebd
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
58 DilloPlain(BrowserWindow *bw);
59 ~DilloPlain();
60
61 void write(void *Buf, uint_t BufSize, int Eof);
62};
63
64/* FSM states */
65enum {
68 ST_Eof
69};
70
71/*
72 * Exported function with C linkage.
73 */
74extern "C" {
75void *a_Plain_text(const char *type, void *P, CA_Callback_t *Call,void **Data);
76}
77
78/*
79 * Forward declarations
80 */
81static void Plain_callback(int Op, CacheClient_t *Client);
82void a_Plain_free(void *data);
83
84
88DilloPlain::DilloPlain(BrowserWindow *p_bw)
89{
90 /* Init event receiver */
91 plainReceiver.plain = this;
92
93 /* Init internal variables */
94 bw = p_bw;
96 Start_Ofs = 0;
97 state = ST_SeekingEol;
98
99 Layout *layout = (Layout*) bw->render_layout;
100 // TODO (1x) No URL?
101 StyleEngine styleEngine (layout, NULL, NULL, bw->zoom);
102
103 styleEngine.startElement ("body", bw);
104 styleEngine.startElement ("pre", bw);
105 widgetStyle = styleEngine.wordStyle (bw);
106 widgetStyle->ref ();
107
108 /* The context menu */
109 layout->connectLink (&plainReceiver);
110
111 /* Hook destructor to the dw delete call */
112 dw->setDeleteCallback(a_Plain_free, this);
113}
114
118DilloPlain::~DilloPlain()
119{
120 _MSG("::~DilloPlain()\n");
121 widgetStyle->unref();
122}
123
127bool DilloPlain::PlainLinkReceiver::press (Widget *widget, int, int, int, int,
128 EventButton *event)
129{
130 _MSG("DilloPlain::PlainLinkReceiver::buttonPress\n");
131
132 if (event->button == 3) {
133 a_UIcmd_page_popup(plain->bw, FALSE, NULL);
134 return true;
135 }
136 return false;
137}
138
139void DilloPlain::addLine(char *Buf, uint_t BufSize)
140{
141 int len;
142 char buf[129];
143 char *end = Buf + BufSize;
144
145 if (BufSize > 0) {
146 // Limit word length to avoid X11 coordinate
147 // overflow with extremely long lines.
148 while ((len = a_Misc_expand_tabs(&Buf, end, buf, sizeof(buf) - 1))) {
149 assert ((uint_t)len < sizeof(buf));
150 buf[len] = '\0';
151 DW2TB(dw)->addText(buf, len, widgetStyle);
152 }
153 } else {
154 // Add dummy word for empty lines - otherwise the parbreak is ignored.
155 DW2TB(dw)->addText("", 0, widgetStyle);
156 }
157
158 DW2TB(dw)->addParbreak(0, widgetStyle);
159}
160
165void DilloPlain::write(void *Buf, uint_t BufSize, int Eof)
166{
167 char *Start;
168 uint_t i, len, MaxBytes;
169
170 _MSG("DilloPlain::write Eof=%d\n", Eof);
171
172 Start = (char*)Buf + Start_Ofs;
173 MaxBytes = BufSize - Start_Ofs;
174 i = len = 0;
175 while ( i < MaxBytes ) {
176 switch ( state ) {
177 case ST_SeekingEol:
178 if (Start[i] == '\n' || Start[i] == '\r')
179 state = ST_Eol;
180 else {
181 ++i; ++len;
182 }
183 break;
184 case ST_Eol:
185 addLine(Start + i - len, len);
186 if (Start[i] == '\r' && Start[i + 1] == '\n') ++i;
187 if (i < MaxBytes) ++i;
188 state = ST_SeekingEol;
189 len = 0;
190 break;
191 }
192 }
193 Start_Ofs += i - len;
194 if (Eof && len) {
195 addLine(Start + i - len, len);
196 Start_Ofs += len;
197 }
198
199 DW2TB(dw)->flush();
200}
201
205void *a_Plain_text(const char *type, void *P, CA_Callback_t *Call, void **Data)
206{
207 DilloWeb *web = (DilloWeb*)P;
208 DilloPlain *plain = new DilloPlain(web->bw);
209
211 *Data = (void*)plain;
212
213 return (void*)plain->dw;
214}
215
216void a_Plain_free(void *data)
217{
218 _MSG("a_Plain_free! %p\n", data);
219 delete ((DilloPlain *)data);
220}
221
225static void Plain_callback(int Op, CacheClient_t *Client)
226{
227 DilloPlain *plain = (DilloPlain*)Client->CbData;
228
229 if (Op) {
230 /* Do the last line: */
231 plain->write(Client->Buf, Client->BufSize, 1);
232 /* remove this client from our active list */
233 a_Bw_close_client(plain->bw, Client->Key);
234 } else {
235 plain->write(Client->Buf, Client->BufSize, 0);
236 }
237}
238
#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:66
@ ST_Eol
Definition plain.cc:67
@ ST_Eof
Definition plain.cc:68
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:205
#define DW2TB(dw)
Definition plain.cc:32
void a_Plain_free(void *data)
Definition plain.cc:216
static void Plain_callback(int Op, CacheClient_t *Client)
This function is a cache client.
Definition plain.cc:225
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