1 /* 2 * Copyright 2006, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Ingo Weinhold <bonefish@cs.tu-berlin.de> 7 */ 8 9 #include "Scroller.h" 10 11 #include <stdio.h> 12 13 #include <Point.h> 14 #include <Rect.h> 15 16 #include "Scrollable.h" 17 18 // constructor 19 Scroller::Scroller() 20 : fScrollTarget(NULL) 21 { 22 } 23 24 // destructor 25 Scroller::~Scroller() 26 { 27 } 28 29 // SetScrollTarget 30 // 31 // Sets a new scroll target. Notifies the old and the new target 32 // of the change if necessary . 33 void 34 Scroller::SetScrollTarget(Scrollable* target) 35 { 36 Scrollable* oldTarget = fScrollTarget; 37 if (oldTarget != target) { 38 fScrollTarget = NULL; 39 // Notify the old target, if it doesn't know about the change. 40 if (oldTarget && oldTarget->ScrollSource() == this) 41 oldTarget->SetScrollSource(NULL); 42 fScrollTarget = target; 43 // Notify the new target, if it doesn't know about the change. 44 if (target && target->ScrollSource() != this) 45 target->SetScrollSource(this); 46 // Notify ourselves. 47 ScrollTargetChanged(oldTarget, target); 48 } 49 } 50 51 // ScrollTarget 52 // 53 // Returns the current scroll target. May be NULL, if we don't have any. 54 Scrollable* 55 Scroller::ScrollTarget() const 56 { 57 return fScrollTarget; 58 } 59 60 // SetDataRect 61 // 62 // Sets the data rect of the scroll target, if we have one. 63 void 64 Scroller::SetDataRect(BRect dataRect) 65 { 66 if (fScrollTarget) 67 fScrollTarget->SetDataRect(dataRect); 68 } 69 70 // DataRect 71 // 72 // Returns the data rect of the scroll target or a undefined value, if 73 // we have none. 74 BRect 75 Scroller::DataRect() const 76 { 77 if (fScrollTarget) 78 return fScrollTarget->DataRect(); 79 return BRect(); 80 } 81 82 // SetScrollOffset 83 // 84 // Sets the scroll offset of the scroll target, if we have one. 85 void 86 Scroller::SetScrollOffset(BPoint offset) 87 { 88 if (fScrollTarget) 89 fScrollTarget->SetScrollOffset(offset); 90 } 91 92 // ScrollOffset 93 // 94 // Returns the scroll offset of the scroll target or a undefined value, if 95 // we have none. 96 BPoint 97 Scroller::ScrollOffset() const 98 { 99 if (fScrollTarget) 100 return fScrollTarget->ScrollOffset(); 101 return BPoint(0.0, 0.0); 102 } 103 104 // SetVisibleSize 105 // 106 // Sets the visible size of the scroll target, if we have one. 107 void 108 Scroller::SetVisibleSize(float width, float height) 109 { 110 if (fScrollTarget) 111 fScrollTarget->SetVisibleSize(width, height); 112 } 113 114 // VisibleBounds 115 // 116 // Returns the visible bounds of the scroll target or a undefined value, if 117 // we have none. 118 BRect 119 Scroller::VisibleBounds() const 120 { 121 if (fScrollTarget) 122 return fScrollTarget->VisibleBounds(); 123 return BRect(); 124 } 125 126 // VisibleRect 127 // 128 // Returns the visible rect of the scroll target or a undefined value, if 129 // we have none. 130 BRect 131 Scroller::VisibleRect() const 132 { 133 if (fScrollTarget) 134 return fScrollTarget->VisibleRect(); 135 return BRect(); 136 } 137 138 139 // hooks 140 141 // DataRectChanged 142 // 143 // Hook function. Implemented by derived classes to get notified when 144 // the data rect of the sroll target has changed. 145 void 146 Scroller::DataRectChanged(BRect /*oldDataRect*/, BRect /*newDataRect*/) 147 { 148 } 149 150 // ScrollOffsetChanged 151 // 152 // Hook function. Implemented by derived classes to get notified when 153 // the scroll offset of the sroll target has changed. 154 void 155 Scroller::ScrollOffsetChanged(BPoint /*oldOffset*/, BPoint /*newOffset*/) 156 { 157 } 158 159 // VisiblSizeChanged 160 // 161 // Hook function. Implemented by derived classes to get notified when 162 // the visible size of the sroll target has changed. 163 void 164 Scroller::VisibleSizeChanged(float /*oldWidth*/, float /*oldHeight*/, 165 float /*newWidth*/, float /*newHeight*/) 166 { 167 } 168 169 // ScrollTargetChanged 170 // 171 // Hook function. Implemented by derived classes to get notified when 172 // the sroll target has changed. /target/ may be NULL. 173 void 174 Scroller::ScrollTargetChanged(Scrollable* /*oldTarget*/, 175 Scrollable* /*newTarget*/) 176 { 177 } 178 179