editwidget.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, 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 (Widget *, 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, edit_callback);
  114. edit_bar = find_buttonbar (h);
  115. widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
  116. widget_set_size ((Widget *) edit_bar, 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, 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. edit_callback,
  167. edit_mouse_event);
  168. widget_want_cursor (wedit->widget, 1);
  169. edit_bar = buttonbar_new (1);
  170. edit_menubar = edit_init_menu ();
  171. add_widget (edit_dlg, edit_bar);
  172. add_widget (edit_dlg, wedit);
  173. add_widget (edit_dlg, edit_menubar);
  174. run_dlg (edit_dlg);
  175. edit_done_menu (edit_menubar); /* editmenu.c */
  176. destroy_dlg (edit_dlg);
  177. return 1;
  178. }
  179. static void edit_my_define (Dlg_head * h, int idx, const char *text,
  180. void (*fn) (WEdit *), WEdit * edit)
  181. {
  182. /* function-cast ok */
  183. buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
  184. }
  185. static void cmd_F1 (WEdit * edit)
  186. {
  187. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
  188. }
  189. static void cmd_F2 (WEdit * edit)
  190. {
  191. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
  192. }
  193. static void cmd_F3 (WEdit * edit)
  194. {
  195. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
  196. }
  197. static void cmd_F4 (WEdit * edit)
  198. {
  199. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
  200. }
  201. static void cmd_F5 (WEdit * edit)
  202. {
  203. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
  204. }
  205. static void cmd_F6 (WEdit * edit)
  206. {
  207. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
  208. }
  209. static void cmd_F7 (WEdit * edit)
  210. {
  211. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
  212. }
  213. static void cmd_F8 (WEdit * edit)
  214. {
  215. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
  216. }
  217. #if 0
  218. static void cmd_F9 (WEdit * edit)
  219. {
  220. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
  221. }
  222. #endif
  223. static void cmd_F10 (WEdit * edit)
  224. {
  225. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
  226. }
  227. static void
  228. edit_labels (WEdit *edit)
  229. {
  230. Dlg_head *h = edit->widget.parent;
  231. edit_my_define (h, 1, _("Help"), cmd_F1, edit);
  232. edit_my_define (h, 2, _("Save"), cmd_F2, edit);
  233. edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
  234. edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
  235. edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
  236. edit_my_define (h, 6, _("Move"), cmd_F6, edit);
  237. edit_my_define (h, 7, _("Search"), cmd_F7, edit);
  238. edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
  239. edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
  240. edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
  241. buttonbar_redraw (h);
  242. }
  243. void edit_update_screen (WEdit * e)
  244. {
  245. edit_scroll_screen_over_cursor (e);
  246. edit_update_curs_col (e);
  247. edit_status (e);
  248. /* pop all events for this window for internal handling */
  249. if (!is_idle ()) {
  250. e->force |= REDRAW_PAGE;
  251. return;
  252. }
  253. if (e->force & REDRAW_COMPLETELY)
  254. e->force |= REDRAW_PAGE;
  255. edit_render_keypress (e);
  256. }
  257. static cb_ret_t
  258. edit_callback (Widget *w, widget_msg_t msg, int parm)
  259. {
  260. WEdit *e = (WEdit *) w;
  261. switch (msg) {
  262. case WIDGET_INIT:
  263. e->force |= REDRAW_COMPLETELY;
  264. edit_labels (e);
  265. return MSG_HANDLED;
  266. case WIDGET_DRAW:
  267. e->force |= REDRAW_COMPLETELY;
  268. e->num_widget_lines = LINES - 2;
  269. e->num_widget_columns = COLS;
  270. /* fallthrough */
  271. case WIDGET_FOCUS:
  272. edit_update_screen (e);
  273. return MSG_HANDLED;
  274. case WIDGET_KEY:
  275. {
  276. int cmd, ch;
  277. /* first check alt-f, alt-e, alt-s, etc for drop menus */
  278. if (edit_drop_hotkey_menu (e, parm))
  279. return MSG_HANDLED;
  280. if (!edit_translate_key (e, parm, &cmd, &ch))
  281. return MSG_NOT_HANDLED;
  282. edit_execute_key_command (e, cmd, ch);
  283. edit_update_screen (e);
  284. }
  285. return MSG_HANDLED;
  286. case WIDGET_CURSOR:
  287. widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
  288. e->curs_col + e->start_col);
  289. return MSG_HANDLED;
  290. case WIDGET_DESTROY:
  291. edit_clean (e);
  292. return MSG_HANDLED;
  293. default:
  294. return default_proc (msg, parm);
  295. }
  296. }