editwidget.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. shortcut = lookup_keymap_shortcut (editor_map, command);
  52. if (shortcut != NULL)
  53. return g_strdup (shortcut);
  54. ext_map = lookup_keymap_shortcut (editor_map, CK_Ext_Mode);
  55. if (ext_map != NULL)
  56. shortcut = lookup_keymap_shortcut (editor_x_map, command);
  57. if (shortcut != NULL)
  58. return g_strdup_printf ("%s %s", ext_map, 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 cb_ret_t
  126. edit_command_execute (WEdit *edit, unsigned long command)
  127. {
  128. if (command == CK_Menu)
  129. edit_menu_cmd (edit);
  130. else {
  131. edit_execute_key_command (edit, command, -1);
  132. edit_update_screen (edit);
  133. }
  134. return MSG_HANDLED;
  135. }
  136. static inline void
  137. edit_set_buttonbar (WEdit *edit, WButtonBar *bb)
  138. {
  139. buttonbar_set_label (bb, 1, Q_("ButtonBar|Help"), editor_map, (Widget *) edit);
  140. buttonbar_set_label (bb, 2, Q_("ButtonBar|Save"), editor_map, (Widget *) edit);
  141. buttonbar_set_label (bb, 3, Q_("ButtonBar|Mark"), editor_map, (Widget *) edit);
  142. buttonbar_set_label (bb, 4, Q_("ButtonBar|Replac"), editor_map, (Widget *) edit);
  143. buttonbar_set_label (bb, 5, Q_("ButtonBar|Copy"), editor_map, (Widget *) edit);
  144. buttonbar_set_label (bb, 6, Q_("ButtonBar|Move"), editor_map, (Widget *) edit);
  145. buttonbar_set_label (bb, 7, Q_("ButtonBar|Search"), editor_map, (Widget *) edit);
  146. buttonbar_set_label (bb, 8, Q_("ButtonBar|Delete"), editor_map, (Widget *) edit);
  147. buttonbar_set_label (bb, 9, Q_("ButtonBar|PullDn"), editor_map, (Widget *) edit);
  148. buttonbar_set_label (bb, 10, Q_("ButtonBar|Quit"), editor_map, (Widget *) edit);
  149. }
  150. /* Callback for the edit dialog */
  151. static cb_ret_t
  152. edit_dialog_callback (Dlg_head *h, Widget *sender,
  153. dlg_msg_t msg, int parm, void *data)
  154. {
  155. WEdit *edit;
  156. WMenuBar *menubar;
  157. WButtonBar *buttonbar;
  158. edit = (WEdit *) find_widget_type (h, edit_callback);
  159. menubar = find_menubar (h);
  160. buttonbar = find_buttonbar (h);
  161. switch (msg) {
  162. case DLG_INIT:
  163. edit_set_buttonbar (edit, buttonbar);
  164. return MSG_HANDLED;
  165. case DLG_RESIZE:
  166. widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
  167. widget_set_size (&buttonbar->widget , LINES - 1, 0, 1, COLS);
  168. widget_set_size (&menubar->widget, 0, 0, 1, COLS);
  169. menubar_arrange (menubar);
  170. return MSG_HANDLED;
  171. case DLG_ACTION:
  172. if (sender == (Widget *) menubar)
  173. return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
  174. if (sender == (Widget *) buttonbar)
  175. return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
  176. return MSG_HANDLED;
  177. case DLG_VALIDATE:
  178. if (!edit_ok_to_exit (edit))
  179. h->running = 1;
  180. return MSG_HANDLED;
  181. default:
  182. return default_dlg_callback (h, sender, msg, parm, data);
  183. }
  184. }
  185. int
  186. edit_file (const char *_file, int line)
  187. {
  188. static gboolean made_directory = FALSE;
  189. Dlg_head *edit_dlg;
  190. if (!made_directory) {
  191. char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
  192. made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
  193. g_free (dir);
  194. }
  195. wedit = edit_init (NULL, LINES - 2, COLS, _file, line);
  196. if (wedit == NULL)
  197. return 0;
  198. /* Create a new dialog and add it widgets to it */
  199. edit_dlg =
  200. create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
  201. "[Internal File Editor]", NULL, DLG_WANT_TAB);
  202. edit_dlg->get_shortcut = edit_get_shortcut;
  203. edit_menubar = menubar_new (0, 0, COLS, NULL);
  204. add_widget (edit_dlg, edit_menubar);
  205. edit_init_menu (edit_menubar);
  206. init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
  207. edit_callback, edit_event);
  208. widget_want_cursor (wedit->widget, 1);
  209. add_widget (edit_dlg, wedit);
  210. add_widget (edit_dlg, buttonbar_new (TRUE));
  211. run_dlg (edit_dlg);
  212. destroy_dlg (edit_dlg);
  213. return 1;
  214. }
  215. const char *
  216. edit_get_file_name (const WEdit *edit)
  217. {
  218. return edit->filename;
  219. }
  220. void
  221. edit_update_screen (WEdit * e)
  222. {
  223. edit_scroll_screen_over_cursor (e);
  224. edit_update_curs_col (e);
  225. edit_status (e);
  226. /* pop all events for this window for internal handling */
  227. if (!is_idle ())
  228. e->force |= REDRAW_PAGE;
  229. else {
  230. if (e->force & REDRAW_COMPLETELY)
  231. e->force |= REDRAW_PAGE;
  232. edit_render_keypress (e);
  233. }
  234. }
  235. static cb_ret_t
  236. edit_callback (Widget *w, widget_msg_t msg, int parm)
  237. {
  238. WEdit *e = (WEdit *) w;
  239. switch (msg) {
  240. case WIDGET_DRAW:
  241. e->force |= REDRAW_COMPLETELY;
  242. e->num_widget_lines = LINES - 2;
  243. e->num_widget_columns = COLS;
  244. /* fallthrough */
  245. case WIDGET_FOCUS:
  246. edit_update_screen (e);
  247. return MSG_HANDLED;
  248. case WIDGET_KEY:
  249. {
  250. int cmd, ch;
  251. cb_ret_t ret = MSG_NOT_HANDLED;
  252. /* The user may override the access-keys for the menu bar. */
  253. if (edit_translate_key (e, parm, &cmd, &ch)) {
  254. edit_execute_key_command (e, cmd, ch);
  255. edit_update_screen (e);
  256. ret = MSG_HANDLED;
  257. } else if (edit_drop_hotkey_menu (e, parm))
  258. ret = MSG_HANDLED;
  259. return ret;
  260. }
  261. case WIDGET_COMMAND:
  262. /* command from menubar or buttonbar */
  263. return edit_command_execute (e, parm);
  264. case WIDGET_CURSOR:
  265. widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
  266. e->curs_col + e->start_col + e->over_col +
  267. EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
  268. return MSG_HANDLED;
  269. case WIDGET_DESTROY:
  270. edit_clean (e);
  271. return MSG_HANDLED;
  272. default:
  273. return default_proc (msg, parm);
  274. }
  275. }