26#define NEWS_MAX_LENGTH 8192
47static double textlength = 0.;
57static void news_render(
double bx,
double by,
double w,
double h,
void *data );
58static void news_focusLose(
unsigned int wid,
const char *wgtname );
59static int news_mouse(
unsigned int wid,
const SDL_Event *event,
double mx,
60 double my,
double w,
double h,
double rx,
double ry,
63int news_saveArticles( xmlTextWriterPtr writer );
65static void clear_newslines(
void );
67static int news_cmp(
const void *p1,
const void *p2 )
80 return n1->
id - n2->
id;
94int news_add(
const char *title,
const char *content,
const char *faction,
95 const char *tag, ntime_t date, ntime_t date_to_rm,
int priority )
103 memset( n, 0,
sizeof(
news_t ) );
105 n->
title = strdup( title );
106 n->
desc = strdup( content );
107 n->
faction = strdup( faction );
109 n->
tag = strdup( tag );
179void news_free(
news_t *n )
187void news_rm(
int id )
207 const char *
const *tags;
209 fname = ( faction >= 0 ) ?
faction_name( faction ) : NULL;
221 tags = ( faction >= 0 ) ?
faction_tags( faction ) : NULL;
228 for (
int j = 0; j <
array_size( tags ); j++) {
229 if (strcasecmp( tags[j], n->
faction ) == 0) {
238 ( ( fname != NULL ) && ( strcasecmp( n->
faction, fname ) == 0 ) )) {
245 free( article_time );
257 _(
"No news is available." ) );
275 unsigned int *widptr;
290 iter.
l_end - iter.l_begin ) );
301 widptr = malloc(
sizeof(
unsigned int ) );
303 window_addCust( wid, x, y, w, h,
"cstNews", 1,
news_render,
news_mouse, NULL,
305 window_custSetDynamic( wid,
"cstNews", 1 );
307 window_custFreeDataFunc( wid,
"cstNews", free );
311void clear_newslines(
void )
343static int news_mouse(
unsigned int wid,
const SDL_Event *event,
double mx,
344 double my,
double w,
double h,
double rx,
double ry,
350 switch (event->type) {
353 if (( mx < 0. ) || ( mx > w ) || ( my < 0. ) || ( my > h ))
356 if (event->wheel.y > 0)
358 else if (event->wheel.y < 0)
362 case SDL_MOUSEBUTTONDOWN:
364 if (( mx < 0. ) || ( mx > w ) || ( my < 0. ) || ( my > h ))
371 case SDL_MOUSEBUTTONUP:
375 case SDL_MOUSEMOTION:
393static void news_render(
double bx,
double by,
double w,
double h,
void *data )
396 unsigned int t, *wid;
404 double dt = (double)( t -
news_tick ) / 1000.;
418 m = (int)ceil( h / (
news_font->h + 5. ) );
432 for (
int i = s; i < p; i++) {
446int news_saveArticles( xmlTextWriterPtr writer )
448 xmlw_startElem( writer,
"news" );
451 const char *ntitle, *ndesc;
457 xmlw_startElem( writer,
"article" );
462 xmlw_attr( writer,
"title",
"%s", ntitle );
463 xmlw_attr( writer,
"desc",
"%s", ndesc );
464 xmlw_attr( writer,
"faction",
"%s", n->
faction );
465 xmlw_attr( writer,
"date",
"%" PRIi64, n->
date );
466 xmlw_attr( writer,
"date_to_rm",
"%" PRIi64, n->
date_to_rm );
467 xmlw_attr( writer,
"id",
"%i", n->
id );
468 xmlw_attr( writer,
"priority",
"%i", n->
priority );
471 xmlw_attr( writer,
"tag",
"%s", n->
tag );
473 xmlw_endElem( writer );
476 xmlw_endElem( writer );
499 node = parent->xmlChildrenNode;
501 if (xml_isNode( node,
"news" ))
503 }
while (xml_nextNode( node ));
520 node = parent->xmlChildrenNode;
522#define NEWS_READ( elem, s ) \
523 xmlr_attr_strd( node, s, elem ); \
524 if (elem == NULL) { \
525 WARN( _( "Event is missing '%s', skipping." ), s ); \
530 char *title, *desc, *faction, *tag, *buff;
532 ntime_t date, date_to_rm;
534 if (!xml_isNode( node,
"article" ))
542 NEWS_READ( title,
"title" );
543 NEWS_READ( desc,
"desc" );
544 NEWS_READ( faction,
"faction" );
546 NEWS_READ( buff,
"date" );
547 date = atoll( buff );
550 NEWS_READ( buff,
"date_to_rm" );
551 date_to_rm = atoll( buff );
554 NEWS_READ( buff,
"id" );
559 xmlr_attr_strd( node,
"priority", buff );
560 priority = ( buff == NULL ) ? 5 : atoi( buff );
565 xmlr_attr_strd( node,
"tag", tag );
570 news_add( title, desc, faction, tag, date, date_to_rm, priority );
577 }
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 *const * faction_tags(int f)
Gets the tags the faction has.
const char * faction_name(int f)
Gets a factions "real" (internal) name.
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.