mirror of https://github.com/OpenTTD/OpenTTD
(svn r13039) -Codechange: make a class of the Query window.
parent
7884a4ff19
commit
46875e28be
105
src/misc_gui.cpp
105
src/misc_gui.cpp
|
@ -1104,63 +1104,76 @@ enum QueryWidgets {
|
||||||
QUERY_WIDGET_YES
|
QUERY_WIDGET_YES
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
struct query_d {
|
* Window used for asking the user a YES/NO question.
|
||||||
|
*/
|
||||||
|
struct QueryWindow : public Window {
|
||||||
void (*proc)(Window*, bool); ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise
|
void (*proc)(Window*, bool); ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise
|
||||||
uint64 params[10]; ///< local copy of _decode_parameters
|
uint64 params[10]; ///< local copy of _decode_parameters
|
||||||
StringID message; ///< message shown for query window
|
StringID message; ///< message shown for query window
|
||||||
bool calledback; ///< has callback been executed already (internal usage for WE_DESTROY event)
|
|
||||||
};
|
|
||||||
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(query_d));
|
|
||||||
|
|
||||||
|
QueryWindow(const WindowDesc *desc, StringID caption, StringID message, Window *parent, void (*callback)(Window*, bool)) : Window(desc)
|
||||||
|
{
|
||||||
|
if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
|
||||||
|
this->parent = parent;
|
||||||
|
this->left = parent->left + (parent->width / 2) - (this->width / 2);
|
||||||
|
this->top = parent->top + (parent->height / 2) - (this->height / 2);
|
||||||
|
|
||||||
static void QueryWndProc(Window *w, WindowEvent *e)
|
/* Create a backup of the variadic arguments to strings because it will be
|
||||||
{
|
* overridden pretty often. We will copy these back for drawing */
|
||||||
query_d *q = &WP(w, query_d);
|
CopyOutDParam(this->params, 0, lengthof(this->params));
|
||||||
|
this->widget[QUERY_WIDGET_CAPTION].data = caption;
|
||||||
|
this->message = message;
|
||||||
|
this->proc = callback;
|
||||||
|
}
|
||||||
|
|
||||||
switch (e->event) {
|
~QueryWindow()
|
||||||
case WE_PAINT:
|
{
|
||||||
CopyInDParam(0, q->params, lengthof(q->params));
|
if (this->proc != NULL) this->proc(this->parent, false);
|
||||||
DrawWindowWidgets(w);
|
}
|
||||||
CopyInDParam(0, q->params, lengthof(q->params));
|
|
||||||
|
|
||||||
DrawStringMultiCenter(w->width / 2, (w->height / 2) - 10, q->message, w->width - 2);
|
virtual void OnPaint()
|
||||||
break;
|
{
|
||||||
|
CopyInDParam(0, this->params, lengthof(this->params));
|
||||||
|
DrawWindowWidgets(this);
|
||||||
|
CopyInDParam(0, this->params, lengthof(this->params));
|
||||||
|
|
||||||
case WE_CLICK:
|
DrawStringMultiCenter(this->width / 2, (this->height / 2) - 10, this->message, this->width - 2);
|
||||||
switch (e->we.click.widget) {
|
}
|
||||||
|
|
||||||
|
virtual void OnClick(Point pt, int widget)
|
||||||
|
{
|
||||||
|
switch (widget) {
|
||||||
case QUERY_WIDGET_YES:
|
case QUERY_WIDGET_YES:
|
||||||
q->calledback = true;
|
if (this->proc != NULL) {
|
||||||
if (q->proc != NULL) q->proc(w->parent, true);
|
this->proc(this->parent, true);
|
||||||
|
this->proc = NULL;
|
||||||
|
}
|
||||||
/* Fallthrough */
|
/* Fallthrough */
|
||||||
case QUERY_WIDGET_NO:
|
case QUERY_WIDGET_NO:
|
||||||
delete w;
|
delete this;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
|
|
||||||
case WE_KEYPRESS: // ESC closes the window, Enter confirms the action
|
virtual bool OnKeyPress(uint16 key, uint16 keycode)
|
||||||
switch (e->we.keypress.keycode) {
|
{
|
||||||
|
/* ESC closes the window, Enter confirms the action */
|
||||||
|
switch (keycode) {
|
||||||
case WKC_RETURN:
|
case WKC_RETURN:
|
||||||
case WKC_NUM_ENTER:
|
case WKC_NUM_ENTER:
|
||||||
q->calledback = true;
|
if (this->proc != NULL) {
|
||||||
if (q->proc != NULL) q->proc(w->parent, true);
|
this->proc(this->parent, true);
|
||||||
|
this->proc = NULL;
|
||||||
|
}
|
||||||
/* Fallthrough */
|
/* Fallthrough */
|
||||||
case WKC_ESC:
|
case WKC_ESC:
|
||||||
e->we.keypress.cont = false;
|
delete this;
|
||||||
delete w;
|
return false;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
break;
|
return true;
|
||||||
|
|
||||||
case WE_DESTROY: // Call callback function (if any) on window close if not yet called
|
|
||||||
if (!q->calledback && q->proc != NULL) {
|
|
||||||
q->calledback = true;
|
|
||||||
q->proc(w->parent, false);
|
|
||||||
}
|
}
|
||||||
break;
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static const Widget _query_widgets[] = {
|
static const Widget _query_widgets[] = {
|
||||||
|
@ -1177,7 +1190,7 @@ static const WindowDesc _query_desc = {
|
||||||
WC_CONFIRM_POPUP_QUERY, WC_NONE,
|
WC_CONFIRM_POPUP_QUERY, WC_NONE,
|
||||||
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_UNCLICK_BUTTONS | WDF_DEF_WIDGET | WDF_MODAL,
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_UNCLICK_BUTTONS | WDF_DEF_WIDGET | WDF_MODAL,
|
||||||
_query_widgets,
|
_query_widgets,
|
||||||
QueryWndProc
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Show a modal confirmation window with standard 'yes' and 'no' buttons
|
/** Show a modal confirmation window with standard 'yes' and 'no' buttons
|
||||||
|
@ -1190,21 +1203,7 @@ static const WindowDesc _query_desc = {
|
||||||
* @param callback callback function pointer to set in the window descriptor*/
|
* @param callback callback function pointer to set in the window descriptor*/
|
||||||
void ShowQuery(StringID caption, StringID message, Window *parent, void (*callback)(Window*, bool))
|
void ShowQuery(StringID caption, StringID message, Window *parent, void (*callback)(Window*, bool))
|
||||||
{
|
{
|
||||||
Window *w = new Window(&_query_desc);
|
new QueryWindow(&_query_desc, caption, message, parent, callback);
|
||||||
if (w == NULL) return;
|
|
||||||
|
|
||||||
if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
|
|
||||||
w->parent = parent;
|
|
||||||
w->left = parent->left + (parent->width / 2) - (w->width / 2);
|
|
||||||
w->top = parent->top + (parent->height / 2) - (w->height / 2);
|
|
||||||
|
|
||||||
/* Create a backup of the variadic arguments to strings because it will be
|
|
||||||
* overridden pretty often. We will copy these back for drawing */
|
|
||||||
CopyOutDParam(WP(w, query_d).params, 0, lengthof(WP(w, query_d).params));
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue