Fl_Browser_.H 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // "$Id: Fl_Browser_.H 12459 2017-09-14 00:50:36Z greg.ercolano $"
  3. //
  4. // Common browser header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2016 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. // Yes, I know this should be a template...
  21. #ifndef Fl_Browser__H
  22. #define Fl_Browser__H
  23. #ifndef Fl_Group_H
  24. #include "Fl_Group.H"
  25. #endif
  26. #include "Fl_Scrollbar.H"
  27. #include <FL/Fl.H> // Fl::scrollbar_size()
  28. #define FL_NORMAL_BROWSER 0 /**< type() of Fl_Browser */
  29. #define FL_SELECT_BROWSER 1 /**< type() of FL_Select_Browser */
  30. #define FL_HOLD_BROWSER 2 /**< type() of Fl_Hold_Browser */
  31. #define FL_MULTI_BROWSER 3 /**< type() of Fl_Multi_Browser */
  32. #define FL_SORT_ASCENDING 0 /**< sort browser items in ascending alphabetic order. */
  33. #define FL_SORT_DESCENDING 1 /**< sort in descending order */
  34. /**
  35. This is the base class for browsers. To be useful it must be
  36. subclassed and several virtual functions defined. The Forms-compatible
  37. browser and the file chooser's browser are subclassed off of this.
  38. This has been designed so that the subclass has complete control
  39. over the storage of the data, although because next() and
  40. prev() functions are used to index, it works best as a linked list
  41. or as a large block of characters in which the line breaks must be
  42. searched for.
  43. A great deal of work has been done so that the "height" of a data
  44. object does not need to be determined until it is drawn. This is
  45. useful if actually figuring out the size of an object requires
  46. accessing image data or doing stat() on a file or doing some
  47. other slow operation.
  48. Keyboard navigation of browser items
  49. ------------------------------------
  50. The keyboard navigation of browser items is only possible if
  51. visible_focus() is enabled. If disabled, the widget rejects keyboard focus;
  52. Tab and Shift-Tab focus navigation will skip the widget.
  53. In 'Select' and 'Normal' mode, the widget rejects keyboard focus;
  54. no navigation keys are supported (other than scrollbar positioning).
  55. In 'Hold' mode, the widget accepts keyboard focus, and Up/Down arrow
  56. keys can navigate the selected item.
  57. In 'Multi' mode, the widget accepts keyboard focus, and Up/Down arrow
  58. keys navigate the focus box; Space toggles the current item's selection,
  59. Enter selects only the current item (deselects all others). If Shift
  60. (or Ctrl) is combined with Up/Down arrow keys, the current item's
  61. selection state is extended to the next item. In this way one can
  62. extend a selection or de-selection.
  63. */
  64. class FL_EXPORT Fl_Browser_ : public Fl_Group {
  65. int position_; // where user wants it scrolled to
  66. int real_position_; // the current vertical scrolling position
  67. int hposition_; // where user wants it panned to
  68. int real_hposition_; // the current horizontal scrolling position
  69. int offset_; // how far down top_ item the real_position is
  70. int max_width; // widest object seen so far
  71. uchar has_scrollbar_; // which scrollbars are enabled
  72. Fl_Font textfont_;
  73. Fl_Fontsize textsize_;
  74. Fl_Color textcolor_;
  75. void* top_; // which item scrolling position is in
  76. void* selection_; // which is selected (except for FL_MULTI_BROWSER)
  77. void *redraw1,*redraw2; // minimal update pointers
  78. void* max_width_item; // which item has max_width_
  79. int scrollbar_size_; // size of scrollbar trough
  80. void update_top();
  81. protected:
  82. // All of the following must be supplied by the subclass:
  83. /**
  84. This method must be provided by the subclass
  85. to return the first item in the list.
  86. \see item_first(), item_next(), item_last(), item_prev()
  87. */
  88. virtual void *item_first() const = 0;
  89. /**
  90. This method must be provided by the subclass
  91. to return the item in the list after \p item.
  92. \see item_first(), item_next(), item_last(), item_prev()
  93. */
  94. virtual void *item_next(void *item) const = 0;
  95. /**
  96. This method must be provided by the subclass
  97. to return the item in the list before \p item.
  98. \see item_first(), item_next(), item_last(), item_prev()
  99. */
  100. virtual void *item_prev(void *item) const = 0;
  101. /**
  102. This method must be provided by the subclass
  103. to return the last item in the list.
  104. \see item_first(), item_next(), item_last(), item_prev()
  105. */
  106. virtual void *item_last() const { return 0L; }
  107. /**
  108. This method must be provided by the subclass to return
  109. the height of \p item in pixels.
  110. Allow for two additional pixels for the list selection box.
  111. \param[in] item The item whose height is returned.
  112. \returns The height of the specified \p item in pixels.
  113. \see item_height(), item_width(), item_quick_height()
  114. */
  115. virtual int item_height(void *item) const = 0;
  116. /**
  117. This method must be provided by the subclass to return the width of the
  118. \p item in pixels. Allow for two additional pixels for the list
  119. selection box.
  120. \param[in] item The item whose width is returned.
  121. \returns The width of the item in pixels.
  122. */
  123. virtual int item_width(void *item) const = 0;
  124. virtual int item_quick_height(void *item) const ;
  125. /**
  126. This method must be provided by the subclass to draw the \p item
  127. in the area indicated by \p X, \p Y, \p W, \p H.
  128. */
  129. virtual void item_draw(void *item,int X,int Y,int W,int H) const = 0;
  130. /**
  131. This optional method returns a string (label) that may be used for sorting.
  132. \param[in] item The item whose label text is returned.
  133. \returns The item's text label. (Can be NULL if blank)
  134. */
  135. virtual const char *item_text(void *item) const { (void)item; return 0L; }
  136. /**
  137. This optional method should be provided by the subclass
  138. to efficiently swap browser items \p a and \p b, such as for sorting.
  139. \param[in] a,b The two items to be swapped.
  140. */
  141. virtual void item_swap(void *a,void *b) { (void)a; (void)b; }
  142. /**
  143. This method must be provided by the subclass
  144. to return the item for the specified \p index.
  145. \param[in] index The \p index of the item to be returned
  146. \returns The item at the specified \p index.
  147. */
  148. virtual void *item_at(int index) const { (void)index; return 0L; }
  149. // you don't have to provide these but it may help speed it up:
  150. virtual int full_width() const ; // current width of all items
  151. virtual int full_height() const ; // current height of all items
  152. virtual int incr_height() const ; // average height of an item
  153. // These only need to be done by subclass if you want a multi-browser:
  154. virtual void item_select(void *item,int val=1);
  155. virtual int item_selected(void *item) const ;
  156. // things the subclass may want to call:
  157. /**
  158. Returns the item that appears at the top of the list.
  159. */
  160. void *top() const { return top_; }
  161. /**
  162. Returns the item currently selected, or NULL if there is no selection.
  163. For multiple selection browsers this call returns the currently focused item,
  164. even if it is not selected. To find all selected items, call
  165. Fl_Multi_Browser::selected() for every item in question.
  166. */
  167. void *selection() const { return selection_; }
  168. void new_list(); // completely clobber all data, as though list replaced
  169. void deleting(void *item); // get rid of any pointers to item
  170. void replacing(void *a,void *b); // change a pointers to b
  171. void swapping(void *a,void *b); // exchange pointers a and b
  172. void inserting(void *a,void *b); // insert b near a
  173. int displayed(void *item) const ; // true if this item is visible
  174. void redraw_line(void *item); // minimal update, no change in size
  175. /**
  176. This method will cause the entire list to be redrawn.
  177. \see redraw_lines(), redraw_line()
  178. */
  179. void redraw_lines() { damage(FL_DAMAGE_SCROLL); } // redraw all of them
  180. void bbox(int &X,int &Y,int &W,int &H) const;
  181. int leftedge() const; // x position after scrollbar & border
  182. void *find_item(int ypos); // item under mouse
  183. void draw();
  184. Fl_Browser_(int X,int Y,int W,int H,const char *L=0);
  185. public:
  186. /**
  187. Vertical scrollbar. Public, so that it can be accessed directly.
  188. */
  189. Fl_Scrollbar scrollbar;
  190. /**
  191. Horizontal scrollbar. Public, so that it can be accessed directly.
  192. */
  193. Fl_Scrollbar hscrollbar;
  194. int handle(int event);
  195. void resize(int X,int Y,int W,int H);
  196. int select(void *item,int val=1,int docallbacks=0);
  197. int select_only(void *item,int docallbacks=0);
  198. int deselect(int docallbacks=0);
  199. /**
  200. Gets the vertical scroll position of the list as a pixel position \p pos.
  201. The position returned is how many pixels of the list are scrolled off the top edge
  202. of the screen. Example: A position of '3' indicates the top 3 pixels of
  203. the list are scrolled off the top edge of the screen.
  204. \see position(), hposition()
  205. */
  206. int position() const { return position_; }
  207. void position(int pos); // scroll to here
  208. /**
  209. Gets the horizontal scroll position of the list as a pixel position \p pos.
  210. The position returned is how many pixels of the list are scrolled off the left edge
  211. of the screen. Example: A position of '18' indicates the left 18 pixels of
  212. the list are scrolled off the left edge of the screen.
  213. \see position(), hposition()
  214. */
  215. int hposition() const { return hposition_; }
  216. void hposition(int); // pan to here
  217. void display(void *item); // scroll so this item is shown
  218. /**
  219. Values for has_scrollbar().
  220. */
  221. /** Anonymous enum bit flags for has_scrollbar().
  222. - bit 0: horizontal
  223. - bit 1: vertical
  224. - bit 2: 'always' (to be combined with bits 0 and 1)
  225. - bit 3-31: reserved for future use
  226. */
  227. enum { // values for has_scrollbar()
  228. HORIZONTAL = 1, ///< Only show horizontal scrollbar.
  229. VERTICAL = 2, ///< Only show vertical scrollbar.
  230. BOTH = 3, ///< Show both scrollbars. (default)
  231. ALWAYS_ON = 4, ///< Specified scrollbar(s) should 'always' be shown (to be used with HORIZONTAL/VERTICAL)
  232. HORIZONTAL_ALWAYS = 5, ///< Horizontal scrollbar always on.
  233. VERTICAL_ALWAYS = 6, ///< Vertical scrollbar always on.
  234. BOTH_ALWAYS = 7 ///< Both scrollbars always on.
  235. };
  236. /**
  237. Returns the current scrollbar mode, see Fl_Browser_::has_scrollbar(uchar)
  238. */
  239. uchar has_scrollbar() const { return has_scrollbar_; }
  240. /**
  241. Sets whether the widget should have scrollbars or not (default Fl_Browser_::BOTH).
  242. By default you can scroll in both directions, and the scrollbars
  243. disappear if the data will fit in the widget.
  244. has_scrollbar() changes this based on the value of \p mode:
  245. - 0 - No scrollbars.
  246. - Fl_Browser_::HORIZONTAL - Only a horizontal scrollbar.
  247. - Fl_Browser_::VERTICAL - Only a vertical scrollbar.
  248. - Fl_Browser_::BOTH - The default is both scrollbars.
  249. - Fl_Browser_::HORIZONTAL_ALWAYS - Horizontal scrollbar always on,
  250. vertical always off.
  251. - Fl_Browser_::VERTICAL_ALWAYS - Vertical scrollbar always on,
  252. horizontal always off.
  253. - Fl_Browser_::BOTH_ALWAYS - Both always on.
  254. */
  255. void has_scrollbar(uchar mode) { has_scrollbar_ = mode; }
  256. /**
  257. Gets the default text font for the lines in the browser.
  258. \see textfont(), textsize(), textcolor()
  259. */
  260. Fl_Font textfont() const { return textfont_; }
  261. /**
  262. Sets the default text font for the lines in the browser to \p font.
  263. */
  264. void textfont(Fl_Font font) { textfont_ = font; }
  265. /**
  266. Gets the default text size (in pixels) for the lines in the browser.
  267. */
  268. Fl_Fontsize textsize() const { return textsize_; }
  269. /**
  270. Sets the default text size (in pixels) for the lines in the browser to \p size.
  271. */
  272. void textsize(Fl_Fontsize newSize) { textsize_ = newSize; }
  273. /**
  274. Gets the default text color for the lines in the browser.
  275. */
  276. Fl_Color textcolor() const { return textcolor_; }
  277. /**
  278. Sets the default text color for the lines in the browser to color \p col.
  279. */
  280. void textcolor(Fl_Color col) { textcolor_ = col; }
  281. /**
  282. Gets the current size of the scrollbars' troughs, in pixels.
  283. If this value is zero (default), this widget will use the
  284. Fl::scrollbar_size() value as the scrollbar's width.
  285. \returns Scrollbar size in pixels, or 0 if the global Fl::scrollbar_size() is being used.
  286. \see Fl::scrollbar_size(int)
  287. */
  288. int scrollbar_size() const {
  289. return(scrollbar_size_);
  290. }
  291. /**
  292. Sets the pixel size of the scrollbars' troughs to \p newSize, in pixels.
  293. Normally you should not need this method, and should use
  294. Fl::scrollbar_size(int) instead to manage the size of ALL
  295. your widgets' scrollbars. This ensures your application
  296. has a consistent UI, is the default behavior, and is normally
  297. what you want.
  298. Only use THIS method if you really need to override the global
  299. scrollbar size. The need for this should be rare.
  300. Setting \p newSize to the special value of 0 causes the widget to
  301. track the global Fl::scrollbar_size(), which is the default.
  302. \param[in] newSize Sets the scrollbar size in pixels.\n
  303. If 0 (default), scrollbar size tracks the global Fl::scrollbar_size()
  304. \see Fl::scrollbar_size()
  305. */
  306. void scrollbar_size(int newSize) {
  307. scrollbar_size_ = newSize;
  308. }
  309. /**
  310. This method has been deprecated, existing for backwards compatibility only.
  311. Use scrollbar_size() instead.
  312. This method always returns the global value Fl::scrollbar_size().
  313. \returns Always returns the global value Fl::scrollbar_size().
  314. \todo This method should eventually be removed in 1.4+
  315. */
  316. int scrollbar_width() const {
  317. return(Fl::scrollbar_size());
  318. }
  319. /**
  320. This method has been deprecated, existing for backwards compatibility only.
  321. Use scrollbar_size(int) instead.
  322. This method sets the global Fl::scrollbar_size(), and forces this
  323. instance of the widget to use it.
  324. \todo This method should eventually be removed in 1.4+
  325. */
  326. void scrollbar_width(int width) {
  327. Fl::scrollbar_size(width);
  328. scrollbar_size_ = 0;
  329. }
  330. /**
  331. Moves the vertical scrollbar to the righthand side of the list.
  332. For back compatibility.
  333. */
  334. void scrollbar_right() { scrollbar.align(FL_ALIGN_RIGHT); }
  335. /**
  336. Moves the vertical scrollbar to the lefthand side of the list.
  337. For back compatibility.
  338. */
  339. void scrollbar_left() { scrollbar.align(FL_ALIGN_LEFT); }
  340. void sort(int flags=0);
  341. DECLARE_CLASS_CHEAP_RTTI_2(Fl_Browser_, Fl_Group)
  342. };
  343. #endif
  344. //
  345. // End of "$Id: Fl_Browser_.H 12459 2017-09-14 00:50:36Z greg.ercolano $".
  346. //