Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gtkmm.h>
- #include <gtkmm/drawingarea.h>
- #include <cairomm/context.h>
- #include <gtkmm/application.h>
- #include <gtkmm/window.h>
- #include <iostream>
- #include <sstream>
- #include <stdio.h>
- #include <cstdio>
- #include <cstdlib>
- using std::sprintf;
- using std::strtol;
- using namespace std;
- float some_float = 2;
- class App{
- public :
- };
- class EntryWindow : public Gtk::Window
- {
- public:
- EntryWindow();
- virtual ~EntryWindow();
- std::stringstream ss;
- float some_float;
- protected:
- //Signal handlers:
- void on_button_close();
- void on_button_enter();
- //Child widgets:
- Gtk::Box m_HBox;
- Gtk::Box m_VBox;
- Gtk::Entry m_Entry;
- Gtk::Button m_Button_Close, m_Button_Enter;
- };
- class CoordWindow : public Gtk::Window
- {
- public:
- CoordWindow();
- virtual ~CoordWindow();
- private:
- //Signal handlers:
- void on_button_quit();
- void on_button_enter();
- //Tree model columns:
- class ModelColumns : public Gtk::TreeModel::ColumnRecord
- {
- public:
- ModelColumns()
- { add(m_col_id); add(m_col_x); add(m_col_y);}
- Gtk::TreeModelColumn<unsigned int> m_col_id;
- Gtk::TreeModelColumn<int> m_col_x;
- Gtk::TreeModelColumn<int> m_col_y;
- };
- ModelColumns m_Columns;
- //Child widgets:
- Gtk::Box m_VBox;
- Gtk::ScrolledWindow m_ScrolledWindow;
- Gtk::TreeView m_TreeView;
- Glib::RefPtr<Gtk::ListStore> m_refTreeModel;
- Gtk::ButtonBox m_ButtonBox;
- Gtk::Button m_Button_Quit;
- Gtk::Button m_Button_Enter;
- };
- class MyArea : public Gtk::DrawingArea
- {
- public:
- MyArea();
- virtual ~MyArea();
- protected:
- //Override default signal handler:
- virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
- };
- EntryWindow::EntryWindow()
- : m_VBox(Gtk::ORIENTATION_VERTICAL),
- m_Button_Close("Close"),
- m_Button_Enter("Enter")
- {
- set_size_request(200, 100);
- set_title("Type coordinate points");
- add(m_VBox);
- m_Entry.set_max_length(2);
- m_Entry.select_region(0, m_Entry.get_text_length());
- m_VBox.pack_start(m_Entry);
- // Note that add() can also be used instead of pack_xxx()
- m_VBox.add(m_HBox);
- m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
- &EntryWindow::on_button_close) );
- m_Button_Enter.signal_clicked().connect( sigc::mem_fun(*this,
- &EntryWindow::on_button_enter) );
- m_VBox.pack_start(m_Button_Close);
- m_VBox.pack_start(m_Button_Enter);
- m_Button_Close.set_can_default();
- m_Button_Close.grab_default();
- show_all_children();
- }
- EntryWindow::~EntryWindow()
- {
- }
- CoordWindow::CoordWindow()
- : m_VBox(Gtk::ORIENTATION_VERTICAL),
- m_Button_Quit("Quit"),
- m_Button_Enter("Enter")
- {
- set_title("Gtk::TreeView (ListStore) example");
- set_border_width(5);
- set_default_size(300, 300);
- add(m_VBox);
- //Add the TreeView, inside a ScrolledWindow, with the button underneath:
- m_ScrolledWindow.add(m_TreeView);
- //Only show the scrollbars when they are necessary:
- m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
- m_VBox.pack_start(m_ScrolledWindow);
- m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);
- m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
- m_ButtonBox.pack_start(m_Button_Enter, Gtk::PACK_SHRINK);
- m_ButtonBox.set_border_width(5);
- m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
- m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this,
- &CoordWindow::on_button_quit) );
- m_Button_Enter.signal_clicked().connect( sigc::mem_fun(*this,
- &CoordWindow::on_button_enter) );
- //Create the Tree model:
- m_refTreeModel = Gtk::ListStore::create(m_Columns);
- m_TreeView.set_model(m_refTreeModel);
- //Fill the TreeView's model
- Gtk::TreeModel::Row row = *(m_refTreeModel->append());
- for(int i=0;i<some_float;i++)
- {
- if(i!=0) row = *(m_refTreeModel->append());
- row[m_Columns.m_col_id] = i+1;
- row[m_Columns.m_col_x] = 0;
- row[m_Columns.m_col_y] = 0;
- }
- //Add the TreeView's view columns:
- //This number will be shown with the default numeric formatting.
- m_TreeView.append_column("ID", m_Columns.m_col_id);
- m_TreeView.append_column_editable("X", m_Columns.m_col_x);
- m_TreeView.append_column_editable("Y", m_Columns.m_col_y);
- //Make all the columns reorderable:
- //This is not necessary, but it's nice to show the feature.
- //You can use TreeView::set_column_drag_function() to more
- //finely control column drag and drop.
- for(guint i = 0; i < 2; i++)
- {
- Gtk::TreeView::Column* pColumn = m_TreeView.get_column(i);
- pColumn->set_reorderable();
- }
- show_all_children();
- }
- CoordWindow::~CoordWindow()
- {
- }
- void CoordWindow::on_button_quit()
- {
- hide();
- }
- void CoordWindow::on_button_enter()
- {
- hide();
- }
- MyArea::MyArea()
- {
- }
- MyArea::~MyArea()
- {
- }
- bool MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
- {
- set_size_request(400, 400);
- Gtk::Allocation allocation = get_allocation();
- const int width = allocation.get_width();
- const int height = allocation.get_height();
- // coordinates for the center of the window
- int xc, yc;
- xc = width / 2;
- yc = height / 2;
- cr->set_line_width(2.0);
- // draw red lines out from the center of the window
- cr->set_source_rgb(0.0, 1.0, 0.0);
- cr->move_to(0, 0);
- cr->line_to(xc, yc);
- cr->line_to(0, height);
- cr->move_to(xc, yc);
- cr->line_to(width, yc);
- cr->stroke();
- return true;
- }
- void EntryWindow::on_button_close()
- {
- hide();
- }
- void EntryWindow::on_button_enter()
- {
- ss << m_Entry.get_text();
- ss >> some_float;
- }
- int main(int argc, char** argv)
- {
- Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
- EntryWindow enter;
- enter.show();
- return app->run(enter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement