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
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,
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/key.h"
44 #include "lib/strutil.h"
47 #include "src/dialog.h" /* Dlg_head */
48 #include "src/charsets.h"
49 #include "src/widget.h" /* WButtonBar */
54 /*** global variables ****************************************************************************/
56 /*** file scope macro definitions ****************************************************************/
58 /*** file scope type declarations ****************************************************************/
60 /*** file scope variables ************************************************************************/
62 /* If set, show a ruler */
63 static enum ruler_type
70 /*** file scope functions ************************************************************************/
72 /* --------------------------------------------------------------------------------------------- */
74 /* Define labels and handlers for functional keys */
76 mcview_set_buttonbar (mcview_t
* view
)
78 Dlg_head
*h
= view
->widget
.parent
;
79 WButtonBar
*b
= find_buttonbar (h
);
80 const global_keymap_t
*keymap
= view
->hex_mode
? view
->hex_map
: view
->plain_map
;
82 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), keymap
, (Widget
*) view
);
86 if (view
->hexedit_mode
)
87 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|View"), keymap
, (Widget
*) view
);
88 else if (view
->datasource
== DS_FILE
)
89 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Edit"), keymap
, (Widget
*) view
);
91 buttonbar_set_label (b
, 2, "", keymap
, (Widget
*) view
);
93 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Ascii"), keymap
, (Widget
*) view
);
94 buttonbar_set_label (b
, 6, Q_ ("ButtonBar|Save"), keymap
, (Widget
*) view
);
95 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|HxSrch"), keymap
, (Widget
*) view
);
100 buttonbar_set_label (b
, 2, view
->text_wrap_mode
? Q_ ("ButtonBar|UnWrap")
101 : Q_ ("ButtonBar|Wrap"), keymap
, (Widget
*) view
);
102 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Hex"), keymap
, (Widget
*) view
);
103 buttonbar_set_label (b
, 6, "", keymap
, (Widget
*) view
);
104 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), keymap
, (Widget
*) view
);
107 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Goto"), keymap
, (Widget
*) view
);
108 buttonbar_set_label (b
, 8, view
->magic_mode
? Q_ ("ButtonBar|Raw")
109 : Q_ ("ButtonBar|Parse"), keymap
, (Widget
*) view
);
111 if (mcview_is_in_panel (view
))
112 buttonbar_set_label (b
, 10, "", keymap
, (Widget
*) view
);
115 /* don't override some panel buttonbar keys */
116 buttonbar_set_label (b
, 3, Q_ ("ButtonBar|Quit"), keymap
, (Widget
*) view
);
117 buttonbar_set_label (b
, 9, view
->text_nroff_mode
? Q_ ("ButtonBar|Unform")
118 : Q_ ("ButtonBar|Format"), keymap
, (Widget
*) view
);
119 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), keymap
, (Widget
*) view
);
123 /* --------------------------------------------------------------------------------------------- */
127 mcview_display_status (mcview_t
* view
)
129 const screen_dimen top
= view
->status_area
.top
;
130 const screen_dimen left
= view
->status_area
.left
;
131 const screen_dimen width
= view
->status_area
.width
;
132 const screen_dimen height
= view
->status_area
.height
;
133 const char *file_label
;
134 screen_dimen file_label_width
;
139 tty_setcolor (SELECTED_COLOR
);
140 widget_move (view
, top
, left
);
141 tty_draw_hline (-1, -1, ' ', width
);
143 file_label
= view
->filename
? view
->filename
: view
->command
? view
->command
: "";
144 file_label_width
= str_term_width1 (file_label
) - 2;
147 char buffer
[BUF_TINY
];
148 widget_move (view
, top
, width
- 32);
151 tty_printf ("0x%08lx", (unsigned long) view
->hex_cursor
);
155 size_trunc_len (buffer
, 5, mcview_get_filesize (view
), 0);
156 tty_printf ("%9lli/%s%s %s", view
->dpy_end
,
157 buffer
, mcview_may_still_grow (view
) ? "+" : " ",
159 source_codepage
>= 0 ? get_codepage_id (source_codepage
) : ""
166 widget_move (view
, top
, left
);
168 tty_print_string (str_fit_to_term (file_label
, width
- 34, J_LEFT_FIT
));
170 tty_print_string (str_fit_to_term (file_label
, width
- 5, J_LEFT_FIT
));
172 mcview_percent (view
, view
->hex_mode
? view
->hex_cursor
: view
->dpy_end
);
175 /* --------------------------------------------------------------------------------------------- */
177 /*** public functions ****************************************************************************/
179 /* --------------------------------------------------------------------------------------------- */
182 mcview_update (mcview_t
* view
)
184 static int dirt_limit
= 1;
186 if (view
->dpy_bbar_dirty
)
188 view
->dpy_bbar_dirty
= FALSE
;
189 mcview_set_buttonbar (view
);
190 buttonbar_redraw (find_buttonbar (view
->widget
.parent
));
193 if (view
->dirty
> dirt_limit
)
195 /* Too many updates skipped -> force a update */
196 mcview_display (view
);
198 /* Raise the update skipping limit */
200 if (dirt_limit
> mcview_max_dirt_limit
)
201 dirt_limit
= mcview_max_dirt_limit
;
203 else if (view
->dirty
> 0)
207 /* We have time to update the screen properly */
208 mcview_display (view
);
215 /* We are busy -> skipping full update,
216 only the status line is updated */
217 mcview_display_status (view
);
219 /* Here we had a refresh, if fast scrolling does not work
220 restore the refresh, although this should not happen */
224 /* --------------------------------------------------------------------------------------------- */
226 /* Displays as much data from view->dpy_start as fits on the screen */
228 mcview_display (mcview_t
* view
)
232 mcview_display_hex (view
);
234 else if (view
->text_nroff_mode
)
236 mcview_display_nroff (view
);
240 mcview_display_text (view
);
242 mcview_display_status (view
);
245 /* --------------------------------------------------------------------------------------------- */
248 mcview_compute_areas (mcview_t
* view
)
250 struct area view_area
;
251 screen_dimen height
, rest
, y
;
253 /* The viewer is surrounded by a frame of size view->dpy_frame_size.
254 * Inside that frame, there are: The status line (at the top),
255 * the data area and an optional ruler, which is shown above or
256 * below the data area. */
258 view_area
.top
= view
->dpy_frame_size
;
259 view_area
.left
= view
->dpy_frame_size
;
260 view_area
.height
= mcview_dimen_doz (view
->widget
.lines
, 2 * view
->dpy_frame_size
);
261 view_area
.width
= mcview_dimen_doz (view
->widget
.cols
, 2 * view
->dpy_frame_size
);
263 /* Most coordinates of the areas equal those of the whole viewer */
264 view
->status_area
= view_area
;
265 view
->ruler_area
= view_area
;
266 view
->data_area
= view_area
;
268 /* Compute the heights of the areas */
269 rest
= view_area
.height
;
271 height
= mcview_dimen_min (rest
, 1);
272 view
->status_area
.height
= height
;
275 height
= mcview_dimen_min (rest
, (ruler
== RULER_NONE
|| view
->hex_mode
) ? 0 : 2);
276 view
->ruler_area
.height
= height
;
279 view
->data_area
.height
= rest
;
281 /* Compute the position of the areas */
284 view
->status_area
.top
= y
;
285 y
+= view
->status_area
.height
;
287 if (ruler
== RULER_TOP
)
289 view
->ruler_area
.top
= y
;
290 y
+= view
->ruler_area
.height
;
293 view
->data_area
.top
= y
;
294 y
+= view
->data_area
.height
;
296 if (ruler
== RULER_BOTTOM
)
297 view
->ruler_area
.top
= y
;
300 /* --------------------------------------------------------------------------------------------- */
303 mcview_update_bytes_per_line (mcview_t
* view
)
305 const screen_dimen cols
= view
->data_area
.width
;
311 bytes
= 4 * ((cols
- 8) / ((cols
< 80) ? 17 : 18));
314 view
->bytes_per_line
= bytes
;
315 view
->dirty
= mcview_max_dirt_limit
+ 1; /* To force refresh */
318 /* --------------------------------------------------------------------------------------------- */
321 mcview_display_toggle_ruler (mcview_t
* view
)
323 static const enum ruler_type next
[3] =
330 assert ((size_t) ruler
< 3);
331 ruler
= next
[(size_t) ruler
];
332 mcview_compute_areas (view
);
336 /* --------------------------------------------------------------------------------------------- */
339 mcview_display_clean (mcview_t
* view
)
341 tty_setcolor (NORMAL_COLOR
);
342 widget_erase ((Widget
*) view
);
343 if (view
->dpy_frame_size
!= 0)
344 tty_draw_box (view
->widget
.y
, view
->widget
.x
, view
->widget
.lines
, view
->widget
.cols
, FALSE
);
347 /* --------------------------------------------------------------------------------------------- */
350 mcview_display_ruler (mcview_t
* view
)
352 static const char ruler_chars
[] = "|----*----";
353 const screen_dimen top
= view
->ruler_area
.top
;
354 const screen_dimen left
= view
->ruler_area
.left
;
355 const screen_dimen width
= view
->ruler_area
.width
;
356 const screen_dimen height
= view
->ruler_area
.height
;
357 const screen_dimen line_row
= (ruler
== RULER_TOP
) ? 0 : 1;
358 const screen_dimen nums_row
= (ruler
== RULER_TOP
) ? 1 : 0;
364 if (ruler
== RULER_NONE
|| height
< 1)
367 tty_setcolor (MARKED_COLOR
);
368 for (c
= 0; c
< width
; c
++)
370 cl
= view
->dpy_text_column
+ c
;
371 if (line_row
< height
)
373 widget_move (view
, top
+ line_row
, left
+ c
);
374 tty_print_char (ruler_chars
[cl
% 10]);
377 if ((cl
!= 0) && (cl
% 10) == 0)
379 g_snprintf (r_buff
, sizeof (r_buff
), "%" OFFSETTYPE_PRId
, (long unsigned int) cl
);
380 if (nums_row
< height
)
382 widget_move (view
, top
+ nums_row
, left
+ c
- 1);
383 tty_print_string (r_buff
);
387 tty_setcolor (NORMAL_COLOR
);
390 /* --------------------------------------------------------------------------------------------- */
393 mcview_percent (mcview_t
* view
, off_t p
)
395 const screen_dimen top
= view
->status_area
.top
;
396 const screen_dimen right
= view
->status_area
.left
+ view
->status_area
.width
;
397 const screen_dimen height
= view
->status_area
.height
;
401 if (height
< 1 || right
< 4)
403 if (mcview_may_still_grow (view
))
405 filesize
= mcview_get_filesize (view
);
407 if (filesize
== 0 || view
->dpy_end
== filesize
)
409 else if (p
> (INT_MAX
/ 100))
410 percent
= p
/ (filesize
/ 100);
412 percent
= p
* 100 / filesize
;
414 widget_move (view
, top
, right
- 4);
415 tty_printf ("%3d%%", percent
);
418 /* --------------------------------------------------------------------------------------------- */