summaryrefslogtreecommitdiff
path: root/indra/llui/llscrolllistcolumn.cpp
blob: a4510d1fc248652722ac6f8b24549f727322272e (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
/**
 * @file llscrollcolumnheader.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 "llscrolllistcolumn.h"

#include "llbutton.h"
#include "llresizebar.h"
#include "llscrolllistcell.h"
#include "llscrolllistctrl.h"
#include "llscrolllistitem.h"
#include "lluictrlfactory.h"

const S32 MIN_COLUMN_WIDTH = 20;

// defaults for LLScrollColumnHeader param block pulled from widgets/scroll_column_header.xml
static LLWidgetNameRegistry::StaticRegistrar sRegisterColumnHeaderParams(&typeid(LLScrollColumnHeader::Params), "scroll_column_header");

//---------------------------------------------------------------------------
// LLScrollColumnHeader
//---------------------------------------------------------------------------
LLScrollColumnHeader::Params::Params()
:   column("column")
{}


LLScrollColumnHeader::LLScrollColumnHeader(const LLScrollColumnHeader::Params& p)
:   LLButton(p), // use combobox params to steal images
    mColumn(p.column),
    mHasResizableElement(false)
{
    setClickedCallback(boost::bind(&LLScrollColumnHeader::onClick, this, _2));

    // resize handles on left and right
    const S32 RESIZE_BAR_THICKNESS = 3;
    LLResizeBar::Params resize_bar_p;
    resize_bar_p.resizing_view(this);
    resize_bar_p.rect(LLRect(getRect().getWidth() - RESIZE_BAR_THICKNESS, getRect().getHeight(), getRect().getWidth(), 0));
    resize_bar_p.min_size(MIN_COLUMN_WIDTH);
    resize_bar_p.side(LLResizeBar::RIGHT);
    resize_bar_p.enabled(false);
    mResizeBar = LLUICtrlFactory::create<LLResizeBar>(resize_bar_p);
    addChild(mResizeBar);
}

LLScrollColumnHeader::~LLScrollColumnHeader()
{}

void LLScrollColumnHeader::draw()
{
    std::string sort_column = mColumn->mParentCtrl->getSortColumnName();
    bool draw_arrow = !mColumn->mLabel.empty()
            && mColumn->mParentCtrl->isSorted()
            // check for indirect sorting column as well as column's sorting name
            && (sort_column == mColumn->mSortingColumn || sort_column == mColumn->mName);

    bool is_ascending = mColumn->mParentCtrl->getSortAscending();
    if (draw_arrow)
    {
        setImageOverlay(is_ascending ? "up_arrow.tga" : "down_arrow.tga", LLFontGL::RIGHT, LLColor4::white);
    }
    else
    {
        setImageOverlay(LLUUID::null);
    }

    // Draw children
    LLButton::draw();
}

bool LLScrollColumnHeader::handleDoubleClick(S32 x, S32 y, MASK mask)
{
    if (canResize() && mResizeBar->getRect().pointInRect(x, y))
    {
        // reshape column to max content width
        mColumn->mParentCtrl->calcMaxContentWidth();
        LLRect column_rect = getRect();
        column_rect.mRight = column_rect.mLeft + mColumn->mMaxContentWidth;
        setShape(column_rect, true);
    }
    else
    {
        onClick(LLSD());
    }
    return true;
}

void LLScrollColumnHeader::onClick(const LLSD& data)
{
    if (mColumn)
    {
        LLScrollListCtrl::onClickColumn(mColumn);
    }
}

LLView* LLScrollColumnHeader::findSnapEdge(S32& new_edge_val, const LLCoordGL& mouse_dir, ESnapEdge snap_edge, ESnapType snap_type, S32 threshold, S32 padding)
{
    // this logic assumes dragging on right
    llassert(snap_edge == SNAP_RIGHT);

    // use higher snap threshold for column headers
    threshold = llmin(threshold, 10);

    LLRect snap_rect = getSnapRect();

    mColumn->mParentCtrl->calcMaxContentWidth();

    S32 snap_delta = mColumn->mMaxContentWidth - snap_rect.getWidth();

    // x coord growing means column growing, so same signs mean we're going in right direction
    if (llabs(snap_delta) <= threshold && mouse_dir.mX * snap_delta > 0 )
    {
        new_edge_val = snap_rect.mRight + snap_delta;
    }
    else
    {
        LLScrollListColumn* next_column = mColumn->mParentCtrl->getColumn(mColumn->mIndex + 1);
        while (next_column)
        {
            if (next_column->mHeader)
            {
                snap_delta = (next_column->mHeader->getSnapRect().mRight - next_column->mMaxContentWidth) - snap_rect.mRight;
                if (llabs(snap_delta) <= threshold && mouse_dir.mX * snap_delta > 0 )
                {
                    new_edge_val = snap_rect.mRight + snap_delta;
                }
                break;
            }
            next_column = mColumn->mParentCtrl->getColumn(next_column->mIndex + 1);
        }
    }

    return this;
}

void LLScrollColumnHeader::handleReshape(const LLRect& new_rect, bool by_user)
{
    S32 new_width = new_rect.getWidth();
    S32 delta_width = new_width - (getRect().getWidth() /*+ mColumn->mParentCtrl->getColumnPadding()*/);

    if (delta_width != 0)
    {
        S32 remaining_width = -delta_width;
        S32 col;
        for (col = mColumn->mIndex + 1; col < mColumn->mParentCtrl->getNumColumns(); col++)
        {
            LLScrollListColumn* columnp = mColumn->mParentCtrl->getColumn(col);
            if (!columnp) continue;

            if (columnp->mHeader && columnp->mHeader->canResize())
            {
                // how many pixels in width can this column afford to give up?
                S32 resize_buffer_amt = llmax(0, columnp->getWidth() - MIN_COLUMN_WIDTH);

                // user shrinking column, need to add width to other columns
                if (delta_width < 0)
                {
                    if (columnp->getWidth() > 0)
                    {
                        // statically sized column, give all remaining width to this column
                        columnp->setWidth(columnp->getWidth() + remaining_width);
                        if (columnp->mRelWidth > 0.f)
                        {
                            columnp->mRelWidth = (F32)columnp->getWidth() / (F32)mColumn->mParentCtrl->getItemListRect().getWidth();
                        }
                        // all padding went to this widget, we're done
                        break;
                    }
                }
                else
                {
                    // user growing column, need to take width from other columns
                    remaining_width += resize_buffer_amt;

                    if (columnp->getWidth() > 0)
                    {
                        columnp->setWidth(columnp->getWidth() - llmin(columnp->getWidth() - MIN_COLUMN_WIDTH, delta_width));
                        if (columnp->mRelWidth > 0.f)
                        {
                            columnp->mRelWidth = (F32)columnp->getWidth() / (F32)mColumn->mParentCtrl->getItemListRect().getWidth();
                        }
                    }

                    if (remaining_width >= 0)
                    {
                        // width sucked up from neighboring columns, done
                        break;
                    }
                }
            }
        }

        // clamp resize amount to maximum that can be absorbed by other columns
        if (delta_width > 0)
        {
            delta_width += llmin(remaining_width, 0);
        }

        // propagate constrained delta_width to new width for this column
        new_width = getRect().getWidth() + delta_width - mColumn->mParentCtrl->getColumnPadding();

        // use requested width
        mColumn->setWidth(new_width);

        // update proportional spacing
        if (mColumn->mRelWidth > 0.f)
        {
            mColumn->mRelWidth = (F32)new_width / (F32)mColumn->mParentCtrl->getItemListRect().getWidth();
        }

        // tell scroll list to layout columns again
        // do immediate update to get proper feedback to resize handle
        // which needs to know how far the resize actually went
        const bool force_update = true;
        mColumn->mParentCtrl->updateColumns(force_update);
    }
}

