Add edit_add_window() function.
[midnight-commander.git] / src / help.c
blob09835791c69adea217563cac67bfab86d31075ac
1 /*
2 Hypertext file browser.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2011
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 appropiate 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 "keybind-defaults.h"
71 #include "help.h"
72 #include "main.h"
74 /*** global variables ****************************************************************************/
76 /*** file scope macro definitions ****************************************************************/
78 #define MAXLINKNAME 80
79 #define HISTORY_SIZE 20
80 #define HELP_WINDOW_WIDTH min(80, COLS - 16)
82 #define STRING_LINK_START "\01"
83 #define STRING_LINK_POINTER "\02"
84 #define STRING_LINK_END "\03"
85 #define STRING_NODE_END "\04"
87 /*** file scope type declarations ****************************************************************/
89 /* Link areas for the mouse */
90 typedef struct Link_Area
92 int x1, y1, x2, y2;
93 const char *link_name;
94 } Link_Area;
96 /*** file scope variables ************************************************************************/
98 static char *fdata = NULL; /* Pointer to the loaded data file */
99 static int help_lines; /* Lines in help viewer */
100 static int history_ptr; /* For the history queue */
101 static const char *main_node; /* The main node */
102 static const char *last_shown = NULL; /* Last byte shown in a screen */
103 static gboolean end_of_node = FALSE; /* Flag: the last character of the node shown? */
104 static const char *currentpoint;
105 static const char *selected_item;
107 /* The widget variables */
108 static Dlg_head *whelp;
110 static struct
112 const char *page; /* Pointer to the selected page */
113 const char *link; /* Pointer to the selected link */
114 } history[HISTORY_SIZE];
116 static GSList *link_area = NULL;
117 static gboolean inside_link_area = FALSE;
119 static cb_ret_t help_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data);
121 /*** file scope functions ************************************************************************/
122 /* --------------------------------------------------------------------------------------------- */
124 /** returns the position where text was found in the start buffer
125 * or 0 if not found
127 static const char *
128 search_string (const char *start, const char *text)
130 const char *result = NULL;
131 char *local_text = g_strdup (text);
132 char *d = local_text;
133 const char *e = start;
135 /* fmt sometimes replaces a space with a newline in the help file */
136 /* Replace the newlines in the link name with spaces to correct the situation */
137 while (*d != '\0')
139 if (*d == '\n')
140 *d = ' ';
141 str_next_char (&d);
144 /* Do search */
145 for (d = local_text; *e; e++)
147 if (*d == *e)
148 d++;
149 else
150 d = local_text;
151 if (*d == '\0')
153 result = e + 1;
154 break;
158 g_free (local_text);
159 return result;
162 /* --------------------------------------------------------------------------------------------- */
163 /** Searches text in the buffer pointed by start. Search ends
164 * if the CHAR_NODE_END is found in the text.
165 * @returns 0 on failure
168 static const char *
169 search_string_node (const char *start, const char *text)
171 const char *d = text;
172 const char *e = start;
174 if (start != NULL)
175 for (; *e && *e != CHAR_NODE_END; e++)
177 if (*d == *e)
178 d++;
179 else
180 d = text;
181 if (*d == '\0')
182 return e + 1;
185 return NULL;
188 /* --------------------------------------------------------------------------------------------- */
189 /** Searches the_char in the buffer pointer by start and searches
190 * it can search forward (direction = 1) or backward (direction = -1)
193 static const char *
194 search_char_node (const char *start, char the_char, int direction)
196 const char *e;
198 for (e = start; (*e != '\0') && (*e != CHAR_NODE_END); e += direction)
199 if (*e == the_char)
200 return e;
202 return NULL;
205 /* --------------------------------------------------------------------------------------------- */
206 /** Returns the new current pointer when moved lines lines */
208 static const char *
209 move_forward2 (const char *c, int lines)
211 const char *p;
212 int line;
214 currentpoint = c;
215 for (line = 0, p = currentpoint; (*p != '\0') && (*p != CHAR_NODE_END); str_cnext_char (&p))
217 if (line == lines)
218 return currentpoint = p;
220 if (*p == '\n')
221 line++;
223 return currentpoint = c;
226 /* --------------------------------------------------------------------------------------------- */
228 static const char *
229 move_backward2 (const char *c, int lines)
231 const char *p;
232 int line;
234 currentpoint = c;
235 for (line = 0, p = currentpoint; (*p != '\0') && ((int) (p - fdata) >= 0); str_cprev_char (&p))
237 if (*p == CHAR_NODE_END)
239 /* We reached the beginning of the node */
240 /* Skip the node headers */
241 while (*p != ']')
242 str_cnext_char (&p);
243 return currentpoint = p + 2; /* Skip the newline following the start of the node */
246 if (*(p - 1) == '\n')
247 line++;
248 if (line == lines)
249 return currentpoint = p;
251 return currentpoint = c;
254 /* --------------------------------------------------------------------------------------------- */
256 static void
257 move_forward (int i)
259 if (!end_of_node)
260 currentpoint = move_forward2 (currentpoint, i);
263 /* --------------------------------------------------------------------------------------------- */
265 static void
266 move_backward (int i)
268 currentpoint = move_backward2 (currentpoint, ++i);
271 /* --------------------------------------------------------------------------------------------- */
273 static void
274 move_to_top (void)
276 while (((int) (currentpoint > fdata) > 0) && (*currentpoint != CHAR_NODE_END))
277 currentpoint--;
279 while (*currentpoint != ']')
280 currentpoint++;
281 currentpoint = currentpoint + 2; /* Skip the newline following the start of the node */
282 selected_item = NULL;
285 /* --------------------------------------------------------------------------------------------- */
287 static void
288 move_to_bottom (void)
290 while ((*currentpoint != '\0') && (*currentpoint != CHAR_NODE_END))
291 currentpoint++;
292 currentpoint--;
293 move_backward (1);
296 /* --------------------------------------------------------------------------------------------- */
298 static const char *
299 help_follow_link (const char *start, const char *lc_selected_item)
301 char link_name[MAXLINKNAME];
302 const char *p;
303 int i = 0;
305 if (lc_selected_item == NULL)
306 return start;
308 for (p = lc_selected_item; *p && *p != CHAR_NODE_END && *p != CHAR_LINK_POINTER; p++)
310 if (*p == CHAR_LINK_POINTER)
312 link_name[0] = '[';
313 for (i = 1; *p != CHAR_LINK_END && *p && *p != CHAR_NODE_END && i < MAXLINKNAME - 3;)
314 link_name[i++] = *++p;
315 link_name[i - 1] = ']';
316 link_name[i] = '\0';
317 p = search_string (fdata, link_name);
318 if (p != NULL)
320 p += 1; /* Skip the newline following the start of the node */
321 return p;
325 /* Create a replacement page with the error message */
326 return _("Help file format error\n");
329 /* --------------------------------------------------------------------------------------------- */
331 static const char *
332 select_next_link (const char *current_link)
334 const char *p;
336 if (current_link == NULL)
337 return NULL;
339 p = search_string_node (current_link, STRING_LINK_END);
340 if (p == NULL)
341 return NULL;
342 p = search_string_node (p, STRING_LINK_START);
343 if (p == NULL)
344 return NULL;
345 return p - 1;
348 /* --------------------------------------------------------------------------------------------- */
350 static const char *
351 select_prev_link (const char *current_link)
353 return current_link == NULL ? NULL : search_char_node (current_link - 1, CHAR_LINK_START, -1);
356 /* --------------------------------------------------------------------------------------------- */
358 static void
359 start_link_area (int x, int y, const char *link_name)
361 Link_Area *la;
363 if (inside_link_area)
364 message (D_NORMAL, _("Warning"), _("Internal bug: Double start of link area"));
366 /* Allocate memory for a new link area */
367 la = g_new (Link_Area, 1);
368 /* Save the beginning coordinates of the link area */
369 la->x1 = x;
370 la->y1 = y;
371 /* Save the name of the destination anchor */
372 la->link_name = link_name;
373 link_area = g_slist_prepend (link_area, la);
375 inside_link_area = TRUE;
378 /* --------------------------------------------------------------------------------------------- */
380 static void
381 end_link_area (int x, int y)
383 if (inside_link_area)
385 Link_Area *la = (Link_Area *) link_area->data;
386 /* Save the end coordinates of the link area */
387 la->x2 = x;
388 la->y2 = y;
389 inside_link_area = FALSE;
393 /* --------------------------------------------------------------------------------------------- */
395 static void
396 clear_link_areas (void)
398 g_slist_foreach (link_area, (GFunc) g_free, NULL);
399 g_slist_free (link_area);
400 link_area = NULL;
401 inside_link_area = FALSE;
404 /* --------------------------------------------------------------------------------------------- */
406 static void
407 help_print_word (Dlg_head * h, GString * word, int *col, int *line, gboolean add_space)
409 if (*line >= help_lines)
410 g_string_set_size (word, 0);
411 else
413 int w;
415 w = str_term_width1 (word->str);
416 if (*col + w >= HELP_WINDOW_WIDTH)
418 *col = 0;
419 (*line)++;
422 if (*line >= help_lines)
423 g_string_set_size (word, 0);
424 else
426 dlg_move (h, *line + 2, *col + 2);
427 tty_print_string (word->str);
428 g_string_set_size (word, 0);
429 *col += w;
433 if (add_space)
435 if (*col < HELP_WINDOW_WIDTH - 1)
437 tty_print_char (' ');
438 (*col)++;
440 else
442 *col = 0;
443 (*line)++;
448 /* --------------------------------------------------------------------------------------------- */
450 static void
451 help_show (Dlg_head * h, const char *paint_start)
453 const char *p, *n;
454 int col, line, c;
455 gboolean painting = TRUE;
456 gboolean acs; /* Flag: Alternate character set active? */
457 gboolean repeat_paint;
458 int active_col, active_line; /* Active link position */
459 char buff[MB_LEN_MAX + 1];
460 GString *word;
462 word = g_string_sized_new (32);
464 tty_setcolor (HELP_NORMAL_COLOR);
467 line = col = active_col = active_line = 0;
468 repeat_paint = FALSE;
469 acs = FALSE;
471 clear_link_areas ();
472 if ((int) (selected_item - paint_start) < 0)
473 selected_item = NULL;
475 p = paint_start;
476 n = paint_start;
477 while ((n[0] != '\0') && (n[0] != CHAR_NODE_END) && (line < help_lines))
479 p = n;
480 n = str_cget_next_char (p);
481 memcpy (buff, p, n - p);
482 buff[n - p] = '\0';
484 c = (unsigned char) buff[0];
485 switch (c)
487 case CHAR_LINK_START:
488 if (selected_item == NULL)
489 selected_item = p;
490 if (p != selected_item)
491 tty_setcolor (HELP_LINK_COLOR);
492 else
494 tty_setcolor (HELP_SLINK_COLOR);
496 /* Store the coordinates of the link */
497 active_col = col + 2;
498 active_line = line + 2;
500 start_link_area (col, line, p);
501 break;
502 case CHAR_LINK_POINTER:
503 painting = FALSE;
504 break;
505 case CHAR_LINK_END:
506 painting = TRUE;
507 help_print_word (h, word, &col, &line, FALSE);
508 end_link_area (col - 1, line);
509 tty_setcolor (HELP_NORMAL_COLOR);
510 break;
511 case CHAR_ALTERNATE:
512 acs = TRUE;
513 break;
514 case CHAR_NORMAL:
515 acs = FALSE;
516 break;
517 case CHAR_VERSION:
518 dlg_move (h, line + 2, col + 2);
519 tty_print_string (VERSION);
520 col += str_term_width1 (VERSION);
521 break;
522 case CHAR_FONT_BOLD:
523 tty_setcolor (HELP_BOLD_COLOR);
524 break;
525 case CHAR_FONT_ITALIC:
526 tty_setcolor (HELP_ITALIC_COLOR);
527 break;
528 case CHAR_FONT_NORMAL:
529 help_print_word (h, word, &col, &line, FALSE);
530 tty_setcolor (HELP_NORMAL_COLOR);
531 break;
532 case '\n':
533 if (painting)
534 help_print_word (h, word, &col, &line, FALSE);
535 line++;
536 col = 0;
537 break;
538 case '\t':
539 col = (col / 8 + 1) * 8;
540 if (col >= HELP_WINDOW_WIDTH)
542 line++;
543 col = 8;
545 break;
546 case ' ':
547 /* word delimeter */
548 if (painting)
549 help_print_word (h, word, &col, &line, TRUE);
550 break;
551 default:
552 if (painting && (line < help_lines))
554 if (!acs)
555 /* accumulate symbols in a word */
556 g_string_append (word, buff);
557 else if (col < HELP_WINDOW_WIDTH)
559 dlg_move (h, line + 2, col + 2);
561 if ((c == ' ') || (c == '.'))
562 tty_print_char (c);
563 else
564 #ifndef HAVE_SLANG
565 tty_print_char (acs_map[c]);
566 #else
567 SLsmg_draw_object (h->y + line + 2, h->x + col + 2, c);
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 dlg_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 help_callback (w->owner, NULL, DLG_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 help_callback (w->owner, NULL, DLG_DRAW, 0, NULL);
681 return MOU_NORMAL;
684 /* --------------------------------------------------------------------------------------------- */
685 /** show help */
687 static void
688 help_help (Dlg_head * 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 help_callback (h, NULL, DLG_DRAW, 0, NULL);
705 /* --------------------------------------------------------------------------------------------- */
707 static void
708 help_index (Dlg_head * 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 help_callback (h, NULL, DLG_DRAW, 0, NULL);
728 /* --------------------------------------------------------------------------------------------- */
730 static void
731 help_back (Dlg_head * 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 help_callback (h, NULL, DLG_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 (Dlg_head * 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 help_callback (h, NULL, DLG_DRAW, 0, NULL);
939 return MSG_HANDLED;
942 /* --------------------------------------------------------------------------------------------- */
944 static cb_ret_t
945 help_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
947 WButtonBar *bb;
949 switch (msg)
951 case DLG_RESIZE:
952 help_lines = min (LINES - 4, max (2 * LINES / 3, 18));
953 dlg_set_size (h, help_lines + 4, HELP_WINDOW_WIDTH + 4);
954 bb = find_buttonbar (h);
955 widget_set_size (&bb->widget, LINES - 1, 0, 1, COLS);
956 return MSG_HANDLED;
958 case DLG_DRAW:
959 common_dialog_repaint (h);
960 help_show (h, currentpoint);
961 return MSG_HANDLED;
963 case DLG_KEY:
964 return help_handle_key (h, parm);
966 case DLG_ACTION:
967 /* shortcut */
968 if (sender == NULL)
969 return help_execute_cmd (parm);
970 /* message from buttonbar */
971 if (sender == (Widget *) find_buttonbar (h))
973 if (data != NULL)
974 return send_message ((Widget *) data, WIDGET_COMMAND, parm);
975 return help_execute_cmd (parm);
977 return MSG_NOT_HANDLED;
979 default:
980 return default_dlg_callback (h, sender, msg, parm, data);
984 /* --------------------------------------------------------------------------------------------- */
986 static void
987 interactive_display_finish (void)
989 clear_link_areas ();
992 /* --------------------------------------------------------------------------------------------- */
993 /** translate help file into terminal encoding */
995 static void
996 translate_file (char *filedata)
998 GIConv conv;
999 GString *translated_data;
1001 /* initial allocation for largest whole help file */
1002 translated_data = g_string_sized_new (32 * 1024);
1004 conv = str_crt_conv_from ("UTF-8");
1006 if (conv == INVALID_CONV)
1007 g_string_free (translated_data, TRUE);
1008 else
1010 g_free (fdata);
1012 if (str_convert (conv, filedata, translated_data) != ESTR_FAILURE)
1013 fdata = g_string_free (translated_data, FALSE);
1014 else
1016 fdata = NULL;
1017 g_string_free (translated_data, TRUE);
1019 str_close_conv (conv);
1023 /* --------------------------------------------------------------------------------------------- */
1025 static cb_ret_t
1026 md_callback (Widget * w, widget_msg_t msg, int parm)
1028 switch (msg)
1030 case WIDGET_RESIZED:
1031 w->lines = help_lines;
1032 return MSG_HANDLED;
1034 default:
1035 return default_proc (msg, parm);
1039 /* --------------------------------------------------------------------------------------------- */
1041 static Widget *
1042 mousedispatch_new (int y, int x, int yl, int xl)
1044 Widget *w = g_new (Widget, 1);
1045 init_widget (w, y, x, yl, xl, md_callback, help_event);
1046 return w;
1049 /* --------------------------------------------------------------------------------------------- */
1050 /*** public functions ****************************************************************************/
1051 /* --------------------------------------------------------------------------------------------- */
1053 /* event callback */
1054 gboolean
1055 help_interactive_display (const gchar * event_group_name, const gchar * event_name,
1056 gpointer init_data, gpointer data)
1058 const dlg_colors_t help_colors = {
1059 HELP_NORMAL_COLOR, /* common text color */
1060 0, /* unused in help */
1061 HELP_BOLD_COLOR, /* bold text color */
1062 0, /* unused in help */
1063 HELP_TITLE_COLOR /* title color */
1066 WButtonBar *help_bar;
1067 Widget *md;
1068 char *hlpfile = NULL;
1069 char *filedata;
1070 ev_help_t *event_data = (ev_help_t *) data;
1072 (void) event_group_name;
1073 (void) event_name;
1074 (void) init_data;
1076 if (event_data->filename != NULL)
1077 g_file_get_contents (event_data->filename, &filedata, NULL, NULL);
1078 else
1079 filedata = load_mc_home_file (mc_global.share_data_dir, MC_HELP, &hlpfile);
1081 if (filedata == NULL)
1082 message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"),
1083 event_data->filename ? event_data->filename : hlpfile, unix_error_string (errno));
1085 g_free (hlpfile);
1087 if (filedata == NULL)
1088 return TRUE;
1090 translate_file (filedata);
1092 g_free (filedata);
1094 if (fdata == NULL)
1095 return TRUE;
1097 if ((event_data->node == NULL) || (*event_data->node == '\0'))
1098 event_data->node = "[main]";
1100 main_node = search_string (fdata, event_data->node);
1102 if (main_node == NULL)
1104 message (D_ERROR, MSG_ERROR, _("Cannot find node %s in help file"), event_data->node);
1106 /* Fallback to [main], return if it also cannot be found */
1107 main_node = search_string (fdata, "[main]");
1108 if (main_node == NULL)
1110 interactive_display_finish ();
1111 return TRUE;
1115 help_lines = min (LINES - 4, max (2 * LINES / 3, 18));
1117 whelp =
1118 create_dlg (TRUE, 0, 0, help_lines + 4, HELP_WINDOW_WIDTH + 4,
1119 help_colors, help_callback, NULL, "[Help]", _("Help"),
1120 DLG_TRYUP | DLG_CENTER | DLG_WANT_TAB);
1122 selected_item = search_string_node (main_node, STRING_LINK_START) - 1;
1123 currentpoint = main_node + 1; /* Skip the newline following the start of the node */
1125 for (history_ptr = HISTORY_SIZE; history_ptr;)
1127 history_ptr--;
1128 history[history_ptr].page = currentpoint;
1129 history[history_ptr].link = selected_item;
1132 help_bar = buttonbar_new (TRUE);
1133 help_bar->widget.y -= whelp->y;
1134 help_bar->widget.x -= whelp->x;
1136 md = mousedispatch_new (1, 1, help_lines, HELP_WINDOW_WIDTH - 2);
1138 add_widget (whelp, md);
1139 add_widget (whelp, help_bar);
1141 buttonbar_set_label (help_bar, 1, Q_ ("ButtonBar|Help"), help_map, NULL);
1142 buttonbar_set_label (help_bar, 2, Q_ ("ButtonBar|Index"), help_map, NULL);
1143 buttonbar_set_label (help_bar, 3, Q_ ("ButtonBar|Prev"), help_map, NULL);
1144 buttonbar_set_label (help_bar, 4, "", help_map, NULL);
1145 buttonbar_set_label (help_bar, 5, "", help_map, NULL);
1146 buttonbar_set_label (help_bar, 6, "", help_map, NULL);
1147 buttonbar_set_label (help_bar, 7, "", help_map, NULL);
1148 buttonbar_set_label (help_bar, 8, "", help_map, NULL);
1149 buttonbar_set_label (help_bar, 9, "", help_map, NULL);
1150 buttonbar_set_label (help_bar, 10, Q_ ("ButtonBar|Quit"), help_map, NULL);
1152 run_dlg (whelp);
1153 interactive_display_finish ();
1154 destroy_dlg (whelp);
1155 return TRUE;
1158 /* --------------------------------------------------------------------------------------------- */