Fl_Browser.H 12 KB

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