naev 0.12.5
dialogue.c File Reference

Is a high-level API around toolkit.c for easy window creation. More...

#include "dialogue.h"
#include "conf.h"
#include "input.h"
#include "log.h"
#include "menu.h"
#include "ndata.h"
#include "opengl.h"
#include "toolkit.h"
Include dependency graph for dialogue.c:

Go to the source code of this file.

Data Structures

struct  dialogue_update_t
struct  dialogue_custom_data_s
struct  InputDialogue
 Used to store information for input dialogues. More...

Functions

static int dialogue_custom_event (unsigned int wid, SDL_Event *event)
void main_loop (int nested)
 Split main loop from main() for secondary loop hack in toolkit.c.
static void dialogue_close (unsigned int wid, const char *str)
 Generic window close.
static void dialogue_cancel (unsigned int wid, const char *str)
 Generic window cancel.
static glFontdialogue_getSize (const char *title, const char *msg, int *width, int *height)
 Gets the size needed for the dialogue.
static void dialogue_YesNoClose (unsigned int wid, const char *str)
 Closes a yesno dialogue.
static void dialogue_inputClose (unsigned int wid, const char *str)
 Closes an input dialogue.
static void dialogue_choiceClose (unsigned int wid, const char *str)
 Closes a choice dialogue.
static void dialogue_listClose (unsigned int wid, const char *str)
static void dialogue_listCancel (unsigned int wid, const char *str)
static int toolkit_loop (int *loop_done, dialogue_update_t *du)
 Creates a secondary loop until loop_done is set to 1 or the toolkit closes.
static void select_call_wrapper (unsigned int wid, const char *wgtname)
 used to pass appropriate information to the method that handles updating the extra information area in the dialogue listpanel.
int dialogue_isOpen (void)
 Checks to see if a dialogue is open.
void dialogue_alert (const char *fmt,...)
 Displays an alert popup with only an ok button and a message.
void dialogue_alertRaw (const char *msg)
 Displays an alert popup with only an ok button and a message.
void dialogue_msg (const char *caption, const char *fmt,...)
 Opens a dialogue window with an ok button and a message.
void dialogue_msgImg (const char *caption, const char *img, const char *fmt,...)
 Opens a dialogue window with an ok button, a message and an image.
void dialogue_msgRaw (const char *caption, const char *msg)
 Opens a dialogue window with an ok button and a fixed message.
void dialogue_msgImgRaw (const char *caption, const char *msg, const char *img, int width, int height)
 Opens a dialogue window with an ok button, a fixed message and an image.
int dialogue_YesNo (const char *caption, const char *fmt,...)
 Runs a dialogue with both yes and no options.
int dialogue_YesNoRaw (const char *caption, const char *msg)
 Runs a dialogue with both yes and no options.
char * dialogue_input (const char *title, int min, int max, const char *fmt,...)
 Creates a dialogue that allows the player to write a message.
char * dialogue_inputRaw (const char *title, int min, int max, const char *msg)
 Creates a dialogue that allows the player to write a message.
int dialogue_list (const char *title, char **items, int nitems, const char *fmt,...)
 Creates a list dialogue with OK and Cancel button with a fixed message.
int dialogue_listRaw (const char *title, char **items, int nitems, const char *msg)
 Creates a list dialogue with OK and Cancel button.
int dialogue_listPanel (const char *title, char **items, int nitems, int extrawidth, int minheight, void(*add_widgets)(unsigned int wid, int x, int y, int w, int h), void(*select_call)(unsigned int wid, const char *wgtname, int x, int y, int w, int h), const char *fmt,...)
 Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra area for the list to react to item selected events.
int dialogue_listPanelRaw (const char *title, char **items, int nitems, int extrawidth, int minheight, void(*add_widgets)(unsigned int wid, int x, int y, int w, int h), void(*select_call)(unsigned int wid, const char *wgtname, int x, int y, int w, int h), const char *msg)
 Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra area for the list to react to item selected events.
void dialogue_makeChoice (const char *caption, const char *msg, int opts)
 Create the choice dialog. Need to add choices with below method.
void dialogue_addChoice (const char *caption, const char *msg, const char *opt)
 Add a choice to the dialog.
char * dialogue_runChoice (void)
 Run the dialog and return the clicked string.
