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);
217 delete new_window; new_window = 0;
218 if( result || !filepath || !*filepath ) {
219 window->editing_lock.lock();
221 window->editing_lock.unlock();
222 return; // cancel or no filename given
225 // Extend the filename with .svg
226 if( strlen(filename) < 4 ||
227 strcasecmp(&filename[strlen(filename) - 4], ".svg") ) {
228 strcat(filename, ".svg");
231 if( !access(filename, R_OK) )
234 FILE *out = fopen(filename,"w");
236 unsigned long size = sizeof(empty_svg) - 4;
237 fwrite(empty_svg+4, size, 1, out);
242 } while(result); // file doesn't exist so repeat
244 strcpy(client->config.svg_file, filename);
245 client->config.ms_time = 0;
246 window->update_gui(client->config);
247 client->send_configure_change();
249 window->editing_lock.lock();
251 window->editing_lock.unlock();
256 void NewSvgButton::stop()
259 new_window->set_done(1);
264 EditSvgButton::EditSvgButton(SvgMain *client, SvgWin *window, int x, int y)
265 : BC_GenericButton(x, y, _("Edit")), Thread(1)
267 this->client = client;
268 this->window = window;
272 EditSvgButton::~EditSvgButton()
277 void EditSvgButton::stop()
281 struct fifo_struct fifo_buf;
282 fifo_buf.pid = getpid();
284 write(fh_fifo, &fifo_buf, sizeof(fifo_buf));
290 int EditSvgButton::handle_event()
293 window->editing_lock.lock();
294 if( !window->editing && client->config.svg_file[0] != 0 ) {
296 window->editing_lock.unlock();
301 window->editing_lock.unlock();
306 void EditSvgButton::run()
308 // ======================================= get path from user
309 char filename_png[1024];
310 char filename_fifo[1024];
311 strcpy(filename_png, client->config.svg_file);
312 strcat(filename_png, ".png");
313 remove(filename_png);
314 strcpy(filename_fifo, filename_png);
315 strcat(filename_fifo, ".fifo");
316 remove(filename_fifo);
317 if( !mkfifo(filename_fifo, S_IRWXU) &&
318 (fh_fifo = ::open(filename_fifo, O_RDWR+O_NONBLOCK)) >= 0 ) {
319 SvgInkscapeThread inkscape_thread(this);
320 inkscape_thread.start();
322 while( inkscape_thread.running() && !done ) {
324 int64_t ms_time = stat(client->config.svg_file, &st) ? 0 :
325 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
326 if( client->config.ms_time != ms_time ) {
327 client->config.ms_time = ms_time;
328 client->send_configure_change();
330 // select(fh_fifo+1,rds,0,ers,tmo) does not work here
332 struct fifo_struct fifo_buf; fifo_buf.action = 1;
333 int ret = read(fh_fifo, &fifo_buf, sizeof(fifo_buf));
335 if( errno == EAGAIN ) continue;
339 if( ret != sizeof(fifo_buf) ) continue;
340 switch( fifo_buf.action ) {
342 case 2: printf(_("Inkscape has exited\n"));
344 case 3: printf(_("Plugin window has closed\n"));
351 perror(_("Error opening fifo file"));
352 remove(filename_fifo); // fifo destroyed on last close
354 window->editing_lock.lock();
356 window->editing_lock.unlock();
358 client->config.ms_time = stat(client->config.svg_file, &st) ? 0 :
359 st.st_mtim.tv_sec*1000 + st.st_mtim.tv_nsec/1000000;
360 client->send_configure_change();
363 SvgInkscapeThread::SvgInkscapeThread(EditSvgButton *edit)
369 SvgInkscapeThread::~SvgInkscapeThread()
375 static int exec_command(char* const*argv)
378 if( pid < 0 ) return -1;
381 waitpid(pid, &stat, 0);
384 sprintf(msg, "%s: error exit status %d", argv[0], stat);
385 MainError::show_error(msg);
389 execvp(argv[0], &argv[0]);
393 void SvgInkscapeThread::run()
397 snprintf(command, sizeof(command),
398 "inkscape --with-gui %s", edit->client->config.svg_file);
399 printf(_("Running external SVG editor: %s\n"), command);
400 char *const argv[] = {
403 edit->client->config.svg_file,
409 printf(_("External SVG editor finished\n"));
410 struct fifo_struct fifo_buf;
411 fifo_buf.pid = getpid();
413 write(edit->fh_fifo, &fifo_buf, sizeof(fifo_buf));
420 NewSvgWindow::NewSvgWindow(SvgMain *client, SvgWin *window, char *init_directory)
422 BC_WindowBase::get_resources()->filebox_h / 2,
424 _("SVG Plugin: Pick SVG file"),
425 _("Open an existing SVG file or create a new one"))
427 this->window = window;
430 NewSvgWindow::~NewSvgWindow() {}
433 DpiValue::DpiValue(SvgWin *win, SvgMain *client, int x, int y, float *value)
434 : BC_TumbleTextBox(win, *value, (float)10, (float)1000, x, y, 100)
436 //printf("SvgWidth::SvgWidth %f\n", client->config.w);
437 this->client = client;
442 DpiValue::~DpiValue()
446 int DpiValue::handle_event()
448 *value = atof(get_text());
453 DpiButton::DpiButton( SvgWin *window, SvgMain *client, int x, int y)
454 : BC_GenericButton(x, y, _("update dpi"))
456 this->client = client;
457 this->window = window;
460 DpiButton::~DpiButton()
464 int DpiButton::handle_event()
466 client->send_configure_change();