00001 #include "bcdisplayinfo.h"
00002 #include "clip.h"
00003 #include "svgwin.h"
00004 #include "string.h"
00005 #include "filexml.h"
00006 #include <unistd.h>
00007 #include <fcntl.h>
00008 #include <sys/types.h>
00009 #include <fcntl.h>
00010 #include <sys/stat.h>
00011
00012 struct fifo_struct {
00013 int pid;
00014 int action;
00015 };
00016
00017
00018 #include <libintl.h>
00019 #define _(String) gettext(String)
00020 #define gettext_noop(String) String
00021 #define N_(String) gettext_noop (String)
00022
00023 #include "empty_svg.h"
00024
00025
00026
00027 PLUGIN_THREAD_OBJECT(SvgMain, SvgThread, SvgWin)
00028
00029
00030
00031
00032
00033
00034 SvgWin::SvgWin(SvgMain *client, int x, int y)
00035 : BC_Window(client->gui_string,
00036 x,
00037 y,
00038 300,
00039 280,
00040 300,
00041 280,
00042 0,
00043 0,
00044 1)
00045 {
00046 this->client = client;
00047 this->editing = 0;
00048 }
00049
00050 SvgWin::~SvgWin()
00051 {
00052 }
00053
00054 int SvgWin::create_objects()
00055 {
00056 int x = 10, y = 10;
00057
00058
00059 y += 20;
00060
00061
00062 y += 30;
00063
00064
00065 y += 20;
00066
00067
00068 y += 30;
00069
00070
00071 y += 20;
00072
00073
00074 y += 30;
00075
00076
00077 y += 20;
00078
00079
00080 y += 30;
00081
00082
00083 x += 150;
00084 y = 10;
00085 add_tool(new BC_Title(x, y, _("Out X:")));
00086 y += 20;
00087 out_x = new SvgCoord(this, client, x, y, &client->config.out_x);
00088 out_x->create_objects();
00089 y += 30;
00090
00091 add_tool(new BC_Title(x, y, _("Out Y:")));
00092 y += 20;
00093 out_y = new SvgCoord(this, client, x, y, &client->config.out_y);
00094 out_y->create_objects();
00095 y += 30;
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 x -= 150;
00110 add_tool(new_svg_button = new NewSvgButton(client, this, x, y));
00111 add_tool(edit_svg_button = new EditSvgButton(client, this, x+190, y));
00112 add_tool(svg_file_title = new BC_Title(x, y+26, client->config.svg_file));
00113
00114 x +=150;
00115
00116 show_window();
00117 flush();
00118 return 0;
00119 }
00120
00121 int SvgWin::close_event()
00122 {
00123 set_done(1);
00124 return 1;
00125 }
00126
00127 SvgCoord::SvgCoord(SvgWin *win,
00128 SvgMain *client,
00129 int x,
00130 int y,
00131 float *value)
00132 : BC_TumbleTextBox(win,
00133 *value,
00134 (float)0,
00135 (float)3000,
00136 x,
00137 y,
00138 100)
00139 {
00140
00141 this->client = client;
00142 this->win = win;
00143 this->value = value;
00144 }
00145
00146 SvgCoord::~SvgCoord()
00147 {
00148 }
00149
00150 int SvgCoord::handle_event()
00151 {
00152 *value = atof(get_text());
00153
00154 client->send_configure_change();
00155 return 1;
00156 }
00157
00158 NewSvgButton::NewSvgButton(SvgMain *client, SvgWin *window, int x, int y)
00159 : BC_GenericButton(x, y, _("New/Open SVG..."))
00160 {
00161 this->client = client;
00162 this->window = window;
00163 quit_now = 0;
00164 }
00165 int NewSvgButton::handle_event()
00166 {
00167 window->editing_lock.lock();
00168 if (!window->editing)
00169 {
00170 window->editing = 1;
00171 window->editing_lock.unlock();
00172 quit_now = 0;
00173 start();
00174 } else
00175 {
00176
00177 window->editing_lock.unlock();
00178 }
00179
00180 return 1;
00181 }
00182
00183 void NewSvgButton::run()
00184 {
00185
00186 int result;
00187
00188 char directory[1024], filename[1024];
00189 sprintf(directory, "~");
00190 client->defaults->get("DIRECTORY", directory);
00191
00192 result = 1;
00193
00194 do{
00195 NewSvgWindow *new_window;
00196
00197 new_window = new NewSvgWindow(client, window, directory);
00198 new_window->create_objects();
00199 new_window->update_filter("*.svg");
00200 result = new_window->run_window();
00201 client->defaults->update("DIRECTORY", new_window->get_path(0));
00202 strcpy(filename, new_window->get_path(0));
00203 delete new_window;
00204
00205
00206 if(strlen(filename) < 4 ||
00207 strcasecmp(&filename[strlen(filename) - 4], ".svg"))
00208 {
00209 strcat(filename, ".svg");
00210 }
00211
00212
00213 if((filename[0] == 0) || (result == 1)) {
00214 window->editing_lock.lock();
00215 window->editing = 0;
00216 window->editing_lock.unlock();
00217 return;
00218 }
00219 FILE *in;
00220 if(in = fopen(filename, "rb"))
00221 {
00222 fclose(in);
00223
00224 result = 0;
00225 } else
00226 {
00227
00228 in = fopen(filename, "w+");
00229 unsigned long size;
00230 size = (((unsigned long)empty_svg[0]) << 24) +
00231 (((unsigned long)empty_svg[1]) << 16) +
00232 (((unsigned long)empty_svg[2]) << 8) +
00233 ((unsigned long)empty_svg[3]);
00234 printf("in: %p size: %li\n", in, size);
00235
00236
00237 fwrite(empty_svg+4, size, 1, in);
00238 fclose(in);
00239 result = 0;
00240 }
00241 } while(result);
00242
00243
00244 window->svg_file_title->update(filename);
00245 window->flush();
00246 strcpy(client->config.svg_file, filename);
00247 client->need_reconfigure = 1;
00248 client->force_raw_render = 1;
00249 client->send_configure_change();
00250
00251
00252 if(quit_now) window->set_done(0);
00253 window->editing_lock.lock();
00254 window->editing = 0;
00255 window->editing_lock.unlock();
00256
00257 return;
00258 }
00259
00260 EditSvgButton::EditSvgButton(SvgMain *client, SvgWin *window, int x, int y)
00261 : BC_GenericButton(x, y, _("Edit"))
00262 {
00263 this->client = client;
00264 this->window = window;
00265 quit_now = 0;
00266 }
00267
00268 EditSvgButton::~EditSvgButton() {
00269 struct fifo_struct fifo_buf;
00270 fifo_buf.pid = getpid();
00271 fifo_buf.action = 3;
00272 quit_now = 1;
00273 write (fh_fifo, &fifo_buf, sizeof(fifo_buf));
00274 }
00275
00276 int EditSvgButton::handle_event()
00277 {
00278
00279 window->editing_lock.lock();
00280 if (!window->editing && client->config.svg_file[0] != 0)
00281 {
00282 window->editing = 1;
00283 window->editing_lock.unlock();
00284 start();
00285 } else
00286 {
00287
00288 window->editing_lock.unlock();
00289 }
00290 return 1;
00291 }
00292
00293 void EditSvgButton::run()
00294 {
00295
00296 Timer pausetimer;
00297 long delay;
00298 int result;
00299 time_t last_update;
00300 struct stat st_raw;
00301 char filename_raw[1024];
00302 char filename_fifo[1024];
00303 struct fifo_struct fifo_buf;
00304 SvgInkscapeThread *inkscape_thread = new SvgInkscapeThread(client, window);
00305
00306 strcpy(filename_raw, client->config.svg_file);
00307 strcat(filename_raw, ".raw");
00308 result = stat (filename_raw, &st_raw);
00309 last_update = st_raw.st_mtime;
00310 if (result)
00311 last_update = 0;
00312
00313 strcpy(filename_fifo, filename_raw);
00314 strcat(filename_fifo, ".fifo");
00315 if (mkfifo(filename_fifo, S_IRWXU) != 0) {
00316 perror("Error while creating fifo file");
00317 }
00318 fh_fifo = open(filename_fifo, O_RDWR);
00319 fifo_buf.action = 0;
00320 inkscape_thread->fh_fifo = fh_fifo;
00321 inkscape_thread->start();
00322 while (inkscape_thread->running() && (!quit_now)) {
00323
00324 read(fh_fifo, &fifo_buf, sizeof(fifo_buf));
00325
00326 if (fifo_buf.action == 1) {
00327 result = stat (filename_raw, &st_raw);
00328
00329
00330
00331
00332
00333 client->config.last_load = 1;
00334 client->send_configure_change();
00335 last_update = st_raw.st_mtime;
00336
00337 } else
00338 if (fifo_buf.action == 2) {
00339 printf(_("Inkscape has exited\n"));
00340 } else
00341 if (fifo_buf.action == 3) {
00342 printf(_("Plugin window has closed\n"));
00343 delete inkscape_thread;
00344 close(fh_fifo);
00345 return;
00346 }
00347 }
00348 inkscape_thread->join();
00349 close(fh_fifo);
00350 window->editing_lock.lock();
00351 window->editing = 0;
00352 window->editing_lock.unlock();
00353
00354 }
00355
00356 SvgInkscapeThread::SvgInkscapeThread(SvgMain *client, SvgWin *window)
00357 : Thread(1)
00358 {
00359 this->client = client;
00360 this->window = window;
00361 }
00362
00363 SvgInkscapeThread::~SvgInkscapeThread()
00364 {
00365
00366 cancel();
00367 }
00368
00369 void SvgInkscapeThread::run()
00370 {
00371
00372 char command[1024];
00373 char filename_raw[1024];
00374 strcpy(filename_raw, client->config.svg_file);
00375 strcat(filename_raw, ".raw");
00376
00377 sprintf(command, "inkscape --cinelerra-export-file=%s %s",
00378 filename_raw, client->config.svg_file);
00379 printf(_("Running external SVG editor: %s\n"), command);
00380 enable_cancel();
00381 system(command);
00382 printf(_("External SVG editor finished\n"));
00383 {
00384 struct fifo_struct fifo_buf;
00385 fifo_buf.pid = getpid();
00386 fifo_buf.action = 2;
00387 write (fh_fifo, &fifo_buf, sizeof(fifo_buf));
00388 }
00389 disable_cancel();
00390 return;
00391 }
00392
00393
00394
00395 NewSvgWindow::NewSvgWindow(SvgMain *client, SvgWin *window, char *init_directory)
00396 : BC_FileBox(0,
00397 BC_WindowBase::get_resources()->filebox_h / 2,
00398 init_directory,
00399 _("SVG Plugin: Pick SVG file"),
00400 _("Open an existing SVG file or create a new one"))
00401 {
00402 this->window = window;
00403 }
00404
00405 NewSvgWindow::~NewSvgWindow() {}
00406
00407
00408
00409
00410
00411
00412