Search code examples
linuxoperating-systemgeditxserver

In Linux, Which component decides to open a new window in gedit?


Let's consider that I open a file with the command gedit toto1.txt, a new window shows up with the content of toto1.txt. This sound familiar and usual, however the two following cases are not that straight to undertand: (1) a new command (let's says gedit toto2.txt) open a new tab in the previous window and (2) a new command (let's says gedit toto3.txt) will open a new tab in a new window.

My question is : which component decide to open the new window in case (2) and what are the condition to do so ? Why It did not opened a new window in case (1) ?

Any idea ?


Solution

  • It's gedit itself that makes that decision. Let's take a look at the source code. The function open_files will open a new window when it cannot find an active window (or when the flag --new-window was explicitly specified).

    static void
    open_files (GApplication            *application,
                gboolean                 new_window,
                ...)
    {
            GeditWindow *window = NULL;
    
            if (!new_window)
            {
                    window = get_active_window (GTK_APPLICATION (application));
            }
    
            if (window == NULL)
            {
                    gedit_debug_message (DEBUG_APP, "Create main window");
                    window = gedit_app_create_window (GEDIT_APP (application), NULL);
    
                    gedit_debug_message (DEBUG_APP, "Show window");
                    gtk_widget_show (GTK_WIDGET (window));
            }
    
            ...
    }
    

    So what's an "active window"? Let's look at get_active_window:

    static GeditWindow *
    get_active_window (GtkApplication *app)
    {
        GdkScreen *screen;
        guint workspace;
        gint viewport_x, viewport_y;
        GList *windows, *l;
    
        screen = gdk_screen_get_default ();
    
        workspace = gedit_utils_get_current_workspace (screen);
        gedit_utils_get_current_viewport (screen, &viewport_x, &viewport_y);
    
        /* Gtk documentation says the window list is always in MRU order */
        windows = gtk_application_get_windows (app);
        for (l = windows; l != NULL; l = l->next)
        {
            GtkWindow *window = l->data;
    
            if (GEDIT_IS_WINDOW (window) && is_in_viewport (window, screen, workspace, viewport_x, viewport_y))
            {
                return GEDIT_WINDOW (window);
            }
        }
    
        return NULL;
    }
    

    So, the answer is: gedit will open a new window if there's not already a gedit window on screen.

    (Well, there could of course be bugs here. I haven't looked very closely. That viewport_x/y stuff looks a bit suspect, as a viewport should have four coordinates: top/bottom/left/right. The code might be confused by multi-monitor setups).