Skip to content

Commit c14911d

Browse files
committed
ENH: Adding Draw Example for Display Area
- `cairomm` library added to support vector graphics - Makefile (macOS) updated to support `cairomm` - Callback/Handler function updated for drawing - Example line drawing used from GNOME documentation - BRK: This will break SemaphoreCI due to missing libs for build environment
1 parent fb2b286 commit c14911d

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

calculator_simple/build/macos/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ COMPILER_WITH_FLAGS := $(COMPILER) $(FLAGS)
3636

3737
LIB_GTKMM = gtkmm-3.0
3838
LIB_GLIB = glib-2.0
39-
PKG_CONFIG_LIBS := $(LIB_GTKMM) $(LIB_GLIB)
39+
LIB_CAIRO = cairomm-1.0
40+
PKG_CONFIG_LIBS := $(LIB_GTKMM) $(LIB_GLIB) $(LIB_CAIRO)
4041
OTHER_LIBS := -lstdc++
4142
LIBS := $(OTHER_LIBS) `pkg-config --cflags --libs $(PKG_CONFIG_LIBS)`
4243

calculator_simple/src/app_window.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ AppWindow::AppWindow():
140140
layout_buttons.attach(btn_alt_dot_obj, 1, 4, 1, 1);
141141
layout_buttons.attach(btn_alt_inv_obj, 4, 4, 1, 1);
142142

143+
144+
display_area.signal_draw().connect(sigc::mem_fun(*this, &AppWindow::handle_display_update), false);
145+
143146
layout_display.pack_start(display_area);
144147

145148
layout_main.pack_start(layout_display);
@@ -163,9 +166,34 @@ AppWindow::~AppWindow()
163166
/**
164167
* @brief Handler method for the display area redraws.
165168
*/
166-
void AppWindow::handle_display_update()
169+
bool AppWindow::handle_display_update(
170+
::Cairo::RefPtr<::Cairo::Context> const & draw_context_ptr
171+
)
167172
{
168-
return;
173+
display_area_allocation = display_area.get_allocation();
174+
int const area_x_full = display_area_allocation.get_width();
175+
int const area_y_full = display_area_allocation.get_height();
176+
177+
int const top_left_x = (0);
178+
int const top_left_y = (0);
179+
180+
int const center_x = (area_x_full / 2);
181+
int const center_y = (area_y_full / 2);
182+
183+
int const bot_rght_x = (area_x_full);
184+
int const bot_rght_y = (area_y_full);
185+
186+
187+
draw_context_ptr->set_line_width(10.0);
188+
draw_context_ptr->set_source_rgb(0.8, 0.0, 0.0);
189+
draw_context_ptr->move_to(top_left_x, top_left_y);
190+
draw_context_ptr->line_to(center_x, center_y);
191+
draw_context_ptr->line_to(top_left_x, bot_rght_y);
192+
draw_context_ptr->move_to(center_x, center_y);
193+
draw_context_ptr->line_to(bot_rght_x, center_y);
194+
draw_context_ptr->stroke();
195+
196+
return PROPAGATE_SIG; // Should propagate to make sure save/restore are processed correctly.
169197
}
170198

171199

calculator_simple/src/app_window.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <gtkmm/scale.h>
2424
#include <gtkmm/button.h>
2525

26+
#include <cairomm/context.h>
27+
2628
#include <array>
2729

2830
#include "logic_calculator.hpp"
@@ -36,7 +38,9 @@ class AppWindow :
3638
virtual ~AppWindow();
3739

3840
protected:
39-
void handle_display_update();
41+
bool handle_display_update(
42+
::Cairo::RefPtr<::Cairo::Context> const & draw_context_ptr
43+
);
4044

4145
bool handle_radix_change(
4246
Gtk::ScrollType const & scroll_type
@@ -70,6 +74,7 @@ class AppWindow :
7074
Gtk::Grid layout_buttons;
7175

7276
Gtk::DrawingArea display_area;
77+
Gtk::Allocation display_area_allocation;
7378

7479
Gtk::Label radix_label_obj;
7580
Gtk::Scale radix_slider_obj;

0 commit comments

Comments
 (0)