summaryrefslogtreecommitdiff
path: root/indra/llui/llscrollcontainer.cpp
blob: d0b28e8f22905b43c6fc23e47f6fa34004e477b2 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/**
 * @file llscrollcontainer.cpp
 * @brief LLScrollContainer base class
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */


#include "linden_common.h"

#include "llscrollcontainer.h"

#include "llrender.h"
#include "llcontainerview.h"
#include "lllocalcliprect.h"
// #include "llfolderview.h"
#include "llscrollingpanellist.h"
#include "llscrollbar.h"
#include "llui.h"
#include "llkeyboard.h"
#include "llviewborder.h"
#include "llfocusmgr.h"
#include "llframetimer.h"
#include "lluictrlfactory.h"
#include "llpanel.h"
#include "llfontgl.h"

///----------------------------------------------------------------------------
/// Local function declarations, constants, enums, and typedefs
///----------------------------------------------------------------------------

static const S32 VERTICAL_MULTIPLE = 16;
static const F32 AUTO_SCROLL_RATE_ACCEL = 120.f;

///----------------------------------------------------------------------------
/// Class LLScrollContainer
///----------------------------------------------------------------------------

static LLDefaultChildRegistry::Register<LLScrollContainer> r("scroll_container");

#include "llscrollingpanellist.h"
#include "llcontainerview.h"
#include "llpanel.h"

static ScrollContainerRegistry::Register<LLScrollingPanelList> r1("scrolling_panel_list");
static ScrollContainerRegistry::Register<LLContainerView> r2("container_view");
static ScrollContainerRegistry::Register<LLPanel> r3("panel", &LLPanel::fromXML);

LLScrollContainer::Params::Params()
:   is_opaque("opaque"),
    bg_color("color"),
    border_visible("border_visible"),
    hide_scrollbar("hide_scrollbar"),
    ignore_arrow_keys("ignore_arrow_keys"),
    min_auto_scroll_rate("min_auto_scroll_rate", 100),
    max_auto_scroll_rate("max_auto_scroll_rate", 1000),
    max_auto_scroll_zone("max_auto_scroll_zone", 16),
    reserve_scroll_corner("reserve_scroll_corner", false),
    size("size", -1)
{}


// Default constructor
LLScrollContainer::LLScrollContainer(const LLScrollContainer::Params& p)
:   LLUICtrl(p),
    mAutoScrolling( FALSE ),
    mAutoScrollRate( 0.f ),
    mBackgroundColor(p.bg_color()),
    mIsOpaque(p.is_opaque),
    mHideScrollbar(p.hide_scrollbar),
    mIgnoreArrowKeys(p.ignore_arrow_keys),
    mReserveScrollCorner(p.reserve_scroll_corner),
    mMinAutoScrollRate(p.min_auto_scroll_rate),
    mMaxAutoScrollRate(p.max_auto_scroll_rate),
    mMaxAutoScrollZone(p.max_auto_scroll_zone),
    mScrolledView(NULL),
    mSize(p.size)
{
    static LLUICachedControl<S32> scrollbar_size_control ("UIScrollbarSize", 0);
    S32 scrollbar_size = (mSize == -1 ? scrollbar_size_control : mSize);

    LLRect border_rect( 0, getRect().getHeight(), getRect().getWidth(), 0 );
    LLViewBorder::Params params;
    params.name("scroll border");
    params.rect(border_rect);
    params.visible(p.border_visible);
    params.bevel_style(LLViewBorder::BEVEL_IN);
    mBorder = LLUICtrlFactory::create<LLViewBorder> (params);
    LLView::addChild( mBorder );

    mInnerRect = getLocalRect();
    mInnerRect.stretch( -getBorderWidth() );

    LLRect vertical_scroll_rect = mInnerRect;
    vertical_scroll_rect.mLeft = vertical_scroll_rect.mRight - scrollbar_size;
    LLScrollbar::Params sbparams;
    sbparams.name("scrollable vertical");
    sbparams.rect(vertical_scroll_rect);
    sbparams.orientation(LLScrollbar::VERTICAL);
    sbparams.doc_size(mInnerRect.getHeight());
    sbparams.doc_pos(0);
    sbparams.page_size(mInnerRect.getHeight());
    sbparams.step_size(VERTICAL_MULTIPLE);
    sbparams.follows.flags(FOLLOWS_RIGHT | FOLLOWS_TOP | FOLLOWS_BOTTOM);
    sbparams.visible(false);
    sbparams.change_callback(p.scroll_callback);
    mScrollbar[VERTICAL] = LLUICtrlFactory::create<LLScrollbar> (sbparams);
    LLView::addChild( mScrollbar[VERTICAL] );

    LLRect horizontal_scroll_rect;
    horizontal_scroll_rect.mTop = scrollbar_size;
    horizontal_scroll_rect.mRight = mInnerRect.getWidth();
    sbparams.name("scrollable horizontal");
    sbparams.rect(horizontal_scroll_rect);
    sbparams.orientation(LLScrollbar::HORIZONTAL);
    sbparams.doc_size(mInnerRect.getWidth());
    sbparams.doc_pos(0);
    sbparams.page_size(mInnerRect.getWidth());
    sbparams.step_size(VERTICAL_MULTIPLE);
    sbparams.visible(false);
    sbparams.follows.flags(FOLLOWS_LEFT | FOLLOWS_RIGHT | FOLLOWS_BOTTOM);
    sbparams.change_callback(p.scroll_callback);
    mScrollbar[HORIZONTAL] = LLUICtrlFactory::create<LLScrollbar> (sbparams);
    LLView::addChild( mScrollbar[HORIZONTAL] );
}

