/* Copyright 2015-2020, 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: main.c
 */

#include "oslib/osspriteop.h"
#include "oslib/wimp.h"
#include "sflib/errors.h"
#include "sflib/event.h"
#include "sflib/resources.h"
#include "sflib/url.h"

#include "main.h"

#include "ibar.h"
#include "menu.h"
#include "win.h"

/* Application Details */

static char *main_application_name = "Example App";

static char *main_application_sprite = "!examplapp";

/* Global Variables */

osbool main_quit_flag = FALSE;

/* Function Prototypes */

static void main_initialise(void);
static void main_poll(void);
static void main_terminate(void);
static osbool main_message_quit(wimp_message *message);

/* Main code entry point. */

int main(int argc, char *argv[])
{
	main_initialise();
	main_poll();
	main_terminate();

	return 0;
}

/* Global application initialisation. */

static void main_initialise(void)
{
	os_error	*error;
	osspriteop_area	*sprites;

	wimp_initialise(wimp_VERSION_RO3, main_application_name, NULL, NULL);

	error_initialise(main_application_name, main_application_sprite, NULL);

	event_add_message_handler(message_QUIT, EVENT_MESSAGE_INCOMING, main_message_quit);

	/* Initialise library modules. */

	url_initialise();

	/* Load the application sprites. */

	sprites = resources_load_user_sprite_area("<ExamplApp$Dir>.Sprites");
	if (sprites == NULL)
		error_report_fatal("Failed to load application Sprites");

	/* Open the templates file. */

	error = xwimp_open_template("<ExamplApp$Dir>.Templates");
	if (error != NULL)
		error_report_program(error);

	/* Initialise the program modules. */

	menu_initialise();
	ibar_initialise(main_application_sprite);
	win_initialise(sprites);

	/* Close the templates file. */

	wimp_close_template();
}

/* Wimp_Poll loop. */

static void main_poll(void)
{
	wimp_block	blk;
	wimp_event_no	reason;
	int		pollword;

	while (!main_quit_flag) {
		reason = wimp_poll(wimp_MASK_NULL, &blk, &pollword);

		if (!event_process_event(reason, &blk, pollword, NULL)) {
			switch (reason) {
			case wimp_OPEN_WINDOW_REQUEST:
				wimp_open_window(&(blk.open));
				break;

			case wimp_CLOSE_WINDOW_REQUEST:
				wimp_close_window(blk.close.w);
				break;

			case wimp_MENU_SELECTION:
				menu_process_event(&(blk.selection));
				break;
			}
		}
	}
}

/* Global application termination. */

static void main_terminate(void)
{
	wimp_close_down(0);
}

/* Message_Quit event handler. */

static osbool main_message_quit(wimp_message *message)
{
	main_quit_flag = TRUE;

	return TRUE;
}
