cppcheck: reduce variable scope.
[midnight-commander.git] / src / help.c
blob326a1465be4a603e0ebef6ce650f8493670b1ebf
1 /*
2 Hypertext file browser.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2011, 2013
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /** \file help.c
26 * \brief Source: hypertext file browser
28 * Implements the hypertext file viewer.
29 * The hypertext file is a file that may have one or more nodes. Each
30 * node ends with a ^D character and starts with a bracket, then the
31 * name of the node and then a closing bracket. Right after the closing
32 * bracket a newline is placed. This newline is not to be displayed by
33 * the help viewer and must be skipped - its sole purpose is to faciliate
34 * the work of the people managing the help file template (xnc.hlp) .
36 * Links in the hypertext file are specified like this: the text that
37 * will be highlighted should have a leading ^A, then it comes the
38 * text, then a ^B indicating that highlighting is done, then the name
39 * of the node you want to link to and then a ^C.
41 * The file must contain a ^D at the beginning and at the end of the
42 * file or the program will not be able to detect the end of file.
44 * Lazyness/widgeting attack: This file does use the dialog manager
45 * and uses mainly the dialog to achieve the help work. there is only
46 * one specialized widget and it's only used to forward the mouse messages
47 * to the appropriate routine.
51 #include <config.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
58 #include "lib/global.h"
60 #include "lib/tty/tty.h"
61 #include "lib/tty/mouse.h"
62 #include "lib/skin.h"
63 #include "lib/strutil.h"
64 #include "lib/fileloc.h"
65 #include "lib/util.h"
66 #include "lib/widget.h"
67 #include "lib/event-types.h"
69 #include "keybind-defaults.h"
70 #include "help.h"
72 /*** global variables ****************************************************************************/
74 /*** file scope macro definitions ****************************************************************/
76 #define MAXLINKNAME 80
77 #define HISTORY_SIZE 20
78 #define HELP_WINDOW_WIDTH min(80, COLS - 16)
80 #define STRING_LINK_START "\01"
81 #define STRING_LINK_POINTER "\02"
82 #define STRING_LINK_END "\03"
83 #define STRING_NODE_END "\04"
85 /*** file scope type declarations ****************************************************************/
87 /* Link areas for the mouse */
88 typedef struct Link_Area
90 int x1, y1, x2, y2;
91 const char *link_name;
92 } Link_Area;
94 /*** file scope variables ************************************************************************/
96 static char *fdata = NULL; /* Pointer to the loaded data file */
97 static int help_lines; /* Lines in help viewer */
98 static int history_ptr; /* For the history queue */
99 static const char *main_node; /* The main node */
100 static const char *last_shown = NULL; /* Last byte shown in a screen */
101 static gboolean end_of_node = FALSE; /* Flag: the last character of the node shown? */
102 static const char *currentpoint;
103 static const char *selected_item;
105 /* The widget variables */
106 static WDialog *whelp;
108 static struct
110 const char *page; /* Pointer to the selected page */
111 const char *link; /* Pointer to the selected link */
112 } history[HISTORY_SIZE];
114 static GSList *link_area = NULL;
115 static gboolean inside_link_area = FALSE;
117 static cb_ret_t help_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data);
119 /*** file scope functions ************************************************************************/
120 /* --------------------------------------------------------------------------------------------- */
122 /** returns the position where text was found in the start buffer
123 * or 0 if not found
125 static const char *
126 search_string (const char *start, const char *text)
128 const char *result = NULL;
129 char *local_text = g_strdup (text);
130 char *d = local_text;
131 const char *e = start;
133 /* fmt sometimes replaces a space with a newline in the help file */
134 /* Replace the newlines in the link name with spaces to correct the situation */
135 while (*d != '\0')
137 if (*d == '\n')
138 *d = ' ';
139 str_next_char (&d);
142 /* Do search */
143 for (d = local_text; *e; e++)
145 if (*d == *e)
146 d++;
147 else
148 d = local_text;
149 if (*d == '\0')
151 result = e + 1;
152 break;
156 g_free (local_text);
157 return result;
160 /* --------------------------------------------------------------------------------------------- */
161 /** Searches text in the buffer pointed by start. Search ends
162 * if the CHAR_NODE_END is found in the text.
163 * @return NULL on failure
166 static const char *
167 search_string_node (const char *start, const char *text)
169 const char *d = text;
170 const char *e = start;
172 if (start != NULL)
173 for (; *e && *e != CHAR_NODE_END; e++)
175 if (*d == *e)
176 d++;
177 else
178 d = text;
179 if (*d == '\0')
180 return e + 1;
183 return NULL;
186 /* --------------------------------------------------------------------------------------------- */
187 /** Searches the_char in the buffer pointer by start and searches
188 * it can search forward (direction = 1) or backward (direction = -1)
191 static const char *
192 search_char_node (const char *start, char the_char, int direction)
194 const char *e;
196 for (e = start; (*e != '\0') && (*e != CHAR_NODE_END); e += direction)
197 if (*e == the_char)
198 return e;
200 return NULL;
203 /* --------------------------------------------------------------------------------------------- */
204 /** Returns the new current pointer when moved lines lines */
206 static const char *
207 move_forward2 (const char *c, int lines)
209 const char *p;
210 int line;
212 currentpoint = c;
213 for (line = 0, p = currentpoint; (*p != '\0') && (*p != CHAR_NODE_END); str_cnext_char (&p))
215 if (line == lines)
216 return currentpoint = p;
218 if (*p == '\n')
219 line++;
221 return currentpoint = c;
224 /* --------------------------------------------------------------------------------------------- */
226 static const char *
227 move_backward2 (const char *c, int lines)
229 const char *p;
230 int line;
232 currentpoint = c;
233 for (line = 0, p = currentpoint; (*p != '\0') && ((int) (p - fdata) >= 0); str_cprev_char (&p))
235 if (*p == CHAR_NODE_END)
237 /* We reached the beginning of the node */
238 /* Skip the node headers */
239 while (*p != ']')
240 str_cnext_char (&p);
241 return currentpoint = p + 2; /* Skip the newline following the start of the node */
244 if (*(p - 1) == '\n')
245 line++;
246 if (line == lines)
247 return currentpoint = p;
249 return currentpoint = c;
252 /* --------------------------------------------------------------------------------------------- */
254 static void
255 move_forward (int i)
257 if (!end_of_node)
258 currentpoint = move_forward2 (currentpoint, i);
261 /* --------------------------------------------------------------------------------------------- */
263 static void
264 move_backward (int i)
266 currentpoint = move_backward2 (currentpoint, ++i);
269 /* --------------------------------------------------------------------------------------------- */
271 static void
272 move_to_top (void)
274 while (((int) (currentpoint > fdata) > 0) && (*currentpoint != CHAR_NODE_END))
275 currentpoint--;
277 while (*currentpoint != ']')
278 currentpoint++;
279 currentpoint = currentpoint + 2; /* Skip the newline following the start of the node */
280 selected_item = NULL;
283 /* --------------------------------------------------------------------------------------------- */
285 static void
286 move_to_bottom (void)
288 while ((*currentpoint != '\0') && (*currentpoint != CHAR_NODE_END))
289 currentpoint++;
290 currentpoint--;
291 move_backward (1);
294 /* --------------------------------------------------------------------------------------------- */
296 static const char *
297 help_follow_link (const char *start, const char *lc_selected_item)
299 const char *p;
301 if (lc_selected_item == NULL)
302 return start;
304 for (p = lc_selected_item; *p && *p != CHAR_NODE_END && *p != CHAR_LINK_POINTER; p++)
306 if (*p == CHAR_LINK_POINTER)
308 int i;
309 char link_name[MAXLINKNAME];
311 link_name[0] = '[';
312 for (i = 1; *p != CHAR_LINK_END && *p && *p != CHAR_NODE_END && i < MAXLINKNAME - 3;)
313 link_name[i++] = *++p;
314 link_name[i - 1] = ']';
315 link_name[i] = '\0';
316 p = search_string (fdata, link_name);
317 if (p != NULL)
319 p += 1; /* Skip the newline following the start of the node */
320 return p;
324 /* Create a replacement page with the error message */
325 return _("Help file format error\n");
328 /* --------------------------------------------------------------------------------------------- */
330 static const char *
331 select_next_link (const char *current_link)
333 const char *p;
335 if (current_link == NULL)
336 return NULL;
338 p = search_string_node (current_link, STRING_LINK_END);
339 if (p == NULL)
340 return NULL;
341 p = search_string_node (p, STRING_LINK_START);
342 if (p == NULL)
343 return NULL;
344 return p - 1;
347 /* --------------------------------------------------------------------------------------------- */
349 static const char *
350 select_prev_link (const char *current_link)
352 return current_link == NULL ? NULL : search_char_node (current_link - 1, CHAR_LINK_START, -1);
355 /* --------------------------------------------------------------------------------------------- */
357 static void
358 start_link_area (int x, int y, const char *link_name)
360 Link_Area *la;
362 if (inside_link_area)
363 message (D_NORMAL, _("Warning"), _("Internal bug: Double start of link area"));
365 /* Allocate memory for a new link area */
366 la = g_new (Link_Area, 1);
367 /* Save the beginning coordinates of the link area */
368 la->x1 = x;
369 la->y1 = y;
370 /* Save the name of the destination anchor */
371 la->link_name = link_name;
372 link_area = g_slist_prepend (link_area, la);
374 inside_link_area = TRUE;
377 /* --------------------------------------------------------------------------------------------- */
379 static void
380 end_link_area (int x, int y)
382 if (inside_link_area)
384 Link_Area *la = (Link_Area *) link_area->data;
385 /* Save the end coordinates of the link area */
386 la->x2 = x;
387 la->y2 = y;
388 inside_link_area = FALSE;
392 /* --------------------------------------------------------------------------------------------- */
394 static void
395 clear_link_areas (void)
397 g_slist_foreach (link_area, (GFunc) g_free, NULL);
398 g_slist_free (link_area);
399 link_area = NULL;
400 inside_link_area = FALSE;
403 /* --------------------------------------------------------------------------------------------- */
405 static void
406 help_print_word (WDialog * h, GString * word, int *col, int *line, gboolean add_space)
408 if (*line >= help_lines)
409 g_string_set_size (word, 0);
410 else
412 int w;
414 w = str_term_width1 (word->str);
415 if (*col + w >= HELP_WINDOW_WIDTH)
417 *col = 0;
418 (*line)++;
421 if (*line >= help_lines)
422 g_string_set_size (word, 0);
423 else
425 widget_move (h, *line + 2, *col + 2);
426 tty_print_string (word->str);
427 g_string_set_size (word, 0);
428 *col += w;
432 if (add_space)
434 if (*col < HELP_WINDOW_WIDTH - 1)
436 tty_print_char (' ');
437 (*col)++;
439 else
441 *col = 0;
442 (*line)++;
447 /* --------------------------------------------------------------------------------------------- */
449 static void
450 help_show (WDialog * h, const char *paint_start)
452 const char *p, *n;
453 int col, line, c;
454 gboolean painting = TRUE;
455 gboolean acs; /* Flag: Alternate character set active? */
456 gboolean repeat_paint;
457 int active_col, active_line; /* Active link position */
458 char buff[MB_LEN_MAX + 1];
459 GString *word;
461 word = g_string_sized_new (32);
463 tty_setcolor (HELP_NORMAL_COLOR);
466 line = col = active_col = active_line = 0;
467 repeat_paint = FALSE;
468 acs = FALSE;
470 clear_link_areas ();
471 if ((int) (selected_item - paint_start) < 0)
472 selected_item = NULL;
474 p = paint_start;
475 n = paint_start;
476 while ((n[0] != '\0') && (n[0] != CHAR_NODE_END) && (line < help_lines))
478 p = n;
479 n = str_cget_next_char (p);
480 memcpy (buff, p, n - p);
481 buff[n - p] = '\0';
483 c = (unsigned char) buff[0];
484 switch (c)
486 case CHAR_LINK_START:
487 if (selected_item == NULL)
488 selected_item = p;
489 if (p != selected_item)
490 tty_setcolor (HELP_LINK_COLOR);
491 else
493 tty_setcolor (HELP_SLINK_COLOR);
495 /* Store the coordinates of the link */
496 active_col = col + 2;
497 active_line = line + 2;
499 start_link_area (col, line, p);
500 break;
501 case CHAR_LINK_POINTER:
502 painting = FALSE;
503 break;
504 case CHAR_LINK_END:
505 painting = TRUE;
506 help_print_word (h, word, &col, &line, FALSE);
507 end_link_area (col - 1, line);
508 tty_setcolor (HELP_NORMAL_COLOR);
509 break;
510 case CHAR_ALTERNATE:
511 acs = TRUE;
512 break;
513 case CHAR_NORMAL:
514 acs = FALSE;
515 break;
516 case CHAR_VERSION:
517 widget_move (h, line + 2, col + 2);
518 tty_print_string (VERSION);
519 col += str_term_width1 (VERSION);
520 break;
521 case CHAR_FONT_BOLD:
522 tty_setcolor (HELP_BOLD_COLOR);
523 break;
524 case CHAR_FONT_ITALIC:
525 tty_setcolor (HELP_ITALIC_COLOR);
526 break;
527 case CHAR_FONT_NORMAL:
528 help_print_word (h, word, &col, &line, FALSE);
529 tty_setcolor (HELP_NORMAL_COLOR);
530 break;
531 case '\n':
532 if (painting)
533 help_print_word (h, word, &col, &line, FALSE);
534 line++;
535 col = 0;
536 break;
537 case '\t':
538 col = (col / 8 + 1) * 8;
539 if (col >= HELP_WINDOW_WIDTH)
541 line++;
542 col = 8;
544 break;
545 case ' ':
546 /* word delimiter */
547 if (painting)
548 help_print_word (h, word, &col, &line, TRUE);
549 break;
550 default:
551 if (painting && (line < help_lines))
553 if (!acs)
554 /* accumulate symbols in a word */
555 g_string_append (word, buff);
556 else if (col < HELP_WINDOW_WIDTH)
558 widget_move (h, line + 2, col + 2);
560 if ((c == ' ') || (c == '.'))
561 tty_print_char (c);
562 else
563 #ifndef HAVE_SLANG
564 tty_print_char (acs_map[c]);
565 #else
566 SLsmg_draw_object (WIDGET (h)->y + line + 2, WIDGET (h)->x + col + 2,
568 #endif
569 col++;
575 /* print last word */
576 if (n[0] == CHAR_NODE_END)
577 help_print_word (h, word, &col, &line, FALSE);
579 last_shown = p;
580 end_of_node = line < help_lines;
581 tty_setcolor (HELP_NORMAL_COLOR);
582 if ((int) (selected_item - last_shown) >= 0)
584 if ((link_area == NULL) || (link_area->data == NULL))
585 selected_item = NULL;
586 else
588 selected_item = ((Link_Area *) link_area->data)->link_name;
589 repeat_paint = TRUE;
593 while (repeat_paint);
595 g_string_free (word, TRUE);
597 /* Position the cursor over a nice link */
598 if (active_col)
599 widget_move (h, active_line, active_col);
602 /* --------------------------------------------------------------------------------------------- */
604 static int
605 help_event (Gpm_Event * event, void *vp)
607 Widget *w = WIDGET (vp);
608 GSList *current_area;
609 Gpm_Event local;
611 if (!mouse_global_in_widget (event, w))
612 return MOU_UNHANDLED;
614 if ((event->type & GPM_UP) == 0)
615 return MOU_NORMAL;
617 local = mouse_get_local (event, w);
619 /* The event is relative to the dialog window, adjust it: */
620 local.x -= 2;
621 local.y -= 2;
623 if ((local.buttons & GPM_B_RIGHT) != 0)
625 currentpoint = history[history_ptr].page;
626 selected_item = history[history_ptr].link;
627 history_ptr--;
628 if (history_ptr < 0)
629 history_ptr = HISTORY_SIZE - 1;
631 send_message (w->owner, NULL, MSG_DRAW, 0, NULL);
632 return MOU_NORMAL;
635 /* Test whether the mouse click is inside one of the link areas */
636 for (current_area = link_area; current_area != NULL; current_area = g_slist_next (current_area))
638 Link_Area *la = (Link_Area *) current_area->data;
640 /* Test one line link area */
641 if (local.y == la->y1 && local.x >= la->x1 && local.y == la->y2 && local.x <= la->x2)
642 break;
644 /* Test two line link area */
645 if (la->y1 + 1 == la->y2)
647 /* The first line */
648 if (local.y == la->y1 && local.x >= la->x1)
649 break;
650 /* The second line */
651 if (local.y == la->y2 && local.x <= la->x2)
652 break;
654 /* Mouse will not work with link areas of more than two lines */
657 /* Test whether a link area was found */
658 if (current_area != NULL)
660 Link_Area *la = (Link_Area *) current_area->data;
662 /* The click was inside a link area -> follow the link */
663 history_ptr = (history_ptr + 1) % HISTORY_SIZE;
664 history[history_ptr].page = currentpoint;
665 history[history_ptr].link = la->link_name;
666 currentpoint = help_follow_link (currentpoint, la->link_name);
667 selected_item = NULL;
669 else if (local.y < 0)
670 move_backward (help_lines - 1);
671 else if (local.y >= help_lines)
672 move_forward (help_lines - 1);
673 else if (local.y < help_lines / 2)
674 move_backward (1);
675 else
676 move_forward (1);
678 /* Show the new node */
679 send_message (w->owner, NULL, MSG_DRAW, 0, NULL);
681 return MOU_NORMAL;
684 /* --------------------------------------------------------------------------------------------- */
685 /** show help */
687 static void
688 help_help (WDialog * h)
690 const char *p;
692 history_ptr = (history_ptr + 1) % HISTORY_SIZE;
693 history[history_ptr].page = currentpoint;
694 history[history_ptr].link = selected_item;
696 p = search_string (fdata, "[How to use help]");
697 if (p != NULL)
699 currentpoint = p + 1; /* Skip the newline following the start of the node */
700 selected_item = NULL;
701 send_message (h, NULL, MSG_DRAW, 0, NULL);
705 /* --------------------------------------------------------------------------------------------- */
707 static void
708 help_index (WDialog * h)
710 const char *new_item;
712 new_item = search_string (fdata, "[Contents]");
714 if (new_item == NULL)
715 message (D_ERROR, MSG_ERROR, _("Cannot find node %s in help file"), "[Contents]");
716 else
718 history_ptr = (history_ptr + 1) % HISTORY_SIZE;
719 history[history_ptr].page = currentpoint;
720 history[history_ptr].link = selected_item;
722 currentpoint = new_item + 1; /* Skip the newline following the start of the node */
723 selected_item = NULL;
724 send_message (h, NULL, MSG_DRAW, 0, NULL);
728 /* --------------------------------------------------------------------------------------------- */
730 static void
731 help_back (WDialog * h)
733 currentpoint = history[history_ptr].page;
734 selected_item = history[history_ptr].link;
735 history_ptr--;
736 if (history_ptr < 0)
737 history_ptr = HISTORY_SIZE - 1;
739 send_message (h, NULL, MSG_DRAW, 0, NULL); /* FIXME: unneeded? */
742 /* --------------------------------------------------------------------------------------------- */
744 static void
745 help_next_link (gboolean move_down)
747 const char *new_item;
749 new_item = select_next_link (selected_item);
750 if (new_item != NULL)
752 selected_item = new_item;
753 if ((int) (selected_item - last_shown) >= 0)
755 if (move_down)
756 move_forward (1);
757 else
758 selected_item = NULL;
761 else if (move_down)
762 move_forward (1);
763 else
764 selected_item = NULL;
767 /* --------------------------------------------------------------------------------------------- */
769 static void
770 help_prev_link (gboolean move_up)
772 const char *new_item;
774 new_item = select_prev_link (selected_item);
775 selected_item = new_item;
776 if ((selected_item == NULL) || (selected_item < currentpoint))
778 if (move_up)
779 move_backward (1);
780 else if ((link_area != NULL) && (link_area->data != NULL))
781 selected_item = ((Link_Area *) link_area->data)->link_name;
782 else
783 selected_item = NULL;
787 /* --------------------------------------------------------------------------------------------- */
789 static void
790 help_next_node (void)
792 const char *new_item;
794 new_item = currentpoint;
795 while ((*new_item != '\0') && (*new_item != CHAR_NODE_END))
796 new_item++;
798 if (*++new_item == '[')
799 while (*++new_item != '\0')
800 if ((*new_item == ']') && (*++new_item != '\0') && (*++new_item != '\0'))
802 currentpoint = new_item;
803 selected_item = NULL;
804 break;
808 /* --------------------------------------------------------------------------------------------- */
810 static void
811 help_prev_node (void)
813 const char *new_item;
815 new_item = currentpoint;
816 while (((int) (new_item - fdata) > 1) && (*new_item != CHAR_NODE_END))
817 new_item--;
818 new_item--;
819 while (((int) (new_item - fdata) > 0) && (*new_item != CHAR_NODE_END))
820 new_item--;
821 while (*new_item != ']')
822 new_item++;
823 currentpoint = new_item + 2;
824 selected_item = NULL;
827 /* --------------------------------------------------------------------------------------------- */
829 static void
830 help_select_link (void)
832 /* follow link */
833 if (selected_item == NULL)
835 #ifdef WE_WANT_TO_GO_BACKWARD_ON_KEY_RIGHT
836 /* Is there any reason why the right key would take us
837 * backward if there are no links selected?, I agree
838 * with Torben than doing nothing in this case is better
840 /* If there are no links, go backward in history */
841 history_ptr--;
842 if (history_ptr < 0)
843 history_ptr = HISTORY_SIZE - 1;
845 currentpoint = history[history_ptr].page;
846 selected_item = history[history_ptr].link;
847 #endif
849 else
851 history_ptr = (history_ptr + 1) % HISTORY_SIZE;
852 history[history_ptr].page = currentpoint;
853 history[history_ptr].link = selected_item;
854 currentpoint = help_follow_link (currentpoint, selected_item);
857 selected_item = NULL;
860 /* --------------------------------------------------------------------------------------------- */
862 static cb_ret_t
863 help_execute_cmd (unsigned long command)
865 cb_ret_t ret = MSG_HANDLED;
867 switch (command)
869 case CK_Help:
870 help_help (whelp);
871 break;
872 case CK_Index:
873 help_index (whelp);
874 break;
875 case CK_Back:
876 help_back (whelp);
877 break;
878 case CK_Up:
879 help_prev_link (TRUE);
880 break;
881 case CK_Down:
882 help_next_link (TRUE);
883 break;
884 case CK_PageDown:
885 move_forward (help_lines - 1);
886 break;
887 case CK_PageUp:
888 move_backward (help_lines - 1);
889 break;
890 case CK_HalfPageDown:
891 move_forward (help_lines / 2);
892 break;
893 case CK_HalfPageUp:
894 move_backward (help_lines / 2);
895 break;
896 case CK_Top:
897 move_to_top ();
898 break;
899 case CK_Bottom:
900 move_to_bottom ();
901 break;
902 case CK_Enter:
903 help_select_link ();
904 break;
905 case CK_LinkNext:
906 help_next_link (FALSE);
907 break;
908 case CK_LinkPrev:
909 help_prev_link (FALSE);
910 break;
911 case CK_NodeNext:
912 help_next_node ();
913 break;
914 case CK_NodePrev:
915 help_prev_node ();
916 break;
917 case CK_Quit:
918 dlg_stop (whelp);
919 break;
920 default:
921 ret = MSG_NOT_HANDLED;
924 return ret;
927 /* --------------------------------------------------------------------------------------------- */
929 static cb_ret_t
930 help_handle_key (WDialog * h, int c)
932 unsigned long command;
934 command = keybind_lookup_keymap_command (help_map, c);
935 if ((command == CK_IgnoreKey) || (help_execute_cmd (command) == MSG_NOT_HANDLED))
936 return MSG_NOT_HANDLED;
938 send_message (h, NULL, MSG_DRAW, 0, NULL);
939 return MSG_HANDLED;
942 /* --------------------------------------------------------------------------------------------- */
944 static cb_ret_t
945 help_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
947 WDialog *h = DIALOG (w);
949 switch (msg)
951 case MSG_RESIZE:
953 WButtonBar *bb;
955 help_lines = min (LINES - 4, max (2 * LINES / 3, 18));
956 dlg_set_size (h, help_lines + 4, HELP_WINDOW_WIDTH + 4);
957 bb = find_buttonbar (h);
958 widget_set_size (WIDGET (bb), LINES - 1, 0, 1, COLS);
959 return MSG_HANDLED;
962 case MSG_DRAW:
963 dlg_default_repaint (h);
964 help_show (h, currentpoint);
965 return MSG_HANDLED;
967 case MSG_KEY:
968 return help_handle_key (h, parm);
970 case MSG_ACTION:
971 /* shortcut */
972 if (sender == NULL)
973 return help_execute_cmd (parm);
974 /* message from buttonbar */
975 if (sender == WIDGET (find_buttonbar (h)))
977 if (data != NULL)
978 return send_message (data, NULL, MSG_ACTION, parm, NULL);
979 return help_execute_cmd (parm);
981 return MSG_NOT_HANDLED;
983 default:
984 return dlg_default_callback (w, sender, msg, parm, data);
988 /* --------------------------------------------------------------------------------------------- */
990 static void
991 interactive_display_finish (void)
993 clear_link_areas ();
996 /* --------------------------------------------------------------------------------------------- */
997 /** translate help file into terminal encoding */
999 static void
1000 translate_file (char *filedata)
1002 GIConv conv;
1003 GString *translated_data;
1005 /* initial allocation for largest whole help file */
1006 translated_data = g_string_sized_new (32 * 1024);
1008 conv = str_crt_conv_from ("UTF-8");
1010 if (conv == INVALID_CONV)
1011 g_string_free (translated_data, TRUE);
1012 else
1014 g_free (fdata);
1016 if (str_convert (conv, filedata, translated_data) != ESTR_FAILURE)
1017 fdata = g_string_free (translated_data, FALSE);
1018 else
1020 fdata = NULL;
1021 g_string_free (translated_data, TRUE);
1023 str_close_conv (conv);
1027 /* --------------------------------------------------------------------------------------------- */
1029 static cb_ret_t
1030 md_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
1032 switch (msg)
1034 case MSG_RESIZE:
1035 w->lines = help_lines;
1036 return MSG_HANDLED;
1038 default:
1039 return widget_default_callback (w, sender, msg, parm, data);
1043 /* --------------------------------------------------------------------------------------------- */
1045 static Widget *
1046 mousedispatch_new (int y, int x, int yl, int xl)
1048 Widget *w;
1050 w = g_new (Widget, 1);
1051 widget_init (w, y, x, yl, xl, md_callback, help_event);
1052 return w;
1055 /* --------------------------------------------------------------------------------------------- */
1056 /*** public functions ****************************************************************************/
1057 /* --------------------------------------------------------------------------------------------- */
1059 /* event callback */
1060 gboolean
1061 help_interactive_display (const gchar * event_group_name, const gchar * event_name,
1062 gpointer init_data, gpointer data)
1064 const dlg_colors_t help_colors = {
1065 HELP_NORMAL_COLOR, /* common text color */
1066 0, /* unused in help */
1067 HELP_BOLD_COLOR, /* bold text color */
1068 0, /* unused in help */
1069 HELP_TITLE_COLOR /* title color */
1072 WButtonBar *help_bar;
1073 Widget *md;
1074 char *hlpfile = NULL;
1075 char *filedata;
1076 ev_help_t *event_data = (ev_help_t *) data;
1078 (void) event_group_name;
1079 (void) event_name;
1080 (void) init_data;
1082 if (event_data->filename != NULL)
1083 g_file_get_contents (event_data->filename, &filedata, NULL, NULL);
1084 else
1085 filedata = load_mc_home_file (mc_global.share_data_dir, MC_HELP, &hlpfile);
1087 if (filedata == NULL)
1088 message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"),
1089 event_data->filename ? event_data->filename : hlpfile, unix_error_string (errno));
1091 g_free (hlpfile);
1093 if (filedata == NULL)
1094 return TRUE;
1096 translate_file (filedata);
1098 g_free (filedata);
1100 if (fdata == NULL)
1101 return TRUE;
1103 if ((event_data->node == NULL) || (*event_data->node == '\0'))
1104 event_data->node = "[main]";
1106 main_node = search_string (fdata, event_data->node);
1108 if (main_node == NULL)
1110 message (D_ERROR, MSG_ERROR, _("Cannot find node %s in help file"), event_data->node);
1112 /* Fallback to [main], return if it also cannot be found */
1113 main_node = search_string (fdata, "[main]");
1114 if (main_node == NULL)
1116 interactive_display_finish ();
1117 return TRUE;
1121 help_lines = min (LINES - 4, max (2 * LINES / 3, 18));
1123 whelp =
1124 dlg_create (TRUE, 0, 0, help_lines + 4, HELP_WINDOW_WIDTH + 4,
1125 help_colors, help_callback, NULL, "[Help]", _("Help"),
1126 DLG_TRYUP | DLG_CENTER | DLG_WANT_TAB);
1128 selected_item = search_string_node (main_node, STRING_LINK_START) - 1;
1129 currentpoint = main_node + 1; /* Skip the newline following the start of the node */
1131 for (history_ptr = HISTORY_SIZE; history_ptr;)
1133 history_ptr--;
1134 history[history_ptr].page = currentpoint;
1135 history[history_ptr].link = selected_item;
1138 help_bar = buttonbar_new (TRUE);
1139 WIDGET (help_bar)->y -= WIDGET (whelp)->y;
1140 WIDGET (help_bar)->x -= WIDGET (whelp)->x;
1142 md = mousedispatch_new (1, 1, help_lines, HELP_WINDOW_WIDTH - 2);
1144 add_widget (whelp, md);
1145 add_widget (whelp, help_bar);
1147 buttonbar_set_label (help_bar, 1, Q_ ("ButtonBar|Help"), help_map, NULL);
1148 buttonbar_set_label (help_bar, 2, Q_ ("ButtonBar|Index"), help_map, NULL);
1149 buttonbar_set_label (help_bar, 3, Q_ ("ButtonBar|Prev"), help_map, NULL);
1150 buttonbar_set_label (help_bar, 4, "", help_map, NULL);
1151 buttonbar_set_label (help_bar, 5, "", help_map, NULL);
1152 buttonbar_set_label (help_bar, 6, "", help_map, NULL);
1153 buttonbar_set_label (help_bar, 7, "", help_map, NULL);
1154 buttonbar_set_label (help_bar, 8, "", help_map, NULL);
1155 buttonbar_set_label (help_bar, 9, "", help_map, NULL);
1156 buttonbar_set_label (help_bar, 10, Q_ ("ButtonBar|Quit"), help_map, NULL);
1158 dlg_run (whelp);
1159 interactive_display_finish ();
1160 dlg_destroy (whelp);
1161 return TRUE;
1164 /* --------------------------------------------------------------------------------------------- */