// Destroys the object
LLScrollContainer::~LLScrollContainer( void )
{
    // mScrolledView and mScrollbar are child views, so the LLView
    // destructor takes care of memory deallocation.
    for( S32 i = 0; i < ORIENTATION_COUNT; i++ )
    {
        mScrollbar[i] = NULL;
    }
    mScrolledView = NULL;
}

// internal scrollbar handlers
// virtual
void LLScrollContainer::scrollHorizontal( S32 new_pos )
{
    if( mScrolledView )
    {
        LLRect doc_rect = mScrolledView->getRect();
        S32 old_pos = -(doc_rect.mLeft - mInnerRect.mLeft);
        mScrolledView->translate( -(new_pos - old_pos), 0 );
    }
}

// virtual
void LLScrollContainer::scrollVertical( S32 new_pos )
{
    if( mScrolledView )
    {
        LLRect doc_rect = mScrolledView->getRect();
        S32 old_pos = doc_rect.mTop - mInnerRect.mTop;
        mScrolledView->translate( 0, new_pos - old_pos );
    }
}

// LLView functionality
void LLScrollContainer::reshape(S32 width, S32 height,
                                        BOOL called_from_parent)
{
    LLUICtrl::reshape( width, height, called_from_parent );

    mInnerRect = getLocalRect();
    mInnerRect.stretch( -getBorderWidth() );

    if (mScrolledView)
    {
        const LLRect& scrolled_rect = mScrolledView->getRect();

        S32 visible_width = 0;
        S32 visible_height = 0;
        BOOL show_v_scrollbar = FALSE;
        BOOL show_h_scrollbar = FALSE;
        calcVisibleSize( &visible_width, &visible_height, &show_h_scrollbar, &show_v_scrollbar );

        mScrollbar[VERTICAL]->setDocSize( scrolled_rect.getHeight() );
        mScrollbar[VERTICAL]->setPageSize( visible_height );

        mScrollbar[HORIZONTAL]->setDocSize( scrolled_rect.getWidth() );
        mScrollbar[HORIZONTAL]->setPageSize( visible_width );
        updateScroll();
    }
}

// virtual
BOOL LLScrollContainer::handleKeyHere(KEY key, MASK mask)
{
    if (mIgnoreArrowKeys)
    {
        switch(key)
        {
        case KEY_LEFT:
        case KEY_RIGHT:
        case KEY_UP:
        case KEY_DOWN:
        case KEY_PAGE_UP:
        case KEY_PAGE_DOWN:
        case KEY_HOME:
        case KEY_END:
            return FALSE;
        default:
            break;
        }
    }

    // allow scrolled view to handle keystrokes in case it delegated keyboard focus
    // to the scroll container.
    // NOTE: this should not recurse indefinitely as handleKeyHere
    // should not propagate to parent controls, so mScrolledView should *not*
    // call LLScrollContainer::handleKeyHere in turn
    if (mScrolledView && mScrolledView->handleKeyHere(key, mask))
    {
        return TRUE;
    }
    for( S32 i = 0; i < ORIENTATION_COUNT; i++ )
    {
        if( mScrollbar[i]->handleKeyHere(key, mask) )
        {
            updateScroll();
            return TRUE;
        }
    }

    return FALSE;
}

