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
25 #include "mainerror.h"
30 #include <sys/types.h>
33 #include <sys/types.h>
38 #include "empty_svg.h"
42 // 1 = update from client, 2 = client closes, 3 = quit
46 SvgWin::SvgWin(SvgMain *client)
47 : PluginClientWindow(client, 420, 210, 420, 210, 1)
49 this->client = client;
57 void SvgWin::create_objects()
62 add_tool(title = new BC_Title(x0, y, _("Out X:")));
63 int x1 = x0 + title->get_w() + 10;
64 out_x = new SvgCoord(this, client, x1, y, &client->config.out_x);
65 out_x->create_objects();
66 int x2 = x1 + out_x->get_w() + 15;
67 add_tool(title = new BC_Title(x2, y, _("Out W:")));
68 int x3 = x2 + title->get_w() + 10;
69 out_w = new SvgCoord(this, client, x3, y, &client->config.out_w);
70 out_w->create_objects();
71 y += out_x->get_h() + 5;
73 add_tool(new BC_Title(x0, y, _("Out Y:")));
74 out_y = new SvgCoord(this, client, x1, y, &client->config.out_y);
75 out_y->create_objects();
76 add_tool(title = new BC_Title(x2, y, _("Out H:")));
77 out_h = new SvgCoord(this, client, x3, y, &client->config.out_h);
78 out_h->create_objects();
79 y += out_y->get_h() + 20;
81 add_tool(title = new BC_Title(x0, y, _("DPI:")));
82 dpi = new DpiValue(this, client, x1, y, &client->config.dpi);
83 dpi->create_objects();
84 add_tool(dpi_button = new DpiButton(this, client, x2, y));
85 dpi_button->create_objects();
86 y += dpi->get_h() + 20;
88 add_tool(svg_file_title = new BC_Title(x0, y, client->config.svg_file));
89 y += svg_file_title->get_h() + 5;
91 int64_t ms_time = stat(client->config.svg_file, &st) ? 0 :
92 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
93 char mtime[BCSTRLEN]; mtime[0] = 0;
95 time_t tm = ms_time/1000;
98 add_tool(svg_file_mstime = new BC_Title(x0, y, mtime));
99 y += svg_file_mstime->get_h() + 15;
101 y = get_h() - NewSvgButton::calculate_h() - 5;
102 add_tool(new_svg_button = new NewSvgButton(client, this, x0, y));
103 y = get_h() - EditSvgButton::calculate_h() - 5;
104 add_tool(edit_svg_button = new EditSvgButton(client, this, x0+300, y));
110 int SvgWin::close_event()
112 new_svg_button->stop();
113 edit_svg_button->stop();
118 int SvgWin::hide_window(int flush)
120 new_svg_button->stop();
121 edit_svg_button->stop();
122 return BC_WindowBase::hide_window(flush);
126 void SvgWin::update_gui(SvgConfig &config)
128 lock_window("SvgWin::update_gui");
129 out_x->update(config.out_x);
130 out_y->update(config.out_y);
131 out_w->update(config.out_w);
132 out_h->update(config.out_h);
133 dpi->update(config.dpi);
134 svg_file_title->update(config.svg_file);
135 char mtime[BCSTRLEN]; mtime[0] = 0;
136 if( config.ms_time > 0 ) {
137 time_t tm = config.ms_time/1000;
140 svg_file_mstime->update(mtime);
144 SvgCoord::SvgCoord(SvgWin *win, SvgMain *client, int x, int y, float *value)
145 : BC_TumbleTextBox(win, *value, (float)0, (float)3000, x, y, 100)
147 //printf("SvgWidth::SvgWidth %f\n", client->config.w);
148 this->client = client;
153 SvgCoord::~SvgCoord()
157 int SvgCoord::handle_event()
159 *value = atof(get_text());
160 client->send_configure_change();
164 NewSvgButton::NewSvgButton(SvgMain *client, SvgWin *window, int x, int y)
165 : BC_GenericButton(x, y, _("New/Open SVG..."))
167 this->client = client;
168 this->window = window;
171 NewSvgButton::~NewSvgButton()
177 int NewSvgButton::handle_event()
179 window->editing_lock.lock();
180 if( !window->editing ) {
182 window->editing_lock.unlock();
187 window->editing_lock.unlock();
193 void NewSvgButton::run()
195 // ======================================= get path from user
197 //printf("NewSvgButton::run 1\n");
200 strncpy(filename, client->config.svg_file, sizeof(filename));
201 // Loop until file is chosen
203 char directory[1024];
204 strncpy(directory, client->config.svg_file, sizeof(directory));
205 char *cp = strrchr(directory, '/');
207 if( !directory[0] ) {
208 char *cp = getenv("HOME");
209 if( cp ) strncpy(directory, cp, sizeof(directory));
211 new_window = new NewSvgWindow(client, window, directory);
212 new_window->create_objects();
213 new_window->update_filter("*.svg");
214 result = new_window->run_window();
215 const char *filepath = new_window->get_path(0);
216 strcpy(filename, filepath ? filepath : "" );
217 delete new_window; new_window = 0;
218 window->editing_lock.lock();
219 if( result || !filename[0] )
221 window->editing_lock.unlock();
222 if( !window->editing ) return; // cancel or no filename given
224 // Extend the filename with .svg
225 if( strlen(filename) < 4 ||
226 strcasecmp(&filename[strlen(filename) - 4], ".svg") ) {
227 strcat(filename, ".svg");
230 if( !access(filename, R_OK) )
233 FILE *out = fopen(filename,"w");
235 unsigned long size = sizeof(empty_svg) - 4;
236 fwrite(empty_svg+4, size, 1, out);
241 } while(result); // file doesn't exist so repeat
243 strcpy(client->config.svg_file, filename);
244 client->config.ms_time = 0;
245 window->update_gui(client->config);
246 client->send_configure_change();
248 window->editing_lock.lock();
250 window->editing_lock.unlock();
255 void NewSvgButton::stop()
258 new_window->set_done(1);
263 EditSvgButton::EditSvgButton(SvgMain *client, SvgWin *window, int x, int y)
264 : BC_GenericButton(x, y, _("Edit")), Thread(1)
266 this->client = client;
267 this->window = window;
271 EditSvgButton::~EditSvgButton()
276 void EditSvgButton::stop()
280 struct fifo_struct fifo_buf;
281 fifo_buf.pid = getpid();
283 write(fh_fifo, &fifo_buf, sizeof(fifo_buf));
289 int EditSvgButton::handle_event()
292 window->editing_lock.lock();
293 if( !window->editing && client->config.svg_file[0] != 0 ) {
295 window->editing_lock.unlock();
300 window->editing_lock.unlock();
305 void EditSvgButton::run()
307 // ======================================= get path from user
308 char filename_png[1024];
309 char filename_fifo[1024];
310 strcpy(filename_png, client->config.svg_file);
311 strcat(filename_png, ".png");
312 remove(filename_png);
313 strcpy(filename_fifo, filename_png);
314 strcat(filename_fifo, ".fifo");
315 remove(filename_fifo);
316 if( !mkfifo(filename_fifo, S_IRWXU) &&
317 (fh_fifo = ::open(filename_fifo, O_RDWR+O_NONBLOCK)) >= 0 ) {
318 SvgInkscapeThread inkscape_thread(this);
319 inkscape_thread.start();
321 while( inkscape_thread.running() && !done ) {
323 int64_t ms_time = stat(client->config.svg_file, &st) ? 0 :
324 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
325 if( client->config.ms_time != ms_time ) {
326 client->config.ms_time = ms_time;
327 client->send_configure_change();
329 // select(fh_fifo+1,rds,0,ers,tmo) does not work here
331 struct fifo_struct fifo_buf; fifo_buf.action = 1;
332 int ret = read(fh_fifo, &fifo_buf, sizeof(fifo_buf));
334 if( errno == EAGAIN ) continue;
338 if( ret != sizeof(fifo_buf) ) continue;
339 switch( fifo_buf.action ) {
341 case 2: printf(_("Inkscape has exited\n"));
343 case 3: printf(_("Plugin window has closed\n"));
350 perror(_("Error opening fifo file"));
351 remove(filename_fifo); // fifo destroyed on last close
353 window->editing_lock.lock();
355 window->editing_lock.unlock();
357 client->config.ms_time = stat(client->config.svg_file, &st) ? 0 :
358 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
359 client->send_configure_change();
362 SvgInkscapeThread::SvgInkscapeThread(EditSvgButton *edit)
368 SvgInkscapeThread::~SvgInkscapeThread()
374 static int exec_command(char* const*argv)
377 if( pid < 0 ) return -1;
380 waitpid(pid, &stat, 0);
383 sprintf(msg, "%s: error exit status %d", argv[0], stat);
384 MainError::show_error(msg);
388 execvp(argv[0], &argv[0]);
392 void SvgInkscapeThread::run()
396 snprintf(command, sizeof(command),
397 "inkscape --with-gui %s", edit->client->config.svg_file);
398 printf(_("Running external SVG editor: %s\n"), command);
399 char *const argv[] = {
402 edit->client->config.svg_file,
408 printf(_("External SVG editor finished\n"));
409 struct fifo_struct fifo_buf;
410 fifo_buf.pid = getpid();
412 write(edit->fh_fifo, &fifo_buf, sizeof(fifo_buf));
419 NewSvgWindow::NewSvgWindow(SvgMain *client, SvgWin *window, char *init_directory)
421 BC_WindowBase::get_resources()->filebox_h / 2,
423 _("SVG Plugin: Pick SVG file"),
424 _("Open an existing SVG file or create a new one"))
426 this->window = window;
429 NewSvgWindow::~NewSvgWindow() {}
432 DpiValue::DpiValue(SvgWin *win, SvgMain *client, int x, int y, float *value)
433 : BC_TumbleTextBox(win, *value, (float)10, (float)1000, x, y, 100)
435 //printf("SvgWidth::SvgWidth %f\n", client->config.w);
436 this->client = client;
441 DpiValue::~DpiValue()
445 int DpiValue::handle_event()
447 *value = atof(get_text());
452 DpiButton::DpiButton( SvgWin *window, SvgMain *client, int x, int y)
453 : BC_GenericButton(x, y, _("update dpi"))
455 this->client = client;
456 this->window = window;
459 DpiButton::~DpiButton()
463 int DpiButton::handle_event()
465 client->send_configure_change();