editwidget.c 10 KB

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