summaryrefslogtreecommitdiff
path: root/indra/llui/llscrolllistcell.cpp
blob: 5dccf9a8ba67ce6b64f844af5ab6757c59d50cc5 (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
/**
 * @file llscrolllistcell.cpp
 * @brief Scroll lists are composed of rows (items), each of which
 * contains columns (cells).
 *
 * $LicenseInfo:firstyear=2007&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 "llscrolllistcell.h"

#include "llcheckboxctrl.h"
#include "llui.h"   // LLUIImage
#include "lluictrlfactory.h"

//static
LLScrollListCell* LLScrollListCell::create(const LLScrollListCell::Params& cell_p)
{
    LLScrollListCell* cell = NULL;

    if (cell_p.type() == "icon")
    {
        cell = new LLScrollListIcon(cell_p);
    }
    else if (cell_p.type() == "checkbox")
    {
        cell = new LLScrollListCheck(cell_p);
    }
    else if (cell_p.type() == "date")
    {
        cell = new LLScrollListDate(cell_p);
    }
    else if (cell_p.type() == "icontext")
    {
        cell = new LLScrollListIconText(cell_p);
    }
    else if (cell_p.type() == "bar")
    {
        cell = new LLScrollListBar(cell_p);
    }
    else    // default is "text"
    {
        cell = new LLScrollListText(cell_p);
    }

    if (cell_p.value.isProvided())
    {
        cell->setValue(cell_p.value);
    }

    return cell;
}


LLScrollListCell::LLScrollListCell(const LLScrollListCell::Params& p)
:   mWidth(p.width),
    mToolTip(p.tool_tip)
{}

// virtual
const LLSD LLScrollListCell::getValue() const
{
    return LLStringUtil::null;
}


// virtual
const LLSD LLScrollListCell::getAltValue() const
{
    return LLStringUtil::null;
}


//
// LLScrollListIcon
//
LLScrollListIcon::LLScrollListIcon(const LLScrollListCell::Params& p)
:   LLScrollListCell(p),
    mIcon(LLUI::getUIImage(p.value().asString())),
    mColor(p.color),
    mAlignment(p.font_halign)
{}

LLScrollListIcon::~LLScrollListIcon()
{
}

/*virtual*/
S32     LLScrollListIcon::getHeight() const
{ return mIcon ? mIcon->getHeight() : 0; }

/*virtual*/
const LLSD      LLScrollListIcon::getValue() const
{ return mIcon.isNull() ? LLStringUtil::null : mIcon->getName(); }

void LLScrollListIcon::setValue(const LLSD& value)
{
    if (value.isUUID())
    {
        // don't use default image specified by LLUUID::null, use no image in that case
        LLUUID image_id = value.asUUID();
        mIcon = image_id.notNull() ? LLUI::getUIImageByID(image_id) : LLUIImagePtr(NULL);
    }
    else
    {
        std::string value_string = value.asString();
        if (LLUUID::validate(value_string))
        {
            setValue(LLUUID(value_string));
        }
        else if (!value_string.empty())
        {
            mIcon = LLUI::getUIImage(value.asString());
        }
        else
        {
            mIcon = NULL;
        }
    }
}


void LLScrollListIcon::setColor(const LLColor4& color)
{
    mColor = color;
}

S32 LLScrollListIcon::getWidth() const
{
    // if no specified fix width, use width of icon
    if (LLScrollListCell::getWidth() == 0 && mIcon.notNull())
    {
        return mIcon->getWidth();
    }
    return LLScrollListCell::getWidth();
}


void LLScrollListIcon::draw(const LLColor4& color, const LLColor4& highlight_color)  const
{
    if (mIcon)
    {
        switch(mAlignment)
        {
        case LLFontGL::LEFT:
            mIcon->draw(0, 0, mColor);
            break;
        case LLFontGL::RIGHT:
            mIcon->draw(getWidth() - mIcon->getWidth(), 0, mColor);
            break;
        case LLFontGL::HCENTER:
            mIcon->draw((getWidth() - mIcon->getWidth()) / 2, 0, mColor);
            break;
        default:
            break;
        }
    }
}

//
// LLScrollListBar
//
LLScrollListBar::LLScrollListBar(const LLScrollListCell::Params& p)
    :   LLScrollListCell(p),
    mRatio(0),
    mColor(p.color),
    mBottom(1),
    mLeftPad(1),
    mRightPad(1)
{}

LLScrollListBar::~LLScrollListBar()
{
}

/*virtual*/
S32 LLScrollListBar::getHeight() const
{
    return LLScrollListCell::getHeight();
}

/*virtual*/
const LLSD LLScrollListBar::getValue() const
{
    return LLStringUtil::null;
}

void LLScrollListBar::setValue(const LLSD& value)
{
    if (value.has("ratio"))
    {
        mRatio = (F32)value["ratio"].asReal();
    }
    if (value.has("bottom"))
    {
        mBottom = value["bottom"].asInteger();
    }
    if (value.has("left_pad"))
    {
        mLeftPad = value["left_pad"].asInteger();
    }
    if (value.has("right_pad"))
    {
        mRightPad = value["right_pad"].asInteger();
    }
}

void LLScrollListBar::setColor(const LLColor4& color)
{
    mColor = color;
}

S32 LLScrollListBar::getWidth() const
{
    return LLScrollListCell::getWidth();
}


void LLScrollListBar::draw(const LLColor4& color, const LLColor4& highlight_color)   const
{
    S32 bar_width = getWidth() - mLeftPad - mRightPad;
    S32 left = (S32)(bar_width - bar_width * mRatio);
    left = llclamp(left, mLeftPad, getWidth() - mRightPad - 1);

    gl_rect_2d(left, mBottom, getWidth() - mRightPad, mBottom - 1, mColor);
}

//
// LLScrollListText
//
U32 LLScrollListText::sCount = 0;

LLScrollListText::LLScrollListText(const LLScrollListCell::Params& p)
:   LLScrollListCell(p),
    mText(p.label.isProvided() ? p.label() : p.value().asString()),
    mAltText(p.alt_value().asString()),
    mFont(p.font),
    mColor(p.color),
    mUseColor(p.color.isProvided()),
    mFontAlignment(p.font_halign),
    mVisible(p.visible),
    mHighlightCount( 0 ),
    mHighlightOffset( 0 )
{
    sCount++;

    mTextWidth = getWidth();

    // initialize rounded rect image
    if (!mRoundedRectImage)
    {
        mRoundedRectImage = LLUI::getUIImage("Rounded_Square");
    }
}

//virtual
void LLScrollListText::highlightText(S32 offset, S32 num_chars)
{
    mHighlightOffset = offset;
    mHighlightCount = llmax(0, num_chars);
}

//virtual
bool LLScrollListText::isText() const
{
    return true;
}

// virtual
const std::string &LLScrollListText::getToolTip() const
{
    // If base class has a tooltip, return that
    if (! LLScrollListCell::getToolTip().empty())
        return LLScrollListCell::getToolTip();

    // ...otherwise, return the value itself as the tooltip
    return mText.getString();
}

// virtual
bool LLScrollListText::needsToolTip() const
{
    // If base class has a tooltip, return that
    if (LLScrollListCell::needsToolTip())
        return LLScrollListCell::needsToolTip();

    // ...otherwise, show tooltips for truncated text
    return mFont->getWidth(mText.getString()) > getWidth();
}

//virtual
bool LLScrollListText::getVisible() const
{
    return mVisible;
}

//virtual
S32 LLScrollListText::getHeight() const
{
    return mFont->getLineHeight();
}


LLScrollListText::~LLScrollListText()
{
    sCount--;
}

S32 LLScrollListText::getContentWidth() const
{
    return mFont->getWidth(mText.getString());
}


void LLScrollListText::setColor(const LLColor4& color)
{
    mColor = color;
    mUseColor = true;
}

void LLScrollListText::setText(const LLStringExplicit& text)
{
    mText = text;
}

void LLScrollListText::setFontStyle(const U8 font_style)
{
    LLFontDescriptor new_desc(mFont->getFontDesc());
    new_desc.setStyle(font_style);
    mFont = LLFontGL::getFont(new_desc);
}

//virtual
void LLScrollListText::setValue(const LLSD& text)
{
    setText(text.asString());
}

//virtual
void LLScrollListText::setAltValue(const LLSD& text)
{
    mAltText = text.asString();
}

//virtual
const LLSD LLScrollListText::getValue() const
{
    return LLSD(mText.getString());
}

//virtual
const LLSD LLScrollListText::getAltValue() const
{
    return LLSD(mAltText.getString());
}


void LLScrollListText::draw(const LLColor4& color, const LLColor4& highlight_color) const
{
    LLColor4 display_color;
    if (mUseColor)
    {
        display_color = mColor;
    }
    else
    {
        display_color = color;
    }

    if (mHighlightCount > 0)
    {
        // Highlight text
        S32 left = 0;
        switch(mFontAlignment)
        {
        case LLFontGL::LEFT:
            left = mFont->getWidth(mText.getString(), 1, mHighlightOffset);
            break;
        case LLFontGL::RIGHT:
            left = getWidth() - mFont->getWidth(mText.getString(), mHighlightOffset, S32_MAX);
            break;
        case LLFontGL::HCENTER:
            left = (getWidth() - mFont->getWidth(mText.getString())) / 2;
            break;
        }
        LLRect highlight_rect(left - 2,
                mFont->getLineHeight() + 1,
                left + mFont->getWidth(mText.getString(), mHighlightOffset, mHighlightCount) + 1,
                1);
        mRoundedRectImage->draw(highlight_rect, highlight_color);
    }

    // Try to draw the entire string
    F32 right_x;
    U32 string_chars = mText.length();
    F32 start_x = 0.f;
    switch(mFontAlignment)
    {
    case LLFontGL::LEFT:
        start_x = 1.f;
        break;
    case LLFontGL::RIGHT:
        start_x = (F32)getWidth();
        break;
    case LLFontGL::HCENTER:
        start_x = (F32)getWidth() * 0.5f;
        break;
    }
    mFont->render(mText.getWString(), 0,
                    start_x, 0.f,
                    display_color,
                    mFontAlignment,
                    LLFontGL::BOTTOM,
                    0,
                    LLFontGL::NO_SHADOW,
                    string_chars,
                    getTextWidth(),
                    &right_x,
                    true);
}

//
// LLScrollListCheck
//
LLScrollListCheck::LLScrollListCheck(const LLScrollListCell::Params& p)
:   LLScrollListCell(p)
{
    LLCheckBoxCtrl::Params checkbox_p;
    checkbox_p.name("checkbox");
    checkbox_p.rect = LLRect(0, p.width, p.width, 0);
    checkbox_p.enabled(p.enabled);
    checkbox_p.initial_value(p.value());

    mCheckBox = LLUICtrlFactory::create<LLCheckBoxCtrl>(checkbox_p);

    LLRect rect(mCheckBox->getRect());
    if (p.width)
    {
        rect.mRight = rect.mLeft + p.width;
        mCheckBox->setRect(rect);
        setWidth(p.width);
    }
    else
    {
        setWidth(rect.getWidth()); //check_box->getWidth();
    }

    mCheckBox->setColor(p.color);
}


LLScrollListCheck::~LLScrollListCheck()
{
    delete mCheckBox;
    mCheckBox = NULL;
}

void LLScrollListCheck::draw(const LLColor4& color, const LLColor4& highlight_color) const
{
    mCheckBox->draw();
}

bool LLScrollListCheck::handleClick()
{
    if (mCheckBox->getEnabled())
    {
        mCheckBox->toggle();
    }
    // don't change selection when clicking on embedded checkbox
    return true;
}

/*virtual*/
const LLSD LLScrollListCheck::getValue() const
{
    return mCheckBox->getValue();
}

/*virtual*/
void LLScrollListCheck::setValue(const LLSD& value)
{
    mCheckBox->setValue(value);
}

/*virtual*/
void LLScrollListCheck::onCommit()
{
    mCheckBox->onCommit();
}

/*virtual*/
void LLScrollListCheck::setEnabled(bool enable)
{
    mCheckBox->setEnabled(enable);
}

//
// LLScrollListDate
//

LLScrollListDate::LLScrollListDate( const LLScrollListCell::Params& p)
:   LLScrollListText(p),
    mDate(p.value().asDate())
{}

void LLScrollListDate::setValue(const LLSD& value)
{
    mDate = value.asDate();
    LLScrollListText::setValue(mDate.asRFC1123());
}

const LLSD LLScrollListDate::getValue() const
{
    return mDate;
}

//
// LLScrollListIconText
//
LLScrollListIconText::LLScrollListIconText(const LLScrollListCell::Params& p)
    : LLScrollListText(p),
    mIcon(p.value().isUUID() ? LLUI::getUIImageByID(p.value().asUUID()) : LLUI::getUIImage(p.value().asString())),
    mPad(4)
{
    mTextWidth = getWidth() - mPad /*padding*/ - mFont->getLineHeight();
}

LLScrollListIconText::~LLScrollListIconText()
{
}

const LLSD LLScrollListIconText::getValue() const
{
    if (mIcon.isNull())
    {
        return LLStringUtil::null;
    }
    return mIcon->getName();
}

void LLScrollListIconText::setValue(const LLSD& value)
{
    if (value.isUUID())
    {
        // don't use default image specified by LLUUID::null, use no image in that case
        LLUUID image_id = value.asUUID();
        mIcon = image_id.notNull() ? LLUI::getUIImageByID(image_id) : LLUIImagePtr(NULL);
    }
    else
    {
        std::string value_string = value.asString();
        if (LLUUID::validate(value_string))
        {
            setValue(LLUUID(value_string));
        }
        else if (!value_string.empty())
        {
            mIcon = LLUI::getUIImage(value.asString());
        }
        else
        {
            mIcon = NULL;
        }
    }
}

void LLScrollListIconText::setWidth(S32 width)
{
    LLScrollListCell::setWidth(width);
    // Assume that iamge height and width is identical to font height and width
    mTextWidth = width - mPad /*padding*/ - mFont->getLineHeight();
}


void LLScrollListIconText::draw(const LLColor4& color, const LLColor4& highlight_color)  const
{
    LLColor4 display_color;
    if (mUseColor)
    {
        display_color = mColor;
    }
    else
    {
        display_color = color;
    }

    S32 icon_height = mFont->getLineHeight();
    S32 icon_space = mIcon ? (icon_height + mPad) : 0;

    if (mHighlightCount > 0)
    {
        S32 left = 0;
        switch (mFontAlignment)
        {
        case LLFontGL::LEFT:
            left = mFont->getWidth(mText.getString(), icon_space + 1, mHighlightOffset);
            break;
        case LLFontGL::RIGHT:
            left = getWidth() - mFont->getWidth(mText.getString(), mHighlightOffset, S32_MAX) - icon_space;
            break;
        case LLFontGL::HCENTER:
            left = (getWidth() - mFont->getWidth(mText.getString()) - icon_space) / 2;
            break;
        }
        LLRect highlight_rect(left - 2,
            mFont->getLineHeight() + 1,
            left + mFont->getWidth(mText.getString(), mHighlightOffset, mHighlightCount) + 1,
            1);
        mRoundedRectImage->draw(highlight_rect, highlight_color);
    }

    // Try to draw the entire string
    F32 right_x;
    U32 string_chars = mText.length();
    F32 start_text_x = 0.f;
    S32 start_icon_x = 0;
    switch (mFontAlignment)
    {
    case LLFontGL::LEFT:
        start_text_x = icon_space + 1.f;
        start_icon_x = 1;
        break;
    case LLFontGL::RIGHT:
        start_text_x = (F32)getWidth();
        start_icon_x = getWidth() - mFont->getWidth(mText.getString()) - icon_space;
        break;
    case LLFontGL::HCENTER:
        F32 center = (F32)getWidth()* 0.5f;
        start_text_x = center + ((F32)icon_space * 0.5f);
        start_icon_x = (S32)(center - (((F32)icon_space + mFont->getWidth(mText.getString())) * 0.5f));
        break;
    }
    mFont->render(mText.getWString(), 0,
        start_text_x, 0.f,
        display_color,
        mFontAlignment,
        LLFontGL::BOTTOM,
        0,
        LLFontGL::NO_SHADOW,
        string_chars,
        getTextWidth(),
        &right_x,
        true);

    if (mIcon)
    {
        mIcon->draw(start_icon_x, 0, icon_height, icon_height, mColor);
    }
}