4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
34 #include "empty_svg.h"
38 // 1 = update from client, 2 = client closes, 3 = quit
42 SvgWin::SvgWin(SvgMain *client)
43 : PluginClientWindow(client, 420, 210, 420, 210, 1)
45 this->client = client;
53 void SvgWin::create_objects()
58 add_tool(title = new BC_Title(x0, y, _("Out X:")));
59 int x1 = x0 + title->get_w() + 10;
60 out_x = new SvgCoord(this, client, x1, y, &client->config.out_x);
61 out_x->create_objects();
62 int x2 = x1 + out_x->get_w() + 15;
63 add_tool(title = new BC_Title(x2, y, _("Out W:")));
64 int x3 = x2 + title->get_w() + 10;
65 out_w = new SvgCoord(this, client, x3, y, &client->config.out_w);
66 out_w->create_objects();
67 y += out_x->get_h() + 5;
69 add_tool(new BC_Title(x0, y, _("Out Y:")));
70 out_y = new SvgCoord(this, client, x1, y, &client->config.out_y);
71 out_y->create_objects();
72 add_tool(title = new BC_Title(x2, y, _("Out H:")));
73 out_h = new SvgCoord(this, client, x3, y, &client->config.out_h);
74 out_h->create_objects();
75 y += out_y->get_h() + 20;
77 add_tool(title = new BC_Title(x0, y, _("DPI:")));
78 dpi = new DpiValue(this, client, x1, y, &client->config.dpi);
79 dpi->create_objects();
80 add_tool(dpi_button = new DpiButton(this, client, x2, y));
81 dpi_button->create_objects();
82 y += dpi->get_h() + 20;
84 add_tool(svg_file_title = new BC_Title(x0, y, client->config.svg_file));
85 y += svg_file_title->get_h() + 5;
87 int64_t ms_time = stat(client->config.svg_file, &st) ? 0 :
88 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
89 char mtime[BCSTRLEN]; mtime[0] = 0;
91 time_t tm = ms_time/1000;
94 add_tool(svg_file_mstime = new BC_Title(x0, y, mtime));
95 y += svg_file_mstime->get_h() + 15;
97 y = get_h() - NewSvgButton::calculate_h() - 5;
98 add_tool(new_svg_button = new NewSvgButton(client, this, x0, y));
99 y = get_h() - EditSvgButton::calculate_h() - 5;
100 add_tool(edit_svg_button = new EditSvgButton(client, this, x0+300, y));
106 int SvgWin::close_event()
108 edit_svg_button->stop();
113 int SvgWin::hide_window(int flush)
115 edit_svg_button->stop();
116 return BC_WindowBase::hide_window(flush);
120 void SvgWin::update_gui(SvgConfig &config)
122 lock_window("SvgWin::update_gui");
123 out_x->update(config.out_x);
124 out_y->update(config.out_y);
125 out_w->update(config.out_w);
126 out_h->update(config.out_h);
127 dpi->update(config.dpi);
128 svg_file_title->update(config.svg_file);
129 char mtime[BCSTRLEN]; mtime[0] = 0;
130 if( config.ms_time > 0 ) {
131 time_t tm = config.ms_time/1000;
134 svg_file_mstime->update(mtime);
138 SvgCoord::SvgCoord(SvgWin *win, SvgMain *client, int x, int y, float *value)
139 : BC_TumbleTextBox(win, *value, (float)0, (float)3000, x, y, 100)
141 //printf("SvgWidth::SvgWidth %f\n", client->config.w);
142 this->client = client;
147 SvgCoord::~SvgCoord()
151 int SvgCoord::handle_event()
153 *value = atof(get_text());
154 client->send_configure_change();
158 NewSvgButton::NewSvgButton(SvgMain *client, SvgWin *window, int x, int y)
159 : BC_GenericButton(x, y, _("New/Open SVG..."))
161 this->client = client;
162 this->window = window;
165 int NewSvgButton::handle_event()
167 window->editing_lock.lock();
168 if( !window->editing ) {
170 window->editing_lock.unlock();
175 window->editing_lock.unlock();
181 void NewSvgButton::run()
183 // ======================================= get path from user
185 //printf("NewSvgButton::run 1\n");
188 strncpy(filename, client->config.svg_file, sizeof(filename));
189 // Loop until file is chosen
191 char directory[1024];
192 strncpy(directory, client->config.svg_file, sizeof(directory));
193 char *cp = strrchr(directory, '/');
195 if( !directory[0] ) {
196 char *cp = getenv("HOME");
197 if( cp ) strncpy(directory, cp, sizeof(directory));
199 NewSvgWindow *new_window = new NewSvgWindow(client, window, directory);
200 new_window->create_objects();
201 new_window->update_filter("*.svg");
202 result = new_window->run_window();
203 const char *filepath = new_window->get_path(0);
204 strcpy(filename, filepath);
206 if( result || !filepath || !*filepath ) {
207 window->editing_lock.lock();
209 window->editing_lock.unlock();
210 return; // cancel or no filename given
213 // Extend the filename with .svg
214 if( strlen(filename) < 4 ||
215 strcasecmp(&filename[strlen(filename) - 4], ".svg") ) {
216 strcat(filename, ".svg");
219 if( !access(filename, R_OK) )
222 FILE *out = fopen(filename,"w");
224 unsigned long size = sizeof(empty_svg) - 4;
225 fwrite(empty_svg+4, size, 1, out);
230 } while(result); // file doesn't exist so repeat
232 strcpy(client->config.svg_file, filename);
234 client->config.ms_time = stat(filename, &st) ? 0 :
235 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
236 window->update_gui(client->config);
237 client->send_configure_change();
239 window->editing_lock.lock();
241 window->editing_lock.unlock();
246 EditSvgButton::EditSvgButton(SvgMain *client, SvgWin *window, int x, int y)
247 : BC_GenericButton(x, y, _("Edit")), Thread(1)
249 this->client = client;
250 this->window = window;
254 EditSvgButton::~EditSvgButton()
259 void EditSvgButton::stop()
263 struct fifo_struct fifo_buf;
264 fifo_buf.pid = getpid();
266 write(fh_fifo, &fifo_buf, sizeof(fifo_buf));
272 int EditSvgButton::handle_event()
275 window->editing_lock.lock();
276 if( !window->editing && client->config.svg_file[0] != 0 ) {
278 window->editing_lock.unlock();
283 window->editing_lock.unlock();
288 void EditSvgButton::run()
290 // ======================================= get path from user
291 char filename_png[1024];
292 char filename_fifo[1024];
293 strcpy(filename_png, client->config.svg_file);
294 strcat(filename_png, ".png");
295 remove(filename_png);
296 strcpy(filename_fifo, filename_png);
297 strcat(filename_fifo, ".fifo");
298 remove(filename_fifo);
299 if( !mkfifo(filename_fifo, S_IRWXU) &&
300 (fh_fifo = ::open(filename_fifo, O_RDWR+O_NONBLOCK)) >= 0 ) {
301 SvgInkscapeThread inkscape_thread(this);
302 inkscape_thread.start();
304 while( inkscape_thread.running() && !done ) {
306 int64_t ms_time = stat(client->config.svg_file, &st) ? 0 :
307 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
308 if( client->config.ms_time != ms_time ) {
309 client->config.ms_time = ms_time;
310 client->send_configure_change();
312 // select(fh_fifo+1,rds,0,ers,tmo) does not work here
314 struct fifo_struct fifo_buf; fifo_buf.action = 1;
315 int ret = read(fh_fifo, &fifo_buf, sizeof(fifo_buf));
317 if( errno == EAGAIN ) continue;
321 if( ret != sizeof(fifo_buf) ) continue;
322 switch( fifo_buf.action ) {
324 case 2: printf(_("Inkscape has exited\n"));
326 case 3: printf(_("Plugin window has closed\n"));
333 perror(_("Error opening fifo file"));
334 remove(filename_fifo); // fifo destroyed on last close
336 window->editing_lock.lock();
338 window->editing_lock.unlock();
340 client->config.ms_time = stat(client->config.svg_file, &st) ? 0 :
341 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
342 client->send_configure_change();
345 SvgInkscapeThread::SvgInkscapeThread(EditSvgButton *edit)
351 SvgInkscapeThread::~SvgInkscapeThread()
357 void SvgInkscapeThread::run()
361 sprintf(command, "inkscape --with-gui %s", edit->client->config.svg_file);
362 printf(_("Running external SVG editor: %s\n"), command);
366 printf(_("External SVG editor finished\n"));
367 struct fifo_struct fifo_buf;
368 fifo_buf.pid = getpid();
370 write(edit->fh_fifo, &fifo_buf, sizeof(fifo_buf));
378 NewSvgWindow::NewSvgWindow(SvgMain *client, SvgWin *window, char *init_directory)
380 BC_WindowBase::get_resources()->filebox_h / 2,
382 _("SVG Plugin: Pick SVG file"),
383 _("Open an existing SVG file or create a new one"))
385 this->window = window;
388 NewSvgWindow::~NewSvgWindow() {}
391 DpiValue::DpiValue(SvgWin *win, SvgMain *client, int x, int y, float *value)
392 : BC_TumbleTextBox(win, *value, (float)10, (float)1000, x, y, 100)
394 //printf("SvgWidth::SvgWidth %f\n", client->config.w);
395 this->client = client;
400 DpiValue::~DpiValue()
404 int DpiValue::handle_event()
406 *value = atof(get_text());
411 DpiButton::DpiButton( SvgWin *window, SvgMain *client, int x, int y)
412 : BC_GenericButton(x, y, _("update dpi"))
414 this->client = client;
415 this->window = window;
418 DpiButton::~DpiButton()
422 int DpiButton::handle_event()
424 client->send_configure_change();