BOOL LLScrollContainer::handleUnicodeCharHere(llwchar uni_char)
{
    if (mScrolledView && mScrolledView->handleUnicodeCharHere(uni_char))
    {
        return TRUE;
    }
    return FALSE;
}

BOOL LLScrollContainer::handleScrollWheel( S32 x, S32 y, S32 clicks )
{
    // Give event to my child views - they may have scroll bars
    // (Bad UI design, but technically possible.)
    if (LLUICtrl::handleScrollWheel(x,y,clicks))
        return TRUE;

    // When the vertical scrollbar is visible, scroll wheel
    // only affects vertical scrolling.  It's confusing to have
    // scroll wheel perform both vertical and horizontal in a
    // single container.
    LLScrollbar* vertical = mScrollbar[VERTICAL];
    if (vertical->getVisible()
        && vertical->getEnabled())
    {
        // Pretend the mouse is over the scrollbar
        if (vertical->handleScrollWheel( 0, 0, clicks ) )
        {
            updateScroll();
        }
        // Always eat the event
        return TRUE;
    }

    LLScrollbar* horizontal = mScrollbar[HORIZONTAL];
    // Test enablement and visibility for consistency with
    // LLView::childrenHandleScrollWheel().
    if (horizontal->getVisible()
        && horizontal->getEnabled()
        && horizontal->handleScrollWheel( 0, 0, clicks ) )
    {
        updateScroll();
        return TRUE;
    }
    return FALSE;
}

BOOL LLScrollContainer::handleScrollHWheel(S32 x, S32 y, S32 clicks)
{
    if (LLUICtrl::handleScrollHWheel(x,y,clicks))
    {
        return TRUE;
    }

    LLScrollbar* horizontal = mScrollbar[HORIZONTAL];
    if (horizontal->getVisible()
        && horizontal->getEnabled()
        && horizontal->handleScrollHWheel( 0, 0, clicks ) )
    {
        updateScroll();
        return TRUE;
    }

    return FALSE;
}

BOOL LLScrollContainer::handleDragAndDrop(S32 x, S32 y, MASK mask,
                                                  BOOL drop,
                                                  EDragAndDropType cargo_type,
                                                  void* cargo_data,
                                                  EAcceptance* accept,
                                                  std::string& tooltip_msg)
{
    // Scroll folder view if needed.  Never accepts a drag or drop.
    *accept = ACCEPT_NO;
    BOOL handled = autoScroll(x, y);

    if( !handled )
    {
        handled = childrenHandleDragAndDrop(x, y, mask, drop, cargo_type,
                                            cargo_data, accept, tooltip_msg) != NULL;
    }

    return TRUE;
}

bool LLScrollContainer::canAutoScroll(S32 x, S32 y)
{
    if (mAutoScrolling)
    {
        return true; // already scrolling
    }
    return autoScroll(x, y, false);
}

bool LLScrollContainer::autoScroll(S32 x, S32 y)
{
    return autoScroll(x, y, true);
}

