/* libsswf_tag_framelabel.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2002-2008 */

/*

Copyright (c) 2002-2008 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

/** \file
 *
 * \brief The implementation of the sswf::TagFrameLabel class
 *
 * This file declares the body of the functions which are not
 * inline. It is part of the SSWF library.
 */

#include	"sswf/libsswf.h"

using namespace sswf;


/////////////////////////////////////////// TagFrameLabel

/** \class sswf::TagFrameLabel
 *
 * \brief Defines a label (name) for a frame
 *
 * This tag is used to name a frame in a movie. The advantage is to be
 * able to go to that frame by name later.
 *
 * \sa <a href="../SWFalexref.html#tag_framelabel">SWF Alexis' Reference&mdash;Frame Label</a>
 * \sa <a href="../SWFalexref.html#swf_tag">SWF Alexis' Reference&mdash;swf_tag</a>
 */


/** \brief Initializes the frame label object
 *
 * The constructor initializes the frame label to <i>no name</i>.
 *
 * \param[in] parent The parent (TagHeader) of this frame
 */
TagFrameLabel::TagFrameLabel(TagBase *parent)
	: TagBase("framelabel", parent)
{
	f_label = 0;
}


/** \brief This function returns the type of a TagFrameLabel object
 *
 * This function returns SWF_TYPE_CONTROL.
 *
 * \return SWF_TYPE_CONTROL
 */
TagBase::swf_type_t TagFrameLabel::TypeFlags(void) const
{
	return SWF_TYPE_CONTROL;
}


/** \brief This function defines the name (label) of a frame
 *
 * This function can be used to define the name of a frame.
 *
 * The name can really be anything, but it is strongly suggested
 * that you start the name with a letter and only use letters,
 * digits and underscores for full compatibility.
 *
 * When the name starts with a '#' character, it is taken as
 * an anchor. This anchor can be specified on the URL when
 * accessing the Flash animation.
 *
 * Set the frame label to an empty name (or do not set it) to
 * avoid saving it in the output movie.
 *
 * \param[in] label The frame label/name
 */
void TagFrameLabel::SetFrameLabel(const char *label)
{
	MemFree(f_label);
	f_label = StrDup(label);
}


/** \brief Ensure the minimum version is valid.
 *
 * This function checks that the movie is at least a version 3
 * Flash animation.
 *
 * If the anchor extension is used (i.e. the label name starts
 * with a '#') then the minimum version must be 6.
 *
 * \return An ErrorManager::error_code_t, ErrorManager::ERROR_CODE_NONE when no errors occur
 */
ErrorManager::error_code_t TagFrameLabel::PreSave(void)
{
	if(f_label != 0 && *f_label != '\0') {
		if(*f_label == '#') {
			MinimumVersion(6);
		}
		else {
			MinimumVersion(3);
		}
	}

	return ErrorManager::ERROR_CODE_NONE;
}


/** \brief Save the frame label in the specified Data buffer
 *
 * This function saves the label in the specified Data buffer.
 *
 * If the name has not been set or set to an empty string, then
 * this function has no effect (nothing is saved.)
 *
 * If an anchor is saved, the label string is followed by one
 * byte set to 1.
 *
 * \param[in,out] data The Data buffer where the frame label is saved
 *
 * \return An ErrorManager::error_code_t, ErrorManager::ERROR_CODE_NONE when no errors occur
 */
ErrorManager::error_code_t TagFrameLabel::Save(Data& data)
{
	long				size, anchor;
	const char			*name;
	ErrorManager::error_code_t	ec;

	name = f_label;
	if(name == 0 || *name == '\0') {
		return ErrorManager::ERROR_CODE_NONE;
	}

	size = strlen(name) + 1;

	anchor = *name == '#';

	// NOTE: the size is correct in either case since when we remove
	//	 one character from the name, we have to add 1 char at
	//	 the end of the string

	SaveTag(data, SWF_TAG_FRAME_LABEL, size);

	if(anchor) {
		ec = SaveString(data, name + 1);
		data.PutByte(1);
	}
	else {
		ec = SaveString(data, name);
	}

	return ec;
}





/* The following options fold the documentation; use 'zi' to turn on and off
 *
 * vim: foldexpr=getline(v\:lnum)!~'^/\\*\\*'&&getline(v\:lnum)!~'^\ \\*'?0\:1 foldcolumn=2 foldmethod=expr
 */
