繁体   English   中英

如何使用C ++在gtkmm:gtk :: Listbox中添加文本框

[英]how to add text box in gtkmm:gtk::Listbox using c++

我想在C ++中使用Gtkmm在列表框中添加这种类型的文本。

带有文本的列表框

您能以任何方式建议我如何做吗?

这是我创建的一个用于制作如上所述的ListBox的示例。

//=======================================================
// GTKMM3/C++11 ListBox Example / W.Moore
//=======================================================
#include <gtkmm.h>
#include <iostream>
#include <string>
#include <vector>
#include <initializer_list>

using namespace std;

//======================================================
// My List Row
//======================================================
class MyListRow : public Gtk::ListBoxRow
{
public:
    MyListRow(const string text)
      : label{text}
    {
       add(label);
       set_halign(Gtk::Align::ALIGN_START);
       label.set_size_request(150); 
       show_all_children();
    }
protected:
    Gtk::Label label;
};

//======================================================
// My List Box
//======================================================
class MyListBox : public Gtk::ListBox
{
public:
    MyListBox();
    MyListBox(initializer_list<string> list);
    void api_AddRow(string line);
    void api_Clear();
};

inline MyListBox::MyListBox()
{
}

inline MyListBox::MyListBox(initializer_list<string> list) {
  for (auto s : list) {
    api_AddRow(s);
  }
}

inline void MyListBox::api_AddRow(string text)
{
    auto row = Gtk::manage(new MyListRow{text});
    append(*row);
    row->show();
}

inline void MyListBox::api_Clear() {
    vector<Gtk::Widget* w> children = get_children();
    foreach (widget* w : children) {
        remove(*w);
        delete w;
    }
}

//======================================================
// My Window
//======================================================
class MyWindow : public Gtk::Window {
  public:
    MyWindow();

  protected:
    MyListBox listbox1 {
        "List Item 1",
        "List Item 2",
        "List Item 3",
        "List Item 4",
        "List Item 5",
        "List Item 6",
        "List Item 7"
    };

};

inline MyWindow::MyWindow() {
  add(listbox1);
  set_title("ListBox Example");
  set_border_width(6);
  set_default_size(300, 100);

  show_all_children();
}

//======================================================
// Main
//======================================================
int main(int argc, char** argv) {
    auto appMain = Gtk::Application::create(argc, argv, "org.gtkmm.example");
    MyWindow MyWindow1;
    appMain->run(MyWindow1);
    return 0;
}

上面的内容仍然不是很有用,因为它缺少像标准Windows文本框这样的滚动条,如果您将其填满,则会使窗口长于屏幕。 这是赋予它滚动条的代码:

//=======================================================
// GTKMM3/C++11 ListBox Example / W.Moore
//=======================================================
#include <gtkmm.h>
#include <iostream>
#include <string>
#include <sstream>
#include <initializer_list>

using namespace std;

//======================================================
// My List Row
//======================================================
class MyListRow : public Gtk::ListBoxRow
{
public:
    MyListRow(const string text)
      : c1{text}
    {
       add(c1);
       set_halign(Gtk::Align::ALIGN_START);
       show_all_children();
    }
    Gtk::Label c1;
};

//======================================================
// List Box with scrollbars
//======================================================
class ListBoxScroll : public Gtk::ScrolledWindow {
  public:
    ListBoxScroll();
    ListBoxScroll(initializer_list<string> list);
    void api_AddRow(string line);
    void api_AddRows(vector<string>& lines);
    void api_Clear();

    function<void(string label)> fun_selected;

protected:
    Gtk::Box               hbox {Gtk::ORIENTATION_HORIZONTAL};
    Gtk::ListBox           listbox;

    void sig_row_selected(Gtk::ListBoxRow* listboxrow);
};

//======================================================
// ListBoxScroll
//======================================================
inline ListBoxScroll::ListBoxScroll()
{
    add(hbox);
    hbox.pack_start(listbox, Gtk::PackOptions::PACK_EXPAND_WIDGET);
    listbox.signal_row_selected().connect(sigc::mem_fun(*this, ListBoxScroll::sig_row_selected));
    show_all_children();
}

inline ListBoxScroll::ListBoxScroll(initializer_list<string> list) {
  for (auto s : list) {
    api_AddRow(s);
  }
}

