// swad_HTML.c: tables, divs
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2019 Antonio Caņas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
#define _GNU_SOURCE // For vasprintf
#include // For va_start, va_end
#include // For fprintf, vasprintf
#include // For free
#include "swad_global.h"
#include "swad_HTML.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/******************************* Private types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private vatiables *****************************/
/*****************************************************************************/
static unsigned HTM_TABLE_NestingLevel = 0;
static unsigned HTM_TR_NestingLevel = 0;
static unsigned HTM_TH_NestingLevel = 0;
static unsigned HTM_TD_NestingLevel = 0;
static unsigned HTM_DIV_NestingLevel = 0;
static unsigned HTM_SPAN_NestingLevel = 0;
static unsigned HTM_OL_NestingLevel = 0;
static unsigned HTM_UL_NestingLevel = 0;
static unsigned HTM_LI_NestingLevel = 0;
static unsigned HTM_DL_NestingLevel = 0;
static unsigned HTM_DT_NestingLevel = 0;
static unsigned HTM_DD_NestingLevel = 0;
static unsigned HTM_A_NestingLevel = 0;
static unsigned HTM_SCRIPT_NestingLevel = 0;
static unsigned HTM_LABEL_NestingLevel = 0;
static unsigned HTM_BUTTON_NestingLevel = 0;
static unsigned HTM_TEXTAREA_NestingLevel = 0;
static unsigned HTM_SELECT_NestingLevel = 0;
static unsigned HTM_OPTGROUP_NestingLevel = 0;
static unsigned HTM_STRONG_NestingLevel = 0;
static unsigned HTM_EM_NestingLevel = 0;
static unsigned HTM_U_NestingLevel = 0;
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
static void HTM_TABLE_BeginWithoutAttr (void);
static void HTM_TR_BeginWithoutAttr (void);
static void HTM_TH_BeginWithoutAttr (void);
static void HTM_TH_BeginAttr (const char *fmt,...);
static void HTM_TD_BeginWithoutAttr (void);
static void HTM_DIV_BeginWithoutAttr (void);
static void HTM_SPAN_BeginWithoutAttr (void);
static void HTM_UL_BeginWithoutAttr (void);
static void HTM_LI_BeginWithoutAttr (void);
static void HTM_A_BeginWithoutAttr (void);
static void HTM_LABEL_BeginWithoutAttr (void);
static void HTM_TEXTAREA_BeginWithoutAttr (void);
static void HTM_SELECT_BeginWithoutAttr (void);
/*****************************************************************************/
/******************************* Start/end table *****************************/
/*****************************************************************************/
void HTM_TABLE_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Class;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Class,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"
",
CellPadding); // CellPadding must be 0, 1, 2, 5 or 10
HTM_TABLE_NestingLevel++;
}
else
HTM_TABLE_BeginWideMargin ();
}
void HTM_TABLE_BeginWideMargin (void)
{
fprintf (Gbl.F.Out,"
");
HTM_TABLE_NestingLevel++;
}
void HTM_TABLE_End (void)
{
if (HTM_TABLE_NestingLevel == 0) // No TABLE open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TABLE.");
fprintf (Gbl.F.Out,"
");
HTM_TABLE_NestingLevel--;
}
/*****************************************************************************/
/**************************** Start/end table row ****************************/
/*****************************************************************************/
void HTM_TR_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"
");
}
void HTM_TR_End (void)
{
if (HTM_TR_NestingLevel == 0) // No TR open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TR.");
fprintf (Gbl.F.Out,"
");
HTM_TR_NestingLevel--;
}
/*****************************************************************************/
/***************************** Table heading cells ***************************/
/*****************************************************************************/
void HTM_TH (unsigned RowSpan,unsigned ColSpan,const char *Class,const char *Txt)
{
HTM_TH_Begin (RowSpan,ColSpan,Class);
if (Txt)
if (Txt[0])
HTM_Txt (Txt);
HTM_TH_End ();
}
void HTM_TH_Begin (unsigned RowSpan,unsigned ColSpan,const char *Class)
{
if (RowSpan > 1 && ColSpan > 1)
{
if (Class)
HTM_TH_BeginAttr ("rowspan=\"%u\" colspan=\"%u\" class=\"%s\"",
RowSpan,ColSpan,Class);
else
HTM_TH_BeginAttr ("rowspan=\"%u\" colspan=\"%u\"",
RowSpan,ColSpan);
}
else if (RowSpan > 1)
{
if (Class)
HTM_TH_BeginAttr ("rowspan=\"%u\" class=\"%s\"",
RowSpan,Class);
else
HTM_TH_BeginAttr ("rowspan=\"%u\"",
RowSpan);
}
else if (ColSpan > 1)
{
if (Class)
HTM_TH_BeginAttr ("colspan=\"%u\" class=\"%s\"",
ColSpan,Class);
else
HTM_TH_BeginAttr ("colspan=\"%u\"",
ColSpan);
}
else
{
if (Class)
HTM_TH_BeginAttr ("class=\"%s\"",
Class);
else
HTM_TH_BeginWithoutAttr ();
}
}
static void HTM_TH_BeginAttr (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"
");
}
void HTM_TH_End (void)
{
if (HTM_TH_NestingLevel == 0) // No TH open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TR.");
fprintf (Gbl.F.Out,"
");
HTM_TH_NestingLevel--;
}
void HTM_TH_Empty (unsigned NumColumns)
{
unsigned NumCol;
for (NumCol = 0;
NumCol < NumColumns;
NumCol++)
{
HTM_TH_BeginAttr (NULL);
HTM_TH_End ();
}
}
/*****************************************************************************/
/********************************* Table cells *******************************/
/*****************************************************************************/
void HTM_TD_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"
");
}
void HTM_TD_End (void)
{
if (HTM_TD_NestingLevel == 0) // No TD open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TD.");
fprintf (Gbl.F.Out,"
");
HTM_TD_NestingLevel--;
}
void HTM_TD_Empty (unsigned NumColumns)
{
unsigned NumCol;
for (NumCol = 0;
NumCol < NumColumns;
NumCol++)
{
HTM_TD_Begin (NULL);
HTM_TD_End ();
}
}
void HTM_TD_ColouredEmpty (unsigned NumColumns)
{
unsigned NumCol;
for (NumCol = 0;
NumCol < NumColumns;
NumCol++)
{
HTM_TD_Begin ("class=\"COLOR%u\"",Gbl.RowEvenOdd);
HTM_TD_End ();
}
}
/*****************************************************************************/
/************************************ Divs ***********************************/
/*****************************************************************************/
void HTM_DIV_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"
");
}
void HTM_DIV_End (void)
{
if (HTM_DIV_NestingLevel == 0) // No DIV open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened DIV.");
fprintf (Gbl.F.Out,"
");
HTM_DIV_NestingLevel--;
}
/*****************************************************************************/
/******************************** Main zone **********************************/
/*****************************************************************************/
void HTM_MAIN_Begin (const char *Class)
{
fprintf (Gbl.F.Out,"",Class);
}
void HTM_MAIN_End (void)
{
fprintf (Gbl.F.Out,"");
}
/*****************************************************************************/
/********************************* Articles **********************************/
/*****************************************************************************/
void HTM_ARTICLE_Begin (const char *ArticleId)
{
fprintf (Gbl.F.Out,"",ArticleId);
}
void HTM_ARTICLE_End (void)
{
fprintf (Gbl.F.Out,"");
}
/*****************************************************************************/
/********************************* Sections **********************************/
/*****************************************************************************/
void HTM_SECTION_Begin (const char *SectionId)
{
fprintf (Gbl.F.Out,"",SectionId);
}
void HTM_SECTION_End (void)
{
fprintf (Gbl.F.Out,"");
}
/*****************************************************************************/
/*********************************** Spans ***********************************/
/*****************************************************************************/
void HTM_SPAN_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"",Attr);
free (Attr);
}
else
HTM_SPAN_BeginWithoutAttr ();
}
else
HTM_SPAN_BeginWithoutAttr ();
HTM_SPAN_NestingLevel++;
}
static void HTM_SPAN_BeginWithoutAttr (void)
{
fprintf (Gbl.F.Out,"");
}
void HTM_SPAN_End (void)
{
if (HTM_SPAN_NestingLevel == 0) // No SPAN open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened SPAN.");
fprintf (Gbl.F.Out,"");
HTM_SPAN_NestingLevel--;
}
/*****************************************************************************/
/*********************************** Lists ***********************************/
/*****************************************************************************/
void HTM_OL_Begin (void)
{
fprintf (Gbl.F.Out,"");
HTM_OL_NestingLevel++;
}
void HTM_OL_End (void)
{
if (HTM_OL_NestingLevel == 0) // No OL open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened OL.");
fprintf (Gbl.F.Out,"");
HTM_OL_NestingLevel--;
}
void HTM_UL_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"
");
}
void HTM_UL_End (void)
{
if (HTM_UL_NestingLevel == 0) // No UL open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened UL.");
fprintf (Gbl.F.Out,"
");
HTM_UL_NestingLevel--;
}
/*****************************************************************************/
/******************************** List items *********************************/
/*****************************************************************************/
void HTM_LI_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"
");
}
void HTM_LI_End (void)
{
if (HTM_LI_NestingLevel == 0) // No LI open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened LI.");
fprintf (Gbl.F.Out,"
");
HTM_DL_NestingLevel++;
}
void HTM_DL_End (void)
{
if (HTM_DL_NestingLevel == 0) // No DL open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened DL.");
fprintf (Gbl.F.Out,"
");
HTM_DT_NestingLevel++;
}
void HTM_DT_End (void)
{
if (HTM_DL_NestingLevel == 0) // No DT open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened DT.");
fprintf (Gbl.F.Out,"
");
HTM_DD_NestingLevel++;
}
void HTM_DD_End (void)
{
if (HTM_DD_NestingLevel == 0) // No DD open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened DD.");
fprintf (Gbl.F.Out,"
");
HTM_DD_NestingLevel--;
}
/*****************************************************************************/
/********************************** Anchors **********************************/
/*****************************************************************************/
void HTM_A_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"",Attr);
free (Attr);
}
else
HTM_A_BeginWithoutAttr ();
}
else
HTM_A_BeginWithoutAttr ();
HTM_A_NestingLevel++;
}
static void HTM_A_BeginWithoutAttr (void)
{
fprintf (Gbl.F.Out,"");
}
void HTM_A_End (void)
{
if (HTM_A_NestingLevel == 0) // No A open
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened A.");
fprintf (Gbl.F.Out,"");
HTM_A_NestingLevel--;
}
/*****************************************************************************/
/*********************************** Scripts *********************************/
/*****************************************************************************/
void HTM_SCRIPT_Begin (const char *URL,const char *CharSet)
{
fprintf (Gbl.F.Out,"\n");
HTM_SCRIPT_NestingLevel--;
}
/*****************************************************************************/
/********************************* Parameters ********************************/
/*****************************************************************************/
void HTM_PARAM (const char *Name,
const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Value;
if (fmt)
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Value,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"\n",Name,Value);
free (Value);
}
}
/*****************************************************************************/
/*********************************** Labels **********************************/
/*****************************************************************************/
void HTM_LABEL_Begin (const char *fmt,...)
{
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
{
if (fmt[0])
{
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // -1 if no memory or any other error
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"