Fl_Help_View.H 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //
  2. // "$Id: Fl_Help_View.H 9126 2011-10-04 13:10:55Z manolo $"
  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. Distribution and use rights are outlined in
  10. // the file "COPYING" which should have been included with this file. If this
  11. // file is missing or damaged, see the license at:
  12. //
  13. // http://www.fltk.org/COPYING.php
  14. //
  15. // Please report all bugs and problems on the following page:
  16. //
  17. // http://www.fltk.org/str.php
  18. //
  19. // Inline:
  20. //
  21. // Fl_Help_View::directory() - Get the directory string.
  22. // Fl_Help_View::filename() - Get the filename string.
  23. // Fl_Help_View::link() - Set the link callback.
  24. // Fl_Help_View::size() - Get the total document length.
  25. // Fl_Help_View::size() - Set the document width and height.
  26. // Fl_Help_View::textcolor() - Set the default text color.
  27. // Fl_Help_View::textcolor() - Get the default text color.
  28. // Fl_Help_View::textsize() - Set the default font size.
  29. // Fl_Help_View::textsize() - Get the default font size.
  30. // Fl_Help_View::title() - Get the title string.
  31. // Fl_Help_View::topline() - Get the top line in document.
  32. // Fl_Help_View::leftline() - Get the left line position.
  33. // Fl_Help_View::value() - Get the HTML text value.
  34. // Fl_Help_View::monofont() - Set the default mono font.
  35. // Fl_Help_View::monofont() - Get the default mono font.
  36. // Fl_Help_View::reformat() - Format the help text.
  37. // Fl_Help_View::sansfont() - Set the default sans font.
  38. // Fl_Help_View::sansfont() - Get the default sans font.
  39. // Fl_Help_View::serifont() - Set the default serif font.
  40. // Fl_Help_View::serifont() - Get the default serif font.
  41. //
  42. #ifndef Fl_Help_View_H
  43. # define Fl_Help_View_H
  44. //
  45. // Include necessary header files...
  46. //
  47. # include <stdio.h>
  48. # include <FL/Fl.H>
  49. # include <FL/Fl_Group.H>
  50. # include <FL/Fl_Scrollbar.H>
  51. # include <FL/fl_draw.H>
  52. # include <FL/Fl_Shared_Image.H>
  53. # include <FL/filename.H>
  54. //
  55. // Fl_Help_Func type - link callback function for files...
  56. //
  57. typedef const char *(Fl_Help_Func)(Fl_Widget *, const char *, const char *);
  58. //
  59. // Fl_Help_Block structure
  60. //
  61. // note: we can change private structs accessed by pointers
  62. struct Fl_Help_Block
  63. {
  64. const char *start, // Start of text
  65. *end; // End of text
  66. unsigned char border; // Draw border?
  67. Fl_Color border_color; // Border color
  68. Fl_Color bgcolor; // Background color
  69. int x, // Indentation/starting X coordinate
  70. y, // Starting Y coordinate
  71. w, // Width
  72. h; // Height
  73. // new fields, removed int line[32];
  74. unsigned char fsize; // current font size
  75. unsigned char font; // current font
  76. int maxh, // max image height
  77. imgy, // image y position
  78. pre, // pre text flag
  79. type, // blockquote/ol/ul/li type parameter
  80. tag, // tag/element fourcc int
  81. line, // Left starting position for each line
  82. cbi; // current block index
  83. };
  84. //
  85. // Fl_Help_Link structure
  86. //
  87. struct Fl_Help_Link
  88. {
  89. char filename[192], // Link reference filename
  90. name[32]; // Link target name, blank if none
  91. int x, // X offset of link text
  92. y, // Y offset of link text
  93. w, // Width of link text
  94. h; // Height of link text
  95. };
  96. //
  97. // Fl_Help_Target structure
  98. //
  99. // note: hijacking this lets us add new data members via the d-pointer
  100. struct Fl_Help_Target
  101. {
  102. // new fields, removed char name[32]; int y;
  103. Fl_Help_Link *targets, // Targets
  104. *linkp; // Currently clicked link
  105. unsigned char ispush, // link is pushed
  106. islink, // link clicked
  107. resized, // window resized
  108. ispath, // is path used
  109. nstyle, // navigation style flag
  110. isnew, // is new page
  111. pad7, //
  112. pad8; //
  113. int top, // current topline
  114. ltop, // last topline
  115. isnav, // is nav link
  116. rwidth, // resize width
  117. cssurllen, // css url length
  118. csswordlen, // css word length
  119. *cssword; // css word value
  120. long rtime, // resize time
  121. rsec, // resize seconds
  122. rmil, // resize millisecs
  123. csstextlen; // css text length
  124. char *csstext, // css text value
  125. *cssurl, // css url value
  126. path[1024], // current file path
  127. lpath[1024]; // last file path
  128. int nfonts, // number of fonts in stack
  129. fonts[100][2]; // font stack
  130. };
  131. //
  132. // Fl_Help_View class...
  133. //
  134. class FL_EXPORT Fl_Help_View : public Fl_Group // Help viewer widget
  135. {
  136. private:
  137. enum { RIGHT = -1, CENTER, LEFT }; // Alignments
  138. char title_[1024]; // Title string
  139. Fl_Color defcolor_, // Default text color
  140. bgcolor_, // Background color
  141. textcolor_, // Text color
  142. linkcolor_; // Link color
  143. unsigned char textfont_, // Default font - replaced
  144. textsize_; // Default font size - replaced
  145. const char *value_; // HTML text value
  146. int nblocks_, // Number of blocks/lines
  147. ablocks_; // Allocated blocks
  148. Fl_Help_Block *blocks_; // Blocks
  149. Fl_Help_Func *link_; // Link transform function
  150. int nlinks_, // Number of links
  151. alinks_; // Allocated links
  152. Fl_Help_Link *links_; // Links
  153. int ntargets_, // Number of targets
  154. atargets_; // Allocated targets
  155. Fl_Help_Target *d; // d-pointer struct - was *targets_
  156. // note: we can rename existing data members
  157. char directory_[FL_PATH_MAX], // Directory for current file
  158. filename_[FL_PATH_MAX]; // Current filename
  159. int topline_, // Top line in document
  160. leftline_, // Left line position
  161. size_, // Total document length
  162. hsize_, // Maximum document width
  163. scrollbar_size_; ///< Size for both scrollbars
  164. Fl_Scrollbar scrollbar_, // Vertical scrollbar for document
  165. hscrollbar_; // Horizontal scrollbar
  166. // Text selection
  167. Fl_Offscreen Fl_Help_View_buffer;
  168. int selection_first; // Text selection
  169. int selection_last;
  170. int selection_push_first;
  171. int selection_push_last;
  172. int selection_drag_first;
  173. int selection_drag_last;
  174. int selected;
  175. int draw_mode;
  176. int mouse_x;
  177. int mouse_y;
  178. int current_pos;
  179. Fl_Help_View *current_view;
  180. Fl_Color hv_selection_color;
  181. Fl_Color hv_selection_text_color;
  182. // new private static class variables
  183. int serifont_; // default serif font
  184. int sansfont_; // default sans font
  185. int monofont_; // default monospace font
  186. unsigned char fontsize_; // default font size
  187. short face_[250][4]; // font face table [m,b,i,p]
  188. unsigned char flet_[30]; // first face for letter table
  189. unsigned char fref_[1000]; // face reference table
  190. int default_margin_; //internall padding
  191. int tr_td_adjust_; //when printint with pdfs
  192. Fl_Help_Block *add_block(const char *sp, int xx, int yy, int ww, int hh, unsigned char bc = 0);
  193. void add_link(const char *np, int xx, int yy, int ww, int hh);
  194. void add_target(const char *np, int yy);
  195. static int compare_targets(const Fl_Help_Target *t0, const Fl_Help_Target *t1);
  196. int do_align(Fl_Help_Block *b, int li, int xx, int ca, int &sl);
  197. void draw();
  198. void format();
  199. void format_table(int *tw, int *maxcols, const char *tp);
  200. void free_data();
  201. int get_align(const char *ap, int da);
  202. const char *get_attr(const char *ap, const char *np, char *buf, int sb);
  203. Fl_Color get_color(const char *np, Fl_Color dc);
  204. Fl_Shared_Image *get_image(const char *np, int iw, int ih);
  205. int get_length(const char *lp);
  206. //int handle(int event); // moved to public
  207. // note: you can remove private non-virtual functions if not called by inline functions
  208. void initfont(unsigned char &fi, unsigned char &fs) { // Reset stack - replaced, for when nfonts > 255
  209. d->nfonts = 0;
  210. fl_font(fi = d->fonts[0][0] = serifont_, fs = d->fonts[0][1] = fontsize_);
  211. }
  212. void pushfont(unsigned char fi, unsigned char fs) { // Push font - replaced
  213. if (d->nfonts < 99) d->nfonts ++;
  214. fl_font(d->fonts[d->nfonts][0] = fi, d->fonts[d->nfonts][1] = fs);
  215. }
  216. void popfont(unsigned char &fi, unsigned char &fs) { // Pop font - replaced
  217. if (d->nfonts > 0) d->nfonts --;
  218. fl_font(fi = d->fonts[d->nfonts][0], fs = d->fonts[d->nfonts][1]);
  219. }
  220. void hv_draw(const char *tp, int xx, int yy);
  221. char begin_selection();
  222. char extend_selection();
  223. void end_selection(int cb = 0);
  224. void clear_global_selection();
  225. Fl_Help_Link *find_link(int xx, int yy);
  226. void follow_link(Fl_Help_Link *lp);
  227. // new private functions
  228. // note: for BC we can't modify existing function declarations
  229. Fl_Help_Block *add_block(const Fl_Help_Block &b, const char *sp, int ww);
  230. unsigned char build_faces();
  231. static int cmp_targets(const Fl_Help_Link *t0, const Fl_Help_Link *t1);
  232. const char *format_table(int &tw, int *maxcols, const char *tp, int xx, int rc);
  233. int get_css_value(const char *sp, const char *pp, char *vp);
  234. int get_font_size(const char *hp);
  235. int get_length(const char *lp, int hw);
  236. int load_css(const char *fp);
  237. void parse_css(Fl_Help_Block &b, const char *sp, char *buf);
  238. void popfont(int &fi, unsigned char &fs);
  239. void pushfont(int fi, unsigned char fs);
  240. public:
  241. Fl_Help_View(int xx, int yy, int ww, int hh, const char *lp = 0);
  242. ~Fl_Help_View();
  243. const char *directory() const { // Get the directory string
  244. if (directory_[0]) return directory_;
  245. else return (const char *)0;
  246. }
  247. const char *filename() const { // Get the filename string
  248. if (filename_[0]) return filename_;
  249. else return (const char *)0;
  250. }
  251. int find(const char *sp, int pos = 0);
  252. /**
  253. This method assigns a callback function to use when a link is
  254. followed or a file is loaded (via Fl_Help_View::load()) that
  255. requires a different file or path.
  256. The callback function receives a pointer to the Fl_Help_View
  257. widget and the URI or full pathname for the file in question.
  258. It must return a pathname that can be opened as a local file or NULL:
  259. \code
  260. const char *fn(Fl_Widget *w, const char *uri);
  261. \endcode
  262. The link function can be used to retrieve remote or virtual
  263. documents, returning a temporary file that contains the actual
  264. data. If the link function returns NULL, the value of
  265. the Fl_Help_View widget will remain unchanged.
  266. If the link callback cannot handle the URI scheme, it should
  267. return the uri value unchanged or set the value() of the widget
  268. before returning NULL.
  269. */
  270. void link(Fl_Help_Func *fn) { // Set the link callback
  271. link_ = fn;
  272. }
  273. int load(const char *fp);
  274. void resize(int xx, int yy, int ww, int hh);
  275. int size() const { // Get the total document length
  276. return size_;
  277. }
  278. void size(int ww, int hh) { // Set the document width and height
  279. Fl_Widget::size(ww, hh);
  280. }
  281. void textcolor(Fl_Color tc) { // Set the default text color
  282. if (textcolor_ == defcolor_) textcolor_ = tc;
  283. defcolor_ = tc;
  284. }
  285. Fl_Color textcolor() const { // Get the default text color
  286. return defcolor_;
  287. }
  288. void textfont(unsigned char fi) { // Set the text font, obsolete
  289. textfont_ = fi;
  290. //format(); // removed
  291. // note: we can remove the contents of inline functions
  292. }
  293. Fl_Font textfont() const { // Get the text font, obsolete
  294. return textfont_;
  295. }
  296. void textsize(unsigned char fs) { // Set the default font size
  297. fontsize_ = fs;
  298. //format(); // removed
  299. }
  300. Fl_Fontsize textsize() const { // Get the default font size
  301. return fontsize_;
  302. }
  303. const char *title() { // Get the title string
  304. return title_;
  305. }
  306. void topline(const char *np);
  307. void topline(int yy);
  308. int topline() const { // Get the top line in document
  309. return topline_;
  310. }
  311. void leftline(int xx);
  312. int leftline() const { // Get the left line position
  313. return leftline_;
  314. }
  315. void value(const char *tp);
  316. const char *value() const { // Get the HTML text value
  317. return value_;
  318. }
  319. void clear_selection();
  320. void select_all();
  321. /**
  322. Gets the current size of the scrollbars' troughs, in pixels.
  323. If this value is zero (default), this widget will use the
  324. Fl::scrollbar_size() value as the scrollbar's width.
  325. \returns Scrollbar size in pixels, or 0 if the global Fl::scrollbar_size() is being used.
  326. \see Fl::scrollbar_size(int)
  327. */
  328. int scrollbar_size() const {
  329. return(scrollbar_size_);
  330. }
  331. /**
  332. Sets the pixel size of the scrollbars' troughs to the \p size, in pixels.
  333. Normally you should not need this method, and should use
  334. Fl::scrollbar_size(int) instead to manage the size of ALL
  335. your widgets' scrollbars. This ensures your application
  336. has a consistent UI, is the default behavior, and is normally
  337. what you want.
  338. Only use THIS method if you really need to override the global
  339. scrollbar size. The need for this should be rare.
  340. Setting \p size to the special value of 0 causes the widget to
  341. track the global Fl::scrollbar_size(), which is the default.
  342. \param[in] size Sets the scrollbar size in pixels.\n
  343. If 0 (default), scrollbar size tracks the global Fl::scrollbar_size()
  344. \see Fl::scrollbar_size()
  345. */
  346. void scrollbar_size(int size) {
  347. scrollbar_size_ = size;
  348. }
  349. // new public functions
  350. int fileislink();
  351. void filepath(const char *fp);
  352. char *filepath();
  353. int font_face(const char *sp);
  354. int font_style(int fi, unsigned char fs);
  355. int gettopline();
  356. int handle(int event); // moved from private
  357. void monofont(int fi) { // set the default mono font
  358. monofont_ = fi;
  359. }
  360. int monofont() const { // get the default mono font
  361. return monofont_;
  362. }
  363. void reformat() { // format the help text - wrapper function
  364. format();
  365. }
  366. void sansfont(int fi) { // set the default sans font
  367. sansfont_ = fi;
  368. }
  369. int sansfont() const { // get the default sans font
  370. return sansfont_;
  371. }
  372. void serifont(int fi) { // set the default serif font
  373. serifont_ = fi;
  374. }
  375. int serifont() const { // get the default serif font
  376. return serifont_;
  377. }
  378. void setstyle(int flag);
  379. void default_margin(int value) { // set the default margin
  380. default_margin_ = value;
  381. }
  382. int default_margin() const { // get the default margin
  383. return default_margin_;
  384. }
  385. void tr_td_adjust(int value) { // set the pdf print adjust
  386. tr_td_adjust_ = value;
  387. }
  388. int tr_td_adjust() const { // get the pdf print adjust
  389. return tr_td_adjust_;
  390. }
  391. DECLARE_CLASS_CHEAP_RTTI_2(Fl_Help_View, Fl_Group)
  392. };
  393. #endif // !Fl_Help_View_H
  394. //
  395. // End of "$Id: Fl_Help_View.H 5991 2007-12-15 16:08:23Z mike $".
  396. //