Advertisement
Guest User

main.cpp

a guest
Feb 23rd, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. /*
  2.  * OS:              Ubuntu 18.04
  3.  * Kernel:          Linux 4.15.0-42-generic
  4.  * Compiler:        GCC 7.3.0
  5.  * Libraries:       gtkmm 3.22.2-2
  6.  * Instructions:    Compile with 'g++ -o test main.cpp `pkg-config --libs --cflags gtkmm-3.0`'
  7.  *                  and run with './test'.
  8.  * Description: Creates window called 'TITLE' of size 'WIDTH' x 'HEIGHT'
  9.  *                  and fills it with RGB color that is specified using 'Color' struct.
  10.  *                  The drawing area onto which is drawn is placed into scrolled window
  11.  *                  so that we can use the scrollbars to navigate the drawing area
  12.  *                  if the window is resized to smaller than initial size.
  13.  *                  The problem is that these scrollbars are not shown.
  14. */
  15.  
  16. #include <gtkmm.h>
  17. #include <string>
  18.  
  19. // window attributes
  20. #define TITLE "Test"
  21. #define WIDTH 640
  22. #define HEIGHT 480
  23.  
  24. // color to display in RGB format
  25. struct Color
  26. {
  27.     double black = 0.0;
  28.     double green = 0.0;
  29.     double red = 0.0;
  30. };
  31.  
  32. class DrawingArea : public Gtk::DrawingArea
  33. {
  34.     public:
  35.         // copies dimensions of the window so the whole window is filled
  36.         void copy_dimensions(Gdk::Rectangle& dimensions)
  37.         {
  38.             memcpy(&_dimensions, &dimensions, sizeof(Gdk::Rectangle));
  39.             signal_draw().connect(sigc::mem_fun(*this, &DrawingArea::render));
  40.             size_allocate(_dimensions);
  41.         }
  42.  
  43.     private:
  44.         Gdk::Rectangle _dimensions;
  45.  
  46.         // renders color onto the surface
  47.         bool render(const Cairo::RefPtr<Cairo::Context>& cr)
  48.         {
  49.             Color color;
  50.             cr->set_source_rgb(color.black, color.green, color.red);
  51.             cr->rectangle(_dimensions.get_x(), _dimensions.get_y(), _dimensions.get_width(), _dimensions.get_height());
  52.             cr->fill();
  53.             return true;
  54.         }
  55. };
  56.  
  57. class Window : public Gtk::Window
  58. {
  59.     public:
  60.         Window(const int width, const int height)
  61.         {
  62.             // set dimensions
  63.             _dimensions.set_x(0);
  64.             _dimensions.set_y(0);
  65.             _dimensions.set_width(width);
  66.             _dimensions.set_height(height);
  67.  
  68.             // set properties of window and its elements
  69.             describe_window();
  70.  
  71.             // set drawing area
  72.             _drawing_area.copy_dimensions(_dimensions);
  73.  
  74.             // attach all together
  75.             attach_elements();
  76.         }
  77.     private:
  78.         Gdk::Rectangle _dimensions;
  79.         Gtk::ScrolledWindow _scroll_win;;
  80.         DrawingArea _drawing_area;
  81.  
  82.         // set major window properties
  83.         void describe_window()
  84.         {
  85.             // window itself
  86.             set_title(TITLE);
  87.             set_default_size(_dimensions.get_width(), _dimensions.get_height());
  88.  
  89.             // scrolling area
  90.             _scroll_win.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC, Gtk::PolicyType::POLICY_AUTOMATIC);
  91.             _scroll_win.set_overlay_scrolling(true);
  92.             _scroll_win.set_hexpand(true);
  93.             _scroll_win.set_vexpand(true);
  94.         }
  95.  
  96.         // properly glues all elements together
  97.         void attach_elements()
  98.         {
  99.             _scroll_win.add(_drawing_area);
  100.             add(_scroll_win);
  101.         }
  102. };
  103.  
  104. int main(int argc, char *argv[])
  105. {
  106.     // create app and run it
  107.     Glib::RefPtr<Gtk::Application> test = Gtk::Application::create(argc, argv, "this.is.just.a.test");
  108.     Window win(WIDTH, HEIGHT);
  109.     win.show_all();
  110.     return test->run(win);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement