Mercurial > octave-nkf
comparison src/graphics.h.in @ 7964:9cd3ee5298a0
Use common rep/class pattern for graphics events
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 22 Jul 2008 16:03:19 -0400 |
parents | 78400fde223e |
children | 0ff67bd96f8d |
comparison
equal
deleted
inserted
replaced
7963:bd3fa5ca55e6 | 7964:9cd3ee5298a0 |
---|---|
3489 set_property_in_handle (double handle, const std::string &property, | 3489 set_property_in_handle (double handle, const std::string &property, |
3490 const octave_value &arg, const std::string &func); | 3490 const octave_value &arg, const std::string &func); |
3491 | 3491 |
3492 // --------------------------------------------------------------------- | 3492 // --------------------------------------------------------------------- |
3493 | 3493 |
3494 class graphics_event; | |
3495 | |
3496 class | |
3497 base_graphics_event | |
3498 { | |
3499 public: | |
3500 friend class graphics_event; | |
3501 | |
3502 base_graphics_event (void) : count (1) { } | |
3503 | |
3504 virtual ~base_graphics_event (void) { } | |
3505 | |
3506 virtual void execute (void) = 0; | |
3507 | |
3508 private: | |
3509 int count; | |
3510 }; | |
3511 | |
3512 class | |
3513 graphics_event | |
3514 { | |
3515 public: | |
3516 typedef void (*event_fcn) (void*); | |
3517 | |
3518 graphics_event (void) : rep (0) { } | |
3519 | |
3520 graphics_event (const graphics_event& e) | |
3521 { | |
3522 rep = e.rep; | |
3523 rep->count++; | |
3524 } | |
3525 | |
3526 ~graphics_event (void) | |
3527 { | |
3528 if (rep && --rep->count == 0) | |
3529 delete rep; | |
3530 } | |
3531 | |
3532 graphics_event& operator = (const graphics_event& e) | |
3533 { | |
3534 if (rep != e.rep) | |
3535 { | |
3536 if (rep && --rep->count == 0) | |
3537 delete rep; | |
3538 | |
3539 rep = e.rep; | |
3540 if (rep) | |
3541 rep->count++; | |
3542 } | |
3543 | |
3544 return *this; | |
3545 } | |
3546 | |
3547 void execute (void) | |
3548 { if (rep) rep->execute (); } | |
3549 | |
3550 bool ok (void) const | |
3551 { return (rep != 0); } | |
3552 | |
3553 static graphics_event | |
3554 create_callback_event (const graphics_handle& h, | |
3555 const std::string& name, | |
3556 const octave_value& data = Matrix ()); | |
3557 | |
3558 static graphics_event | |
3559 create_function_event (event_fcn fcn, void *data = 0); | |
3560 | |
3561 static graphics_event | |
3562 create_set_event (const graphics_handle& h, | |
3563 const std::string& name, | |
3564 const octave_value& value); | |
3565 private: | |
3566 base_graphics_event *rep; | |
3567 }; | |
3568 | |
3494 class OCTINTERP_API gh_manager | 3569 class OCTINTERP_API gh_manager |
3495 { | 3570 { |
3496 protected: | 3571 protected: |
3497 | 3572 |
3498 gh_manager (void); | 3573 gh_manager (void); |
3499 | 3574 |
3500 public: | 3575 public: |
3501 | |
3502 typedef void (*event_fcn) (void*); | |
3503 | 3576 |
3504 static bool instance_ok (void) | 3577 static bool instance_ok (void) |
3505 { | 3578 { |
3506 bool retval = true; | 3579 bool retval = true; |
3507 | 3580 |
3618 { | 3691 { |
3619 if (instance_ok ()) | 3692 if (instance_ok ()) |
3620 instance->do_post_callback (h, name, data); | 3693 instance->do_post_callback (h, name, data); |
3621 } | 3694 } |
3622 | 3695 |
3623 static void post_function (event_fcn fcn, void* data = 0) | 3696 static void post_function (graphics_event::event_fcn fcn, void* data = 0) |
3624 { | 3697 { |
3625 if (instance_ok ()) | 3698 if (instance_ok ()) |
3626 instance->do_post_function (fcn, data); | 3699 instance->do_post_function (fcn, data); |
3627 } | 3700 } |
3628 | 3701 |
3657 // No copying! | 3730 // No copying! |
3658 autolock (const autolock&); | 3731 autolock (const autolock&); |
3659 autolock& operator = (const autolock&); | 3732 autolock& operator = (const autolock&); |
3660 }; | 3733 }; |
3661 | 3734 |
3662 public: | |
3663 class event_data | |
3664 { | |
3665 public: | |
3666 event_data (void) : rep (0) { } | |
3667 | |
3668 event_data (const event_data& d) | |
3669 { | |
3670 rep = d.rep; | |
3671 if (rep) | |
3672 rep->refcount++; | |
3673 } | |
3674 | |
3675 virtual ~event_data (void) | |
3676 { | |
3677 if (rep && --rep->refcount == 0) | |
3678 { | |
3679 delete rep; | |
3680 rep = 0; | |
3681 } | |
3682 } | |
3683 | |
3684 event_data& operator = (const event_data& d) | |
3685 { | |
3686 if (d.rep != rep) | |
3687 { | |
3688 if (rep && --rep->refcount == 0) | |
3689 delete rep; | |
3690 | |
3691 rep = d.rep; | |
3692 if (rep) | |
3693 rep->refcount++; | |
3694 } | |
3695 | |
3696 return *this; | |
3697 } | |
3698 | |
3699 virtual void execute (void) | |
3700 { if (rep) rep->execute (); } | |
3701 | |
3702 bool ok (void) const { return (rep != 0); } | |
3703 | |
3704 static event_data | |
3705 create_callback_event (const graphics_handle& h, | |
3706 const std::string& name, | |
3707 const octave_value& data = Matrix ()); | |
3708 | |
3709 static event_data | |
3710 create_function_event (event_fcn fcn, void *data = 0); | |
3711 | |
3712 static event_data | |
3713 create_set_event (const graphics_handle& h, | |
3714 const std::string& name, | |
3715 const octave_value& value); | |
3716 | |
3717 protected: | |
3718 explicit event_data (int /* dummy */) | |
3719 : refcount (0) { } | |
3720 | |
3721 private: | |
3722 union | |
3723 { | |
3724 event_data *rep; | |
3725 int refcount; | |
3726 }; | |
3727 }; | |
3728 | |
3729 private: | 3735 private: |
3730 | 3736 |
3731 static gh_manager *instance; | 3737 static gh_manager *instance; |
3732 | 3738 |
3733 typedef std::map<graphics_handle, graphics_object>::iterator iterator; | 3739 typedef std::map<graphics_handle, graphics_object>::iterator iterator; |
3754 | 3760 |
3755 // The lock for accessing the graphics sytsem | 3761 // The lock for accessing the graphics sytsem |
3756 octave_mutex graphics_lock; | 3762 octave_mutex graphics_lock; |
3757 | 3763 |
3758 // The list of event queued by backends | 3764 // The list of event queued by backends |
3759 std::list<event_data> event_queue; | 3765 std::list<graphics_event> event_queue; |
3760 | 3766 |
3761 // The stack of callback objects | 3767 // The stack of callback objects |
3762 std::list<graphics_object> callback_objects; | 3768 std::list<graphics_object> callback_objects; |
3763 | 3769 |
3764 graphics_handle get_handle (const std::string& go_name); | 3770 graphics_handle get_handle (const std::string& go_name); |
3827 const octave_value& data); | 3833 const octave_value& data); |
3828 | 3834 |
3829 void do_post_callback (const graphics_handle& h, const std::string name, | 3835 void do_post_callback (const graphics_handle& h, const std::string name, |
3830 const octave_value& data); | 3836 const octave_value& data); |
3831 | 3837 |
3832 void do_post_function (event_fcn fcn, void* fcn_data); | 3838 void do_post_function (graphics_event::event_fcn fcn, void* fcn_data); |
3833 | 3839 |
3834 void do_post_set (const graphics_handle& h, const std::string name, | 3840 void do_post_set (const graphics_handle& h, const std::string name, |
3835 const octave_value& value); | 3841 const octave_value& value); |
3836 | 3842 |
3837 int do_process_events (bool force = false); | 3843 int do_process_events (bool force = false); |
3842 instance->do_restore_gcbo (); | 3848 instance->do_restore_gcbo (); |
3843 } | 3849 } |
3844 | 3850 |
3845 void do_restore_gcbo (void); | 3851 void do_restore_gcbo (void); |
3846 | 3852 |
3847 void do_post_event (const event_data& e); | 3853 void do_post_event (const graphics_event& e); |
3848 }; | 3854 }; |
3849 | 3855 |
3850 | 3856 |
3851 // This function is NOT equivalent to the scripting language function gcf. | 3857 // This function is NOT equivalent to the scripting language function gcf. |
3852 OCTINTERP_API graphics_handle gcf (void); | 3858 OCTINTERP_API graphics_handle gcf (void); |