/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: html_item.cxx,v 1.13 2003/02/10 10:56:26 schmitzj Exp $)
 * 
 * Copyright (C) 1998 Jürgen Schmitz
 *
 * HTML Parser system
 * (c) 1998 by Jürgen Schmitz - all rights reserved
 * THIS IS NOT GPL OR SOMETHING OTHER FREE LICENSE!!
 * it may only be used together with Xclasses!
 *-----------------------------------------------------------------------
 */

#include "include/html_item.h"
#include "include/html_parser.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

html_boxitem::html_boxitem(html_box *m) : html_item()
{
	html_box2item *i=new html_box2item;
	m->AddItem(i);
	mybox=m->CreateBox();
	i->SetBox(mybox);
}
html_boxitem::~html_boxitem()
{
	if (mybox) delete mybox;
}
html_box2item::html_box2item(void) : html_item()
{
	mybox=NULL;
}
html_box2item::~html_box2item()
{
}
void html_box2item::SetBox(class html_box *m) { mybox=m; }
void html_box2item::CalcSize(void)
{
	mybox->SetFont(box->GetFontstyle(),box->GetFontsize());
	mybox->CalcSize();
	if (width<1) width=1;
	if (height<1) height=1;
}
void html_box2item::CalcItem(void)
{
	int w;
	mybox->CalcBox(x,y,width,w,height);
	if (w>width) mybox->CalcBox(x,y,w,w,height);
	width=w;
	if (width<1) width=1;
	if (height<1) height=1;
}
void html_box2item::Create(html_drawspec *parent) { mybox->CreateBox(parent); }
void html_box2item::Move(void) { mybox->MoveBox(x,y); }
void html_box2item::Draw(int nosub) { if (!nosub) mybox->DrawBox(nosub); }
void html_box2item::Free(void) { mybox->FreeBox(); }
char *html_box2item::Mark(int x1,int y1,int x2,int y2,int nosub) { if (!nosub) return mybox->Mark(x1,y1,x2,y2,nosub); else return NULL; }
void html_box2item::Unmark(int nosub) { if (!nosub) mybox->Unmark(nosub); }


html_boxenditem::html_boxenditem(html_box *m) : html_item()
{
	m->ResetBox();
}
html_boxenditem::~html_boxenditem()
{
}

// -------------------------------------------------------------------

html_callbackitem::html_callbackitem(html_parser *p) : html_item()
{
	callback=p;
}
html_callbackitem::~html_callbackitem()
{
	box->Callback(NULL);
}
void html_callbackitem::CalcSize(void) { box->Callback(callback); }

// -------------------------------------------------------------------

html_textitem::html_textitem(char *txt) : html_item()
{
	text=strdup(txt);
	mtext=NULL;
}
html_textitem::html_textitem(char *txt,int size) : html_item()
{
	text=(char*)malloc(size+1);
	text[size]=0;
	while (size>0)
	{
		text[size-1]=txt[size-1];
		size--;
	}
	mtext=NULL;
}
html_textitem::~html_textitem()
{
	delete [] mtext;
	free(text);
}
void html_textitem::CalcSize(void)
{
	width=box->TextLength(text);
	height=box->TextHeight(text);
}
void html_textitem::Draw(int nosub)
{
	box->Text(text);
}
char *html_textitem::Mark(int x1,int y1,int x2,int y2,int nosub)
{
	int size;
	box->MarkedText(text,x1,x2);
	char *t=box->CutText(text,x1,x2,size);
	delete [] mtext;
	mtext=new char[size+1];
	mtext[0]=0;
	strncat(mtext,t,size);
	return mtext;
}
void html_textitem::Unmark(int nosub)
{
	Draw(nosub);
}

// -------------------------------------------------------------------

html_spaceitem::html_spaceitem(void) : html_item()
{
	free_width=1;
}
html_spaceitem::~html_spaceitem()
{
}
void html_spaceitem::CalcSize(void) { width=box->TextLength(" "); height=box->TextHeight(" "); }
void html_spaceitem::Draw(int nosub)
{
	if (!hidden>0)
	{
		box->Text(" ");
	}
}
char *html_spaceitem::Mark(int x1,int y1,int x2,int y2,int nosub)
{
	if (!hidden>0)
	{
		box->MarkedText(" ",x1,x2);
		return " ";
	}
	return NULL;
}
void html_spaceitem::Unmark(int nosub)
{
	Draw(nosub);
}

// -------------------------------------------------------------------

html_newlineitem::html_newlineitem(void) : html_item()
{
	finishline=1;
}
html_newlineitem::~html_newlineitem()
{
}
void html_newlineitem::CalcSize(void) { box->Newline(); height=box->TextHeight(" "); }
char *html_newlineitem::Mark(int x1,int y1,int x2,int y2,int nosub)
{
	return "\n";
}
void html_newlineitem::Unmark(int nosub)
{
}

// -------------------------------------------------------------------

html_nextlineitem::html_nextlineitem(void) : html_item()
{
	finishfullline=1;
}
html_nextlineitem::~html_nextlineitem()
{
}
void html_nextlineitem::CalcSize(void) { box->Newline(); height=box->TextHeight(" "); }
char *html_nextlineitem::Mark(int x1,int y1,int x2,int y2,int nosub)
{
	return "\n";
}
void html_nextlineitem::Unmark(int nosub)
{
}

// -------------------------------------------------------------------

html_newparagraphitem::html_newparagraphitem(void) : html_newlineitem()
{
	size=10;
}
html_newparagraphitem::~html_newparagraphitem()
{
}
void html_newparagraphitem::CalcSize(void) { size=box->TextHeight(" ")+10; if (box->Newline(size)==size) height=size; }
char *html_newparagraphitem::Mark(int x1,int y1,int x2,int y2,int nosub)
{
	return height>0?(char*)"\n\n":(char*)NULL;
}
void html_newparagraphitem::Unmark(int nosub)
{
}

// -------------------------------------------------------------------

html_offsetitem::html_offsetitem(int val) : html_item()
{
	offset=val;
}
html_offsetitem::~html_offsetitem()
{
}
void html_offsetitem::CalcItem(void) { box->XOffset(offset); }

// -------------------------------------------------------------------

html_adjustitem::html_adjustitem(enum ADJUST a) : html_item()
{
	adj=a;
}
html_adjustitem::~html_adjustitem()
{
}
void html_adjustitem::CalcSize(void) { box->Adjust(adj); }

// -------------------------------------------------------------------

html_fontstyleitem::html_fontstyleitem(FONTSTYLE f) : html_item()
{
	fstyle=f;
}
html_fontstyleitem::~html_fontstyleitem()
{
}
void html_fontstyleitem::CalcSize(void) { box->Font(fstyle,box->GetFontsize()); }

// -------------------------------------------------------------------

html_fontsizeitem::html_fontsizeitem(FONTSIZE f) : html_item()
{
	fsize=f;
}
html_fontsizeitem::~html_fontsizeitem()
{
}
void html_fontsizeitem::CalcSize(void) { box->Font(box->GetFontstyle(),fsize); }

// -------------------------------------------------------------------

html_coloritem::html_coloritem(char *c) : html_item()
{
	col=c?strdup(c):(char*)NULL;
}
html_coloritem::~html_coloritem()
{
	free(col);
}
void html_coloritem::CalcSize(int)      { box->Color(col); }

// -------------------------------------------------------------------

html_backcoloritem::html_backcoloritem(char *c) : html_coloritem(c)
{
}
html_backcoloritem::~html_backcoloritem()
{
}
void html_backcoloritem::Draw(int)      { box->BackgroundColor(col); }

// -------------------------------------------------------------------

html_bulletitem::html_bulletitem(int l) : html_item()
{
	level=l;
}
html_bulletitem::~html_bulletitem()
{
}
void html_bulletitem::Draw(int)
{
	static char *cc[]={"*","-","+","#"};
	char buf[20];
	sprintf(buf,"%s",cc[level>3?3:level]);
	box->Text(buf,5+box->TextLength(buf));
}

// -------------------------------------------------------------------

html_countitem::html_countitem(int l) : html_item()
{
	number=l;
}
html_countitem::~html_countitem()
{
}
void html_countitem::Draw(int)
{
	char buf[20];
	sprintf(buf,"%d.",number);
	box->Text(buf,5+box->TextLength(buf));
}

// -------------------------------------------------------------------