void dialogue_custom (const char *wdwname, const char *caption, int width, int height, int(*update)(double dt, void *data), void(*render)(double x, double y, double w, double h, void *data), int(*event)(unsigned int wid, SDL_Event *event, void *data), void *data, int dynamic, void(*freefunc)(void *data))
 Opens a custom dialogue window.
int dialogue_customFullscreen (unsigned int wid, int enable)
 Converts a custom dialogue to fullscreen.
int dialogue_customResize (unsigned int wid, int width, int height)
 Resizes a custom dialogue.

Variables

static int dialogue_open
static int dlgid = 0
static InputDialogue input_dialogue
static int dialogue_listSelected = -1
static unsigned int choice_wid = 0
static char * choice_result
static int choice_nopts

Detailed Description

Is a high-level API around toolkit.c for easy window creation.

Only one dialogue may be open at once or behaviour is unspecified.

All these dialogues use what I call the secondary main loop hack. Basically they spawn another main loop identical to the primary whose only difference is that it breaks on loop_done. Therefore this loop hijacks the main loop until it's over, making these functions seem to be blocking without really being blocking.

See also
toolkit.c

Definition in file dialogue.c.

Function Documentation

◆ dialogue_addChoice()

void dialogue_addChoice ( const char * caption,
const char * msg,
const char * opt )

Add a choice to the dialog.

Parameters
captionCaption to use for the dialogue (for sizing).
msgMessage to display (for sizing).
*optThe value of the option.

Definition at line 796 of file dialogue.c.

◆ dialogue_alert()

void dialogue_alert ( const char * fmt,
... )

Displays an alert popup with only an ok button and a message.

Parameters
fmtPrintf style message to display.

Definition at line 130 of file dialogue.c.

◆ dialogue_alertRaw()

void dialogue_alertRaw ( const char * msg)

Displays an alert popup with only an ok button and a message.

Parameters
msgString to display.

Definition at line 149 of file dialogue.c.

◆ dialogue_cancel()

void dialogue_cancel ( unsigned int wid,
const char * str )
static

Generic window cancel.

Definition at line 116 of file dialogue.c.

◆ dialogue_choiceClose()

void dialogue_choiceClose ( unsigned int wid,
const char * str )
static

Closes a choice dialogue.

Parameters
widWindow being closed.
strStored to choice_result.

Definition at line 837 of file dialogue.c.

◆ dialogue_close()

void dialogue_close ( unsigned int wid,
const char * str )
static

Generic window close.

Definition at line 104 of file dialogue.c.

◆ dialogue_custom()

void dialogue_custom ( const char * wdwname,
const char * caption,
int width,
int height,
int(* update )(double dt, void *data),
void(* render )(double x, double y, double w, double h, void *data),
int(* event )(unsigned int wid, SDL_Event *event, void *data),
void * data,
int dynamic,
void(* freefunc )(void *data) )

Opens a custom dialogue window.

Parameters
wdwnameWindow name.
captionWindow title.
widthWidth of the widget.
heightHeight of the widget.
updateCustom render callback.
renderCustom render callback.
eventCustom event callback.
dataCustom data.
dynamicWhether or not the custom is dynamic.
freefuncFunction to be run when cleaning the custom data.

Definition at line 890 of file dialogue.c.

◆ dialogue_custom_event()

int dialogue_custom_event ( unsigned int wid,
SDL_Event * event )
static

Definition at line 853 of file dialogue.c.

◆ dialogue_customFullscreen()

int dialogue_customFullscreen ( unsigned int wid,
int enable )

Converts a custom dialogue to fullscreen.

Parameters
widWindow ID to use.
enableWhether or not to enable it.
Returns
0 on success.

Definition at line 956 of file dialogue.c.

◆ dialogue_customResize()

int dialogue_customResize ( unsigned int wid,
int width,
int height )

Resizes a custom dialogue.

Parameters
widWindow ID.
widthWidth to set to.
heightHeight to set to.
Returns
0 on success.

Definition at line 992 of file dialogue.c.

◆ dialogue_getSize()

glFont * dialogue_getSize ( const char * title,
const char * msg,
int * width,
int * height )
static

Gets the size needed for the dialogue.

Parameters
titleTitle of the dialogue.
msgMessage of the dialogue.
[out]widthGets the width needed.
[out]heightGets the height needed.
Returns
The font that matches the size.

Definition at line 177 of file dialogue.c.

◆ dialogue_input()

char * dialogue_input ( const char * title,
int min,
int max,
const char * fmt,
... )

