1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Base Browser widget class for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
|
2002-07-18 19:43:48 +04:00
|
|
|
#define DISPLAY_SEARCH_BOTH_WAYS_AT_ONCE
|
|
|
|
|
2001-08-03 19:48:20 +04:00
|
|
|
#include <stdio.h>
|
1998-10-20 00:46:58 +04:00
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Widget.H>
|
|
|
|
#include <FL/Fl_Browser_.H>
|
|
|
|
#include <FL/fl_draw.H>
|
1998-10-06 22:21:25 +04:00
|
|
|
|
1999-05-01 19:08:22 +04:00
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
// This is the base class for browsers. To be useful it must be
|
|
|
|
// subclassed and several virtual functions defined. The
|
2009-03-26 02:09:04 +03:00
|
|
|
// Forms-compatible browser and the file chooser's browser are
|
1998-10-06 22:21:25 +04:00
|
|
|
// subclassed off of this.
|
|
|
|
|
|
|
|
// Yes, I know this should be a template...
|
|
|
|
|
|
|
|
// This has been designed so that the subclass has complete control
|
|
|
|
// over the storage of the data, although because next() and prev()
|
|
|
|
// functions are used to index, it works best as a linked list or as a
|
|
|
|
// large block of characters in which the line breaks must be searched
|
|
|
|
// for.
|
|
|
|
|
|
|
|
// A great deal of work has been done so that the "height" of a data
|
|
|
|
// object does not need to be determined until it is drawn. This was
|
|
|
|
// done for the file chooser, because the height requires doing stat()
|
|
|
|
// to see if the file is a directory, which can be annoyingly slow
|
|
|
|
// over the network.
|
|
|
|
|
|
|
|
/* redraw bits:
|
|
|
|
1 = redraw children (the scrollbar)
|
|
|
|
2 = redraw one or two items
|
|
|
|
4 = redraw all items
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void scrollbar_callback(Fl_Widget* s, void*) {
|
|
|
|
((Fl_Browser_*)(s->parent()))->position(int(((Fl_Scrollbar*)s)->value()));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hscrollbar_callback(Fl_Widget* s, void*) {
|
|
|
|
((Fl_Browser_*)(s->parent()))->hposition(int(((Fl_Scrollbar*)s)->value()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// return where to draw the actual box:
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
Returns the bounding box for the interior of the list's display window, inside
|
2008-09-14 16:45:42 +04:00
|
|
|
the scrollbars.
|
2009-03-26 02:09:04 +03:00
|
|
|
\param[out] X,Y,W,H The returned bounding box.\n
|
|
|
|
(The original contents of these parameters are overwritten)
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Browser_::bbox(int& X, int& Y, int& W, int& H) const {
|
2009-04-02 10:44:34 +04:00
|
|
|
int scrollsize = scrollbar_size_ ? scrollbar_size_ : Fl::scrollbar_size();
|
1998-12-02 18:39:39 +03:00
|
|
|
Fl_Boxtype b = box() ? box() : FL_DOWN_BOX;
|
1998-10-06 22:21:25 +04:00
|
|
|
X = x()+Fl::box_dx(b);
|
|
|
|
Y = y()+Fl::box_dy(b);
|
|
|
|
W = w()-Fl::box_dw(b);
|
|
|
|
H = h()-Fl::box_dh(b);
|
|
|
|
if (scrollbar.visible()) {
|
2009-04-02 10:44:34 +04:00
|
|
|
W -= scrollsize;
|
|
|
|
if (scrollbar.align() & FL_ALIGN_LEFT) X += scrollsize;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
1999-11-07 11:33:36 +03:00
|
|
|
if (W < 0) W = 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
if (hscrollbar.visible()) {
|
2009-04-02 10:44:34 +04:00
|
|
|
H -= scrollsize;
|
|
|
|
if (scrollbar.align() & FL_ALIGN_TOP) Y += scrollsize;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
1999-11-07 11:33:36 +03:00
|
|
|
if (H < 0) H = 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
|
|
|
This method returns the X position of the left edge of the list area
|
|
|
|
after adjusting for the scrollbar and border, if any.
|
2009-03-26 02:09:04 +03:00
|
|
|
\returns The X position of the left edge of the list, in pixels.
|
|
|
|
\see Fl_Browser_::bbox()
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Browser_::leftedge() const {
|
|
|
|
int X, Y, W, H; bbox(X, Y, W, H);
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
1999-12-19 08:32:34 +03:00
|
|
|
// The scrollbars may be moved again by draw(), since each one's size
|
1998-10-06 22:21:25 +04:00
|
|
|
// depends on whether the other is visible or not. This skips over
|
|
|
|
// Fl_Group::resize since it moves the scrollbars uselessly.
|
2009-03-26 02:09:04 +03:00
|
|
|
/**
|
|
|
|
Repositions and/or resizes the browser.
|
|
|
|
\param[in] X,Y,W,H The new position and size for the browser, in pixels.
|
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Browser_::resize(int X, int Y, int W, int H) {
|
2009-04-02 10:44:34 +04:00
|
|
|
int scrollsize = scrollbar_size_ ? scrollbar_size_ : Fl::scrollbar_size();
|
1998-10-06 22:21:25 +04:00
|
|
|
Fl_Widget::resize(X, Y, W, H);
|
1999-12-19 08:32:34 +03:00
|
|
|
// move the scrollbars so they can respond to events:
|
|
|
|
bbox(X,Y,W,H);
|
|
|
|
scrollbar.resize(
|
2009-04-02 10:44:34 +04:00
|
|
|
scrollbar.align()&FL_ALIGN_LEFT ? X-scrollsize : X+W,
|
|
|
|
Y, scrollsize, H);
|
1999-12-19 08:32:34 +03:00
|
|
|
hscrollbar.resize(
|
2009-04-02 10:44:34 +04:00
|
|
|
X, scrollbar.align()&FL_ALIGN_TOP ? Y-scrollsize : Y+H,
|
|
|
|
W, scrollsize);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cause minimal update to redraw the given item:
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
This method should be called when the contents of \p item has changed,
|
|
|
|
but not its height.
|
|
|
|
\param[in] item The item that needs to be redrawn.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see redraw_lines(), redraw_line()
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void Fl_Browser_::redraw_line(void* item) {
|
|
|
|
if (!redraw1 || redraw1 == item) {redraw1 = item; damage(FL_DAMAGE_EXPOSE);}
|
|
|
|
else if (!redraw2 || redraw2 == item) {redraw2 = item; damage(FL_DAMAGE_EXPOSE);}
|
1998-10-19 21:53:09 +04:00
|
|
|
else damage(FL_DAMAGE_SCROLL);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out top() based on position():
|
|
|
|
void Fl_Browser_::update_top() {
|
|
|
|
if (!top_) top_ = item_first();
|
|
|
|
if (position_ != real_position_) {
|
|
|
|
void* l;
|
|
|
|
int ly;
|
2002-08-09 05:09:49 +04:00
|
|
|
int yy = position_;
|
1998-10-06 22:21:25 +04:00
|
|
|
// start from either head or current position, whichever is closer:
|
2002-08-09 05:09:49 +04:00
|
|
|
if (!top_ || yy <= (real_position_/2)) {
|
1998-10-06 22:21:25 +04:00
|
|
|
l = item_first();
|
|
|
|
ly = 0;
|
|
|
|
} else {
|
|
|
|
l = top_;
|
|
|
|
ly = real_position_-offset_;
|
|
|
|
}
|
|
|
|
if (!l) {
|
|
|
|
top_ = 0;
|
|
|
|
offset_ = 0;
|
|
|
|
real_position_ = 0;
|
|
|
|
} else {
|
2002-08-09 05:09:49 +04:00
|
|
|
int hh = item_quick_height(l);
|
1998-10-06 22:21:25 +04:00
|
|
|
// step through list until we find line containing this point:
|
2002-08-09 05:09:49 +04:00
|
|
|
while (ly > yy) {
|
1998-10-06 22:21:25 +04:00
|
|
|
void* l1 = item_prev(l);
|
|
|
|
if (!l1) {ly = 0; break;} // hit the top
|
2002-08-09 05:09:49 +04:00
|
|
|
l = l1;
|
|
|
|
hh = item_quick_height(l);
|
|
|
|
ly -= hh;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2002-08-09 05:09:49 +04:00
|
|
|
while ((ly+hh) <= yy) {
|
1998-10-06 22:21:25 +04:00
|
|
|
void* l1 = item_next(l);
|
2002-08-09 05:09:49 +04:00
|
|
|
if (!l1) {yy = ly+hh-1; break;}
|
1998-10-06 22:21:25 +04:00
|
|
|
l = l1;
|
2002-08-09 05:09:49 +04:00
|
|
|
ly += hh;
|
|
|
|
hh = item_quick_height(l);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
// top item must *really* be visible, use slow height:
|
|
|
|
for (;;) {
|
2002-08-09 05:09:49 +04:00
|
|
|
hh = item_height(l);
|
|
|
|
if ((ly+hh) > yy) break; // it is big enough to see
|
1998-10-06 22:21:25 +04:00
|
|
|
// go up to top of previous item:
|
|
|
|
void* l1 = item_prev(l);
|
2002-08-09 05:09:49 +04:00
|
|
|
if (!l1) {ly = yy = 0; break;} // hit the top
|
|
|
|
l = l1; yy = position_ = ly = ly-item_quick_height(l);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
// use it:
|
|
|
|
top_ = l;
|
2002-08-09 05:09:49 +04:00
|
|
|
offset_ = yy-ly;
|
|
|
|
real_position_ = yy;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
1998-10-19 21:53:09 +04:00
|
|
|
damage(FL_DAMAGE_SCROLL);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change position(), top() will update when update_top() is called
|
|
|
|
// (probably by draw() or handle()):
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
Sets the vertical scroll position of the list to pixel position \p pos.
|
|
|
|
The position is how many pixels of the list are scrolled off the top edge
|
|
|
|
of the screen. Example: A position of '3' scrolls the top three pixels of
|
|
|
|
the list off the top edge of the screen.
|
|
|
|
\param[in] pos The vertical position (in pixels) to scroll the browser to.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see position(), hposition()
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void Fl_Browser_::position(int pos) {
|
|
|
|
if (pos < 0) pos = 0;
|
|
|
|
if (pos == position_) return;
|
|
|
|
position_ = pos;
|
|
|
|
if (pos != real_position_) redraw_lines();
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
Sets the horizontal scroll position of the list to pixel position \p pos.
|
|
|
|
The position is how many pixels of the list are scrolled off the left edge
|
|
|
|
of the screen. Example: A position of '18' scrolls the left 18 pixels of the list
|
|
|
|
off the left edge of the screen.
|
|
|
|
\param[in] pos The horizontal position (in pixels) to scroll the browser to.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see position(), hposition()
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void Fl_Browser_::hposition(int pos) {
|
|
|
|
if (pos < 0) pos = 0;
|
|
|
|
if (pos == hposition_) return;
|
|
|
|
hposition_ = pos;
|
|
|
|
if (pos != real_hposition_) redraw_lines();
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tell whether item is currently displayed:
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
Returns non-zero if \p item has been scrolled to a position where it is being displayed.
|
|
|
|
Checks to see if the item's vertical position is within the top and bottom
|
|
|
|
edges of the display window. This does NOT take into account the hide()/show()
|
|
|
|
status of the widget or item.
|
|
|
|
\param[in] item The item to check
|
|
|
|
\returns 1 if visible, 0 if not visible.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see display(), displayed()
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
int Fl_Browser_::displayed(void* item) const {
|
1998-10-06 22:21:25 +04:00
|
|
|
int X, Y, W, H; bbox(X, Y, W, H);
|
|
|
|
int yy = H+offset_;
|
|
|
|
for (void* l = top_; l && yy > 0; l = item_next(l)) {
|
2009-03-26 02:09:04 +03:00
|
|
|
if (l == item) return 1;
|
1998-10-06 22:21:25 +04:00
|
|
|
yy -= item_height(l);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-18 19:43:48 +04:00
|
|
|
// Ensure this item is displayed:
|
1998-10-06 22:21:25 +04:00
|
|
|
// Messy because we have no idea if it is before top or after bottom:
|
2009-03-26 02:09:04 +03:00
|
|
|
/**
|
|
|
|
Displays the \p item, scrolling the list as necessary.
|
|
|
|
\param[in] item The item to be displayed.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see display(), displayed()
|
2009-03-26 02:09:04 +03:00
|
|
|
*/
|
|
|
|
void Fl_Browser_::display(void* item) {
|
2002-07-18 19:43:48 +04:00
|
|
|
|
|
|
|
// First special case - want to display first item in the list?
|
1998-12-02 18:48:43 +03:00
|
|
|
update_top();
|
2009-03-26 02:09:04 +03:00
|
|
|
if (item == item_first()) {position(0); return;}
|
2002-07-18 19:43:48 +04:00
|
|
|
|
|
|
|
int X, Y, W, H, Yp; bbox(X, Y, W, H);
|
1998-10-06 22:21:25 +04:00
|
|
|
void* l = top_;
|
2002-07-18 19:43:48 +04:00
|
|
|
Y = Yp = -offset_;
|
|
|
|
int h1;
|
|
|
|
|
|
|
|
// 2nd special case - want to display item already displayed at top of browser?
|
2009-03-26 02:09:04 +03:00
|
|
|
if (l == item) {position(real_position_+Y); return;} // scroll up a bit
|
2002-07-18 19:43:48 +04:00
|
|
|
|
|
|
|
// 3rd special case - want to display item just above top of browser?
|
1998-10-06 22:21:25 +04:00
|
|
|
void* lp = item_prev(l);
|
2009-03-26 02:09:04 +03:00
|
|
|
if (lp == item) {position(real_position_+Y-item_quick_height(lp)); return;}
|
2002-07-18 19:43:48 +04:00
|
|
|
|
|
|
|
#ifdef DISPLAY_SEARCH_BOTH_WAYS_AT_ONCE
|
|
|
|
// search for item. We search both up and down the list at the same time,
|
|
|
|
// this evens up the execution time for the two cases - the old way was
|
|
|
|
// much slower for going up than for going down.
|
|
|
|
while (l || lp) {
|
|
|
|
if (l) {
|
|
|
|
h1 = item_quick_height(l);
|
2009-03-26 02:09:04 +03:00
|
|
|
if (l == item) {
|
2002-07-18 19:43:48 +04:00
|
|
|
if (Y <= H) { // it is visible or right at bottom
|
|
|
|
Y = Y+h1-H; // find where bottom edge is
|
|
|
|
if (Y > 0) position(real_position_+Y); // scroll down a bit
|
|
|
|
} else {
|
|
|
|
position(real_position_+Y-(H-h1)/2); // center it
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Y += h1;
|
|
|
|
l = item_next(l);
|
|
|
|
}
|
|
|
|
if (lp) {
|
|
|
|
h1 = item_quick_height(lp);
|
|
|
|
Yp -= h1;
|
2009-03-26 02:09:04 +03:00
|
|
|
if (lp == item) {
|
2002-07-18 19:43:48 +04:00
|
|
|
if ((Yp + h1) >= 0) position(real_position_+Yp);
|
|
|
|
else position(real_position_+Yp-(H-h1)/2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lp = item_prev(lp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Old version went forwards and then backwards:
|
1998-10-06 22:21:25 +04:00
|
|
|
// search forward for it:
|
2002-07-18 19:43:48 +04:00
|
|
|
l = top_;
|
1998-10-06 22:21:25 +04:00
|
|
|
for (; l; l = item_next(l)) {
|
2002-07-18 19:43:48 +04:00
|
|
|
h1 = item_quick_height(l);
|
2009-03-26 02:09:04 +03:00
|
|
|
if (l == item) {
|
1998-10-06 22:21:25 +04:00
|
|
|
if (Y <= H) { // it is visible or right at bottom
|
|
|
|
Y = Y+h1-H; // find where bottom edge is
|
|
|
|
if (Y > 0) position(real_position_+Y); // scroll down a bit
|
|
|
|
} else {
|
|
|
|
position(real_position_+Y-(H-h1)/2); // center it
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Y += h1;
|
|
|
|
}
|
|
|
|
// search backward for it, if found center it:
|
|
|
|
l = lp;
|
|
|
|
Y = -offset_;
|
|
|
|
for (; l; l = item_prev(l)) {
|
2002-07-18 19:43:48 +04:00
|
|
|
h1 = item_quick_height(l);
|
1998-10-06 22:21:25 +04:00
|
|
|
Y -= h1;
|
2009-03-26 02:09:04 +03:00
|
|
|
if (l == item) {
|
1998-10-20 00:46:58 +04:00
|
|
|
if ((Y + h1) >= 0) position(real_position_+Y);
|
1998-10-06 22:21:25 +04:00
|
|
|
else position(real_position_+Y-(H-h1)/2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2002-07-18 19:43:48 +04:00
|
|
|
#endif
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// redraw, has side effect of updating top and setting scrollbar:
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2008-09-21 01:04:28 +04:00
|
|
|
Draws the list within the normal widget bounding box.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Browser_::draw() {
|
|
|
|
int drawsquare = 0;
|
|
|
|
update_top();
|
|
|
|
int full_width_ = full_width();
|
|
|
|
int full_height_ = full_height();
|
|
|
|
int X, Y, W, H; bbox(X, Y, W, H);
|
1999-06-07 11:03:34 +04:00
|
|
|
int dont_repeat = 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
J1:
|
2005-12-31 21:26:01 +03:00
|
|
|
if (damage() & FL_DAMAGE_ALL) { // redraw the box if full redraw
|
|
|
|
Fl_Boxtype b = box() ? box() : FL_DOWN_BOX;
|
|
|
|
draw_box(b, x(), y(), w(), h(), color());
|
|
|
|
drawsquare = 1;
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
// see if scrollbar needs to be switched on/off:
|
|
|
|
if ((has_scrollbar_ & VERTICAL) && (
|
|
|
|
(has_scrollbar_ & ALWAYS_ON) || position_ || full_height_ > H)) {
|
1999-05-01 19:08:22 +04:00
|
|
|
if (!scrollbar.visible()) {
|
|
|
|
scrollbar.set_visible();
|
|
|
|
drawsquare = 1;
|
|
|
|
bbox(X, Y, W, H);
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
} else {
|
|
|
|
top_ = item_first(); real_position_ = offset_ = 0;
|
1998-12-02 18:48:43 +03:00
|
|
|
if (scrollbar.visible()) {
|
|
|
|
scrollbar.clear_visible();
|
2002-11-19 19:37:36 +03:00
|
|
|
clear_damage((uchar)(damage()|FL_DAMAGE_SCROLL));
|
1998-12-02 18:48:43 +03:00
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((has_scrollbar_ & HORIZONTAL) && (
|
|
|
|
(has_scrollbar_ & ALWAYS_ON) || hposition_ || full_width_ > W)) {
|
1999-05-01 19:08:22 +04:00
|
|
|
if (!hscrollbar.visible()) {
|
|
|
|
hscrollbar.set_visible();
|
|
|
|
drawsquare = 1;
|
|
|
|
bbox(X, Y, W, H);
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
} else {
|
|
|
|
real_hposition_ = 0;
|
1998-12-02 18:48:43 +03:00
|
|
|
if (hscrollbar.visible()) {
|
|
|
|
hscrollbar.clear_visible();
|
2002-11-19 19:37:36 +03:00
|
|
|
clear_damage((uchar)(damage()|FL_DAMAGE_SCROLL));
|
1998-12-02 18:48:43 +03:00
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
1999-09-15 19:18:12 +04:00
|
|
|
// Check the vertical scrollbar again, just in case it needs to be drawn
|
|
|
|
// because the horizontal one is drawn. There should be a cleaner way
|
|
|
|
// to do this besides copying the same code...
|
|
|
|
if ((has_scrollbar_ & VERTICAL) && (
|
|
|
|
(has_scrollbar_ & ALWAYS_ON) || position_ || full_height_ > H)) {
|
|
|
|
if (!scrollbar.visible()) {
|
|
|
|
scrollbar.set_visible();
|
|
|
|
drawsquare = 1;
|
|
|
|
bbox(X, Y, W, H);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
top_ = item_first(); real_position_ = offset_ = 0;
|
|
|
|
if (scrollbar.visible()) {
|
|
|
|
scrollbar.clear_visible();
|
2002-11-19 19:37:36 +03:00
|
|
|
clear_damage((uchar)(damage()|FL_DAMAGE_SCROLL));
|
1999-09-15 19:18:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
bbox(X, Y, W, H);
|
|
|
|
|
2008-12-06 17:59:52 +03:00
|
|
|
fl_push_clip(X, Y, W, H);
|
1998-10-06 22:21:25 +04:00
|
|
|
// for each line, draw it if full redraw or scrolled. Erase background
|
|
|
|
// if not a full redraw or if it is selected:
|
|
|
|
void* l = top();
|
|
|
|
int yy = -offset_;
|
|
|
|
for (; l && yy < H; l = item_next(l)) {
|
|
|
|
int hh = item_height(l);
|
|
|
|
if (hh <= 0) continue;
|
1998-10-19 21:53:09 +04:00
|
|
|
if ((damage()&(FL_DAMAGE_SCROLL|FL_DAMAGE_ALL)) || l == redraw1 || l == redraw2) {
|
1998-10-06 22:21:25 +04:00
|
|
|
if (item_selected(l)) {
|
2001-10-29 06:44:33 +03:00
|
|
|
fl_color(active_r() ? selection_color() : fl_inactive(selection_color()));
|
1998-10-06 22:21:25 +04:00
|
|
|
fl_rectf(X, yy+Y, W, hh);
|
1998-10-19 21:53:09 +04:00
|
|
|
} else if (!(damage()&FL_DAMAGE_ALL)) {
|
2002-08-13 19:42:44 +04:00
|
|
|
fl_push_clip(X, yy+Y, W, hh);
|
|
|
|
draw_box(box() ? box() : FL_DOWN_BOX, x(), y(), w(), h(), color());
|
|
|
|
fl_pop_clip();
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2000-03-17 12:38:20 +03:00
|
|
|
item_draw(l, X-hposition_, yy+Y, W+hposition_, hh);
|
2001-08-05 00:17:10 +04:00
|
|
|
if (l == selection_ && Fl::focus() == this) {
|
2005-04-11 02:44:48 +04:00
|
|
|
draw_box(FL_BORDER_FRAME, X, yy+Y, W, hh, color());
|
2005-04-11 04:18:48 +04:00
|
|
|
draw_focus(FL_NO_BOX, X, yy+Y, W+1, hh+1);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2002-08-09 05:09:49 +04:00
|
|
|
int ww = item_width(l);
|
|
|
|
if (ww > max_width) {max_width = ww; max_width_item = l;}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
yy += hh;
|
|
|
|
}
|
|
|
|
// erase the area below last line:
|
1998-10-19 21:53:09 +04:00
|
|
|
if (!(damage()&FL_DAMAGE_ALL) && yy < H) {
|
2002-08-16 23:51:31 +04:00
|
|
|
fl_push_clip(X, yy+Y, W, H-yy);
|
|
|
|
draw_box(box() ? box() : FL_DOWN_BOX, x(), y(), w(), h(), color());
|
|
|
|
fl_pop_clip();
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
fl_pop_clip();
|
|
|
|
redraw1 = redraw2 = 0;
|
|
|
|
|
1999-06-07 11:03:34 +04:00
|
|
|
if (!dont_repeat) {
|
|
|
|
dont_repeat = 1;
|
|
|
|
// see if changes to full_height caused by calls to slow_height
|
|
|
|
// caused scrollbar state to change, in which case we have to redraw:
|
|
|
|
full_height_ = full_height();
|
|
|
|
full_width_ = full_width();
|
|
|
|
if ((has_scrollbar_ & VERTICAL) &&
|
|
|
|
((has_scrollbar_ & ALWAYS_ON) || position_ || full_height_>H)) {
|
2005-12-31 21:26:01 +03:00
|
|
|
if (!scrollbar.visible()) { damage(FL_DAMAGE_ALL); goto J1; }
|
1999-06-07 11:03:34 +04:00
|
|
|
} else {
|
2005-12-31 21:26:01 +03:00
|
|
|
if (scrollbar.visible()) { damage(FL_DAMAGE_ALL); goto J1; }
|
1999-06-07 11:03:34 +04:00
|
|
|
}
|
|
|
|
if ((has_scrollbar_ & HORIZONTAL) &&
|
|
|
|
((has_scrollbar_ & ALWAYS_ON) || hposition_ || full_width_>W)) {
|
2005-12-31 21:26:01 +03:00
|
|
|
if (!hscrollbar.visible()) { damage(FL_DAMAGE_ALL); goto J1; }
|
1999-06-07 11:03:34 +04:00
|
|
|
} else {
|
2005-12-31 21:26:01 +03:00
|
|
|
if (hscrollbar.visible()) { damage(FL_DAMAGE_ALL); goto J1; }
|
1999-06-07 11:03:34 +04:00
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the scrollbars and redraw them:
|
2009-04-02 10:44:34 +04:00
|
|
|
int scrollsize = scrollbar_size_ ? scrollbar_size_ : Fl::scrollbar_size();
|
1998-10-06 22:21:25 +04:00
|
|
|
int dy = top_ ? item_quick_height(top_) : 0; if (dy < 10) dy = 10;
|
|
|
|
if (scrollbar.visible()) {
|
|
|
|
scrollbar.damage_resize(
|
2009-04-02 10:44:34 +04:00
|
|
|
scrollbar.align()&FL_ALIGN_LEFT ? X-scrollsize : X+W,
|
|
|
|
Y, scrollsize, H);
|
1998-10-06 22:21:25 +04:00
|
|
|
scrollbar.value(position_, H, 0, full_height_);
|
|
|
|
scrollbar.linesize(dy);
|
|
|
|
if (drawsquare) draw_child(scrollbar);
|
|
|
|
else update_child(scrollbar);
|
|
|
|
}
|
|
|
|
if (hscrollbar.visible()) {
|
|
|
|
hscrollbar.damage_resize(
|
2009-04-02 10:44:34 +04:00
|
|
|
X, scrollbar.align()&FL_ALIGN_TOP ? Y-scrollsize : Y+H,
|
|
|
|
W, scrollsize);
|
1998-10-06 22:21:25 +04:00
|
|
|
hscrollbar.value(hposition_, W, 0, full_width_);
|
|
|
|
hscrollbar.linesize(dy);
|
|
|
|
if (drawsquare) draw_child(hscrollbar);
|
|
|
|
else update_child(hscrollbar);
|
|
|
|
}
|
|
|
|
|
1998-11-08 18:05:48 +03:00
|
|
|
// draw that little square between the scrollbars:
|
1998-10-06 22:21:25 +04:00
|
|
|
if (drawsquare && scrollbar.visible() && hscrollbar.visible()) {
|
|
|
|
fl_color(parent()->color());
|
2009-04-02 10:44:34 +04:00
|
|
|
fl_rectf(scrollbar.x(), hscrollbar.y(), scrollsize, scrollsize);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
real_hposition_ = hposition_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quick way to delete and reset everything:
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
|
|
|
This method should be called when the list data is completely replaced
|
|
|
|
or cleared. It informs the Fl_Browser_ widget that any cached
|
|
|
|
information it has concerning the items is invalid.
|
2009-03-26 02:09:04 +03:00
|
|
|
This method does not clear the list, it just handles the follow up
|
|
|
|
bookkeeping after the list has been cleared.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Browser_::new_list() {
|
|
|
|
top_ = 0;
|
|
|
|
position_ = real_position_ = 0;
|
|
|
|
hposition_ = real_hposition_ = 0;
|
|
|
|
selection_ = 0;
|
|
|
|
offset_ = 0;
|
|
|
|
max_width = 0;
|
|
|
|
max_width_item = 0;
|
|
|
|
redraw_lines();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell it that this item is going away, and that this must remove
|
|
|
|
// all pointers to it:
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
This method should be used when \p item is being deleted from the list.
|
|
|
|
It allows the Fl_Browser_ to discard any cached data it has on the item.
|
|
|
|
This method does not actually delete the item, but handles the follow up
|
|
|
|
bookkeeping after the item has just been deleted.
|
|
|
|
\param[in] item The item being deleted.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void Fl_Browser_::deleting(void* item) {
|
|
|
|
if (displayed(item)) {
|
2000-06-30 08:23:13 +04:00
|
|
|
redraw_lines();
|
2009-03-26 02:09:04 +03:00
|
|
|
if (item == top_) {
|
2000-06-30 08:23:13 +04:00
|
|
|
real_position_ -= offset_;
|
|
|
|
offset_ = 0;
|
2009-03-26 02:09:04 +03:00
|
|
|
top_ = item_next(item);
|
|
|
|
if (!top_) top_ = item_prev(item);
|
2000-06-30 08:23:13 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we don't know where this item is, recalculate top...
|
|
|
|
real_position_ = 0;
|
2005-05-12 21:10:58 +04:00
|
|
|
offset_ = 0;
|
2000-06-30 08:23:13 +04:00
|
|
|
top_ = 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2009-03-26 02:09:04 +03:00
|
|
|
if (item == selection_) selection_ = 0;
|
|
|
|
if (item == max_width_item) {max_width_item = 0; max_width = 0;}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
This method should be used when item \p a is being replaced by item \p b.
|
|
|
|
It allows the Fl_Browser_ to update its cache data as needed,
|
|
|
|
schedules a redraw for the item being changed, and tries to maintain the selection.
|
|
|
|
This method does not actually replace the item, but handles the follow up
|
|
|
|
bookkeeping after the item has just been replaced.
|
|
|
|
\param[in] a Item being replaced
|
|
|
|
\param[in] b Item to replace 'a'
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Browser_::replacing(void* a, void* b) {
|
|
|
|
redraw_line(a);
|
|
|
|
if (a == selection_) selection_ = b;
|
|
|
|
if (a == top_) top_ = b;
|
|
|
|
if (a == max_width_item) {max_width_item = 0; max_width = 0;}
|
|
|
|
}
|
|
|
|
|
2008-09-21 01:04:28 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
This method should be used when two items \p a and \p b are being swapped.
|
|
|
|
It allows the Fl_Browser_ to update its cache data as needed,
|
|
|
|
schedules a redraw for the two items, and tries to maintain the current selection.
|
|
|
|
This method does not actually swap items, but handles the follow up
|
|
|
|
bookkeeping after items have been swapped.
|
|
|
|
\param[in] a,b Items being swapped.
|
2008-09-21 01:04:28 +04:00
|
|
|
*/
|
2006-03-29 03:27:20 +04:00
|
|
|
void Fl_Browser_::swapping(void* a, void* b) {
|
|
|
|
redraw_line(a);
|
|
|
|
redraw_line(b);
|
|
|
|
if (a == selection_) selection_ = b;
|
|
|
|
else if (b == selection_) selection_ = a;
|
|
|
|
if (a == top_) top_ = b;
|
|
|
|
else if (b == top_) top_ = a;
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
This method should be used when an item is in the process of
|
|
|
|
being inserted into the list.
|
|
|
|
It allows the Fl_Browser_ to update its cache data as needed,
|
|
|
|
scheduling a redraw for the affected lines.
|
|
|
|
This method does not actually insert items, but handles the
|
|
|
|
follow up bookkeeping after items have been inserted.
|
|
|
|
\param[in] a The starting item position
|
|
|
|
\param[in] b The new item being inserted
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Browser_::inserting(void* a, void* b) {
|
|
|
|
if (displayed(a)) redraw_lines();
|
|
|
|
if (a == top_) top_ = b;
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
This method returns the item under mouse y position \p ypos.
|
|
|
|
NULL is returned if no item is displayed at that position.
|
|
|
|
\param[in] ypos The y position (eg. Fl::event_y()) to find an item under.
|
|
|
|
\returns The item, or NULL if not found
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void* Fl_Browser_::find_item(int ypos) {
|
1998-10-06 22:21:25 +04:00
|
|
|
update_top();
|
|
|
|
int X, Y, W, H; bbox(X, Y, W, H);
|
|
|
|
int yy = Y-offset_;
|
2009-03-26 02:09:04 +03:00
|
|
|
for (void *l = top_; l; l = item_next(l)) {
|
1998-10-06 22:21:25 +04:00
|
|
|
int hh = item_height(l); if (hh <= 0) continue;
|
|
|
|
yy += hh;
|
2009-03-26 02:09:04 +03:00
|
|
|
if (ypos <= yy || yy>=(Y+H)) return l;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
Sets the selection state of \p item to \p val,
|
|
|
|
and returns 1 if the state changed or 0 if it did not.
|
2008-09-14 16:45:42 +04:00
|
|
|
|
2009-03-26 02:09:04 +03:00
|
|
|
If \p docallbacks is non-zero, select tries to call
|
|
|
|
the callback function for the widget.
|
|
|
|
|
|
|
|
\param[in] item The item whose selection state is to be changed
|
|
|
|
\param[in] val The new selection state (1=select, 0=de-select)
|
|
|
|
\param[in] docallbacks If 1, invokes widget callback if item changed.\n
|
|
|
|
If 0, doesn't do callback (default).
|
|
|
|
\returns 1 if state was changed, 0 if not.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
int Fl_Browser_::select(void* item, int val, int docallbacks) {
|
1998-10-06 22:21:25 +04:00
|
|
|
if (type() == FL_MULTI_BROWSER) {
|
2009-03-26 02:09:04 +03:00
|
|
|
if (selection_ != item) {
|
1998-10-06 22:21:25 +04:00
|
|
|
if (selection_) redraw_line(selection_);
|
2009-03-26 02:09:04 +03:00
|
|
|
selection_ = item;
|
|
|
|
redraw_line(item);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2009-03-26 02:09:04 +03:00
|
|
|
if ((!val)==(!item_selected(item))) return 0;
|
|
|
|
item_select(item, val);
|
|
|
|
redraw_line(item);
|
1998-10-06 22:21:25 +04:00
|
|
|
} else {
|
2009-03-26 02:09:04 +03:00
|
|
|
if (val && selection_ == item) return 0;
|
|
|
|
if (!val && selection_ != item) return 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
if (selection_) {
|
|
|
|
item_select(selection_, 0);
|
|
|
|
redraw_line(selection_);
|
|
|
|
selection_ = 0;
|
|
|
|
}
|
2009-03-26 02:09:04 +03:00
|
|
|
if (val) {
|
|
|
|
item_select(item, 1);
|
|
|
|
selection_ = item;
|
|
|
|
redraw_line(item);
|
|
|
|
display(item);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
2004-07-27 20:02:21 +04:00
|
|
|
if (docallbacks) {
|
|
|
|
set_changed();
|
|
|
|
do_callback();
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
|
|
|
Deselects all items in the list and returns 1 if the state changed
|
|
|
|
or 0 if it did not.
|
|
|
|
|
2009-03-26 02:09:04 +03:00
|
|
|
If the optional \p docallbacks parameter is non-zero, deselect tries
|
|
|
|
to call the callback function for the widget.
|
|
|
|
|
|
|
|
\param[in] docallbacks If 1, invokes widget callback if item changed.\n
|
|
|
|
If 0, doesn't do callback (default).
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Browser_::deselect(int docallbacks) {
|
|
|
|
if (type() == FL_MULTI_BROWSER) {
|
|
|
|
int change = 0;
|
|
|
|
for (void* p = item_first(); p; p = item_next(p))
|
|
|
|
change |= select(p, 0, docallbacks);
|
|
|
|
return change;
|
|
|
|
} else {
|
|
|
|
if (!selection_) return 0;
|
|
|
|
item_select(selection_, 0);
|
|
|
|
redraw_line(selection_);
|
|
|
|
selection_ = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
Selects \p item and returns 1 if the state changed or 0 if it did not.
|
|
|
|
Any other items in the list are deselected.
|
|
|
|
\param[in] item The \p item to select.
|
|
|
|
\param[in] docallbacks If 1, invokes widget callback if item changed.\n
|
|
|
|
If 0, doesn't do callback (default).
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
int Fl_Browser_::select_only(void* item, int docallbacks) {
|
|
|
|
if (!item) return deselect(docallbacks);
|
1998-10-06 22:21:25 +04:00
|
|
|
int change = 0;
|
2010-02-20 20:40:07 +03:00
|
|
|
Fl_Widget_Tracker wp(this);
|
1998-10-06 22:21:25 +04:00
|
|
|
if (type() == FL_MULTI_BROWSER) {
|
2010-02-20 20:40:07 +03:00
|
|
|
for (void* p = item_first(); p; p = item_next(p)) {
|
2009-03-26 02:09:04 +03:00
|
|
|
if (p != item) change |= select(p, 0, docallbacks);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return change;
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2009-03-26 02:09:04 +03:00
|
|
|
change |= select(item, 1, docallbacks);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return change;
|
2009-03-26 02:09:04 +03:00
|
|
|
display(item);
|
1998-10-06 22:21:25 +04:00
|
|
|
return change;
|
|
|
|
}
|
2009-03-26 02:09:04 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
Handles the \p event within the normal widget bounding box.
|
|
|
|
\param[in] event The event to process.
|
|
|
|
\returns 1 if event was processed, 0 if not.
|
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Browser_::handle(int event) {
|
2010-02-20 20:40:07 +03:00
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
// We use Fl_Widget_Tracker to test if the user has deleted
|
|
|
|
// this widget in a callback. Callbacks can be called by:
|
|
|
|
// - do_callback()
|
|
|
|
// - select()
|
|
|
|
// - select_only()
|
|
|
|
// - deselect()
|
|
|
|
// Thus we must test wp.deleted() after each of these calls,
|
|
|
|
// unless we return directly after one of these.
|
|
|
|
// If wp.deleted() is true, we return 1 because we used the event.
|
|
|
|
|
|
|
|
Fl_Widget_Tracker wp(this);
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
// must do shortcuts first or the scrollbar will get them...
|
2001-09-30 02:59:45 +04:00
|
|
|
if (event == FL_ENTER || event == FL_LEAVE) return 1;
|
2001-08-05 00:17:10 +04:00
|
|
|
if (event == FL_KEYBOARD && type() >= FL_HOLD_BROWSER) {
|
1998-10-06 22:21:25 +04:00
|
|
|
void* l1 = selection_;
|
|
|
|
void* l = l1; if (!l) l = top_; if (!l) l = item_first();
|
|
|
|
if (l) {
|
2007-10-07 22:39:23 +04:00
|
|
|
if (type()==FL_HOLD_BROWSER) {
|
|
|
|
switch (Fl::event_key()) {
|
|
|
|
case FL_Down:
|
|
|
|
while ((l = item_next(l)))
|
|
|
|
if (item_height(l)>0) {select_only(l, when()); break;}
|
|
|
|
return 1;
|
|
|
|
case FL_Up:
|
2010-02-20 20:40:07 +03:00
|
|
|
while ((l = item_prev(l))) {
|
|
|
|
if (item_height(l)>0) {
|
|
|
|
select_only(l, when());
|
|
|
|
break; // no need to test wp (return 1)
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 22:39:23 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (Fl::event_key()) {
|
|
|
|
case FL_Enter:
|
|
|
|
case FL_KP_Enter:
|
2007-12-15 19:20:16 +03:00
|
|
|
select_only(l, when() & ~FL_WHEN_ENTER_KEY);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2007-12-15 19:20:16 +03:00
|
|
|
if (when() & FL_WHEN_ENTER_KEY) {
|
|
|
|
set_changed();
|
|
|
|
do_callback();
|
|
|
|
}
|
2007-10-07 22:39:23 +04:00
|
|
|
return 1;
|
|
|
|
case ' ':
|
|
|
|
selection_ = l;
|
2007-12-15 19:20:16 +03:00
|
|
|
select(l, !item_selected(l), when() & ~FL_WHEN_ENTER_KEY);
|
2007-10-07 22:39:23 +04:00
|
|
|
return 1;
|
|
|
|
case FL_Down:
|
|
|
|
while ((l = item_next(l))) {
|
|
|
|
if (Fl::event_state(FL_SHIFT|FL_CTRL))
|
|
|
|
select(l, l1 ? item_selected(l1) : 1, when());
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2007-10-07 22:39:23 +04:00
|
|
|
if (item_height(l)>0) goto J1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
case FL_Up:
|
|
|
|
while ((l = item_prev(l))) {
|
|
|
|
if (Fl::event_state(FL_SHIFT|FL_CTRL))
|
|
|
|
select(l, l1 ? item_selected(l1) : 1, when());
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2007-10-07 22:39:23 +04:00
|
|
|
if (item_height(l)>0) goto J1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
J1:
|
|
|
|
if (selection_) redraw_line(selection_);
|
|
|
|
selection_ = l; redraw_line(l);
|
|
|
|
display(l);
|
|
|
|
return 1;
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 22:39:23 +04:00
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
if (Fl_Group::handle(event)) return 1;
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
int X, Y, W, H; bbox(X, Y, W, H);
|
|
|
|
int my;
|
2005-08-08 06:59:27 +04:00
|
|
|
// NOTE:
|
|
|
|
// instead of:
|
|
|
|
// change = select_only(find_item(my), when() & FL_WHEN_CHANGED)
|
|
|
|
// we use the construct:
|
|
|
|
// change = select_only(find_item(my), 0);
|
|
|
|
// if (change && (when() & FL_WHEN_CHANGED)) {
|
|
|
|
// set_changed();
|
|
|
|
// do_callback();
|
|
|
|
// }
|
|
|
|
// See str #834
|
|
|
|
// The first form calls the callback *before* setting change.
|
|
|
|
// The callback may execute an Fl::wait(), resulting in another
|
|
|
|
// call of Fl_Browser_::handle() for the same widget. The sequence
|
|
|
|
// of events can be an FL_PUSH followed by an FL_RELEASE.
|
|
|
|
// This second call of Fl_Browser_::handle() may result in a -
|
|
|
|
// somewhat unexpected - second concurrent invocation of the callback.
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
static char change;
|
|
|
|
static char whichway;
|
|
|
|
static int py;
|
|
|
|
switch (event) {
|
|
|
|
case FL_PUSH:
|
|
|
|
if (!Fl::event_inside(X, Y, W, H)) return 0;
|
2005-07-16 12:25:06 +04:00
|
|
|
if (Fl::visible_focus()) {
|
|
|
|
Fl::focus(this);
|
|
|
|
redraw();
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
my = py = Fl::event_y();
|
|
|
|
change = 0;
|
|
|
|
if (type() == FL_NORMAL_BROWSER || !top_)
|
|
|
|
;
|
2000-01-20 09:14:34 +03:00
|
|
|
else if (type() != FL_MULTI_BROWSER) {
|
2005-05-12 17:34:36 +04:00
|
|
|
change = select_only(find_item(my), 0);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2005-05-12 17:34:36 +04:00
|
|
|
if (change && (when() & FL_WHEN_CHANGED)) {
|
|
|
|
set_changed();
|
|
|
|
do_callback();
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2005-05-12 17:34:36 +04:00
|
|
|
}
|
2000-01-20 09:14:34 +03:00
|
|
|
} else {
|
1998-10-06 22:21:25 +04:00
|
|
|
void* l = find_item(my);
|
|
|
|
whichway = 1;
|
2000-01-20 09:14:34 +03:00
|
|
|
if (Fl::event_state(FL_CTRL)) { // toggle selection:
|
2001-07-05 04:20:41 +04:00
|
|
|
TOGGLE:
|
1998-10-06 22:21:25 +04:00
|
|
|
if (l) {
|
|
|
|
whichway = !item_selected(l);
|
2005-08-08 06:59:27 +04:00
|
|
|
change = select(l, whichway, 0);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2005-08-08 06:59:27 +04:00
|
|
|
if (change && (when() & FL_WHEN_CHANGED)) {
|
|
|
|
set_changed();
|
|
|
|
do_callback();
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2005-08-08 06:59:27 +04:00
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2000-01-20 09:14:34 +03:00
|
|
|
} else if (Fl::event_state(FL_SHIFT)) { // extend selection:
|
2001-07-05 04:20:41 +04:00
|
|
|
if (l == selection_) goto TOGGLE;
|
|
|
|
// state of previous selection determines new value:
|
|
|
|
whichway = l ? !item_selected(l) : 1;
|
|
|
|
// see which of the new item or previous selection is earlier,
|
|
|
|
// by searching from the previous forward for this one:
|
|
|
|
int down;
|
|
|
|
if (!l) down = 1;
|
|
|
|
else {for (void* m = selection_; ; m = item_next(m)) {
|
|
|
|
if (m == l) {down = 1; break;}
|
|
|
|
if (!m) {down = 0; break;}
|
|
|
|
}}
|
|
|
|
if (down) {
|
2010-02-20 20:40:07 +03:00
|
|
|
for (void* m = selection_; m != l; m = item_next(m)) {
|
2001-07-05 04:20:41 +04:00
|
|
|
select(m, whichway, when() & FL_WHEN_CHANGED);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
|
|
|
}
|
2001-07-05 04:20:41 +04:00
|
|
|
} else {
|
|
|
|
void* e = selection_;
|
|
|
|
for (void* m = item_next(l); m; m = item_next(m)) {
|
2000-01-20 09:14:34 +03:00
|
|
|
select(m, whichway, when() & FL_WHEN_CHANGED);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2001-07-05 04:20:41 +04:00
|
|
|
if (m == e) break;
|
2000-01-20 09:14:34 +03:00
|
|
|
}
|
|
|
|
}
|
2001-07-05 04:20:41 +04:00
|
|
|
// do the clicked item last so the select box is around it:
|
|
|
|
change = 1;
|
2005-08-08 06:59:27 +04:00
|
|
|
if (l) select(l, whichway, when() & FL_WHEN_CHANGED);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2000-01-20 09:14:34 +03:00
|
|
|
} else { // select only this item
|
2005-08-08 06:59:27 +04:00
|
|
|
change = select_only(l, 0);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2005-08-08 06:59:27 +04:00
|
|
|
if (change && (when() & FL_WHEN_CHANGED)) {
|
|
|
|
set_changed();
|
|
|
|
do_callback();
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2005-08-08 06:59:27 +04:00
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
case FL_DRAG:
|
|
|
|
// do the scrolling first:
|
|
|
|
my = Fl::event_y();
|
|
|
|
if (my < Y && my < py) {
|
|
|
|
int p = real_position_+my-Y;
|
|
|
|
if (p<0) p = 0;
|
|
|
|
position(p);
|
1998-10-20 00:46:58 +04:00
|
|
|
} else if (my > (Y+H) && my > py) {
|
1998-10-06 22:21:25 +04:00
|
|
|
int p = real_position_+my-(Y+H);
|
2002-08-09 05:09:49 +04:00
|
|
|
int hh = full_height()-H; if (p > hh) p = hh;
|
1998-10-06 22:21:25 +04:00
|
|
|
if (p<0) p = 0;
|
|
|
|
position(p);
|
|
|
|
}
|
|
|
|
if (type() == FL_NORMAL_BROWSER || !top_)
|
|
|
|
;
|
|
|
|
else if (type() == FL_MULTI_BROWSER) {
|
|
|
|
void* l = find_item(my);
|
|
|
|
void* t; void* b; // this will be the range to change
|
|
|
|
if (my > py) { // go down
|
|
|
|
t = selection_ ? item_next(selection_) : 0;
|
|
|
|
b = l ? item_next(l) : 0;
|
|
|
|
} else { // go up
|
|
|
|
t = l;
|
|
|
|
b = selection_;
|
|
|
|
}
|
2005-08-08 06:59:27 +04:00
|
|
|
for (; t && t != b; t = item_next(t)) {
|
|
|
|
char change_t;
|
|
|
|
change_t = select(t, whichway, 0);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2005-08-08 06:59:27 +04:00
|
|
|
change |= change_t;
|
|
|
|
if (change_t && (when() & FL_WHEN_CHANGED)) {
|
|
|
|
set_changed();
|
|
|
|
do_callback();
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
2005-08-08 06:59:27 +04:00
|
|
|
}
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
if (l) selection_ = l;
|
|
|
|
} else {
|
|
|
|
void* l1 = selection_;
|
|
|
|
void* l =
|
|
|
|
(Fl::event_x()<x() || Fl::event_x()>x()+w()) ? selection_ :
|
|
|
|
find_item(my);
|
|
|
|
change = (l != l1);
|
2005-08-08 06:59:27 +04:00
|
|
|
select_only(l, when() & FL_WHEN_CHANGED);
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
py = my;
|
|
|
|
return 1;
|
|
|
|
case FL_RELEASE:
|
|
|
|
if (type() == FL_SELECT_BROWSER) {
|
2010-02-20 20:40:07 +03:00
|
|
|
void* t = selection_;
|
|
|
|
deselect();
|
|
|
|
if (wp.deleted()) return 1;
|
|
|
|
selection_ = t;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
if (change) {
|
2004-07-27 20:02:21 +04:00
|
|
|
set_changed();
|
1998-10-06 22:21:25 +04:00
|
|
|
if (when() & FL_WHEN_RELEASE) do_callback();
|
|
|
|
} else {
|
|
|
|
if (when() & FL_WHEN_NOT_CHANGED) do_callback();
|
|
|
|
}
|
2010-02-20 20:40:07 +03:00
|
|
|
if (wp.deleted()) return 1;
|
|
|
|
|
2007-12-15 19:20:16 +03:00
|
|
|
// double click calls the callback: (like Enter Key)
|
|
|
|
if (Fl::event_clicks() && (when() & FL_WHEN_ENTER_KEY)) {
|
|
|
|
set_changed();
|
|
|
|
do_callback();
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
return 1;
|
2001-08-05 00:17:10 +04:00
|
|
|
case FL_FOCUS:
|
|
|
|
case FL_UNFOCUS:
|
2001-11-03 22:24:22 +03:00
|
|
|
if (type() >= FL_HOLD_BROWSER && Fl::visible_focus()) {
|
2001-08-05 00:17:10 +04:00
|
|
|
redraw();
|
|
|
|
return 1;
|
|
|
|
} else return 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-26 02:09:04 +03:00
|
|
|
/**
|
|
|
|
The constructor makes an empty browser.
|
|
|
|
\param[in] X,Y,W,H position and size.
|
|
|
|
\param[in] L The label string, may be NULL.
|
|
|
|
*/
|
|
|
|
Fl_Browser_::Fl_Browser_(int X, int Y, int W, int H, const char* L)
|
|
|
|
: Fl_Group(X, Y, W, H, L),
|
1998-10-06 22:21:25 +04:00
|
|
|
scrollbar(0, 0, 0, 0, 0), // they will be resized by draw()
|
|
|
|
hscrollbar(0, 0, 0, 0, 0)
|
|
|
|
{
|
|
|
|
box(FL_NO_BOX);
|
|
|
|
align(FL_ALIGN_BOTTOM);
|
|
|
|
position_ = real_position_ = 0;
|
|
|
|
hposition_ = real_hposition_ = 0;
|
|
|
|
offset_ = 0;
|
|
|
|
top_ = 0;
|
|
|
|
when(FL_WHEN_RELEASE_ALWAYS);
|
|
|
|
selection_ = 0;
|
2002-04-11 14:46:19 +04:00
|
|
|
color(FL_BACKGROUND2_COLOR, FL_SELECTION_COLOR);
|
1998-10-06 22:21:25 +04:00
|
|
|
scrollbar.callback(scrollbar_callback);
|
2008-09-15 21:46:42 +04:00
|
|
|
//scrollbar.align(FL_ALIGN_LEFT|FL_ALIGN_BOTTOM); // back compatibility?
|
1998-10-06 22:21:25 +04:00
|
|
|
hscrollbar.callback(hscrollbar_callback);
|
|
|
|
hscrollbar.type(FL_HORIZONTAL);
|
|
|
|
textfont_ = FL_HELVETICA;
|
2008-04-23 23:09:28 +04:00
|
|
|
textsize_ = FL_NORMAL_SIZE;
|
2002-04-11 14:46:19 +04:00
|
|
|
textcolor_ = FL_FOREGROUND_COLOR;
|
1998-10-06 22:21:25 +04:00
|
|
|
has_scrollbar_ = BOTH;
|
|
|
|
max_width = 0;
|
|
|
|
max_width_item = 0;
|
2009-04-02 10:44:34 +04:00
|
|
|
scrollbar_size_ = 0;
|
1999-03-09 09:46:36 +03:00
|
|
|
redraw1 = redraw2 = 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
end();
|
|
|
|
}
|
|
|
|
|
2009-03-26 02:09:04 +03:00
|
|
|
/**
|
|
|
|
Sort the items in the browser based on \p flags.
|
|
|
|
item_swap(void*, void*) and item_text(void*) must be implemented for this call.
|
|
|
|
\param[in] flags FL_SORT_ASCENDING -- sort in ascending order\n
|
|
|
|
FL_SORT_DESCENDING -- sort in descending order\n
|
|
|
|
Values other than the above will cause undefined behavior\n
|
|
|
|
Other flags may appear in the future.
|
|
|
|
\todo Add a flag to ignore case
|
|
|
|
*/
|
2008-12-27 00:20:38 +03:00
|
|
|
void Fl_Browser_::sort(int flags) {
|
2009-03-26 02:09:04 +03:00
|
|
|
//
|
|
|
|
// Simple bubble sort - pure lazyness on my side.
|
|
|
|
//
|
|
|
|
int i, j, n = -1, desc = ((flags&FL_SORT_DESCENDING)==FL_SORT_DESCENDING);
|
2008-12-27 00:20:38 +03:00
|
|
|
void *a =item_first(), *b, *c;
|
|
|
|
if (!a) return;
|
|
|
|
while (a) {
|
|
|
|
a = item_next(a);
|
|
|
|
n++;
|
|
|
|
}
|
2010-02-19 23:25:54 +03:00
|
|
|
for (i=n; i>0; i--) {
|
2008-12-27 00:20:38 +03:00
|
|
|
char swapped = 0;
|
|
|
|
a = item_first();
|
|
|
|
b = item_next(a);
|
|
|
|
for (j=0; j<i; j++) {
|
|
|
|
const char *ta = item_text(a);
|
|
|
|
const char *tb = item_text(b);
|
|
|
|
c = item_next(b);
|
|
|
|
if (desc) {
|
|
|
|
if (strcmp(ta, tb)<0) {
|
|
|
|
item_swap(a, b);
|
|
|
|
swapped = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcmp(ta, tb)>0) {
|
|
|
|
item_swap(a, b);
|
|
|
|
swapped = 1;
|
|
|
|
}
|
|
|
|
}
|
2010-02-19 23:25:54 +03:00
|
|
|
if (!c) break;
|
2008-12-27 00:20:38 +03:00
|
|
|
b = c; a = item_prev(b);
|
|
|
|
}
|
|
|
|
if (!swapped)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
// Default versions of some of the virtual functions:
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
|
|
|
This method may be provided by the subclass to return the height of the
|
2009-03-26 02:09:04 +03:00
|
|
|
\p item, in pixels.
|
|
|
|
Allow for two additional pixels for the list selection box.
|
|
|
|
This method differs from item_height in that it is only called for
|
|
|
|
selection and scrolling operations.
|
|
|
|
The default implementation calls item_height.
|
|
|
|
\param[in] item The item whose height to return.
|
|
|
|
\returns The height, in pixels.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
int Fl_Browser_::item_quick_height(void* item) const {
|
|
|
|
return item_height(item);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
This method may be provided to return the average height of all items
|
|
|
|
to be used for scrolling.
|
|
|
|
The default implementation uses the height of the first item.
|
|
|
|
\returns The average height of items, in pixels.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Browser_::incr_height() const {
|
|
|
|
return item_quick_height(item_first());
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
|
|
|
This method may be provided by the subclass to indicate the full height
|
2009-03-26 02:09:04 +03:00
|
|
|
of the item list, in pixels.
|
|
|
|
The default implementation computes the full height from the item heights.
|
|
|
|
Includes the items that are scrolled off screen.
|
|
|
|
\returns The height of the entire list, in pixels.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Browser_::full_height() const {
|
|
|
|
int t = 0;
|
|
|
|
for (void* p = item_first(); p; p = item_next(p))
|
|
|
|
t += item_quick_height(p);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
|
|
|
This method may be provided by the subclass to indicate the full width
|
2009-03-26 02:09:04 +03:00
|
|
|
of the item list, in pixels.
|
|
|
|
The default implementation computes the full width from the item widths.
|
|
|
|
\returns The maximum width of all the items, in pixels.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Browser_::full_width() const {
|
|
|
|
return max_width;
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-26 02:09:04 +03:00
|
|
|
This method must be implemented by the subclass if it supports
|
|
|
|
multiple selections; sets the selection state to \p val for the \p item.
|
|
|
|
Sets the selection state for \p item, where optional \p val is 1 (select, the default)
|
|
|
|
or 0 (de-select).
|
|
|
|
\param[in] item The item to be selected
|
|
|
|
\param[in] val The optional selection state; 1=select, 0=de-select.\n
|
|
|
|
The default is to select the item (1).
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void Fl_Browser_::item_select(void *item, int val) {}
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
|
|
|
This method must be implemented by the subclass if it supports
|
2009-03-26 02:09:04 +03:00
|
|
|
multiple selections; returns the selection state for \p item.
|
|
|
|
The method should return 1 if \p item is selected, or 0 otherwise.
|
|
|
|
\param[in] item The item to test.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
int Fl_Browser_::item_selected(void* item) const { return item==selection_ ? 1 : 0; }
|
1998-10-06 22:21:25 +04:00
|
|
|
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|