How to update the code below from GTK 3.x to GTK 4.x

There is no way to do this using only GTK4, please read this thread for a further discussion: Gtk4 migration - window management functions gone

You will have to use your backend’s API (Xlib, Win32, etc) if you want to continue using this functionality.