/* Copyright 2026, Stephen Fryatt (info@stevefryatt.org.uk)
 *
 * This file is part of Wimp Programming In C:
 *
 *   http://www.stevefryatt.org.uk/risc-os/wimp-prog
 *
 * Licensed under the EUPL, Version 1.2 only (the "Licence");
 * You may not use this work except in compliance with the
 * Licence.
 *
 * You may obtain a copy of the Licence at:
 *
 *   http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the Licence is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the Licence for the specific language governing
 * permissions and limitations under the Licence.
 */

/**
 * File: ihelp.c
 */

#include <string.h>

#include "oslib/help.h"

#include "sflib/errors.h"
#include "sflib/event.h"
#include "sflib/msgs.h"

#include "inthlp.h"

/* Round the given block size to the next full word. */

#define INTHLP_WORDALIGN(x) ( (x+3) & ~3 )

/* Function Prototypes. */

static osbool inthlp_send_reply_help_request(wimp_message *message);

/* Interactive Help Initialisation. */

void inthlp_initialise(void)
{
	event_add_message_handler(message_HELP_REQUEST, EVENT_MESSAGE_INCOMING, inthlp_send_reply_help_request);
}

/* Message_HelpRequest event handler. */

static osbool inthlp_send_reply_help_request(wimp_message *message)
{
	help_full_message_request	*help_request = (help_full_message_request *) message;
	help_full_message_reply		help_reply;

	msgs_lookup("HelpMessage", help_reply.reply, 236);

	help_reply.size = INTHLP_WORDALIGN(21 + strlen(help_reply.reply));
	help_reply.your_ref = help_request->my_ref;
	help_reply.action = message_HELP_REPLY;

	os_error *error = xwimp_send_message(wimp_USER_MESSAGE, (wimp_message *) &help_reply, help_request->sender);
	if (error != NULL)
		error_report_os_error(error, wimp_ERROR_BOX_CANCEL_ICON);

	return TRUE;
}
