From bfbfa39199900d78326796d8f26d6a721336cbf5 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 21 Oct 2022 03:55:00 +0300
Subject: [PATCH 31/31] gtk4: Implement pregame players list menu

Initially with just the player info entry.

See osdn #45922

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-gtk-4.0/pages.c | 70 +++++++++++++++++++++++++-------------
 1 file changed, 46 insertions(+), 24 deletions(-)

diff --git a/client/gui-gtk-4.0/pages.c b/client/gui-gtk-4.0/pages.c
index ad74b7fc6e..966c9372a5 100644
--- a/client/gui-gtk-4.0/pages.c
+++ b/client/gui-gtk-4.0/pages.c
@@ -1542,7 +1542,6 @@ static void client_take_player(struct player *pplayer)
                                            client_aitoggle_player, data);
 }
 
-#ifdef MENUS_GTK3
 /**********************************************************************//**
   Connect the object to the player and the connection.
 **************************************************************************/
@@ -1588,7 +1587,6 @@ static bool object_extract(GObject *object, struct player **ppplayer,
 
   return ret;
 }
-#endif /* MENUS_GTK3 */
 
 /**********************************************************************//**
   Request the game options dialog.
@@ -1775,6 +1773,7 @@ static void conn_menu_connection_command(GObject *object, gpointer data)
                      pconn->username);
   }
 }
+#endif /* MENUS_GTK3 */
 
 /**********************************************************************//**
   Show details about a user in the Connected Users dialog in a popup.
@@ -1810,12 +1809,14 @@ static void show_conn_popup(struct player *pplayer, struct connection *pconn)
 /**********************************************************************//**
   Callback for when the "info" entry is chosen from the conn menu.
 **************************************************************************/
-static void conn_menu_info_chosen(GObject *object, gpointer data)
+static void conn_menu_info_chosen(GSimpleAction *action, GVariant *parameter,
+                                  gpointer data)
 {
   struct player *pplayer;
   struct connection *pconn;
+  GMenu *menu = data;
 
-  if (object_extract(object, &pplayer, &pconn)) {
+  if (object_extract(G_OBJECT(menu), &pplayer, &pconn)) {
     show_conn_popup(pplayer, pconn);
   }
 }
@@ -1827,15 +1828,29 @@ static void conn_menu_info_chosen(GObject *object, gpointer data)
 static GtkWidget *create_conn_menu(struct player *pplayer,
                                    struct connection *pconn)
 {
-  GtkWidget *menu;
-  GtkWidget *item;
+  GtkWidget *popover;
+  GMenu *menu;
   gchar *buf;
+  GSimpleAction *act;
+  GMenuItem *item;
+  GActionGroup *group;
+
+  group = G_ACTION_GROUP(g_simple_action_group_new());
+
+  menu = g_menu_new();
 
-  menu = gtk_menu_button_new();
   object_put(G_OBJECT(menu), pplayer, pconn);
 
   buf = g_strdup_printf(_("%s info"),
                         pconn ? pconn->username : player_name(pplayer));
+
+  act = g_simple_action_new("info", NULL);
+  g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act));
+  g_signal_connect(act, "activate", G_CALLBACK(conn_menu_info_chosen), menu);
+  item = g_menu_item_new(buf, "win.info");
+  g_menu_append_item(menu, item);
+
+#ifdef MENUS_GTK3
   item = gtk_menu_item_new_with_label(buf);
   g_free(buf);
   gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
@@ -1978,10 +1993,13 @@ static GtkWidget *create_conn_menu(struct player *pplayer,
   }
 
   gtk_widget_show(menu);
+#endif /* MENUS_GTK3 */
+
+  popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu));
+  gtk_widget_insert_action_group(popover, "win", group);
 
-  return menu;
+  return popover;
 }
-#endif /* MENUS_GTK3 */
 
 /**********************************************************************//**
   Unselect a tree path.
@@ -2036,25 +2054,27 @@ static gboolean connect_list_right_button(GtkGestureClick *gesture,
                                           double x, double y, gpointer data)
 {
   GtkEventController *controller = GTK_EVENT_CONTROLLER(gesture);
-  GtkTreeView *tree = GTK_TREE_VIEW(gtk_event_controller_get_widget(controller));
+  GtkWidget *tree = gtk_event_controller_get_widget(controller);
   GtkTreePath *path = NULL;
-#ifdef MENUS_GTK3
-  GtkTreeSelection *selection = gtk_tree_view_get_selection(tree);
-#endif
+  GtkWidget *menu;
+  GtkTreeSelection *selection;
+  GtkTreeModel *model;
+  GtkTreeIter iter;
+  int player_no, conn_id;
+  struct player *pplayer;
+  struct connection *pconn;
+  int bx, by;
+  GdkRectangle rect = { .x = x, .y = y, .width = 1, .height = 1};
 
-  if (!gtk_tree_view_get_path_at_pos(tree,
-                                     x, y,
+  gtk_tree_view_convert_widget_to_bin_window_coords(GTK_TREE_VIEW(tree), x, y, &bx, &by);
+
+  if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(tree), bx, by,
                                      &path, NULL, NULL, NULL)) {
     return FALSE;
   }
 
-#ifdef MENUS_GTK3
-  GtkTreeModel *model = gtk_tree_view_get_model(tree);
-  GtkTreeIter iter;
-  GtkWidget *menu;
-  int player_no, conn_id;
-  struct player *pplayer;
-  struct connection *pconn;
+  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
+  model = gtk_tree_view_get_model(GTK_TREE_VIEW(tree));
 
   if (!gtk_tree_selection_path_is_selected(selection, path)) {
     gtk_tree_selection_select_path(selection, path);
@@ -2068,8 +2088,10 @@ static gboolean connect_list_right_button(GtkGestureClick *gesture,
   pconn = conn_by_number(conn_id);
 
   menu = create_conn_menu(pplayer, pconn);
-  gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
-#endif /* MENUS_GTK3 */
+  gtk_widget_set_parent(menu, GTK_WIDGET(tree));
+  gtk_popover_set_pointing_to(GTK_POPOVER(menu), &rect);
+
+  gtk_popover_popup(GTK_POPOVER(menu));
 
   gtk_tree_path_free(path);
 
-- 
2.35.1