23#include "nlua_faction.h"
34#define NEWS_MAX_LENGTH 8192
54static double textlength = 0.;
64static void news_render(
double bx,
double by,
double w,
double h,
void *data );
65static void news_focusLose(
unsigned int wid,
const char* wgtname );
66static int news_mouse(
unsigned int wid,
const SDL_Event *event,
double mx,
double my,
67 double w,
double h,
double rx,
double ry,
void *data );
69int news_saveArticles( xmlTextWriterPtr writer );
71static void clear_newslines (
void);
73static int news_cmp(
const void *p1,
const void *p2 )
86 return n1->
id - n2->
id;
100int news_add(
const char *title,
const char *content,
101 const char *faction,
const char *tag,
102 ntime_t date, ntime_t date_to_rm,
int priority )
110 memset( n, 0,
sizeof(
news_t) );
112 n->title = strdup( title );
113 n->desc = strdup( content );
114 n->faction = strdup( faction );
116 n->tag = strdup( tag );
118 n->date_to_rm = date_to_rm;
119 n->priority = priority;
186void news_free(
news_t *n )
194void news_rm(
int id )
216 fname = (faction >= 0) ?
faction_name( faction ) : NULL;
223 if (n->date_to_rm <= curtime)
236 if (strcasecmp( tags[j], n->faction)==0) {
244 if (match_tag || ((fname != NULL) && (strcasecmp(n->faction, fname) == 0))) {
249 "%s: %s#0\n\n", n->title, article_time, n->desc );
250 free( article_time );
255 "%s#0\n\n", n->title, n->desc );
279 unsigned int *widptr;
304 widptr = malloc(
sizeof(
unsigned int));
306 window_addCust( wid, x, y, w, h,
"cstNews", 1,
news_render,
news_mouse, NULL,
news_focusLose, widptr );
307 window_custSetDynamic( wid,
"cstNews", 1 );
309 window_custAutoFreeData( wid,
"cstNews" );
313void clear_newslines (
void)
345static int news_mouse(
unsigned int wid,
const SDL_Event *event,
double mx,
double my,
346 double w,
double h,
double rx,
double ry,
void *data )
351 switch (event->type) {
354 if ((mx < 0.) || (mx > w) || (my < 0.) || (my > h))
357 if (event->wheel.y > 0)
359 else if (event->wheel.y < 0)
363 case SDL_MOUSEBUTTONDOWN:
365 if ((mx < 0.) || (mx > w) || (my < 0.) || (my > h))
372 case SDL_MOUSEBUTTONUP:
376 case SDL_MOUSEMOTION:
394static void news_render(
double bx,
double by,
double w,
double h,
void *data )
397 unsigned int t, *wid;
433 for (
int i=s; i<p; i++) {
436 bx+10, by+y, &cFontGreen, -1.,
news_lines[i] );
447int news_saveArticles( xmlTextWriterPtr writer )
449 xmlw_startElem(writer,
"news");
452 const char *ntitle, *ndesc;
455 if (n->title == NULL || n->desc==NULL || n->faction == NULL)
458 xmlw_startElem(writer,
"article");
463 xmlw_attr(writer,
"title",
"%s", ntitle);
464 xmlw_attr(writer,
"desc",
"%s", ndesc);
465 xmlw_attr(writer,
"faction",
"%s", n->faction);
466 xmlw_attr(writer,
"date",
"%"PRIi64, n->date);
467 xmlw_attr(writer,
"date_to_rm",
"%"PRIi64, n->date_to_rm);
468 xmlw_attr(writer,
"id",
"%i", n->id);
469 xmlw_attr(writer,
"priority",
"%i", n->priority);
472 xmlw_attr(writer,
"tag",
"%s", n->tag);
474 xmlw_endElem(writer);
477 xmlw_endElem(writer);
500 node = parent->xmlChildrenNode;
502 if (xml_isNode(node,
"news"))
504 }
while (xml_nextNode(node));
521 node = parent->xmlChildrenNode;
523#define NEWS_READ(elem, s) \
524xmlr_attr_strd(node, s, elem); \
525if (elem == NULL) { WARN(_("Event is missing '%s', skipping."), s); goto cleanup; }
528 char *title, *desc, *faction, *tag, *buff;
530 ntime_t date, date_to_rm;
532 if (!xml_isNode(node,
"article"))
540 NEWS_READ(title,
"title");
541 NEWS_READ(desc,
"desc");
542 NEWS_READ(faction,
"faction");
544 NEWS_READ(buff,
"date");
548 NEWS_READ(buff,
"date_to_rm");
549 date_to_rm = atoll(buff);
552 NEWS_READ(buff,
"id");
557 xmlr_attr_strd( node,
"priority", buff );
558 priority = (buff==NULL) ? 5 : atoi(buff);
563 xmlr_attr_strd( node,
"tag", tag );
568 news_add( title, desc, faction, tag, date, date_to_rm, priority );
575 }
while (xml_nextNode(node));
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
#define array_resize(ptr_array, new_size)
Resizes the array to accomodate new_size elements.
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
#define array_back(ptr_array)
Returns the last element in the array.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
const char * faction_name(int f)
Gets a factions "real" (internal) name.
const char ** faction_tags(int f)
Gets the tags the faction has.
void gl_printRestoreInit(glFontRestore *restore)
Initializes a restore structure.
int gl_printLineIteratorNext(glPrintLineIterator *iter)
Updates iter with the next line's information.
int gl_printMidRaw(const glFont *ft_font, int width, double x, double y, const glColour *c, double outlineR, const char *text)
Displays text centered in position and width.
void gl_printRestore(const glFontRestore *restore)
Restores last colour from a restore structure.
void gl_printLineIteratorInit(glPrintLineIterator *iter, const glFont *ft_font, const char *text, int width)
Initialize an iterator object for breaking text into lines.
void gl_printStore(glFontRestore *restore, const char *text)
Stores the colour information from a piece of text.
Header file with generic functions and naev-specifics.
static glFont * news_font
static void news_focusLose(unsigned int wid, const char *wgtname)
Called when it's de-focused.
static char ** news_lines
int news_loadArticles(xmlNodePtr parent)
Loads the player's active articles from a save, initilizes news.
int news_add(const char *title, const char *content, const char *faction, const char *tag, ntime_t date, ntime_t date_to_rm, int priority)
makes a new article and puts it into the list
news_t * news_get(int id)
gets the article with id ID, else NULL
static void news_render(double bx, double by, double w, double h, void *data)
Renders a news widget.
static glFontRestore * news_restores
static unsigned int news_tick
void news_exit(void)
Kills the old news thread.
int * generate_news(int faction)
Generates news from newslist from specific faction AND Generic news.
static int news_mouse(unsigned int wid, const SDL_Event *event, double mx, double my, double w, double h, double rx, double ry, void *data)
News widget mouse event handler.
void news_widget(unsigned int wid, int x, int y, int w, int h)
Creates a news widget.
int news_init(void)
Initiate news linked list with a stack.
static int news_parseArticle(xmlNodePtr parent)
Parses articles.
int scnprintf(char *text, size_t maxlen, const char *fmt,...)
Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer....
char * strndup(const char *s, size_t n)
Return a pointer to a new string, which is a duplicate of the string s (or, if necessary,...
ntime_t ntime_get(void)
Gets the current time.
char * ntime_pretty(ntime_t t, int d)
Gets the time in a pretty human readable format.
void gl_renderRect(double x, double y, double w, double h, const glColour *c)
Renders a rectangle.
Evil hack to allow restoring, yes it makes me cry myself to sleep.
Represents a font in memory.
The state of a line iteration. This matches the process of rendering text into an on-screen box: An e...
Represents a news article.