libgnt/libgnt

Add private accessors to GntWS->ordered.

2019-04-24, Elliott Sales de Andrade
f89f35b1a9b4
Parents 9ed8d7ec255e
Children 49842f6491e1
Add private accessors to GntWS->ordered.
  • +8 -4
    gntmain.c
  • +76 -76
    gntwm.c
  • +64 -0
    gntws.c
  • +42 -0
    gntwsprivate.h
  • +4 -1
    wms/irssi.c
  • --- a/gntmain.c Wed Apr 24 04:24:43 2019 +0000
    +++ b/gntmain.c Wed Apr 24 21:54:50 2019 -0400
    @@ -50,6 +50,8 @@
    #include "gntwindow.h"
    #include "gntwm.h"
    +#include "gntwsprivate.h"
    +
    #include <panel.h>
    #include <stdio.h>
    @@ -158,8 +160,9 @@
    GntWidget *widget = NULL;
    PANEL *p = NULL;
    - if (!wm->cws->ordered || buffer[0] != 27)
    + if (gnt_ws_is_empty(wm->cws) || buffer[0] != 27) {
    return FALSE;
    + }
    buffer++;
    if (strlen(buffer) < 5)
    @@ -214,7 +217,7 @@
    if (event == GNT_LEFT_MOUSE_DOWN && widget &&
    widget != wm->_list.window && !gnt_widget_get_transient(widget)) {
    - if (widget != wm->cws->ordered->data) {
    + if (!gnt_ws_is_top_widget(wm->cws, widget)) {
    gnt_wm_raise_window(wm, widget);
    }
    if (y == widget->priv.y) {
    @@ -707,7 +710,7 @@
    if (widget == wm->_list.window)
    return TRUE;
    - if (wm->cws->ordered && wm->cws->ordered->data == widget) {
    + if (gnt_ws_is_top_widget(wm->cws, widget)) {
    if (GNT_IS_BOX(widget) &&
    (GNT_BOX(widget)->active == w || widget == w))
    return TRUE;
    @@ -719,8 +722,9 @@
    {
    widget = gnt_widget_get_toplevel(widget);
    - if (wm->cws->ordered && wm->cws->ordered->data == widget)
    + if (gnt_ws_is_top_widget(wm->cws, widget)) {
    return;
    + }
    gnt_widget_set_is_urgent(widget, TRUE);
    --- a/gntwm.c Wed Apr 24 04:24:43 2019 +0000
    +++ b/gntwm.c Wed Apr 24 21:54:50 2019 -0400
    @@ -60,6 +60,8 @@
    #include "gntutils.h"
    #include "gntwindow.h"
    +#include "gntwsprivate.h"
    +
    #define IDLE_CHECK_INTERVAL 5 /* 5 seconds */
    enum
    @@ -106,14 +108,6 @@
    G_DEFINE_TYPE(GntWM, gnt_wm, GNT_TYPE_BINDABLE)
    -static GList *
    -g_list_bring_to_front(GList *list, gpointer data)
    -{
    - list = g_list_remove(list, data);
    - list = g_list_prepend(list, data);
    - return list;
    -}
    -
    static void
    free_node(gpointer data)
    {
    @@ -417,14 +411,15 @@
    if (wm->_list.window || wm->menu)
    return;
    - if (!wm->cws->ordered || !wm->cws->ordered->next)
    + if (gnt_ws_is_empty(wm->cws) || gnt_ws_is_single(wm->cws)) {
    return;
    + }
    if (wm->mode != GNT_KP_MODE_NORMAL) {
    ensure_normal_mode(wm);
    }
    - w = wm->cws->ordered->data;
    + w = gnt_ws_get_top_widget(wm->cws);
    orgpos = pos = g_list_index(wm->cws->list, w);
    g_return_if_fail(pos >= 0);
    @@ -467,8 +462,9 @@
    GList *l;
    int n;
    - if (!wm->cws->ordered)
    + if (gnt_ws_is_empty(wm->cws)) {
    return TRUE;
    + }
    if (list)
    n = GPOINTER_TO_INT(list->data);
    @@ -490,10 +486,11 @@
    GntWidget *window;
    GntNode *node;
    - if (!wm->cws->ordered)
    + if (gnt_ws_is_empty(wm->cws)) {
    return TRUE;
    + }
    - window = wm->cws->ordered->data;
    + window = gnt_ws_get_top_widget(wm->cws);
    node = g_hash_table_lookup(wm->nodes, window);
    if (!node)
    return TRUE;
    @@ -514,10 +511,11 @@
    GntNode *node;
    int w, h;
    - if (!wm->cws->ordered)
    + if (gnt_ws_is_empty(wm->cws)) {
    return TRUE;
    + }
    - window = wm->cws->ordered->data;
    + window = gnt_ws_get_top_widget(wm->cws);
    node = g_hash_table_lookup(wm->nodes, window);
    if (!node)
    return TRUE;
    @@ -539,8 +537,8 @@
    if (wm->_list.window)
    return TRUE;
    - if (wm->cws->ordered) {
    - gnt_widget_destroy(wm->cws->ordered->data);
    + if (!gnt_ws_is_empty(wm->cws)) {
    + gnt_widget_destroy(gnt_ws_get_top_widget(wm->cws));
    ensure_normal_mode(wm);
    }
    @@ -622,7 +620,7 @@
    static gboolean
    window_list_key_pressed(GntWidget *widget, const char *text, GntWM *wm)
    {
    - if (text[1] == 0 && wm->cws->ordered) {
    + if (text[1] == 0 && !gnt_ws_is_empty(wm->cws)) {
    GntBindable *sel = gnt_tree_get_selection_data(GNT_TREE(widget));
    switch (text[0]) {
    case '-':
    @@ -664,10 +662,11 @@
    populate_window_list(wm, workspace);
    - if (wm->cws->ordered)
    - gnt_tree_set_selected(GNT_TREE(tree), wm->cws->ordered->data);
    - else if (workspace)
    + if (!gnt_ws_is_empty(wm->cws)) {
    + gnt_tree_set_selected(GNT_TREE(tree), gnt_ws_get_top_widget(wm->cws));
    + } else if (workspace) {
    gnt_tree_set_selected(GNT_TREE(tree), wm->cws);
    + }
    g_signal_connect(G_OBJECT(tree), "activate", G_CALLBACK(window_list_activate), wm);
    g_signal_connect(G_OBJECT(tree), "key_pressed", G_CALLBACK(window_list_key_pressed), wm);
    @@ -688,8 +687,9 @@
    if (wm->_list.window || wm->menu)
    return TRUE;
    - if (!wm->cws->ordered)
    + if (gnt_ws_is_empty(wm->cws)) {
    return TRUE;
    + }
    list_of_windows(wm, FALSE);
    @@ -887,8 +887,8 @@
    all = g_list_delete_link(all, list);
    wm->cws->list = all;
    gnt_ws_draw_taskbar(wm->cws, FALSE);
    - if (wm->cws->ordered) {
    - GntWidget *w = wm->cws->ordered->data;
    + if (!gnt_ws_is_empty(wm->cws)) {
    + GntWidget *w = gnt_ws_get_top_widget(wm->cws);
    GntNode *node = g_hash_table_lookup(wm->nodes, w);
    top_panel(node->panel);
    update_panels();
    @@ -903,10 +903,11 @@
    if (wm->_list.window)
    return TRUE;
    - if(!wm->cws->ordered)
    - return FALSE;
    + if (gnt_ws_is_empty(wm->cws)) {
    + return TRUE;
    + }
    - shift_window(wm, wm->cws->ordered->data, -1);
    + shift_window(wm, gnt_ws_get_top_widget(wm->cws), -1);
    return TRUE;
    }
    @@ -918,10 +919,11 @@
    if (wm->_list.window)
    return TRUE;
    - if(!wm->cws->ordered)
    - return FALSE;
    + if (gnt_ws_is_empty(wm->cws)) {
    + return TRUE;
    + }
    - shift_window(wm, wm->cws->ordered->data, 1);
    + shift_window(wm, gnt_ws_get_top_widget(wm->cws), 1);
    return TRUE;
    }
    @@ -1055,8 +1057,9 @@
    ensure_normal_mode(GntWM *wm)
    {
    if (wm->mode != GNT_KP_MODE_NORMAL) {
    - if (wm->cws->ordered)
    - window_reverse(wm->cws->ordered->data, FALSE, wm);
    + if (!gnt_ws_is_empty(wm->cws)) {
    + window_reverse(gnt_ws_get_top_widget(wm->cws), FALSE, wm);
    + }
    wm->mode = GNT_KP_MODE_NORMAL;
    }
    }
    @@ -1067,11 +1070,12 @@
    GntWM *wm = GNT_WM(bindable);
    if (wm->_list.window || wm->menu)
    return TRUE;
    - if (!wm->cws->ordered)
    + if (gnt_ws_is_empty(wm->cws)) {
    return TRUE;
    + }
    wm->mode = GNT_KP_MODE_MOVE;
    - window_reverse(GNT_WIDGET(wm->cws->ordered->data), TRUE, wm);
    + window_reverse(gnt_ws_get_top_widget(wm->cws), TRUE, wm);
    return TRUE;
    }
    @@ -1082,11 +1086,12 @@
    GntWM *wm = GNT_WM(bindable);
    if (wm->_list.window || wm->menu)
    return TRUE;
    - if (!wm->cws->ordered)
    + if (gnt_ws_is_empty(wm->cws)) {
    return TRUE;
    + }
    wm->mode = GNT_KP_MODE_RESIZE;
    - window_reverse(GNT_WIDGET(wm->cws->ordered->data), TRUE, wm);
    + window_reverse(gnt_ws_get_top_widget(wm->cws), TRUE, wm);
    return TRUE;
    }
    @@ -1120,7 +1125,7 @@
    g_hash_table_foreach(wm->nodes, (GHFunc)refresh_node, GINT_TO_POINTER(TRUE));
    g_signal_emit(wm, signals[SIG_TERMINAL_REFRESH], 0);
    - for (iter = g_list_last(wm->cws->ordered); iter; iter = iter->prev) {
    + for (iter = gnt_ws_get_last(wm->cws); iter; iter = iter->prev) {
    GntWidget *w = iter->data;
    GntNode *node = g_hash_table_lookup(wm->nodes, w);
    top_panel(node->panel);
    @@ -1175,9 +1180,10 @@
    GntWM *wm = GNT_WM(b);
    GntWidget *widget;
    - if (!wm->cws->ordered)
    + if (gnt_ws_is_empty(wm->cws)) {
    return FALSE;
    - widget = wm->cws->ordered->data;
    + }
    + widget = gnt_ws_get_top_widget(wm->cws);
    if (g_list_find(wm->tagged, widget)) {
    remove_tag(widget, wm);
    @@ -1342,10 +1348,11 @@
    GntWM *wm = GNT_WM(bindable);
    GntWidget *widget;
    - if(!wm->cws->ordered)
    + if (gnt_ws_is_empty(wm->cws)) {
    return FALSE;
    + }
    - widget = wm->cws->ordered->data;
    + widget = gnt_ws_get_top_widget(wm->cws);
    return help_for_bindable(wm,GNT_BINDABLE(widget));
    }
    @@ -1356,10 +1363,11 @@
    GntWM *wm = GNT_WM(bindable);
    GntWidget *widget;
    - if (!wm->cws->ordered)
    + if (gnt_ws_is_empty(wm->cws)) {
    return TRUE;
    + }
    - widget = wm->cws->ordered->data;
    + widget = gnt_ws_get_top_widget(wm->cws);
    if (!GNT_IS_BOX(widget))
    return TRUE;
    @@ -1611,8 +1619,8 @@
    gnt_ws_draw_taskbar(wm->cws, TRUE);
    update_screen(wm);
    - if (wm->cws->ordered) {
    - gnt_wm_raise_window(wm, wm->cws->ordered->data);
    + if (!gnt_ws_is_empty(wm->cws)) {
    + gnt_wm_raise_window(wm, gnt_ws_get_top_widget(wm->cws));
    }
    if (act && g_list_find(act, wm->cws)) {
    @@ -1701,10 +1709,11 @@
    if (wm->windows == NULL)
    return;
    - if (wm->cws->ordered && wid == wm->cws->ordered->data)
    + if (gnt_ws_is_top_widget(wm->cws, wid)) {
    flag |= GNT_TEXT_FLAG_DIM;
    - else if (gnt_widget_get_is_urgent(wid))
    + } else if (gnt_widget_get_is_urgent(wid)) {
    flag |= GNT_TEXT_FLAG_BOLD;
    + }
    gnt_tree_set_row_flags(GNT_TREE(wm->windows->tree), wid, flag);
    }
    @@ -1794,12 +1803,11 @@
    ws = new_widget_find_workspace(wm, widget);
    }
    node->ws = ws;
    - ws->list = g_list_append(ws->list, widget);
    - ws->ordered = g_list_append(ws->ordered, widget);
    + gnt_ws_append_widget(ws, widget);
    }
    if (wm->event_stack || node->me == wm->_list.window ||
    - node->me == ws->ordered->data) {
    + gnt_ws_is_top_widget(ws, node->me)) {
    gnt_wm_raise_window(wm, node->me);
    } else {
    bottom_panel(node->panel); /* New windows should not grab focus */
    @@ -1876,14 +1884,14 @@
    pos = g_list_index(s->list, widget);
    if (pos != -1) {
    - s->list = g_list_remove(s->list, widget);
    - s->ordered = g_list_remove(s->ordered, widget);
    + gnt_ws_remove_widget(s, widget);
    - if (s->ordered && wm->cws == s)
    - gnt_wm_raise_window(wm, s->ordered->data);
    + if (!gnt_ws_is_empty(s) && wm->cws == s) {
    + gnt_wm_raise_window(wm, gnt_ws_get_top_widget(s));
    + }
    }
    - } else if (transient && wm->cws && wm->cws->ordered) {
    - gnt_wm_update_window(wm, wm->cws->ordered->data);
    + } else if (transient && wm->cws && !gnt_ws_is_empty(wm->cws)) {
    + gnt_wm_update_window(wm, gnt_ws_get_top_widget(wm->cws));
    }
    gnt_ws_draw_taskbar(wm->cws, FALSE);
    @@ -1908,7 +1916,10 @@
    return TRUE;
    }
    }
    - return wm->cws->ordered ? gnt_widget_key_pressed(GNT_WIDGET(wm->cws->ordered->data), keys) : FALSE;
    + if (gnt_ws_is_empty(wm->cws)) {
    + return FALSE;
    + }
    + return gnt_widget_key_pressed(gnt_ws_get_top_widget(wm->cws), keys);
    }
    if (gnt_bindable_perform_action_key(GNT_BINDABLE(wm), keys)) {
    @@ -1916,10 +1927,10 @@
    }
    /* Do some manual checking */
    - if (wm->cws->ordered && wm->mode != GNT_KP_MODE_NORMAL) {
    + if (!gnt_ws_is_empty(wm->cws) && wm->mode != GNT_KP_MODE_NORMAL) {
    int xmin = 0, ymin = 0, xmax = getmaxx(stdscr), ymax = getmaxy(stdscr) - 1;
    int x, y, w, h;
    - GntWidget *widget = GNT_WIDGET(wm->cws->ordered->data);
    + GntWidget *widget = gnt_ws_get_top_widget(wm->cws);
    int ox, oy, ow, oh;
    gnt_widget_get_position(widget, &x, &y);
    @@ -1995,8 +2006,8 @@
    ret = gnt_widget_key_pressed(GNT_WIDGET(wm->menu), keys);
    else if (wm->_list.window)
    ret = gnt_widget_key_pressed(wm->_list.window, keys);
    - else if (wm->cws->ordered) {
    - GntWidget *win = wm->cws->ordered->data;
    + else if (!gnt_ws_is_empty(wm->cws)) {
    + GntWidget *win = gnt_ws_get_top_widget(wm->cws);
    if (GNT_IS_WINDOW(win)) {
    GntMenu *menu = gnt_window_get_menu(GNT_WINDOW(win));
    if (menu) {
    @@ -2139,15 +2150,11 @@
    if (!node)
    return;
    - if (widget != wm->_list.window && !GNT_IS_MENU(widget) &&
    - wm->cws->ordered->data != widget) {
    - GntWidget *w = wm->cws->ordered->data;
    - wm->cws->ordered = g_list_bring_to_front(wm->cws->ordered, widget);
    - gnt_widget_set_focus(w, FALSE);
    + gnt_widget_set_is_urgent(widget, FALSE);
    + if (widget != wm->_list.window && !GNT_IS_MENU(widget)) {
    + gnt_ws_bring_to_front(wm->cws, widget);
    }
    - gnt_widget_set_focus(widget, TRUE);
    - gnt_widget_set_is_urgent(widget, FALSE);
    gnt_widget_draw(widget);
    top_panel(node->panel);
    @@ -2208,14 +2215,7 @@
    g_return_if_fail(wm->cws != NULL);
    - if (widget != wm->cws->ordered->data) {
    - GntWidget *wid = wm->cws->ordered->data;
    - wm->cws->ordered = g_list_bring_to_front(wm->cws->ordered, widget);
    - gnt_widget_set_focus(wid, FALSE);
    - gnt_widget_draw(wid);
    - }
    - gnt_widget_set_focus(widget, TRUE);
    - gnt_widget_draw(widget);
    + gnt_ws_bring_to_front(wm->cws, widget);
    g_signal_emit(wm, signals[SIG_GIVE_FOCUS], 0, widget);
    }
    --- a/gntws.c Wed Apr 24 04:24:43 2019 +0000
    +++ b/gntws.c Wed Apr 24 21:54:50 2019 -0400
    @@ -219,3 +219,67 @@
    return priv->name;
    }
    +/* Internal. */
    +gboolean gnt_ws_is_empty(GntWS *ws)
    +{
    + g_return_val_if_fail(GNT_IS_WS(ws), TRUE);
    + return ws->ordered == NULL;
    +}
    +
    +/* Internal. */
    +gboolean gnt_ws_is_single(GntWS *ws)
    +{
    + g_return_val_if_fail(GNT_IS_WS(ws), FALSE);
    + return ws->ordered != NULL && ws->ordered->next == NULL;
    +}
    +
    +/* Internal. */
    +GntWidget *
    +gnt_ws_get_top_widget(GntWS *ws)
    +{
    + g_return_val_if_fail(GNT_IS_WS(ws), NULL);
    + return ws->ordered ? ws->ordered->data : NULL;
    +}
    +
    +/* Internal. */
    +gboolean
    +gnt_ws_is_top_widget(GntWS *ws, GntWidget *widget)
    +{
    + g_return_val_if_fail(GNT_IS_WS(ws), FALSE);
    + return ws->ordered && ws->ordered->data == widget;
    +}
    +
    +/* Internal. */
    +GList *
    +gnt_ws_get_last(GntWS *ws)
    +{
    + g_return_val_if_fail(GNT_IS_WS(ws), NULL);
    + return ws->ordered ? g_list_last(ws->ordered) : NULL;
    +}
    +
    +/* Internal.
    + * Different from gnt_ws_add_widget in that it doesn't modify focus. */
    +void
    +gnt_ws_append_widget(GntWS *ws, GntWidget *widget)
    +{
    + g_return_if_fail(GNT_IS_WS(ws));
    + ws->list = g_list_append(ws->list, widget);
    + ws->ordered = g_list_append(ws->ordered, widget);
    +}
    +
    +/* Internal. */
    +void
    +gnt_ws_bring_to_front(GntWS *ws, GntWidget *widget)
    +{
    + g_return_if_fail(GNT_IS_WS(ws));
    +
    + if (widget != ws->ordered->data) {
    + GntWidget *old_widget = ws->ordered->data;
    + ws->ordered = g_list_remove(ws->ordered, widget);
    + ws->ordered = g_list_prepend(ws->ordered, widget);
    + gnt_widget_set_focus(old_widget, FALSE);
    + gnt_widget_draw(old_widget);
    + }
    + gnt_widget_set_focus(widget, TRUE);
    + gnt_widget_draw(widget);
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/gntwsprivate.h Wed Apr 24 21:54:50 2019 -0400
    @@ -0,0 +1,42 @@
    +/*
    + * GNT - The GLib Ncurses Toolkit
    + *
    + * GNT is the legal property of its developers, whose names are too numerous
    + * to list here. Please refer to the COPYRIGHT file distributed with this
    + * source distribution.
    + *
    + * This library is free software; you can redistribute it and/or modify
    + * it under the terms of the GNU General Public License as published by
    + * the Free Software Foundation; either version 2 of the License, or
    + * (at your option) any later version.
    + *
    + * This program is distributed in the hope that it will be useful,
    + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    + * GNU General Public License for more details.
    + *
    + * You should have received a copy of the GNU General Public License along with
    + * this program; if not, write to the Free Software Foundation, Inc.,
    + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
    + */
    +
    +#ifndef GNT_WS_PRIVATE_H
    +#define GNT_WS_PRIVATE_H
    +
    +#include "gnt.h"
    +#include "gntws.h"
    +
    +G_BEGIN_DECLS
    +
    +/* Private access to some internals. Contact us if you need these. */
    +gboolean gnt_ws_is_empty(GntWS *ws);
    +gboolean gnt_ws_is_single(GntWS *ws);
    +GntWidget *gnt_ws_get_top_widget(GntWS *ws);
    +gboolean gnt_ws_is_top_widget(GntWS *ws, GntWidget *widget);
    +GList *gnt_ws_get_last(GntWS *ws);
    +void gnt_ws_append_widget(GntWS *ws, GntWidget *widget);
    +void gnt_ws_bring_to_front(GntWS *ws, GntWidget *widget);
    +
    +G_END_DECLS
    +
    +#endif /* GNT_WS_PRIVATE_H */
    --- a/wms/irssi.c Wed Apr 24 04:24:43 2019 +0000
    +++ b/wms/irssi.c Wed Apr 24 21:54:50 2019 -0400
    @@ -43,6 +43,8 @@
    #include "gntwindow.h"
    #include "gntlabel.h"
    +#include "gntwsprivate.h"
    +
    #define GNT_TYPE_IRSSI_WM gnt_irssi_wm_get_type()
    G_DECLARE_FINAL_TYPE(GntIrssiWM, gnt_irssi_wm, GNT, IRSSI_WM, GntWM)
    @@ -267,8 +269,9 @@
    int x, y, w, h;
    GntWidget *win;
    - if (wm->cws->ordered == NULL || is_budddylist(win = GNT_WIDGET(wm->cws->ordered->data)))
    + if (gnt_ws_is_empty(wm->cws) || is_budddylist(win = gnt_ws_get_top_widget(wm->cws))) {
    return FALSE;
    + }
    find_window_position(irssi, win, &hor, &vert);