help-view.nut 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2013 by Domingo Alvarez Duarte <[email protected]>
  3. *
  4. * Licensed under GPLv3, see http://www.gnu.org/licenses/gpl.html.
  5. */
  6. class OurHelpWindow extends HelpWindow
  7. {
  8. _help_file_name = null;
  9. _navigation = null;
  10. _last_found_pos = null;
  11. _help_text_buffer = null;
  12. constructor() {
  13. base.constructor();
  14. _help_file_name = "help.txt";
  15. _last_found_pos = 0;
  16. _navigation = [];
  17. help_text->wrap_mode(1, 0);
  18. _help_text_buffer = help_text->buffer();
  19. view_html->textsize(view_html->labelsize());
  20. view_html->link(hlp_link);
  21. view_html->scrollbar_size(fl_width("W").tointeger());
  22. btnSavehelp.callback(save_help_file);
  23. btnSearchHelp.callback(on_search);
  24. words_to_search.callback(on_search);
  25. btnFontSmaller.callback(on_font_size);
  26. btnFontBigger.callback(on_font_size);
  27. btnBackward.callback(on_navigate);
  28. btnTop.callback(on_navigate);
  29. btnForward.callback(on_navigate);
  30. tabs.callback(on_tab_change);
  31. }
  32. function hlp_link(sender, uri, anchor){
  33. this = sender->window();
  34. if(uri.startswith("db:/")){
  35. printf("%s\n", uri);
  36. return null;
  37. }
  38. _navigation.push(sender->topline());
  39. return uri;
  40. }
  41. function refresh_html() {
  42. local html = markdown2html(_help_text_buffer->text(), _help_text_buffer->length());
  43. view_html->value(html);
  44. //printf(html.c_str());
  45. }
  46. function load_help_file(fn=null){
  47. fn = fn ? fn : _help_file_name;
  48. _help_text_buffer->loadfile(fn);
  49. _help_file_name = fn;
  50. refresh_html();
  51. }
  52. function save_help_file(sender, udata) {
  53. this = sender->window();
  54. _help_text_buffer->savefile(_help_file_name);
  55. btnSavehelp->deactivate();
  56. }
  57. function on_search(sender, udata){
  58. this = sender->window();
  59. search_help_file(words_to_search->value());
  60. }
  61. function search_help_file(topic, fromStart=false) {
  62. words_to_search->value(topic);
  63. if(!topic) return;
  64. if(!_help_text_buffer->length()) load_help_file();
  65. if(fromStart || words_to_search->changed2()){
  66. _last_found_pos =0;
  67. words_to_search->clear_changed2();
  68. }
  69. local foundPos = -1;
  70. if(tabs->value() == tabView){
  71. foundPos = view_html->find(topic, _last_found_pos);
  72. if(foundPos >= 0) _last_found_pos = foundPos;
  73. else foundPos = 0;
  74. view_html->redraw();
  75. }
  76. else
  77. {
  78. if((foundPos = _help_text_buffer->search_forward(_last_found_pos, topic)) >= 0){
  79. _help_text_buffer->select(foundPos, foundPos+topic.len());
  80. help_text->insert_position(foundPos);
  81. help_text->show_insert_position();
  82. _last_found_pos = foundPos+1;
  83. //delayed_focus(help_text);
  84. }
  85. }
  86. if(foundPos < 0){
  87. _last_found_pos = 0;
  88. fl_alert(format(_tr("Nothing found for '%s' !"), topic));
  89. }
  90. }
  91. function on_font_size(sender, udata){
  92. this = sender->window(); //resolve vars on this window class
  93. local fsize = view_html->textsize();
  94. if(sender == btnFontSmaller){
  95. if (fsize > 10){
  96. view_html->textsize(fsize - 2);
  97. view_html->reformat();
  98. }
  99. if (view_html->textsize() <= 10) btnFontSmaller->deactivate();
  100. btnFontBigger->activate();
  101. }
  102. else
  103. {
  104. if (fsize <= 24){
  105. view_html->textsize(fsize + 2);
  106. view_html->reformat();
  107. }
  108. if (view_html->textsize() > 24) btnFontBigger->deactivate();
  109. btnFontSmaller->activate();
  110. }
  111. }
  112. function on_navigate(sender, udata){
  113. this = sender->window();
  114. if(sender == btnBackward && !_navigation.isempty() )
  115. {
  116. view_html->topline(_navigation.top());
  117. _navigation.pop();
  118. }
  119. else if(sender == btnTop)
  120. {
  121. view_html->topline(0);
  122. }
  123. }
  124. function on_tab_change(sender, udata){
  125. this = sender->window();
  126. if(tabs->value() == tabView){
  127. if(help_text->changed2()){
  128. refresh_html();
  129. help_text->clear_changed2();
  130. btnSavehelp->activate();
  131. }
  132. }
  133. }
  134. };