GTKmm 練習筆記(一)關於BOX容器的應用

来源:互联网 发布:java电子商务平台源码 编辑:程序博客网 时间:2024/05/18 20:52

gtkmm的GUI開發上最大的困難點在於文檔與範例稀少,學習的來源有限,故在自己學習的路上希望能做些筆記,

這一系列筆記一方面為了強化自己的記憶,另一方面給有興趣一同研究這套跨平台GUI庫的同好一點參考的來源。

如果尚不知道如何架設環境的朋友可以看我的另一篇文章,"gtkmm環境架設筆記"。

這次主要紀錄練習關於BOX容器的應用,範例檔來自於gtkmm開發團隊在github上的box容器範例,我會在源碼中在難以理解的地方加上註釋,並對原有官方

註釋進行翻譯。

範例連結

執行範例時要傳入參數,共有1,2,3種選項。

擷取畫面一


擷取畫面二


擷取畫面三


開始進入正題

首先是頭文件 "examplewindow.h",用於定義主要窗口的構成

//$Id: examplewindow.h 705 2006-07-19 02:55:32Z jjongsma $ -*- c++ -*-/* gtkmm example Copyright (C) 2002 gtkmm development team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#ifndef GTKMM_EXAMPLEWINDOW_H#define GTKMM_EXAMPLEWINDOW_H#include <gtkmm.h>class ExampleWindow : public Gtk::Window //主要視窗,必須繼承自Gtk::Window{public:  ExampleWindow(int which);  virtual ~ExampleWindow();protected:   //Signal handlers:  void on_button_quit_clicked(); //自訂義Button點擊事件  //Child widgets:  Gtk::Button m_button;          //button 容器宣告  Gtk::Box m_box1;   //box   容器宣告  Gtk::Box m_boxQuit; //box   容器宣告  Gtk::Button m_buttonQuit; //Button  容器宣告  Gtk::Label m_Label1, m_Label2;//Label 容器宣告  Gtk::Separator m_separator1, m_separator2;//用於控件分離的控件 官方字典連結};#endif //GTKMM_EXAMPLEWINDOW_H

接著是 "examplewindow.cpp",實現剛剛各個容器的定義。

#include <iostream>#include "examplewindow.h"#include "packbox.h"ExampleWindow::ExampleWindow(int which): m_box1(Gtk::ORIENTATION_VERTICAL),  m_buttonQuit("Quit"){  set_title("Gtk::Box example");  PackBox *pPackBox1, *pPackBox2, *pPackBox3, *pPackBox4, *pPackBox5;//這裡這五個控件是自訂義控件,定義內容寫於"packbox.h"  switch(which)//這是一開始傳進來的參數,用於決定顯示哪個demo  {    case 1://第一個範例    {      m_Label1.set_text("Gtk::Box(Gtk::ORIENTATION_HORIZONTAL); set_homogeneous(false);");//這裡一整句是在設定靜態文本控件的字串,內容為 設定box為橫向,且內部子控件可以大小不一
     // 將控件對齊線設定為左側
      m_Label1.set_halign(Gtk::ALIGN_START);      m_Label1.set_valign(Gtk::ALIGN_START);      // Pack the label into the vertical box (vbox box1).  Remember that      // widgets added to a vbox will be packed one on top of the other in      // order.
     //將靜態文本控件打包到直向box(vbox)中,所有被打包進vbox的控件將會依次由上自下排列
      m_box1.pack_start(m_Label1, Gtk::PACK_SHRINK);//Gtk::PACK_SHERINK :將m_Label1打包進m_box1中,大小與子控件關聯 官網字典連結      // Create a PackBox - homogeneous = false, spacing = 0,//創建自訂義PackBox,內部子控件可以大小不一致,間距為零。
      // options = Gtk::PACK_SHRINK, padding = 0      pPackBox1 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_SHRINK));//gtkmm可以動態載入控件,使用的方法為gtk::manage 官網字典連結
      m_box1.pack_start(*pPackBox1, Gtk::PACK_SHRINK);//打包控件      // Create a PackBox - homogeneous = false, spacing = 0,      // options = Gtk::PACK_EXPAND_PADDING, padding = 0
      pPackBox2 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_EXPAND_PADDING));// Gtk::PACK_EXPAND_PADDING :空間將被拓展,剩餘空間由空白填充      m_box1.pack_start(*pPackBox2, Gtk::PACK_SHRINK);      // Create a PackBox - homogeneous = false, spacing = 0,      // options = Gtk::PACK_EXPAND_WIDGET, padding = 0      pPackBox3 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_EXPAND_WIDGET));      m_box1.pack_start(*pPackBox3, Gtk::PACK_SHRINK);      // pack the separator into the vbox.  Remember each of these      // widgets are being packed into a vbox, so they'll be stacked      // vertically.
      //將分離控件打包進vbox中,這些控件會被垂直堆疊      m_box1.pack_start(m_separator1, Gtk::PACK_SHRINK, 5);      // create another new label, and show it.
