summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Controls/ScrollerBar.cpp
blob: d333cdd78e3b7e1d0800ad83f1f0bc5fc88bb6c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//==============================================================================
//	Prefix
//==============================================================================
#include "stdafx.h"

//==============================================================================
//	Includes
//==============================================================================
#include "ScrollerBar.h"
#include "Scroller.h"
#include "Renderer.h"
#include "ButtonControl.h"
#include "CoreUtils.h"
#include "ScrollerBackground.h"
#include "ScrollerThumb.h"
#include "StudioPreferences.h"
#include "Renderer.h"
#include "SystemPreferences.h"
#include "StudioUtils.h"
#include "ScrollerButtonControl.h"
#include "MasterP.h"

//==============================================================================
//	Static variables
//==============================================================================
const long CScrollerBar::DEFAULT_WIDTH = 6;
const CColor CScrollerBar::DEFAULT_COLOR = CColor(196, 194, 189);
const CColor CScrollerBar::SCROLLER_TOP = CColor(165, 162, 161);

IMPLEMENT_OBJECT_COUNTER(CScrollerBar)

//=============================================================================
/**
 * Constructor
 * @param inScroller the scroller on which this is operating.
 * @param inCreateImmediately false for lazy construction, only for subclasses.
 */
CScrollerBar::CScrollerBar(CScroller *inScroller, bool inCreateImmediately /* = true */)
{
    ADDTO_OBJECT_COUNTER(CScrollerBar)

    m_Scroller = inScroller;
    SetFlowDirection(FLOW_HORIZONTAL);

    if (inCreateImmediately)
        Initialize();
}

//=============================================================================
/**
 * Destructor
 */
CScrollerBar::~CScrollerBar()
{
    REMOVEFROM_OBJECT_COUNTER(CScrollerBar)
}

//=============================================================================
/**
 * Initialize this scroller bar.
 * This is used for lazy construction of this object by subclasses. If a subclass
 * specifies lazy construction in the constructor of this class then it must call
 * this to put it into a valid state.
 */
void CScrollerBar::Initialize()
{
    m_ScrollerThumb = CreateThumb();

    // First button
    m_ButtonBackward = new CScrollerButtonControl(this, CScrollerButtonControl::BACKWARD);

    // Background
    m_Background = new CScrollerBackground(CStudioPreferences::GetScrollBGColor());
    m_Background->SetScrollerBar(this);
    m_Background->AddChild(m_ScrollerThumb);

    m_ScrollerThumb->SetPosition(CPt(0, 0));

    // Second button (either right or bottom)
    m_ButtonForward = new CScrollerButtonControl(this, CScrollerButtonControl::FORWARD);

    // Determine the placement of the scroll bar arrows based on the system preference
    if (CSystemPreferences::AreScrollArrowsAdjacent()) {
        // Scroll arrows go together at one end of the bar
        AddChild(m_Background);
        AddChild(m_ButtonBackward);
        AddChild(m_ButtonForward);
    } else {
        // Scroll arrows go on each end of the scroll bar
        AddChild(m_ButtonBackward);
        AddChild(m_Background);
        AddChild(m_ButtonForward);
    }
}

//=============================================================================
/**
 * Virtual class to create the thumb class.
 * This is used to allow sub-classes to specialize the scroller thumb that they
 * are using.
 * @return the thumb that was created.
 */
CControl *CScrollerBar::CreateThumb()
{
    return new CScrollerThumb(this);
}

//=============================================================================
/**
 * Set the orientation of this scroller bar.
 * This determines which way this is scrolling.
 */
void CScrollerBar::SetOrientation(EOrientation inOrientation)
{
    m_Orientation = inOrientation;
    m_Background->SetOrientation(inOrientation);

    if (m_Orientation == VERTICAL)
        SetFlowDirection(FLOW_VERTICAL);
    else
        SetFlowDirection(FLOW_HORIZONTAL);
}

//=============================================================================
/**
 * Get the orientation of this scroller bar.
 */
CScrollerBar::EOrientation CScrollerBar::GetOrientation()
{
    return m_Orientation;
}

//=============================================================================
/**
 * Get the minimum size of this scroller bar.
 * The minumum size is the width or height of both of it's buttons (depending
 * on orientation.
 * @return the minimum allowable size of this bar.
 */
CPt CScrollerBar::GetMinimumSize()
{
    if (GetOrientation() == VERTICAL) {
        return CPt(DEFAULT_WIDTH, DEFAULT_WIDTH * 2);
    } else {
        return CPt(DEFAULT_WIDTH * 2, DEFAULT_WIDTH);
    }
}

//=============================================================================
/**
 * Set the size of this scroller bar.
 */
void CScrollerBar::SetSize(CPt inSize)
{
    CFlowLayout::SetSize(inSize);

    RepositionThumb();
}

