summaryrefslogtreecommitdiff
path: root/src/PSEditor.cxx
blob: 308d0dde23512dea73b8aa3d5a8eb7ed961cc4e9 (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
// 
// "$Id: PSEditor.cxx,v 1.8 2004/07/28 20:15:48 hofmann Exp $"
//
// PSEditor 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 <errno.h>
#include <FL/fl_ask.H>
#include "PSEditor.H"
#include "Postscript.H"

PSEditor::PSEditor(int X,int Y,int W, int H) : PSEditWidget(X, Y, W, H) {
  loaded = 0;
  mod = 0;
  ps_level = 1;
}

int PSEditor::handle(int event) {
  switch(event) {
  case FL_PUSH:    
    if (Fl::event_button() == 1) {
      if (!file_loaded()) {
	fl_beep();
	return 0;
      }
      
      mark_x = Fl::event_x()-x();
      mark_y = Fl::event_y()-y();

      if (!set_cur_text(mark_x, mark_y) == 0) {
	new_text(mark_x, mark_y, "");
	mod++;
      }

      Fl::focus(this);
      return 1;
    }
    break;
  case FL_RELEASE:
    if (Fl::event_button() == 2) {
      Fl::paste(*this, 0);
      return 1;
    }
    break;
  case FL_DRAG:
    move(Fl::event_x()-x(), Fl::event_y()-y());
    mod++;
    return 1;
    break;
  case FL_KEYBOARD:
    {
      int del;
      int key = Fl::event_key();
      if (key == FL_BackSpace) {
	rm_char();  
	mod++;
      } else if (Fl::compose(del)) {
	if (del > 0) {
	  for (int i=0; i<del; i++) rm_char();
	  mod++;
	}
	if (Fl::event_length()) {
	  append_text(Fl::event_text());
	  mod++;
	}
      } else {
	return 0;
      }
      
      return 1;
    }
    break;
  case FL_PASTE:
    append_text(Fl::event_text());
    return 1;
    break;
  case FL_FOCUS:
    return 1;
    break;
  case FL_UNFOCUS:
    return 0;
    break;
  }
  return 0;
}


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

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

int PSEditor::load(char *f) {
  FILE *fp = fopen(f, "r");
  char tmpname[256];
  char linebuf[1024];
  int size, ret, ret1, ret2;
  PSParser *p1 = new PSParser_1(this);
  PSParser *p2 = new PSParser_2(this);
  
  strncpy(tmpname, "/tmp/PSEditorXXXXXX", 256);
  tmp_fd = mkstemp(tmpname);
  if (tmp_fd < 0) {
    fprintf(stderr, "Could not create temporary file (errno %d).\n", errno);
    return 1;
  }
  unlink(tmpname);

  clear_text();

  while (fgets(linebuf, 1024, fp) != NULL) {
    
    if (!p2->parse(linebuf) && !p1->parse(linebuf)) {
      ret = write(tmp_fd, linebuf, strlen(linebuf));
      if (ret != strlen(linebuf)) {
	fprintf(stderr, "Error while writing to temporary file\n");
      }
    }
  }
  fclose(fp);
  lseek(tmp_fd, 0L, SEEK_SET);

  delete(p1);
  delete(p2);

  mod = 0;
  loaded = 1;
  return GsWidget::load(tmp_fd);
}

int PSEditor::save(const char* savefile) {
  off_t pos = lseek(tmp_fd, 0, SEEK_CUR); // save current position

  if (!file_loaded()) {
    return 1;
  }
  FILE *fp = fdopen(tmp_fd, "r");
  rewind(fp);
  FILE *sfp = fopen(savefile, "w");
  PSWriter *pw;
  
  if (ps_level == 2) {
    pw = new PSLevel2Writer(this);
  } else {
    pw = new PSLevel1Writer(this);
  }

  pw->write(fp, sfp);

  delete(pw);
   
  fclose(sfp);
  lseek(tmp_fd, pos, SEEK_SET);           // restore current position
  mod = 0;
  return 0;
}

int PSEditor::get_ps_level() {
  return ps_level;
}

void PSEditor::set_ps_level(int l) {
  if (l == 2) {
    ps_level = 2;
  } else {
    ps_level = 1;
  }
}