blob: 408828ad46964f71fc3010d5cc45f140d072ab39 [file] [log] [blame]
rwlbuiscdba55a2006-08-18 22:47:37 +00001/*
2 * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
3 * (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <QScrollBar>
30#include <QListWidget>
31
rwlbuis98294332006-08-20 22:27:28 +000032#include "Font.h"
rwlbuiscdba55a2006-08-18 22:47:37 +000033#include "IntSize.h"
rwlbuiscdba55a2006-08-18 22:47:37 +000034#include "ListBox.h"
35
36namespace WebCore {
37
38ListBox::ListBox()
39 : ScrollView()
40 , m_listWidget(0)
41 , m_selectionMode(Single)
42 , _width(0.0)
43 , _widthGood(false)
44{
45 qDebug("ListBox::ListBox(), this=%p", this);
46}
47
48ListBox::~ListBox()
49{
50 qDebug("ListBox::~ListBox(), this=%p", this);
51}
52
53void ListBox::setParentWidget(QWidget* parent)
54{
55 qDebug("ListBox::setParentWidget(), parent=%p", parent);
56 ScrollView::setParentWidget(parent);
57
58 Q_ASSERT(m_listWidget == 0);
59 m_listWidget = new QListWidget(parent);
60
61 if(m_selectionMode == Single)
62 m_listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
63 else
64 m_listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
65
66 setQWidget(m_listWidget);
67}
68
69IntSize ListBox::sizeForNumberOfLines(int numLines) const
70{
71 QSize size(0, 0);
72
73 for (int i = 0; i <= numLines; i++) {
74 QListWidgetItem *item = m_listWidget->item(i);
75 Q_ASSERT(item != 0);
76
77 QRect visual = m_listWidget->visualItemRect(item);
78
79 size.setWidth(qMax(size.width(), visual.width()));
80 size.setHeight(qMax(size.height(), visual.height()));
81 }
82
83 // Logic taken from our khtml :-)
84 size.setWidth(size.width() + 2 * m_listWidget->frameWidth() + m_listWidget->verticalScrollBar()->sizeHint().width());
85 size.setHeight(numLines * size.height() + 2 * m_listWidget->frameWidth());
86
87 return size;
88}
89
90void ListBox::setSelectionMode(SelectionMode mode)
91{
92 // We can not directly assign to m_listWidget here, as
93 // setSelectionMode() is called from DeprecatedRenderSelect before
94 // setWidget() and our setParentWidget() functions have been called...
95 m_selectionMode = mode;
96}
97
98void ListBox::clear()
99{
100 m_listWidget->clear();
101}
102
103void ListBox::doneAppendingItems()
104{
105 // no-op
106}
107
108void ListBox::setSelected(int row, bool selected)
109{
110 QListWidgetItem *item = m_listWidget->item(row);
111 if (!item)
112 return;
113
114 m_listWidget->setItemSelected(item, selected);
115}
116
117bool ListBox::isSelected(int row) const
118{
119 QListWidgetItem *item = m_listWidget->item(row);
120 if (!item)
121 return false;
122
123 return m_listWidget->isItemSelected(item);
124}
125
126void ListBox::setEnabled(bool value)
127{
128 m_listWidget->setEnabled(value);
129}
130
131bool ListBox::isEnabled()
132{
133 return m_listWidget->isEnabled();
134}
135
136void ListBox::setWritingDirection(TextDirection dir)
137{
138 Qt::LayoutDirection qDir;
139
140 switch(dir)
141 {
142 case LTR:
143 qDir = Qt::LeftToRight;
144 break;
145 case RTL:
146 qDir = Qt::RightToLeft;
147 break;
148 }
149
150 m_listWidget->setLayoutDirection(qDir);
151}
152
153Widget::FocusPolicy ListBox::focusPolicy() const
154{
155 return Widget::focusPolicy();
156}
157
158bool ListBox::checksDescendantsForFocus() const
159{
160 return true;
161}
162
163void ListBox::clearCachedTextRenderers()
164{
165 // no-op
166}
167
168void ListBox::setFont(const Font& font)
169{
rwlbuis98294332006-08-20 22:27:28 +0000170 m_listWidget->setFont(font);
rwlbuiscdba55a2006-08-18 22:47:37 +0000171}
172
173void ListBox::appendItem(const DeprecatedString& string, ListBoxItemType type, bool enabled)
174{
175 // FIXME: take into account type/enabled...
176 Q_ASSERT(m_listWidget != 0);
rwlbuis98294332006-08-20 22:27:28 +0000177 (void) new QListWidgetItem(string, m_listWidget); // No this does not leak...
rwlbuiscdba55a2006-08-18 22:47:37 +0000178}
179
180};
181
182// vim: ts=4 sw=4 et