ui.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "ui.hh"
  2. #include <gtkmm.h>
  3. #include <iostream>
  4. Ui::Ui(const Glib::RefPtr<Gtk::Application>& app): m_Box(Gtk::Orientation::VERTICAL), m_Box1(Gtk::Orientation::VERTICAL), m_Button("Add document"), m_Button1("Print all"){
  5. set_title("Priority queue (heap)");
  6. set_resizable(false);
  7. set_child(m_Box); //We can put a MenuBar at the top of the box and other stuff below it.
  8. //Define the actions:
  9. m_refActionGroup = Gio::SimpleActionGroup::create();
  10. m_refActionGroup->add_action("quit",
  11. sigc::mem_fun(*this, &Ui::on_action_file_quit));
  12. m_refActionGroup->add_action("about",
  13. sigc::mem_fun(*this, &Ui::on_action_about));
  14. insert_action_group("example", m_refActionGroup);
  15. //Define how the actions are presented in the menus and toolbars:
  16. m_refBuilder = Gtk::Builder::create();
  17. //Layout the actions in a menubar and toolbar:
  18. const Glib::ustring ui_info =
  19. "<interface>"
  20. " <menu id='menubar'>"
  21. " <submenu>"
  22. " <attribute name='label' translatable='yes'>_File</attribute>"
  23. " <section>"
  24. " <item>"
  25. " <attribute name='label' translatable='yes'>_Quit</attribute>"
  26. " <attribute name='action'>example.quit</attribute>"
  27. " </item>"
  28. " </section>"
  29. " </submenu>"
  30. " <submenu>"
  31. " <attribute name='label' translatable='yes'>_Help</attribute>"
  32. " <item>"
  33. " <attribute name='label' translatable='yes'>_About</attribute>"
  34. " <attribute name='action'>example.about</attribute>"
  35. " </item>"
  36. " </submenu>"
  37. " </menu>"
  38. "</interface>";
  39. // Set accelerator keys:
  40. app->set_accel_for_action("example.quit", "<Primary>q");
  41. app->set_accel_for_action("example.about", "<Primary>a");
  42. try{
  43. m_refBuilder->add_from_string(ui_info);
  44. }
  45. catch(const Glib::Error& ex){
  46. std::cerr << "Building menus and toolbar failed: " << ex.what();
  47. }
  48. //Get the menubar:
  49. auto gmenu = m_refBuilder->get_object<Gio::Menu>("menubar");
  50. if (!gmenu)
  51. g_warning("GMenu not found");
  52. else{
  53. auto pMenuBar = Gtk::make_managed<Gtk::PopoverMenuBar>(gmenu);
  54. //Add the PopoverMenuBar to the window:
  55. m_Box.append(*pMenuBar);
  56. }
  57. //Get the toolbar and add it to a container widget:
  58. auto toolbar = m_refBuilder->get_widget<Gtk::Box>("toolbar");
  59. if (!toolbar)
  60. g_warning("toolbar not found");
  61. else
  62. m_Box.append(*toolbar);
  63. m_Box.append(m_Box1);
  64. m_Box1.append(m_Button);
  65. m_Box1.append(m_Button1);
  66. m_Box1.set_homogeneous(true);
  67. m_Button.set_margin(10);
  68. m_Button1.set_margin(10);
  69. m_Button.signal_clicked().connect(sigc::mem_fun(*this,
  70. &Ui::addDocument));
  71. m_Button1.signal_clicked().connect(sigc::mem_fun(*this,
  72. &Ui::printAll));
  73. }
  74. void Ui::addDocument(){
  75. uiAddDocument = new UiAddDocument(fila);
  76. uiAddDocument->show();
  77. }
  78. void Ui::printAll(){
  79. uiPrintAll = new UiPrintAll(fila);
  80. uiPrintAll->show();
  81. }
  82. void Ui::on_action_file_quit(){
  83. set_visible(false); //Closes the main window to stop the app->make_window_and_run().
  84. }
  85. void Ui::on_action_about(){
  86. uiAbout = new UiAbout;
  87. uiAbout->show();
  88. }
  89. void Ui::on_action_toggle(){
  90. std::cout << "The toggle menu item was selected." << std::endl;
  91. bool active = false;
  92. m_refActionRain->get_state(active);
  93. //The toggle action's state does not change automatically:
  94. active = !active;
  95. m_refActionRain->change_state(active);
  96. Glib::ustring message;
  97. if(active)
  98. message = "Toggle is active.";
  99. else
  100. message = "Toggle is not active";
  101. std::cout << message << std::endl;
  102. }