inline void ListBoxScroll::api_Clear() {
  listbox.unselect_all();
  vector<Gtk::Widget*> children = listbox.get_children();
  for (Widget* w : children) {
    listbox.remove(*w);
    delete w;
  }
}

inline void ListBoxScroll::api_AddRow(string text)
{
    auto row = Gtk::manage(new MyListRow{text});
    listbox.append(*row);
    row->show();
}

inline void ListBoxScroll::api_AddRows(vector<string>& lines) {
  for (string& L : lines) {
    api_AddRow(L);
  }
}

inline void ListBoxScroll::sig_row_selected(Gtk::ListBoxRow* listboxrow) {
  if (fun_selected && (listboxrow != nullptr)) {
    MyListRow* listrow = (MyListRow*)listboxrow;
    Glib::ustring us = listrow->c1.get_text();
    fun_selected(us.c_str());
  }
}


class WnViewer : public Gtk::Window {
  public:
    WnViewer();
    ~WnViewer();

  private:
    Gtk::Box          m_vbox       {Gtk::ORIENTATION_VERTICAL};
    Gtk::Box          m_box        {Gtk::ORIENTATION_HORIZONTAL};
    Gtk::Box          m_boxleft    {Gtk::ORIENTATION_VERTICAL};
    Gtk::Box          m_boxbtn     {Gtk::ORIENTATION_HORIZONTAL};
    Gtk::Button       m_btnClear   {"Clear"};
    Gtk::Button       m_btnRefresh {"Refresh"};
    ListBoxScroll     m_listbox;
    Gtk::TextView     m_textview;
};

inline WnViewer::WnViewer() {
  set_title("Viewer");
  set_border_width(6);
  set_default_size(600, 600);

  add(m_vbox);
  m_vbox.pack_start(m_boxbtn, Gtk::PackOptions::PACK_SHRINK);
  m_vbox.pack_start(m_box);
  m_boxbtn.pack_start(m_btnClear,   Gtk::PackOptions::PACK_SHRINK);
  m_boxbtn.pack_start(m_btnRefresh, Gtk::PackOptions::PACK_SHRINK);

  m_box     .pack_start(m_boxleft,  Gtk::PackOptions::PACK_SHRINK);
  m_box     .pack_start(m_textview, Gtk::PackOptions::PACK_EXPAND_WIDGET);
  m_boxleft .pack_start(m_listbox,  Gtk::PackOptions::PACK_EXPAND_WIDGET);
  m_listbox.set_size_request(200);


  for(int i=0; i < 30; i++) {
    stringstream x;
    x << "Testing " << i << "\n";
    m_listbox.api_AddRow(x.str().c_str());
  }

  auto lamba_clear = [&]() {
      m_listbox.api_Clear();
  };

  auto lamba_refresh = [&]() {
    for(int i=0; i < 30; i++) {
      stringstream x;
      x << "Testing " << i << "\n";
      m_listbox.api_AddRow(x.str().c_str());
    }
  };

  m_btnClear   .signal_clicked().connect(lamba_clear);
  m_btnRefresh .signal_clicked().connect(lamba_refresh);

  m_listbox.fun_selected = [](string label) {
      cout << "selected:" << label << "\n";
  };

  show_all_children();
}

inline WnViewer::~WnViewer() {

}

//======================================================
// Main
//======================================================
int main(int argc, char** argv) {
    auto appMain = Gtk::Application::create(argc, argv, 
                       "org.gtkmm.example");
    WnViewer WnViewer1;
    appMain->run(WnViewer1);
    return 0;
}

我认为gtkmm3确实很好用。 但是您需要围绕所有组件创建包装器,以使它们像常规的c#小部件一样工作,因为它们是比c#.net小部件更小的构建基块(无论如何我还是认为)。

一些松散的结局:

  • 在此示例中,如何使标签水平宽度缩小到最小标签高度?
  • 如何使垂直滚动条始终看起来像标准窗口滚动条?

(真的希望Gnome / Gtk的人们在gtk中添加如下所示的复合小部件,而无需每次有人需要像c#/。net这样的简单列表框时都重新发明轮子的方法。)

这是上面的一个更详细的示例:

#ifndef WIDGET_LISTBOX_H
#define WIDGET_LISTBOX_H

//=======================================================
// GTKMM3 ListBox Example: C++11 / W.Moore
//=======================================================
#include <gtkmm.h>
#include <iostream>
#include <string>
#include <initializer_list>

using namespace std;

//======================================================
// List Row
//======================================================
class ListRow : public Gtk::ListBoxRow
{
public:
    ListRow(const string text) : c1{text} {
       add(c1);
       set_halign(Gtk::Align::ALIGN_START);
       show_all_children();
    }
    Gtk::Label c1;
};

//======================================================
// List Box with scrollbars
//======================================================
class ListBoxScroll : public Gtk::ScrolledWindow {
  public:
    ListBoxScroll();
    ListBoxScroll(initializer_list<string> list);

    void           api_AddRow(string line);
    void           api_AddRows(vector<string>& lines);
    void           api_Clear();
    void           api_Focus();
    vector<string> api_GetAll();
    int            api_GetCount();
    string         api_GetAt(int i);
    string         api_GetSelected();
    void           api_DelSelected();
    void           api_DelAll();

    function<void(string label)> fun_selected;   //single-click or cursor selection
    function<void(string label)> fun_activated;  //double-click

protected:
    Gtk::Box               hbox {Gtk::ORIENTATION_HORIZONTAL};
    Gtk::ListBox           listbox;
    void sig_row_selected  (Gtk::ListBoxRow* listboxrow);
    void sig_row_activated (Gtk::ListBoxRow* listboxrow);

};

inline void ListBoxScroll::api_Focus() {
  grab_focus();
  listbox.grab_focus();
}

//======================================================
// ListBoxScroll: Constructor 1
//======================================================
inline ListBoxScroll::ListBoxScroll()
{
    add(hbox);
    hbox.pack_start(listbox, Gtk::PackOptions::PACK_EXPAND_WIDGET);

    //make double click active row
    listbox.set_activate_on_single_click(false);

    listbox.signal_row_activated().connect(sigc::mem_fun(
                    *this,
                    ListBoxScroll::sig_row_activated)
    );
    listbox.signal_row_selected().connect(sigc::mem_fun(
                    *this,
                    ListBoxScroll::sig_row_selected)
    );
    listbox.set_can_focus();
    show_all_children();

}

//======================================================
// ListBoxScroll: Constructor 2
//======================================================
inline ListBoxScroll::ListBoxScroll(initializer_list<string> list) {
  for (auto s : list) {
    api_AddRow(s);
  }
}

//======================================================
// ListBoxScroll: Delete All
//======================================================
inline void ListBoxScroll::api_DelAll() {
  api_Clear();
}

//======================================================
// ListBoxScroll: Get All
//======================================================
inline vector<string> ListBoxScroll::api_GetAll() {
  vector<Gtk::Widget*> children = listbox.get_children();
  vector<string>       strlist;

  for (Widget* row : children) {
    ListRow* rowi = (ListRow*)row;
    strlist.push_back(rowi->c1.get_text().c_str());
  }

  return strlist;
}

//======================================================
// ListBoxScroll: Get At
//======================================================
inline string ListBoxScroll::api_GetAt(int i) {
  vector<Gtk::Widget*> children = listbox.get_children();
  Widget*  row  = children.at(i);
  ListRow* rowi = (ListRow*)row;
  return rowi->c1.get_text().c_str();
}

//======================================================
// ListBoxScroll: Get Count
//======================================================
inline int ListBoxScroll::api_GetCount() {
  vector<Gtk::Widget*> children = listbox.get_children();
  return (int) children.size();
}

//======================================================
// ListBoxScroll: Delete Selected
//======================================================
inline void ListBoxScroll::api_DelSelected() {
  ListRow* row_selected = (ListRow*) listbox.get_selected_row();

  //Hackishly find selected position since GTK lacks a better way...
  vector<Gtk::Widget*> wrow = listbox.get_children();
  int row_posi              = -1;
  int row_count             = (int) wrow.size();
  for(int i = 0; i <row_count; i++) {
    ListRow* row_i = (ListRow*)wrow[i];
    //cout << "row_i:" << i << " (" << row_i->c1.get_text() << ")\n";
    if (row_i == row_selected) {
        row_posi = i;
        break;
    }
  }
  //cout << "row_pos:" << row_posi << "\n";

  if (row_posi != -1) {

    // Unselect Current
    listbox.unselect_row();

    Gtk::ListBoxRow* select_next = nullptr;

    // Move Selection Line by One
    if ((row_posi + 1) < row_count) {
      select_next = (Gtk::ListBoxRow*)wrow[row_posi+1];
    }
    else if ((row_posi-1) >= 0) {
      select_next = (Gtk::ListBoxRow*)wrow[row_posi-1];
    }

    // Remove Initially Selected
    //const Glib::RefPtr<Gtk::Adjustment> hadj = get_hadjustment(); //save hpos before remove
    const Glib::RefPtr<Gtk::Adjustment> vadj = get_vadjustment(); //save hpos before remove
    double vvalue = vadj->get_value();

    listbox.remove(*row_selected);
    delete row_selected;

    // On Remove, for some reason...
    // the widget loses focus and vadjustment gets reset
    vadj->set_value(vvalue);
    listbox.grab_focus(); //need this?
    listbox.select_row(*select_next);
    select_next->grab_focus();
  }
}

//======================================================
// ListBoxScroll: Get Selected
//======================================================
inline string ListBoxScroll::api_GetSelected() {
  Gtk::ListBoxRow* row = listbox.get_selected_row();
  ListRow* rowi = (ListRow*) row;
  return rowi->c1.get_text().c_str();
}

//======================================================
// ListBoxScroll: Clear
//======================================================
inline void ListBoxScroll::api_Clear() {
  vector<Gtk::Widget*> children = listbox.get_children();
  listbox.unselect_all();
  for (Widget* w1 : children) {
    listbox.remove(*w1);
  }

  //Allow GUI to update
  while (gtk_events_pending()) {
    gtk_main_iteration_do(false);
  }

  for (Widget* w2 : children) {
    delete w2;
  }

}

//======================================================
// ListBoxScroll: AddRow
//======================================================
inline void ListBoxScroll::api_AddRow(string text)
{
    auto row = Gtk::manage(new ListRow{text});
    listbox.append(*row);
    row->show();
}

//======================================================
// ListBoxScroll: AddRows
//======================================================
inline void ListBoxScroll::api_AddRows(vector<string>& lines) {
  for (string& L : lines) {
    api_AddRow(L);
  }
}

//======================================================
// ListBoxScroll: sig_row_selected
//======================================================
inline void ListBoxScroll::sig_row_selected(Gtk::ListBoxRow* listboxrow) {
  if (fun_selected && (listboxrow != nullptr)) {
    //cout << "<selected>\n";
    ListRow* listrow = (ListRow*)listboxrow;
    Glib::ustring us = listrow->c1.get_text();
    fun_selected(us.c_str());
  }
}

//======================================================
// ListBoxScroll: sig_row_activated
//======================================================
inline void ListBoxScroll::sig_row_activated(Gtk::ListBoxRow* listboxrow) {
  if (fun_activated && (listboxrow != nullptr)) {
    //cout << "<activated>\n";
    ListRow* listrow = (ListRow*)listboxrow;
    Glib::ustring us = listrow->c1.get_text();
    fun_activated(us.c_str());
  }
}


#endif // WIDGET_LIST_BOX_H

对于这种非常简单的情况,您可能需要使用gtkmm的ListViewText小部件: https : //developer.gnome.org/gtkmm/stable/classGtk_1_1ListViewText.html

这是一些示例代码: https : //git.gnome.org/browse/gtkmm-documentation/tree/examples/book/treeview/listviewtext/examplewindow.cc

但是,如果您想做更复杂的事情,则需要使用完整的Gtk :: TreeView类。

我认为Gtk :: ListBox不适合提供这样的简单值列表。 实际上,您可能想使用Gtk :: ComboBox-甚至可能使用简单的Gtk :: ComboBoxText小部件: https : //developer.gnome.org/gtkmm/stable/classGtk_1_1ComboBoxText.html

这是一些Gtk :: ComboBoxText示例代码: https : //git.gnome.org/browse/gtkmm-documentation/tree/examples/book/combobox/text/examplewindow.cc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM