Split file src/keybind.[ch] to lib/keybind.[ch] and src/keybind-defaults.[ch].
[midnight-commander.git] / src / viewer / display.c
blob64891ebff7ad0a93694c5c8a34600ec72481375d
1 /*
2 Internal file viewer for the Midnight Commander
3 Function for whow info on display
5 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
6 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
8 Written by: 1994, 1995, 1998 Miguel de Icaza
9 1994, 1995 Janne Kukonlehto
10 1995 Jakub Jelinek
11 1996 Joseph M. Hinkle
12 1997 Norbert Warmuth
13 1998 Pavel Machek
14 2004 Roland Illig <roland.illig@gmx.de>
15 2005 Roland Illig <roland.illig@gmx.de>
16 2009 Slava Zanko <slavazanko@google.com>
17 2009 Andrew Borodin <aborodin@vmail.ru>
18 2009, 2010 Ilia Maslakov <il.smind@gmail.com>
20 This file is part of the Midnight Commander.
22 The Midnight Commander is free software; you can redistribute it
23 and/or modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2 of the
25 License, or (at your option) any later version.
27 The Midnight Commander is distributed in the hope that it will be
28 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
29 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
35 MA 02110-1301, USA.
38 #include <config.h>
39 #include <stdint.h> /* uintmax_t */
41 #include "lib/global.h"
42 #include "lib/skin.h"
43 #include "lib/tty/tty.h"
44 #include "lib/tty/key.h"
45 #include "lib/strutil.h"
46 #include "lib/util.h"
47 #include "lib/widget.h"
48 #include "lib/charsets.h"
50 #include "src/setup.h" /* panels_options */
51 #include "src/main.h" /* source_codepage */
53 #include "internal.h"
54 #include "mcviewer.h"
56 /*** global variables ****************************************************************************/
58 /*** file scope macro definitions ****************************************************************/
60 /*** file scope type declarations ****************************************************************/
62 /*** file scope variables ************************************************************************/
64 /* If set, show a ruler */
65 static enum ruler_type
67 RULER_NONE,
68 RULER_TOP,
69 RULER_BOTTOM
70 } ruler = RULER_NONE;
72 /*** file scope functions ************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
75 /** Define labels and handlers for functional keys */
77 static void
78 mcview_set_buttonbar (mcview_t * view)
80 Dlg_head *h = view->widget.owner;
81 WButtonBar *b = find_buttonbar (h);
82 const global_keymap_t *keymap = view->hex_mode ? view->hex_map : view->plain_map;
84 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), keymap, (Widget *) view);
86 if (view->hex_mode)
88 if (view->hexedit_mode)
89 buttonbar_set_label (b, 2, Q_ ("ButtonBar|View"), keymap, (Widget *) view);
90 else if (view->datasource == DS_FILE)
91 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Edit"), keymap, (Widget *) view);
92 else
93 buttonbar_set_label (b, 2, "", keymap, (Widget *) view);
95 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Ascii"), keymap, (Widget *) view);
96 buttonbar_set_label (b, 6, Q_ ("ButtonBar|Save"), keymap, (Widget *) view);
97 buttonbar_set_label (b, 7, Q_ ("ButtonBar|HxSrch"), keymap, (Widget *) view);
100 else
102 buttonbar_set_label (b, 2, view->text_wrap_mode ? Q_ ("ButtonBar|UnWrap")
103 : Q_ ("ButtonBar|Wrap"), keymap, (Widget *) view);
104 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Hex"), keymap, (Widget *) view);
105 buttonbar_set_label (b, 6, "", keymap, (Widget *) view);
106 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), keymap, (Widget *) view);
109 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Goto"), keymap, (Widget *) view);
110 buttonbar_set_label (b, 8, view->magic_mode ? Q_ ("ButtonBar|Raw")
111 : Q_ ("ButtonBar|Parse"), keymap, (Widget *) view);
113 if (mcview_is_in_panel (view))
114 buttonbar_set_label (b, 10, "", keymap, (Widget *) view);
115 else
117 /* don't override some panel buttonbar keys */
118 buttonbar_set_label (b, 3, Q_ ("ButtonBar|Quit"), keymap, (Widget *) view);
119 buttonbar_set_label (b, 9, view->text_nroff_mode ? Q_ ("ButtonBar|Unform")
120 : Q_ ("ButtonBar|Format"), keymap, (Widget *) view);
121 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), keymap, (Widget *) view);
125 /* --------------------------------------------------------------------------------------------- */
127 static void
128 mcview_display_status (mcview_t * view)
130 const screen_dimen top = view->status_area.top;
131 const screen_dimen left = view->status_area.left;
132 const screen_dimen width = view->status_area.width;
133 const screen_dimen height = view->status_area.height;
134 const char *file_label;
135 screen_dimen file_label_width;
137 if (height < 1)
138 return;
140 tty_setcolor (STATUSBAR_COLOR);
141 widget_move (view, top, left);
142 tty_draw_hline (-1, -1, ' ', width);
144 file_label = view->filename ? view->filename : view->command ? view->command : "";
145 file_label_width = str_term_width1 (file_label) - 2;
146 if (width > 40)
148 char buffer[BUF_TINY];
149 widget_move (view, top, width - 32);
150 if (view->hex_mode)
151 tty_printf ("0x%08jx", (uintmax_t) view->hex_cursor);
152 else
154 size_trunc_len (buffer, 5, mcview_get_filesize (view), 0, panels_options.kilobyte_si);
155 tty_printf ("%9ju/%s%s %s", (uintmax_t) view->dpy_end,
156 buffer, mcview_may_still_grow (view) ? "+" : " ",
157 #ifdef HAVE_CHARSET
158 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
159 #else
161 #endif
165 widget_move (view, top, left);
166 if (width > 40)
167 tty_print_string (str_fit_to_term (file_label, width - 34, J_LEFT_FIT));
168 else
169 tty_print_string (str_fit_to_term (file_label, width - 5, J_LEFT_FIT));
170 if (width > 26)
171 mcview_percent (view, view->hex_mode ? view->hex_cursor : view->dpy_end);
174 /* --------------------------------------------------------------------------------------------- */
175 /*** public functions ****************************************************************************/
176 /* --------------------------------------------------------------------------------------------- */
178 void
179 mcview_update (mcview_t * view)
181 static int dirt_limit = 1;
183 if (view->dpy_bbar_dirty)
185 view->dpy_bbar_dirty = FALSE;
186 mcview_set_buttonbar (view);
187 buttonbar_redraw (find_buttonbar (view->widget.owner));
190 if (view->dirty > dirt_limit)
192 /* Too many updates skipped -> force a update */
193 mcview_display (view);
194 view->dirty = 0;
195 /* Raise the update skipping limit */
196 dirt_limit++;
197 if (dirt_limit > mcview_max_dirt_limit)
198 dirt_limit = mcview_max_dirt_limit;
200 else if (view->dirty > 0)
202 if (is_idle ())
204 /* We have time to update the screen properly */
205 mcview_display (view);
206 view->dirty = 0;
207 if (dirt_limit > 1)
208 dirt_limit--;
210 else
212 /* We are busy -> skipping full update,
213 only the status line is updated */
214 mcview_display_status (view);
216 /* Here we had a refresh, if fast scrolling does not work
217 restore the refresh, although this should not happen */
221 /* --------------------------------------------------------------------------------------------- */
222 /** Displays as much data from view->dpy_start as fits on the screen */
224 void
225 mcview_display (mcview_t * view)
227 if (view->hex_mode)
229 mcview_display_hex (view);
231 else if (view->text_nroff_mode)
233 mcview_display_nroff (view);
235 else
237 mcview_display_text (view);
239 mcview_display_status (view);
242 /* --------------------------------------------------------------------------------------------- */
244 void
245 mcview_compute_areas (mcview_t * view)
247 struct area view_area;
248 screen_dimen height, rest, y;
250 /* The viewer is surrounded by a frame of size view->dpy_frame_size.
251 * Inside that frame, there are: The status line (at the top),
252 * the data area and an optional ruler, which is shown above or
253 * below the data area. */
255 view_area.top = view->dpy_frame_size;
256 view_area.left = view->dpy_frame_size;
257 view_area.height = mcview_dimen_doz (view->widget.lines, 2 * view->dpy_frame_size);
258 view_area.width = mcview_dimen_doz (view->widget.cols, 2 * view->dpy_frame_size);
260 /* Most coordinates of the areas equal those of the whole viewer */
261 view->status_area = view_area;
262 view->ruler_area = view_area;
263 view->data_area = view_area;
265 /* Compute the heights of the areas */
266 rest = view_area.height;
268 height = mcview_dimen_min (rest, 1);
269 view->status_area.height = height;
270 rest -= height;
272 height = mcview_dimen_min (rest, (ruler == RULER_NONE || view->hex_mode) ? 0 : 2);
273 view->ruler_area.height = height;
274 rest -= height;
276 view->data_area.height = rest;
278 /* Compute the position of the areas */
279 y = view_area.top;
281 view->status_area.top = y;
282 y += view->status_area.height;
284 if (ruler == RULER_TOP)
286 view->ruler_area.top = y;
287 y += view->ruler_area.height;
290 view->data_area.top = y;
291 y += view->data_area.height;
293 if (ruler == RULER_BOTTOM)
294 view->ruler_area.top = y;
297 /* --------------------------------------------------------------------------------------------- */
299 void
300 mcview_update_bytes_per_line (mcview_t * view)
302 const screen_dimen cols = view->data_area.width;
303 int bytes;
305 if (cols < 8 + 17)
306 bytes = 4;
307 else
308 bytes = 4 * ((cols - 8) / ((cols < 80) ? 17 : 18));
309 assert (bytes != 0);
311 view->bytes_per_line = bytes;
312 view->dirty = mcview_max_dirt_limit + 1; /* To force refresh */
315 /* --------------------------------------------------------------------------------------------- */
317 void
318 mcview_display_toggle_ruler (mcview_t * view)
320 static const enum ruler_type next[3] =
322 RULER_TOP,
323 RULER_BOTTOM,
324 RULER_NONE
327 assert ((size_t) ruler < 3);
328 ruler = next[(size_t) ruler];
329 mcview_compute_areas (view);
330 view->dirty++;
333 /* --------------------------------------------------------------------------------------------- */
335 void
336 mcview_display_clean (mcview_t * view)
338 tty_setcolor (NORMAL_COLOR);
339 widget_erase ((Widget *) view);
340 if (view->dpy_frame_size != 0)
341 tty_draw_box (view->widget.y, view->widget.x, view->widget.lines, view->widget.cols, FALSE);
344 /* --------------------------------------------------------------------------------------------- */
346 void
347 mcview_display_ruler (mcview_t * view)
349 static const char ruler_chars[] = "|----*----";
350 const screen_dimen top = view->ruler_area.top;
351 const screen_dimen left = view->ruler_area.left;
352 const screen_dimen width = view->ruler_area.width;
353 const screen_dimen height = view->ruler_area.height;
354 const screen_dimen line_row = (ruler == RULER_TOP) ? 0 : 1;
355 const screen_dimen nums_row = (ruler == RULER_TOP) ? 1 : 0;
357 char r_buff[10];
358 off_t cl;
359 screen_dimen c;
361 if (ruler == RULER_NONE || height < 1)
362 return;
364 tty_setcolor (VIEW_BOLD_COLOR);
365 for (c = 0; c < width; c++)
367 cl = view->dpy_text_column + c;
368 if (line_row < height)
370 widget_move (view, top + line_row, left + c);
371 tty_print_char (ruler_chars[cl % 10]);
374 if ((cl != 0) && (cl % 10) == 0)
376 g_snprintf (r_buff, sizeof (r_buff), "%ju", (uintmax_t) cl);
377 if (nums_row < height)
379 widget_move (view, top + nums_row, left + c - 1);
380 tty_print_string (r_buff);
384 tty_setcolor (NORMAL_COLOR);
387 /* --------------------------------------------------------------------------------------------- */
389 void
390 mcview_percent (mcview_t * view, off_t p)
392 const screen_dimen top = view->status_area.top;
393 const screen_dimen right = view->status_area.left + view->status_area.width;
394 const screen_dimen height = view->status_area.height;
395 int percent;
396 off_t filesize;
398 if (height < 1 || right < 4)
399 return;
400 if (mcview_may_still_grow (view))
401 return;
402 filesize = mcview_get_filesize (view);
404 if (filesize == 0 || view->dpy_end == filesize)
405 percent = 100;
406 else if (p > (INT_MAX / 100))
407 percent = p / (filesize / 100);
408 else
409 percent = p * 100 / filesize;
411 widget_move (view, top, right - 4);
412 tty_printf ("%3d%%", percent);
415 /* --------------------------------------------------------------------------------------------- */