editwidget.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <stdio.h>
  19. #include <stdarg.h>
  20. #include <sys/types.h>
  21. #ifdef HAVE_UNISTD_H
  22. # include <unistd.h>
  23. #endif
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <errno.h>
  27. #include <sys/stat.h>
  28. #include <stdlib.h>
  29. #include "../src/global.h"
  30. #include "edit.h"
  31. #include "edit-widget.h"
  32. #include "../src/tty.h" /* LINES */
  33. #include "../src/widget.h" /* buttonbar_redraw() */
  34. #include "../src/menu.h" /* menubar_new() */
  35. #include "../src/key.h" /* is_idle() */
  36. WEdit *wedit;
  37. struct WMenu *edit_menubar;
  38. int column_highlighting = 0;
  39. static cb_ret_t edit_callback (WEdit *edit, widget_msg_t msg, int parm);
  40. static int
  41. edit_event (WEdit * edit, Gpm_Event * event, int *result)
  42. {
  43. *result = MOU_NORMAL;
  44. edit_update_curs_row (edit);
  45. edit_update_curs_col (edit);
  46. /* Unknown event type */
  47. if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
  48. return 0;
  49. /* Wheel events */
  50. if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
  51. edit_move_up (edit, 2, 1);
  52. goto update;
  53. }
  54. if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
  55. edit_move_down (edit, 2, 1);
  56. goto update;
  57. }
  58. /* Outside editor window */
  59. if (event->y <= 1 || event->x <= 0
  60. || event->x > edit->num_widget_columns
  61. || event->y > edit->num_widget_lines + 1)
  62. return 0;
  63. /* A lone up mustn't do anything */
  64. if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
  65. return 1;
  66. if (event->type & (GPM_DOWN | GPM_UP))
  67. edit_push_key_press (edit);
  68. edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
  69. if (--event->y > (edit->curs_row + 1))
  70. edit_cursor_move (edit,
  71. edit_move_forward (edit, edit->curs1,
  72. event->y - (edit->curs_row +
  73. 1), 0)
  74. - edit->curs1);
  75. if (event->y < (edit->curs_row + 1))
  76. edit_cursor_move (edit,
  77. edit_move_backward (edit, edit->curs1,
  78. (edit->curs_row + 1) -
  79. event->y) - edit->curs1);
  80. edit_cursor_move (edit,
  81. (int) edit_move_forward3 (edit, edit->curs1,
  82. event->x -
  83. edit->start_col - 1,
  84. 0) - edit->curs1);
  85. edit->prev_col = edit_get_col (edit);
  86. if (event->type & GPM_DOWN) {
  87. edit_mark_cmd (edit, 1); /* reset */
  88. edit->highlight = 0;
  89. }
  90. if (!(event->type & GPM_DRAG))
  91. edit_mark_cmd (edit, 0);
  92. update:
  93. edit->force |= REDRAW_COMPLETELY;
  94. edit_update_curs_row (edit);
  95. edit_update_curs_col (edit);
  96. edit_update_screen (edit);
  97. return 1;
  98. }
  99. static int
  100. edit_mouse_event (Gpm_Event *event, void *x)
  101. {
  102. int result;
  103. if (edit_event ((WEdit *) x, event, &result))
  104. return result;
  105. else
  106. return (*edit_menubar->widget.mouse) (event, edit_menubar);
  107. }
  108. static void
  109. edit_adjust_size (Dlg_head *h)
  110. {
  111. WEdit *edit;
  112. WButtonBar *edit_bar;
  113. edit = (WEdit *) find_widget_type (h, (callback_fn) edit_callback);
  114. edit_bar = find_buttonbar (h);
  115. widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
  116. widget_set_size (&edit_bar->widget, LINES - 1, 0, 1, COLS);
  117. widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
  118. #ifdef RESIZABLE_MENUBAR
  119. menubar_arrange (edit_menubar);
  120. #endif
  121. }
  122. /* Callback for the edit dialog */
  123. static cb_ret_t
  124. edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
  125. {
  126. WEdit *edit;
  127. switch (msg) {
  128. case DLG_RESIZE:
  129. edit_adjust_size (h);
  130. return MSG_HANDLED;
  131. case DLG_VALIDATE:
  132. edit = (WEdit *) find_widget_type (h, (callback_fn) edit_callback);
  133. if (!edit_ok_to_exit (edit)) {
  134. h->running = 1;
  135. }
  136. return MSG_HANDLED;
  137. default:
  138. return default_dlg_callback (h, msg, parm);
  139. }
  140. }
  141. int
  142. edit_file (const char *_file, int line)
  143. {
  144. static int made_directory = 0;
  145. Dlg_head *edit_dlg;
  146. WButtonBar *edit_bar;
  147. if (option_backup_ext_int != -1) {
  148. option_backup_ext = g_malloc (sizeof (int) + 1);
  149. option_backup_ext[sizeof (int)] = '\0';
  150. memcpy (option_backup_ext, (char *) &option_backup_ext_int,
  151. sizeof (int));
  152. }
  153. if (!made_directory) {
  154. char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
  155. made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
  156. g_free (dir);
  157. }
  158. if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
  159. return 0;
  160. }
  161. /* Create a new dialog and add it widgets to it */
  162. edit_dlg =
  163. create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
  164. "[Internal File Editor]", NULL, DLG_WANT_TAB);
  165. init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
  166. (callback_fn) edit_callback,
  167. (mouse_h) edit_mouse_event);
  168. widget_want_cursor (wedit->widget, 1);
  169. edit_bar = buttonbar_new (1);
  170. switch (edit_key_emulation) {
  171. case EDIT_KEY_EMULATION_NORMAL:
  172. edit_init_menu_normal (); /* editmenu.c */
  173. break;
  174. case EDIT_KEY_EMULATION_EMACS:
  175. edit_init_menu_emacs (); /* editmenu.c */
  176. break;
  177. }
  178. edit_menubar = menubar_new (0, 0, COLS, EditMenuBar, N_menus);
  179. add_widget (edit_dlg, edit_bar);
  180. add_widget (edit_dlg, wedit);
  181. add_widget (edit_dlg, edit_menubar);
  182. run_dlg (edit_dlg);
  183. edit_done_menu (); /* editmenu.c */
  184. destroy_dlg (edit_dlg);
  185. return 1;
  186. }
  187. static void edit_my_define (Dlg_head * h, int idx, const char *text,
  188. void (*fn) (WEdit *), WEdit * edit)
  189. {
  190. buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
  191. }
  192. static void cmd_F1 (WEdit * edit)
  193. {
  194. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
  195. }
  196. static void cmd_F2 (WEdit * edit)
  197. {
  198. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
  199. }
  200. static void cmd_F3 (WEdit * edit)
  201. {
  202. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
  203. }
  204. static void cmd_F4 (WEdit * edit)
  205. {
  206. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
  207. }
  208. static void cmd_F5 (WEdit * edit)
  209. {
  210. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
  211. }
  212. static void cmd_F6 (WEdit * edit)
  213. {
  214. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
  215. }
  216. static void cmd_F7 (WEdit * edit)
  217. {
  218. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
  219. }
  220. static void cmd_F8 (WEdit * edit)
  221. {
  222. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
  223. }
  224. #if 0
  225. static void cmd_F9 (WEdit * edit)
  226. {
  227. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
  228. }
  229. #endif
  230. static void cmd_F10 (WEdit * edit)
  231. {
  232. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
  233. }
  234. static void
  235. edit_labels (WEdit *edit)
  236. {
  237. Dlg_head *h = edit->widget.parent;
  238. edit_my_define (h, 1, _("Help"), cmd_F1, edit);
  239. edit_my_define (h, 2, _("Save"), cmd_F2, edit);
  240. edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
  241. edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
  242. edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
  243. edit_my_define (h, 6, _("Move"), cmd_F6, edit);
  244. edit_my_define (h, 7, _("Search"), cmd_F7, edit);
  245. edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
  246. edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
  247. edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
  248. buttonbar_redraw (h);
  249. }
  250. void edit_update_screen (WEdit * e)
  251. {
  252. edit_scroll_screen_over_cursor (e);
  253. edit_update_curs_col (e);
  254. edit_status (e);
  255. /* pop all events for this window for internal handling */
  256. if (!is_idle ()) {
  257. e->force |= REDRAW_PAGE;
  258. return;
  259. }
  260. if (e->force & REDRAW_COMPLETELY)
  261. e->force |= REDRAW_PAGE;
  262. edit_render_keypress (e);
  263. }
  264. static cb_ret_t
  265. edit_callback (WEdit *e, widget_msg_t msg, int parm)
  266. {
  267. switch (msg) {
  268. case WIDGET_INIT:
  269. e->force |= REDRAW_COMPLETELY;
  270. edit_labels (e);
  271. return MSG_HANDLED;
  272. case WIDGET_DRAW:
  273. e->force |= REDRAW_COMPLETELY;
  274. e->num_widget_lines = LINES - 2;
  275. e->num_widget_columns = COLS;
  276. /* fallthrough */
  277. case WIDGET_FOCUS:
  278. edit_update_screen (e);
  279. return MSG_HANDLED;
  280. case WIDGET_KEY:
  281. {
  282. int cmd, ch;
  283. /* first check alt-f, alt-e, alt-s, etc for drop menus */
  284. if (edit_drop_hotkey_menu (e, parm))
  285. return MSG_HANDLED;
  286. if (!edit_translate_key (e, parm, &cmd, &ch))
  287. return MSG_NOT_HANDLED;
  288. edit_execute_key_command (e, cmd, ch);
  289. edit_update_screen (e);
  290. }
  291. return MSG_HANDLED;
  292. case WIDGET_CURSOR:
  293. widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
  294. e->curs_col + e->start_col);
  295. return MSG_HANDLED;
  296. case WIDGET_DESTROY:
  297. edit_clean (e);
  298. return MSG_HANDLED;
  299. default:
  300. return default_proc (msg, parm);
  301. }
  302. }