Skip to content

Commit c107350

Browse files
committed
finished exercise12 for chapter12
1 parent 4cf2fba commit c107350

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

Chapter12/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,6 @@ Define a class `Box` that is a closed shape like a `Rectangle` (so it has fill c
132132

133133
## [Exercise 11](exercises/11)
134134
Define a `Group` to be a container of `Shape`s with suitable operations applied to the various members of the `Group`. Hint: `Vector_ref`. Use a `Group` to define a checkers (draughts) boardwhere pieces can be moved under program control.
135+
136+
## [Exercise 12](exercises/12)
137+
Define a class `Pseudo_window` that looks as much like a `Window` as you can make it without heroic efforts. It should have rounded corners, a label, and control icons. Maybe you could add some fake "contents", such as an image. It need not actually do anything. It is acceptable (and indeed recommended) to have it appear within a `Simple_window`.

Chapter12/exercises/12/cat.png

111 KB
Loading

Chapter12/exercises/12/main.cpp

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,74 @@ class Pseudo_window {
4646
public:
4747
Pseudo_window(Point tl, int w, int h, const std::string& s)
4848
: b{tl, w, h, c_radius},
49-
l{Point{tl.x, tl.y + 20}, Point{tl.x + w, tl.y + 20}},
50-
t{tl, s}
49+
l{Point{tl.x, tl.y + top_bar_height}, Point{tl.x + w, tl.y + top_bar_height}},
50+
t{tl, s},
51+
mi{Point{tl.x + w - 3 * icon_spacing, tl.y + top_bar_height / 2}, top_bar_height / 3},
52+
mi_i{
53+
Point{tl.x + w - 3 * icon_spacing - top_bar_height / 3, tl.y + top_bar_height / 2},
54+
Point{tl.x + w - 3 * icon_spacing + top_bar_height / 3, tl.y + top_bar_height / 2}
55+
},
56+
ma{Point{tl.x + w - 2 * icon_spacing, tl.y + top_bar_height / 2}, top_bar_height / 3},
57+
ma_i{Point{tl.x + w - 2 * icon_spacing - top_bar_height / 6,
58+
tl.y + top_bar_height / 2 - top_bar_height / 6}, top_bar_height / 3, top_bar_height / 3
59+
},
60+
cl{Point{tl.x + w - icon_spacing, tl.y + top_bar_height / 2}, top_bar_height / 3},
61+
cl_i{
62+
Point{tl.x + w - icon_spacing - top_bar_height / 6, tl.y + top_bar_height / 2 - top_bar_height / 6},
63+
Point{tl.x + w - icon_spacing + top_bar_height / 6, tl.y + top_bar_height / 2 + top_bar_height / 6},
64+
Point{tl.x + w - icon_spacing - top_bar_height / 6, tl.y + top_bar_height / 2 + top_bar_height / 6},
65+
Point{tl.x + w - icon_spacing + top_bar_height / 6, tl.y + top_bar_height / 2 - top_bar_height / 6}
66+
}
5167
{
5268
b.set_color(Color::invisible);
5369
b.set_fill_color(Color{100, 100, 100});
5470
t.set_color(Color::white);
71+
mi.set_fill_color(Color::green);
72+
ma.set_fill_color(Color{255, 200, 0});
73+
cl.set_fill_color(Color::red);
5574
}
5675
void attach_to(Window& w) {
5776
w.attach(b);
5877
w.attach(l);
5978
w.attach(t);
79+
w.attach(mi);
80+
w.attach(mi_i);
81+
w.attach(ma);
82+
w.attach(ma_i);
83+
w.attach(cl);
84+
w.attach(cl_i);
6085
}
86+
void move(int dx, int dy) {
87+
b.move(dx, dy);
88+
l.move(dx, dy);
89+
t.move(dx, dy);
90+
mi.move(dx, dy);
91+
mi_i.move(dx, dy);
92+
ma.move(dx, dy);
93+
ma_i.move(dx, dy);
94+
cl.move(dx, dy);
95+
cl_i.move(dx, dy);
96+
}
97+
static const int c_radius; // Corner radius
98+
static const int top_bar_height;
99+
static const int icon_spacing;
61100
private:
62-
Box b;
63-
Line l;
64-
Text t;
65-
static int c_radius;
101+
Box b; // Background
102+
Line l; // Top bar separator
103+
Text t; // Label
104+
// Control icons
105+
Circle mi; // Minimize
106+
Line mi_i; // Minimize icon
107+
Circle ma; // Maximize
108+
Rectangle ma_i; // Maximize icon
109+
Circle cl; // Close
110+
Lines cl_i; // Close icon
111+
66112
};
67113

68-
int Pseudo_window::c_radius = 10;
114+
const int Pseudo_window::c_radius = 8;
115+
const int Pseudo_window::top_bar_height = 20;
116+
const int Pseudo_window::icon_spacing = 20;
69117

70118
int main(int /*argc*/, char * /*argv*/[])
71119
{
@@ -83,8 +131,11 @@ int main(int /*argc*/, char * /*argv*/[])
83131
const std::string label = "Pseudo_window";
84132
Simple_window win{Point{win_x, win_y}, win_width, win_height, label};
85133

86-
Pseudo_window ps{Point{100, 100}, 600, 400, "Pseudo_window"};
87-
134+
Pseudo_window ps{Point{100, 100}, 600, 400, "Felis catus"};
88135
ps.attach_to(win);
136+
137+
Image i{Point{100 + 600 / 2 - 240 / 2, 100 + 400 / 2 - 287 / 2 + Pseudo_window::top_bar_height / 2}, "../../cat.png"};
138+
win.attach(i);
139+
89140
win.wait_for_button();
90141
}

0 commit comments

Comments
 (0)