bool LLScrollContainer::autoScroll(S32 x, S32 y, bool do_scroll)
{
    static LLUICachedControl<S32> scrollbar_size_control ("UIScrollbarSize", 0);
    S32 scrollbar_size = (mSize == -1 ? scrollbar_size_control : mSize);

    bool scrolling = false;
    if( mScrollbar[HORIZONTAL]->getVisible() || mScrollbar[VERTICAL]->getVisible() )
    {
        LLRect screen_local_extents;
        screenRectToLocal(getRootView()->getLocalRect(), &screen_local_extents);

        LLRect inner_rect_local( 0, mInnerRect.getHeight(), mInnerRect.getWidth(), 0 );
        // Note: Will also include scrollers as scroll zones, so opposite
        // scroll zones might have different size due to visible scrollers
        if( mScrollbar[HORIZONTAL]->getVisible() )
        {
            inner_rect_local.mBottom += scrollbar_size;
        }
        if( mScrollbar[VERTICAL]->getVisible() )
        {
            inner_rect_local.mRight -= scrollbar_size;
        }

        // clip rect against root view
        inner_rect_local.intersectWith(screen_local_extents);

        S32 auto_scroll_speed = ll_round(mAutoScrollRate * LLFrameTimer::getFrameDeltaTimeF32());
        // autoscroll region should take up no more than one third of visible scroller area
        S32 auto_scroll_region_width = llmin(inner_rect_local.getWidth() / 3, (S32)mMaxAutoScrollZone);
        S32 auto_scroll_region_height = llmin(inner_rect_local.getHeight() / 3, (S32)mMaxAutoScrollZone);

        if( mScrollbar[HORIZONTAL]->getVisible() )
        {
            LLRect left_scroll_rect = screen_local_extents;
            left_scroll_rect.mRight = inner_rect_local.mLeft + auto_scroll_region_width;
            if( left_scroll_rect.pointInRect( x, y ) && (mScrollbar[HORIZONTAL]->getDocPos() > 0) )
            {
                if (do_scroll)
                {
                    mScrollbar[HORIZONTAL]->setDocPos(mScrollbar[HORIZONTAL]->getDocPos() - auto_scroll_speed);
                    mAutoScrolling = TRUE;
                }
                scrolling = true;
            }

            LLRect right_scroll_rect = screen_local_extents;
            right_scroll_rect.mLeft = inner_rect_local.mRight - auto_scroll_region_width;
            if( right_scroll_rect.pointInRect( x, y ) && (mScrollbar[HORIZONTAL]->getDocPos() < mScrollbar[HORIZONTAL]->getDocPosMax()) )
            {
                if (do_scroll)
                {
                    mScrollbar[HORIZONTAL]->setDocPos(mScrollbar[HORIZONTAL]->getDocPos() + auto_scroll_speed);
                    mAutoScrolling = TRUE;
                }
                scrolling = true;
            }
        }
        if( mScrollbar[VERTICAL]->getVisible() )
        {
            LLRect bottom_scroll_rect = screen_local_extents;
            bottom_scroll_rect.mTop = inner_rect_local.mBottom + auto_scroll_region_height;
            if( bottom_scroll_rect.pointInRect( x, y ) && (mScrollbar[VERTICAL]->getDocPos() < mScrollbar[VERTICAL]->getDocPosMax()) )
            {
                if (do_scroll)
                {
                    mScrollbar[VERTICAL]->setDocPos(mScrollbar[VERTICAL]->getDocPos() + auto_scroll_speed);
                    mAutoScrolling = TRUE;
                }
                scrolling = true;
            }

            LLRect top_scroll_rect = screen_local_extents;
            top_scroll_rect.mBottom = inner_rect_local.mTop - auto_scroll_region_height;
            if( top_scroll_rect.pointInRect( x, y ) && (mScrollbar[VERTICAL]->getDocPos() > 0) )
            {
                if (do_scroll)
                {
                    mScrollbar[VERTICAL]->setDocPos(mScrollbar[VERTICAL]->getDocPos() - auto_scroll_speed);
                    mAutoScrolling = TRUE;
                }
                scrolling = true;
            }
        }
    }
    return scrolling;
}

void LLScrollContainer::calcVisibleSize( S32 *visible_width, S32 *visible_height, BOOL* show_h_scrollbar, BOOL* show_v_scrollbar ) const
{
    const LLRect& doc_rect = getScrolledViewRect();
    static LLUICachedControl<S32> scrollbar_size_control ("UIScrollbarSize", 0);
    S32 scrollbar_size = (mSize == -1 ? scrollbar_size_control : mSize);

    S32 doc_width = doc_rect.getWidth();
    S32 doc_height = doc_rect.getHeight();

    S32 border_width = getBorderWidth();
    *visible_width = getRect().getWidth() - 2 * border_width;
    *visible_height = getRect().getHeight() - 2 * border_width;

    *show_v_scrollbar = FALSE;
    *show_h_scrollbar = FALSE;

    if (!mHideScrollbar)
    {
        // Note: 1 pixel change can happen on final animation and should not trigger
        // the display of sliders.
        if ((doc_height - *visible_height) > 1)
        {
            *show_v_scrollbar = TRUE;
            *visible_width -= scrollbar_size;
        }
        if ((doc_width - *visible_width) > 1)
        {
            *show_h_scrollbar = TRUE;
            *visible_height -= scrollbar_size;
            // Note: Do *not* recompute *show_v_scrollbar here because with
            // The view inside the scroll container should not be extended
            // to container's full height to ensure the correct computation
            // of *show_v_scrollbar after subtracting horizontal scrollbar_size.

            if( !*show_v_scrollbar && ((doc_height - *visible_height) > 1) )
            {
                *show_v_scrollbar = TRUE;
                *visible_width -= scrollbar_size;
            }
        }
    }
}


