Original patch as attached on the bugreport
[midnight-commander.git] / src / win.c
blob86cf05f9628ece6b310ba89149848b0ba04fe3d6
1 /* Curses utilities
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007 Free Software Foundation, Inc.
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.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 #include <config.h>
21 #include <stdio.h>
22 #include <string.h>
24 #ifdef HAVE_TERMIOS_H
25 #include <termios.h>
26 #endif
28 #include "global.h"
29 #include "tty.h"
30 #include "color.h"
31 #include "mouse.h"
32 #include "dialog.h"
33 #include "widget.h"
34 #include "win.h"
35 #include "key.h" /* XCTRL and ALT macros */
36 #include "layout.h"
39 * Common handler for standard movement keys in a text area. Provided
40 * functions are called with the "data" argument. backfn and forfn also
41 * get an argument indicating how many lines to scroll. Return 1 if
42 * the key was handled, 0 otherwise.
44 int
45 check_movement_keys (int key, int page_size, void *data, movefn backfn,
46 movefn forfn, movefn topfn, movefn bottomfn)
48 switch (key) {
49 case KEY_UP:
50 case XCTRL ('p'):
51 (*backfn) (data, 1);
52 break;
54 case KEY_DOWN:
55 case XCTRL ('n'):
56 (*forfn) (data, 1);
57 break;
59 case KEY_PPAGE:
60 case ALT ('v'):
61 (*backfn) (data, page_size - 1);
62 break;
64 case KEY_NPAGE:
65 case XCTRL ('v'):
66 (*forfn) (data, page_size - 1);
67 break;
69 case KEY_HOME:
70 case KEY_M_CTRL | KEY_HOME:
71 case KEY_M_CTRL | KEY_PPAGE:
72 case KEY_A1:
73 case ALT ('<'):
74 (*topfn) (data, 0);
75 break;
77 case KEY_END:
78 case KEY_M_CTRL | KEY_END:
79 case KEY_M_CTRL | KEY_NPAGE:
80 case KEY_C1:
81 case ALT ('>'):
82 (*bottomfn) (data, 0);
83 break;
85 case 'b':
86 case KEY_BACKSPACE:
87 (*backfn) (data, page_size - 1);
88 break;
90 case ' ':
91 (*forfn) (data, page_size - 1);
92 break;
94 case 'u':
95 (*backfn) (data, page_size / 2);
96 break;
98 case 'd':
99 (*forfn) (data, page_size / 2);
100 break;
102 case 'g':
103 (*topfn) (data, 0);
104 break;
106 case 'G':
107 (*bottomfn) (data, 0);
108 break;
110 default:
111 return MSG_NOT_HANDLED;
113 return MSG_HANDLED;
116 /* Classification routines */
117 int is_abort_char (int c)
119 return (c == XCTRL('c') || c == XCTRL('g') || c == ESC_CHAR ||
120 c == KEY_F(10));
123 void mc_raw_mode (void)
125 raw ();
128 void mc_noraw_mode (void)
130 noraw ();
133 /* This flag is set by xterm detection routine in function main() */
134 /* It is used by function view_other_cmd() */
135 int xterm_flag = 0;
137 /* The following routines only work on xterm terminals */
139 void do_enter_ca_mode (void)
141 if (!xterm_flag)
142 return;
143 fprintf (stdout, /* ESC_STR ")0" */ ESC_STR "7" ESC_STR "[?47h");
144 fflush (stdout);
147 void do_exit_ca_mode (void)
149 if (!xterm_flag)
150 return;
151 fprintf (stdout, ESC_STR "[?47l" ESC_STR "8" ESC_STR "[m");
152 fflush (stdout);
155 /* This table is a mapping between names and the constants we use
156 * We use this to allow users to define alternate definitions for
157 * certain keys that may be missing from the terminal database
159 key_code_name_t key_name_conv_tab [] = {
160 /* KEY_F(0) is not here, since we are mapping it to f10, so there is no reason
161 to define f0 as well. Also, it makes Learn keys a bunch of problems :( */
162 { KEY_F(1), "f1", N_("Function key 1") },
163 { KEY_F(2), "f2", N_("Function key 2") },
164 { KEY_F(3), "f3", N_("Function key 3") },
165 { KEY_F(4), "f4", N_("Function key 4") },
166 { KEY_F(5), "f5", N_("Function key 5") },
167 { KEY_F(6), "f6", N_("Function key 6") },
168 { KEY_F(7), "f7", N_("Function key 7") },
169 { KEY_F(8), "f8", N_("Function key 8") },
170 { KEY_F(9), "f9", N_("Function key 9") },
171 { KEY_F(10), "f10", N_("Function key 10") },
172 { KEY_F(11), "f11", N_("Function key 11") },
173 { KEY_F(12), "f12", N_("Function key 12") },
174 { KEY_F(13), "f13", N_("Function key 13") },
175 { KEY_F(14), "f14", N_("Function key 14") },
176 { KEY_F(15), "f15", N_("Function key 15") },
177 { KEY_F(16), "f16", N_("Function key 16") },
178 { KEY_F(17), "f17", N_("Function key 17") },
179 { KEY_F(18), "f18", N_("Function key 18") },
180 { KEY_F(19), "f19", N_("Function key 19") },
181 { KEY_F(20), "f20", N_("Function key 20") },
182 { KEY_BACKSPACE, "bs", N_("Backspace key") },
183 { KEY_END, "end", N_("End key") },
184 { KEY_UP, "up", N_("Up arrow key") },
185 { KEY_DOWN, "down", N_("Down arrow key") },
186 { KEY_LEFT, "left", N_("Left arrow key") },
187 { KEY_RIGHT, "right", N_("Right arrow key") },
188 { KEY_HOME, "home", N_("Home key") },
189 { KEY_NPAGE, "pgdn", N_("Page Down key") },
190 { KEY_PPAGE, "pgup", N_("Page Up key") },
191 { KEY_IC, "insert", N_("Insert key") },
192 { KEY_DC, "delete", N_("Delete key") },
193 { ALT('\t'), "complete", N_("Completion/M-tab") },
194 { KEY_KP_ADD, "kpplus", N_("+ on keypad") },
195 { KEY_KP_SUBTRACT,"kpminus", N_("- on keypad") },
196 { KEY_KP_MULTIPLY,"kpasterix", N_("* on keypad") },
197 /* From here on, these won't be shown in Learn keys (no space) */
198 { KEY_LEFT, "kpleft", N_("Left arrow keypad") },
199 { KEY_RIGHT, "kpright", N_("Right arrow keypad") },
200 { KEY_UP, "kpup", N_("Up arrow keypad") },
201 { KEY_DOWN, "kpdown", N_("Down arrow keypad") },
202 { KEY_HOME, "kphome", N_("Home on keypad") },
203 { KEY_END, "kpend", N_("End on keypad") },
204 { KEY_NPAGE, "kpnpage", N_("Page Down keypad") },
205 { KEY_PPAGE, "kpppage", N_("Page Up keypad") },
206 { KEY_IC, "kpinsert", N_("Insert on keypad") },
207 { KEY_DC, "kpdelete", N_("Delete on keypad") },
208 { (int) '\n', "kpenter", N_("Enter on keypad") },
209 { (int) '/', "kpslash", N_("Slash on keypad") },
210 { (int) '#', "kpnumlock", N_("NumLock on keypad") },
211 { 0, 0, 0 }
214 /* Return the code associated with the symbolic name keyname */
215 int lookup_key (char *keyname)
217 int i;
219 for (i = 0; key_name_conv_tab [i].code; i++){
220 if ( g_strcasecmp (key_name_conv_tab [i].name, keyname))
221 continue;
222 return key_name_conv_tab [i].code;
224 return 0;