如何启用gtk菜单长滚动?

时间:2019-05-23 17:21:57

标签: c menu gtk vertical-scrolling appindicator

我正在将程序从Linux Ubuntu 16.04移植到Ubuntu 18.04和19。我正在将appindicator-1.0与Gtk + 2.0菜单结合使用。菜单由常数项和许多动态添加的项组成。如果屏幕像素分辨率不如必须的那么好,那么最后的菜单项将不可见。当鼠标指针悬停在这些栏上时,Ubuntu 16.04自动使用“ ^”和“ v”箭头添加顶部和底部栏来滚动菜单。但是在Linux Ubuntu 18.04和19中却没有出现这样的滚动条!我在哪里可以解决这个问题?

似乎问题出在Gtk或Gnome设置中...但是dconf-editor中的Gtk设置在Ubuntu 16.04和18.04中看起来相同。 /etc/gtk+-2.0也一样。 〜/ .gtkrc-2.0不存在。另一方面,菜单滚动到带有菜单栏的常规GTK窗口中的任何位置都可以正常工作。 Ubuntu 16.04和Ubuntu 18.04使用相同的12.10.1 libappindicator版本。这是否意味着施药者有罪?

$ cat menu.c 
#include <gtk/gtk.h>
#include <libappindicator/app-indicator.h>

#define LOGO_PNG "/home/super/my-project/menu-test/logo.png"

AppIndicator* c_indicator;
GtkWidget* c_menu;