//=============================================================================
/**
 * Recalculate the size and position of the thumb control.
 */
void CScrollerBar::RepositionThumb()
{
    long theBarLen;

    if (GetOrientation() == VERTICAL) {
        float thePercentage =
            (float)m_Scroller->GetVisibleSize().y / (float)m_Scroller->GetContaineeSize().y;
        theBarLen = (long)(thePercentage * ((float)m_Background->GetSize().y));

        if (theBarLen < CScrollerThumb::MIN_LENGTH)
            theBarLen = CScrollerThumb::MIN_LENGTH;

        CPt theSize(GetMinimumSize().x, theBarLen);

        m_ScrollerThumb->SetSize(theSize);

        float theVisPosY = (float)(m_Scroller->GetVisiblePosition().y);
        float theMaxPosY = (float)(m_Scroller->GetMaxVisiblePosition().y);
        long thePosY = 0;
        if (theMaxPosY != 0)
            thePosY = ::dtol((theVisPosY / theMaxPosY)
                             * (m_Background->GetSize().y - m_ScrollerThumb->GetSize().y));

        m_ScrollerThumb->SetPosition(CPt(0, thePosY));
    } else {
        float thePercentage =
            (float)m_Scroller->GetVisibleSize().x / (float)m_Scroller->GetContaineeSize().x;
        theBarLen = ::dtol(thePercentage * ((float)m_Background->GetSize().x));
        if (theBarLen < CScrollerThumb::MIN_LENGTH)
            theBarLen = CScrollerThumb::MIN_LENGTH;

        CPt theSize(theBarLen, GetMinimumSize().y);

        m_ScrollerThumb->SetSize(theSize);

        float theVisPosX = (float)(m_Scroller->GetVisiblePosition().x);
        float theMaxPosX = (float)(m_Scroller->GetMaxVisiblePosition().x);
        long thePosX = 0;
        if (theMaxPosX != 0)
            thePosX = ::dtol((theVisPosX / theMaxPosX)
                             * (m_Background->GetSize().x - m_ScrollerThumb->GetSize().x));

        m_ScrollerThumb->SetPosition(CPt(thePosX, 0));
    }

    Invalidate();
}

IScroller *CScrollerBar::GetScroller()
{
    return m_Scroller;
};

//=============================================================================
/**
 * Called by the ScrollerThumb to reposition itself.
 * This is used to reposition the thumb, the positions here are used to derive
 * the position of the visible window, so that this provides absolute positioning
 * of the thumb.
 * @param inPosition the new position of the thumb.
 */
void CScrollerBar::SetBarPosition(long inPosition)
{
    long theAvailableSpace;
    if (GetOrientation() == HORIZONTAL) {
        theAvailableSpace = m_Background->GetSize().x - m_ScrollerThumb->GetSize().x;
    } else {
        theAvailableSpace = m_Background->GetSize().y - m_ScrollerThumb->GetSize().y;
    }

    if (inPosition > theAvailableSpace)
        inPosition = theAvailableSpace;
    if (inPosition < 0)
        inPosition = 0;

    CPt theVisPos = m_Scroller->GetVisiblePosition();
    if (GetOrientation() == HORIZONTAL) {
        m_ScrollerThumb->SetPosition(CPt(inPosition, 0));
        double theMaxPosX = (double)(m_Scroller->GetMaxVisiblePosition().x);
        double theScrollDiff = (double)(m_Background->GetSize().x - m_ScrollerThumb->GetSize().x);
        theVisPos.x = ::dtol((double)inPosition / theScrollDiff * theMaxPosX);
    } else {
        m_ScrollerThumb->SetPosition(CPt(0, inPosition));
        double theMaxPosY = (double)(m_Scroller->GetMaxVisiblePosition().y);
        double theScrollDiff = (double)(m_Background->GetSize().y - m_ScrollerThumb->GetSize().y);
        theVisPos.y = ::dtol((double)inPosition / theScrollDiff * theMaxPosY);
    }
    m_Scroller->SetVisiblePosition(theVisPos);

    RepositionThumb();

    Invalidate();
}

//=============================================================================
/**
 * Get the current position of the thumb.
 * @return the current position of the thumb.
 */
long CScrollerBar::GetBarPosition()
{
    if (GetOrientation() == HORIZONTAL)
        return m_ScrollerThumb->GetPosition().x;
    else
        return m_ScrollerThumb->GetPosition().y;
}

//=============================================================================
/**
 * Get the ScrollerThumb.
 * @return the scroller thumb.
 */
CControl *CScrollerBar::GetThumb()
{
    return m_ScrollerThumb;
}

//=============================================================================
/**
 * Get the background of the thumb.
 * @return the background of the thumb.
 */
CControl *CScrollerBar::GetThumbBackground()
{
    return m_Background;
}