cgv
Loading...
Searching...
No Matches
control.cxx
1#include "view.h"
2#include "control.h"
3#include <map>
4namespace cgv {
5 namespace gui {
6
8abst_view::abst_view(const std::string& name) : cgv::base::node(name)
9{
10 next_in_list = 0;
11 prev_in_list = 0;
12 ptr = 0;
13}
14
15bool abst_control::access_on_release(const void* value_ptr, char access_mode)
16{
17 static std::map<const void*, bool> on_release_map;
18 auto iter = on_release_map.find(value_ptr);
19 if (access_mode == 'q')
20 return iter == on_release_map.end() ? false : iter->second;
21 if (access_mode == 's') {
22 if (iter == on_release_map.end())
23 on_release_map[value_ptr] = true;
24 else
25 iter->second = true;
26 return true;
27 }
28 if (iter != on_release_map.end())
29 iter->second = false;
30 return false;
31}
32
33bool abst_control::on_release(const void* value_ptr)
34{
35 return access_on_release(value_ptr, 'q');
36}
37
39abst_control::abst_control(const std::string& name) : abst_view(name)
40{
41}
42
43std::map<const void*, abst_view*>& ref_view_map()
44{
45 static std::map<const void*, abst_view*> view_map;
46 return view_map;
47}
48
50bool abst_control::shows(const void* ptr) const
51{
52 return controls(ptr);
53}
54
57{
58 if (ptr)
60}
61
63void abst_view::attach_to_reference(const void* ptr)
64{
65 if (this->ptr)
67 std::map<const void*, abst_view*>& vm = ref_view_map();
68 if (vm.find(ptr) == vm.end()) {
69 vm[ptr] = this;
70 }
71 else {
72 abst_view* v = vm[ptr];
73 next_in_list = v->next_in_list;
74 prev_in_list = v;
75 v->next_in_list = this;
76 if (next_in_list)
77 next_in_list->prev_in_list = this;
78 }
79 this->ptr = ptr;
80}
81
84{
85 std::map<const void*, abst_view*>& vm = ref_view_map();
86 if (vm.find(ptr) == vm.end())
87 return;
88 if (vm[ptr] == this) {
89 if (next_in_list == 0)
90 vm.erase(ptr);
91 else {
92 vm[ptr] = next_in_list;
93 next_in_list->prev_in_list = 0;
94 }
95 }
96 else {
97 if (prev_in_list == 0) {
98 if (next_in_list == 0)
99 return;
100 std::cerr << "ups, abst_view::detach_from_reference called for view that is not representative and has no previous element in list" << std::endl;
101 next_in_list = 0;
102 return;
103 }
104 prev_in_list->next_in_list = next_in_list;
105 if (next_in_list)
106 next_in_list->prev_in_list = prev_in_list;
107 }
108
109 next_in_list = prev_in_list = 0;
110 return;
111}
112
113void update_views(void* member_ptr)
114{
115 std::map<const void*, abst_view*>& vm = ref_view_map();
116 if (vm.find(member_ptr) != vm.end()) {
117 abst_view* v = vm[member_ptr];
118 v->update();
119 v->update_views();
120 }
121}
122
123
126{
127 abst_view* v = next_in_list;
128 while (v != 0) {
129 v->update();
130 v = v->next_in_list;
131 }
132 v = prev_in_list;
133 while (v != 0) {
134 v->update();
135 v = v->prev_in_list;
136 }
137}
138
139 }
140}
virtual void update()
this virtual update allows for example to ask a view to update the viewed value. The default implemen...
Definition base.cxx:61
virtual bool controls(const void *ptr) const =0
return whether the control controls the value pointed to by ptr
static bool access_on_release(const void *value_ptr, char access_mode)
access on_release value for given value ptr to 'q'uery, 's'et or 'u'nset
Definition control.cxx:15
static bool on_release(const void *value_ptr)
return whether value_change signal was sent due to a release event
Definition control.cxx:33
bool shows(const void *ptr) const
add default implementation passing the query to the controls() method
Definition control.cxx:50
abst_control(const std::string &name)
construct from name
Definition control.cxx:39
type independent &base class of all views
Definition view.h:13
void detach_from_reference()
detach view again
Definition control.cxx:83
abst_view(const std::string &name)
pass name on to node, careful the implementation of this is in the source file control....
Definition control.cxx:8
void update_views()
calls the update method of all other attached views
Definition control.cxx:125
void attach_to_reference(const void *ptr)
links all views to a reference into a doubly linked list in order to allow controls of the reference ...
Definition control.cxx:63
~abst_view()
ensures detachment of view
Definition control.cxx:56
the cgv namespace
Definition print.h:11