void LLScrollContainer::draw()
{
    static LLUICachedControl<S32> scrollbar_size_control ("UIScrollbarSize", 0);
    S32 scrollbar_size = (mSize == -1 ? scrollbar_size_control : mSize);

    if (mAutoScrolling)
    {
        // add acceleration to autoscroll
        mAutoScrollRate = llmin(mAutoScrollRate + (LLFrameTimer::getFrameDeltaTimeF32() * AUTO_SCROLL_RATE_ACCEL), mMaxAutoScrollRate);
    }
    else
    {
        // reset to minimum for next time
        mAutoScrollRate = mMinAutoScrollRate;
    }
    // clear this flag to be set on next call to autoScroll
    mAutoScrolling = FALSE;

    // auto-focus when scrollbar active
    // this allows us to capture user intent (i.e. stop automatically scrolling the view/etc)
    if (!hasFocus()
        && (mScrollbar[VERTICAL]->hasMouseCapture() || mScrollbar[HORIZONTAL]->hasMouseCapture()))
    {
        focusFirstItem();
    }

    if (getRect().isValid())
    {
        // Draw background
        if( mIsOpaque )
        {
            F32 alpha = getCurrentTransparency();

            gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
            gl_rect_2d(mInnerRect, mBackgroundColor.get() % alpha);
        }

        // Draw mScrolledViews and update scroll bars.
        // get a scissor region ready, and draw the scrolling view. The
        // scissor region ensures that we don't draw outside of the bounds
        // of the rectangle.
        if( mScrolledView )
        {
            updateScroll();

            // Draw the scrolled area.
            {
                S32 visible_width = 0;
                S32 visible_height = 0;
                BOOL show_v_scrollbar = FALSE;
                BOOL show_h_scrollbar = FALSE;
                calcVisibleSize( &visible_width, &visible_height, &show_h_scrollbar, &show_v_scrollbar );

                LLLocalClipRect clip(LLRect(mInnerRect.mLeft,
                        mInnerRect.mBottom + (show_h_scrollbar ? scrollbar_size : 0) + visible_height,
                        mInnerRect.mRight - (show_v_scrollbar ? scrollbar_size: 0),
                        mInnerRect.mBottom + (show_h_scrollbar ? scrollbar_size : 0)
                        ));
                drawChild(mScrolledView);
            }
        }

        // Highlight border if a child of this container has keyboard focus
        if( mBorder->getVisible() )
        {
            mBorder->setKeyboardFocusHighlight( gFocusMgr.childHasKeyboardFocus(this) );
        }

        // Draw all children except mScrolledView
        // Note: scrollbars have been adjusted by above drawing code
        for (child_list_const_reverse_iter_t child_iter = getChildList()->rbegin();
             child_iter != getChildList()->rend(); ++child_iter)
        {
            LLView *viewp = *child_iter;
            if( sDebugRects )
            {
                sDepth++;
            }
            if( (viewp != mScrolledView) && viewp->getVisible() )
            {
                drawChild(viewp);
            }
            if( sDebugRects )
            {
                sDepth--;
            }
        }
    }
} // end draw

bool LLScrollContainer::addChild(LLView* view, S32 tab_group)
{
    if (!mScrolledView)
    {
        // Use the first panel or container as the scrollable view (bit of a hack)
        mScrolledView = view;
    }

    bool ret_val = LLView::addChild(view, tab_group);

    //bring the scrollbars to the front
    sendChildToFront( mScrollbar[HORIZONTAL] );
    sendChildToFront( mScrollbar[VERTICAL] );

    return ret_val;
}