//創建另一個靜態文本控件,然後顯示它      m_Label2.set_text("Gtk::Box(Gtk::ORIENTATION_HORIZONTAL); set_homogeneous(true);");      m_Label2.set_halign(Gtk::ALIGN_START);      m_Label2.set_valign(Gtk::ALIGN_START);      m_box1.pack_start(m_Label2, Gtk::PACK_SHRINK);      // Args are: homogeneous, spacing, options, padding
//參數分別為,"同質性(子控件大小是否一致)"、"選項"、"間距選項"      pPackBox4 = Gtk::manage(new PackBox(true, 0, Gtk::PACK_EXPAND_PADDING));      m_box1.pack_start(*pPackBox4, Gtk::PACK_SHRINK);      // Args are: homogeneous, spacing, options, padding      pPackBox5 = Gtk::manage(new PackBox(true, 0, Gtk::PACK_EXPAND_WIDGET));      m_box1.pack_start(*pPackBox5, Gtk::PACK_SHRINK);      m_box1.pack_start(m_separator2, Gtk::PACK_SHRINK, 5);      break;    }//剩下都是大同小異,與上面語法並無不同,但可繼續學習控件的打包方式    case 2:    {      m_Label1.set_text("Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 10); set_homogeneous(false);");      m_Label1.set_halign(Gtk::ALIGN_START);      m_Label1.set_valign(Gtk::ALIGN_START);      m_box1.pack_start(m_Label1, Gtk::PACK_SHRINK);      pPackBox1 = Gtk::manage(new PackBox(false, 10, Gtk::PACK_EXPAND_PADDING));      m_box1.pack_start(*pPackBox1, Gtk::PACK_SHRINK);      pPackBox2 = Gtk::manage(new PackBox(false, 10, Gtk::PACK_EXPAND_WIDGET));      m_box1.pack_start(*pPackBox2, Gtk::PACK_SHRINK);      m_box1.pack_start(m_separator1, Gtk::PACK_SHRINK, 5);      m_Label2.set_text("Gtk::Box(Gtk::ORIENTATION_HORIZONTAL); set_homogeneous(false);");      m_Label2.set_halign(Gtk::ALIGN_START);      m_Label2.set_valign(Gtk::ALIGN_START);      m_box1.pack_start(m_Label2, Gtk::PACK_SHRINK);      pPackBox3 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_SHRINK, 10));      m_box1.pack_start(*pPackBox3, Gtk::PACK_SHRINK);      pPackBox4 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_EXPAND_WIDGET, 10));      m_box1.pack_start(*pPackBox4, Gtk::PACK_SHRINK);      m_box1.pack_start(m_separator2, Gtk::PACK_SHRINK, 5);      break;    }    case 3:    {      // This demonstrates the ability to use Gtk::Box::pack_end() to      // right justify widgets.  First, we create a new box as before.      pPackBox1 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_SHRINK));      // create the label that will be put at the end.      m_Label1.set_text("end");      // pack it using pack_end(), so it is put on the right side      // of the PackBox.      pPackBox1->pack_end(m_Label1, Gtk::PACK_SHRINK);      m_box1.pack_start(*pPackBox1, Gtk::PACK_SHRINK);      // this explicitly sets the separator to 500 pixels wide by 5 pixels      // high.  This is so the hbox we created will also be 500 pixels wide,      // and the "end" label will be separated from the other labels in the      // hbox.  Otherwise, all the widgets in the hbox would be packed as      // close together as possible.      m_separator1.set_size_request(500, 5);      // pack the separator into the vbox.      m_box1.pack_start(m_separator1, Gtk::PACK_SHRINK, 5);      break;    }    default:    {      std::cerr << "Unexpected command-line option." << std::endl;      break;    }  }  // Connect the signal to hide the window:  m_buttonQuit.signal_clicked().connect( sigc::mem_fun(*this,              &ExampleWindow::on_button_quit_clicked) );  // pack the button into the quitbox.  // The last 2 arguments to Box::pack_start are: options, padding.  m_boxQuit.pack_start(m_buttonQuit, Gtk::PACK_EXPAND_PADDING);  m_box1.pack_start(m_boxQuit, Gtk::PACK_SHRINK);  // pack the vbox (box1) which now contains all our widgets, into the  // main window.  add(m_box1);  show_all_children();}ExampleWindow::~ExampleWindow(){}void ExampleWindow::on_button_quit_clicked(){  hide();}

