cgtk4

Why doesn't the child window pop up on top of the parent window?


I am trying to create a preferences window for my app. I want the child-window (preferences) to open above the parent-window (main-window) and in the middle of the parent-window. But the problem is, when running the app for the first time, the child window is not launching on top of the parent window. But when i close the child window(preferences) and open it again, it opens on top of the parent window.

Note: parent-window = main-window. & child-window = preferences.

#include <gtk/gtk.h>

void preferences_cb(GtkButton *button, gpointer data)
{
    GtkWidget *widget;
    widget = (GtkWidget *) data;
    if (widget == NULL)
    return;
      gtk_widget_realize(widget);
      gtk_window_present ((GtkWindow *)widget);
    return;
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *grid;
  GtkWidget *button; 
  GtkWidget *preferences;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "parent window");
  gtk_widget_set_size_request(window, 700, 200);
  gtk_window_set_resizable (GTK_WINDOW(window), TRUE);

  grid = gtk_grid_new ();
  gtk_window_set_child ((GtkWindow *)window, grid);
  gtk_widget_set_vexpand(grid, TRUE);
  gtk_widget_set_hexpand(grid, TRUE);

  button = gtk_button_new_with_label("Show preferences.win..");
  gtk_grid_attach ((GtkGrid *)grid, button, 0, 0, 80, 22); 
  gtk_widget_set_vexpand(button, TRUE);
  gtk_widget_set_hexpand(button, TRUE);

  preferences = gtk_window_new();
  gtk_window_set_title (GTK_WINDOW (window), "preferences");
  gtk_window_set_transient_for ((GtkWindow *)preferences, (GtkWindow *)window);
  gtk_window_set_default_size(GTK_WINDOW(preferences), 400, 500);
  gtk_window_set_hide_on_close (GTK_WINDOW(preferences), TRUE);
  gtk_window_set_resizable (GTK_WINDOW(preferences), FALSE);    

  g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(preferences_cb), preferences);
  gtk_window_present ((GtkWindow *)window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;
  #if GLIB_CHECK_VERSION(2, 74, 0)
      app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS); 
  #else
       app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE); 
  #endif
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Solution

  • As far as I know it, the window position is determined by the window system of the operating system independently of GTK.

    So you can't do anything.

    However, I have changed one thing in the script:

    ...
    preferences = gtk_window_new();
    gtk_window_set_title (GTK_WINDOW (preferences), "preferences"); // not (window)
    ...
    

    And with Ubuntu it works now the way I would expect to do after studying your script.

    Enjoy testing and programming.