(dlg_overlap): rename to widget_overlapped()
[midnight-commander.git] / src / filemanager / info.c
blob3bb7defb48318a46048d36445bf492b6423460ba
1 /*
2 Panel managing.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2007, 2011, 2013
6 The Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2013
10 Andrew Borodin <aborodin@vmail.ru>, 2013
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file info.c
29 * \brief Source: panel managing
32 #include <config.h>
34 #include <stdio.h>
35 #include <sys/stat.h>
36 #include <inttypes.h> /* PRIuMAX */
38 #include "lib/global.h"
39 #include "lib/unixcompat.h"
40 #include "lib/tty/tty.h"
41 #include "lib/tty/key.h" /* is_idle() */
42 #include "lib/tty/mouse.h" /* Gpm_Event */
43 #include "lib/skin.h"
44 #include "lib/strutil.h"
45 #include "lib/timefmt.h" /* file_date() */
46 #include "lib/util.h"
47 #include "lib/widget.h"
49 #include "src/setup.h" /* panels_options */
51 #include "midnight.h" /* the_menubar */
52 #include "layout.h"
53 #include "mountlist.h"
54 #include "info.h"
56 /*** global variables ****************************************************************************/
58 /*** file scope macro definitions ****************************************************************/
60 #ifndef VERSION
61 #define VERSION "undefined"
62 #endif
64 /*** file scope type declarations ****************************************************************/
66 struct WInfo
68 Widget widget;
69 int ready;
72 /*** file scope variables ************************************************************************/
74 static struct my_statfs myfs_stats;
76 /*** file scope functions ************************************************************************/
77 /* --------------------------------------------------------------------------------------------- */
79 static void
80 info_box (WInfo * info)
82 Widget *w = WIDGET (info);
84 const char *title = _("Information");
85 const int len = str_term_width1 (title);
87 tty_set_normal_attrs ();
88 tty_setcolor (NORMAL_COLOR);
89 widget_erase (w);
90 tty_draw_box (w->y, w->x, w->lines, w->cols, FALSE);
92 widget_move (w, 0, (w->cols - len - 2) / 2);
93 tty_printf (" %s ", title);
95 widget_move (w, 2, 0);
96 tty_print_alt_char (ACS_LTEE, FALSE);
97 widget_move (w, 2, w->cols - 1);
98 tty_print_alt_char (ACS_RTEE, FALSE);
99 tty_draw_hline (w->y + 2, w->x + 1, ACS_HLINE, w->cols - 2);
102 /* --------------------------------------------------------------------------------------------- */
104 static void
105 info_show_info (WInfo * info)
107 Widget *w = WIDGET (info);
108 static int i18n_adjust = 0;
109 static const char *file_label;
110 GString *buff;
111 struct stat st;
113 if (!is_idle ())
114 return;
116 info_box (info);
118 tty_setcolor (MARKED_COLOR);
119 widget_move (w, 1, 3);
120 tty_printf (_("Midnight Commander %s"), VERSION);
122 if (!info->ready)
123 return;
125 if (get_current_type () != view_listing)
126 return;
128 my_statfs (&myfs_stats, vfs_path_as_str (current_panel->cwd_vpath));
130 st = current_panel->dir.list[current_panel->selected].st;
132 /* Print only lines which fit */
134 if (i18n_adjust == 0)
136 /* This printf pattern string is used as a reference for size */
137 file_label = _("File: %s");
138 i18n_adjust = str_term_width1 (file_label) + 2;
141 tty_setcolor (NORMAL_COLOR);
143 buff = g_string_new ("");
145 switch (w->lines - 2)
147 /* Note: all cases are fall-throughs */
149 default:
151 case 16:
152 widget_move (w, 16, 3);
153 if (myfs_stats.nfree == 0 && myfs_stats.nodes == 0)
154 tty_print_string (_("No node information"));
155 else
156 tty_printf ("%s %" PRIuMAX "/%" PRIuMAX " (%d%%)",
157 _("Free nodes:"),
158 myfs_stats.nfree, myfs_stats.nodes,
159 myfs_stats.nodes == 0 ? 0 :
160 (int) (100 * (long double) myfs_stats.nfree / myfs_stats.nodes));
162 case 15:
163 widget_move (w, 15, 3);
164 if (myfs_stats.avail == 0 && myfs_stats.total == 0)
165 tty_print_string (_("No space information"));
166 else
168 char buffer1[6], buffer2[6];
170 size_trunc_len (buffer1, 5, myfs_stats.avail, 1, panels_options.kilobyte_si);
171 size_trunc_len (buffer2, 5, myfs_stats.total, 1, panels_options.kilobyte_si);
172 tty_printf (_("Free space: %s/%s (%d%%)"), buffer1, buffer2,
173 myfs_stats.total == 0 ? 0 :
174 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
177 case 14:
178 widget_move (w, 14, 3);
179 tty_printf (_("Type: %s"),
180 myfs_stats.typename ? myfs_stats.typename : _("non-local vfs"));
181 if (myfs_stats.type != 0xffff && myfs_stats.type != -1)
182 tty_printf (" (%Xh)", myfs_stats.type);
184 case 13:
185 widget_move (w, 13, 3);
186 str_printf (buff, _("Device: %s"), str_trunc (myfs_stats.device, w->cols - i18n_adjust));
187 tty_print_string (buff->str);
188 g_string_set_size (buff, 0);
189 case 12:
190 widget_move (w, 12, 3);
191 str_printf (buff, _("Filesystem: %s"),
192 str_trunc (myfs_stats.mpoint, w->cols - i18n_adjust));
193 tty_print_string (buff->str);
194 g_string_set_size (buff, 0);
195 case 11:
196 widget_move (w, 11, 3);
197 str_printf (buff, _("Accessed: %s"), file_date (st.st_atime));
198 tty_print_string (buff->str);
199 g_string_set_size (buff, 0);
200 case 10:
201 widget_move (w, 10, 3);
202 str_printf (buff, _("Modified: %s"), file_date (st.st_mtime));
203 tty_print_string (buff->str);
204 g_string_set_size (buff, 0);
205 case 9:
206 widget_move (w, 9, 3);
207 /* The field st_ctime is changed by writing or by setting inode
208 information (i.e., owner, group, link count, mode, etc.). */
209 /* TRANSLATORS: Time of last status change as in stat(2) man. */
210 str_printf (buff, _("Changed: %s"), file_date (st.st_ctime));
211 tty_print_string (buff->str);
212 g_string_set_size (buff, 0);
214 case 8:
215 widget_move (w, 8, 3);
216 #ifdef HAVE_STRUCT_STAT_ST_RDEV
217 if (S_ISCHR (st.st_mode) || S_ISBLK (st.st_mode))
218 tty_printf (_("Dev. type: major %lu, minor %lu"),
219 (unsigned long) major (st.st_rdev), (unsigned long) minor (st.st_rdev));
220 else
221 #endif
223 char buffer[10];
224 size_trunc_len (buffer, 9, st.st_size, 0, panels_options.kilobyte_si);
225 tty_printf (_("Size: %s"), buffer);
226 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
227 tty_printf (ngettext (" (%ld block)", " (%ld blocks)",
228 (unsigned long) st.st_blocks), (unsigned long) st.st_blocks);
229 #endif
232 case 7:
233 widget_move (w, 7, 3);
234 tty_printf (_("Owner: %s/%s"), get_owner (st.st_uid), get_group (st.st_gid));
236 case 6:
237 widget_move (w, 6, 3);
238 tty_printf (_("Links: %d"), (int) st.st_nlink);
240 case 5:
241 widget_move (w, 5, 3);
242 tty_printf (_("Mode: %s (%04o)"),
243 string_perm (st.st_mode), (unsigned) st.st_mode & 07777);
245 case 4:
246 widget_move (w, 4, 3);
247 tty_printf (_("Location: %Xh:%Xh"), (int) st.st_dev, (int) st.st_ino);
249 case 3:
251 const char *fname;
253 widget_move (w, 3, 2);
254 fname = current_panel->dir.list[current_panel->selected].fname;
255 str_printf (buff, file_label, str_trunc (fname, w->cols - i18n_adjust));
256 tty_print_string (buff->str);
259 case 2:
260 case 1:
261 case 0:
263 } /* switch */
264 g_string_free (buff, TRUE);
267 /* --------------------------------------------------------------------------------------------- */
269 static void
270 info_hook (void *data)
272 struct WInfo *info = (struct WInfo *) data;
273 Widget *other_widget;
275 other_widget = get_panel_widget (get_current_index ());
276 if (!other_widget)
277 return;
278 if (widget_overlapped (WIDGET (info), other_widget))
279 return;
281 info->ready = 1;
282 info_show_info (info);
285 /* --------------------------------------------------------------------------------------------- */
287 static cb_ret_t
288 info_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
290 struct WInfo *info = (struct WInfo *) w;
292 switch (msg)
294 case MSG_INIT:
295 init_my_statfs ();
296 add_hook (&select_file_hook, info_hook, info);
297 info->ready = 0;
298 return MSG_HANDLED;
300 case MSG_DRAW:
301 info_hook (info);
302 return MSG_HANDLED;
304 case MSG_FOCUS:
305 return MSG_NOT_HANDLED;
307 case MSG_DESTROY:
308 delete_hook (&select_file_hook, info_hook);
309 free_my_statfs ();
310 return MSG_HANDLED;
312 default:
313 return widget_default_callback (w, sender, msg, parm, data);
317 /* --------------------------------------------------------------------------------------------- */
318 /*** public functions ****************************************************************************/
319 /* --------------------------------------------------------------------------------------------- */
321 WInfo *
322 info_new (int y, int x, int lines, int cols)
324 WInfo *info;
325 Widget *w;
327 info = g_new (struct WInfo, 1);
328 w = WIDGET (info);
329 init_widget (w, y, x, lines, cols, info_callback, NULL);
330 /* We do not want the cursor */
331 widget_want_cursor (w, FALSE);
333 return info;
336 /* --------------------------------------------------------------------------------------------- */