inputline.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #ifndef BDE_INPUTLINE_H
  17. #define BDE_INPUTLINE_H
  18. #include <vector>
  19. #include "editbox.h"
  20. class InputLine : public EditBox {
  21. public:
  22. typedef std::vector<unistring> StringArray;
  23. // What kind of filenames, if any, to complete?
  24. enum CompleteType {
  25. cmpltOff, // turn off filename completion.
  26. cmpltAll,
  27. cmpltDirectories // complete only directory names.
  28. // this is useful for, e.g., a "Change Dir:"
  29. // input line.
  30. };
  31. private:
  32. unistring vis_label;
  33. direction_t label_dir;
  34. int label_width;
  35. // Filename completion variables:
  36. CompleteType complete_type;
  37. StringArray files_list;
  38. u8string files_directory; // the directory we're listing
  39. int event_num;
  40. int last_tab_event_num;
  41. int insertion_pos;
  42. int prefix_len;
  43. int slice_begin;
  44. int slice_end;
  45. int curr_choice;
  46. /*
  47. A little example can help in explaining what each completion
  48. variable means.
  49. Let's suppose our input-line looks like:
  50. ~/documents/no_ -cp862
  51. ^-----------cursor
  52. We press TAB and a "TAB session" starts:
  53. 0. "no" is the partial filename component and "~/documents/" is the
  54. directory name component.
  55. 1. files_list is populated with all filenames in "~/documents":
  56. 0. archive.txt
  57. 1. notes.txt
  58. 2. notes2.txt
  59. 3. november.txt
  60. 4. questions.txt
  61. 2. only entries 1 to 3 are relevant in our TAB session (because
  62. they start in "no"), so slice_begin is set to 1 and slice_end
  63. to 3.
  64. 3. insertion_pos is set to the cursor position.
  65. 4. prefix_len is set to the length of the partial filename
  66. component: 2. (that is, strlen("no")).
  67. 5. While moving forward and backward in files_list (as a result
  68. of pressing TAB and M-TAB), curr_choice is updated to point
  69. to the current entry.
  70. */
  71. // History variables:
  72. StringArray *history;
  73. int history_idx;
  74. protected:
  75. // Filename completion
  76. void init_completion();
  77. void get_directory_files(u8string directory, const char *prefix = NULL);
  78. void complete(bool forward);
  79. // History
  80. void init_history(int history_set);
  81. void update_history();
  82. ////
  83. void trim();
  84. protected:
  85. virtual void do_syntax_highlight(const unistring &str,
  86. AttributeArray &attributes, int para_num);
  87. public:
  88. HAS_ACTIONS_MAP(InputLine, EditBox);
  89. HAS_BINDINGS_MAP(InputLine, EditBox);
  90. InputLine(const char *aLabel, const unistring &default_text,
  91. int history_set = 0, CompleteType complete = cmpltOff);
  92. void set_label(const char *aLabel);
  93. void set_text(const unistring &s);
  94. unistring get_text() { return curr_para()->str; }
  95. INTERACTIVE void end_modal();
  96. INTERACTIVE void next_completion();
  97. INTERACTIVE void previous_completion();
  98. INTERACTIVE void next_history();
  99. INTERACTIVE void previous_history();
  100. virtual void redraw_paragraph(Paragraph &p,
  101. int window_start_line, bool only_cursor, int);
  102. virtual bool handle_event(const Event &evt);
  103. };
  104. #endif