Ticket #2524: added mc-4.6 skin.
[midnight-commander.git] / src / viewer / display.c
bloba1491fbb5c5238c481c08028859e283a343691e0
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 <inttypes.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 #ifdef HAVE_CHARSET
49 #include "lib/charsets.h"
50 #endif
52 #include "src/setup.h" /* panels_options */
53 #include "src/main.h"
55 #include "internal.h"
56 #include "mcviewer.h"
58 /*** global variables ****************************************************************************/
60 /*** file scope macro definitions ****************************************************************/
62 /*** file scope type declarations ****************************************************************/
64 /*** file scope variables ************************************************************************/
66 /* If set, show a ruler */
67 static enum ruler_type
69 RULER_NONE,
70 RULER_TOP,
71 RULER_BOTTOM
72 } ruler = RULER_NONE;
74 /*** file scope functions ************************************************************************/
75 /* --------------------------------------------------------------------------------------------- */
77 /** Define labels and handlers for functional keys */
79 static void
80 mcview_set_buttonbar (mcview_t * view)
82 Dlg_head *h = view->widget.owner;
83 WButtonBar *b = find_buttonbar (h);
84 const global_keymap_t *keymap = view->hex_mode ? view->hex_map : view->plain_map;
86 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), keymap, (Widget *) view);
88 if (view->hex_mode)
90 if (view->hexedit_mode)
91 buttonbar_set_label (b, 2, Q_ ("ButtonBar|View"), keymap, (Widget *) view);
92 else if (view->datasource == DS_FILE)
93 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Edit"), keymap, (Widget *) view);
94 else
95 buttonbar_set_label (b, 2, "", keymap, (Widget *) view);
97 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Ascii"), keymap, (Widget *) view);
98 buttonbar_set_label (b, 6, Q_ ("ButtonBar|Save"), keymap, (Widget *) view);
99 buttonbar_set_label (b, 7, Q_ ("ButtonBar|HxSrch"), keymap, (Widget *) view);
102 else
104 buttonbar_set_label (b, 2, view->text_wrap_mode ? Q_ ("ButtonBar|UnWrap")
105 : Q_ ("ButtonBar|Wrap"), keymap, (Widget *) view);
106 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Hex"), keymap, (Widget *) view);
107 buttonbar_set_label (b, 6, "", keymap, (Widget *) view);
108 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), keymap, (Widget *) view);
111 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Goto"), keymap, (Widget *) view);
112 buttonbar_set_label (b, 8, view->magic_mode ? Q_ ("ButtonBar|Raw")
113 : Q_ ("ButtonBar|Parse"), keymap, (Widget *) view);
115 if (mcview_is_in_panel (view))
116 buttonbar_set_label (b, 10, "", keymap, (Widget *) view);
117 else
119 /* don't override some panel buttonbar keys */
120 buttonbar_set_label (b, 3, Q_ ("ButtonBar|Quit"), keymap, (Widget *) view);
121 buttonbar_set_label (b, 9, view->text_nroff_mode ? Q_ ("ButtonBar|Unform")
122 : Q_ ("ButtonBar|Format"), keymap, (Widget *) view);
123 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), keymap, (Widget *) view);
127 /* --------------------------------------------------------------------------------------------- */
129 static void
130 mcview_display_status (mcview_t * view)
132 const screen_dimen top = view->status_area.top;
133 const screen_dimen left = view->status_area.left;
134 const screen_dimen width = view->status_area.width;
135 const screen_dimen height = view->status_area.height;
136 const char *file_label;
137 screen_dimen file_label_width;
139 if (height < 1)
140 return;
142 tty_setcolor (STATUSBAR_COLOR);
143 widget_move (view, top, left);
144 tty_draw_hline (-1, -1, ' ', width);
146 file_label = view->filename ? view->filename : view->command ? view->command : "";
147 file_label_width = str_term_width1 (file_label) - 2;
148 if (width > 40)
150 char buffer[BUF_TINY];
151 widget_move (view, top, width - 32);
152 if (view->hex_mode)
153 tty_printf ("0x%08" PRIxMAX, (uintmax_t) view->hex_cursor);
154 else
156 size_trunc_len (buffer, 5, mcview_get_filesize (view), 0, panels_options.kilobyte_si);
157 tty_printf ("%9" PRIuMAX "/%s%s %s", (uintmax_t) 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 /* --------------------------------------------------------------------------------------------- */
177 /*** public functions ****************************************************************************/
178 /* --------------------------------------------------------------------------------------------- */
180 void
181 mcview_update (mcview_t * view)
183 static int dirt_limit = 1;
185 if (view->dpy_bbar_dirty)
187 view->dpy_bbar_dirty = FALSE;
188 mcview_set_buttonbar (view);
189 buttonbar_redraw (find_buttonbar (view->widget.owner));
192 if (view->dirty > dirt_limit)
194 /* Too many updates skipped -> force a update */
195 mcview_display (view);
196 view->dirty = 0;
197 /* Raise the update skipping limit */
198 dirt_limit++;
199 if (dirt_limit > mcview_max_dirt_limit)
200 dirt_limit = mcview_max_dirt_limit;
202 else if (view->dirty > 0)
204 if (is_idle ())
206 /* We have time to update the screen properly */
207 mcview_display (view);
208 view->dirty = 0;
209 if (dirt_limit > 1)
210 dirt_limit--;
212 else
214 /* We are busy -> skipping full update,
215 only the status line is updated */
216 mcview_display_status (view);
218 /* Here we had a refresh, if fast scrolling does not work
219 restore the refresh, although this should not happen */
223 /* --------------------------------------------------------------------------------------------- */
224 /** Displays as much data from view->dpy_start as fits on the screen */
226 void
227 mcview_display (mcview_t * view)
229 if (view->hex_mode)
231 mcview_display_hex (view);
233 else if (view->text_nroff_mode)
235 mcview_display_nroff (view);
237 else
239 mcview_display_text (view);
241 mcview_display_status (view);
244 /* --------------------------------------------------------------------------------------------- */
246 void
247 mcview_compute_areas (mcview_t * view)
249 struct area view_area;
250 screen_dimen height, rest, y;
252 /* The viewer is surrounded by a frame of size view->dpy_frame_size.
253 * Inside that frame, there are: The status line (at the top),
254 * the data area and an optional ruler, which is shown above or
255 * below the data area. */
257 view_area.top = view->dpy_frame_size;
258 view_area.left = view->dpy_frame_size;
259 view_area.height = mcview_dimen_doz (view->widget.lines, 2 * view->dpy_frame_size);
260 view_area.width = mcview_dimen_doz (view->widget.cols, 2 * view->dpy_frame_size);
262 /* Most coordinates of the areas equal those of the whole viewer */
263 view->status_area = view_area;
264 view->ruler_area = view_area;
265 view->data_area = view_area;
267 /* Compute the heights of the areas */
268 rest = view_area.height;
270 height = mcview_dimen_min (rest, 1);
271 view->status_area.height = height;
272 rest -= height;
274 height = mcview_dimen_min (rest, (ruler == RULER_NONE || view->hex_mode) ? 0 : 2);
275 view->ruler_area.height = height;
276 rest -= height;
278 view->data_area.height = rest;
280 /* Compute the position of the areas */
281 y = view_area.top;
283 view->status_area.top = y;
284 y += view->status_area.height;
286 if (ruler == RULER_TOP)
288 view->ruler_area.top = y;
289 y += view->ruler_area.height;
292 view->data_area.top = y;
293 y += view->data_area.height;
295 if (ruler == RULER_BOTTOM)
296 view->ruler_area.top = y;
299 /* --------------------------------------------------------------------------------------------- */
301 void
302 mcview_update_bytes_per_line (mcview_t * view)
304 const screen_dimen cols = view->data_area.width;
305 int bytes;
307 if (cols < 8 + 17)
308 bytes = 4;
309 else
310 bytes = 4 * ((cols - 8) / ((cols < 80) ? 17 : 18));
311 assert (bytes != 0);
313 view->bytes_per_line = bytes;
314 view->dirty = mcview_max_dirt_limit + 1; /* To force refresh */
317 /* --------------------------------------------------------------------------------------------- */
319 void
320 mcview_display_toggle_ruler (mcview_t * view)
322 static const enum ruler_type next[3] =
324 RULER_TOP,
325 RULER_BOTTOM,
326 RULER_NONE
329 assert ((size_t) ruler < 3);
330 ruler = next[(size_t) ruler];
331 mcview_compute_areas (view);
332 view->dirty++;
335 /* --------------------------------------------------------------------------------------------- */
337 void
338 mcview_display_clean (mcview_t * view)
340 tty_setcolor (NORMAL_COLOR);
341 widget_erase ((Widget *) view);
342 if (view->dpy_frame_size != 0)
343 tty_draw_box (view->widget.y, view->widget.x, view->widget.lines, view->widget.cols, FALSE);
346 /* --------------------------------------------------------------------------------------------- */
348 void
349 mcview_display_ruler (mcview_t * view)
351 static const char ruler_chars[] = "|----*----";
352 const screen_dimen top = view->ruler_area.top;
353 const screen_dimen left = view->ruler_area.left;
354 const screen_dimen width = view->ruler_area.width;
355 const screen_dimen height = view->ruler_area.height;
356 const screen_dimen line_row = (ruler == RULER_TOP) ? 0 : 1;
357 const screen_dimen nums_row = (ruler == RULER_TOP) ? 1 : 0;
359 char r_buff[10];
360 off_t cl;
361 screen_dimen c;
363 if (ruler == RULER_NONE || height < 1)
364 return;
366 tty_setcolor (VIEW_BOLD_COLOR);
367 for (c = 0; c < width; c++)
369 cl = view->dpy_text_column + c;
370 if (line_row < height)
372 widget_move (view, top + line_row, left + c);
373 tty_print_char (ruler_chars[cl % 10]);
376 if ((cl != 0) && (cl % 10) == 0)
378 g_snprintf (r_buff, sizeof (r_buff), "%" PRIuMAX, (uintmax_t) cl);
379 if (nums_row < height)
381 widget_move (view, top + nums_row, left + c - 1);
382 tty_print_string (r_buff);
386 tty_setcolor (NORMAL_COLOR);
389 /* --------------------------------------------------------------------------------------------- */
391 void
392 mcview_percent (mcview_t * view, off_t p)
394 const screen_dimen top = view->status_area.top;
395 const screen_dimen right = view->status_area.left + view->status_area.width;
396 const screen_dimen height = view->status_area.height;
397 int percent;
398 off_t filesize;
400 if (height < 1 || right < 4)
401 return;
402 if (mcview_may_still_grow (view))
403 return;
404 filesize = mcview_get_filesize (view);
406 if (filesize == 0 || view->dpy_end == filesize)
407 percent = 100;
408 else if (p > (INT_MAX / 100))
409 percent = p / (filesize / 100);
410 else
411 percent = p * 100 / filesize;
413 widget_move (view, top, right - 4);
414 tty_printf ("%3d%%", percent);
417 /* --------------------------------------------------------------------------------------------- */