3 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mainerror.h"
29 #include <sys/types.h>
32 #include <sys/types.h>
37 #include "empty_svg.h"
41 // 1 = update from client, 2 = client closes, 3 = quit
45 SvgWin::SvgWin(SvgMain *client)
46 : PluginClientWindow(client, xS(420), yS(210), xS(420), yS(210), 1)
48 this->client = client;
56 void SvgWin::create_objects()
58 int xs10 = xS(10), xs15 = xS(15);
59 int ys5 = yS(5), ys10 = yS(10), ys20 = yS(20), ys15 = yS(15);
61 int x0 = xs10, y = ys10;
63 add_tool(title = new BC_Title(x0, y, _("Out X:")));
64 int x1 = x0 + title->get_w() + xs10;
65 out_x = new SvgCoord(this, client, x1, y, &client->config.out_x);
66 out_x->create_objects();
67 int x2 = x1 + out_x->get_w() + xs15;
68 add_tool(title = new BC_Title(x2, y, _("Out W:")));
69 int x3 = x2 + title->get_w() + xs10;
70 out_w = new SvgCoord(this, client, x3, y, &client->config.out_w);
71 out_w->create_objects();
72 y += out_x->get_h() + ys5;
74 add_tool(new BC_Title(x0, y, _("Out Y:")));
75 out_y = new SvgCoord(this, client, x1, y, &client->config.out_y);
76 out_y->create_objects();
77 add_tool(title = new BC_Title(x2, y, _("Out H:")));
78 out_h = new SvgCoord(this, client, x3, y, &client->config.out_h);
79 out_h->create_objects();
80 y += out_y->get_h() + ys20;
82 add_tool(title = new BC_Title(x0, y, _("DPI:")));
83 dpi = new DpiValue(this, client, x1, y, &client->config.dpi);
84 dpi->create_objects();
85 add_tool(dpi_button = new DpiButton(this, client, x2, y));
86 dpi_button->create_objects();
87 y += dpi->get_h() + ys20;
89 add_tool(svg_file_title = new BC_Title(x0, y, client->config.svg_file));
90 y += svg_file_title->get_h() + ys5;
92 int64_t ms_time = stat(client->config.svg_file, &st) ? 0 :
93 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
94 char mtime[BCSTRLEN]; mtime[0] = 0;
96 time_t tm = ms_time/1000;
99 add_tool(svg_file_mstime = new BC_Title(x0, y, mtime));
100 y += svg_file_mstime->get_h() + ys15;
102 y = get_h() - NewSvgButton::calculate_h() - ys5;
103 add_tool(new_svg_button = new NewSvgButton(client, this, x0, y));
104 y = get_h() - EditSvgButton::calculate_h() - ys5;
105 add_tool(edit_svg_button = new EditSvgButton(client, this, x0+xS(300), y));
111 int SvgWin::close_event()
113 new_svg_button->stop();
114 edit_svg_button->stop();
119 int SvgWin::hide_window(int flush)
121 new_svg_button->stop();
122 edit_svg_button->stop();
123 return BC_WindowBase::hide_window(flush);
127 void SvgWin::update_gui(SvgConfig &config)
129 lock_window("SvgWin::update_gui");
130 out_x->update(config.out_x);
131 out_y->update(config.out_y);
132 out_w->update(config.out_w);
133 out_h->update(config.out_h);
134 dpi->update(config.dpi);
135 svg_file_title->update(config.svg_file);
136 char mtime[BCSTRLEN]; mtime[0] = 0;
137 if( config.ms_time > 0 ) {
138 time_t tm = config.ms_time/1000;
141 svg_file_mstime->update(mtime);
145 SvgCoord::SvgCoord(SvgWin *win, SvgMain *client, int x, int y, float *value)
146 : BC_TumbleTextBox(win, *value, (float)0, (float)3000, x, y, xS(100))
148 //printf("SvgWidth::SvgWidth %f\n", client->config.w);
149 this->client = client;
154 SvgCoord::~SvgCoord()
158 int SvgCoord::handle_event()
160 *value = atof(get_text());
161 client->send_configure_change();
165 NewSvgButton::NewSvgButton(SvgMain *client, SvgWin *window, int x, int y)
166 : BC_GenericButton(x, y, _("New/Open SVG..."))
168 this->client = client;
169 this->window = window;
172 NewSvgButton::~NewSvgButton()
178 int NewSvgButton::handle_event()
180 window->editing_lock.lock();
181 if( !window->editing ) {
183 window->editing_lock.unlock();
188 window->editing_lock.unlock();
194 void NewSvgButton::run()
196 // ======================================= get path from user
198 //printf("NewSvgButton::run 1\n");
201 strncpy(filename, client->config.svg_file, sizeof(filename));
202 // Loop until file is chosen
204 char directory[1024];
205 strncpy(directory, client->config.svg_file, sizeof(directory));
206 char *cp = strrchr(directory, '/');
208 if( !directory[0] ) {
209 char *cp = getenv("HOME");
210 if( cp ) strncpy(directory, cp, sizeof(directory));
212 new_window = new NewSvgWindow(client, window, directory);
213 new_window->create_objects();
214 new_window->lock_window("NewSvgButton::run");
215 new_window->update_filter("*.svg");
216 new_window->unlock_window();
217 result = new_window->run_window();
218 const char *filepath = new_window->get_path(0);
219 strcpy(filename, filepath ? filepath : "" );
220 delete new_window; new_window = 0;
221 window->editing_lock.lock();
222 if( result || !filename[0] )
224 window->editing_lock.unlock();
225 if( !window->editing ) return; // cancel or no filename given
227 // Extend the filename with .svg
228 if( strlen(filename) < 4 ||
229 strcasecmp(&filename[strlen(filename) - 4], ".svg") ) {
230 strcat(filename, ".svg");
233 if( !access(filename, R_OK) )
236 FILE *out = fopen(filename,"w");
238 unsigned long size = sizeof(empty_svg) - 4;
239 fwrite(empty_svg+4, size, 1, out);
244 } while(result); // file doesn't exist so repeat
246 strcpy(client->config.svg_file, filename);
247 client->config.ms_time = 0;
248 window->update_gui(client->config);
249 client->send_configure_change();
251 window->editing_lock.lock();
253 window->editing_lock.unlock();
258 void NewSvgButton::stop()
261 new_window->set_done(1);
266 EditSvgButton::EditSvgButton(SvgMain *client, SvgWin *window, int x, int y)
267 : BC_GenericButton(x, y, _("Edit")), Thread(1)
269 this->client = client;
270 this->window = window;
274 EditSvgButton::~EditSvgButton()
279 void EditSvgButton::stop()
283 struct fifo_struct fifo_buf;
284 fifo_buf.pid = getpid();
286 write(fh_fifo, &fifo_buf, sizeof(fifo_buf));
292 int EditSvgButton::handle_event()
295 window->editing_lock.lock();
296 if( !window->editing && client->config.svg_file[0] != 0 ) {
298 window->editing_lock.unlock();
303 window->editing_lock.unlock();
308 void EditSvgButton::run()
310 // ======================================= get path from user
311 char filename_png[1024];
312 char filename_fifo[1024];
313 strcpy(filename_png, client->config.svg_file);
314 strcat(filename_png, ".png");
315 remove(filename_png);
316 strcpy(filename_fifo, filename_png);
317 strcat(filename_fifo, ".fifo");
318 remove(filename_fifo);
319 if( !mkfifo(filename_fifo, S_IRWXU) &&
320 (fh_fifo = ::open(filename_fifo, O_RDWR+O_NONBLOCK)) >= 0 ) {
321 SvgInkscapeThread inkscape_thread(this);
322 inkscape_thread.start();
324 while( inkscape_thread.running() && !done ) {
326 int64_t ms_time = stat(client->config.svg_file, &st) ? 0 :
327 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
328 if( client->config.ms_time != ms_time ) {
329 client->config.ms_time = ms_time;
330 client->send_configure_change();
332 // select(fh_fifo+1,rds,0,ers,tmo) does not work here
334 struct fifo_struct fifo_buf; fifo_buf.action = 1;
335 int ret = read(fh_fifo, &fifo_buf, sizeof(fifo_buf));
337 if( errno == EAGAIN ) continue;
341 if( ret != sizeof(fifo_buf) ) continue;
342 switch( fifo_buf.action ) {
344 case 2: // printf(_("Inkscape has exited\n"));
346 case 3: // printf(_("Plugin window has closed\n"));
353 perror(_("Error opening fifo file"));
354 remove(filename_fifo); // fifo destroyed on last close
356 window->editing_lock.lock();
358 window->editing_lock.unlock();
360 client->config.ms_time = stat(client->config.svg_file, &st) ? 0 :
361 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
362 client->send_configure_change();
365 SvgInkscapeThread::SvgInkscapeThread(EditSvgButton *edit)
371 SvgInkscapeThread::~SvgInkscapeThread()
377 static int exec_command(char* const*argv)
380 if( pid < 0 ) return -1;
383 waitpid(pid, &stat, 0);
386 sprintf(msg, "%s: error exit status %d", argv[0], stat);
387 MainError::show_error(msg);
391 execvp(argv[0], &argv[0]);
395 void SvgInkscapeThread::run()
399 snprintf(command, sizeof(command),
400 "inkscape --with-gui %s", edit->client->config.svg_file);
401 printf(_("Running external SVG editor: %s\n"), command);
402 char *const argv[] = {
405 edit->client->config.svg_file,
411 printf(_("External SVG editor finished\n"));
412 struct fifo_struct fifo_buf;
413 fifo_buf.pid = getpid();
415 write(edit->fh_fifo, &fifo_buf, sizeof(fifo_buf));
422 NewSvgWindow::NewSvgWindow(SvgMain *client, SvgWin *window, char *init_directory)
424 BC_WindowBase::get_resources()->filebox_h / 2,
426 _("SVG Plugin: Pick SVG file"),
427 _("Open an existing SVG file or create a new one"))
429 this->window = window;
432 NewSvgWindow::~NewSvgWindow() {}
435 DpiValue::DpiValue(SvgWin *win, SvgMain *client, int x, int y, float *value)
436 : BC_TumbleTextBox(win, *value, 10.f, 1000.f, x, y, xS(100), 2)
438 //printf("SvgWidth::SvgWidth %f\n", client->config.w);
439 this->client = client;
444 DpiValue::~DpiValue()
448 int DpiValue::handle_event()
450 *value = atof(get_text());
455 DpiButton::DpiButton( SvgWin *window, SvgMain *client, int x, int y)
456 : BC_GenericButton(x, y, _("update dpi"))
458 this->client = client;
459 this->window = window;
462 DpiButton::~DpiButton()
466 int DpiButton::handle_event()
468 client->send_configure_change();