void LLScrollContainer::updateScroll()
{
    if (!getVisible() || !mScrolledView)
    {
        return;
    }
    static LLUICachedControl<S32> scrollbar_size_control ("UIScrollbarSize", 0);
    S32 scrollbar_size = (mSize == -1 ? scrollbar_size_control : mSize);

    LLRect doc_rect = mScrolledView->getRect();
    S32 doc_width = doc_rect.getWidth();
    S32 doc_height = doc_rect.getHeight();
    S32 visible_width = 0;
    S32 visible_height = 0;
    BOOL show_v_scrollbar = FALSE;
    BOOL show_h_scrollbar = FALSE;
    calcVisibleSize( &visible_width, &visible_height, &show_h_scrollbar, &show_v_scrollbar );

    S32 border_width = getBorderWidth();
    if( show_v_scrollbar )
    {
        if( doc_rect.mTop < getRect().getHeight() - border_width )
        {
            mScrolledView->translate( 0, getRect().getHeight() - border_width - doc_rect.mTop );
        }

        scrollVertical( mScrollbar[VERTICAL]->getDocPos() );
        mScrollbar[VERTICAL]->setVisible( TRUE );

        S32 v_scrollbar_height = visible_height;
        if( !show_h_scrollbar && mReserveScrollCorner )
        {
            v_scrollbar_height -= scrollbar_size;
        }
        mScrollbar[VERTICAL]->reshape( scrollbar_size, v_scrollbar_height, TRUE );

        // Make room for the horizontal scrollbar (or not)
        S32 v_scrollbar_offset = 0;
        if( show_h_scrollbar || mReserveScrollCorner )
        {
            v_scrollbar_offset = scrollbar_size;
        }
        LLRect r = mScrollbar[VERTICAL]->getRect();
        r.translate( 0, mInnerRect.mBottom - r.mBottom + v_scrollbar_offset );
        mScrollbar[VERTICAL]->setRect( r );
    }
    else
    {
        mScrolledView->translate( 0, getRect().getHeight() - border_width - doc_rect.mTop );

        mScrollbar[VERTICAL]->setVisible( FALSE );
        mScrollbar[VERTICAL]->setDocPos( 0 );
    }

    if( show_h_scrollbar )
    {
        if( doc_rect.mLeft > border_width )
        {
            mScrolledView->translate( border_width - doc_rect.mLeft, 0 );
            mScrollbar[HORIZONTAL]->setDocPos( 0 );
        }
        else
        {
            scrollHorizontal( mScrollbar[HORIZONTAL]->getDocPos() );
        }

        mScrollbar[HORIZONTAL]->setVisible( TRUE );
        S32 h_scrollbar_width = visible_width;
        if( !show_v_scrollbar && mReserveScrollCorner )
        {
            h_scrollbar_width -= scrollbar_size;
        }
        mScrollbar[HORIZONTAL]->reshape( h_scrollbar_width, scrollbar_size, TRUE );
    }
    else
    {
        mScrolledView->translate( border_width - doc_rect.mLeft, 0 );

        mScrollbar[HORIZONTAL]->setVisible( FALSE );
        mScrollbar[HORIZONTAL]->setDocPos( 0 );
    }

    mScrollbar[HORIZONTAL]->setDocSize( doc_width );
    mScrollbar[HORIZONTAL]->setPageSize( visible_width );

    mScrollbar[VERTICAL]->setDocSize( doc_height );
    mScrollbar[VERTICAL]->setPageSize( visible_height );
} // end updateScroll

void LLScrollContainer::setBorderVisible(BOOL b)
{
    mBorder->setVisible( b );
    // Recompute inner rect, as border visibility changes it
    mInnerRect = getLocalRect();
    mInnerRect.stretch( -getBorderWidth() );
}

LLRect LLScrollContainer::getVisibleContentRect()
{
    updateScroll();
    LLRect visible_rect = getContentWindowRect();
    LLRect contents_rect = mScrolledView->getRect();
    visible_rect.translate(-contents_rect.mLeft, -contents_rect.mBottom);
    return visible_rect;
}

