editwidget.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "edit-impl.h"
  35. #include "edit-widget.h"
  36. #include "../src/tty.h" /* LINES */
  37. #include "../src/widget.h" /* buttonbar_redraw() */
  38. #include "../src/menu.h" /* menubar_new() */
  39. #include "../src/key.h" /* is_idle() */
  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. edit->prev_col = event->x - edit->start_col - 1 - option_line_state_width;
  73. if (--event->y > (edit->curs_row + 1))
  74. edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
  75. else if (event->y < (edit->curs_row + 1))
  76. edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
  77. else
  78. edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
  79. if (event->type & GPM_DOWN) {
  80. edit_mark_cmd (edit, 1); /* reset */
  81. edit->highlight = 0;
  82. }
  83. if (!(event->type & GPM_DRAG))
  84. edit_mark_cmd (edit, 0);
  85. update:
  86. edit_find_bracket (edit);
  87. edit->force |= REDRAW_COMPLETELY;
  88. edit_update_curs_row (edit);
  89. edit_update_curs_col (edit);
  90. edit_update_screen (edit);
  91. return 1;
  92. }
  93. static int
  94. edit_mouse_event (Gpm_Event *event, void *x)
  95. {
  96. int result;
  97. if (edit_event ((WEdit *) x, event, &result))
  98. return result;
  99. else
  100. return (*edit_menubar->widget.mouse) (event, edit_menubar);
  101. }
  102. static void
  103. edit_adjust_size (Dlg_head *h)
  104. {
  105. WEdit *edit;
  106. WButtonBar *edit_bar;
  107. edit = (WEdit *) find_widget_type (h, edit_callback);
  108. edit_bar = find_buttonbar (h);
  109. widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
  110. widget_set_size ((Widget *) edit_bar, LINES - 1, 0, 1, COLS);
  111. widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
  112. #ifdef RESIZABLE_MENUBAR
  113. menubar_arrange (edit_menubar);
  114. #endif
  115. }
  116. /* Callback for the edit dialog */
  117. static cb_ret_t
  118. edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
  119. {
  120. WEdit *edit;
  121. switch (msg) {
  122. case DLG_RESIZE:
  123. edit_adjust_size (h);
  124. return MSG_HANDLED;
  125. case DLG_VALIDATE:
  126. edit = (WEdit *) find_widget_type (h, edit_callback);
  127. if (!edit_ok_to_exit (edit)) {
  128. h->running = 1;
  129. }
  130. return MSG_HANDLED;
  131. default:
  132. return default_dlg_callback (h, msg, parm);
  133. }
  134. }
  135. int
  136. edit_file (const char *_file, int line)
  137. {
  138. static int made_directory = 0;
  139. Dlg_head *edit_dlg;
  140. WButtonBar *edit_bar;
  141. if (!made_directory) {
  142. char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
  143. made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
  144. g_free (dir);
  145. }
  146. if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
  147. return 0;
  148. }
  149. /* Create a new dialog and add it widgets to it */
  150. edit_dlg =
  151. create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
  152. "[Internal File Editor]", NULL, DLG_WANT_TAB);
  153. init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
  154. edit_callback,
  155. edit_mouse_event);
  156. widget_want_cursor (wedit->widget, 1);
  157. edit_bar = buttonbar_new (1);
  158. edit_menubar = edit_create_menu ();
  159. add_widget (edit_dlg, edit_bar);
  160. add_widget (edit_dlg, wedit);
  161. add_widget (edit_dlg, edit_menubar);
  162. run_dlg (edit_dlg);
  163. edit_done_menu (edit_menubar); /* editmenu.c */
  164. destroy_dlg (edit_dlg);
  165. return 1;
  166. }
  167. const char *
  168. edit_get_file_name (const WEdit *edit)
  169. {
  170. return edit->filename;
  171. }
  172. static void edit_my_define (Dlg_head * h, int idx, const char *text,
  173. void (*fn) (WEdit *), WEdit * edit)
  174. {
  175. text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
  176. /* function-cast ok */
  177. buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
  178. }
  179. static void cmd_F1 (WEdit * edit)
  180. {
  181. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
  182. }
  183. static void cmd_F2 (WEdit * edit)
  184. {
  185. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
  186. }
  187. static void cmd_F3 (WEdit * edit)
  188. {
  189. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
  190. }
  191. static void cmd_F4 (WEdit * edit)
  192. {
  193. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
  194. }
  195. static void cmd_F5 (WEdit * edit)
  196. {
  197. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
  198. }
  199. static void cmd_F6 (WEdit * edit)
  200. {
  201. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
  202. }
  203. static void cmd_F7 (WEdit * edit)
  204. {
  205. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
  206. }
  207. static void cmd_F8 (WEdit * edit)
  208. {
  209. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
  210. }
  211. #if 0
  212. static void cmd_F9 (WEdit * edit)
  213. {
  214. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
  215. }
  216. #endif
  217. static void cmd_F10 (WEdit * edit)
  218. {
  219. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
  220. }
  221. static void
  222. edit_labels (WEdit *edit)
  223. {
  224. Dlg_head *h = edit->widget.parent;
  225. edit_my_define (h, 1, _("Help"), cmd_F1, edit);
  226. edit_my_define (h, 2, _("Save"), cmd_F2, edit);
  227. edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
  228. edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
  229. edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
  230. edit_my_define (h, 6, _("Move"), cmd_F6, edit);
  231. edit_my_define (h, 7, _("Search"), cmd_F7, edit);
  232. edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
  233. edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
  234. edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
  235. buttonbar_redraw (h);
  236. }
  237. void edit_update_screen (WEdit * e)
  238. {
  239. edit_scroll_screen_over_cursor (e);
  240. edit_update_curs_col (e);
  241. edit_status (e);
  242. /* pop all events for this window for internal handling */
  243. if (!is_idle ()) {
  244. e->force |= REDRAW_PAGE;
  245. return;
  246. }
  247. if (e->force & REDRAW_COMPLETELY)
  248. e->force |= REDRAW_PAGE;
  249. edit_render_keypress (e);
  250. }
  251. static cb_ret_t
  252. edit_callback (Widget *w, widget_msg_t msg, int parm)
  253. {
  254. WEdit *e = (WEdit *) w;
  255. switch (msg) {
  256. case WIDGET_INIT:
  257. e->force |= REDRAW_COMPLETELY;
  258. edit_labels (e);
  259. return MSG_HANDLED;
  260. case WIDGET_DRAW:
  261. e->force |= REDRAW_COMPLETELY;
  262. e->num_widget_lines = LINES - 2;
  263. e->num_widget_columns = COLS;
  264. /* fallthrough */
  265. case WIDGET_FOCUS:
  266. edit_update_screen (e);
  267. return MSG_HANDLED;
  268. case WIDGET_KEY:
  269. {
  270. int cmd, ch;
  271. /* The user may override the access-keys for the menu bar. */
  272. if (edit_translate_key (e, parm, &cmd, &ch)) {
  273. edit_execute_key_command (e, cmd, ch);
  274. edit_update_screen (e);
  275. return MSG_HANDLED;
  276. } else if (edit_drop_hotkey_menu (e, parm)) {
  277. return MSG_HANDLED;
  278. } else {
  279. return MSG_NOT_HANDLED;
  280. }
  281. }
  282. case WIDGET_CURSOR:
  283. widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
  284. e->curs_col + e->start_col);
  285. return MSG_HANDLED;
  286. case WIDGET_DESTROY:
  287. edit_clean (e);
  288. return MSG_HANDLED;
  289. default:
  290. return default_proc (msg, parm);
  291. }
  292. }