editwidget.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* editor initialisation and callback handler.
  2. Copyright (C) 1996, 1997 the Free Software Foundation
  3. Authors: 1996, 1997 Paul Sheer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307, USA.
  16. */
  17. #include <config.h>
  18. #include "edit.h"
  19. WEdit *wedit;
  20. Dlg_head *edit_dlg;
  21. WMenu *edit_menubar;
  22. int column_highlighting = 0;
  23. static int edit_callback (Dlg_head * h, WEdit * edit, int msg, int par);
  24. int edit_event (WEdit * edit, Gpm_Event * event, int *result)
  25. {
  26. *result = MOU_NORMAL;
  27. edit_update_curs_row (edit);
  28. edit_update_curs_col (edit);
  29. if (event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)) {
  30. if (event->y > 1 && event->x > 0
  31. && event->x <= edit->num_widget_columns
  32. && event->y <= edit->num_widget_lines + 1) {
  33. if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
  34. return 1; /* a lone up mustn't do anything */
  35. if (event->type & (GPM_DOWN | GPM_UP))
  36. edit_push_key_press (edit);
  37. edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
  38. if (--event->y > (edit->curs_row + 1))
  39. edit_cursor_move (edit,
  40. edit_move_forward (edit, edit->curs1, event->y - (edit->curs_row + 1), 0)
  41. - edit->curs1);
  42. if (event->y < (edit->curs_row + 1))
  43. edit_cursor_move (edit,
  44. +edit_move_backward (edit, edit->curs1, (edit->curs_row + 1) - event->y)
  45. - edit->curs1);
  46. edit_cursor_move (edit, (int) edit_move_forward3 (edit, edit->curs1,
  47. event->x - edit->start_col - 1, 0) - edit->curs1);
  48. edit->prev_col = edit_get_col (edit);
  49. if (event->type & GPM_DOWN) {
  50. edit_mark_cmd (edit, 1); /* reset */
  51. edit->highlight = 0;
  52. }
  53. if (!(event->type & GPM_DRAG))
  54. edit_mark_cmd (edit, 0);
  55. edit->force |= REDRAW_COMPLETELY;
  56. edit_update_curs_row (edit);
  57. edit_update_curs_col (edit);
  58. edit_update_screen (edit);
  59. return 1;
  60. }
  61. }
  62. return 0;
  63. }
  64. int menubar_event (Gpm_Event * event, WMenu * menubar); /* menu.c */
  65. int edit_mouse_event (Gpm_Event * event, void *x)
  66. {
  67. int result;
  68. if (edit_event ((WEdit *) x, event, &result))
  69. return result;
  70. else
  71. return menubar_event (event, edit_menubar);
  72. }
  73. int
  74. edit (const char *_file, int line)
  75. {
  76. static int made_directory = 0;
  77. int framed = 0;
  78. char *text = 0;
  79. WButtonBar *edit_bar;
  80. if (option_backup_ext_int != -1) {
  81. option_backup_ext = malloc (sizeof (int) + 1);
  82. option_backup_ext[sizeof (int)] = '\0';
  83. memcpy (option_backup_ext, (char *) &option_backup_ext_int,
  84. sizeof (int));
  85. }
  86. if (!made_directory) {
  87. mkdir (catstrs (home_dir, EDIT_DIR, 0), 0700);
  88. made_directory = 1;
  89. }
  90. if (_file) {
  91. if (!(*_file)) {
  92. _file = 0;
  93. text = "";
  94. }
  95. } else
  96. text = "";
  97. if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, text, "", 0))) {
  98. return 0;
  99. }
  100. wedit->macro_i = -1;
  101. /* Create a new dialog and add it widgets to it */
  102. edit_dlg =
  103. create_dlg (0, 0, LINES, COLS, NULL, NULL,
  104. "[Internal File Editor]", NULL, DLG_WANT_TAB);
  105. init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
  106. (callback_fn) edit_callback, (destroy_fn) edit_clean,
  107. (mouse_h) edit_mouse_event, 0);
  108. widget_want_cursor (wedit->widget, 1);
  109. edit_bar = buttonbar_new (1);
  110. if (!framed) {
  111. switch (edit_key_emulation) {
  112. case EDIT_KEY_EMULATION_NORMAL:
  113. edit_init_menu_normal (); /* editmenu.c */
  114. break;
  115. case EDIT_KEY_EMULATION_EMACS:
  116. edit_init_menu_emacs (); /* editmenu.c */
  117. break;
  118. }
  119. edit_menubar = menubar_new (0, 0, COLS, EditMenuBar, N_menus);
  120. }
  121. add_widget (edit_dlg, wedit);
  122. if (!framed)
  123. add_widget (edit_dlg, edit_menubar);
  124. add_widget (edit_dlg, edit_bar);
  125. edit_move_display (wedit, line - 1);
  126. edit_move_to_line (wedit, line - 1);
  127. run_dlg (edit_dlg);
  128. if (!framed)
  129. edit_done_menu (); /* editmenu.c */
  130. destroy_dlg (edit_dlg);
  131. return 1;
  132. }
  133. static void edit_my_define (Dlg_head * h, int idx, char *text,
  134. void (*fn) (WEdit *), WEdit * edit)
  135. {
  136. define_label_data (h, (Widget *) edit, idx, text, (buttonbarfn) fn, edit);
  137. }
  138. static void cmd_F1 (WEdit * edit)
  139. {
  140. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (1));
  141. }
  142. static void cmd_F2 (WEdit * edit)
  143. {
  144. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (2));
  145. }
  146. static void cmd_F3 (WEdit * edit)
  147. {
  148. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (3));
  149. }
  150. static void cmd_F4 (WEdit * edit)
  151. {
  152. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (4));
  153. }
  154. static void cmd_F5 (WEdit * edit)
  155. {
  156. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (5));
  157. }
  158. static void cmd_F6 (WEdit * edit)
  159. {
  160. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (6));
  161. }
  162. static void cmd_F7 (WEdit * edit)
  163. {
  164. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (7));
  165. }
  166. static void cmd_F8 (WEdit * edit)
  167. {
  168. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (8));
  169. }
  170. #if 0
  171. static void cmd_F9 (WEdit * edit)
  172. {
  173. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (9));
  174. }
  175. #endif
  176. static void cmd_F10 (WEdit * edit)
  177. {
  178. send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (10));
  179. }
  180. void edit_labels (WEdit * edit)
  181. {
  182. Dlg_head *h = edit->widget.parent;
  183. edit_my_define (h, 1, _ ("Help"), cmd_F1, edit);
  184. edit_my_define (h, 2, _ ("Save"), cmd_F2, edit);
  185. edit_my_define (h, 3, _ ("Mark"), cmd_F3, edit);
  186. edit_my_define (h, 4, _ ("Replac"), cmd_F4, edit);
  187. edit_my_define (h, 5, _ ("Copy"), cmd_F5, edit);
  188. edit_my_define (h, 6, _ ("Move"), cmd_F6, edit);
  189. edit_my_define (h, 7, _ ("Search"), cmd_F7, edit);
  190. edit_my_define (h, 8, _ ("Delete"), cmd_F8, edit);
  191. if (!edit->have_frame)
  192. edit_my_define (h, 9, _ ("PullDn"), edit_menu_cmd, edit);
  193. edit_my_define (h, 10, _ ("Quit"), cmd_F10, edit);
  194. redraw_labels (h, (Widget *) edit);
  195. }
  196. long get_key_state (void)
  197. {
  198. return (long) get_modifier ();
  199. }
  200. void edit_adjust_size (Dlg_head * h)
  201. {
  202. WEdit *edit;
  203. WButtonBar *edit_bar;
  204. edit = (WEdit *) find_widget_type (h, (callback_fn) edit_callback);
  205. edit_bar = (WButtonBar *) edit->widget.parent->current->next->widget;
  206. widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
  207. widget_set_size (&edit_bar->widget, LINES - 1, 0, 1, COLS);
  208. widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
  209. #ifdef RESIZABLE_MENUBAR
  210. menubar_arrange (edit_menubar);
  211. #endif
  212. }
  213. void edit_update_screen (WEdit * e)
  214. {
  215. edit_scroll_screen_over_cursor (e);
  216. edit_update_curs_col (e);
  217. edit_status (e);
  218. /* pop all events for this window for internal handling */
  219. if (!is_idle ()) {
  220. e->force |= REDRAW_PAGE;
  221. return;
  222. }
  223. if (e->force & REDRAW_COMPLETELY)
  224. e->force |= REDRAW_PAGE;
  225. edit_render_keypress (e);
  226. }
  227. static int edit_callback (Dlg_head * h, WEdit * e, int msg, int par)
  228. {
  229. switch (msg) {
  230. case WIDGET_INIT:
  231. e->force |= REDRAW_COMPLETELY;
  232. edit_labels (e);
  233. break;
  234. case WIDGET_DRAW:
  235. e->force |= REDRAW_COMPLETELY;
  236. e->num_widget_lines = LINES - 2;
  237. e->num_widget_columns = COLS;
  238. case WIDGET_FOCUS:
  239. edit_update_screen (e);
  240. return 1;
  241. case WIDGET_KEY:{
  242. int cmd, ch;
  243. if (edit_drop_hotkey_menu (e, par)) /* first check alt-f, alt-e, alt-s, etc for drop menus */
  244. return 1;
  245. if (!edit_translate_key (e, 0, par, get_key_state (), &cmd, &ch))
  246. return 0;
  247. edit_execute_key_command (e, cmd, ch);
  248. edit_update_screen (e);
  249. }
  250. return 1;
  251. case WIDGET_COMMAND:
  252. edit_execute_key_command (e, par, -1);
  253. edit_update_screen (e);
  254. return 1;
  255. case WIDGET_CURSOR:
  256. widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET, e->curs_col + e->start_col);
  257. return 1;
  258. }
  259. return default_proc (h, msg, par);
  260. }