Fl_Text_Editor.H 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // "$Id: Fl_Text_Editor.H 8864 2011-07-19 04:49:30Z greg.ercolano $"
  3. //
  4. // Header file for Fl_Text_Editor class.
  5. //
  6. // Copyright 2001-2010 by Bill Spitzak and others.
  7. // Original code Copyright Mark Edel. Permission to distribute under
  8. // the LGPL for the FLTK library granted by Mark Edel.
  9. //
  10. // This library is free software. Distribution and use rights are outlined in
  11. // the file "COPYING" which should have been included with this file. If this
  12. // file is missing or damaged, see the license at:
  13. //
  14. // http://www.fltk.org/COPYING.php
  15. //
  16. // Please report all bugs and problems on the following page:
  17. //
  18. // http://www.fltk.org/str.php
  19. //
  20. /* \file
  21. Fl_Text_Editor widget . */
  22. #ifndef FL_TEXT_EDITOR_H
  23. #define FL_TEXT_EDITOR_H
  24. #include "Fl_Text_Display.H"
  25. // key will match in any state
  26. #define FL_TEXT_EDITOR_ANY_STATE (-1L)
  27. /**
  28. This is the FLTK text editor widget. It allows the user to
  29. edit multiple lines of text and supports highlighting and
  30. scrolling. The buffer that is displayed in the widget is managed
  31. by the Fl_Text_Buffer
  32. class.
  33. */
  34. class FL_EXPORT Fl_Text_Editor : public Fl_Text_Display {
  35. public:
  36. /** Key function binding callback type */
  37. typedef int (*Key_Func)(int key, Fl_Text_Editor* editor);
  38. /** Simple linked list associating a key/state to a function */
  39. struct Key_Binding {
  40. int key; ///< the key pressed
  41. int state; ///< the state of key modifiers
  42. Key_Func function; ///< associated function
  43. Key_Binding* next; ///< next key binding in the list
  44. };
  45. Fl_Text_Editor(int X, int Y, int W, int H, const char* l = 0);
  46. ~Fl_Text_Editor() { remove_all_key_bindings(); }
  47. virtual int handle(int e);
  48. /**
  49. Sets the current insert mode; if non-zero, new text
  50. is inserted before the current cursor position. Otherwise, new
  51. text replaces text at the current cursor position.
  52. */
  53. void insert_mode(int b) { insert_mode_ = b; }
  54. /**
  55. Gets the current insert mode; if non-zero, new text
  56. is inserted before the current cursor position. Otherwise, new
  57. text replaces text at the current cursor position.
  58. */
  59. int insert_mode() { return insert_mode_; }
  60. void add_key_binding(int key, int state, Key_Func f, Key_Binding** list);
  61. /** Adds a key of state "state" with the function "function" */
  62. void add_key_binding(int key, int state, Key_Func f)
  63. { add_key_binding(key, state, f, &key_bindings); }
  64. void remove_key_binding(int key, int state, Key_Binding** list);
  65. /** Removes the key binding associated with the key "key" of state "state". */
  66. void remove_key_binding(int key, int state)
  67. { remove_key_binding(key, state, &key_bindings); }
  68. void remove_all_key_bindings(Key_Binding** list);
  69. /** Removes all of the key bindings associated with the text editor or list. */
  70. void remove_all_key_bindings() { remove_all_key_bindings(&key_bindings); }
  71. void add_default_key_bindings(Key_Binding** list);
  72. Key_Func bound_key_function(int key, int state, Key_Binding* list);
  73. /** Returns the function associated with a key binding. */
  74. Key_Func bound_key_function(int key, int state)
  75. { return bound_key_function(key, state, key_bindings); }
  76. /** Sets the default key function for unassigned keys. */
  77. void default_key_function(Key_Func f) { default_key_function_ = f; }
  78. // functions for the built in default bindings
  79. static int kf_default(int c, Fl_Text_Editor* e);
  80. static int kf_ignore(int c, Fl_Text_Editor* e);
  81. static int kf_backspace(int c, Fl_Text_Editor* e);
  82. static int kf_enter(int c, Fl_Text_Editor* e);
  83. static int kf_move(int c, Fl_Text_Editor* e);
  84. static int kf_shift_move(int c, Fl_Text_Editor* e);
  85. static int kf_ctrl_move(int c, Fl_Text_Editor* e);
  86. static int kf_c_s_move(int c, Fl_Text_Editor* e);
  87. static int kf_meta_move(int c, Fl_Text_Editor* e);
  88. static int kf_m_s_move(int c, Fl_Text_Editor* e);
  89. static int kf_home(int, Fl_Text_Editor* e);
  90. static int kf_end(int c, Fl_Text_Editor* e);
  91. static int kf_left(int c, Fl_Text_Editor* e);
  92. static int kf_up(int c, Fl_Text_Editor* e);
  93. static int kf_right(int c, Fl_Text_Editor* e);
  94. static int kf_down(int c, Fl_Text_Editor* e);
  95. static int kf_page_up(int c, Fl_Text_Editor* e);
  96. static int kf_page_down(int c, Fl_Text_Editor* e);
  97. static int kf_insert(int c, Fl_Text_Editor* e);
  98. static int kf_delete(int c, Fl_Text_Editor* e);
  99. static int kf_copy(int c, Fl_Text_Editor* e);
  100. static int kf_cut(int c, Fl_Text_Editor* e);
  101. static int kf_paste(int c, Fl_Text_Editor* e);
  102. static int kf_select_all(int c, Fl_Text_Editor* e);
  103. static int kf_undo(int c, Fl_Text_Editor* e);
  104. protected:
  105. int handle_key();
  106. void maybe_do_callback();
  107. #ifndef FL_DOXYGEN
  108. int insert_mode_;
  109. Key_Binding* key_bindings;
  110. static Key_Binding* global_key_bindings;
  111. Key_Func default_key_function_;
  112. #endif
  113. DECLARE_CLASS_CHEAP_RTTI_2(Fl_Text_Editor, Fl_Text_Display)
  114. };
  115. class FL_EXPORT Fl_Text_Editor_Buffered : public Fl_Text_Editor {
  116. Fl_Text_Buffer *buf_to_free;
  117. public:
  118. Fl_Text_Editor_Buffered(int X, int Y, int W, int H, const char* l = 0):
  119. Fl_Text_Editor(X, Y, W, H, l) {
  120. buf_to_free = new Fl_Text_Buffer();
  121. buffer(buf_to_free);
  122. };
  123. ~Fl_Text_Editor_Buffered() {
  124. buf_to_free->text("");
  125. buffer(NULL);
  126. delete buf_to_free;
  127. };
  128. };
  129. #endif
  130. //
  131. // End of "$Id: Fl_Text_Editor.H 8864 2011-07-19 04:49:30Z greg.ercolano $".
  132. //