学习Gtkmm系列之五

程序客户区的创建可谓仁者见仁,智者见智了。比较常用的就是使用hpaned控件来完成,左边为sidepane,右边位mainpane,这样将来可以根据实际应用的需要来采用不同的控件。我这里使用两个Button来演示。该控件采用属性来与菜单中的选项的使能状态一一对应。由于不太熟悉gtkmm,其资料也比较少,用了点时间来研究属性的用法(gtkmm源码都是对gtk的封装,不太适合)。

头文件代码如下:(特别注意构造函数的用法

class ClientRoot : public Gtk::HPaned                                              
{ 
 public:
  ClientRoot();
  virtual ~ClientRoot();
  Glib::PropertyProxy property_can_save();
  Glib::PropertyProxy property_can_save_as();
  Glib::PropertyProxy property_can_undo();
  Glib::PropertyProxy property_can_redo();
  Glib::PropertyProxy property_can_cut();
  Glib::PropertyProxy property_can_copy();
  Glib::PropertyProxy property_can_paste();
  Glib::PropertyProxy property_can_delete();
  Glib::PropertyProxy property_can_select_all();
 protected:
  void onTestProperties();
 private:
  Glib::Property prop_can_save;
  Glib::Property prop_can_save_as;
  Glib::Property prop_can_undo;
  Glib::Property prop_can_redo;
  Glib::Property prop_can_cut;
  Glib::Property prop_can_copy;
  Glib::Property prop_can_paste;
  Glib::Property prop_can_delete;
  Glib::Property prop_can_select_all;
  Gtk::Button m_SidePane;
  Gtk::Button m_MainPane;
};
实现文件关键代码如下:

ClientRoot::ClientRoot() 
  : Glib::ObjectBase (typeid (ClientRoot)),
    prop_can_save(*this, "can-save"),
    prop_can_save_as(*this, "can-save-as"),
    prop_can_undo(*this, "can-undo"),
    prop_can_redo(*this, "can-redo"),
    prop_can_cut(*this, "can-cut"),
    prop_can_copy(*this, "can-copy"),
    prop_can_paste(*this, "can-paste"),
    prop_can_delete(*this, "can-delete"),
    prop_can_select_all(*this, "can-select-all"),
    m_SidePane("SidePane"),
    m_MainPane("MainPane")
{ 
  pack1(m_SidePane, true, false);
  pack2(m_MainPane, true, false);
  m_MainPane.signal_clicked().connect(sigc::mem_fun(*this, &ClientRoot::onTestProperties));
} 

void ClientRoot::onTestProperties()
{ 
  static bool sState = false;
  sState = ! sState;
  
  prop_can_save = sState;
  prop_can_save_as = sState;
  prop_can_undo = sState;
  prop_can_redo = sState;
  prop_can_cut = sState;
  prop_can_copy = sState;
  prop_can_paste = sState;
  prop_can_delete = sState;
  prop_can_select_all = sState;
} 

Glib::PropertyProxy
ClientRoot::property_can_save()
{ 
  return prop_can_save.get_proxy();
}

Glib::PropertyProxy
ClientRoot::property_can_save_as()
{
  return prop_can_save_as.get_proxy();
}
...

信号连接方法如下:

m_ClientRoot.property_can_save().signal_changed().connect(
      sigc::mem_fun(*this, &SampleWindow::onCanSaveChanged));
  m_ClientRoot.property_can_save_as().signal_changed().connect(
      sigc::mem_fun(*this, &SampleWindow::onCanSaveAsChanged));
  m_ClientRoot.property_can_undo().signal_changed().connect(
      sigc::mem_fun(*this, &SampleWindow::onCanUndoChanged));
  m_ClientRoot.property_can_redo().signal_changed().connect(
      sigc::mem_fun(*this, &SampleWindow::onCanRedoChanged));
  m_ClientRoot.property_can_cut().signal_changed().connect(
      sigc::mem_fun(*this, &SampleWindow::onCanCutChanged));
  m_ClientRoot.property_can_copy().signal_changed().connect(
      sigc::mem_fun(*this, &SampleWindow::onCanCopyChanged));
  m_ClientRoot.property_can_paste().signal_changed().connect(
      sigc::mem_fun(*this, &SampleWindow::onCanPasteChanged));
  m_ClientRoot.property_can_delete().signal_changed().connect(
      sigc::mem_fun(*this, &SampleWindow::onCanDeleteChanged));                 
  m_ClientRoot.property_can_select_all().signal_changed().connect(
      sigc::mem_fun(*this, &SampleWindow::onCanSelectAllChanged));
回调函数只是简单的更改action的sensitivity,如下:

void 
SampleWindow::onCanSaveChanged()
{     
  Glib::RefPtr action = m_refNormalActionGroup->get_action("FileSave");
  g_assert(action != 0);
  action->set_sensitive(m_ClientRoot.property_can_save().get_value());
}



效果图如下



你可能感兴趣的:(Gtkmm)