LLRect LLScrollContainer::getContentWindowRect()
{
    updateScroll();
    LLRect scroller_view_rect;
    S32 visible_width = 0;
    S32 visible_height = 0;
    BOOL show_h_scrollbar = FALSE;
    BOOL show_v_scrollbar = FALSE;
    calcVisibleSize( &visible_width, &visible_height, &show_h_scrollbar, &show_v_scrollbar );
    S32 border_width = getBorderWidth();
    scroller_view_rect.setOriginAndSize(border_width,
                                        show_h_scrollbar ? mScrollbar[HORIZONTAL]->getRect().mTop : border_width,
                                        visible_width,
                                        visible_height);
    return scroller_view_rect;
}

// rect is in document coordinates, constraint is in display coordinates relative to content window rect
void LLScrollContainer::scrollToShowRect(const LLRect& rect, const LLRect& constraint)
{
    if (!mScrolledView)
    {
        LL_WARNS() << "LLScrollContainer::scrollToShowRect with no view!" << LL_ENDL;
        return;
    }

    LLRect content_window_rect = getContentWindowRect();
    // get document rect
    LLRect scrolled_rect = mScrolledView->getRect();

    // shrink target rect to fit within constraint region, biasing towards top left
    LLRect rect_to_constrain = rect;
    rect_to_constrain.mBottom = llmax(rect_to_constrain.mBottom, rect_to_constrain.mTop - constraint.getHeight());
    rect_to_constrain.mRight = llmin(rect_to_constrain.mRight, rect_to_constrain.mLeft + constraint.getWidth());

    // calculate allowable positions for scroller window in document coordinates
    LLRect allowable_scroll_rect(rect_to_constrain.mRight - constraint.mRight,
                                rect_to_constrain.mBottom - constraint.mBottom,
                                rect_to_constrain.mLeft - constraint.mLeft,
                                rect_to_constrain.mTop - constraint.mTop);

    // translate from allowable region for lower left corner to upper left corner
    allowable_scroll_rect.translate(0, content_window_rect.getHeight());

    S32 vert_pos = llclamp(mScrollbar[VERTICAL]->getDocPos(),
                    mScrollbar[VERTICAL]->getDocSize() - allowable_scroll_rect.mTop, // min vertical scroll
                    mScrollbar[VERTICAL]->getDocSize() - allowable_scroll_rect.mBottom); // max vertical scroll

    mScrollbar[VERTICAL]->setDocSize( scrolled_rect.getHeight() );
    mScrollbar[VERTICAL]->setPageSize( content_window_rect.getHeight() );
    mScrollbar[VERTICAL]->setDocPos( vert_pos );

    S32 horizontal_pos = llclamp(mScrollbar[HORIZONTAL]->getDocPos(),
                                allowable_scroll_rect.mLeft,
                                allowable_scroll_rect.mRight);

    mScrollbar[HORIZONTAL]->setDocSize( scrolled_rect.getWidth() );
    mScrollbar[HORIZONTAL]->setPageSize( content_window_rect.getWidth() );
    mScrollbar[HORIZONTAL]->setDocPos( horizontal_pos );

    // propagate scroll to document
    updateScroll();

    // In case we are in accordion tab notify parent to show selected rectangle
    LLRect screen_rc;
    localRectToScreen(rect_to_constrain, &screen_rc);
    notifyParent(LLSD().with("scrollToShowRect",screen_rc.getValue()));
}

void LLScrollContainer::pageUp(S32 overlap)
{
    mScrollbar[VERTICAL]->pageUp(overlap);
    updateScroll();
}

void LLScrollContainer::pageDown(S32 overlap)
{
    mScrollbar[VERTICAL]->pageDown(overlap);
    updateScroll();
}

void LLScrollContainer::goToTop()
{
    mScrollbar[VERTICAL]->setDocPos(0);
    updateScroll();
}

void LLScrollContainer::goToBottom()
{
    mScrollbar[VERTICAL]->setDocPos(mScrollbar[VERTICAL]->getDocSize());
    updateScroll();
}

S32 LLScrollContainer::getBorderWidth() const
{
    if (mBorder->getVisible())
    {
        return mBorder->getBorderWidth();
    }

    return 0;
}

void LLScrollContainer::setSize(S32 size)
{
    mSize = size;
    mScrollbar[VERTICAL]->setThickness(size);
    mScrollbar[HORIZONTAL]->setThickness(size);
}