Move widget add/del API from WDialog to WGroup.
[midnight-commander.git] / src / help.c
bloba05ff7ade2ad4a9486e4211c4d2725434e28094b
1 /*
2 Hypertext file browser.
4 Copyright (C) 1994-2020
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file help.c
25 * \brief Source: hypertext file browser
27 * Implements the hypertext file viewer.
28 * The hypertext file is a file that may have one or more nodes. Each
29 * node ends with a ^D character and starts with a bracket, then the
30 * name of the node and then a closing bracket. Right after the closing
31 * bracket a newline is placed. This newline is not to be displayed by
32 * the help viewer and must be skipped - its sole purpose is to faciliate
33 * the work of the people managing the help file template (xnc.hlp) .
35 * Links in the hypertext file are specified like this: the text that
36 * will be highlighted should have a leading ^A, then it comes the
37 * text, then a ^B indicating that highlighting is done, then the name
38 * of the node you want to link to and then a ^C.
40 * The file must contain a ^D at the beginning and at the end of the
41 * file or the program will not be able to detect the end of file.
43 * Lazyness/widgeting attack: This file does use the dialog manager
44 * and uses mainly the dialog to achieve the help work. there is only
45 * one specialized widget and it's only used to forward the mouse messages
46 * to the appropriate routine.
50 #include <config.h>
52 #include <errno.h>
53 #include <limits.h> /* MB_LEN_MAX */
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/skin.h"
62 #include "lib/strutil.h"
63 #include "lib/fileloc.h"
64 #include "lib/util.h"
65 #include "lib/widget.h"
66 #include "lib/event-types.h"
68 #include "keybind-defaults.h"
69 #include "help.h"
71 /*** global variables ****************************************************************************/
73 /*** file scope macro definitions ****************************************************************/
75 #define MAXLINKNAME 80
76 #define HISTORY_SIZE 20
77 #define HELP_WINDOW_WIDTH MIN(80, COLS - 16)
79 #define STRING_LINK_START "\01"
80 #define STRING_LINK_POINTER "\02"
81 #define STRING_LINK_END "\03"
82 #define STRING_NODE_END "\04"
84 /*** file scope type declarations ****************************************************************/
86 /* Link areas for the mouse */
87 typedef struct Link_Area
89 int x1, y1, x2, y2;
90 const char *link_name;
91 } Link_Area;
93 /*** file scope variables ************************************************************************/
95 static char *fdata = NULL; /* Pointer to the loaded data file */
96 static int help_lines; /* Lines in help viewer */
97 static int history_ptr; /* For the history queue */
98 static const char *main_node; /* The main node */
99 static const char *last_shown = NULL; /* Last byte shown in a screen */
100 static gboolean end_of_node = FALSE; /* Flag: the last character of the node shown? */
101 static const char *currentpoint;
102 static const char *selected_item;
104 /* The widget variables */
105 static WDialog *whelp;
107 static struct
109 const char *page; /* Pointer to the selected page */
110 const char *link; /* Pointer to the selected link */
111 } history[HISTORY_SIZE];
113 static GSList *link_area = NULL;
114 static gboolean inside_link_area = FALSE;
116 static cb_ret_t help_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data);
118 /*** file scope functions ************************************************************************/
119 /* --------------------------------------------------------------------------------------------- */
121 /** returns the position where text was found in the start buffer
122 * or 0 if not found
124 static const char *
125 search_string (const char *start, const char *text)
127 const char *result = NULL;
128 char *local_text = g_strdup (text);
129 char *d = local_text;
130 const char *e = start;
132 /* fmt sometimes replaces a space with a newline in the help file */
133 /* Replace the newlines in the link name with spaces to correct the situation */
134 while (*d != '\0')
136 if (*d == '\n')
137 *d = ' ';
138 str_next_char (&d);
141 /* Do search */
142 for (d = local_text; *e; e++)
144 if (*d == *e)
145 d++;
146 else
147 d = local_text;
148 if (*d == '\0')
150 result = e + 1;
151 break;
155 g_free (local_text);
156 return result;
159 /* --------------------------------------------------------------------------------------------- */
160 /** Searches text in the buffer pointed by start. Search ends
161 * if the CHAR_NODE_END is found in the text.
162 * @return NULL on failure
165 static const char *
166 search_string_node (const char *start, const char *text)
168 const char *d = text;
169 const char *e = start;
171 if (start != NULL)
172 for (; *e && *e != CHAR_NODE_END; e++)
174 if (*d == *e)
175 d++;
176 else
177 d = text;
178 if (*d == '\0')
179 return e + 1;
182 return NULL;
185 /* --------------------------------------------------------------------------------------------- */
186 /** Searches the_char in the buffer pointer by start and searches
187 * it can search forward (direction = 1) or backward (direction = -1)
190 static const char *
191 search_char_node (const char *start, char the_char, int direction)
193 const char *e;
195 for (e = start; (*e != '\0') && (*e != CHAR_NODE_END); e += direction)
196 if (*e == the_char)
197 return e;
199 return NULL;
202 /* --------------------------------------------------------------------------------------------- */
203 /** Returns the new current pointer when moved lines lines */
205 static const char *
206 move_forward2 (const char *c, int lines)
208 const char *p;
209 int line;
211 currentpoint = c;
212 for (line = 0, p = currentpoint; (*p != '\0') && (*p != CHAR_NODE_END); str_cnext_char (&p))
214 if (line == lines)
215 return currentpoint = p;
217 if (*p == '\n')
218 line++;
220 return currentpoint = c;
223 /* --------------------------------------------------------------------------------------------- */
225 static const char *
226 move_backward2 (const char *c, int lines)
228 const char *p;
229 int line;
231 currentpoint = c;
232 for (line = 0, p = currentpoint; (*p != '\0') && ((int) (p - fdata) >= 0); str_cprev_char (&p))
234 if (*p == CHAR_NODE_END)
236 /* We reached the beginning of the node */
237 /* Skip the node headers */
238 while (*p != ']')
239 str_cnext_char (&p);
240 return currentpoint = p + 2; /* Skip the newline following the start of the node */
243 if (*(p - 1) == '\n')
244 line++;
245 if (line == lines)
246 return currentpoint = p;
248 return currentpoint = c;
251 /* --------------------------------------------------------------------------------------------- */
253 static void
254 move_forward (int i)
256 if (!end_of_node)
257 currentpoint = move_forward2 (currentpoint, i);
260 /* --------------------------------------------------------------------------------------------- */
262 static void
263 move_backward (int i)
265 currentpoint = move_backward2 (currentpoint, ++i);
268 /* --------------------------------------------------------------------------------------------- */
270 static void
271 move_to_top (void)
273 while (((int) (currentpoint > fdata) > 0) && (*currentpoint != CHAR_NODE_END))
274 currentpoint--;
276 while (*currentpoint != ']')
277 currentpoint++;
278 currentpoint = currentpoint + 2; /* Skip the newline following the start of the node */
279 selected_item = NULL;
282 /* --------------------------------------------------------------------------------------------- */
284 static void
285 move_to_bottom (void)
287 while ((*currentpoint != '\0') && (*currentpoint != CHAR_NODE_END))
288 currentpoint++;
289 currentpoint--;
290 move_backward (1);
293 /* --------------------------------------------------------------------------------------------- */
295 static const char *
296 help_follow_link (const char *start, const char *lc_selected_item)
298 const char *p;
300 if (lc_selected_item == NULL)
301 return start;
303 for (p = lc_selected_item; *p && *p != CHAR_NODE_END && *p != CHAR_LINK_POINTER; p++)
305 if (*p == CHAR_LINK_POINTER)
307 int i;
308 char link_name[MAXLINKNAME];
310 link_name[0] = '[';
311 for (i = 1; *p != CHAR_LINK_END && *p && *p != CHAR_NODE_END && i < MAXLINKNAME - 3;)
312 link_name[i++] = *++p;
313 link_name[i - 1] = ']';
314 link_name[i] = '\0';
315 p = search_string (fdata, link_name);
316 if (p != NULL)
318 p += 1; /* Skip the newline following the start of the node */
319 return p;
323 /* Create a replacement page with the error message */
324 return _("Help file format error\n");
327 /* --------------------------------------------------------------------------------------------- */
329 static const char *
330 select_next_link (const char *current_link)
332 const char *p;
334 if (current_link == NULL)
335 return NULL;
337 p = search_string_node (current_link, STRING_LINK_END);
338 if (p == NULL)
339 return NULL;
340 p = search_string_node (p, STRING_LINK_START);
341 if (p == NULL)
342 return NULL;
343 return p - 1;
346 /* --------------------------------------------------------------------------------------------- */
348 static const char *
349 select_prev_link (const char *current_link)
351 return current_link == NULL ? NULL : search_char_node (current_link - 1, CHAR_LINK_START, -1);
354 /* --------------------------------------------------------------------------------------------- */
356 static void
357 start_link_area (int x, int y, const char *link_name)
359 Link_Area *la;
361 if (inside_link_area)
362 message (D_NORMAL, _("Warning"), "%s", _("Internal bug: Double start of link area"));
364 /* Allocate memory for a new link area */
365 la = g_new (Link_Area, 1);
366 /* Save the beginning coordinates of the link area */
367 la->x1 = x;
368 la->y1 = y;
369 /* Save the name of the destination anchor */
370 la->link_name = link_name;
371 link_area = g_slist_prepend (link_area, la);
373 inside_link_area = TRUE;
376 /* --------------------------------------------------------------------------------------------- */
378 static void
379 end_link_area (int x, int y)
381 if (inside_link_area)
383 Link_Area *la = (Link_Area *) link_area->data;
384 /* Save the end coordinates of the link area */
385 la->x2 = x;
386 la->y2 = y;
387 inside_link_area = FALSE;
391 /* --------------------------------------------------------------------------------------------- */
393 static void
394 clear_link_areas (void)
396 g_clear_slist (&link_area, g_free);
397 inside_link_area = FALSE;
400 /* --------------------------------------------------------------------------------------------- */
402 static void
403 help_print_word (WDialog * h, GString * word, int *col, int *line, gboolean add_space)
405 if (*line >= help_lines)
406 g_string_set_size (word, 0);
407 else
409 int w;
411 w = str_term_width1 (word->str);
412 if (*col + w >= HELP_WINDOW_WIDTH)
414 *col = 0;
415 (*line)++;
418 if (*line >= help_lines)
419 g_string_set_size (word, 0);
420 else
422 widget_gotoyx (h, *line + 2, *col + 2);
423 tty_print_string (word->str);
424 g_string_set_size (word, 0);
425 *col += w;
429 if (add_space)
431 if (*col < HELP_WINDOW_WIDTH - 1)
433 tty_print_char (' ');
434 (*col)++;
436 else
438 *col = 0;
439 (*line)++;
444 /* --------------------------------------------------------------------------------------------- */
446 static void
447 help_show (WDialog * h, const char *paint_start)
449 const char *p, *n;
450 int col, line, c;
451 gboolean painting = TRUE;
452 gboolean acs; /* Flag: Alternate character set active? */
453 gboolean repeat_paint;
454 int active_col, active_line; /* Active link position */
455 char buff[MB_LEN_MAX + 1];
456 GString *word;
458 word = g_string_sized_new (32);
460 tty_setcolor (HELP_NORMAL_COLOR);
463 line = col = active_col = active_line = 0;
464 repeat_paint = FALSE;
465 acs = FALSE;
467 clear_link_areas ();
468 if ((int) (selected_item - paint_start) < 0)
469 selected_item = NULL;
471 p = paint_start;
472 n = paint_start;
473 while ((n[0] != '\0') && (n[0] != CHAR_NODE_END) && (line < help_lines))
475 p = n;
476 n = str_cget_next_char (p);
477 memcpy (buff, p, n - p);
478 buff[n - p] = '\0';
480 c = (unsigned char) buff[0];
481 switch (c)
483 case CHAR_LINK_START:
484 if (selected_item == NULL)
485 selected_item = p;
486 if (p != selected_item)
487 tty_setcolor (HELP_LINK_COLOR);
488 else
490 tty_setcolor (HELP_SLINK_COLOR);
492 /* Store the coordinates of the link */
493 active_col = col + 2;
494 active_line = line + 2;
496 start_link_area (col, line, p);
497 break;
498 case CHAR_LINK_POINTER:
499 painting = FALSE;
500 break;
501 case CHAR_LINK_END:
502 painting = TRUE;
503 help_print_word (h, word, &col, &line, FALSE);
504 end_link_area (col - 1, line);
505 tty_setcolor (HELP_NORMAL_COLOR);
506 break;
507 case CHAR_ALTERNATE:
508 acs = TRUE;
509 break;
510 case CHAR_NORMAL:
511 acs = FALSE;
512 break;
513 case CHAR_VERSION:
514 widget_gotoyx (h, line + 2, col + 2);
515 tty_print_string (VERSION);
516 col += str_term_width1 (VERSION);
517 break;
518 case CHAR_FONT_BOLD:
519 tty_setcolor (HELP_BOLD_COLOR);
520 break;
521 case CHAR_FONT_ITALIC:
522 tty_setcolor (HELP_ITALIC_COLOR);
523 break;
524 case CHAR_FONT_NORMAL:
525 help_print_word (h, word, &col, &line, FALSE);
526 tty_setcolor (HELP_NORMAL_COLOR);
527 break;
528 case '\n':
529 if (painting)
530 help_print_word (h, word, &col, &line, FALSE);
531 line++;
532 col = 0;
533 break;
534 case ' ':
535 case '\t':
536 /* word delimiter */
537 if (painting)
539 help_print_word (h, word, &col, &line, c == ' ');
540 if (c == '\t')
542 col = (col / 8 + 1) * 8;
543 if (col >= HELP_WINDOW_WIDTH)
545 line++;
546 col = 8;
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 widget_gotoyx (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 (WIDGET (h)->y + line + 2, WIDGET (h)->x + col + 2,
569 #endif
570 col++;
576 /* print last word */
577 if (n[0] == CHAR_NODE_END)
578 help_print_word (h, word, &col, &line, FALSE);
580 last_shown = p;
581 end_of_node = line < help_lines;
582 tty_setcolor (HELP_NORMAL_COLOR);
583 if ((int) (selected_item - last_shown) >= 0)
585 if ((link_area == NULL) || (link_area->data == NULL))
586 selected_item = NULL;
587 else
589 selected_item = ((Link_Area *) link_area->data)->link_name;
590 repeat_paint = TRUE;
594 while (repeat_paint);
596 g_string_free (word, TRUE);
598 /* Position the cursor over a nice link */
599 if (active_col)
600 widget_gotoyx (h, active_line, active_col);
603 /* --------------------------------------------------------------------------------------------- */
604 /** show help */
606 static void
607 help_help (WDialog * h)
609 const char *p;
611 history_ptr = (history_ptr + 1) % HISTORY_SIZE;
612 history[history_ptr].page = currentpoint;
613 history[history_ptr].link = selected_item;
615 p = search_string (fdata, "[How to use help]");
616 if (p != NULL)
618 currentpoint = p + 1; /* Skip the newline following the start of the node */
619 selected_item = NULL;
620 send_message (h, NULL, MSG_DRAW, 0, NULL);
624 /* --------------------------------------------------------------------------------------------- */
626 static void
627 help_index (WDialog * h)
629 const char *new_item;
631 new_item = search_string (fdata, "[Contents]");
633 if (new_item == NULL)
634 message (D_ERROR, MSG_ERROR, _("Cannot find node %s in help file"), "[Contents]");
635 else
637 history_ptr = (history_ptr + 1) % HISTORY_SIZE;
638 history[history_ptr].page = currentpoint;
639 history[history_ptr].link = selected_item;
641 currentpoint = new_item + 1; /* Skip the newline following the start of the node */
642 selected_item = NULL;
643 send_message (h, NULL, MSG_DRAW, 0, NULL);
647 /* --------------------------------------------------------------------------------------------- */
649 static void
650 help_back (WDialog * h)
652 currentpoint = history[history_ptr].page;
653 selected_item = history[history_ptr].link;
654 history_ptr--;
655 if (history_ptr < 0)
656 history_ptr = HISTORY_SIZE - 1;
658 send_message (h, NULL, MSG_DRAW, 0, NULL); /* FIXME: unneeded? */
661 /* --------------------------------------------------------------------------------------------- */
663 static void
664 help_next_link (gboolean move_down)
666 const char *new_item;
668 new_item = select_next_link (selected_item);
669 if (new_item != NULL)
671 selected_item = new_item;
672 if ((int) (selected_item - last_shown) >= 0)
674 if (move_down)
675 move_forward (1);
676 else
677 selected_item = NULL;
680 else if (move_down)
681 move_forward (1);
682 else
683 selected_item = NULL;
686 /* --------------------------------------------------------------------------------------------- */
688 static void
689 help_prev_link (gboolean move_up)
691 const char *new_item;
693 new_item = select_prev_link (selected_item);
694 selected_item = new_item;
695 if ((selected_item == NULL) || (selected_item < currentpoint))
697 if (move_up)
698 move_backward (1);
699 else if ((link_area != NULL) && (link_area->data != NULL))
700 selected_item = ((Link_Area *) link_area->data)->link_name;
701 else
702 selected_item = NULL;
706 /* --------------------------------------------------------------------------------------------- */
708 static void
709 help_next_node (void)
711 const char *new_item;
713 new_item = currentpoint;
714 while ((*new_item != '\0') && (*new_item != CHAR_NODE_END))
715 new_item++;
717 if (*++new_item == '[')
718 while (*++new_item != '\0')
719 if ((*new_item == ']') && (*++new_item != '\0') && (*++new_item != '\0'))
721 currentpoint = new_item;
722 selected_item = NULL;
723 break;
727 /* --------------------------------------------------------------------------------------------- */
729 static void
730 help_prev_node (void)
732 const char *new_item;
734 new_item = currentpoint;
735 while (((int) (new_item - fdata) > 1) && (*new_item != CHAR_NODE_END))
736 new_item--;
737 new_item--;
738 while (((int) (new_item - fdata) > 0) && (*new_item != CHAR_NODE_END))
739 new_item--;
740 while (*new_item != ']')
741 new_item++;
742 currentpoint = new_item + 2;
743 selected_item = NULL;
746 /* --------------------------------------------------------------------------------------------- */
748 static void
749 help_select_link (void)
751 /* follow link */
752 if (selected_item == NULL)
754 #ifdef WE_WANT_TO_GO_BACKWARD_ON_KEY_RIGHT
755 /* Is there any reason why the right key would take us
756 * backward if there are no links selected?, I agree
757 * with Torben than doing nothing in this case is better
759 /* If there are no links, go backward in history */
760 history_ptr--;
761 if (history_ptr < 0)
762 history_ptr = HISTORY_SIZE - 1;
764 currentpoint = history[history_ptr].page;
765 selected_item = history[history_ptr].link;
766 #endif
768 else
770 history_ptr = (history_ptr + 1) % HISTORY_SIZE;
771 history[history_ptr].page = currentpoint;
772 history[history_ptr].link = selected_item;
773 currentpoint = help_follow_link (currentpoint, selected_item);
776 selected_item = NULL;
779 /* --------------------------------------------------------------------------------------------- */
781 static cb_ret_t
782 help_execute_cmd (long command)
784 cb_ret_t ret = MSG_HANDLED;
786 switch (command)
788 case CK_Help:
789 help_help (whelp);
790 break;
791 case CK_Index:
792 help_index (whelp);
793 break;
794 case CK_Back:
795 help_back (whelp);
796 break;
797 case CK_Up:
798 help_prev_link (TRUE);
799 break;
800 case CK_Down:
801 help_next_link (TRUE);
802 break;
803 case CK_PageDown:
804 move_forward (help_lines - 1);
805 break;
806 case CK_PageUp:
807 move_backward (help_lines - 1);
808 break;
809 case CK_HalfPageDown:
810 move_forward (help_lines / 2);
811 break;
812 case CK_HalfPageUp:
813 move_backward (help_lines / 2);
814 break;
815 case CK_Top:
816 move_to_top ();
817 break;
818 case CK_Bottom:
819 move_to_bottom ();
820 break;
821 case CK_Enter:
822 help_select_link ();
823 break;
824 case CK_LinkNext:
825 help_next_link (FALSE);
826 break;
827 case CK_LinkPrev:
828 help_prev_link (FALSE);
829 break;
830 case CK_NodeNext:
831 help_next_node ();
832 break;
833 case CK_NodePrev:
834 help_prev_node ();
835 break;
836 case CK_Quit:
837 dlg_stop (whelp);
838 break;
839 default:
840 ret = MSG_NOT_HANDLED;
843 return ret;
846 /* --------------------------------------------------------------------------------------------- */
848 static cb_ret_t
849 help_handle_key (WDialog * h, int c)
851 long command;
853 command = keybind_lookup_keymap_command (help_map, c);
854 if ((command == CK_IgnoreKey) || (help_execute_cmd (command) == MSG_NOT_HANDLED))
855 return MSG_NOT_HANDLED;
857 send_message (h, NULL, MSG_DRAW, 0, NULL);
858 return MSG_HANDLED;
861 /* --------------------------------------------------------------------------------------------- */
863 static cb_ret_t
864 help_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
866 WDialog *h = DIALOG (w);
868 switch (msg)
870 case MSG_RESIZE:
872 WButtonBar *bb;
874 help_lines = MIN (LINES - 4, MAX (2 * LINES / 3, 18));
875 dlg_set_size (h, help_lines + 4, HELP_WINDOW_WIDTH + 4);
876 bb = find_buttonbar (h);
877 widget_set_size (WIDGET (bb), LINES - 1, 0, 1, COLS);
878 return MSG_HANDLED;
881 case MSG_DRAW:
882 dlg_default_repaint (h);
883 help_show (h, currentpoint);
884 return MSG_HANDLED;
886 case MSG_KEY:
887 return help_handle_key (h, parm);
889 case MSG_ACTION:
890 /* Handle shortcuts and buttonbar. */
891 return help_execute_cmd (parm);
893 default:
894 return dlg_default_callback (w, sender, msg, parm, data);
898 /* --------------------------------------------------------------------------------------------- */
900 static void
901 interactive_display_finish (void)
903 clear_link_areas ();
906 /* --------------------------------------------------------------------------------------------- */
907 /** translate help file into terminal encoding */
909 static void
910 translate_file (char *filedata)
912 GIConv conv;
913 GString *translated_data;
915 /* initial allocation for largest whole help file */
916 translated_data = g_string_sized_new (32 * 1024);
918 conv = str_crt_conv_from ("UTF-8");
920 if (conv == INVALID_CONV)
921 g_string_free (translated_data, TRUE);
922 else
924 g_free (fdata);
926 if (str_convert (conv, filedata, translated_data) != ESTR_FAILURE)
927 fdata = g_string_free (translated_data, FALSE);
928 else
930 fdata = NULL;
931 g_string_free (translated_data, TRUE);
933 str_close_conv (conv);
937 /* --------------------------------------------------------------------------------------------- */
939 static cb_ret_t
940 md_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
942 switch (msg)
944 case MSG_RESIZE:
945 w->lines = help_lines;
946 return MSG_HANDLED;
948 default:
949 return widget_default_callback (w, sender, msg, parm, data);
953 /* --------------------------------------------------------------------------------------------- */
955 static void
956 help_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
958 int x, y;
959 GSList *current_area;
961 if (msg != MSG_MOUSE_CLICK)
962 return;
964 if ((event->buttons & GPM_B_RIGHT) != 0)
966 /* Right button click */
967 help_back (whelp);
968 return;
971 /* Left bytton click */
973 /* The event is relative to the dialog window, adjust it: */
974 x = event->x - 1;
975 y = event->y - 1;
977 /* Test whether the mouse click is inside one of the link areas */
978 for (current_area = link_area; current_area != NULL; current_area = g_slist_next (current_area))
980 Link_Area *la = (Link_Area *) current_area->data;
982 /* Test one line link area */
983 if (y == la->y1 && x >= la->x1 && y == la->y2 && x <= la->x2)
984 break;
986 /* Test two line link area */
987 if (la->y1 + 1 == la->y2)
989 /* The first line || The second line */
990 if ((y == la->y1 && x >= la->x1) || (y == la->y2 && x <= la->x2))
991 break;
993 /* Mouse will not work with link areas of more than two lines */
996 /* Test whether a link area was found */
997 if (current_area != NULL)
999 Link_Area *la = (Link_Area *) current_area->data;
1001 /* The click was inside a link area -> follow the link */
1002 history_ptr = (history_ptr + 1) % HISTORY_SIZE;
1003 history[history_ptr].page = currentpoint;
1004 history[history_ptr].link = la->link_name;
1005 currentpoint = help_follow_link (currentpoint, la->link_name);
1006 selected_item = NULL;
1008 else if (y < 0)
1009 move_backward (help_lines - 1);
1010 else if (y >= help_lines)
1011 move_forward (help_lines - 1);
1012 else if (y < help_lines / 2)
1013 move_backward (1);
1014 else
1015 move_forward (1);
1017 /* Show the new node */
1018 send_message (w->owner, NULL, MSG_DRAW, 0, NULL);
1021 /* --------------------------------------------------------------------------------------------- */
1023 static Widget *
1024 mousedispatch_new (int y, int x, int yl, int xl)
1026 Widget *w;
1028 w = g_new0 (Widget, 1);
1029 widget_init (w, y, x, yl, xl, md_callback, help_mouse_callback);
1030 w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR;
1032 return w;
1035 /* --------------------------------------------------------------------------------------------- */
1036 /*** public functions ****************************************************************************/
1037 /* --------------------------------------------------------------------------------------------- */
1039 /* event callback */
1040 gboolean
1041 help_interactive_display (const gchar * event_group_name, const gchar * event_name,
1042 gpointer init_data, gpointer data)
1044 const dlg_colors_t help_colors = {
1045 HELP_NORMAL_COLOR, /* common text color */
1046 0, /* unused in help */
1047 HELP_BOLD_COLOR, /* bold text color */
1048 0, /* unused in help */
1049 HELP_TITLE_COLOR /* title color */
1052 WGroup *g;
1053 WButtonBar *help_bar;
1054 Widget *md;
1055 char *hlpfile = NULL;
1056 char *filedata;
1057 ev_help_t *event_data = (ev_help_t *) data;
1059 (void) event_group_name;
1060 (void) event_name;
1061 (void) init_data;
1063 if (event_data->filename != NULL)
1064 g_file_get_contents (event_data->filename, &filedata, NULL, NULL);
1065 else
1066 filedata = load_mc_home_file (mc_global.share_data_dir, MC_HELP, &hlpfile, NULL);
1068 if (filedata == NULL)
1069 message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"),
1070 event_data->filename ? event_data->filename : hlpfile, unix_error_string (errno));
1072 g_free (hlpfile);
1074 if (filedata == NULL)
1075 return TRUE;
1077 translate_file (filedata);
1079 g_free (filedata);
1081 if (fdata == NULL)
1082 return TRUE;
1084 if ((event_data->node == NULL) || (*event_data->node == '\0'))
1085 event_data->node = "[main]";
1087 main_node = search_string (fdata, event_data->node);
1089 if (main_node == NULL)
1091 message (D_ERROR, MSG_ERROR, _("Cannot find node %s in help file"), event_data->node);
1093 /* Fallback to [main], return if it also cannot be found */
1094 main_node = search_string (fdata, "[main]");
1095 if (main_node == NULL)
1097 interactive_display_finish ();
1098 return TRUE;
1102 help_lines = MIN (LINES - 4, MAX (2 * LINES / 3, 18));
1104 whelp =
1105 dlg_create (TRUE, 0, 0, help_lines + 4, HELP_WINDOW_WIDTH + 4, WPOS_CENTER | WPOS_TRYUP,
1106 FALSE, help_colors, help_callback, NULL, "[Help]", _("Help"));
1107 g = GROUP (whelp);
1108 widget_want_tab (WIDGET (whelp), TRUE);
1110 selected_item = search_string_node (main_node, STRING_LINK_START) - 1;
1111 currentpoint = main_node + 1; /* Skip the newline following the start of the node */
1113 for (history_ptr = HISTORY_SIZE; history_ptr;)
1115 history_ptr--;
1116 history[history_ptr].page = currentpoint;
1117 history[history_ptr].link = selected_item;
1120 help_bar = buttonbar_new (TRUE);
1121 WIDGET (help_bar)->y -= WIDGET (whelp)->y;
1122 WIDGET (help_bar)->x -= WIDGET (whelp)->x;
1124 md = mousedispatch_new (1, 1, help_lines, HELP_WINDOW_WIDTH - 2);
1126 group_add_widget (g, md);
1127 group_add_widget (g, help_bar);
1129 buttonbar_set_label (help_bar, 1, Q_ ("ButtonBar|Help"), help_map, NULL);
1130 buttonbar_set_label (help_bar, 2, Q_ ("ButtonBar|Index"), help_map, NULL);
1131 buttonbar_set_label (help_bar, 3, Q_ ("ButtonBar|Prev"), help_map, NULL);
1132 buttonbar_set_label (help_bar, 4, "", help_map, NULL);
1133 buttonbar_set_label (help_bar, 5, "", help_map, NULL);
1134 buttonbar_set_label (help_bar, 6, "", help_map, NULL);
1135 buttonbar_set_label (help_bar, 7, "", help_map, NULL);
1136 buttonbar_set_label (help_bar, 8, "", help_map, NULL);
1137 buttonbar_set_label (help_bar, 9, "", help_map, NULL);
1138 buttonbar_set_label (help_bar, 10, Q_ ("ButtonBar|Quit"), help_map, NULL);
1140 dlg_run (whelp);
1141 interactive_display_finish ();
1142 dlg_destroy (whelp);
1143 return TRUE;
1146 /* --------------------------------------------------------------------------------------------- */