Skip to content

Commit

Permalink
Merge pull request #2723 from Matiiss/matiiss-fix-message-box-parent-…
Browse files Browse the repository at this point in the history
…window-none

Fix passing `parent_window=None` to `message_box`
  • Loading branch information
zoldalma999 authored Jun 19, 2024
2 parents 48420e3 + 40af2bb commit a4b6422
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 1 addition & 4 deletions docs/reST/ref/display.rst
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,10 @@ required).
:param str title: A title string.
:param str message: A message string. If this parameter is set to ``None``, the message will be the title.
:param str message_type: Set the type of message_box, could be ``"info"``, ``"warn"`` or ``"error"``.
:param Window parent_window: The parent window of the message box.
:param tuple buttons: An optional sequence of button name strings to show to the user.
:param int return_button: Button index to use if the return key is hit, ``0`` by default.
:param int escape_button: Button index to use if the escape key is hit, ``None`` for no button linked by default.
..
(Uncomment this after the window API is published)
:param Window parent_window: The parent window of the message_box
..

:return: The index of the button that was pushed.

Expand Down
17 changes: 11 additions & 6 deletions src_c/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -2704,10 +2704,10 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs)
"parent_window", "buttons", "return_button",
"escape_button", NULL};

if (!PyArg_ParseTupleAndKeywords(
arg, kwargs, "s|OsO!OiO", keywords, &title, &message, &msgbox_type,
&pgWindow_Type, &parent_window, &buttons, &return_button_index,
&escape_button_index_obj)) {
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "s|OsOOiO", keywords, &title,
&message, &msgbox_type, &parent_window,
&buttons, &return_button_index,
&escape_button_index_obj)) {
return NULL;
}

Expand Down Expand Up @@ -2744,10 +2744,15 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs)
msgbox_data.flags |= SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT;
#endif

if (parent_window == Py_None)
if (parent_window == Py_None) {
msgbox_data.window = NULL;
else
}
else {
if (!pgWindow_Check(parent_window)) {
return RAISE(PyExc_TypeError, "'parent_window' must be a Window");
}
msgbox_data.window = ((pgWindowObject *)parent_window)->_win;
}

msgbox_data.colorScheme = NULL; // use system color scheme settings

Expand Down
5 changes: 5 additions & 0 deletions test/display_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,11 @@ def test_message_box_buttons(self):
)
self.assertEqual(result, 0)

def test_message_box_parent_window_none(self):
pygame.display.message_box(
"Test", "Just close this messagebox", parent_window=None
)


if __name__ == "__main__":
unittest.main()

0 comments on commit a4b6422

Please sign in to comment.