在 GTK+ 中部分突出显示一个小部件

Highlight a widget partially in GTK+

我的部分界面中有一个列表框,我想按进度单独突出显示 GtkListBoxRows。你加载了几个文件,我的程序分别处理每个文件,我想像进度条一样突出显示列表框行。它非常类似于进度条,只是里面的内容是按钮和一些文本。是否有允许重新着色的特定 Cairo/Pango 函数?

我这里有一个使用 Gtkmm 的解决方案(应该很容易翻译成 C)。我有一系列的 5 个按钮在容器内水平对齐,还有一个“取得进展”按钮。单击它时,容器中的子按钮会更新以显示进度:

#include <iostream>
#include <gtkmm.h>

class MainWindow : public Gtk::ApplicationWindow
{

public:

    MainWindow();

private:

    Gtk::Grid m_container;
    Gtk::Button m_progressButton;

    int m_progressTracker = 0;
};

MainWindow::MainWindow()
: m_progressButton("Make progress...")
{
    // Add five buttons to the container (horizontally):
    for(int index = 0; index < 5; ++index)
    {
        Gtk::Button* button = Gtk::make_managed<Gtk::Button>("B" + std::to_string(index));
        m_container.attach(*button, index, 0, 1, 1);
    }

    // Add a button to control progress:
    m_container.attach(m_progressButton, 0, 1, 5, 1);

    // Add handler to the progress button.
    m_progressButton.signal_clicked().connect(
        // Each time the button is clicked, the "hilighting" of the buttons
        // in the container progresses until completed:
        [this]()
        {
            Gtk::Widget* child = m_container.get_child_at(m_progressTracker, 0);
            if(child != nullptr)
            {
                std::cout << "Making progress ..." << std::endl;

                // Change the button's background color:
                Glib::RefPtr<Gtk::CssProvider> cssProvider = Gtk::CssProvider::create();
                cssProvider->load_from_data("button {background-image: image(cyan);}");
                child->get_style_context()->add_provider(cssProvider, GTK_STYLE_PROVIDER_PRIORITY_USER);
    
                // Update for next child...
                ++m_progressTracker;
            }
        }
    );

    // Make m_container a child of the window:
    add(m_container);
}

int main(int argc, char *argv[])
{
    std::cout << "Gtkmm version : " << gtk_get_major_version() << "."
                                    << gtk_get_minor_version() << "."
                                    << gtk_get_micro_version() << std::endl;

    auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
  
    MainWindow window;
    window.show_all();
  
    return app->run(window);
}

在你的情况下,你将不得不调整容器和信号(也许你需要其他东西来触发重绘),但就更改背景颜色而言,它应该工作得差不多。您可以通过以下方式构建它(使用 Gtkmm 3.24):

g++ main.cpp -o example.out `pkg-config --cflags --libs gtkmm-3.0`