editwidget.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 (Gpm_Event *event, void *data)
  46. {
  47. WEdit *edit = (WEdit *) data;
  48. /* Unknown event type */
  49. if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
  50. return MOU_NORMAL;
  51. /* rest of the upper frame, the menu is invisible - call menu */
  52. if ((event->type & GPM_DOWN) && (event->y == 1))
  53. return edit_menubar->widget.mouse (event, edit_menubar);
  54. edit_update_curs_row (edit);
  55. edit_update_curs_col (edit);
  56. /* Outside editor window */
  57. if (event->y <= 1 || event->x <= 0
  58. || event->x > edit->num_widget_columns
  59. || event->y > edit->num_widget_lines + 1)
  60. return MOU_NORMAL;
  61. /* Wheel events */
  62. if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
  63. edit_move_up (edit, 2, 1);
  64. goto update;
  65. }
  66. if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
  67. edit_move_down (edit, 2, 1);
  68. goto update;
  69. }
  70. /* A lone up mustn't do anything */
  71. if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
  72. return MOU_NORMAL;
  73. if (event->type & (GPM_DOWN | GPM_UP))
  74. edit_push_key_press (edit);
  75. if (option_cursor_beyond_eol) {
  76. long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
  77. edit_eol(edit, edit->curs1));
  78. if ( event->x > line_len ) {
  79. edit->over_col = event->x - line_len - option_line_state_width - 1;
  80. edit->prev_col = line_len;
  81. } else {
  82. edit->over_col = 0;
  83. edit->prev_col = event->x - option_line_state_width - 1;
  84. }
  85. } else {
  86. edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
  87. }
  88. if (--event->y > (edit->curs_row + 1))
  89. edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
  90. else if (event->y < (edit->curs_row + 1))
  91. edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
  92. else
  93. edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
  94. if (event->type & GPM_DOWN) {
  95. edit_mark_cmd (edit, 1); /* reset */
  96. edit->highlight = 0;
  97. }
  98. if (!(event->type & GPM_DRAG))
  99. edit_mark_cmd (edit, 0);
  100. update:
  101. edit_find_bracket (edit);
  102. edit->force |= REDRAW_COMPLETELY;
  103. edit_update_curs_row (edit);
  104. edit_update_curs_col (edit);
  105. edit_update_screen (edit);
  106. return MOU_NORMAL;
  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 (!made_directory) {
  148. char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
  149. made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
  150. g_free (dir);
  151. }
  152. if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
  153. return 0;
  154. }
  155. /* Create a new dialog and add it widgets to it */
  156. edit_dlg =
  157. create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
  158. "[Internal File Editor]", NULL, DLG_WANT_TAB);
  159. init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
  160. edit_callback, edit_event);
  161. widget_want_cursor (wedit->widget, 1);
  162. edit_bar = buttonbar_new (1);
  163. edit_menubar = edit_create_menu ();
  164. add_widget (edit_dlg, edit_bar);
  165. add_widget (edit_dlg, wedit);
  166. add_widget (edit_dlg, edit_menubar);
  167. run_dlg (edit_dlg);
  168. edit_done_menu (edit_menubar); /* editmenu.c */
  169. destroy_dlg (edit_dlg);
  170. return 1;
  171. }
  172. const char *
  173. edit_get_file_name (const WEdit *edit)
  174. {
  175. return edit->filename;
  176. }
  177. static void edit_my_define (Dlg_head * h, int idx, const char *text,
  178. void (*fn) (WEdit *), WEdit * edit)
  179. {
  180. text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
  181. /* function-cast ok */
  182. buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
  183. }
  184. static void cmd_F1 (WEdit * edit)
  185. {
  186. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
  187. }
  188. static void cmd_F2 (WEdit * edit)
  189. {
  190. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
  191. }
  192. static void cmd_F3 (WEdit * edit)
  193. {
  194. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
  195. }
  196. static void cmd_F4 (WEdit * edit)
  197. {
  198. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
  199. }
  200. static void cmd_F5 (WEdit * edit)
  201. {
  202. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
  203. }
  204. static void cmd_F6 (WEdit * edit)
  205. {
  206. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
  207. }
  208. static void cmd_F7 (WEdit * edit)
  209. {
  210. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
  211. }
  212. static void cmd_F8 (WEdit * edit)
  213. {
  214. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
  215. }
  216. #if 0
  217. static void cmd_F9 (WEdit * edit)
  218. {
  219. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
  220. }
  221. #endif
  222. static void cmd_F10 (WEdit * edit)
  223. {
  224. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
  225. }
  226. static void
  227. edit_labels (WEdit *edit)
  228. {
  229. Dlg_head *h = edit->widget.parent;
  230. edit_my_define (h, 1, _("Help"), cmd_F1, edit);
  231. edit_my_define (h, 2, _("Save"), cmd_F2, edit);
  232. edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
  233. edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
  234. edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
  235. edit_my_define (h, 6, _("Move"), cmd_F6, edit);
  236. edit_my_define (h, 7, _("Search"), cmd_F7, edit);
  237. edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
  238. edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
  239. edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
  240. buttonbar_redraw (h);
  241. }
  242. void edit_update_screen (WEdit * e)
  243. {
  244. edit_scroll_screen_over_cursor (e);
  245. edit_update_curs_col (e);
  246. edit_status (e);
  247. /* pop all events for this window for internal handling */
  248. if (!is_idle ()) {
  249. e->force |= REDRAW_PAGE;
  250. return;
  251. }
  252. if (e->force & REDRAW_COMPLETELY)
  253. e->force |= REDRAW_PAGE;
  254. edit_render_keypress (e);
  255. }
  256. static cb_ret_t
  257. edit_callback (Widget *w, widget_msg_t msg, int parm)
  258. {
  259. WEdit *e = (WEdit *) w;
  260. switch (msg) {
  261. case WIDGET_INIT:
  262. e->force |= REDRAW_COMPLETELY;
  263. edit_labels (e);
  264. return MSG_HANDLED;
  265. case WIDGET_DRAW:
  266. e->force |= REDRAW_COMPLETELY;
  267. e->num_widget_lines = LINES - 2;
  268. e->num_widget_columns = COLS;
  269. /* fallthrough */
  270. case WIDGET_FOCUS:
  271. edit_update_screen (e);
  272. return MSG_HANDLED;
  273. case WIDGET_KEY:
  274. {
  275. int cmd, ch;
  276. /* The user may override the access-keys for the menu bar. */
  277. if (edit_translate_key (e, parm, &cmd, &ch)) {
  278. edit_execute_key_command (e, cmd, ch);
  279. edit_update_screen (e);
  280. return MSG_HANDLED;
  281. } else if (edit_drop_hotkey_menu (e, parm)) {
  282. return MSG_HANDLED;
  283. } else {
  284. return MSG_NOT_HANDLED;
  285. }
  286. }
  287. case WIDGET_CURSOR:
  288. widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
  289. e->curs_col + e->start_col + e->over_col +
  290. EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
  291. return MSG_HANDLED;
  292. case WIDGET_DESTROY:
  293. edit_clean (e);
  294. return MSG_HANDLED;
  295. default:
  296. return default_proc (msg, parm);
  297. }
  298. }