xref: /haiku/src/apps/cortex/ParameterView/ParameterContainerView.cpp (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 // ParameterContainerView.cpp
2 
3 #include "ParameterContainerView.h"
4 
5 // Interface Kit
6 #include <ScrollBar.h>
7 #include <View.h>
8 
9 __USE_CORTEX_NAMESPACE
10 
11 #include <Debug.h>
12 #define D_ALLOC(x) //PRINT (x)
13 #define D_HOOK(x) //PRINT (x)
14 #define D_INTERNAL(x) //PRINT (x)
15 
16 // -------------------------------------------------------- //
17 // *** ctor/dtor
18 // -------------------------------------------------------- //
19 
20 ParameterContainerView::ParameterContainerView(
21 	BRect dataRect,
22 	BView *target) :
23 		BView(
24 			target->Frame(),
25 			"ParameterContainerView",
26 			B_FOLLOW_ALL_SIDES,
27 			B_FRAME_EVENTS|B_WILL_DRAW),
28 		m_target(target),
29 	  m_dataRect(dataRect),
30 	  m_boundsRect(Bounds()),
31 	  m_hScroll(0),
32 	  m_vScroll(0) {
33 	D_ALLOC(("ParameterContainerView::ParameterContainerView()\n"));
34 
35 	BView* wrapper = new BView(
36 		m_target->Bounds(), 0, B_FOLLOW_ALL_SIDES, B_WILL_DRAW|B_FRAME_EVENTS);
37 	m_target->SetResizingMode(B_FOLLOW_LEFT|B_FOLLOW_TOP);
38 	m_target->MoveTo(B_ORIGIN);
39 	wrapper->AddChild(m_target);
40 	AddChild(wrapper);
41 
42 	BRect b = wrapper->Frame();
43 
44 	ResizeTo(
45 		b.Width() + B_V_SCROLL_BAR_WIDTH,
46 		b.Height() + B_H_SCROLL_BAR_HEIGHT);
47 
48 	BRect hsBounds = b;
49 	hsBounds.left--;
50 	hsBounds.top = hsBounds.bottom + 1;
51 	hsBounds.right++;
52 	hsBounds.bottom = hsBounds.top + B_H_SCROLL_BAR_HEIGHT + 1;
53 	m_hScroll = new BScrollBar(
54 		hsBounds,
55 		"hScrollBar",
56 		m_target,
57 		0, 0, B_HORIZONTAL);
58 	AddChild(m_hScroll);
59 
60 	BRect vsBounds = b;
61 	vsBounds.left = vsBounds.right + 1;
62 	vsBounds.top--;
63 	vsBounds.right = vsBounds.right + B_V_SCROLL_BAR_WIDTH + 1;
64 	vsBounds.bottom++;
65 	m_vScroll = new BScrollBar(
66 		vsBounds,
67 		"vScrollBar",
68 		m_target,
69 		0, 0, B_VERTICAL);
70 	AddChild(m_vScroll);
71 
72 
73 	SetViewColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), B_LIGHTEN_1_TINT));
74 	_updateScrollBars();
75 }
76 
77 ParameterContainerView::~ParameterContainerView() {
78 	D_ALLOC(("ParameterContainerView::~ParameterContainerView()\n"));
79 
80 }
81 
82 // -------------------------------------------------------- //
83 // *** BScrollView impl
84 // -------------------------------------------------------- //
85 
86 void ParameterContainerView::FrameResized(
87 	float width,
88 	float height) {
89 	D_HOOK(("ParameterContainerView::FrameResized()\n"));
90 
91 	BView::FrameResized(width, height);
92 //	BRect b = ChildAt(0)->Frame();
93 //	printf("param view:\n");
94 //	b.PrintToStream();
95 //	printf("hScroll:\n");
96 //	m_target->ScrollBar(B_HORIZONTAL)->Frame().PrintToStream();
97 //	printf("vScroll:\n");
98 //	m_target->ScrollBar(B_VERTICAL)->Frame().PrintToStream();
99 //	fprintf(stderr, "m: %.1f,%.1f c: %.1f,%.1f)\n", width, height, b.Width(),b.Height());
100 
101 	if(height > m_boundsRect.Height()) {
102 		Invalidate(BRect(
103 			m_boundsRect.left, m_boundsRect.bottom, m_boundsRect.right, m_boundsRect.top+height));
104 	}
105 
106 	if(width > m_boundsRect.Width()) {
107 		Invalidate(BRect(
108 			m_boundsRect.right, m_boundsRect.top, m_boundsRect.left+width, m_boundsRect.bottom));
109 	}
110 
111 	m_boundsRect = Bounds();
112 	_updateScrollBars();
113 }
114 
115 // -------------------------------------------------------- //
116 // *** internal operations
117 // -------------------------------------------------------- //
118 
119 void ParameterContainerView::_updateScrollBars() {
120 	D_INTERNAL(("ParameterContainerView::_updateScrollBars()\n"));
121 
122 	// fetch the vertical ScrollBar
123 	if (m_vScroll) {
124 		float height = Bounds().Height() - B_H_SCROLL_BAR_HEIGHT;
125 		// Disable the ScrollBar if the window is large enough to display
126 		// the entire parameter view
127 		D_INTERNAL((" -> dataRect.Height() = %f   scrollView.Height() = %f\n", m_dataRect.Height(), height));
128 		if (height > m_dataRect.Height()) {
129 			D_INTERNAL((" -> disable vertical scroll bar\n"));
130 			m_vScroll->SetRange(0.0, 0.0);
131 			m_vScroll->SetProportion(1.0);
132 		}
133 		else {
134 			D_INTERNAL((" -> enable vertical scroll bar\n"));
135 			m_vScroll->SetRange(m_dataRect.top, m_dataRect.bottom - height);
136 			m_vScroll->SetProportion(height / m_dataRect.Height());
137 		}
138 	}
139 	if (m_hScroll) {
140 		float width = Bounds().Width() - B_V_SCROLL_BAR_WIDTH;
141 		// Disable the ScrollBar if the view is large enough to display
142 		// the entire data-rect
143 		D_INTERNAL((" -> dataRect.Width() = %f   scrollView.Width() = %f\n", m_dataRect.Width(), width));
144 		if (width > m_dataRect.Width()) {
145 			D_INTERNAL((" -> disable horizontal scroll bar\n"));
146 			m_hScroll->SetRange(0.0, 0.0);
147 			m_hScroll->SetProportion(1.0);
148 		}
149 		else {
150 			D_INTERNAL((" -> enable horizontal scroll bar\n"));
151 			m_hScroll->SetRange(m_dataRect.left, m_dataRect.right - width);
152 			m_hScroll->SetProportion(width / m_dataRect.Width());
153 		}
154 	}
155 }
156 
157 // END -- ParameterContainerView.cpp --
158