editwidget.c 9.8 KB

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