Merge branch '2161_del_clear_dialog_field'
[midnight-commander.git] / src / viewer / display.c
blob671a18fe630abf576fbc92c0100d07299f4c748e
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>
40 #include "lib/global.h"
41 #include "lib/skin.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/key.h"
44 #include "lib/strutil.h"
46 #include "src/dialog.h" /* Dlg_head */
47 #include "src/widget.h" /* WButtonBar */
48 #include "src/charsets.h"
49 #include "src/setup.h" /* panels_options */
50 #include "src/main.h" /* source_codepage */
52 #include "internal.h"
53 #include "mcviewer.h"
55 /*** global variables ****************************************************************************/
57 /*** file scope macro definitions ****************************************************************/
59 /*** file scope type declarations ****************************************************************/
61 /*** file scope variables ************************************************************************/
63 /* If set, show a ruler */
64 static enum ruler_type
66 RULER_NONE,
67 RULER_TOP,
68 RULER_BOTTOM
69 } ruler = RULER_NONE;
71 /*** file scope functions ************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
75 /* Define labels and handlers for functional keys */
76 static void
77 mcview_set_buttonbar (mcview_t * view)
79 Dlg_head *h = view->widget.parent;
80 WButtonBar *b = find_buttonbar (h);
81 const global_keymap_t *keymap = view->hex_mode ? view->hex_map : view->plain_map;
83 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), keymap, (Widget *) view);
85 if (view->hex_mode)
87 if (view->hexedit_mode)
88 buttonbar_set_label (b, 2, Q_ ("ButtonBar|View"), keymap, (Widget *) view);
89 else if (view->datasource == DS_FILE)
90 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Edit"), keymap, (Widget *) view);
91 else
92 buttonbar_set_label (b, 2, "", keymap, (Widget *) view);
94 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Ascii"), keymap, (Widget *) view);
95 buttonbar_set_label (b, 6, Q_ ("ButtonBar|Save"), keymap, (Widget *) view);
96 buttonbar_set_label (b, 7, Q_ ("ButtonBar|HxSrch"), keymap, (Widget *) view);
99 else
101 buttonbar_set_label (b, 2, view->text_wrap_mode ? Q_ ("ButtonBar|UnWrap")
102 : Q_ ("ButtonBar|Wrap"), keymap, (Widget *) view);
103 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Hex"), keymap, (Widget *) view);
104 buttonbar_set_label (b, 6, "", keymap, (Widget *) view);
105 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), keymap, (Widget *) view);
108 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Goto"), keymap, (Widget *) view);
109 buttonbar_set_label (b, 8, view->magic_mode ? Q_ ("ButtonBar|Raw")
110 : Q_ ("ButtonBar|Parse"), keymap, (Widget *) view);
112 if (mcview_is_in_panel (view))
113 buttonbar_set_label (b, 10, "", keymap, (Widget *) view);
114 else
116 /* don't override some panel buttonbar keys */
117 buttonbar_set_label (b, 3, Q_ ("ButtonBar|Quit"), keymap, (Widget *) view);
118 buttonbar_set_label (b, 9, view->text_nroff_mode ? Q_ ("ButtonBar|Unform")
119 : Q_ ("ButtonBar|Format"), keymap, (Widget *) view);
120 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), keymap, (Widget *) view);
124 /* --------------------------------------------------------------------------------------------- */
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 (SELECTED_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)
152 tty_printf ("0x%08lx", (unsigned long) view->hex_cursor);
154 else
156 size_trunc_len (buffer, 5, mcview_get_filesize (view), 0, panels_options.kilobyte_si);
157 tty_printf ("%9lli/%s%s %s", view->dpy_end,
158 buffer, mcview_may_still_grow (view) ? "+" : " ",
159 #ifdef HAVE_CHARSET
160 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
161 #else
163 #endif
167 widget_move (view, top, left);
168 if (width > 40)
169 tty_print_string (str_fit_to_term (file_label, width - 34, J_LEFT_FIT));
170 else
171 tty_print_string (str_fit_to_term (file_label, width - 5, J_LEFT_FIT));
172 if (width > 26)
173 mcview_percent (view, view->hex_mode ? view->hex_cursor : view->dpy_end);
176 /* --------------------------------------------------------------------------------------------- */
178 /*** public functions ****************************************************************************/
180 /* --------------------------------------------------------------------------------------------- */
182 void
183 mcview_update (mcview_t * view)
185 static int dirt_limit = 1;
187 if (view->dpy_bbar_dirty)
189 view->dpy_bbar_dirty = FALSE;
190 mcview_set_buttonbar (view);
191 buttonbar_redraw (find_buttonbar (view->widget.parent));
194 if (view->dirty > dirt_limit)
196 /* Too many updates skipped -> force a update */
197 mcview_display (view);
198 view->dirty = 0;
199 /* Raise the update skipping limit */
200 dirt_limit++;
201 if (dirt_limit > mcview_max_dirt_limit)
202 dirt_limit = mcview_max_dirt_limit;
204 else if (view->dirty > 0)
206 if (is_idle ())
208 /* We have time to update the screen properly */
209 mcview_display (view);
210 view->dirty = 0;
211 if (dirt_limit > 1)
212 dirt_limit--;
214 else
216 /* We are busy -> skipping full update,
217 only the status line is updated */
218 mcview_display_status (view);
220 /* Here we had a refresh, if fast scrolling does not work
221 restore the refresh, although this should not happen */
225 /* --------------------------------------------------------------------------------------------- */
227 /* Displays as much data from view->dpy_start as fits on the screen */
228 void
229 mcview_display (mcview_t * view)
231 if (view->hex_mode)
233 mcview_display_hex (view);
235 else if (view->text_nroff_mode)
237 mcview_display_nroff (view);
239 else
241 mcview_display_text (view);
243 mcview_display_status (view);
246 /* --------------------------------------------------------------------------------------------- */
248 void
249 mcview_compute_areas (mcview_t * view)
251 struct area view_area;
252 screen_dimen height, rest, y;
254 /* The viewer is surrounded by a frame of size view->dpy_frame_size.
255 * Inside that frame, there are: The status line (at the top),
256 * the data area and an optional ruler, which is shown above or
257 * below the data area. */
259 view_area.top = view->dpy_frame_size;
260 view_area.left = view->dpy_frame_size;
261 view_area.height = mcview_dimen_doz (view->widget.lines, 2 * view->dpy_frame_size);
262 view_area.width = mcview_dimen_doz (view->widget.cols, 2 * view->dpy_frame_size);
264 /* Most coordinates of the areas equal those of the whole viewer */
265 view->status_area = view_area;
266 view->ruler_area = view_area;
267 view->data_area = view_area;
269 /* Compute the heights of the areas */
270 rest = view_area.height;
272 height = mcview_dimen_min (rest, 1);
273 view->status_area.height = height;
274 rest -= height;
276 height = mcview_dimen_min (rest, (ruler == RULER_NONE || view->hex_mode) ? 0 : 2);
277 view->ruler_area.height = height;
278 rest -= height;
280 view->data_area.height = rest;
282 /* Compute the position of the areas */
283 y = view_area.top;
285 view->status_area.top = y;
286 y += view->status_area.height;
288 if (ruler == RULER_TOP)
290 view->ruler_area.top = y;
291 y += view->ruler_area.height;
294 view->data_area.top = y;
295 y += view->data_area.height;
297 if (ruler == RULER_BOTTOM)
298 view->ruler_area.top = y;
301 /* --------------------------------------------------------------------------------------------- */
303 void
304 mcview_update_bytes_per_line (mcview_t * view)
306 const screen_dimen cols = view->data_area.width;
307 int bytes;
309 if (cols < 8 + 17)
310 bytes = 4;
311 else
312 bytes = 4 * ((cols - 8) / ((cols < 80) ? 17 : 18));
313 assert (bytes != 0);
315 view->bytes_per_line = bytes;
316 view->dirty = mcview_max_dirt_limit + 1; /* To force refresh */
319 /* --------------------------------------------------------------------------------------------- */
321 void
322 mcview_display_toggle_ruler (mcview_t * view)
324 static const enum ruler_type next[3] =
326 RULER_TOP,
327 RULER_BOTTOM,
328 RULER_NONE
331 assert ((size_t) ruler < 3);
332 ruler = next[(size_t) ruler];
333 mcview_compute_areas (view);
334 view->dirty++;
337 /* --------------------------------------------------------------------------------------------- */
339 void
340 mcview_display_clean (mcview_t * view)
342 tty_setcolor (NORMAL_COLOR);
343 widget_erase ((Widget *) view);
344 if (view->dpy_frame_size != 0)
345 tty_draw_box (view->widget.y, view->widget.x, view->widget.lines, view->widget.cols, FALSE);
348 /* --------------------------------------------------------------------------------------------- */
350 void
351 mcview_display_ruler (mcview_t * view)
353 static const char ruler_chars[] = "|----*----";
354 const screen_dimen top = view->ruler_area.top;
355 const screen_dimen left = view->ruler_area.left;
356 const screen_dimen width = view->ruler_area.width;
357 const screen_dimen height = view->ruler_area.height;
358 const screen_dimen line_row = (ruler == RULER_TOP) ? 0 : 1;
359 const screen_dimen nums_row = (ruler == RULER_TOP) ? 1 : 0;
361 char r_buff[10];
362 off_t cl;
363 screen_dimen c;
365 if (ruler == RULER_NONE || height < 1)
366 return;
368 tty_setcolor (MARKED_COLOR);
369 for (c = 0; c < width; c++)
371 cl = view->dpy_text_column + c;
372 if (line_row < height)
374 widget_move (view, top + line_row, left + c);
375 tty_print_char (ruler_chars[cl % 10]);
378 if ((cl != 0) && (cl % 10) == 0)
380 g_snprintf (r_buff, sizeof (r_buff), "%" OFFSETTYPE_PRId, (long unsigned int) cl);
381 if (nums_row < height)
383 widget_move (view, top + nums_row, left + c - 1);
384 tty_print_string (r_buff);
388 tty_setcolor (NORMAL_COLOR);
391 /* --------------------------------------------------------------------------------------------- */
393 void
394 mcview_percent (mcview_t * view, off_t p)
396 const screen_dimen top = view->status_area.top;
397 const screen_dimen right = view->status_area.left + view->status_area.width;
398 const screen_dimen height = view->status_area.height;
399 int percent;
400 off_t filesize;
402 if (height < 1 || right < 4)
403 return;
404 if (mcview_may_still_grow (view))
405 return;
406 filesize = mcview_get_filesize (view);
408 if (filesize == 0 || view->dpy_end == filesize)
409 percent = 100;
410 else if (p > (INT_MAX / 100))
411 percent = p / (filesize / 100);
412 else
413 percent = p * 100 / filesize;
415 widget_move (view, top, right - 4);
416 tty_printf ("%3d%%", percent);
419 /* --------------------------------------------------------------------------------------------- */