void menu_quit_cb(GtkMenuItem* menuitem, gpointer user_data)
{
    gtk_main_quit();
}

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);

    int i;
    GtkWidget* item;
    char title[128];

    c_menu = gtk_menu_new();

    item = gtk_menu_item_new_with_label("Quit");
    g_signal_connect(item, "activate", G_CALLBACK(menu_quit_cb), NULL);
    gtk_menu_shell_append(GTK_MENU_SHELL(c_menu), item);
    gtk_widget_show(item);

    gtk_menu_shell_append(GTK_MENU_SHELL(c_menu), gtk_separator_menu_item_new());

    for (i = 1; i <= 100; ++i) {
        snprintf(title, sizeof(title), "Item #%03u", i);
        item = gtk_menu_item_new_with_label(title);
        gtk_menu_shell_append(GTK_MENU_SHELL(c_menu), item);
        gtk_widget_show(item);
    }

    //

    c_indicator = app_indicator_new("Menu Test", LOGO_PNG, APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
    app_indicator_set_status(c_indicator, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_icon(c_indicator, LOGO_PNG);
    app_indicator_set_menu(c_indicator, GTK_MENU(c_menu));

    gtk_main();

    return 0;
}

我希望在Linux Ubuntu 16.04和18.04中具有相同的菜单行为。

新信息:

似乎是错误。我已向bugs.launchpad.netgitlab.gnome.org作了报告。

作为解决方法,我已连接到信号指示器的APP_INDICATOR_SIGNAL_SCROLL_EVENT信号,并实现了滚动,这是我的代码的一部分:

#include <gtk/gtk.h>
#include <libappindicator/app-indicator.h>
#include <time.h>

#define LOGO_PNG "/home/super/my-project/menu-test/logo.png"

#define AMOUNT_OF_CONSTANT_ITEMS    (2)
#define AMOUNT_OF_DYNAMIC_ITEMS     (75)

AppIndicator* c_indicator;
GtkWidget* c_menu;
GtkWidget* c_item[AMOUNT_OF_DYNAMIC_ITEMS] = { NULL };
gint c_item_first = 0; // GTK_MENU_ITEM(c_item[c_item_first]) is the first visible dynamic item

static struct timespec time_diff(struct timespec end, struct timespec start)
{
    struct timespec temp;
    if (end.tv_nsec < start.tv_nsec) {
        temp.tv_sec = end.tv_sec - start.tv_sec - 1;
        temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
    } else {
        temp.tv_sec = end.tv_sec - start.tv_sec;
        temp.tv_nsec = end.tv_nsec - start.tv_nsec;
    }
    return temp;
}

static gboolean not_elapsed_enough()
{
    static struct timespec last_moment = { 0, 0 };
    struct timespec current_moment;
    struct timespec temp;

    clock_gettime(CLOCK_MONOTONIC_COARSE, &current_moment);
    if (0 == last_moment.tv_sec && 0 == last_moment.tv_nsec) {
        last_moment = current_moment;
        return FALSE;
    }
    temp = time_diff(current_moment, last_moment);
    if (temp.tv_sec == 0 && temp.tv_nsec < 99999999)
        return TRUE; // do not enough time elapsed
    last_moment = current_moment;
    return FALSE;
}

void appind_scroll_event_cb(AppIndicator* indicator, gint delta, GdkScrollDirection direction, gpointer user_data)
{
    if (not_elapsed_enough()) return;

    if (GDK_SCROLL_UP == direction) {
        GtkWidget* item = c_item[c_item_first];
        g_object_ref(item); // do not destroy item togather with container
        gtk_container_remove(GTK_CONTAINER(c_menu), item); // remove item from menu
        gtk_menu_shell_insert(GTK_MENU_SHELL(c_menu), item, AMOUNT_OF_CONSTANT_ITEMS + AMOUNT_OF_DYNAMIC_ITEMS);
        g_object_unref(item);
        gtk_widget_show(item);
        ++c_item_first;
        if (c_item_first >= AMOUNT_OF_DYNAMIC_ITEMS)
            c_item_first = 0;
    } else if (GDK_SCROLL_DOWN == direction) {
        if (0 == c_item_first)
            c_item_first = AMOUNT_OF_DYNAMIC_ITEMS;
        --c_item_first;
        GtkWidget* item = c_item[c_item_first];
        g_object_ref(item);
        gtk_container_remove(GTK_CONTAINER(c_menu), item);
        gtk_menu_shell_insert(GTK_MENU_SHELL(c_menu), item, AMOUNT_OF_CONSTANT_ITEMS);
        g_object_unref(item);
        gtk_widget_show(item);
    }
}

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);

    int i;
    GtkWidget* item;
    char title[128];

    c_menu = gtk_menu_new();

    item = gtk_menu_item_new_with_label("Quit");
    g_signal_connect(item, "activate", G_CALLBACK(gtk_main_quit), NULL);
    gtk_menu_shell_append(GTK_MENU_SHELL(c_menu), item);
    gtk_widget_show(item);

    item = gtk_menu_item_new_with_label("---------------------");
    gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
    gtk_menu_shell_append(GTK_MENU_SHELL(c_menu), item);
    gtk_widget_show(item);

    for (i = 1; i <= AMOUNT_OF_DYNAMIC_ITEMS; ++i) {
        snprintf(title, sizeof(title), "Item #%03u", i);
        item = gtk_menu_item_new_with_label(title);
        gtk_menu_shell_append(GTK_MENU_SHELL(c_menu), item);
        gtk_widget_show(item);
        c_item[i - 1] = item;
    }

    //

    c_indicator = app_indicator_new("Menu Test", LOGO_PNG, APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
    app_indicator_set_status(c_indicator, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_icon(c_indicator, LOGO_PNG);
    app_indicator_set_menu(c_indicator, GTK_MENU(c_menu));
    g_signal_connect(c_indicator, APP_INDICATOR_SIGNAL_SCROLL_EVENT, G_CALLBACK(appind_scroll_event_cb), NULL);

    gtk_main();

    return 0;
}

将鼠标光标放在指示器的菜单图标上时,可使用滚轮按周期向上或向下滚动配置菜单项。一方面,这不是标准行为,客户无法凭借自己的经验来猜测。另一方面,仅滚动动态菜单项。项目顺序保存在菜单显示之间。菜单顶部的静态(常量)项目不会向上或向下移动。在某些情况下,这可能非常方便。

0 个答案:

没有答案