editwidget.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* editor initialisation and callback handler.
  2. Copyright (C) 1996, 1997 the Free Software Foundation
  3. Authors: 1996, 1997 Paul Sheer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307, USA.
  16. */
  17. #include <config.h>
  18. #include "edit.h"
  19. #include "edit-widget.h"
  20. #include "src/tty.h" /* LINES */
  21. #include "src/widget.h" /* redraw_labels() */
  22. #include "src/menu.h" /* menubar_new() */
  23. WEdit *wedit;
  24. struct WMenu *edit_menubar;
  25. int column_highlighting = 0;
  26. static int edit_callback (WEdit *edit, int msg, int par);
  27. static int
  28. edit_event (WEdit * edit, Gpm_Event * event, int *result)
  29. {
  30. *result = MOU_NORMAL;
  31. edit_update_curs_row (edit);
  32. edit_update_curs_col (edit);
  33. /* Unknown event type */
  34. if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
  35. return 0;
  36. /* Wheel events */
  37. if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
  38. edit_move_up (edit, 2, 1);
  39. goto update;
  40. }
  41. if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
  42. edit_move_down (edit, 2, 1);
  43. goto update;
  44. }
  45. /* Outside editor window */
  46. if (event->y <= 1 || event->x <= 0
  47. || event->x > edit->num_widget_columns
  48. || event->y > edit->num_widget_lines + 1)
  49. return 0;
  50. /* A lone up mustn't do anything */
  51. if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
  52. return 1;
  53. if (event->type & (GPM_DOWN | GPM_UP))
  54. edit_push_key_press (edit);
  55. edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
  56. if (--event->y > (edit->curs_row + 1))
  57. edit_cursor_move (edit,
  58. edit_move_forward (edit, edit->curs1,
  59. event->y - (edit->curs_row +
  60. 1), 0)
  61. - edit->curs1);
  62. if (event->y < (edit->curs_row + 1))
  63. edit_cursor_move (edit,
  64. edit_move_backward (edit, edit->curs1,
  65. (edit->curs_row + 1) -
  66. event->y) - edit->curs1);
  67. edit_cursor_move (edit,
  68. (int) edit_move_forward3 (edit, edit->curs1,
  69. event->x -
  70. edit->start_col - 1,
  71. 0) - edit->curs1);
  72. edit->prev_col = edit_get_col (edit);
  73. if (event->type & GPM_DOWN) {
  74. edit_mark_cmd (edit, 1); /* reset */
  75. edit->highlight = 0;
  76. }
  77. if (!(event->type & GPM_DRAG))
  78. edit_mark_cmd (edit, 0);
  79. update:
  80. edit->force |= REDRAW_COMPLETELY;
  81. edit_update_curs_row (edit);
  82. edit_update_curs_col (edit);
  83. edit_update_screen (edit);
  84. return 1;
  85. }
  86. static int
  87. edit_mouse_event (Gpm_Event *event, void *x)
  88. {
  89. int result;
  90. if (edit_event ((WEdit *) x, event, &result))
  91. return result;
  92. else
  93. return (*edit_menubar->widget.mouse) (event, edit_menubar);
  94. }
  95. static void
  96. edit_adjust_size (Dlg_head *h)
  97. {
  98. WEdit *edit;
  99. WButtonBar *edit_bar;
  100. edit = (WEdit *) find_widget_type (h, (callback_fn) edit_callback);
  101. edit_bar = find_buttonbar (h);
  102. widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
  103. widget_set_size (&edit_bar->widget, LINES - 1, 0, 1, COLS);
  104. widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
  105. #ifdef RESIZABLE_MENUBAR
  106. menubar_arrange (edit_menubar);
  107. #endif
  108. }
  109. /* Callback for the edit dialog */
  110. static int
  111. edit_dialog_callback (Dlg_head * h, int id, int msg)
  112. {
  113. switch (msg) {
  114. case DLG_RESIZE:
  115. edit_adjust_size (h);
  116. return MSG_HANDLED;
  117. }
  118. return default_dlg_callback (h, id, msg);
  119. }
  120. int
  121. edit (const char *_file, int line)
  122. {
  123. static int made_directory = 0;
  124. int framed = 0;
  125. Dlg_head *edit_dlg;
  126. WButtonBar *edit_bar;
  127. if (option_backup_ext_int != -1) {
  128. option_backup_ext = g_malloc (sizeof (int) + 1);
  129. option_backup_ext[sizeof (int)] = '\0';
  130. memcpy (option_backup_ext, (char *) &option_backup_ext_int,
  131. sizeof (int));
  132. }
  133. if (!made_directory) {
  134. mkdir (catstrs (home_dir, EDIT_DIR, 0), 0700);
  135. made_directory = 1;
  136. }
  137. if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
  138. return 0;
  139. }
  140. /* Create a new dialog and add it widgets to it */
  141. edit_dlg =
  142. create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
  143. "[Internal File Editor]", NULL, DLG_WANT_TAB);
  144. init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
  145. (callback_fn) edit_callback, (destroy_fn) edit_clean,
  146. (mouse_h) edit_mouse_event, 0);
  147. widget_want_cursor (wedit->widget, 1);
  148. edit_bar = buttonbar_new (1);
  149. if (!framed) {
  150. switch (edit_key_emulation) {
  151. case EDIT_KEY_EMULATION_NORMAL:
  152. edit_init_menu_normal (); /* editmenu.c */
  153. break;
  154. case EDIT_KEY_EMULATION_EMACS:
  155. edit_init_menu_emacs (); /* editmenu.c */
  156. break;
  157. }
  158. edit_menubar = menubar_new (0, 0, COLS, EditMenuBar, N_menus);
  159. }
  160. add_widget (edit_dlg, wedit);
  161. if (!framed)
  162. add_widget (edit_dlg, edit_menubar);
  163. add_widget (edit_dlg, edit_bar);
  164. run_dlg (edit_dlg);
  165. if (!framed)
  166. edit_done_menu (); /* editmenu.c */
  167. destroy_dlg (edit_dlg);
  168. return 1;
  169. }
  170. static void edit_my_define (Dlg_head * h, int idx, char *text,
  171. void (*fn) (WEdit *), WEdit * edit)
  172. {
  173. define_label_data (h, idx, text, (buttonbarfn) fn, edit);
  174. }
  175. static void cmd_F1 (WEdit * edit)
  176. {
  177. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
  178. }
  179. static void cmd_F2 (WEdit * edit)
  180. {
  181. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
  182. }
  183. static void cmd_F3 (WEdit * edit)
  184. {
  185. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
  186. }
  187. static void cmd_F4 (WEdit * edit)
  188. {
  189. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
  190. }
  191. static void cmd_F5 (WEdit * edit)
  192. {
  193. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
  194. }
  195. static void cmd_F6 (WEdit * edit)
  196. {
  197. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
  198. }
  199. static void cmd_F7 (WEdit * edit)
  200. {
  201. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
  202. }
  203. static void cmd_F8 (WEdit * edit)
  204. {
  205. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
  206. }
  207. #if 0
  208. static void cmd_F9 (WEdit * edit)
  209. {
  210. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
  211. }
  212. #endif
  213. static void cmd_F10 (WEdit * edit)
  214. {
  215. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
  216. }
  217. static void
  218. edit_labels (WEdit *edit)
  219. {
  220. Dlg_head *h = edit->widget.parent;
  221. edit_my_define (h, 1, _("Help"), cmd_F1, edit);
  222. edit_my_define (h, 2, _("Save"), cmd_F2, edit);
  223. edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
  224. edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
  225. edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
  226. edit_my_define (h, 6, _("Move"), cmd_F6, edit);
  227. edit_my_define (h, 7, _("Search"), cmd_F7, edit);
  228. edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
  229. if (!edit->have_frame)
  230. edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
  231. edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
  232. redraw_labels (h);
  233. }
  234. void edit_update_screen (WEdit * e)
  235. {
  236. edit_scroll_screen_over_cursor (e);
  237. edit_update_curs_col (e);
  238. edit_status (e);
  239. /* pop all events for this window for internal handling */
  240. if (!is_idle ()) {
  241. e->force |= REDRAW_PAGE;
  242. return;
  243. }
  244. if (e->force & REDRAW_COMPLETELY)
  245. e->force |= REDRAW_PAGE;
  246. edit_render_keypress (e);
  247. }
  248. static int edit_callback (WEdit *e, int msg, int par)
  249. {
  250. switch (msg) {
  251. case WIDGET_INIT:
  252. e->force |= REDRAW_COMPLETELY;
  253. edit_labels (e);
  254. break;
  255. case WIDGET_DRAW:
  256. e->force |= REDRAW_COMPLETELY;
  257. e->num_widget_lines = LINES - 2;
  258. e->num_widget_columns = COLS;
  259. case WIDGET_FOCUS:
  260. edit_update_screen (e);
  261. return 1;
  262. case WIDGET_KEY:{
  263. int cmd, ch;
  264. if (edit_drop_hotkey_menu (e, par)) /* first check alt-f, alt-e, alt-s, etc for drop menus */
  265. return 1;
  266. if (!edit_translate_key (e, 0, par, &cmd, &ch))
  267. return 0;
  268. edit_execute_key_command (e, cmd, ch);
  269. edit_update_screen (e);
  270. }
  271. return 1;
  272. case WIDGET_COMMAND:
  273. edit_execute_key_command (e, par, -1);
  274. edit_update_screen (e);
  275. return 1;
  276. case WIDGET_CURSOR:
  277. widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET, e->curs_col + e->start_col);
  278. return 1;
  279. }
  280. return default_proc (msg, par);
  281. }