You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
12KB

  1. //
  2. // "$Id: Fl_Browser.H 8623 2011-04-24 17:09:41Z AlbrechtS $"
  3. //
  4. // Browser header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2011 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. /* \file
  28. Fl_Browser widget . */
  29. // Forms-compatible browser. Probably useful for other
  30. // lists of textual data. Notice that the line numbers
  31. // start from 1, and 0 means "no line".
  32. #ifndef Fl_Browser_H
  33. #define Fl_Browser_H
  34. #include "Fl_Browser_.H"
  35. #include "Fl_Image.H"
  36. struct FL_BLINE;
  37. /**
  38. The Fl_Browser widget displays a scrolling list of text
  39. lines, and manages all the storage for the text. This is not a text
  40. editor or spreadsheet! But it is useful for showing a vertical list of
  41. named objects to the user.
  42. Each line in the browser is identified by number. <I>The numbers
  43. start at one</I> (this is so that zero can be reserved for "no line" in
  44. the selective browsers). <I>Unless otherwise noted, the methods do not
  45. check to see if the passed line number is in range and legal. It must
  46. always be greater than zero and &lt;= size().</I>
  47. Each line contains a null-terminated string of text and a void *
  48. data pointer. The text string is displayed, the void *
  49. pointer can be used by the callbacks to reference the object the text
  50. describes.
  51. The base class does nothing when the user clicks on it. The
  52. subclasses
  53. Fl_Select_Browser,
  54. Fl_Hold_Browser, and
  55. Fl_Multi_Browser react to user clicks to select lines in
  56. the browser and do callbacks.
  57. The base class
  58. Fl_Browser_ provides the scrolling and selection mechanisms of
  59. this and all the subclasses, but the dimensions and appearance of each
  60. item are determined by the subclass. You can use Fl_Browser_
  61. to display information other than text, or text that is dynamically
  62. produced from your own data structures. If you find that loading the
  63. browser is a lot of work or is inefficient, you may want to make a
  64. subclass of Fl_Browser_.
  65. Some common coding patterns used for working with Fl_Browser:
  66. \code
  67. // How to loop through all the items in the browser
  68. for ( int t=1; t<=browser->size(); t++ ) { // index 1 based..!
  69. printf("item #%d, label='%s'\n", t, browser->text(t));
  70. }
  71. \endcode
  72. Note: If you are <I>subclassing</I> Fl_Browser, it's more efficient
  73. to use the protected methods item_first() and item_next(), since
  74. Fl_Browser internally uses linked lists to manage the browser's items.
  75. For more info, see find_item(int).
  76. */
  77. class FL_EXPORT Fl_Browser : public Fl_Browser_ {
  78. FL_BLINE *first; // the array of lines
  79. FL_BLINE *last;
  80. FL_BLINE *cache;
  81. int cacheline; // line number of cache
  82. int lines; // Number of lines
  83. int full_height_;
  84. const int* column_widths_;
  85. char format_char_; // alternative to @-sign
  86. char column_char_; // alternative to tab
  87. protected:
  88. // required routines for Fl_Browser_ subclass:
  89. void* item_first() const ;
  90. void* item_next(void* item) const ;
  91. void* item_prev(void* item) const ;
  92. void* item_last()const ;
  93. int item_selected(void* item) const ;
  94. void item_select(void* item, int val);
  95. int item_height(void* item) const ;
  96. int item_width(void* item) const ;
  97. void item_draw(void* item, int X, int Y, int W, int H) const ;
  98. int full_height() const ;
  99. int incr_height() const ;
  100. const char *item_text(void *item) const;
  101. /** Swap the items \p a and \p b.
  102. You must call redraw() to make any changes visible.
  103. \param[in] a,b the items to be swapped.
  104. \see swap(int,int), item_swap()
  105. */
  106. void item_swap(void *a, void *b) { swap((FL_BLINE*)a, (FL_BLINE*)b); }
  107. /** Return the item at specified \p line.
  108. \param[in] line The line of the item to return. (1 based)
  109. \returns The item, or NULL if line out of range.
  110. \see item_at(), find_line(), lineno()
  111. */
  112. void *item_at(int line) const { return (void*)find_line(line); }
  113. FL_BLINE* find_line(int line) const ;
  114. FL_BLINE* _remove(int line) ;
  115. void insert(int line, FL_BLINE* item);
  116. int lineno(void *item) const ;
  117. void swap(FL_BLINE *a, FL_BLINE *b);
  118. public:
  119. void remove(int line);
  120. void add(const char* newtext, void* d = 0);
  121. void insert(int line, const char* newtext, void* d = 0);
  122. void move(int to, int from);
  123. int load(const char* filename);
  124. void swap(int a, int b);
  125. void clear();
  126. /**
  127. Returns how many lines are in the browser.
  128. The last line number is equal to this.
  129. Returns 0 if browser is empty.
  130. */
  131. int size() const { return lines; }
  132. void size(int W, int H) { Fl_Widget::size(W, H); }
  133. int topline() const ;
  134. /** For internal use only? */
  135. enum Fl_Line_Position { TOP, BOTTOM, MIDDLE };
  136. void lineposition(int line, Fl_Line_Position pos);
  137. /**
  138. Scrolls the browser so the top item in the browser
  139. is showing the specified \p line.
  140. \param[in] line The line to be displayed at the top.
  141. \see topline(), middleline(), bottomline(), displayed(), lineposition()
  142. */
  143. void topline(int line) { lineposition(line, TOP); }
  144. /**
  145. Scrolls the browser so the bottom item in the browser
  146. is showing the specified \p line.
  147. \param[in] line The line to be displayed at the bottom.
  148. \see topline(), middleline(), bottomline(), displayed(), lineposition()
  149. */
  150. void bottomline(int line) { lineposition(line, BOTTOM); }
  151. /**
  152. Scrolls the browser so the middle item in the browser
  153. is showing the specified \p line.
  154. \param[in] line The line to be displayed in the middle.
  155. \see topline(), middleline(), bottomline(), displayed(), lineposition()
  156. */
  157. void middleline(int line) { lineposition(line, MIDDLE); }
  158. int select(int line, int val=1);
  159. int selected(int line) const ;
  160. void show(int line);
  161. /** Shows the entire Fl_Browser widget -- opposite of hide(). */
  162. void show() { Fl_Widget::show(); }
  163. void hide(int line);
  164. /** Hides the entire Fl_Browser widget -- opposite of show(). */
  165. void hide() { Fl_Widget::hide(); }
  166. int visible(int line) const ;
  167. int value() const ;
  168. /**
  169. Sets the browser's value(), which selects the specified \p line.
  170. This is the same as calling select(line).
  171. \see select(), selected(), value(), item_select(), item_selected()
  172. */
  173. void value(int line) { select(line); }
  174. const char* text(int line) const ;
  175. void text(int line, const char* newtext);
  176. void* data(int line) const ;
  177. void data(int line, void* d);
  178. Fl_Browser(int X, int Y, int W, int H, const char *L = 0);
  179. /**
  180. The destructor deletes all list items and destroys the browser.
  181. */
  182. ~Fl_Browser() { clear(); }
  183. /**
  184. Gets the current format code prefix character, which by default is '\@'.
  185. A string of formatting codes at the start of each column are stripped off
  186. and used to modify how the rest of the line is printed:
  187. \li <tt>'\@.'</tt> Print rest of line, don't look for more '\@' signs
  188. \li <tt>'\@\@'</tt> Print rest of line starting with '\@'
  189. \li <tt>'\@l'</tt> Use a LARGE (24 point) font
  190. \li <tt>'\@m'</tt> Use a medium large (18 point) font
  191. \li <tt>'\@s'</tt> Use a <SMALL>small</SMALL> (11 point) font
  192. \li <tt>'\@b'</tt> Use a <B>bold</B> font (adds FL_BOLD to font)
  193. \li <tt>'\@i'</tt> Use an <I>italic</I> font (adds FL_ITALIC to font)
  194. \li <tt>'\@f' or '\@t'</tt> Use a fixed-pitch
  195. font (sets font to FL_COURIER)
  196. \li <tt>'\@c'</tt> Center the line horizontally
  197. \li <tt>'\@r'</tt> Right-justify the text
  198. \li <tt>'\@B0', '\@B1', ... '\@B255'</tt> Fill the backgound with
  199. fl_color(n)
  200. \li <tt>'\@C0', '\@C1', ... '\@C255'</tt> Use fl_color(n) to draw the text
  201. \li <tt>'\@F0', '\@F1', ...</tt> Use fl_font(n) to draw the text
  202. \li <tt>'\@S1', '\@S2', ...</tt> Use point size n to draw the text
  203. \li <tt>'\@u' or '\@_'</tt> Underline the text.
  204. \li <tt>'\@-'</tt> draw an engraved line through the middle.
  205. Notice that the '\@.' command can be used to reliably
  206. terminate the parsing. To print a random string in a random color, use
  207. <tt>sprintf("@C%d@.%s", color, string)</tt> and it will work even if the
  208. string starts with a digit or has the format character in it.
  209. */
  210. char format_char() const { return format_char_; }
  211. /**
  212. Sets the current format code prefix character to \p c.
  213. The default prefix is '\@'. Set the prefix to 0 to disable formatting.
  214. \see format_char() for list of '\@' codes
  215. */
  216. void format_char(char c) { format_char_ = c; }
  217. /**
  218. Gets the current column separator character.
  219. The default is '\\t' (tab).
  220. \see column_char(), column_widths()
  221. */
  222. char column_char() const { return column_char_; }
  223. /**
  224. Sets the column separator to c.
  225. This will only have an effect if you also set column_widths().
  226. The default is '\\t' (tab).
  227. \see column_char(), column_widths()
  228. */
  229. void column_char(char c) { column_char_ = c; }
  230. /**
  231. Gets the current column width array.
  232. This array is zero-terminated and specifies the widths in pixels of
  233. each column. The text is split at each column_char() and each part is
  234. formatted into it's own column. After the last column any remaining
  235. text is formatted into the space between the last column and the
  236. right edge of the browser, even if the text contains instances of
  237. column_char() . The default value is a one-element array of just
  238. a zero, which means there are no columns.
  239. Example:
  240. \code
  241. Fl_Browser *b = new Fl_Browser(..);
  242. int widths[] = { 50, 50, 50, 70, 70, 40, 40, 70, 70, 50, 0 }; // widths for each column
  243. b->column_widths(widths); // assign array to widget
  244. b->column_char('\t'); // use tab as the column character
  245. b->add("USER\tPID\tCPU\tMEM\tVSZ\tRSS\tTTY\tSTAT\tSTART\tTIME\tCOMMAND");
  246. b->add("root\t2888\t0.0\t0.0\t1352\t0\ttty3\tSW\tAug15\t0:00\t@b@f/sbin/mingetty tty3");
  247. b->add("root\t13115\t0.0\t0.0\t1352\t0\ttty2\tSW\tAug30\t0:00\t@b@f/sbin/mingetty tty2");
  248. [..]
  249. \endcode
  250. \see column_char(), column_widths()
  251. */
  252. const int* column_widths() const { return column_widths_; }
  253. /**
  254. Sets the current array to \p arr. Make sure the last entry is zero.
  255. \see column_char(), column_widths()
  256. */
  257. void column_widths(const int* arr) { column_widths_ = arr; }
  258. /**
  259. Returns non-zero if \p line has been scrolled to a position where it is being displayed.
  260. Checks to see if the item's vertical position is within the top and bottom
  261. edges of the display window. This does NOT take into account the hide()/show()
  262. status of the widget or item.
  263. \param[in] line The line to be checked
  264. \returns 1 if visible, 0 if not visible.
  265. \see topline(), middleline(), bottomline(), displayed(), lineposition()
  266. */
  267. int displayed(int line) const { return Fl_Browser_::displayed(find_line(line)); }
  268. /**
  269. Make the item at the specified \p line visible().
  270. Functionally similar to show(int line).
  271. If \p line is out of range, redisplay top or bottom of list as appropriate.
  272. \param[in] line The line to be made visible.
  273. \see show(int), hide(int), display(), visible(), make_visible()
  274. */
  275. void make_visible(int line) {
  276. if (line < 1) Fl_Browser_::display(find_line(1));
  277. else if (line > lines) Fl_Browser_::display(find_line(lines));
  278. else Fl_Browser_::display(find_line(line));
  279. }
  280. // icon support
  281. void icon(int line, Fl_Image* icon);
  282. Fl_Image* icon(int line) const;
  283. void remove_icon(int line);
  284. /** For back compatibility only. */
  285. void replace(int a, const char* b) { text(a, b); }
  286. void display(int line, int val=1);
  287. };
  288. #endif
  289. //
  290. // End of "$Id: Fl_Browser.H 8623 2011-04-24 17:09:41Z AlbrechtS $".
  291. //