剛才可以看到自訂義控件packbox可以來參考一下它的實現過程,以下為文檔"packbox.h",

做了自定義控件的定義。

#ifndef GTKMM_EXAMPLE_PACKBOX_H#define GTKMM_EXAMPLE_PACKBOX_H#include <gtkmm.h>class PackBox : public Gtk::Box//繼承至GTK::BOX{public:  PackBox(bool homogeneous, int spacing, Gtk::PackOptions options, int padding = 0);//初始化函數,參數為"子控件大小同質性",
//"間距","打包選項"、"留白"  virtual ~PackBox();protected:  Gtk::Button m_button1, m_button2, m_button3;//這個控件終將有四個button  Gtk::Button* m_pbutton4;};#endif //GTKMM_EXAMPLE_PACKBOX_H
"packbox.h"的內容實現"packbox.cpp"

#include "packbox.h"PackBox::PackBox(bool homogeneous, int spacing, Gtk::PackOptions options,        int padding): Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, spacing),//這個自訂義函數繼承自Gtk::box初始化建構  m_button1("box.pack_start("),  m_button2("button,"),  m_button3((options == Gtk::PACK_SHRINK) ? "Gtk::PACK_SHRINK" :            ((options == Gtk::PACK_EXPAND_PADDING) ?             "Gtk::PACK_EXPAND_PADDING" : "Gtk::PACK_EXPAND_WIDGET")){  set_homogeneous(homogeneous);  pack_start(m_button1, options, padding);//開始打包自己的子控件  pack_start(m_button2, options, padding);  pack_start(m_button3, options, padding);  m_pbutton4 = new Gtk::Button(Glib::ustring::format(padding) + ");");//動態創建控件  pack_start(*m_pbutton4, options, padding);//打包剛創建的控件}PackBox::~PackBox(){  delete m_pbutton4;}
main.cpp

#include "examplewindow.h"//將剛剛的主視窗引入#include <gtkmm/application.h>//引入自訂義控件#include <iostream>#include <cstdlib>#define GTK_APPLICATION_RECEIVES_COMMAND_LINE_ARGUMENTS 0#if GTK_APPLICATION_RECEIVES_COMMAND_LINE_ARGUMENTSnamespace{int on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>& command_line,                    Glib::RefPtr<Gtk::Application>& app){  int argc = 0;  char** argv = command_line->get_arguments(argc);  for (int i = 0; i < argc; ++i)    std::cout << "argv[" << i << "] = " << argv[i] << std::endl;  app->activate(); // Without activate() the window won't be shown.  return EXIT_SUCCESS;}} // anonymous namespace#endif//假如不傳入參數,報錯int main(int argc, char *argv[]){  if (argc != 2)  {    std::cerr << "Usage: example <num>, where <num> is 1, 2, or 3." << std::endl;    return EXIT_FAILURE;  }#if GTK_APPLICATION_RECEIVES_COMMAND_LINE_ARGUMENTS  // The command line arguments must be checked before Gtk::Application::run()  // is called. The Gio::APPLICATION_HANDLES_COMMAND_LINE flag and the  // on_command_line() signal handler are not necessary. This program is simpler  // without them, and with argc = 1 in the call to Gtk::Application::create().  // They are included to show a program with Gio::APPLICATION_HANDLES_COMMAND_LINE.  // Gio::APPLICATION_NON_UNIQUE makes it possible to run several instances of  // this application simultaneously.  auto app = Gtk::Application::create(argc, argv,    "org.gtkmm.example", Gio::APPLICATION_HANDLES_COMMAND_LINE | Gio::APPLICATION_NON_UNIQUE);  // Note after = false.  // Only one signal handler is invoked. This signal handler must run before  // the default signal handler, or else it won't run at all.  app->signal_command_line().connect(sigc::bind(sigc::ptr_fun(&on_command_line), app), false);#else  // Gio::APPLICATION_NON_UNIQUE makes it possible to run several instances of  // this application simultaneously.  int argc1 = 1; // Don't give the command line arguments to Gtk::Application.  auto app = Gtk::Application::create(argc1, argv,    "org.gtkmm.example", Gio::APPLICATION_NON_UNIQUE);#endif  ExampleWindow window(std::atoi(argv[1]));  return app->run(window); //Shows the window and returns when it is closed.}

 Mingw使用的Makefile

CC=g++CFLAGS=$(shell pkg-config --cflags --libs gtkmm-3.0)out.exe:main.cpp examplewindow.h  examplewindow.cpp packbox.h packbox.cpp$(CC)  $^ -o $@ $(CFLAGS) -O3 -Wall



0 0
原创粉丝点击