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
16abst_control::abst_control(const std::string& name) : abst_view(name)
17{
18}
19
20std::map<const void*, abst_view*>& ref_view_map()
21{
22 static std::map<const void*, abst_view*> view_map;
23 return view_map;
24}
25
27bool abst_control::shows(const void* ptr) const
28{
29 return controls(ptr);
30}
31
34{
35 if (ptr)
37}
38
40void abst_view::attach_to_reference(const void* ptr)
41{
42 if (this->ptr)
44 std::map<const void*, abst_view*>& vm = ref_view_map();
45 if (vm.find(ptr) == vm.end()) {
46 vm[ptr] = this;
47 }
48 else {
49 abst_view* v = vm[ptr];
50 next_in_list = v->next_in_list;
51 prev_in_list = v;
52 v->next_in_list = this;
53 if (next_in_list)
54 next_in_list->prev_in_list = this;
55 }
56 this->ptr = ptr;
57}
58
61{
62 std::map<const void*, abst_view*>& vm = ref_view_map();
63 if (vm.find(ptr) == vm.end())
64 return;
65 if (vm[ptr] == this) {
66 if (next_in_list == 0)
67 vm.erase(ptr);
68 else {
69 vm[ptr] = next_in_list;
70 next_in_list->prev_in_list = 0;
71 }
72 }
73 else {
74 if (prev_in_list == 0) {
75 if (next_in_list == 0)
76 return;
77 std::cerr << "ups, abst_view::detach_from_reference called for view that is not representative and has no previous element in list" << std::endl;
78 next_in_list = 0;
79 return;
80 }
81 prev_in_list->next_in_list = next_in_list;
82 if (next_in_list)
83 next_in_list->prev_in_list = prev_in_list;
84 }
85
86 next_in_list = prev_in_list = 0;
87 return;
88}
89
90void update_views(void* member_ptr)
91{
92 std::map<const void*, abst_view*>& vm = ref_view_map();
93 if (vm.find(member_ptr) != vm.end()) {
94 abst_view* v = vm[member_ptr];
95 v->update();
96 v->update_views();
97 }
98}
99
100
103{
104 abst_view* v = next_in_list;
105 while (v != 0) {
106 v->update();
107 v = v->next_in_list;
108 }
109 v = prev_in_list;
110 while (v != 0) {
111 v->update();
112 v = v->prev_in_list;
113 }
114}
115
116 }
117}
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
bool shows(const void *ptr) const
add default implementation passing the query to the controls() method
Definition control.cxx:27
abst_control(const std::string &name)
construct from name
Definition control.cxx:16
type independent &base class of all views
Definition view.h:13
void detach_from_reference()
detach view again
Definition control.cxx:60
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:102
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:40
~abst_view()
ensures detachment of view
Definition control.cxx:33
the cgv namespace
Definition print.h:11