Creates a dialogue that allows the player to write a message.

You must free the result if it's not null.

Parameters
titleTitle of the dialogue window.
minMinimum length of the message (must be non-zero).
maxMaximum length of the message (must be non-zero).
fmtPrintf style message to display on the dialogue.
Returns
The message the player typed or NULL if it was cancelled.

Definition at line 441 of file dialogue.c.

◆ dialogue_inputClose()

void dialogue_inputClose ( unsigned int wid,
const char * str )
static

Closes an input dialogue.

Parameters
widUnused.
strUnused.

Definition at line 542 of file dialogue.c.

◆ dialogue_inputRaw()

char * dialogue_inputRaw ( const char * title,
int min,
int max,
const char * msg )

Creates a dialogue that allows the player to write a message.

You must free the result if it's not null.

Parameters
titleTitle of the dialogue window.
minMinimum length of the message (must be non-zero).
maxMaximum length of the message (must be non-zero).
msgMessage to be displayed.
Returns
The message the player typed or NULL if it was cancelled.

Definition at line 470 of file dialogue.c.

◆ dialogue_isOpen()

int dialogue_isOpen ( void )

Checks to see if a dialogue is open.

Definition at line 96 of file dialogue.c.

◆ dialogue_list()

int dialogue_list ( const char * title,
char ** items,
int nitems,
const char * fmt,
... )

Creates a list dialogue with OK and Cancel button with a fixed message.

Parameters
titleTitle of the dialogue.
itemsItems in the list (should be all malloced, automatically freed).
nitemsNumber of items.
fmtprintf formatted string with text to display.

Definition at line 588 of file dialogue.c.

◆ dialogue_listCancel()

void dialogue_listCancel ( unsigned int wid,
const char * str )
static

Definition at line 553 of file dialogue.c.

◆ dialogue_listClose()

void dialogue_listClose ( unsigned int wid,
const char * str )
static

Definition at line 558 of file dialogue.c.

◆ dialogue_listPanel()

int dialogue_listPanel ( const char * title,
char ** items,
int nitems,
int extrawidth,
int minheight,
void(* add_widgets )(unsigned int wid, int x, int y, int w, int h),
void(* select_call )(unsigned int wid, const char *wgtname, int x, int y, int w, int h),
const char * fmt,
... )

Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra area for the list to react to item selected events.

Parameters
titleTitle of the dialogue.
itemsItems in the list (should be all malloced, automatically freed).
nitemsNumber of items.
extrawidthWidth of area to add for select_call callback.
minheightMinimum height for the window.
add_widgetsThis function is called with the new window as an argument allowing for initial population of the extra area.
select_callThis function is called when a new item in the list is selected, receiving the window's id and the selected widgets name as arguments.
fmtprintf formatted string with text to display.

Definition at line 639 of file dialogue.c.

◆ dialogue_listPanelRaw()

int dialogue_listPanelRaw ( const char * title,
char ** items,
int nitems,
int extrawidth,
int minheight,
void(* add_widgets )(unsigned int wid, int x, int y, int w, int h),
void(* select_call )(unsigned int wid, const char *wgtname, int x, int y, int w, int h),
const char * msg )

Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra area for the list to react to item selected events.

Parameters
titleTitle of the dialogue.
itemsItems in the list (should be all malloced, automatically freed).
nitemsNumber of items.
extrawidthWidth of area to add for select_call callback.
minheightMinimum height for the window.
add_widgetsThis function is called with the new window as an argument allowing for initial population of the extra area.
select_call(optional) This function is called when a new item in the list is selected, receiving the window's id and the selected widgets name as arguments.
msgstring with text to display.

Definition at line 681 of file dialogue.c.

◆ dialogue_listRaw()

int dialogue_listRaw ( const char * title,
char ** items,
int nitems,
const char * msg )

Creates a list dialogue with OK and Cancel button.

Parameters
titleTitle of the dialogue.
itemsItems in the list (should be all malloced, automatically freed).
nitemsNumber of items.
msgstring with text to display.

Definition at line 614 of file dialogue.c.

◆ dialogue_makeChoice()

void dialogue_makeChoice ( const char * caption,
const char * msg,
int opts )

Create the choice dialog. Need to add choices with below method.

Parameters
captionCaption to use for the dialogue.
msgMessage to display.
optsThe number of options.

Definition at line 773 of file dialogue.c.

◆ dialogue_msg()

void dialogue_msg ( const char * caption,
const char * fmt,
... )

Opens a dialogue window with an ok button and a message.

Parameters
captionWindow title.
fmtPrintf style message to display.

Definition at line 227 of file dialogue.c.

◆ dialogue_msgImg()

void dialogue_msgImg ( const char * caption,
const char * img,
const char * fmt,
... )

Opens a dialogue window with an ok button, a message and an image.

Parameters
captionWindow title.
imgPath of the image file to display.
fmtPrintf style message to display.

Definition at line 248 of file dialogue.c.

◆ dialogue_msgImgRaw()

void dialogue_msgImgRaw ( const char * caption,
const char * msg,
const char * img,
int width,
int height )

Opens a dialogue window with an ok button, a fixed message and an image.

Parameters
captionWindow title.
msgMessage to display.
imgPath of the image file to display.
widthWidth of the image. Negative uses image width.
heightHeight of the image. Negative uses image height.

Definition at line 296 of file dialogue.c.

◆ dialogue_msgRaw()

void dialogue_msgRaw ( const char * caption,
const char * msg )

Opens a dialogue window with an ok button and a fixed message.

Parameters
captionWindow title.
msgMessage to display.

Definition at line 269 of file dialogue.c.

◆ dialogue_runChoice()

char * dialogue_runChoice ( void )

Run the dialog and return the clicked string.

Note
You must free the return value.
Returns
The string chosen.

Definition at line 817 of file dialogue.c.

◆ dialogue_YesNo()

int dialogue_YesNo ( const char * caption,
const char * fmt,
... )

Runs a dialogue with both yes and no options.

Parameters
captionCaption to use for the dialogue.
fmtPrintf style message.
Returns
1 if yes is clicked or 0 if no is clicked.

Definition at line 352 of file dialogue.c.

◆ dialogue_YesNoClose()

void dialogue_YesNoClose ( unsigned int wid,
const char * str )
static

Closes a yesno dialogue.

Parameters
widWindow being closed.
strUnused.

Definition at line 408 of file dialogue.c.

◆ dialogue_YesNoRaw()

int dialogue_YesNoRaw ( const char * caption,
const char * msg )

Runs a dialogue with both yes and no options.

Parameters
captionCaption to use for the dialogue.
msgMessage to display.
Returns
1 if yes is clicked or 0 if no is clicked.

Definition at line 373 of file dialogue.c.

◆ main_loop()

void main_loop ( int nested)
extern

Split main loop from main() for secondary loop hack in toolkit.c.

Definition at line 795 of file naev.c.

◆ select_call_wrapper()

void select_call_wrapper ( unsigned int wid,
const char * wgtname )
static

used to pass appropriate information to the method that handles updating the extra information area in the dialogue listpanel.

Parameters
widWindow id
wgtnamename of the widget that raised the event.

Definition at line 571 of file dialogue.c.

◆ toolkit_loop()

int toolkit_loop ( int * loop_done,
dialogue_update_t * du )
static

Creates a secondary loop until loop_done is set to 1 or the toolkit closes.

Almost identical to the main loop in naev.c.

Todo
Fix this, we need proper threading as the music Lua and dialogue running Lua may be run in parallel and this will make everyone cry. So basically we have a race condition due to the "threading" effect this creates. Solved most of it by removing globals in the Lua event/mission code, but this doesn't mean it's solved. It just means it's extremely unlikely.
Returns
0 on success.

Definition at line 1019 of file dialogue.c.

Variable Documentation

◆ choice_nopts

int choice_nopts
static

Counter variable.

Definition at line 765 of file dialogue.c.

◆ choice_result

char* choice_result
static

Pointer to the choice result.

Definition at line 764 of file dialogue.c.

◆ choice_wid

unsigned int choice_wid = 0
static

Stores the choice window id.

Definition at line 763 of file dialogue.c.

◆ dialogue_listSelected

int dialogue_listSelected = -1
static

Definition at line 552 of file dialogue.c.

◆ dialogue_open

int dialogue_open
static

Number of dialogues open.

Definition at line 36 of file dialogue.c.

◆ dlgid

int dlgid = 0
static

ID generator.

Definition at line 37 of file dialogue.c.

◆ input_dialogue

InputDialogue input_dialogue
static

Stores the input window id and callback.

Definition at line 429 of file dialogue.c.