editwidget.c 9.4 KB

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