xref: /haiku/src/tests/kits/interface/layout/widget_layout_test/TwoDimensionalSliderView.cpp (revision 21156fc9e03988b5c8ab479d7bf5fe8ca3f73e49)
1 /*
2  * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include "TwoDimensionalSliderView.h"
7 
8 #include <View.h>
9 
10 
TwoDimensionalSliderView(BMessage * message,BMessenger target)11 TwoDimensionalSliderView::TwoDimensionalSliderView(BMessage* message,
12 	BMessenger target)
13 	: View(BRect(0, 0, 4, 4)),
14 	  BInvoker(message, target),
15 	  fMinLocation(0, 0),
16 	  fMaxLocation(0, 0),
17 	  fDragging(false)
18 {
19 	SetViewColor((rgb_color){255, 0, 0, 255});
20 }
21 
22 
23 void
SetLocationRange(BPoint minLocation,BPoint maxLocation)24 TwoDimensionalSliderView::SetLocationRange(BPoint minLocation,
25 	BPoint maxLocation)
26 {
27 	if (maxLocation.x < minLocation.x)
28 		maxLocation.x = minLocation.x;
29 	if (maxLocation.y < minLocation.y)
30 		maxLocation.y = minLocation.y;
31 
32 	fMinLocation = minLocation;
33 	fMaxLocation = maxLocation;
34 
35 	// force valid value
36 	SetValue(Value());
37 }
38 
39 
40 BPoint
MinLocation() const41 TwoDimensionalSliderView::MinLocation() const
42 {
43 	return fMinLocation;
44 }
45 
46 
47 BPoint
MaxLocation() const48 TwoDimensionalSliderView::MaxLocation() const
49 {
50 	return fMaxLocation;
51 }
52 
53 
54 BPoint
Value() const55 TwoDimensionalSliderView::Value() const
56 {
57 	return Location() - fMinLocation;
58 }
59 
60 
61 void
SetValue(BPoint value)62 TwoDimensionalSliderView::SetValue(BPoint value)
63 {
64 	BPoint location = fMinLocation + value;
65 	if (location.x < fMinLocation.x)
66 		location.x = fMinLocation.x;
67 	if (location.y < fMinLocation.y)
68 		location.y = fMinLocation.y;
69 	if (location.x > fMaxLocation.x)
70 		location.x = fMaxLocation.x;
71 	if (location.y > fMaxLocation.y)
72 		location.y = fMaxLocation.y;
73 
74 	if (location != Location()) {
75 		SetFrame(Frame().OffsetToCopy(location));
76 
77 		// send the message
78 		if (Message()) {
79 			BMessage message(*Message());
80 			message.AddPoint("value", Value());
81 			InvokeNotify(&message);
82 		}
83 	}
84 }
85 
86 
87 void
MouseDown(BPoint where,uint32 buttons,int32 modifiers)88 TwoDimensionalSliderView::MouseDown(BPoint where, uint32 buttons,
89 	int32 modifiers)
90 {
91 	if (fDragging)
92 		return;
93 
94 	fOriginalLocation = Frame().LeftTop();
95 	fOriginalPoint = ConvertToContainer(where);
96 	fDragging = true;
97 }
98 
99 
100 void
MouseUp(BPoint where,uint32 buttons,int32 modifiers)101 TwoDimensionalSliderView::MouseUp(BPoint where, uint32 buttons, int32 modifiers)
102 {
103 	if (!fDragging || (buttons & B_PRIMARY_MOUSE_BUTTON))
104 		return;
105 
106 	fDragging = false;
107 }
108 
109 
110 void
MouseMoved(BPoint where,uint32 buttons,int32 modifiers)111 TwoDimensionalSliderView::MouseMoved(BPoint where, uint32 buttons,
112 	int32 modifiers)
113 {
114 	if (!fDragging)
115 		return;
116 
117 	BPoint moved = ConvertToContainer(where) - fOriginalPoint;
118 	SetValue(fOriginalLocation - fMinLocation + moved);
119 }
120