swad-core/swad_HTML.c

454 lines
11 KiB
C
Raw Normal View History

2019-10-23 19:05:05 +02:00
// swad_HTML.c: tables, divs
2017-06-11 20:09:59 +02:00
/*
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.
2019-01-07 21:52:19 +01:00
Copyright (C) 1999-2019 Antonio Ca<EFBFBD>as Vargas
2017-06-11 20:09:59 +02:00
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 <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
2019-10-04 01:35:40 +02:00
#define _GNU_SOURCE // For vasprintf
#include <stdarg.h> // For va_start, va_end
#include <stdio.h> // For fprintf, vasprintf
2019-10-04 15:19:36 +02:00
#include <stdlib.h> // For free
2017-06-11 20:09:59 +02:00
#include "swad_global.h"
2019-10-23 19:05:05 +02:00
#include "swad_HTML.h"
2017-06-11 20:09:59 +02:00
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/******************************* Private types *******************************/
/*****************************************************************************/
2019-10-13 18:19:26 +02:00
/*****************************************************************************/
/***************************** Private vatiables *****************************/
/*****************************************************************************/
2019-10-23 19:05:05 +02:00
static unsigned HTM_TABLE_NestingLevel = 0;
static unsigned HTM_TR_NestingLevel = 0;
static unsigned HTM_TH_NestingLevel = 0;
static unsigned HTM_TD_NestingLevel = 0;
2019-10-13 18:19:26 +02:00
2017-06-11 20:09:59 +02:00
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2019-10-23 19:05:05 +02:00
static void HTM_TABLE_BeginWithoutAttr (void);
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
static void HTM_TR_BeginWithoutAttr (void);
2019-10-12 19:10:32 +02:00
2019-10-23 19:05:05 +02:00
static void HTM_TH_BeginWithoutAttr (void);
static void HTM_TH_BeginAttr (const char *fmt,...);
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
static void HTM_TD_BeginWithoutAttr (void);
2019-10-10 23:14:13 +02:00
2017-06-11 20:09:59 +02:00
/*****************************************************************************/
/******************************* Start/end table *****************************/
/*****************************************************************************/
2019-10-23 19:05:05 +02:00
void HTM_TABLE_Begin (const char *fmt,...)
2019-10-04 01:35:40 +02:00
{
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) // If memory allocation wasn't possible,
// or some other error occurs,
// vasprintf will return -1
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<table class=\"%s\">",Class);
2019-10-04 01:35:40 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel++;
2019-10-13 22:13:17 +02:00
2019-10-04 01:35:40 +02:00
free ((void *) Class);
}
else
2019-10-23 19:05:05 +02:00
HTM_TABLE_BeginWithoutAttr ();
2019-10-04 01:35:40 +02:00
}
else
2019-10-23 19:05:05 +02:00
HTM_TABLE_BeginWithoutAttr ();
2019-10-04 01:35:40 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TABLE_BeginPadding (unsigned CellPadding)
2017-06-11 20:09:59 +02:00
{
if (CellPadding)
2019-10-13 22:13:17 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<table class=\"CELLS_PAD_%u\">",
2019-10-03 22:43:36 +02:00
CellPadding); // CellPadding must be 0, 1, 2, 5 or 10
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel++;
2019-10-13 22:13:17 +02:00
}
2019-10-03 22:43:36 +02:00
else
2019-10-23 19:05:05 +02:00
HTM_TABLE_BeginWithoutAttr ();
2017-06-11 20:09:59 +02:00
}
2019-10-23 19:05:05 +02:00
static void HTM_TABLE_BeginWithoutAttr (void)
2019-10-03 22:43:36 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<table>");
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel++;
2019-10-03 22:43:36 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TABLE_BeginCenterPadding (unsigned CellPadding)
2017-06-11 20:09:59 +02:00
{
if (CellPadding)
2019-10-13 22:13:17 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<table class=\"FRAME_TBL_CENTER CELLS_PAD_%u\">",
2019-10-03 22:43:36 +02:00
CellPadding); // CellPadding must be 0, 1, 2, 5 or 10
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel++;
2019-10-13 22:13:17 +02:00
}
2019-10-03 22:43:36 +02:00
else
2019-10-23 19:05:05 +02:00
HTM_TABLE_BeginCenter ();
2019-10-03 22:43:36 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TABLE_BeginCenter (void)
2019-10-03 22:43:36 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<table class=\"FRAME_TBL_CENTER\">");
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel++;
2017-06-11 20:09:59 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TABLE_BeginWidePadding (unsigned CellPadding)
2017-06-11 20:09:59 +02:00
{
if (CellPadding)
2019-10-13 22:13:17 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<table class=\"FRAME_TBL_WIDE CELLS_PAD_%u\">",
2019-10-03 22:43:36 +02:00
CellPadding); // CellPadding must be 0, 1, 2, 5 or 10
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel++;
2019-10-13 22:13:17 +02:00
}
2019-10-03 22:43:36 +02:00
else
2019-10-23 19:05:05 +02:00
HTM_TABLE_BeginWide ();
2017-06-11 20:09:59 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TABLE_BeginWide (void)
2019-10-03 22:43:36 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<table class=\"FRAME_TBL_WIDE\">");
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel++;
2019-10-03 22:43:36 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TABLE_BeginWideMarginPadding (unsigned CellPadding)
2017-06-11 20:09:59 +02:00
{
if (CellPadding)
2019-10-13 22:13:17 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<table class=\"FRAME_TBL_WIDE_MARGIN CELLS_PAD_%u\">",
2019-10-03 22:43:36 +02:00
CellPadding); // CellPadding must be 0, 1, 2, 5 or 10
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel++;
2019-10-13 22:13:17 +02:00
}
2019-10-03 22:43:36 +02:00
else
2019-10-23 19:05:05 +02:00
HTM_TABLE_BeginWideMargin ();
2019-10-03 22:43:36 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TABLE_BeginWideMargin (void)
2019-10-03 22:43:36 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<table class=\"FRAME_TBL_WIDE_MARGIN\">");
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel++;
2017-06-11 20:09:59 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TABLE_End (void)
2017-06-11 20:09:59 +02:00
{
2019-10-23 19:05:05 +02:00
if (HTM_TABLE_NestingLevel == 0) // No TABLE open
2019-10-13 22:13:17 +02:00
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TABLE.");
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"</table>");
2019-10-13 22:13:17 +02:00
2019-10-23 19:05:05 +02:00
HTM_TABLE_NestingLevel--;
2017-06-11 20:09:59 +02:00
}
2019-09-23 09:44:10 +02:00
2019-10-05 22:15:52 +02:00
/*****************************************************************************/
/**************************** Start/end table row ****************************/
/*****************************************************************************/
2019-10-23 19:05:05 +02:00
void HTM_TR_Begin (const char *fmt,...)
2019-10-05 13:27:58 +02:00
{
2019-10-05 22:15:52 +02:00
va_list ap;
int NumBytesPrinted;
char *Attr;
if (fmt)
2019-10-05 13:27:58 +02:00
{
2019-10-05 22:15:52 +02:00
if (fmt[0])
2019-10-05 13:27:58 +02:00
{
2019-10-05 22:15:52 +02:00
va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Attr,fmt,ap);
va_end (ap);
if (NumBytesPrinted < 0) // If memory allocation wasn't possible,
// or some other error occurs,
// vasprintf will return -1
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<tr %s>",Attr);
2019-10-05 22:15:52 +02:00
free ((void *) Attr);
2019-10-05 13:27:58 +02:00
}
else
2019-10-23 19:05:05 +02:00
HTM_TR_BeginWithoutAttr ();
2019-10-05 13:27:58 +02:00
}
else
2019-10-23 19:05:05 +02:00
HTM_TR_BeginWithoutAttr ();
2019-10-13 18:19:26 +02:00
2019-10-23 19:05:05 +02:00
HTM_TR_NestingLevel++;
2019-10-05 13:27:58 +02:00
}
2019-10-23 19:05:05 +02:00
static void HTM_TR_BeginWithoutAttr (void)
2019-10-04 01:35:40 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<tr>");
2019-10-04 01:35:40 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TR_End (void)
2019-10-04 01:35:40 +02:00
{
2019-10-23 19:05:05 +02:00
if (HTM_TR_NestingLevel == 0) // No TR open
2019-10-13 22:13:17 +02:00
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TR.");
2019-10-13 18:19:26 +02:00
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"</tr>");
2019-10-13 18:19:26 +02:00
2019-10-23 19:05:05 +02:00
HTM_TR_NestingLevel--;
2019-10-04 01:35:40 +02:00
}
2019-10-11 01:02:51 +02:00
/*****************************************************************************/
/***************************** Table heading cells ***************************/
/*****************************************************************************/
2019-10-23 19:05:05 +02:00
void HTM_TH (unsigned RowSpan,unsigned ColSpan,const char *Class,const char *Txt)
2019-10-12 19:42:10 +02:00
{
2019-10-23 19:05:05 +02:00
HTM_TH_Begin (RowSpan,ColSpan,Class);
2019-10-12 19:42:10 +02:00
if (Txt)
if (Txt[0])
fprintf (Gbl.F.Out,"%s",Txt);
2019-10-23 19:05:05 +02:00
HTM_TH_End ();
2019-10-12 19:42:10 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TH_Begin (unsigned RowSpan,unsigned ColSpan,const char *Class)
2019-10-12 19:10:32 +02:00
{
if (RowSpan > 1 && ColSpan > 1)
{
if (Class)
2019-10-23 19:05:05 +02:00
HTM_TH_BeginAttr ("rowspan=\"%u\" colspan=\"%u\" class=\"%s\"",
2019-10-12 19:10:32 +02:00
RowSpan,ColSpan,Class);
else
2019-10-23 19:05:05 +02:00
HTM_TH_BeginAttr ("rowspan=\"%u\" colspan=\"%u\"",
2019-10-12 19:10:32 +02:00
RowSpan,ColSpan);
}
else if (RowSpan > 1)
{
if (Class)
2019-10-23 19:05:05 +02:00
HTM_TH_BeginAttr ("rowspan=\"%u\" class=\"%s\"",
2019-10-12 19:10:32 +02:00
RowSpan,Class);
else
2019-10-23 19:05:05 +02:00
HTM_TH_BeginAttr ("rowspan=\"%u\"",
2019-10-12 19:10:32 +02:00
RowSpan);
}
else if (ColSpan > 1)
{
if (Class)
2019-10-23 19:05:05 +02:00
HTM_TH_BeginAttr ("colspan=\"%u\" class=\"%s\"",
2019-10-12 19:10:32 +02:00
ColSpan,Class);
else
2019-10-23 19:05:05 +02:00
HTM_TH_BeginAttr ("colspan=\"%u\"",
2019-10-12 19:10:32 +02:00
ColSpan);
}
else
{
if (Class)
2019-10-23 19:05:05 +02:00
HTM_TH_BeginAttr ("class=\"%s\"",
2019-10-12 19:10:32 +02:00
Class);
else
2019-10-23 19:05:05 +02:00
HTM_TH_BeginWithoutAttr ();
2019-10-12 19:10:32 +02:00
}
}
2019-10-23 19:05:05 +02:00
static void HTM_TH_BeginAttr (const char *fmt,...)
2019-10-11 01:02:51 +02:00
{
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) // If memory allocation wasn't possible,
// or some other error occurs,
// vasprintf will return -1
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
fprintf (Gbl.F.Out,"<th %s>",Attr);
free ((void *) Attr);
}
else
2019-10-23 19:05:05 +02:00
HTM_TH_BeginWithoutAttr ();
2019-10-11 01:02:51 +02:00
}
else
2019-10-23 19:05:05 +02:00
HTM_TH_BeginWithoutAttr ();
2019-10-13 18:19:26 +02:00
2019-10-23 19:05:05 +02:00
HTM_TH_NestingLevel++;
2019-10-11 01:02:51 +02:00
}
2019-10-23 19:05:05 +02:00
static void HTM_TH_BeginWithoutAttr (void)
2019-10-11 01:02:51 +02:00
{
fprintf (Gbl.F.Out,"<th>");
}
2019-10-23 19:05:05 +02:00
void HTM_TH_End (void)
2019-10-11 01:02:51 +02:00
{
2019-10-23 19:05:05 +02:00
if (HTM_TH_NestingLevel == 0) // No TH open
2019-10-13 22:13:17 +02:00
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TR.");
2019-10-13 18:19:26 +02:00
2019-10-11 01:02:51 +02:00
fprintf (Gbl.F.Out,"</th>");
2019-10-13 18:19:26 +02:00
2019-10-23 19:05:05 +02:00
HTM_TH_NestingLevel--;
2019-10-11 01:02:51 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TH_Empty (unsigned NumColumns)
2019-10-11 01:02:51 +02:00
{
unsigned NumCol;
for (NumCol = 0;
NumCol < NumColumns;
NumCol++)
{
2019-10-23 19:05:05 +02:00
HTM_TH_BeginAttr (NULL);
HTM_TH_End ();
2019-10-11 01:02:51 +02:00
}
}
2019-10-05 22:15:52 +02:00
/*****************************************************************************/
/********************************* Table cells *******************************/
/*****************************************************************************/
2019-10-23 19:05:05 +02:00
void HTM_TD_Begin (const char *fmt,...)
2019-10-07 22:28:16 +02:00
{
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) // If memory allocation wasn't possible,
// or some other error occurs,
// vasprintf will return -1
Lay_NotEnoughMemoryExit ();
/***** Print HTML *****/
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<td %s>",Attr);
2019-10-07 22:28:16 +02:00
free ((void *) Attr);
}
else
2019-10-23 19:05:05 +02:00
HTM_TD_BeginWithoutAttr ();
2019-10-07 22:28:16 +02:00
}
else
2019-10-23 19:05:05 +02:00
HTM_TD_BeginWithoutAttr ();
2019-10-13 18:19:26 +02:00
2019-10-23 19:05:05 +02:00
HTM_TD_NestingLevel++;
2019-10-07 22:28:16 +02:00
}
2019-10-23 19:05:05 +02:00
static void HTM_TD_BeginWithoutAttr (void)
2019-10-07 22:28:16 +02:00
{
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"<td>");
2019-10-07 22:28:16 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TD_End (void)
2019-10-06 12:00:55 +02:00
{
2019-10-23 19:05:05 +02:00
if (HTM_TD_NestingLevel == 0) // No TH open
2019-10-17 15:40:21 +02:00
Ale_ShowAlert (Ale_ERROR,"Trying to close unopened TD.");
2019-10-13 18:19:26 +02:00
2019-10-10 21:21:24 +02:00
fprintf (Gbl.F.Out,"</td>");
2019-10-13 18:19:26 +02:00
2019-10-23 19:05:05 +02:00
HTM_TD_NestingLevel--;
2019-10-06 12:00:55 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TD_Empty (unsigned NumColumns)
2019-10-05 13:27:58 +02:00
{
unsigned NumCol;
for (NumCol = 0;
NumCol < NumColumns;
NumCol++)
2019-10-10 23:14:13 +02:00
{
2019-10-23 19:05:05 +02:00
HTM_TD_Begin (NULL);
HTM_TD_End ();
2019-10-10 23:14:13 +02:00
}
2019-10-05 13:27:58 +02:00
}
2019-10-23 19:05:05 +02:00
void HTM_TD_ColouredEmpty (unsigned NumColumns)
2019-09-23 09:44:10 +02:00
{
unsigned NumCol;
for (NumCol = 0;
NumCol < NumColumns;
NumCol++)
2019-10-10 23:14:13 +02:00
{
2019-10-23 19:05:05 +02:00
HTM_TD_Begin ("class=\"COLOR%u\"",Gbl.RowEvenOdd);
HTM_TD_End ();
2019-10-10 23:14:13 +02:00
}
2019-09-23 09:44:10 +02:00
}
2019-10-23 20:07:56 +02:00
/*****************************************************************************/
/************************************ Divs ***********************************/
/*****************************************************************************/
void HTM_DIV_End (void)
{
fprintf (Gbl.F.Out,"</div>");
}