gnome-terminal Closing a tab with the middle mouse button

    Good day, dear Khabrovchians!

    As they say, the case was in the evening at the end of the working day, there was nothing to do.

    It seemed to me convenient to close tabs in the browser with the middle mouse button and either the lack of work or just sports interest, I decided to implement this in gnome-terminal.
    Not that I didn’t have enough hot keys, I just wanted this opportunity to be there, all the more so it doesn’t ask for food.
    If you are interested in how this happened and what problems I encountered, welcome to cat.

    Start


    Armed with coffee, I went to look for the sources of gnome-terminal. Google cheerfully issued the first link source .

    Having downloaded the latest version of the terminal (I think why it’s trifling ..) I was severely broken, I could not compile it :(

    Having spit on this case, I downloaded the version of the terminal installed in the system (2.33.0), which was successfully compiled. I started to his "research".

    Analysis of Flight source



    My knowledge of GTK is very superficial, so I hoped for code readability and luck started to study the source.

    While browsing the files, I found terminal-tab-label.c it seemed interesting to me, and when I found there the creation of the button to close the tab, I decided that I was in the right place.

    Having rustled a little with the documentation (the fact that the documentation has a lot of broken links in the documentation a little upset), I realized that in order to catch a mouse click on an inscription, you need to insert the following code:
    g_signal_connect (label, "button-press-event", G_CALLBACK (click_label_cb), tab_label);
    

    But disappointment awaited me, the event did not process.

    The documentation also said: “To receive this signal, the GdkWindow associated to the widget needs to enable the GDK_BUTTON_PRESS_MASK mask.” (On the site, by the way, this link was broken, but then I figured out the secret to building links and found the right page.)

    Well, if you need to, then you need to. Through the search and partial code analysis method, I added lines of the form everywhere:
    gdk_window_set_events(root_window, GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK);
    

    But once again it was broken off, there was no effect. Although there is still a plus. While searching for the main window, I came across an interesting function terminal_window_init (terminal-window.c), namely the line:
      g_signal_connect (priv->notebook, "button-press-event",
                        G_CALLBACK (notebook_button_press_cb), window);
    

    As a result, I tried to wedge there.
    Having tidied up a bit, I came to this decision:
    ....
    static gboolean
    notebook_button_press_cb (GtkWidget *widget,
                              GdkEventButton *event,
                              TerminalWindow *window)
    {
      TerminalWindowPrivate *priv = window->priv;
      GtkNotebook *notebook = GTK_NOTEBOOK (widget);
      GtkWidget *menu;
      GtkAction *action;
      int tab_clicked;
      // Начало добавленного кода.
      if (event->type == GDK_BUTTON_PRESS && event->button == 2 ){
        tab_clicked = find_tab_num_at_pos (notebook, event->x_root, event->y_root);
        if (tab_clicked < 0) return FALSE;
        /* switch to the page the mouse is over */
        gtk_notebook_set_current_page (notebook, tab_clicked);
        action = gtk_action_group_get_action (priv->action_group, "PopupCloseTab");
        gtk_action_activate (action);
        return TRUE;
      }
      // конец добавленного кода.
      if (event->type != GDK_BUTTON_PRESS ||
    .....
    

    That's it! Works!

    Little things



    Inspired by success, I decided to "at the same time" remove one annoying trifle (confirmation to close the tab, if something is running) having scanned the code, I came across a line:
    do_confirm = gconf_client_get_bool (client, CONF_GLOBAL_PREFIX "/confirm_window_close", NULL);

    Googling a little on gconf_client_get_bool I found configuration files in my ~ / .gconf / apps / gnome-terminal.
    By setting the desired property (or maybe not quite correctly setting it), this, unfortunately, did not give the desired
    effect. So I decided to move on.

    In the source, I found the confirm_window_close setting in two places gnome-terminal.schemas and gnome-terminal.schemas.in
    it looks something like this:
    /schemas/apps/gnome-terminal/global/confirm_window_close/apps/gnome-terminal/global/confirm_window_closegnome-terminalbooltruegnome-terminalWhether to ask for confirmation when closing terminal windowsWhether to ask for confirmation when closing a terminal window which has more than one open tab.


    Changing the default value to false all worked. Hurrah!

    Well, then you need to install the changes made to us, this can be done either by assembling the package (recommended) and installing it, or make install (not recommended) I

    apologize in advance for the messy style of presentation.
    Thank you for attention!

    Also popular now: