1998-10-20 01:39:29 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
1998-10-20 01:39:29 +04:00
|
|
|
//
|
|
|
|
// Browser header file for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2016-03-28 19:16:29 +03:00
|
|
|
// Copyright 1998-2016 by Bill Spitzak and others.
|
1998-10-20 01:39:29 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/COPYING.php
|
1998-10-20 01:39:29 +04:00
|
|
|
//
|
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 01:39:29 +04:00
|
|
|
//
|
|
|
|
|
2008-10-15 17:46:06 +04:00
|
|
|
/* \file
|
2008-09-16 11:26:22 +04:00
|
|
|
Fl_Browser widget . */
|
|
|
|
|
2011-04-24 21:09:41 +04:00
|
|
|
// Forms-compatible browser. Probably useful for other
|
1998-10-20 01:39:29 +04:00
|
|
|
// lists of textual data. Notice that the line numbers
|
|
|
|
// start from 1, and 0 means "no line".
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
#ifndef Fl_Browser_H
|
|
|
|
#define Fl_Browser_H
|
|
|
|
|
|
|
|
#include "Fl_Browser_.H"
|
2009-09-07 06:25:51 +04:00
|
|
|
#include "Fl_Image.H"
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
struct FL_BLINE;
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
|
|
|
The Fl_Browser widget displays a scrolling list of text
|
|
|
|
lines, and manages all the storage for the text. This is not a text
|
|
|
|
editor or spreadsheet! But it is useful for showing a vertical list of
|
|
|
|
named objects to the user.
|
2009-03-17 06:29:58 +03:00
|
|
|
|
|
|
|
Each line in the browser is identified by number. <I>The numbers
|
2008-09-17 19:44:43 +04:00
|
|
|
start at one</I> (this is so that zero can be reserved for "no line" in
|
2008-09-14 16:45:42 +04:00
|
|
|
the selective browsers). <I>Unless otherwise noted, the methods do not
|
|
|
|
check to see if the passed line number is in range and legal. It must
|
2009-03-17 06:29:58 +03:00
|
|
|
always be greater than zero and <= size().</I>
|
|
|
|
|
|
|
|
Each line contains a null-terminated string of text and a void *
|
2008-09-14 16:45:42 +04:00
|
|
|
data pointer. The text string is displayed, the void *
|
|
|
|
pointer can be used by the callbacks to reference the object the text
|
2009-03-17 06:29:58 +03:00
|
|
|
describes.
|
|
|
|
|
|
|
|
The base class does nothing when the user clicks on it. The
|
2008-09-14 16:45:42 +04:00
|
|
|
subclasses
|
|
|
|
Fl_Select_Browser,
|
|
|
|
Fl_Hold_Browser, and
|
|
|
|
Fl_Multi_Browser react to user clicks to select lines in
|
2009-03-17 06:29:58 +03:00
|
|
|
the browser and do callbacks.
|
|
|
|
|
|
|
|
The base class
|
2008-09-14 16:45:42 +04:00
|
|
|
Fl_Browser_ provides the scrolling and selection mechanisms of
|
|
|
|
this and all the subclasses, but the dimensions and appearance of each
|
|
|
|
item are determined by the subclass. You can use Fl_Browser_
|
|
|
|
to display information other than text, or text that is dynamically
|
|
|
|
produced from your own data structures. If you find that loading the
|
|
|
|
browser is a lot of work or is inefficient, you may want to make a
|
2009-03-17 06:29:58 +03:00
|
|
|
subclass of Fl_Browser_.
|
|
|
|
|
|
|
|
Some common coding patterns used for working with Fl_Browser:
|
|
|
|
\code
|
|
|
|
// How to loop through all the items in the browser
|
|
|
|
for ( int t=1; t<=browser->size(); t++ ) { // index 1 based..!
|
|
|
|
printf("item #%d, label='%s'\n", t, browser->text(t));
|
|
|
|
}
|
|
|
|
\endcode
|
|
|
|
|
|
|
|
Note: If you are <I>subclassing</I> Fl_Browser, it's more efficient
|
|
|
|
to use the protected methods item_first() and item_next(), since
|
|
|
|
Fl_Browser internally uses linked lists to manage the browser's items.
|
|
|
|
For more info, see find_item(int).
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2002-08-14 20:49:38 +04:00
|
|
|
class FL_EXPORT Fl_Browser : public Fl_Browser_ {
|
1998-10-06 22:21:25 +04:00
|
|
|
|
1999-11-16 10:39:59 +03:00
|
|
|
FL_BLINE *first; // the array of lines
|
|
|
|
FL_BLINE *last;
|
|
|
|
FL_BLINE *cache;
|
|
|
|
int cacheline; // line number of cache
|
|
|
|
int lines; // Number of lines
|
|
|
|
int full_height_;
|
|
|
|
const int* column_widths_;
|
|
|
|
char format_char_; // alternative to @-sign
|
|
|
|
char column_char_; // alternative to tab
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
// required routines for Fl_Browser_ subclass:
|
2002-08-14 20:49:38 +04:00
|
|
|
void* item_first() const ;
|
2009-03-17 06:29:58 +03:00
|
|
|
void* item_next(void* item) const ;
|
|
|
|
void* item_prev(void* item) const ;
|
2008-12-27 00:20:38 +03:00
|
|
|
void* item_last()const ;
|
2009-03-17 06:29:58 +03:00
|
|
|
int item_selected(void* item) const ;
|
|
|
|
void item_select(void* item, int val);
|
|
|
|
int item_height(void* item) const ;
|
|
|
|
int item_width(void* item) const ;
|
|
|
|
void item_draw(void* item, int X, int Y, int W, int H) const ;
|
2002-08-14 20:49:38 +04:00
|
|
|
int full_height() const ;
|
|
|
|
int incr_height() const ;
|
2008-12-27 00:20:38 +03:00
|
|
|
const char *item_text(void *item) const;
|
2009-03-17 06:29:58 +03:00
|
|
|
/** Swap the items \p a and \p b.
|
|
|
|
You must call redraw() to make any changes visible.
|
|
|
|
\param[in] a,b the items to be swapped.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see swap(int,int), item_swap()
|
2009-03-17 06:29:58 +03:00
|
|
|
*/
|
2008-12-27 00:20:38 +03:00
|
|
|
void item_swap(void *a, void *b) { swap((FL_BLINE*)a, (FL_BLINE*)b); }
|
2009-03-17 06:29:58 +03:00
|
|
|
/** Return the item at specified \p line.
|
|
|
|
\param[in] line The line of the item to return. (1 based)
|
|
|
|
\returns The item, or NULL if line out of range.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see item_at(), find_line(), lineno()
|
2009-03-17 06:29:58 +03:00
|
|
|
*/
|
|
|
|
void *item_at(int line) const { return (void*)find_line(line); }
|
2002-08-14 20:49:38 +04:00
|
|
|
|
2009-03-17 06:29:58 +03:00
|
|
|
FL_BLINE* find_line(int line) const ;
|
|
|
|
FL_BLINE* _remove(int line) ;
|
|
|
|
void insert(int line, FL_BLINE* item);
|
|
|
|
int lineno(void *item) const ;
|
2004-07-27 00:52:52 +04:00
|
|
|
void swap(FL_BLINE *a, FL_BLINE *b);
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2009-03-17 06:29:58 +03:00
|
|
|
void remove(int line);
|
|
|
|
void add(const char* newtext, void* d = 0);
|
|
|
|
void insert(int line, const char* newtext, void* d = 0);
|
2002-08-14 20:49:38 +04:00
|
|
|
void move(int to, int from);
|
|
|
|
int load(const char* filename);
|
2004-07-27 00:52:52 +04:00
|
|
|
void swap(int a, int b);
|
2002-08-14 20:49:38 +04:00
|
|
|
void clear();
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2009-03-26 02:09:04 +03:00
|
|
|
/**
|
|
|
|
Returns how many lines are in the browser.
|
|
|
|
The last line number is equal to this.
|
|
|
|
Returns 0 if browser is empty.
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
int size() const { return lines; }
|
2003-06-15 08:47:28 +04:00
|
|
|
void size(int W, int H) { Fl_Widget::size(W, H); }
|
|
|
|
|
2016-03-28 19:16:29 +03:00
|
|
|
/**
|
|
|
|
Gets the default text size (in pixels) for the lines in the browser.
|
|
|
|
*/
|
|
|
|
Fl_Fontsize textsize() const { return Fl_Browser_::textsize(); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
Sets the default text size for the lines in the browser to newSize.
|
|
|
|
Defined and documented in Fl_Browser.cxx
|
|
|
|
*/
|
|
|
|
void textsize(Fl_Fontsize newSize);
|
|
|
|
|
2002-08-14 20:49:38 +04:00
|
|
|
int topline() const ;
|
2008-09-20 18:29:29 +04:00
|
|
|
/** For internal use only? */
|
1999-01-30 03:39:28 +03:00
|
|
|
enum Fl_Line_Position { TOP, BOTTOM, MIDDLE };
|
2009-03-17 06:29:58 +03:00
|
|
|
void lineposition(int line, Fl_Line_Position pos);
|
2009-03-26 02:09:04 +03:00
|
|
|
/**
|
|
|
|
Scrolls the browser so the top item in the browser
|
|
|
|
is showing the specified \p line.
|
|
|
|
\param[in] line The line to be displayed at the top.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see topline(), middleline(), bottomline(), displayed(), lineposition()
|
2009-03-26 02:09:04 +03:00
|
|
|
*/
|
2009-03-17 06:29:58 +03:00
|
|
|
void topline(int line) { lineposition(line, TOP); }
|
2009-03-26 02:09:04 +03:00
|
|
|
/**
|
|
|
|
Scrolls the browser so the bottom item in the browser
|
|
|
|
is showing the specified \p line.
|
|
|
|
\param[in] line The line to be displayed at the bottom.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see topline(), middleline(), bottomline(), displayed(), lineposition()
|
2009-03-26 02:09:04 +03:00
|
|
|
*/
|
2009-03-17 06:29:58 +03:00
|
|
|
void bottomline(int line) { lineposition(line, BOTTOM); }
|
2009-03-26 02:09:04 +03:00
|
|
|
/**
|
|
|
|
Scrolls the browser so the middle item in the browser
|
|
|
|
is showing the specified \p line.
|
|
|
|
\param[in] line The line to be displayed in the middle.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see topline(), middleline(), bottomline(), displayed(), lineposition()
|
2009-03-26 02:09:04 +03:00
|
|
|
*/
|
2009-03-17 06:29:58 +03:00
|
|
|
void middleline(int line) { lineposition(line, MIDDLE); }
|
|
|
|
|
|
|
|
int select(int line, int val=1);
|
|
|
|
int selected(int line) const ;
|
|
|
|
void show(int line);
|
|
|
|
/** Shows the entire Fl_Browser widget -- opposite of hide(). */
|
2009-03-26 02:09:04 +03:00
|
|
|
void show() { Fl_Widget::show(); }
|
2009-03-17 06:29:58 +03:00
|
|
|
void hide(int line);
|
|
|
|
/** Hides the entire Fl_Browser widget -- opposite of show(). */
|
2009-03-26 02:09:04 +03:00
|
|
|
void hide() { Fl_Widget::hide(); }
|
2009-03-17 06:29:58 +03:00
|
|
|
int visible(int line) const ;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2002-08-14 20:49:38 +04:00
|
|
|
int value() const ;
|
2009-03-27 19:52:31 +03:00
|
|
|
/**
|
|
|
|
Sets the browser's value(), which selects the specified \p line.
|
|
|
|
This is the same as calling select(line).
|
|
|
|
\see select(), selected(), value(), item_select(), item_selected()
|
2009-03-17 06:29:58 +03:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void value(int line) { select(line); }
|
2009-03-17 06:29:58 +03:00
|
|
|
const char* text(int line) const ;
|
|
|
|
void text(int line, const char* newtext);
|
|
|
|
void* data(int line) const ;
|
|
|
|
void data(int line, void* d);
|
|
|
|
|
|
|
|
Fl_Browser(int X, int Y, int W, int H, const char *L = 0);
|
2009-03-26 02:09:04 +03:00
|
|
|
/**
|
|
|
|
The destructor deletes all list items and destroys the browser.
|
|
|
|
*/
|
1999-06-20 18:38:12 +04:00
|
|
|
~Fl_Browser() { clear(); }
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2008-09-20 18:29:29 +04:00
|
|
|
Gets the current format code prefix character, which by default is '\@'.
|
|
|
|
A string of formatting codes at the start of each column are stripped off
|
|
|
|
and used to modify how the rest of the line is printed:
|
|
|
|
|
|
|
|
\li <tt>'\@.'</tt> Print rest of line, don't look for more '\@' signs
|
|
|
|
\li <tt>'\@\@'</tt> Print rest of line starting with '\@'
|
|
|
|
\li <tt>'\@l'</tt> Use a LARGE (24 point) font
|
|
|
|
\li <tt>'\@m'</tt> Use a medium large (18 point) font
|
|
|
|
\li <tt>'\@s'</tt> Use a <SMALL>small</SMALL> (11 point) font
|
|
|
|
\li <tt>'\@b'</tt> Use a <B>bold</B> font (adds FL_BOLD to font)
|
|
|
|
\li <tt>'\@i'</tt> Use an <I>italic</I> font (adds FL_ITALIC to font)
|
|
|
|
\li <tt>'\@f' or '\@t'</tt> Use a fixed-pitch
|
|
|
|
font (sets font to FL_COURIER)
|
|
|
|
\li <tt>'\@c'</tt> Center the line horizontally
|
|
|
|
\li <tt>'\@r'</tt> Right-justify the text
|
|
|
|
\li <tt>'\@B0', '\@B1', ... '\@B255'</tt> Fill the backgound with
|
|
|
|
fl_color(n)
|
|
|
|
\li <tt>'\@C0', '\@C1', ... '\@C255'</tt> Use fl_color(n) to draw the text
|
|
|
|
\li <tt>'\@F0', '\@F1', ...</tt> Use fl_font(n) to draw the text
|
|
|
|
\li <tt>'\@S1', '\@S2', ...</tt> Use point size n to draw the text
|
|
|
|
\li <tt>'\@u' or '\@_'</tt> Underline the text.
|
|
|
|
\li <tt>'\@-'</tt> draw an engraved line through the middle.
|
|
|
|
|
2008-09-17 15:08:59 +04:00
|
|
|
Notice that the '\@.' command can be used to reliably
|
2008-09-14 16:45:42 +04:00
|
|
|
terminate the parsing. To print a random string in a random color, use
|
2009-03-27 19:52:31 +03:00
|
|
|
<tt>sprintf("@C%d@.%s", color, string)</tt> and it will work even if the
|
2008-09-14 16:45:42 +04:00
|
|
|
string starts with a digit or has the format character in it.
|
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
char format_char() const { return format_char_; }
|
2008-09-20 18:29:29 +04:00
|
|
|
/**
|
|
|
|
Sets the current format code prefix character to \p c.
|
|
|
|
The default prefix is '\@'. Set the prefix to 0 to disable formatting.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see format_char() for list of '\@' codes
|
2008-09-20 18:29:29 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void format_char(char c) { format_char_ = c; }
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2008-09-20 18:29:29 +04:00
|
|
|
Gets the current column separator character.
|
2009-03-17 06:29:58 +03:00
|
|
|
The default is '\\t' (tab).
|
2009-03-27 19:52:31 +03:00
|
|
|
\see column_char(), column_widths()
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
char column_char() const { return column_char_; }
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2008-09-20 18:29:29 +04:00
|
|
|
Sets the column separator to c.
|
|
|
|
This will only have an effect if you also set column_widths().
|
2009-03-17 06:29:58 +03:00
|
|
|
The default is '\\t' (tab).
|
2009-03-27 19:52:31 +03:00
|
|
|
\see column_char(), column_widths()
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void column_char(char c) { column_char_ = c; }
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2009-03-17 06:29:58 +03:00
|
|
|
Gets the current column width array.
|
|
|
|
This array is zero-terminated and specifies the widths in pixels of
|
|
|
|
each column. The text is split at each column_char() and each part is
|
|
|
|
formatted into it's own column. After the last column any remaining
|
|
|
|
text is formatted into the space between the last column and the
|
|
|
|
right edge of the browser, even if the text contains instances of
|
|
|
|
column_char() . The default value is a one-element array of just
|
|
|
|
a zero, which means there are no columns.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
\code
|
|
|
|
Fl_Browser *b = new Fl_Browser(..);
|
2012-09-18 02:44:54 +04:00
|
|
|
static int widths[] = { 50, 50, 50, 70, 70, 40, 40, 70, 70, 50, 0 }; // widths for each column
|
2009-03-17 06:29:58 +03:00
|
|
|
b->column_widths(widths); // assign array to widget
|
|
|
|
b->column_char('\t'); // use tab as the column character
|
|
|
|
b->add("USER\tPID\tCPU\tMEM\tVSZ\tRSS\tTTY\tSTAT\tSTART\tTIME\tCOMMAND");
|
|
|
|
b->add("root\t2888\t0.0\t0.0\t1352\t0\ttty3\tSW\tAug15\t0:00\t@b@f/sbin/mingetty tty3");
|
|
|
|
b->add("root\t13115\t0.0\t0.0\t1352\t0\ttty2\tSW\tAug30\t0:00\t@b@f/sbin/mingetty tty2");
|
|
|
|
[..]
|
|
|
|
\endcode
|
2009-03-27 19:52:31 +03:00
|
|
|
\see column_char(), column_widths()
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
const int* column_widths() const { return column_widths_; }
|
2008-09-20 18:29:29 +04:00
|
|
|
/**
|
2009-03-17 06:29:58 +03:00
|
|
|
Sets the current array to \p arr. Make sure the last entry is zero.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see column_char(), column_widths()
|
2008-09-20 18:29:29 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
void column_widths(const int* arr) { column_widths_ = arr; }
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2008-09-20 18:29:29 +04:00
|
|
|
/**
|
2009-03-27 19:52:31 +03:00
|
|
|
Returns non-zero if \p line 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] line The line to be checked
|
|
|
|
\returns 1 if visible, 0 if not visible.
|
|
|
|
\see topline(), middleline(), bottomline(), displayed(), lineposition()
|
2008-09-20 18:29:29 +04:00
|
|
|
*/
|
2009-03-26 02:09:04 +03:00
|
|
|
int displayed(int line) const { return Fl_Browser_::displayed(find_line(line)); }
|
2008-09-20 18:29:29 +04:00
|
|
|
|
|
|
|
/**
|
2009-03-17 06:29:58 +03:00
|
|
|
Make the item at the specified \p line visible().
|
|
|
|
Functionally similar to show(int line).
|
|
|
|
If \p line is out of range, redisplay top or bottom of list as appropriate.
|
|
|
|
\param[in] line The line to be made visible.
|
2009-03-27 19:52:31 +03:00
|
|
|
\see show(int), hide(int), display(), visible(), make_visible()
|
2008-09-20 18:29:29 +04:00
|
|
|
*/
|
2009-03-17 06:29:58 +03:00
|
|
|
void make_visible(int line) {
|
|
|
|
if (line < 1) Fl_Browser_::display(find_line(1));
|
|
|
|
else if (line > lines) Fl_Browser_::display(find_line(lines));
|
|
|
|
else Fl_Browser_::display(find_line(line));
|
2001-11-02 23:59:07 +03:00
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2009-09-07 06:25:51 +04:00
|
|
|
// icon support
|
|
|
|
void icon(int line, Fl_Image* icon);
|
|
|
|
Fl_Image* icon(int line) const;
|
|
|
|
void remove_icon(int line);
|
|
|
|
|
2008-09-20 18:29:29 +04:00
|
|
|
/** For back compatibility only. */
|
2009-03-26 02:09:04 +03:00
|
|
|
void replace(int a, const char* b) { text(a, b); }
|
2009-03-17 06:29:58 +03:00
|
|
|
void display(int line, int val=1);
|
1998-10-06 22:21:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
1998-10-20 01:39:29 +04:00
|
|
|
|
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
1998-10-20 01:39:29 +04:00
|
|
|
//
|