Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * OS: Ubuntu 18.04
- * Kernel: Linux 4.15.0-42-generic
- * Compiler: GCC 7.3.0
- * Libraries: gtkmm 3.22.2-2
- * Instructions: Compile with 'g++ -o test main.cpp `pkg-config --libs --cflags gtkmm-3.0`'
- * and run with './test'.
- * Description: Creates window called 'TITLE' of size 'WIDTH' x 'HEIGHT'
- * and fills it with RGB color that is specified using 'Color' struct.
- * The drawing area onto which is drawn is placed into scrolled window
- * so that we can use the scrollbars to navigate the drawing area
- * if the window is resized to smaller than initial size.
- * The problem is that these scrollbars are not shown.
- */
- #include <gtkmm.h>
- #include <string>
- // window attributes
- #define TITLE "Test"
- #define WIDTH 640
- #define HEIGHT 480
- // color to display in RGB format
- struct Color
- {
- double black = 0.0;
- double green = 0.0;
- double red = 0.0;
- };
- class DrawingArea : public Gtk::DrawingArea
- {
- public:
- // copies dimensions of the window so the whole window is filled
- void copy_dimensions(Gdk::Rectangle& dimensions)
- {
- memcpy(&_dimensions, &dimensions, sizeof(Gdk::Rectangle));
- signal_draw().connect(sigc::mem_fun(*this, &DrawingArea::render));
- size_allocate(_dimensions);
- }
- private:
- Gdk::Rectangle _dimensions;
- // renders color onto the surface
- bool render(const Cairo::RefPtr<Cairo::Context>& cr)
- {
- Color color;
- cr->set_source_rgb(color.black, color.green, color.red);
- cr->rectangle(_dimensions.get_x(), _dimensions.get_y(), _dimensions.get_width(), _dimensions.get_height());
- cr->fill();
- return true;
- }
- };
- class Window : public Gtk::Window
- {
- public:
- Window(const int width, const int height)
- {
- // set dimensions
- _dimensions.set_x(0);
- _dimensions.set_y(0);
- _dimensions.set_width(width);
- _dimensions.set_height(height);
- // set properties of window and its elements
- describe_window();
- // set drawing area
- _drawing_area.copy_dimensions(_dimensions);
- // attach all together
- attach_elements();
- }
- private:
- Gdk::Rectangle _dimensions;
- Gtk::ScrolledWindow _scroll_win;;
- DrawingArea _drawing_area;
- // set major window properties
- void describe_window()
- {
- // window itself
- set_title(TITLE);
- set_default_size(_dimensions.get_width(), _dimensions.get_height());
- // scrolling area
- _scroll_win.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC, Gtk::PolicyType::POLICY_AUTOMATIC);
- _scroll_win.set_overlay_scrolling(true);
- _scroll_win.set_hexpand(true);
- _scroll_win.set_vexpand(true);
- }
- // properly glues all elements together
- void attach_elements()
- {
- _scroll_win.add(_drawing_area);
- add(_scroll_win);
- }
- };
- int main(int argc, char *argv[])
- {
- // create app and run it
- Glib::RefPtr<Gtk::Application> test = Gtk::Application::create(argc, argv, "this.is.just.a.test");
- Window win(WIDTH, HEIGHT);
- win.show_all();
- return test->run(win);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement