123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- /* editor initialisation and callback handler.
- Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
- 2007 Free Software Foundation, Inc.
- Authors: 1996, 1997 Paul Sheer
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA.
- */
- /** \file
- * \brief Source: editor initialisation and callback handler
- * \author Paul Sheer
- * \date 1996, 1997
- */
- #include <config.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <string.h>
- #include <ctype.h>
- #include <errno.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include "../src/global.h"
- #include "../src/tty/tty.h" /* LINES, COLS */
- #include "../src/tty/key.h" /* is_idle() */
- #include "edit-impl.h"
- #include "edit-widget.h"
- #include "../src/dialog.h"
- #include "../src/widget.h" /* ButtonBar */
- #include "../src/menu.h" /* menubar_new() */
- #include "../src/cmddef.h"
- WEdit *wedit;
- struct WMenuBar *edit_menubar;
- int column_highlighting = 0;
- static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
- static char *
- edit_get_shortcut (unsigned long command)
- {
- const char *ext_map;
- const char *shortcut = NULL;
- ext_map = lookup_keymap_shortcut (editor_map, CK_Ext_Mode);
- if (ext_map != NULL)
- shortcut = lookup_keymap_shortcut (editor_x_map, command);
- if (shortcut != NULL)
- return g_strdup_printf ("%s %s", ext_map, shortcut);
- shortcut = lookup_keymap_shortcut (editor_map, command);
- if (shortcut != NULL)
- return g_strdup (shortcut);
- return NULL;
- }
- static int
- edit_event (Gpm_Event *event, void *data)
- {
- WEdit *edit = (WEdit *) data;
- /* Unknown event type */
- if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
- return MOU_NORMAL;
- /* rest of the upper frame, the menu is invisible - call menu */
- if ((event->type & GPM_DOWN) && (event->y == 1))
- return edit_menubar->widget.mouse (event, edit_menubar);
- edit_update_curs_row (edit);
- edit_update_curs_col (edit);
- /* Outside editor window */
- if (event->y <= 1 || event->x <= 0
- || event->x > edit->num_widget_columns
- || event->y > edit->num_widget_lines + 1)
- return MOU_NORMAL;
- /* Wheel events */
- if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
- edit_move_up (edit, 2, 1);
- goto update;
- }
- if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
- edit_move_down (edit, 2, 1);
- goto update;
- }
- /* A lone up mustn't do anything */
- if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
- return MOU_NORMAL;
- if (event->type & (GPM_DOWN | GPM_UP))
- edit_push_key_press (edit);
- if (option_cursor_beyond_eol) {
- long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
- edit_eol(edit, edit->curs1));
- if ( event->x > line_len ) {
- edit->over_col = event->x - line_len - edit->start_col - option_line_state_width - 1;
- edit->prev_col = line_len;
- } else {
- edit->over_col = 0;
- edit->prev_col = event->x - option_line_state_width - edit->start_col - 1;
- }
- } else {
- edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
- }
- if (--event->y > (edit->curs_row + 1))
- edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
- else if (event->y < (edit->curs_row + 1))
- edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
- else
- edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
- if (event->type & GPM_DOWN) {
- edit_mark_cmd (edit, 1); /* reset */
- edit->highlight = 0;
- }
- if (!(event->type & GPM_DRAG))
- edit_mark_cmd (edit, 0);
- update:
- edit_find_bracket (edit);
- edit->force |= REDRAW_COMPLETELY;
- edit_update_curs_row (edit);
- edit_update_curs_col (edit);
- edit_update_screen (edit);
- return MOU_NORMAL;
- }
- static void
- edit_adjust_size (Dlg_head *h)
- {
- WEdit *edit;
- WButtonBar *b;
- edit = (WEdit *) find_widget_type (h, edit_callback);
- b = find_buttonbar (h);
- widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
- widget_set_size (&b->widget , LINES - 1, 0, 1, COLS);
- widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
- #ifdef RESIZABLE_MENUBAR
- menubar_arrange (edit_menubar);
- #endif
- }
- static cb_ret_t
- edit_command_execute (WEdit *edit, unsigned long command)
- {
- edit_execute_key_command (edit, command, -1);
- edit_update_screen (edit);
- return MSG_HANDLED;
- }
- /* Callback for the edit dialog */
- static cb_ret_t
- edit_dialog_callback (Dlg_head *h, Widget *sender,
- dlg_msg_t msg, int parm, void *data)
- {
- WEdit *edit;
- WMenuBar *menubar;
- WButtonBar *buttonbar;
- edit = (WEdit *) find_widget_type (h, edit_callback);
- switch (msg) {
- case DLG_RESIZE:
- edit_adjust_size (h);
- return MSG_HANDLED;
- case DLG_VALIDATE:
- if (!edit_ok_to_exit (edit))
- h->running = 1;
- return MSG_HANDLED;
- case DLG_ACTION:
- menubar = find_menubar (h);
- if (sender == (Widget *) menubar)
- return send_message (wedit, WIDGET_COMMAND, parm);
- buttonbar = find_buttonbar (h);
- if (sender == (Widget *) buttonbar)
- return send_message (wedit, WIDGET_COMMAND, parm);
- return MSG_HANDLED;
- default:
- return default_dlg_callback (h, sender, msg, parm, data);
- }
- }
- int
- edit_file (const char *_file, int line)
- {
- static gboolean made_directory = FALSE;
- Dlg_head *edit_dlg;
- if (!made_directory) {
- char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
- made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
- g_free (dir);
- }
- wedit = edit_init (NULL, LINES - 2, COLS, _file, line);
- if (wedit == NULL)
- return 0;
- /* Create a new dialog and add it widgets to it */
- edit_dlg =
- create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
- "[Internal File Editor]", NULL, DLG_WANT_TAB);
- edit_dlg->get_shortcut = edit_get_shortcut;
- edit_menubar = menubar_new (0, 0, COLS, NULL);
- add_widget (edit_dlg, edit_menubar);
- edit_init_menu (edit_menubar);
- init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
- edit_callback, edit_event);
- widget_want_cursor (wedit->widget, 1);
- add_widget (edit_dlg, wedit);
- add_widget (edit_dlg, buttonbar_new (TRUE));
- run_dlg (edit_dlg);
- destroy_dlg (edit_dlg);
- return 1;
- }
- const char *
- edit_get_file_name (const WEdit *edit)
- {
- return edit->filename;
- }
- static void
- edit_set_buttonbar (WEdit *edit)
- {
- WButtonBar *bb = find_buttonbar (edit->widget.parent);
- buttonbar_set_label (bb, 1, Q_("ButtonBar|Help"), editor_map, (Widget *) edit);
- buttonbar_set_label (bb, 2, Q_("ButtonBar|Save"), editor_map, (Widget *) edit);
- buttonbar_set_label (bb, 3, Q_("ButtonBar|Mark"), editor_map, (Widget *) edit);
- buttonbar_set_label (bb, 4, Q_("ButtonBar|Replac"), editor_map, (Widget *) edit);
- buttonbar_set_label (bb, 5, Q_("ButtonBar|Copy"), editor_map, (Widget *) edit);
- buttonbar_set_label (bb, 6, Q_("ButtonBar|Move"), editor_map, (Widget *) edit);
- buttonbar_set_label (bb, 7, Q_("ButtonBar|Search"), editor_map, (Widget *) edit);
- buttonbar_set_label (bb, 8, Q_("ButtonBar|Delete"), editor_map, (Widget *) edit);
- buttonbar_set_label (bb, 9, Q_("ButtonBar|PullDn"), editor_map, (Widget *) edit);
- buttonbar_set_label (bb, 10, Q_("ButtonBar|Quit"), editor_map, (Widget *) edit);
- }
- void
- edit_update_screen (WEdit * e)
- {
- edit_scroll_screen_over_cursor (e);
- edit_update_curs_col (e);
- edit_status (e);
- /* pop all events for this window for internal handling */
- if (!is_idle ())
- e->force |= REDRAW_PAGE;
- else {
- if (e->force & REDRAW_COMPLETELY)
- e->force |= REDRAW_PAGE;
- edit_render_keypress (e);
- }
- }
- static cb_ret_t
- edit_callback (Widget *w, widget_msg_t msg, int parm)
- {
- WEdit *e = (WEdit *) w;
- switch (msg) {
- case WIDGET_INIT:
- e->force |= REDRAW_COMPLETELY;
- edit_set_buttonbar (e);
- return MSG_HANDLED;
- case WIDGET_DRAW:
- e->force |= REDRAW_COMPLETELY;
- e->num_widget_lines = LINES - 2;
- e->num_widget_columns = COLS;
- /* fallthrough */
- case WIDGET_FOCUS:
- edit_update_screen (e);
- return MSG_HANDLED;
- case WIDGET_KEY:
- {
- int cmd, ch;
- /* The user may override the access-keys for the menu bar. */
- if (edit_translate_key (e, parm, &cmd, &ch)) {
- edit_execute_key_command (e, cmd, ch);
- edit_update_screen (e);
- return MSG_HANDLED;
- } else if (edit_drop_hotkey_menu (e, parm)) {
- return MSG_HANDLED;
- } else {
- return MSG_NOT_HANDLED;
- }
- }
- case WIDGET_COMMAND:
- /* command from menubar or buttonbar */
- return edit_command_execute (wedit, parm);
- case WIDGET_CURSOR:
- widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
- e->curs_col + e->start_col + e->over_col +
- EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
- return MSG_HANDLED;
- case WIDGET_DESTROY:
- edit_clean (e);
- return MSG_HANDLED;
- default:
- return default_proc (msg, parm);
- }
- }
|