summaryrefslogtreecommitdiff
path: root/src/PSEditWidget.cxx
blob: 95ddbdea41a877bd4fc67cc15aef39855a3fd79a (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
// 
// "$Id: PSEditWidget.cxx,v 1.26 2004/11/08 18:56:00 hofmann Exp $"
//
// PSEditWidget routines.
//
// Copyright 2004 by Johannes Hofmann
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//

#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);
  
  while (t) {
    fl_color((Fl_Color) t->get_color());
    fl_font(FLPSED_FONT, t->get_size());
    fl_draw(t->get_text(), t->get_x() + x(), t->get_y() + y());
    if (model->is_cur_text(t)) {
      fl_draw_box(FL_BORDER_FRAME, 
		  t->get_x()+x()-1, 
		  t->get_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);
      fl_draw(t->get_tag(), t->get_x() + x(), 
	      t->get_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(paper_x, paper_y, xdpi, ydpi);
  cur_size = 12;
  show_tags = 1;
}
  
int PSEditWidget::next() {
  model->set_page(page);
  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(x1, y1, s, cur_size, p);
  mod++;

  t = model->get_cur_text();

  if (t) {
    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, 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(x1, y1, page) == 0) {

    t_new = model->get_cur_text();

    if (t_new) {
      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) {
    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, int last_x, int last_y) {
  PSEditText *t;

  model->move(x1, y1);
  mod++;
  t = model->get_cur_text();
  if (t) {
    damage(4, bb_x(t), bb_y(t), bb_w(t), bb_h(t));
    damage(4, last_x - 10, last_y - fl_height() - 20, bb_w(t), bb_h(t));
  }
}

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

  t = model->get_cur_text();
  if (t) {
    fl_font(FLPSED_FONT, t->get_size());
    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);
  return GsWidget::reload();
}

void PSEditWidget::set_cur_size(int s) {
  cur_size = s;
}

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);
  }

  set_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;
  }
}

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::bb_x(PSEditText *t) {
  return t->get_x() - 10;
}

int PSEditWidget::bb_y(PSEditText *t) {
  fl_font(FLPSED_FONT, t->get_size());
  return t->get_y() - fl_height() - 20;
}

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

  tag = t->get_tag();
  if (tag) {
     fl_font(FLPSED_TAG_FONT, FLPSED_TAG_FONT_SIZE);
     wt = (int) fl_width(tag) + 20;
  }
  
  return w>=wt?w:wt;
}

int PSEditWidget::bb_h(PSEditText *t) {
  fl_font(FLPSED_FONT, t->get_size());
  return fl_height() + 30;
}