Advertisement
Guest User

Untitled

a guest
Jun 20th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. #include <gtkmm.h>
  2. #include <gtkmm/drawingarea.h>
  3. #include <cairomm/context.h>
  4. #include <gtkmm/application.h>
  5. #include <gtkmm/window.h>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <stdio.h>
  9. #include <cstdio>
  10. #include <cstdlib>
  11.  
  12. using std::sprintf;
  13. using std::strtol;
  14.  
  15. using namespace std;
  16. float some_float = 2;
  17.  
  18. class App{
  19. public :
  20. };
  21. class EntryWindow : public Gtk::Window
  22. {
  23. public:
  24.   EntryWindow();
  25.   virtual ~EntryWindow();
  26.  
  27.   std::stringstream ss;
  28.   float some_float;
  29.  
  30.  
  31. protected:
  32.   //Signal handlers:
  33.  
  34.   void on_button_close();
  35.   void on_button_enter();
  36.   //Child widgets:
  37.   Gtk::Box m_HBox;
  38.   Gtk::Box m_VBox;
  39.   Gtk::Entry m_Entry;
  40.   Gtk::Button m_Button_Close, m_Button_Enter;
  41.  
  42. };
  43.  
  44. class CoordWindow : public Gtk::Window
  45. {
  46. public:
  47.   CoordWindow();
  48.   virtual ~CoordWindow();
  49.  
  50. private:
  51.   //Signal handlers:
  52.   void on_button_quit();
  53.   void on_button_enter();
  54.   //Tree model columns:
  55.   class ModelColumns : public Gtk::TreeModel::ColumnRecord
  56.   {
  57.   public:
  58.  
  59.     ModelColumns()
  60.     { add(m_col_id); add(m_col_x); add(m_col_y);}
  61.  
  62.     Gtk::TreeModelColumn<unsigned int> m_col_id;
  63.     Gtk::TreeModelColumn<int> m_col_x;
  64.     Gtk::TreeModelColumn<int> m_col_y;
  65.    
  66.   };
  67.  
  68.   ModelColumns m_Columns;
  69.  
  70.   //Child widgets:
  71.   Gtk::Box m_VBox;
  72.  
  73.   Gtk::ScrolledWindow m_ScrolledWindow;
  74.   Gtk::TreeView m_TreeView;
  75.   Glib::RefPtr<Gtk::ListStore> m_refTreeModel;
  76.  
  77.   Gtk::ButtonBox m_ButtonBox;
  78.   Gtk::Button m_Button_Quit;
  79.   Gtk::Button m_Button_Enter;
  80. };
  81.  
  82. class MyArea : public Gtk::DrawingArea
  83. {
  84. public:
  85.   MyArea();
  86.   virtual ~MyArea();
  87.  
  88. protected:
  89.   //Override default signal handler:
  90.   virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
  91. };
  92.  
  93. EntryWindow::EntryWindow()
  94. : m_VBox(Gtk::ORIENTATION_VERTICAL),
  95.   m_Button_Close("Close"),
  96.   m_Button_Enter("Enter")
  97. {
  98.  
  99.   set_size_request(200, 100);
  100.   set_title("Type coordinate points");
  101.  
  102.   add(m_VBox);
  103.  
  104.   m_Entry.set_max_length(2);
  105.  
  106.   m_Entry.select_region(0, m_Entry.get_text_length());
  107.   m_VBox.pack_start(m_Entry);
  108.  
  109.   // Note that add() can also be used instead of pack_xxx()
  110.   m_VBox.add(m_HBox);
  111.  
  112.   m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
  113.               &EntryWindow::on_button_close) );
  114.   m_Button_Enter.signal_clicked().connect( sigc::mem_fun(*this,
  115.               &EntryWindow::on_button_enter) );
  116.   m_VBox.pack_start(m_Button_Close);
  117.   m_VBox.pack_start(m_Button_Enter);
  118.   m_Button_Close.set_can_default();
  119.   m_Button_Close.grab_default();
  120.  
  121.   show_all_children();
  122. }
  123.  
  124. EntryWindow::~EntryWindow()
  125. {
  126. }
  127.  
  128. CoordWindow::CoordWindow()
  129. : m_VBox(Gtk::ORIENTATION_VERTICAL),
  130.   m_Button_Quit("Quit"),
  131.   m_Button_Enter("Enter")
  132. {
  133.   set_title("Gtk::TreeView (ListStore) example");
  134.   set_border_width(5);
  135.   set_default_size(300, 300);
  136.  
  137.   add(m_VBox);
  138.  
  139.   //Add the TreeView, inside a ScrolledWindow, with the button underneath:
  140.   m_ScrolledWindow.add(m_TreeView);
  141.  
  142.   //Only show the scrollbars when they are necessary:
  143.   m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  144.  
  145.   m_VBox.pack_start(m_ScrolledWindow);
  146.   m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);
  147.  
  148.   m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
  149.   m_ButtonBox.pack_start(m_Button_Enter, Gtk::PACK_SHRINK);
  150.   m_ButtonBox.set_border_width(5);
  151.   m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
  152.   m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this,
  153.               &CoordWindow::on_button_quit) );
  154.   m_Button_Enter.signal_clicked().connect( sigc::mem_fun(*this,
  155.               &CoordWindow::on_button_enter) );
  156.   //Create the Tree model:
  157.   m_refTreeModel = Gtk::ListStore::create(m_Columns);
  158.   m_TreeView.set_model(m_refTreeModel);
  159.  
  160.   //Fill the TreeView's model
  161.   Gtk::TreeModel::Row row = *(m_refTreeModel->append());
  162.   for(int i=0;i<some_float;i++)
  163.   {
  164.   if(i!=0) row = *(m_refTreeModel->append());  
  165.   row[m_Columns.m_col_id] = i+1;
  166.   row[m_Columns.m_col_x] = 0;
  167.   row[m_Columns.m_col_y] = 0;
  168.  
  169.   }  
  170.  
  171.   //Add the TreeView's view columns:
  172.   //This number will be shown with the default numeric formatting.
  173.   m_TreeView.append_column("ID", m_Columns.m_col_id);
  174.   m_TreeView.append_column_editable("X", m_Columns.m_col_x);
  175.   m_TreeView.append_column_editable("Y", m_Columns.m_col_y);
  176.  
  177.  
  178.   //Make all the columns reorderable:
  179.   //This is not necessary, but it's nice to show the feature.
  180.   //You can use TreeView::set_column_drag_function() to more
  181.   //finely control column drag and drop.
  182.   for(guint i = 0; i < 2; i++)
  183.   {
  184.     Gtk::TreeView::Column* pColumn = m_TreeView.get_column(i);
  185.     pColumn->set_reorderable();
  186.   }
  187.  
  188.   show_all_children();
  189. }
  190.  
  191. CoordWindow::~CoordWindow()
  192. {
  193. }
  194.  
  195. void CoordWindow::on_button_quit()
  196. {
  197.   hide();
  198. }
  199. void CoordWindow::on_button_enter()
  200. {
  201.   hide();
  202. }
  203.  
  204. MyArea::MyArea()
  205. {
  206. }
  207.  
  208. MyArea::~MyArea()
  209. {
  210. }
  211.  
  212. bool MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
  213. {
  214.   set_size_request(400, 400);
  215.   Gtk::Allocation allocation = get_allocation();
  216.   const int width = allocation.get_width();
  217.   const int height = allocation.get_height();
  218.  
  219.   // coordinates for the center of the window
  220.   int xc, yc;
  221.   xc = width / 2;
  222.   yc = height / 2;
  223.  
  224.   cr->set_line_width(2.0);
  225.  
  226.   // draw red lines out from the center of the window
  227.   cr->set_source_rgb(0.0, 1.0, 0.0);
  228.   cr->move_to(0, 0);
  229.   cr->line_to(xc, yc);
  230.   cr->line_to(0, height);
  231.   cr->move_to(xc, yc);
  232.   cr->line_to(width, yc);
  233.   cr->stroke();
  234.  
  235.   return true;
  236. }
  237.  
  238. void EntryWindow::on_button_close()
  239. {
  240.   hide();
  241. }
  242. void EntryWindow::on_button_enter()
  243. {
  244. ss << m_Entry.get_text();
  245. ss >> some_float;
  246.  
  247.  
  248. }
  249.  
  250.  
  251. int main(int argc, char** argv)
  252. {
  253.    
  254.    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
  255.  
  256.    
  257.    EntryWindow enter;
  258.    enter.show();
  259.    return app->run(enter);
  260.    
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement