(svn r7621) -Codechange: Rework ShowQuery into a general modal popup window. It gets passed

a parent pointer which will be blocked as long as the popup is open. This
 applies to newgrf-apply, heightmap warning, genworld progress.
pull/155/head
Darkvater 18 years ago
parent 324e805d51
commit a7f2b8095f

@ -176,16 +176,9 @@ static void StartGeneratingLandscape(glwp_modes mode)
} }
} }
static void HeightmapScaledTooMuchCallback(bool ok_clicked) static void HeightmapScaledTooMuchCallback(Window *w, bool confirmed)
{ {
if (ok_clicked) { if (confirmed) StartGeneratingLandscape((glwp_modes)w->window_number);
Window *w;
glwp_modes mode = 0;
for (mode = 0; mode < GLWP_END; mode++) {
w = FindWindowById(WC_GENERATE_LANDSCAPE, mode);
if (w != NULL) StartGeneratingLandscape(mode);
}
}
} }
void GenerateLandscapeWndProc(Window *w, WindowEvent *e) void GenerateLandscapeWndProc(Window *w, WindowEvent *e)
@ -330,7 +323,11 @@ void GenerateLandscapeWndProc(Window *w, WindowEvent *e)
if (mode == GLWP_HEIGHTMAP && ( if (mode == GLWP_HEIGHTMAP && (
_heightmap_x * 2 < (1U << _patches_newgame.map_x) || _heightmap_x / 2 > (1U << _patches_newgame.map_x) || _heightmap_x * 2 < (1U << _patches_newgame.map_x) || _heightmap_x / 2 > (1U << _patches_newgame.map_x) ||
_heightmap_y * 2 < (1U << _patches_newgame.map_y) || _heightmap_y / 2 > (1U << _patches_newgame.map_y))) { _heightmap_y * 2 < (1U << _patches_newgame.map_y) || _heightmap_y / 2 > (1U << _patches_newgame.map_y))) {
ShowQuery(STR_HEIGHTMAP_SCALE_WARNING_CAPTION, STR_HEIGHTMAP_SCALE_WARNING_MESSAGE, HeightmapScaledTooMuchCallback, WC_GENERATE_LANDSCAPE, mode); ShowQuery(
STR_HEIGHTMAP_SCALE_WARNING_CAPTION,
STR_HEIGHTMAP_SCALE_WARNING_MESSAGE,
w,
HeightmapScaledTooMuchCallback);
} else { } else {
StartGeneratingLandscape(mode); StartGeneratingLandscape(mode);
} }
@ -722,10 +719,13 @@ typedef struct tp_info {
static tp_info _tp; static tp_info _tp;
static void AbortGeneratingWorldCallback(bool ok_clicked) static void AbortGeneratingWorldCallback(Window *w, bool confirmed)
{ {
if (ok_clicked) AbortGeneratingWorld(); if (confirmed) {
else if (IsGeneratingWorld() && !IsGeneratingWorldAborted()) SetMouseCursor(SPR_CURSOR_ZZZ); AbortGeneratingWorld();
} else if (IsGeneratingWorld() && !IsGeneratingWorldAborted()) {
SetMouseCursor(SPR_CURSOR_ZZZ);
}
} }
static void ShowTerrainProgressProc(Window* w, WindowEvent* e) static void ShowTerrainProgressProc(Window* w, WindowEvent* e)
@ -735,7 +735,12 @@ static void ShowTerrainProgressProc(Window* w, WindowEvent* e)
switch (e->we.click.widget) { switch (e->we.click.widget) {
case 2: case 2:
if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE); if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE);
ShowQuery(STR_GENERATION_ABORT_CAPTION, STR_GENERATION_ABORT_MESSAGE, AbortGeneratingWorldCallback, WC_GENERATE_PROGRESS_WINDOW, 0); ShowQuery(
STR_GENERATION_ABORT_CAPTION,
STR_GENERATION_ABORT_MESSAGE,
w,
AbortGeneratingWorldCallback
);
break; break;
} }
break; break;

@ -127,7 +127,7 @@ void ShowBuildBridgeWindow(uint start, uint end, byte type);
void ShowBuildIndustryWindow(void); void ShowBuildIndustryWindow(void);
void ShowQueryString(StringID str, StringID caption, uint maxlen, uint maxwidth, WindowClass window_class, WindowNumber window_number, CharSetFilter afilter); void ShowQueryString(StringID str, StringID caption, uint maxlen, uint maxwidth, WindowClass window_class, WindowNumber window_number, CharSetFilter afilter);
void ShowQuery(StringID caption, StringID message, void (*ok_cancel_callback)(bool ok_clicked), WindowClass window_class, WindowNumber window_number); void ShowQuery(StringID caption, StringID message, Window *w, void (*callback)(Window*, bool));
void ShowMusicWindow(void); void ShowMusicWindow(void);
/* main_gui.c */ /* main_gui.c */

@ -1169,68 +1169,112 @@ void ShowQueryString(StringID str, StringID caption, uint maxlen, uint maxwidth,
InitializeTextBuffer(&WP(w, querystr_d).text, _edit_str_buf, realmaxlen, maxwidth); InitializeTextBuffer(&WP(w, querystr_d).text, _edit_str_buf, realmaxlen, maxwidth);
} }
enum QueryWidgets {
QUERY_WIDGET_CAPTION = 1,
QUERY_WIDGET_NO = 3,
QUERY_WIDGET_YES
};
typedef struct query_d {
StringID message; ///< message shown for query window
uint32 params[20]; ///< local copy of _decode_parameters
void (*proc)(Window*, bool); ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise
bool calledback; ///< has callback been executed already (internal usage for WE_DESTROY event)
} query_d;
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(query_d));
static void QueryWndProc(Window *w, WindowEvent *e) static void QueryWndProc(Window *w, WindowEvent *e)
{ {
query_d *q = &WP(w, query_d);
switch (e->event) { switch (e->event) {
case WE_PAINT: case WE_PAINT:
SetDParam(0, WP(w, query_d).caption); COPY_IN_DPARAM(0, q->params, lengthof(q->params));
DrawWindowWidgets(w); DrawWindowWidgets(w);
COPY_IN_DPARAM(0, q->params, lengthof(q->params));
DrawStringMultiCenter(90, 38, WP(w, query_d).message, 178); DrawStringMultiCenter(w->width / 2, (w->height / 2) - 10, q->message, w->width);
break; break;
case WE_CLICK: case WE_CLICK:
switch (e->we.click.widget) { switch (e->we.click.widget) {
case 3: case QUERY_WIDGET_YES:
case 4: q->calledback = true;
WP(w, query_d).calledback = true; if (q->proc != NULL) q->proc(w->parent, true);
if (WP(w, query_d).ok_cancel_callback != NULL) WP(w, query_d).ok_cancel_callback(e->we.click.widget == 4); /* Fallthrough */
DeleteWindow(w); case QUERY_WIDGET_NO:
DeleteWindow(w);
break;
}
break; break;
}
break;
case WE_MOUSELOOP: case WE_KEYPRESS: /* ESC closes the window, Enter confirms the action */
if (!FindWindowById(WP(w, query_d).wnd_class, WP(w, query_d).wnd_num)) DeleteWindow(w); switch (e->we.keypress.keycode) {
break; case WKC_RETURN:
case WKC_NUM_ENTER:
q->calledback = true;
if (q->proc != NULL) q->proc(w->parent, true);
/* Fallthrough */
case WKC_ESC:
e->we.keypress.cont = false;
DeleteWindow(w);
break;
}
break;
case WE_DESTROY: case WE_DESTROY: /* Call callback function (if any) on window close if not yet called */
if (!WP(w, query_d).calledback && WP(w, query_d).ok_cancel_callback != NULL) WP(w, query_d).ok_cancel_callback(false); if (!q->calledback && q->proc != NULL) q->proc(w->parent, false);
break; break;
} }
} }
static const Widget _query_widgets[] = { static const Widget _query_widgets[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 4, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, { WWT_CLOSEBOX, RESIZE_NONE, 4, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
{ WWT_CAPTION, RESIZE_NONE, 4, 11, 179, 0, 13, STR_012D, STR_NULL}, { WWT_CAPTION, RESIZE_NONE, 4, 11, 209, 0, 13, STR_NULL, STR_NULL},
{ WWT_PANEL, RESIZE_NONE, 4, 0, 179, 14, 91, 0x0, STR_NULL}, { WWT_PANEL, RESIZE_NONE, 4, 0, 209, 14, 81, 0x0, /*OVERRIDE*/STR_NULL},
{ WWT_TEXTBTN, RESIZE_NONE, 12, 25, 84, 72, 83, STR_012E_CANCEL, STR_NULL}, {WWT_PUSHTXTBTN, RESIZE_NONE, 3, 20, 90, 62, 73, STR_00C9_NO, STR_NULL},
{ WWT_TEXTBTN, RESIZE_NONE, 12, 95, 154, 72, 83, STR_012F_OK, STR_NULL}, {WWT_PUSHTXTBTN, RESIZE_NONE, 3, 120, 190, 62, 73, STR_00C8_YES, STR_NULL},
{ WIDGETS_END }, { WIDGETS_END },
}; };
static const WindowDesc _query_desc = { static const WindowDesc _query_desc = {
WDP_CENTER, WDP_CENTER, 180, 92, WDP_CENTER, WDP_CENTER, 210, 82,
WC_OK_CANCEL_QUERY, 0, WC_CONFIRM_POPUP_QUERY, 0,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET, WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_UNCLICK_BUTTONS | WDF_DEF_WIDGET | WDF_MODAL,
_query_widgets, _query_widgets,
QueryWndProc QueryWndProc
}; };
void ShowQuery(StringID caption, StringID message, void (*ok_cancel_callback)(bool ok_clicked), WindowClass window_class, WindowNumber window_number) /** Show a modal confirmation window with standard 'yes' and 'no' buttons
* The window is aligned to the centre of its parent.
* NOTE: You cannot use BindCString as parameter for this window!
* @param caption string shown as window caption
* @param message string that will be shown for the window
* @param parent pointer to parent window, if this pointer is NULL the parent becomes
* the main window WC_MAIN_WINDOW
* @param x,y coordinates to show the window at
* @param yes_no_callback callback function called when window is closed through any button */
void ShowQuery(StringID caption, StringID message, Window *parent, void (*callback)(Window*, bool))
{ {
Window *w; Window *w = AllocateWindowDesc(&_query_desc);
if (w == NULL) return;
DeleteWindowById(WC_OK_CANCEL_QUERY, 0);
if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
w = AllocateWindowDesc(&_query_desc); w->parent = parent;
w->left = parent->left + (parent->width / 2) - (w->width / 2);
WP(w, query_d).caption = caption; w->top = parent->top + (parent->height / 2) - (w->height / 2);
WP(w, query_d).message = message;
WP(w, query_d).wnd_class = window_class; /* Create a backup of the variadic arguments to strings because it will be
WP(w, query_d).wnd_num = window_number; * overridden pretty often. We will copy these back for drawing */
WP(w, query_d).ok_cancel_callback = ok_cancel_callback; COPY_OUT_DPARAM(WP(w, query_d).params, 0, lengthof(WP(w, query_d).params));
WP(w, query_d).calledback = false; w->widget[QUERY_WIDGET_CAPTION].data = caption;
WP(w, query_d).message = message;
WP(w, query_d).proc = callback;
WP(w, query_d).calledback = false;
} }

@ -278,10 +278,9 @@ static void SetupNewGRFWindow(Window *w)
/** Callback function for the newgrf 'apply changes' confirmation window /** Callback function for the newgrf 'apply changes' confirmation window
* @param yes_clicked boolean value, true when yes was clicked, false otherwise */ * @param yes_clicked boolean value, true when yes was clicked, false otherwise */
static void NewGRFConfirmationCallback(bool yes_clicked) static void NewGRFConfirmationCallback(Window *w, bool confirmed)
{ {
if (yes_clicked) { if (confirmed) {
Window *w = FindWindowById(WC_GAME_OPTIONS, 0);
newgrf_d *nd = &WP(w, newgrf_d); newgrf_d *nd = &WP(w, newgrf_d);
CopyGRFConfigList(nd->orig_list, *nd->list); CopyGRFConfigList(nd->orig_list, *nd->list);
@ -424,9 +423,8 @@ static void NewGRFWndProc(Window *w, WindowEvent *e)
ShowQuery( ShowQuery(
STR_POPUP_CAUTION_CAPTION, STR_POPUP_CAUTION_CAPTION,
STR_NEWGRF_CONFIRMATION_TEXT, STR_NEWGRF_CONFIRMATION_TEXT,
NewGRFConfirmationCallback, w,
w->window_class, NewGRFConfirmationCallback
w->window_number
); );
} else { } else {
CopyGRFConfigList(WP(w, newgrf_d).orig_list, *WP(w, newgrf_d).list); CopyGRFConfigList(WP(w, newgrf_d).orig_list, *WP(w, newgrf_d).list);

@ -443,7 +443,7 @@ enum {
WC_SIGN_LIST, WC_SIGN_LIST,
WC_GENERATE_LANDSCAPE, WC_GENERATE_LANDSCAPE,
WC_GENERATE_PROGRESS_WINDOW, WC_GENERATE_PROGRESS_WINDOW,
WC_OK_CANCEL_QUERY, WC_CONFIRM_POPUP_QUERY,
WC_DEPOT_SELL_ALL, WC_DEPOT_SELL_ALL,
}; };

@ -350,16 +350,6 @@ typedef struct querystr_d {
} querystr_d; } querystr_d;
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(querystr_d)); assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(querystr_d));
typedef struct query_d {
StringID caption;
StringID message;
WindowClass wnd_class;
WindowNumber wnd_num;
void (*ok_cancel_callback)(bool ok_clicked);
bool calledback;
} query_d;
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(query_d));
typedef struct { typedef struct {
byte item_count; /* follow_vehicle */ byte item_count; /* follow_vehicle */
byte sel_index; /* scrollpos_x */ byte sel_index; /* scrollpos_x */

Loading…
Cancel
Save