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.

397 lines
13KB

  1. //
  2. // "$Id: Fl_Help_View.H 8306 2011-01-24 17:04:22Z matt $"
  3. //
  4. // Help Viewer widget definitions.
  5. //
  6. // Copyright 1997-2010 by Easy Software Products.
  7. // Image support by Matthias Melcher, Copyright 2000-2009.
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Library General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // Library General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU Library General Public
  20. // License along with this library; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. // USA.
  23. //
  24. // Please report all bugs and problems on the following page:
  25. //
  26. // http://www.fltk.org/str.php
  27. //
  28. /* \file
  29. Fl_Help_View widget . */
  30. #ifndef Fl_Help_View_H
  31. # define Fl_Help_View_H
  32. //
  33. // Include necessary header files...
  34. //
  35. # include <stdio.h>
  36. # include "Fl.H"
  37. # include "Fl_Group.H"
  38. # include "Fl_Scrollbar.H"
  39. # include "fl_draw.H"
  40. # include "Fl_Shared_Image.H"
  41. # include "filename.H"
  42. //
  43. // Fl_Help_Func type - link callback function for files...
  44. //
  45. typedef const char *(Fl_Help_Func)(Fl_Widget *, const char *);
  46. //
  47. // Fl_Help_Block structure...
  48. //
  49. struct Fl_Help_Block {
  50. const char *start, // Start of text
  51. *end; // End of text
  52. uchar border; // Draw border?
  53. Fl_Color bgcolor; // Background color
  54. int x, // Indentation/starting X coordinate
  55. y, // Starting Y coordinate
  56. w, // Width
  57. h; // Height
  58. int line[32]; // Left starting position for each line
  59. };
  60. //
  61. // Fl_Help_Link structure...
  62. //
  63. /** Definition of a link for the html viewer. */
  64. struct Fl_Help_Link {
  65. char filename[192], ///< Reference filename
  66. name[32]; ///< Link target (blank if none)
  67. int x, ///< X offset of link text
  68. y, ///< Y offset of link text
  69. w, ///< Width of link text
  70. h; ///< Height of link text
  71. };
  72. /*
  73. * Fl_Help_View font stack opaque implementation
  74. */
  75. /** Fl_Help_View font stack element definition. */
  76. struct FL_EXPORT Fl_Help_Font_Style {
  77. Fl_Font f; ///< Font
  78. Fl_Fontsize s; ///< Font Size
  79. Fl_Color c; ///< Font Color
  80. void get(Fl_Font &afont, Fl_Fontsize &asize, Fl_Color &acolor) {afont=f; asize=s; acolor=c;} ///< Gets current font attributes
  81. void set(Fl_Font afont, Fl_Fontsize asize, Fl_Color acolor) {f=afont; s=asize; c=acolor;} ///< Sets current font attributes
  82. Fl_Help_Font_Style(Fl_Font afont, Fl_Fontsize asize, Fl_Color acolor) {set(afont, asize, acolor);}
  83. Fl_Help_Font_Style(){} // For in table use
  84. };
  85. /** Fl_Help_View font stack definition. */
  86. const size_t MAX_FL_HELP_FS_ELTS = 100;
  87. struct FL_EXPORT Fl_Help_Font_Stack {
  88. /** font stack construction, initialize attributes. */
  89. Fl_Help_Font_Stack() {
  90. nfonts_ = 0;
  91. }
  92. void init(Fl_Font f, Fl_Fontsize s, Fl_Color c) {
  93. nfonts_ = 0;
  94. elts_[nfonts_].set(f, s, c);
  95. fl_font(f, s);
  96. fl_color(c);
  97. }
  98. /** Gets the top (current) element on the stack. */
  99. void top(Fl_Font &f, Fl_Fontsize &s, Fl_Color &c) { elts_[nfonts_].get(f, s, c); }
  100. /** Pushes the font style triplet on the stack, also calls fl_font() & fl_color() adequately */
  101. void push(Fl_Font f, Fl_Fontsize s, Fl_Color c) {
  102. if (nfonts_ < MAX_FL_HELP_FS_ELTS-1) nfonts_ ++;
  103. elts_[nfonts_].set(f, s, c);
  104. fl_font(f, s); fl_color(c);
  105. }
  106. /** Pops from the stack the font style triplet and calls fl_font() & fl_color() adequately */
  107. void pop(Fl_Font &f, Fl_Fontsize &s, Fl_Color &c) {
  108. if (nfonts_ > 0) nfonts_ --;
  109. top(f, s, c);
  110. fl_font(f, s); fl_color(c);
  111. }
  112. /** Gets the current count of font style elements in the stack. */
  113. size_t count() const {return nfonts_;} // Gets the current number of fonts in the stack
  114. protected:
  115. size_t nfonts_; ///< current number of fonts in stack
  116. Fl_Help_Font_Style elts_[100]; ///< font elements
  117. };
  118. /** Fl_Help_Target structure */
  119. struct Fl_Help_Target {
  120. char name[32]; ///< Target name
  121. int y; ///< Y offset of target
  122. };
  123. /**
  124. The Fl_Help_View widget displays HTML text. Most HTML 2.0
  125. elements are supported, as well as a primitive implementation of tables.
  126. GIF, JPEG, and PNG images are displayed inline.
  127. Supported HTML tags:
  128. - A: HREF/NAME
  129. - B
  130. - BODY: BGCOLOR/TEXT/LINK
  131. - BR
  132. - CENTER
  133. - CODE
  134. - DD
  135. - DL
  136. - DT
  137. - EM
  138. - FONT: COLOR/SIZE/FACE=(helvetica/arial/sans/times/serif/symbol/courier)
  139. - H1/H2/H3/H4/H5/H6
  140. - HEAD
  141. - HR
  142. - I
  143. - IMG: SRC/WIDTH/HEIGHT/ALT
  144. - KBD
  145. - LI
  146. - OL
  147. - P
  148. - PRE
  149. - STRONG
  150. - TABLE: TH/TD/TR/BORDER/BGCOLOR/COLSPAN/ALIGN=CENTER|RIGHT|LEFT
  151. - TITLE
  152. - TT
  153. - U
  154. - UL
  155. - VAR
  156. Supported color names:
  157. - black,red,green,yellow,blue,magenta,fuchsia,cyan,aqua,white,gray,grey,lime,maroon,navy,olive,purple,silver,teal.
  158. Supported urls:
  159. - Internal: file:
  160. - External: http: ftp: https: ipp: mailto: news:
  161. Quoted char names:
  162. - Aacute aacute Acirc acirc acute AElig aelig Agrave agrave amp Aring aring Atilde atilde Auml auml
  163. - brvbar bull
  164. - Ccedil ccedil cedil cent copy curren
  165. - deg divide
  166. - Eacute eacute Ecirc ecirc Egrave egrave ETH eth Euml euml euro
  167. - frac12 frac14 frac34
  168. - gt
  169. - Iacute iacute Icirc icirc iexcl Igrave igrave iquest Iuml iuml
  170. - laquo lt
  171. - macr micro middot
  172. - nbsp not Ntilde ntilde
  173. - Oacute oacute Ocirc ocirc Ograve ograve ordf ordm Oslash oslash Otilde otilde Ouml ouml
  174. - para premil plusmn pound
  175. - quot
  176. - raquo reg
  177. - sect shy sup1 sup2 sup3 szlig
  178. - THORN thorn times trade
  179. - Uacute uacute Ucirc ucirc Ugrave ugrave uml Uuml uuml
  180. - Yacute yacute
  181. - yen Yuml yuml
  182. */
  183. class FL_EXPORT Fl_Help_View : public Fl_Group { // Help viewer widget
  184. enum { RIGHT = -1, CENTER, LEFT }; ///< Alignments
  185. char title_[1024]; ///< Title string
  186. Fl_Color defcolor_, ///< Default text color
  187. bgcolor_, ///< Background color
  188. textcolor_, ///< Text color
  189. linkcolor_; ///< Link color
  190. Fl_Font textfont_; ///< Default font for text
  191. Fl_Fontsize textsize_; ///< Default font size
  192. const char *value_; ///< HTML text value
  193. Fl_Help_Font_Stack fstack_; ///< font stack management
  194. int nblocks_, ///< Number of blocks/paragraphs
  195. ablocks_; ///< Allocated blocks
  196. Fl_Help_Block *blocks_; ///< Blocks
  197. Fl_Help_Func *link_; ///< Link transform function
  198. int nlinks_, ///< Number of links
  199. alinks_; ///< Allocated links
  200. Fl_Help_Link *links_; ///< Links
  201. int ntargets_, ///< Number of targets
  202. atargets_; ///< Allocated targets
  203. Fl_Help_Target *targets_; ///< Targets
  204. char directory_[FL_PATH_MAX];///< Directory for current file
  205. char filename_[FL_PATH_MAX]; ///< Current filename
  206. int topline_, ///< Top line in document
  207. leftline_, ///< Lefthand position
  208. size_, ///< Total document length
  209. hsize_, ///< Maximum document width
  210. scrollbar_size_; ///< Size for both scrollbars
  211. Fl_Scrollbar scrollbar_, ///< Vertical scrollbar for document
  212. hscrollbar_; ///< Horizontal scrollbar
  213. static int selection_first;
  214. static int selection_last;
  215. static int selection_push_first;
  216. static int selection_push_last;
  217. static int selection_drag_first;
  218. static int selection_drag_last;
  219. static int selected;
  220. static int draw_mode;
  221. static int mouse_x;
  222. static int mouse_y;
  223. static int current_pos;
  224. static Fl_Help_View *current_view;
  225. static Fl_Color hv_selection_color;
  226. static Fl_Color hv_selection_text_color;
  227. void initfont(Fl_Font &f, Fl_Fontsize &s, Fl_Color &c) { f = textfont_; s = textsize_; c = textcolor_; fstack_.init(f, s, c); }
  228. void pushfont(Fl_Font f, Fl_Fontsize s) {fstack_.push(f, s, textcolor_);}
  229. void pushfont(Fl_Font f, Fl_Fontsize s, Fl_Color c) {fstack_.push(f, s, c);}
  230. void popfont(Fl_Font &f, Fl_Fontsize &s, Fl_Color &c) {fstack_.pop(f, s, c);}
  231. Fl_Help_Block *add_block(const char *s, int xx, int yy, int ww, int hh, uchar border = 0);
  232. void add_link(const char *n, int xx, int yy, int ww, int hh);
  233. void add_target(const char *n, int yy);
  234. static int compare_targets(const Fl_Help_Target *t0, const Fl_Help_Target *t1);
  235. int do_align(Fl_Help_Block *block, int line, int xx, int a, int &l);
  236. void draw();
  237. void format();
  238. void format_table(int *table_width, int *columns, const char *table);
  239. void free_data();
  240. int get_align(const char *p, int a);
  241. const char *get_attr(const char *p, const char *n, char *buf, int bufsize);
  242. Fl_Color get_color(const char *n, Fl_Color c);
  243. Fl_Shared_Image *get_image(const char *name, int W, int H);
  244. int get_length(const char *l);
  245. int handle(int);
  246. void hv_draw(const char *t, int x, int y);
  247. char begin_selection();
  248. char extend_selection();
  249. void end_selection(int c=0);
  250. void clear_global_selection();
  251. Fl_Help_Link *find_link(int, int);
  252. void follow_link(Fl_Help_Link*);
  253. public:
  254. Fl_Help_View(int xx, int yy, int ww, int hh, const char *l = 0);
  255. ~Fl_Help_View();
  256. /** Returns the current directory for the text in the buffer. */
  257. const char *directory() const { if (directory_[0]) return (directory_);
  258. else return ((const char *)0); }
  259. /** Returns the current filename for the text in the buffer. */
  260. const char *filename() const { if (filename_[0]) return (filename_);
  261. else return ((const char *)0); }
  262. int find(const char *s, int p = 0);
  263. /**
  264. This method assigns a callback function to use when a link is
  265. followed or a file is loaded (via Fl_Help_View::load()) that
  266. requires a different file or path.
  267. The callback function receives a pointer to the Fl_Help_View
  268. widget and the URI or full pathname for the file in question.
  269. It must return a pathname that can be opened as a local file or NULL:
  270. \code
  271. const char *fn(Fl_Widget *w, const char *uri);
  272. \endcode
  273. The link function can be used to retrieve remote or virtual
  274. documents, returning a temporary file that contains the actual
  275. data. If the link function returns NULL, the value of
  276. the Fl_Help_View widget will remain unchanged.
  277. If the link callback cannot handle the URI scheme, it should
  278. return the uri value unchanged or set the value() of the widget
  279. before returning NULL.
  280. */
  281. void link(Fl_Help_Func *fn) { link_ = fn; }
  282. int load(const char *f);
  283. void resize(int,int,int,int);
  284. /** Gets the size of the help view. */
  285. int size() const { return (size_); }
  286. void size(int W, int H) { Fl_Widget::size(W, H); }
  287. /** Sets the default text color. */
  288. void textcolor(Fl_Color c) { if (textcolor_ == defcolor_) textcolor_ = c; defcolor_ = c; }
  289. /** Returns the current default text color. */
  290. Fl_Color textcolor() const { return (defcolor_); }
  291. /** Sets the default text font. */
  292. void textfont(Fl_Font f) { textfont_ = f; format(); }
  293. /** Returns the current default text font. */
  294. Fl_Font textfont() const { return (textfont_); }
  295. /** Sets the default text size. */
  296. void textsize(Fl_Fontsize s) { textsize_ = s; format(); }
  297. /** Gets the default text size. */
  298. Fl_Fontsize textsize() const { return (textsize_); }
  299. /** Returns the current document title, or NULL if there is no title. */
  300. const char *title() { return (title_); }
  301. void topline(const char *n);
  302. void topline(int);
  303. /** Returns the current top line in pixels. */
  304. int topline() const { return (topline_); }
  305. void leftline(int);
  306. /** Gets the left position in pixels. */
  307. int leftline() const { return (leftline_); }
  308. void value(const char *val);
  309. /** Returns the current buffer contents. */
  310. const char *value() const { return (value_); }
  311. void clear_selection();
  312. void select_all();
  313. /**
  314. Gets the current size of the scrollbars' troughs, in pixels.
  315. If this value is zero (default), this widget will use the
  316. Fl::scrollbar_size() value as the scrollbar's width.
  317. \returns Scrollbar size in pixels, or 0 if the global Fl::scrollbar_size() is being used.
  318. \see Fl::scrollbar_size(int)
  319. */
  320. int scrollbar_size() const {
  321. return(scrollbar_size_);
  322. }
  323. /**
  324. Sets the pixel size of the scrollbars' troughs to the \p size, in pixels.
  325. Normally you should not need this method, and should use
  326. Fl::scrollbar_size(int) instead to manage the size of ALL
  327. your widgets' scrollbars. This ensures your application
  328. has a consistent UI, is the default behavior, and is normally
  329. what you want.
  330. Only use THIS method if you really need to override the global
  331. scrollbar size. The need for this should be rare.
  332. Setting \p size to the special value of 0 causes the widget to
  333. track the global Fl::scrollbar_size(), which is the default.
  334. \param[in] size Sets the scrollbar size in pixels.\n
  335. If 0 (default), scrollbar size tracks the global Fl::scrollbar_size()
  336. \see Fl::scrollbar_size()
  337. */
  338. void scrollbar_size(int size) {
  339. scrollbar_size_ = size;
  340. }
  341. };
  342. #endif // !Fl_Help_View_H
  343. //
  344. // End of "$Id: Fl_Help_View.H 8306 2011-01-24 17:04:22Z matt $".
  345. //