void LLScrollColumnHeader::setHasResizableElement(bool resizable)
{
    if (mHasResizableElement != resizable)
    {
        mColumn->mParentCtrl->dirtyColumns();
        mHasResizableElement = resizable;
    }
}

void LLScrollColumnHeader::updateResizeBars()
{
    S32 num_resizable_columns = 0;
    S32 col;
    for (col = 0; col < mColumn->mParentCtrl->getNumColumns(); col++)
    {
        LLScrollListColumn* columnp = mColumn->mParentCtrl->getColumn(col);
        if (columnp && columnp->mHeader && columnp->mHeader->canResize())
        {
            num_resizable_columns++;
        }
    }

    S32 num_resizers_enabled = 0;

    // now enable/disable resize handles on resizable columns if we have at least two
    for (col = 0; col < mColumn->mParentCtrl->getNumColumns(); col++)
    {
        LLScrollListColumn* columnp = mColumn->mParentCtrl->getColumn(col);
        if (!columnp || !columnp->mHeader) continue;
        bool enable = num_resizable_columns >= 2 && num_resizers_enabled < (num_resizable_columns - 1) && columnp->mHeader->canResize();
        columnp->mHeader->enableResizeBar(enable);
        if (enable)
        {
            num_resizers_enabled++;
        }
    }
}

void LLScrollColumnHeader::enableResizeBar(bool enable)
{
    mResizeBar->setEnabled(enable);
}

bool LLScrollColumnHeader::canResize()
{
    return getVisible() && (mHasResizableElement || mColumn->mDynamicWidth);
}

void LLScrollListColumn::SortNames::declareValues()
{
    declare("ascending", LLScrollListColumn::ASCENDING);
    declare("descending", LLScrollListColumn::DESCENDING);
}

//
// LLScrollListColumn
//
//static
const LLScrollListColumn::Params& LLScrollListColumn::getDefaultParams()
{
    return LLUICtrlFactory::getDefaultParams<LLScrollListColumn>();
}


LLScrollListColumn::LLScrollListColumn(const Params& p, LLScrollListCtrl* parent)
:   mWidth(0),
    mIndex (-1),
    mParentCtrl(parent),
    mName(p.name),
    mLabel(p.header.label),
    mHeader(NULL),
    mMaxContentWidth(0),
    mDynamicWidth(p.width.dynamic_width),
    mRelWidth(p.width.relative_width),
    mFontAlignment(p.halign),
    mSortingColumn(p.sort_column)
{
    if (p.sort_ascending.isProvided())
    {
        mSortDirection = p.sort_ascending() ? ASCENDING : DESCENDING;
    }
    else
    {
        mSortDirection = p.sort_direction;
    }

    setWidth(p.width.pixel_width);
}

void LLScrollListColumn::setWidth(S32 width)
{
    if (!mDynamicWidth && mRelWidth <= 0.f)
    {
        mParentCtrl->updateStaticColumnWidth(this, width);
    }
    mWidth = width;
}