1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt3Support module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef Q3LISTBOX_H
43 #define Q3LISTBOX_H
44 
45 #include <Qt3Support/q3scrollview.h>
46 #include <QtGui/qpixmap.h>
47 
48 QT_BEGIN_HEADER
49 
50 QT_BEGIN_NAMESPACE
51 
52 QT_MODULE(Qt3SupportLight)
53 
54 #ifndef QT_NO_LISTBOX
55 
56 class Q3ListBoxPrivate;
57 class Q3ListBoxItem;
58 class QString;
59 class QStringList;
60 
61 class Q_COMPAT_EXPORT Q3ListBox : public Q3ScrollView
62 {
63     friend class Q3ListBoxItem;
64     friend class Q3ListBoxPrivate;
65 
66     Q_OBJECT
67     Q_ENUMS(SelectionMode LayoutMode)
68     Q_PROPERTY(uint count READ count)
69     Q_PROPERTY(int numItemsVisible READ numItemsVisible)
70     Q_PROPERTY(int currentItem READ currentItem WRITE setCurrentItem USER true)
71     Q_PROPERTY(QString currentText READ currentText)
72     Q_PROPERTY(int topItem READ topItem WRITE setTopItem DESIGNABLE false)
73     Q_PROPERTY(SelectionMode selectionMode READ selectionMode WRITE setSelectionMode)
74     Q_PROPERTY(bool multiSelection READ isMultiSelection WRITE setMultiSelection DESIGNABLE false)
75     Q_PROPERTY(LayoutMode columnMode READ columnMode WRITE setColumnMode)
76     Q_PROPERTY(LayoutMode rowMode READ rowMode WRITE setRowMode)
77     Q_PROPERTY(int numColumns READ numColumns)
78     Q_PROPERTY(int numRows READ numRows)
79     Q_PROPERTY(bool variableWidth READ variableWidth WRITE setVariableWidth)
80     Q_PROPERTY(bool variableHeight READ variableHeight WRITE setVariableHeight)
81 
82 public:
83     Q3ListBox(QWidget* parent=0, const char* name=0, Qt::WindowFlags f=0 );
84     ~Q3ListBox();
85 
86     uint count() const;
87 
88     void insertStringList(const QStringList&, int index=-1);
89 // ### fix before Qt 4.0
90 #if 0
91     void insertStrList(const QStrList *, int index=-1);
92     void insertStrList(const QStrList &, int index=-1);
93 #endif
94     void insertStrList(const char **,
95                         int numStrings=-1, int index=-1);
96 
97     void insertItem(const Q3ListBoxItem *, int index=-1);
98     void insertItem(const Q3ListBoxItem *, const Q3ListBoxItem *after);
99     void insertItem(const QString &text, int index=-1);
100     void insertItem(const QPixmap &pixmap, int index=-1);
101     void insertItem(const QPixmap &pixmap, const QString &text, int index=-1);
102 
103     void removeItem(int index);
104 
105     QString text(int index)        const;
106     const QPixmap *pixmap(int index)        const;
107 
108     void changeItem(const Q3ListBoxItem *, int index);
109     void changeItem(const QString &text, int index);
110     void changeItem(const QPixmap &pixmap, int index);
111     void changeItem(const QPixmap &pixmap, const QString &text, int index);
112 
113     void takeItem(const Q3ListBoxItem *);
114 
115     int numItemsVisible() const;
116 
117     int currentItem() const;
currentText()118     QString currentText() const { return text(currentItem()); }
119     virtual void setCurrentItem(int index);
120     virtual void setCurrentItem(Q3ListBoxItem *);
centerCurrentItem()121     void centerCurrentItem() { ensureCurrentVisible(); }
122     int topItem() const;
123     virtual void setTopItem(int index);
124     virtual void setBottomItem(int index);
125 
126     long maxItemWidth() const;
127 
128     enum SelectionMode { Single, Multi, Extended, NoSelection };
129     virtual void setSelectionMode(SelectionMode);
130     SelectionMode selectionMode() const;
131 
132     void setMultiSelection(bool multi);
133     bool isMultiSelection() const;
134 
135     QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
136 
137     virtual void setSelected(Q3ListBoxItem *, bool);
138     void setSelected(int, bool);
139     bool isSelected(int) const;
140     bool isSelected(const Q3ListBoxItem *) const;
141     Q3ListBoxItem* selectedItem() const;
142 
143     QSize sizeHint() const;
144     QSize        minimumSizeHint() const;
145 
146     Q3ListBoxItem *item(int index) const;
147     int index(const Q3ListBoxItem *) const;
148 
149     enum StringComparisonMode {
150         CaseSensitive   = 0x00001, // 0 0001
151         BeginsWith      = 0x00002, // 0 0010
152         EndsWith        = 0x00004, // 0 0100
153         Contains        = 0x00008, // 0 1000
154         ExactMatch      = 0x00010  // 1 0000
155     };
156     typedef uint ComparisonFlags;
157     Q3ListBoxItem *findItem(const QString &text, ComparisonFlags compare = BeginsWith) const;
158 
159     void triggerUpdate(bool doLayout);
160 
161     bool itemVisible(int index);
162     bool itemVisible(const Q3ListBoxItem *);
163 
164     enum LayoutMode { FixedNumber,
165                       FitToWidth, FitToHeight = FitToWidth,
166                       Variable };
167     virtual void setColumnMode(LayoutMode);
168     virtual void setColumnMode(int);
169     virtual void setRowMode(LayoutMode);
170     virtual void setRowMode(int);
171 
172     LayoutMode columnMode() const;
173     LayoutMode rowMode() const;
174 
175     int numColumns() const;
176     int numRows() const;
177 
178     bool variableWidth() const;
179     virtual void setVariableWidth(bool);
180 
181     bool variableHeight() const;
182     virtual void setVariableHeight(bool);
183 
184     void viewportPaintEvent(QPaintEvent *);
185 
dragSelect()186     bool dragSelect() const { return true; }
setDragSelect(bool)187     void setDragSelect(bool) {}
autoScroll()188     bool autoScroll() const { return true; }
setAutoScroll(bool)189     void setAutoScroll(bool) {}
autoScrollBar()190     bool autoScrollBar() const { return vScrollBarMode() == Auto; }
setAutoScrollBar(bool enable)191     void setAutoScrollBar(bool enable) { setVScrollBarMode(enable ? Auto : AlwaysOff); }
scrollBar()192     bool scrollBar() const { return vScrollBarMode() != AlwaysOff; }
setScrollBar(bool enable)193     void setScrollBar(bool enable) { setVScrollBarMode(enable ? AlwaysOn : AlwaysOff); }
autoBottomScrollBar()194     bool autoBottomScrollBar() const { return hScrollBarMode() == Auto; }
setAutoBottomScrollBar(bool enable)195     void setAutoBottomScrollBar(bool enable) { setHScrollBarMode(enable ? Auto : AlwaysOff); }
bottomScrollBar()196     bool bottomScrollBar() const { return hScrollBarMode() != AlwaysOff; }
setBottomScrollBar(bool enable)197     void setBottomScrollBar(bool enable) { setHScrollBarMode(enable ? AlwaysOn : AlwaysOff); }
smoothScrolling()198     bool smoothScrolling() const { return false; }
setSmoothScrolling(bool)199     void setSmoothScrolling(bool) {}
autoUpdate()200     bool autoUpdate() const { return true; }
setAutoUpdate(bool)201     void setAutoUpdate(bool) {}
setFixedVisibleLines(int lines)202     void setFixedVisibleLines(int lines) { setRowMode(lines); }
203     int inSort(const Q3ListBoxItem *);
204     int inSort(const QString& text);
cellHeight(int i)205     int cellHeight(int i) const { return itemHeight(i); }
cellHeight()206     int cellHeight() const { return itemHeight(); }
cellWidth()207     int cellWidth() const { return maxItemWidth(); }
cellWidth(int i)208     int cellWidth(int i) const { Q_ASSERT(i==0); Q_UNUSED(i) return maxItemWidth(); }
numCols()209     int numCols() const { return numColumns(); }
210 
211     int itemHeight(int index = 0) const;
212     Q3ListBoxItem * itemAt(const QPoint &) const;
213 
214     QRect itemRect(Q3ListBoxItem *item) const;
215 
216     Q3ListBoxItem *firstItem() const;
217 
218     void sort(bool ascending = true);
219 
220 public Q_SLOTS:
221     void clear();
222     virtual void ensureCurrentVisible();
223     virtual void clearSelection();
224     virtual void selectAll(bool select);
225     virtual void invertSelection();
226 
227 Q_SIGNALS:
228     void highlighted(int index);
229     void selected(int index);
230     void highlighted(const QString &);
231     void selected(const QString &);
232     void highlighted(Q3ListBoxItem *);
233     void selected(Q3ListBoxItem *);
234 
235     void selectionChanged();
236     void selectionChanged(Q3ListBoxItem *);
237     void currentChanged(Q3ListBoxItem *);
238     void clicked(Q3ListBoxItem *);
239     void clicked(Q3ListBoxItem *, const QPoint &);
240     void pressed(Q3ListBoxItem *);
241     void pressed(Q3ListBoxItem *, const QPoint &);
242 
243     void doubleClicked(Q3ListBoxItem *);
244     void returnPressed(Q3ListBoxItem *);
245     void rightButtonClicked(Q3ListBoxItem *, const QPoint &);
246     void rightButtonPressed(Q3ListBoxItem *, const QPoint &);
247     void mouseButtonPressed(int, Q3ListBoxItem*, const QPoint&);
248     void mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&);
249 
250     void contextMenuRequested(Q3ListBoxItem *, const QPoint &);
251 
252     void onItem(Q3ListBoxItem *item);
253     void onViewport();
254 
255 protected:
256     void changeEvent(QEvent *);
257     void mousePressEvent(QMouseEvent *);
258     void mouseReleaseEvent(QMouseEvent *);
259     void mouseDoubleClickEvent(QMouseEvent *);
260     void mouseMoveEvent(QMouseEvent *);
261     void contentsContextMenuEvent(QContextMenuEvent *);
262 
263     void keyPressEvent(QKeyEvent *e);
264     void focusInEvent(QFocusEvent *e);
265     void focusOutEvent(QFocusEvent *e);
266     void resizeEvent(QResizeEvent *);
267     void showEvent(QShowEvent *);
268 
269     bool eventFilter(QObject *o, QEvent *e);
270 
271     void updateItem(int index);
272     void updateItem(Q3ListBoxItem *);
273 
updateCellWidth()274     void updateCellWidth() { }
totalWidth()275     int totalWidth() const { return contentsWidth(); }
totalHeight()276     int totalHeight() const { return contentsHeight(); }
277 
278     virtual void paintCell(QPainter *, int row, int col);
279 
280     void toggleCurrentItem();
281     bool isRubberSelecting() const;
282 
283     void doLayout() const;
284 
findItem(int yPos)285     int findItem(int yPos) const { return index(itemAt(QPoint(0,yPos))); }
286 
287 protected Q_SLOTS:
288     void clearInputString();
289 
290 private Q_SLOTS:
291     void refreshSlot();
292     void doAutoScroll();
293     void adjustItems();
294 
295 private:
296     Q_DISABLE_COPY(Q3ListBox)
297 
298     void mousePressEventEx(QMouseEvent *);
299     void tryGeometry(int, int) const;
300     int currentRow() const;
301     int currentColumn() const;
302     void updateSelection();
303     void repaintSelection();
304     void drawRubber();
305     void doRubberSelection(const QRect &old, const QRect &rubber);
306     void handleItemChange(Q3ListBoxItem *old, bool shift, bool control);
307     void selectRange(Q3ListBoxItem *from, Q3ListBoxItem *to, bool invert, bool includeFirst, bool clearSel = false);
308 
309     void emitChangedSignal(bool);
310 
311     int columnAt(int) const;
312     int rowAt(int) const;
313 
314     Q3ListBoxPrivate * d;
315 
316     static Q3ListBox * changedListBox;
317 };
318 
319 
320 class Q_COMPAT_EXPORT Q3ListBoxItem
321 {
322 public:
323     Q3ListBoxItem(Q3ListBox* listbox = 0);
324     Q3ListBoxItem(Q3ListBox* listbox, Q3ListBoxItem *after);
325     virtual ~Q3ListBoxItem();
326 
327     virtual QString text() const;
328     virtual const QPixmap *pixmap() const;
329 
330     virtual int         height(const Q3ListBox *) const;
331     virtual int         width(const Q3ListBox *)  const;
332 
isSelected()333     bool isSelected() const { return s; }
334     bool isCurrent() const;
335 
selected()336     bool selected() const { return isSelected(); }
current()337     bool current() const { return isCurrent(); }
338 
339     Q3ListBox *listBox() const;
340 
setSelectable(bool b)341     void setSelectable(bool b) { selectable = b; }
isSelectable()342     bool isSelectable() const { return selectable; }
343 
344     Q3ListBoxItem *next() const;
345     Q3ListBoxItem *prev() const;
346 
347     virtual int rtti() const;
348     enum { RTTI = 0 };
349 
350 protected:
351     virtual void paint(QPainter *) = 0;
setText(const QString & text)352     virtual void setText(const QString &text) { txt = text; }
353     void setCustomHighlighting(bool);
354 
355 private:
356     Q_DISABLE_COPY(Q3ListBoxItem)
357 
358     QString txt;
359     uint selectable : 1;
360     uint s : 1;
361     uint dirty:1;
362     uint custom_highlight : 1;
363     uint unused : 28;
364     Q3ListBoxItem * p, * n;
365     Q3ListBox* lbox;
366     friend class Q3ListBox;
367     friend class Q3ListBoxPrivate;
368     friend class Q3ComboBox;
369     friend class Q3ComboBoxPopupItem;
370 };
371 
372 
373 class Q_COMPAT_EXPORT Q3ListBoxText : public Q3ListBoxItem
374 {
375 public:
376     Q3ListBoxText(Q3ListBox* listbox, const QString & text=QString());
377     Q3ListBoxText(const QString & text=QString());
378     Q3ListBoxText(Q3ListBox* listbox, const QString & text, Q3ListBoxItem *after);
379    ~Q3ListBoxText();
380 
381     int         height(const Q3ListBox *) const;
382     int         width(const Q3ListBox *)  const;
383 
384     int rtti() const;
385     enum { RTTI = 1 };
386 
387 protected:
388     void  paint(QPainter *);
389 
390 private:
391     Q_DISABLE_COPY(Q3ListBoxText)
392 };
393 
394 
395 class Q_COMPAT_EXPORT Q3ListBoxPixmap : public Q3ListBoxItem
396 {
397 public:
398     Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap &);
399     Q3ListBoxPixmap(const QPixmap &);
400     Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap & pix, Q3ListBoxItem *after);
401     Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap &, const QString&);
402     Q3ListBoxPixmap(const QPixmap &, const QString&);
403     Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap & pix, const QString&, Q3ListBoxItem *after);
404    ~Q3ListBoxPixmap();
405 
pixmap()406     const QPixmap *pixmap() const { return &pm; }
407 
408     int         height(const Q3ListBox *) const;
409     int         width(const Q3ListBox *)  const;
410 
411     int rtti() const;
412     enum { RTTI = 2 };
413 
414 protected:
415     void paint(QPainter *);
416 
417 private:
418     Q_DISABLE_COPY(Q3ListBoxPixmap)
419 
420     QPixmap pm;
421 };
422 
423 #endif // QT_NO_LISTBOX
424 
425 QT_END_NAMESPACE
426 
427 QT_END_HEADER
428 
429 #endif // Q3LISTBOX_H
430