editwidget.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* editor initialisation and callback handler.
  2. Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
  3. 2007 Free Software Foundation, Inc.
  4. Authors: 1996, 1997 Paul Sheer
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301, USA.
  17. */
  18. /** \file
  19. * \brief Source: editor initialisation and callback handler
  20. * \author Paul Sheer
  21. * \date 1996, 1997
  22. */
  23. #include <config.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <errno.h>
  31. #include <sys/stat.h>
  32. #include <stdlib.h>
  33. #include "../src/global.h"
  34. #include "../src/tty/tty.h" /* LINES, COLS */
  35. #include "../src/tty/key.h" /* is_idle() */
  36. #include "edit-impl.h"
  37. #include "edit-widget.h"
  38. #include "../src/widget.h" /* buttonbar_redraw() */
  39. #include "../src/menu.h" /* menubar_new() */
  40. WEdit *wedit;
  41. struct WMenu *edit_menubar;
  42. int column_highlighting = 0;
  43. static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
  44. static int
  45. edit_event (WEdit * edit, Gpm_Event * event, int *result)
  46. {
  47. *result = MOU_NORMAL;
  48. edit_update_curs_row (edit);
  49. edit_update_curs_col (edit);
  50. /* Unknown event type */
  51. if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
  52. return 0;
  53. /* Wheel events */
  54. if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
  55. edit_move_up (edit, 2, 1);
  56. goto update;
  57. }
  58. if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
  59. edit_move_down (edit, 2, 1);
  60. goto update;
  61. }
  62. /* Outside editor window */
  63. if (event->y <= 1 || event->x <= 0
  64. || event->x > edit->num_widget_columns
  65. || event->y > edit->num_widget_lines + 1)
  66. return 0;
  67. /* A lone up mustn't do anything */
  68. if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
  69. return 1;
  70. if (event->type & (GPM_DOWN | GPM_UP))
  71. edit_push_key_press (edit);
  72. if (option_cursor_beyond_eol) {
  73. long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
  74. edit_eol(edit, edit->curs1));
  75. if ( event->x > line_len ) {
  76. edit->over_col = event->x - line_len - option_line_state_width - 1;
  77. edit->prev_col = line_len;
  78. } else {
  79. edit->over_col = 0;
  80. edit->prev_col = event->x - option_line_state_width - 1;
  81. }
  82. } else {
  83. edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
  84. }
  85. if (--event->y > (edit->curs_row + 1))
  86. edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
  87. else if (event->y < (edit->curs_row + 1))
  88. edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
  89. else
  90. edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
  91. if (event->type & GPM_DOWN) {
  92. edit_mark_cmd (edit, 1); /* reset */
  93. edit->highlight = 0;
  94. }
  95. if (!(event->type & GPM_DRAG))
  96. edit_mark_cmd (edit, 0);
  97. update:
  98. edit_find_bracket (edit);
  99. edit->force |= REDRAW_COMPLETELY;
  100. edit_update_curs_row (edit);
  101. edit_update_curs_col (edit);
  102. edit_update_screen (edit);
  103. return 1;
  104. }
  105. static int
  106. edit_mouse_event (Gpm_Event *event, void *x)
  107. {
  108. int result;
  109. if (edit_event ((WEdit *) x, event, &result))
  110. return result;
  111. else
  112. return (*edit_menubar->widget.mouse) (event, edit_menubar);
  113. }
  114. static void
  115. edit_adjust_size (Dlg_head *h)
  116. {
  117. WEdit *edit;
  118. WButtonBar *edit_bar;
  119. edit = (WEdit *) find_widget_type (h, edit_callback);
  120. edit_bar = find_buttonbar (h);
  121. widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
  122. widget_set_size ((Widget *) edit_bar, LINES - 1, 0, 1, COLS);
  123. widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
  124. #ifdef RESIZABLE_MENUBAR
  125. menubar_arrange (edit_menubar);
  126. #endif
  127. }
  128. /* Callback for the edit dialog */
  129. static cb_ret_t
  130. edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
  131. {
  132. WEdit *edit;
  133. switch (msg) {
  134. case DLG_RESIZE:
  135. edit_adjust_size (h);
  136. return MSG_HANDLED;
  137. case DLG_VALIDATE:
  138. edit = (WEdit *) find_widget_type (h, edit_callback);
  139. if (!edit_ok_to_exit (edit)) {
  140. h->running = 1;
  141. }
  142. return MSG_HANDLED;
  143. default:
  144. return default_dlg_callback (h, msg, parm);
  145. }
  146. }
  147. int
  148. edit_file (const char *_file, int line)
  149. {
  150. static int made_directory = 0;
  151. Dlg_head *edit_dlg;
  152. WButtonBar *edit_bar;
  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_create_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. const char *
  180. edit_get_file_name (const WEdit *edit)
  181. {
  182. return edit->filename;
  183. }
  184. static void edit_my_define (Dlg_head * h, int idx, const char *text,
  185. void (*fn) (WEdit *), WEdit * edit)
  186. {
  187. text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
  188. /* function-cast ok */
  189. buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
  190. }
  191. static void cmd_F1 (WEdit * edit)
  192. {
  193. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
  194. }
  195. static void cmd_F2 (WEdit * edit)
  196. {
  197. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
  198. }
  199. static void cmd_F3 (WEdit * edit)
  200. {
  201. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
  202. }
  203. static void cmd_F4 (WEdit * edit)
  204. {
  205. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
  206. }
  207. static void cmd_F5 (WEdit * edit)
  208. {
  209. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
  210. }
  211. static void cmd_F6 (WEdit * edit)
  212. {
  213. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
  214. }
  215. static void cmd_F7 (WEdit * edit)
  216. {
  217. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
  218. }
  219. static void cmd_F8 (WEdit * edit)
  220. {
  221. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
  222. }
  223. #if 0
  224. static void cmd_F9 (WEdit * edit)
  225. {
  226. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
  227. }
  228. #endif
  229. static void cmd_F10 (WEdit * edit)
  230. {
  231. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
  232. }
  233. static void
  234. edit_labels (WEdit *edit)
  235. {
  236. Dlg_head *h = edit->widget.parent;
  237. edit_my_define (h, 1, _("Help"), cmd_F1, edit);
  238. edit_my_define (h, 2, _("Save"), cmd_F2, edit);
  239. edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
  240. edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
  241. edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
  242. edit_my_define (h, 6, _("Move"), cmd_F6, edit);
  243. edit_my_define (h, 7, _("Search"), cmd_F7, edit);
  244. edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
  245. edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
  246. edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
  247. buttonbar_redraw (h);
  248. }
  249. void edit_update_screen (WEdit * e)
  250. {
  251. edit_scroll_screen_over_cursor (e);
  252. edit_update_curs_col (e);
  253. edit_status (e);
  254. /* pop all events for this window for internal handling */
  255. if (!is_idle ()) {
  256. e->force |= REDRAW_PAGE;
  257. return;
  258. }
  259. if (e->force & REDRAW_COMPLETELY)
  260. e->force |= REDRAW_PAGE;
  261. edit_render_keypress (e);
  262. }
  263. static cb_ret_t
  264. edit_callback (Widget *w, widget_msg_t msg, int parm)
  265. {
  266. WEdit *e = (WEdit *) w;
  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. /* The user may override the access-keys for the menu bar. */
  284. if (edit_translate_key (e, parm, &cmd, &ch)) {
  285. edit_execute_key_command (e, cmd, ch);
  286. edit_update_screen (e);
  287. return MSG_HANDLED;
  288. } else if (edit_drop_hotkey_menu (e, parm)) {
  289. return MSG_HANDLED;
  290. } else {
  291. return MSG_NOT_HANDLED;
  292. }
  293. }
  294. case WIDGET_CURSOR:
  295. widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
  296. e->curs_col + e->start_col + e->over_col +
  297. EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
  298. return MSG_HANDLED;
  299. case WIDGET_DESTROY:
  300. edit_clean (e);
  301. return MSG_HANDLED;
  302. default:
  303. return default_proc (msg, parm);
  304. }
  305. }