summaryrefslogtreecommitdiff
path: root/src/PSEditWidget.cxx
blob: 31230f22fda2e5d354609e0646bd1afe21b5476f (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//
// Copyright 2007-2009 Johannes Hofmann <Johannes.Hofmann@gmx.de>
//
// This software may be used and distributed according to the terms
// of the GNU General Public License, incorporated herein by reference.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>

#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <FL/x.H>

#include "PSEditWidget.H"

#define FLPSED_FONT FL_HELVETICA
#define FLPSED_TAG_FONT FL_COURIER
#define FLPSED_TAG_FONT_SIZE 10


void PSEditWidget::clear_text() {
	model->clear();
}

void PSEditWidget::draw() {
	GsWidget::draw();
	PSEditText *t = model->get_text(page);
	int t_x, t_y;

	while (t) {
		t_x = ps_to_display_x(t->get_x());
		t_y = ps_to_display_y(t->get_y());

		fl_color(fl_rgb_color(t->text_color.get_r(),
				t->text_color.get_g(),
				t->text_color.get_b()));


		fl_font(FLPSED_FONT, t->get_size() * zoom_percent / 100);
		fl_draw(t->get_text(), t_x + x(), t_y + y());
		if (model->is_cur_text(t)) {
			fl_draw_box(FL_BORDER_FRAME, 
				t_x + x()-1, 
				t_y + y()-fl_height()+fl_descent(),
				(int) fl_width(t->get_text())+2, 
				fl_height(), 
				FL_BLACK);
		}

		if (t->get_tag() && show_tags) {
			int text_height = fl_height() - fl_descent();
			fl_color(FL_BLUE);
			fl_font(FLPSED_TAG_FONT, FLPSED_TAG_FONT_SIZE * zoom_percent / 100);
			fl_draw(t->get_tag(), t_x + x(), 
				t_y + y() - text_height - 1);
		}

		t = t->get_next();
	}
}

PSEditWidget::PSEditWidget(int X,int Y,int W, int H): GsWidget(X, Y, W, H) {
	model = new PSEditModel();
	cur_size = 12;
	cur_text_color.set(0.0, 0.0, 0.0);
	show_tags = 1;
	zoom_percent = 100;
	property_changed_cb = NULL;
}

int PSEditWidget::next() {
	model->set_page(page);  
	PSEditText *t_new;

	t_new = model->get_cur_text();

	if (t_new) {
		cur_text_color.set(&t_new->text_color);
		cur_size = t_new->size;

		if (property_changed_cb) {
			property_changed_cb();
		}
	}  

	return GsWidget::next();
}

void PSEditWidget::new_text(int x1, int y1, const char *s, int p) {
	PSEditText *t_old, *t;

	t_old = model->get_cur_text();

	model->new_text(ps_x(x1), ps_y(y1), s, cur_size, p, &cur_text_color);

	t = model->get_cur_text();

	if (t) {
		if (!t->is_empty()) {
			mod++;
		}
		damage(4, bb_x(t), bb_y(t), bb_w(t), bb_h(t));
	}

	if (t_old) {
		damage(4, bb_x(t_old), bb_y(t_old), bb_w(t_old), bb_h(t_old));
	}
}

void PSEditWidget::new_text(int x1, int y1, const char *s) {
	new_text(x1, y1, s, get_page());
}

int PSEditWidget::set_cur_text(int x1, int y1) {
	PSEditText *t_new, *t_old;

	t_old = model->get_cur_text();

	if (model->set_cur_text(ps_x(x1), ps_y(y1), page) == 0) {

		t_new = model->get_cur_text();

		if (t_new) {
			cur_text_color.set(&t_new->text_color);
			cur_size = t_new->size;
			if (property_changed_cb) {
				property_changed_cb();
			}

			damage(4, bb_x(t_new), bb_y(t_new), bb_w(t_new), bb_h(t_new));
		}
		if (t_old) {
			damage(4, bb_x(t_old), bb_y(t_old), bb_w(t_old), bb_h(t_old));
		}

		return 0;
	}
	return 1;
}

int PSEditWidget::next_text() {
	PSEditText *t_new, *t_old;
	int ret;

	t_old = model->get_cur_text();

	ret = model->next_text(page);

	t_new = model->get_cur_text();

	if (t_new) {
		cur_text_color.set(&t_new->text_color);
		cur_size = t_new->size;
		if (property_changed_cb) {
			property_changed_cb();
		}

		damage(4, bb_x(t_new), bb_y(t_new), bb_w(t_new), bb_h(t_new));
	}

	if (t_old) {
		damage(4, bb_x(t_old), bb_y(t_old), bb_w(t_old), bb_h(t_old));
	}

	return ret;
}

void PSEditWidget::append_text(const char *s) {
	PSEditText *t;

	model->append_text(s);
	mod++;
	t = model->get_cur_text();
	if (t) {
		damage(4, bb_x(t), bb_y(t), bb_w(t), bb_h(t));
	}
}

void PSEditWidget::move(int x1, int y1) {
	PSEditText *t;
	int old_bbx, old_bby, old_bbw, old_bbh;

	t = model->get_cur_text();
	if (t) {
		old_bbx = bb_x(t);
		old_bby = bb_y(t);
		old_bbw = bb_w(t);
		old_bbh = bb_h(t);

		model->move(ps_x(x1), ps_y(y1));
		if (!t->is_empty() || t->get_tag()) {
			mod++;
		}

		damage(4, old_bbx, old_bby, old_bbw, old_bbh);
		damage(4, bb_x(t), bb_y(t), bb_w(t), bb_h(t));
	}
}

void PSEditWidget::rel_move(int dx, int dy) {
	PSEditText *t;

	t = model->get_cur_text();
	if (t) {
		move(ps_to_display_x(t->get_x() + dx), ps_to_display_y(t->get_y() + dy));
	}
}

void PSEditWidget::rm_char() {
	PSEditText *t;
	int width;

	t = model->get_cur_text();
	if (t) {
		fl_font(FLPSED_FONT, t->get_size() * zoom_percent / 100);
		width =  bb_w(t);
	}

	model->rm_char();
	mod++;

	if (t) {
		damage(4, bb_x(t), bb_y(t), width, bb_h(t));
	}
}


int PSEditWidget::reload() {
	model->set_page(0);

	if (property_changed_cb) {
		property_changed_cb();
	}

	return GsWidget::reload();
}

void PSEditWidget::set_size(int s) {
	PSEditText *t;
	int old_x, old_y, old_w, old_h;

	t = model->get_cur_text();
	if (t) {
		old_x = bb_x(t);
		old_y = bb_y(t);
		old_w = bb_w(t);
		old_h = bb_h(t);
	}

	cur_size = s;
	model->set_size(s);

	if (t) {
		damage(4, bb_x(t), bb_y(t), bb_w(t), bb_h(t));
		damage(4, old_x, old_y, old_w, old_h);
	}

}

int PSEditWidget::get_size() {
	int s;

	s = model->get_size();
	if (s >= 0) {
		return s;
	} else {
		return cur_size;
	}
}

void PSEditWidget::set_color(const PSEditColor *c) {
	PSEditText *t;
	uchar *p;

	t = model->get_cur_text();

	p = (uchar*) &c;
	cur_text_color.set(c);

	model->set_color(&cur_text_color);

	if (t) {
		damage(4, bb_x(t), bb_y(t), bb_w(t), bb_h(t));
	}
}

void PSEditWidget::get_color(PSEditColor *c) {
	c->set(&cur_text_color);
}

int PSEditWidget::get_max_pages() {
	return model->get_max_pages();
}

int PSEditWidget::get_show_tags() {
	return show_tags;
}

void PSEditWidget::set_show_tags(int s) {
	show_tags = s;
	redraw();
}

int PSEditWidget::set_tag(const char *t) {
	if (model->set_tag(t) == 0) {
		mod++;
		redraw();
		return 0;
	} else {
		return 1;
	}
}

char *PSEditWidget::get_tag() {
	return model->get_tag();
}

int PSEditWidget::modified() {
	return mod;
}

int PSEditWidget::file_loaded() {
	return loaded;
}

int PSEditWidget::replace_tag(char *tag, char *text) {
	return model->replace_tag(tag, text);
}

int PSEditWidget::zoom(int p) {
	zoom_percent = p;
	return GsWidget::zoom(zoom_percent);
}


int PSEditWidget::bb_x(PSEditText *t) {
	return ps_to_display_x(t->get_x()) + x() - 10;
}

int PSEditWidget::bb_y(PSEditText *t) {
	fl_font(FLPSED_FONT, t->get_size() * zoom_percent / 100);
	return ps_to_display_y(t->get_y()) - fl_height() + y() - 10;
}

int PSEditWidget::bb_w(PSEditText *t) {
	int w, wt = 0;
	char *tag;
	fl_font(FLPSED_FONT, t->get_size() * zoom_percent / 100);
	w = (int) fl_width(t->get_text()) + 20;

	tag = t->get_tag();
	if (tag) {
		fl_font(FLPSED_TAG_FONT, FLPSED_TAG_FONT_SIZE * zoom_percent / 100);
		wt = (int) fl_width(tag) + 20;
	}

	return w>=wt?w:wt;
}

int PSEditWidget::bb_h(PSEditText *t) {
	int ret;
	fl_font(FLPSED_FONT, t->get_size() * zoom_percent / 100);
	ret = fl_height() + 30;

	if (t->get_tag()) {
		fl_font(FLPSED_TAG_FONT, FLPSED_TAG_FONT_SIZE * zoom_percent / 100);
		ret = ret + fl_height();
	}

	return ret;
}

static int round_div(int a, int b) {
	int r;

	r = a / b;
	if (a % b > b / 2) {
		r++;
	}

	return r;
}

int PSEditWidget::ps_to_display_x(int x1) {
	return  round_div(x1 * xdpi,  72);
}

int PSEditWidget::ps_to_display_y(int y1) {
	return round_div((paper_y - y1) * ydpi, 72);
}

int PSEditWidget::ps_x(int x1) {
	return round_div(x1 * 72, xdpi);
}

int PSEditWidget::ps_y(int y1) {
	return paper_y - round_div(y1 * 72, ydpi);
}