comparison src/graphics.cc @ 7408:246f905cb984

[project @ 2008-01-22 19:42:47 by jwe]
author jwe
date Tue, 22 Jan 2008 19:42:48 +0000
parents 135c13496faf
children 73036cdd855d
comparison
equal deleted inserted replaced
7407:8433bb7865bd 7408:246f905cb984
41 #include "oct-obj.h" 41 #include "oct-obj.h"
42 #include "oct-map.h" 42 #include "oct-map.h"
43 #include "ov-fcn-handle.h" 43 #include "ov-fcn-handle.h"
44 #include "parse.h" 44 #include "parse.h"
45 #include "unwind-prot.h" 45 #include "unwind-prot.h"
46 #include "file-ops.h"
47 #include "file-stat.h"
46 48
47 static void 49 static void
48 gripe_set_invalid (const std::string& pname) 50 gripe_set_invalid (const std::string& pname)
49 { 51 {
50 error ("set: invalid value for %s property", pname.c_str ()); 52 error ("set: invalid value for %s property", pname.c_str ());
1144 gh_manager::free (children(i)); 1146 gh_manager::free (children(i));
1145 } 1147 }
1146 1148
1147 // --------------------------------------------------------------------- 1149 // ---------------------------------------------------------------------
1148 1150
1151 class gnuplot_backend : public base_graphics_backend
1152 {
1153 public:
1154 gnuplot_backend (void)
1155 : base_graphics_backend ("gnuplot") { }
1156
1157 ~gnuplot_backend (void) { }
1158
1159 bool is_valid (void) const { return true; }
1160
1161 void close_figure (const octave_value& pstream) const
1162 {
1163 if (! pstream.is_empty())
1164 {
1165 octave_value_list args;
1166 args(1) = "\nquit;\n";
1167 args(0) = pstream;
1168 feval ("fputs", args);
1169 args.resize (1);
1170 feval ("fflush", args);
1171 feval ("pclose", args);
1172 }
1173 }
1174
1175 void redraw_figure (const graphics_handle& fh) const
1176 {
1177 octave_value_list args;
1178 args(0) = fh.as_octave_value ();
1179 feval ("gnuplot_drawnow", args);
1180 }
1181
1182 void print_figure (const graphics_handle& fh, const std::string& term,
1183 const std::string& file, bool mono,
1184 const std::string& debug_file) const
1185 {
1186 octave_value_list args;
1187 args.resize (4);
1188 args(0) = fh.as_octave_value ();
1189 args(1) = term;
1190 args(2) = file;
1191 args(3) = mono;
1192 if (! debug_file.empty ())
1193 args(4) = debug_file;
1194 feval ("gnuplot_drawnow", args);
1195 }
1196
1197 Matrix get_canvas_size (const graphics_handle& fh) const
1198 { return Matrix (1, 2, 0.0); }
1199 };
1200
1201 graphics_backend
1202 graphics_backend::default_backend (void)
1203 {
1204 if (available_backends.size () == 0)
1205 register_backend (new gnuplot_backend ());
1206
1207 return available_backends["gnuplot"];
1208 }
1209
1210 std::map<std::string, graphics_backend> graphics_backend::available_backends;
1211
1212 // ---------------------------------------------------------------------
1213
1149 #include "graphics-props.cc" 1214 #include "graphics-props.cc"
1150 1215
1151 // --------------------------------------------------------------------- 1216 // ---------------------------------------------------------------------
1152 1217
1153 void 1218 void
1200 visible = val; 1265 visible = val;
1201 } 1266 }
1202 } 1267 }
1203 1268
1204 void 1269 void
1205 figure::properties::close (void) 1270 figure::properties::close (bool pop)
1206 { 1271 {
1207 if (! get___plot_stream__ ().is_empty ()) 1272 if (backend)
1208 { 1273 backend.close_figure (get___plot_stream__ ());
1209 octave_value_list args; 1274
1210 args(1) = "\nquit;\n"; 1275 if (pop)
1211 args(0) = get___plot_stream__ (); 1276 {
1212 feval ("fputs", args); 1277 gh_manager::pop_figure (__myhandle__);
1213 args.resize (1); 1278
1214 feval ("fflush", args); 1279 graphics_handle cf = gh_manager::current_figure ();
1215 feval ("pclose", args); 1280
1216 } 1281 xset (0, "currentfigure", cf.value ());
1217 1282 }
1218 gh_manager::pop_figure (__myhandle__);
1219
1220 graphics_handle cf = gh_manager::current_figure ();
1221
1222 xset (0, "currentfigure", cf.value ());
1223 } 1283 }
1224 1284
1225 octave_value 1285 octave_value
1226 figure::get_default (const caseless_str& name) const 1286 figure::get_default (const caseless_str& name) const
1227 { 1287 {
2303 @end deftypefn") 2363 @end deftypefn")
2304 { 2364 {
2305 return octave_value (gh_manager::figure_handle_list ()); 2365 return octave_value (gh_manager::figure_handle_list ());
2306 } 2366 }
2307 2367
2368 DEFUN (drawnow, args, ,
2369 "-*- texinfo -*-\n\
2370 @deftypefn {Built-in Function} {} __go_drawnow__ ()\n\
2371 @deftypefnx {Built-in Function} {} __go_drawnow__ (@var{term}, @var{file}, @var{mono}, @var{debug_file})\n\
2372 Undocumented internal function.\n\
2373 @end deftypefn")
2374 {
2375 static int drawnow_executing = 0;
2376 static bool __go_close_all_registered__ = false;
2377
2378 octave_value retval;
2379
2380 if (drawnow_executing >= 1)
2381 return retval;
2382
2383 if (! __go_close_all_registered__)
2384 {
2385 // FIXME: is there a C++ way to do this?
2386 int parse_status;
2387 eval_string ("atexit (\"__go_close_all__\")", true, parse_status);
2388 __go_close_all_registered__ = true;
2389 }
2390
2391 ++drawnow_executing;
2392
2393 if (args.length () == 0)
2394 {
2395 Matrix hlist = gh_manager::figure_handle_list ();
2396
2397 for (int i = 0; ! error_state && i < hlist.length (); i++)
2398 {
2399 graphics_handle h = gh_manager::lookup (hlist(i));
2400
2401 if (h.ok () && h != 0)
2402 {
2403 graphics_object go = gh_manager::get_object (h);
2404 figure::properties& fprops = dynamic_cast <figure::properties&> (go.get_properties ());
2405
2406 if (fprops.is_modified ())
2407 {
2408 if (fprops.is_visible ())
2409 fprops.get_backend ().redraw_figure (h);
2410 else if (! fprops.get___plot_stream__ ().is_empty ())
2411 {
2412 fprops.close (false);
2413 fprops.set___plot_stream__ (Matrix ());
2414 fprops.set___enhanced__ (false);
2415 }
2416 fprops.set_modified (false);
2417 }
2418 }
2419 }
2420 }
2421 else if (args.length () >= 2 && args.length () <= 4)
2422 {
2423 std::string term, file, debug_file;
2424 bool mono;
2425
2426 term = args(0).string_value ();
2427
2428 if (! error_state)
2429 {
2430 file = args(1).string_value ();
2431
2432 if (! error_state)
2433 {
2434 int pos = file.find_last_of (file_ops::dir_sep_chars);
2435
2436 if (pos != NPOS)
2437 {
2438 file_stat fs (file.substr (0, pos));
2439
2440 if (! (fs && fs.is_dir ()))
2441 error ("drawnow: nonexistent directory `%s'",
2442 file.substr (0, pos).c_str ());
2443 }
2444
2445 mono = (args.length () >= 3 ? args(2).bool_value () : false);
2446
2447 if (! error_state)
2448 {
2449 debug_file = (args.length () > 3 ? args(3).string_value ()
2450 : "");
2451
2452 if (! error_state)
2453 {
2454 graphics_handle h = gcf ();
2455
2456 if (h.ok ())
2457 {
2458 graphics_object go = gh_manager::get_object (h);
2459 figure::properties& fprops = dynamic_cast<figure::properties&> (go.get_properties ());
2460
2461 fprops.get_backend ()
2462 .print_figure (h, term, file, mono, debug_file);
2463 }
2464 else
2465 error ("drawnow: nothing to draw");
2466 }
2467 else
2468 error ("drawnow: invalid debug_file, expected a string value");
2469 }
2470 else
2471 error ("drawnow: invalid colormode, expected a boolean value");
2472 }
2473 else
2474 error ("drawnow: invalid file, expected a string value");
2475 }
2476 else
2477 error ("drawnow: invalid terminal, expected a string value");
2478 }
2479 else
2480 print_usage ();
2481
2482 // FIXME: is there a C++ way to do this?
2483 octave_value_list fargs;
2484 fargs(0) = false;
2485 feval ("__request_drawnow__", fargs);
2486
2487 --drawnow_executing;
2488
2489 return retval;
2490 }
2491
2308 octave_value 2492 octave_value
2309 get_property_from_handle (double handle, const std::string &property, 2493 get_property_from_handle (double handle, const std::string &property,
2310 const std::string &func) 2494 const std::string &func)
2311 { 2495 {
2312 graphics_object obj = gh_manager::get_object (handle); 2496 graphics_object obj = gh_manager::get_object (handle);