Merge branch '1992_mc_startup'
[midnight-commander.git] / src / viewer / display.c
blobd027cc25acdf91c29d8e3e6786aeed79690b8b6f
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/main.h"
47 #include "src/dialog.h" /* Dlg_head */
48 #include "src/charsets.h"
49 #include "src/widget.h" /* WButtonBar */
51 #include "internal.h"
52 #include "mcviewer.h"
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 {
64 RULER_NONE,
65 RULER_TOP,
66 RULER_BOTTOM
67 } ruler = RULER_NONE;
69 /*** file scope functions ************************************************************************/
71 /* --------------------------------------------------------------------------------------------- */
73 /* Define labels and handlers for functional keys */
74 static void
75 mcview_set_buttonbar (mcview_t *view)
77 Dlg_head *h = view->widget.parent;
78 WButtonBar *b = find_buttonbar (h);
79 const global_keymap_t *keymap = view->hex_mode ? view->hex_map : view->plain_map;
81 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), keymap, (Widget *) view);
83 if (view->hex_mode) {
84 if (view->hexedit_mode)
85 buttonbar_set_label (b, 2, Q_ ("ButtonBar|View"), keymap, (Widget *) view);
86 else if (view->datasource == DS_FILE)
87 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Edit"), keymap, (Widget *) view);
88 else
89 buttonbar_set_label (b, 2, "", keymap, (Widget *) view);
91 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Ascii"), keymap, (Widget *) view);
92 buttonbar_set_label (b, 6, Q_ ("ButtonBar|Save"), keymap, (Widget *) view);
93 buttonbar_set_label (b, 7, Q_ ("ButtonBar|HxSrch"), keymap, (Widget *) view);
95 } else {
96 buttonbar_set_label (b, 2, view->text_wrap_mode ? Q_ ("ButtonBar|UnWrap")
97 : Q_ ("ButtonBar|Wrap"),
98 keymap, (Widget *) view);
99 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Hex"), keymap, (Widget *) view);
100 buttonbar_set_label (b, 6, "", keymap, (Widget *) view);
101 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), keymap, (Widget *) view);
104 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Goto"), keymap, (Widget *) view);
105 buttonbar_set_label (b, 8, view->magic_mode ? Q_ ("ButtonBar|Raw")
106 : Q_ ("ButtonBar|Parse"),
107 keymap, (Widget *) view);
109 if (mcview_is_in_panel (view))
110 buttonbar_set_label (b, 10, "", keymap, (Widget *) view);
111 else {
112 /* don't override some panel buttonbar keys */
113 buttonbar_set_label (b, 3, Q_ ("ButtonBar|Quit"), keymap, (Widget *) view);
114 buttonbar_set_label (b, 9, view->text_nroff_mode ? Q_ ("ButtonBar|Unform")
115 : Q_ ("ButtonBar|Format"),
116 keymap, (Widget *) view);
117 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), keymap, (Widget *) view);
121 /* --------------------------------------------------------------------------------------------- */
124 static void
125 mcview_display_status (mcview_t * view)
127 const screen_dimen top = view->status_area.top;
128 const screen_dimen left = view->status_area.left;
129 const screen_dimen width = view->status_area.width;
130 const screen_dimen height = view->status_area.height;
131 const char *file_label;
132 screen_dimen file_label_width, i = 0;
134 if (height < 1)
135 return;
137 tty_setcolor (SELECTED_COLOR);
138 widget_move (view, top, left);
139 tty_draw_hline (-1, -1, ' ', width);
141 file_label = view->filename ? view->filename : view->command ? view->command : "";
142 file_label_width = str_term_width1 (file_label) - 2;
143 if (width > 40) {
144 char buffer [BUF_TINY];
145 widget_move (view, top, width - 32);
146 if (view->hex_mode) {
147 tty_printf ("0x%08lx", (unsigned long) view->hex_cursor);
148 } else {
149 size_trunc_len (buffer, 5, mcview_get_filesize (view), 0);
150 tty_printf ("%9lli/%s%s %s", view->dpy_end,
151 buffer,
152 mcview_may_still_grow (view) ? "+" : " ",
153 #ifdef HAVE_CHARSET
154 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
155 #else
157 #endif
161 widget_move (view, top, left);
162 if (width > 40)
163 tty_print_string (str_fit_to_term (file_label, width - 34, J_LEFT_FIT));
164 else
165 tty_print_string (str_fit_to_term (file_label, width - 5, J_LEFT_FIT));
166 if (width > 26)
167 mcview_percent (view, view->hex_mode ? view->hex_cursor : view->dpy_end);
170 /* --------------------------------------------------------------------------------------------- */
172 /*** public functions ****************************************************************************/
174 /* --------------------------------------------------------------------------------------------- */
176 void
177 mcview_update (mcview_t * view)
179 static int dirt_limit = 1;
181 if (view->dpy_bbar_dirty) {
182 view->dpy_bbar_dirty = FALSE;
183 mcview_set_buttonbar (view);
184 buttonbar_redraw (find_buttonbar (view->widget.parent));
187 if (view->dirty > dirt_limit) {
188 /* Too many updates skipped -> force a update */
189 mcview_display (view);
190 view->dirty = 0;
191 /* Raise the update skipping limit */
192 dirt_limit++;
193 if (dirt_limit > mcview_max_dirt_limit)
194 dirt_limit = mcview_max_dirt_limit;
195 } else if (view->dirty > 0) {
196 if (is_idle ()) {
197 /* We have time to update the screen properly */
198 mcview_display (view);
199 view->dirty = 0;
200 if (dirt_limit > 1)
201 dirt_limit--;
202 } else {
203 /* We are busy -> skipping full update,
204 only the status line is updated */
205 mcview_display_status (view);
207 /* Here we had a refresh, if fast scrolling does not work
208 restore the refresh, although this should not happen */
212 /* --------------------------------------------------------------------------------------------- */
214 /* Displays as much data from view->dpy_start as fits on the screen */
215 void
216 mcview_display (mcview_t * view)
218 if (view->hex_mode) {
219 mcview_display_hex (view);
220 } else if (view->text_nroff_mode) {
221 mcview_display_nroff (view);
222 } else {
223 mcview_display_text (view);
225 mcview_display_status (view);
228 /* --------------------------------------------------------------------------------------------- */
230 void
231 mcview_compute_areas (mcview_t * view)
233 struct area view_area;
234 screen_dimen height, rest, y;
236 /* The viewer is surrounded by a frame of size view->dpy_frame_size.
237 * Inside that frame, there are: The status line (at the top),
238 * the data area and an optional ruler, which is shown above or
239 * below the data area. */
241 view_area.top = view->dpy_frame_size;
242 view_area.left = view->dpy_frame_size;
243 view_area.height = mcview_dimen_doz (view->widget.lines, 2 * view->dpy_frame_size);
244 view_area.width = mcview_dimen_doz (view->widget.cols, 2 * view->dpy_frame_size);
246 /* Most coordinates of the areas equal those of the whole viewer */
247 view->status_area = view_area;
248 view->ruler_area = view_area;
249 view->data_area = view_area;
251 /* Compute the heights of the areas */
252 rest = view_area.height;
254 height = mcview_dimen_min (rest, 1);
255 view->status_area.height = height;
256 rest -= height;
258 height = mcview_dimen_min (rest, (ruler == RULER_NONE || view->hex_mode) ? 0 : 2);
259 view->ruler_area.height = height;
260 rest -= height;
262 view->data_area.height = rest;
264 /* Compute the position of the areas */
265 y = view_area.top;
267 view->status_area.top = y;
268 y += view->status_area.height;
270 if (ruler == RULER_TOP) {
271 view->ruler_area.top = y;
272 y += view->ruler_area.height;
275 view->data_area.top = y;
276 y += view->data_area.height;
278 if (ruler == RULER_BOTTOM)
279 view->ruler_area.top = y;
282 /* --------------------------------------------------------------------------------------------- */
284 void
285 mcview_update_bytes_per_line (mcview_t * view)
287 const screen_dimen cols = view->data_area.width;
288 int bytes;
290 if (cols < 8 + 17)
291 bytes = 4;
292 else
293 bytes = 4 * ((cols - 8) / ((cols < 80) ? 17 : 18));
294 assert (bytes != 0);
296 view->bytes_per_line = bytes;
297 view->dirty = mcview_max_dirt_limit + 1; /* To force refresh */
300 /* --------------------------------------------------------------------------------------------- */
302 void
303 mcview_display_toggle_ruler (mcview_t * view)
305 static const enum ruler_type next[3] = {
306 RULER_TOP,
307 RULER_BOTTOM,
308 RULER_NONE
311 assert ((size_t) ruler < 3);
312 ruler = next[(size_t) ruler];
313 view->dirty++;
316 /* --------------------------------------------------------------------------------------------- */
318 void
319 mcview_display_clean (mcview_t * view)
321 tty_setcolor (NORMAL_COLOR);
322 widget_erase ((Widget *) view);
323 if (view->dpy_frame_size != 0) {
324 tty_draw_box (view->widget.y, view->widget.x, view->widget.lines, view->widget.cols);
325 /* draw_double_box (view->widget.parent, view->widget.y,
326 view->widget.x, view->widget.lines, view->widget.cols);*/
330 /* --------------------------------------------------------------------------------------------- */
332 void
333 mcview_display_ruler (mcview_t * view)
335 static const char ruler_chars[] = "|----*----";
336 const screen_dimen top = view->ruler_area.top;
337 const screen_dimen left = view->ruler_area.left;
338 const screen_dimen width = view->ruler_area.width;
339 const screen_dimen height = view->ruler_area.height;
340 const screen_dimen line_row = (ruler == RULER_TOP) ? 0 : 1;
341 const screen_dimen nums_row = (ruler == RULER_TOP) ? 1 : 0;
343 char r_buff[10];
344 off_t cl;
345 screen_dimen c;
347 if (ruler == RULER_NONE || height < 1)
348 return;
350 tty_setcolor (MARKED_COLOR);
351 for (c = 0; c < width; c++) {
352 cl = view->dpy_text_column + c;
353 if (line_row < height) {
354 widget_move (view, top + line_row, left + c);
355 tty_print_char (ruler_chars[cl % 10]);
358 if ((cl != 0) && (cl % 10) == 0) {
359 g_snprintf (r_buff, sizeof (r_buff), "%" OFFSETTYPE_PRId, (long unsigned int) cl);
360 if (nums_row < height) {
361 widget_move (view, top + nums_row, left + c - 1);
362 tty_print_string (r_buff);
366 tty_setcolor (NORMAL_COLOR);
369 /* --------------------------------------------------------------------------------------------- */
371 void
372 mcview_percent (mcview_t * view, off_t p)
374 const screen_dimen top = view->status_area.top;
375 const screen_dimen right = view->status_area.left + view->status_area.width;
376 const screen_dimen height = view->status_area.height;
377 int percent;
378 off_t filesize;
380 if (height < 1 || right < 4)
381 return;
382 if (mcview_may_still_grow (view))
383 return;
384 filesize = mcview_get_filesize (view);
386 if (filesize == 0 || view->dpy_end == filesize)
387 percent = 100;
388 else if (p > (INT_MAX / 100))
389 percent = p / (filesize / 100);
390 else
391 percent = p * 100 / filesize;
393 widget_move (view, top, right - 4);
394 tty_printf ("%3d%%", percent);
397 /* --------------------------------------------------------------------------------------------- */