Allow ValueViewers to have a bounding box different from the Style's. This closes #64.

This commit is contained in:
Twan van Laarhoven
2020-06-01 01:18:13 +02:00
parent e9305c7554
commit 4ff603d413
24 changed files with 79 additions and 87 deletions
-9
View File
@@ -189,15 +189,6 @@ int Style::update(Context& ctx) {
return changed;
}
bool Style::isVisible() const {
return visible
&& (width()) > 0
&& fabs(left()) < 100000
&& fabs(right()) < 100000
&& (height()) > 0
&& fabs(top()) < 100000
&& fabs(bottom()) < 100000;
}
bool Style::hasSize() const {
int h = is_setw(width) + is_set(left) + is_set(right);
int v = is_setw(height) + is_set(top) + is_set(bottom);
+3 -6
View File
@@ -118,15 +118,12 @@ public:
} automatic_side : 8; ///< Which of (left, width, right) and (top, height, bottom) is determined automatically?
bool content_dependent; ///< Does this style depend on content properties?
inline RealPoint getPos() const { return RealPoint(left, top ); }
inline RealSize getSize() const { return RealSize ( width, height); }
inline RealRect getExternalRect() const { return RealRect (left, top, width, height); }
inline RealRect getInternalRect() const { return RealRect(0, 0, width, height); }
inline RealPoint getPos() const { return RealPoint(left, top); }
inline RealSize getSize() const { return RealSize(width, height); }
inline RealRect getExternalRect() const { return RealRect(left, top, width, height); }
/// Does this style have a non-zero size (or is it scripted)?
bool hasSize() const;
/// Is this style visible, and does it have a sane size
bool isVisible() const;
/// Get a copy of this style
virtual StyleP clone() const = 0;
+2 -2
View File
@@ -150,7 +150,7 @@ struct CompareTabOrder {
};
bool is_enabled(ValueViewerP const& v) {
return v->getField()->editable && v->getStyle()->isVisible();
return v->getField()->editable && v->isVisible();
}
bool DataEditor::selectWithTab(vector<ValueViewerP>::iterator const& it) {
@@ -210,7 +210,7 @@ bool DataEditor::search(Iterator it, Iterator end, FindInfo& find, bool from_sta
for (;it != end; ++it) {
ValueViewer* viewer = *it;
if (viewer == current_viewer) include = true;
if (include && viewer->getField()->editable && viewer->getStyle()->isVisible()) {
if (include && viewer->getField()->editable && viewer->isVisible()) {
ValueEditor* editor = viewer->getEditor();
if (editor) {
if (editor && editor->search(find, from_start || viewer != current_viewer)) {
+2 -2
View File
@@ -44,7 +44,7 @@ void CardViewer::redraw(const ValueViewer& v) {
// causing the two viewers to continously refresh.
if (drawing_card()) return;
up_to_date = false;
RefreshRect(getRotation().trRectToBB(v.boundingBox()), false);
RefreshRect(getRotation().trRectToBB(v.boundingBoxBorder()), false);
}
void CardViewer::onChange() {
@@ -112,7 +112,7 @@ bool CardViewer::shouldDraw(const ValueViewer& v) const {
// int dx = GetScrollPos(wxHORIZONTAL), dy = GetScrollPos(wxVERTICAL);
// wxRegion clip = GetUpdateRegion();
// clip.Offset(dx, dy);
return GetUpdateRegion().Contains(getRotation().trRectToBB(v.boundingBox().toRect()).toRect()) != wxOutRegion;
return GetUpdateRegion().Contains(getRotation().trRectToBB(v.boundingBoxBorder().toRect()).toRect()) != wxOutRegion;
}
// helper class for overdrawDC()
+9 -21
View File
@@ -35,14 +35,13 @@ void NativeLookEditor::drawViewer(RotatedDC& dc, ValueViewer& v) {
ValueEditor* e = v.getEditor();
if (!e || e->drawLabel()) {
// draw control border and box
Style& s = *v.getStyle();
draw_control_box(this, dc.getDC(), dc.trRectToBB(s.getInternalRect().grow(1)), current_editor == e, e != nullptr);
draw_control_box(this, dc.getDC(), dc.getExternalRect().grow(1), current_editor == e, e != nullptr);
// draw label
dc.SetFont(*wxNORMAL_FONT);
dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT));
// TODO : tr using stylesheet or using game?
dc.DrawText(tr(getStylePackage(), s.fieldP->caption, identity),
RealPoint(margin_left - s.left, 1));
dc.DrawText(tr(getStylePackage(), v.getField()->caption, identity),
RealPoint(margin_left - v.bounding_box.x, 1));
}
// draw viewer
v.draw(dc);
@@ -76,15 +75,15 @@ void NativeLookEditor::resizeViewers() {
StyleP s = v->getStyle();
ValueEditor* e = v->getEditor();
if (!e || e->drawLabel()) {
s->left = margin + label_width;
v->bounding_box.x = margin + label_width;
} else {
s->left = margin;
v->bounding_box.x = margin;
}
s->top = y;
s->width = w - s->left - margin;
s->height = default_height;
v->bounding_box.y = y;
v->bounding_box.width = w - v->bounding_box.x - margin;
v->bounding_box.height = s->height() == 0 ? default_height : s->height;
if (e) e->determineSize();
y += s->height + vspace;
y += v->bounding_box.height + vspace;
}
y = y - vspace + margin;
SetVirtualSize(w, (int)y);
@@ -93,17 +92,6 @@ void NativeLookEditor::resizeViewers() {
}
if (y >= h) {
// Doesn't fit vertically, add scrollbar and resize
/*
y = margin;
FOR_EACH(v, viewers) {
StyleP s = v->getStyle();
ValueEditor* e = v->getEditor();
s->top = y;
s->width = s->width - wxSystemSettings::GetMetric(wxSYS_VSCROLL_X, this);
if (e) e->determineSize();
y += s->height + vspace;
}
*/
// create scrollbar
}
}
+6 -6
View File
@@ -55,9 +55,9 @@ TextFieldP TextCtrl::getFieldP() {
}
void TextCtrl::updateSize() {
wxSize cs = GetClientSize();
Style& style = getStyle();
style.width = cs.GetWidth() - 2;
style.height = cs.GetHeight() - 2;
ValueViewer& viewer = *viewers.front();
viewer.bounding_box.width = cs.GetWidth() - 2;
viewer.bounding_box.height = cs.GetHeight() - 2;
viewers.front()->getEditor()->determineSize(true);
}
@@ -76,9 +76,9 @@ void TextCtrl::onChangeSet() {
// initialize
if (viewers.empty()) {
// create a field, style and value
TextFieldP field(new TextField);
TextStyleP style(new TextStyle(field));
TextValueP value(new FakeTextValue(field, nullptr, false, false));
TextFieldP field = make_intrusive<TextField>();
TextStyleP style = make_intrusive<TextStyle>(field);
TextValueP value = make_intrusive<FakeTextValue>(field, nullptr, false, false);
// set stuff
field->index = 0;
field->multi_line = multi_line;
+1 -1
View File
@@ -287,7 +287,7 @@ void DropDownList::redrawArrowOnParent() {
shared_ptr<RotatedDC> dcP = editor.overdrawDC();
RotatedDC& dc = *dcP;
Rotater r(dc, viewer->getRotation());
draw_drop_down_arrow(&editor, dc.getDC(), dc.trRectToBB(dc.getInternalRect().grow(1)), IsShown());
draw_drop_down_arrow(&editor, dc.getDC(), dc.getExternalRect().grow(1), IsShown());
}
}
}
+2 -2
View File
@@ -275,11 +275,11 @@ void ChoiceValueEditor::onLoseFocus() {
void ChoiceValueEditor::draw(RotatedDC& dc) {
ChoiceValueViewer::draw(dc);
if (nativeLook()) {
draw_drop_down_arrow(&editor(), dc.getDC(), dc.trRectToBB(style().getInternalRect().grow(1)), drop_down->IsShown());
draw_drop_down_arrow(&editor(), dc.getDC(), dc.getExternalRect().grow(1), drop_down->IsShown());
}
}
void ChoiceValueEditor::determineSize(bool) {
style().height = max(style().height(), 16.);
bounding_box.height = max(bounding_box.height, 16.);
}
void ChoiceValueEditor::change(const Defaultable<String>& c) {
+1 -1
View File
@@ -148,7 +148,7 @@ void ColorValueEditor::draw(RotatedDC& dc) {
}
}
void ColorValueEditor::determineSize(bool) {
style().height = 20;
bounding_box.height = 20;
}
void ColorValueEditor::change(const Defaultable<Color>& c) {
+1 -1
View File
@@ -14,5 +14,5 @@
IMPLEMENT_VALUE_EDITOR(Info) {}
void InfoValueEditor::determineSize(bool) {
style().height = 26;
bounding_box.height = 26;
}
+1 -1
View File
@@ -105,7 +105,7 @@ void MultipleChoiceValueEditor::determineSize(bool force_fit) {
item_height = 18;
// height depends on number of items and item height
int item_count = field().choices->lastId();
style().height = item_count * item_height;
bounding_box.height = item_count * item_height;
}
bool MultipleChoiceValueEditor::onLeftDown(const RealPoint& pos, wxMouseEvent& ev) {
+2 -2
View File
@@ -101,11 +101,11 @@ void PackageChoiceValueEditor::onLoseFocus() {
void PackageChoiceValueEditor::draw(RotatedDC& dc) {
PackageChoiceValueViewer::draw(dc);
if (nativeLook()) {
draw_drop_down_arrow(&editor(), dc.getDC(), dc.trRectToBB(style().getInternalRect().grow(1)), drop_down && drop_down->IsShown());
draw_drop_down_arrow(&editor(), dc.getDC(), dc.getExternalRect().grow(1), drop_down && drop_down->IsShown());
}
}
void PackageChoiceValueEditor::determineSize(bool) {
style().height = max(style().height(), 16.);
bounding_box.height = max(bounding_box.height, 16.);
}
void PackageChoiceValueEditor::change(const String& c) {
+8 -7
View File
@@ -27,7 +27,7 @@ void SymbolValueEditor::draw(RotatedDC& dc) {
dc.SetFont(wxFont(10,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL));
dc.SetTextForeground(*wxBLACK);
RealSize text_size = dc.GetTextExtent(_("double click to edit symbol"));
dc.DrawText(_("double click to edit symbol"), align_in_rect(ALIGN_MIDDLE_CENTER, text_size, style().getInternalRect()));
dc.DrawText(_("double click to edit symbol"), align_in_rect(ALIGN_MIDDLE_CENTER, text_size, dc.getInternalRect()));
}
if (nativeLook()) {
// draw editor buttons
@@ -38,9 +38,9 @@ void SymbolValueEditor::draw(RotatedDC& dc) {
}
void SymbolValueEditor::drawButton(RotatedDC& dc, int button, const String& text) {
bool down = button == button_down;
double height = style().height;
double width = style().height + 2;
double x = style().width - width - (width + 1) * button;
double height = bounding_box.height;
double width = bounding_box.height + 2;
double x = bounding_box.width - width - (width + 1) * button;
double y = 0;
// draw button
draw_button(&editor(), dc.getDC(), dc.trRectToBB(RealRect(x,y,width,height)), false, down, true);
@@ -54,8 +54,8 @@ void SymbolValueEditor::drawButton(RotatedDC& dc, int button, const String& text
}
int SymbolValueEditor::findButton(const RealPoint& pos) {
if (pos.y < 0 || pos.y >= style().height) return -1;
int button = (int)floor( (style().width - pos.x) / (style().height + 3) );
if (pos.y < 0 || pos.y >= bounding_box.height) return -1;
int button = (int)floor( (bounding_box.width - pos.x) / (bounding_box.height + 3) );
if (button >= 0 && button <= 1) return button;
return -1;
}
@@ -107,7 +107,8 @@ bool SymbolValueEditor::onLeftDClick(const RealPoint& pos, wxMouseEvent&) {
}
void SymbolValueEditor::determineSize(bool) {
style().height = 50;
if (style().height == 0) style().height = 50;
bounding_box.height = 50;
}
+10 -8
View File
@@ -627,7 +627,7 @@ wxMenu* TextValueEditor::getMenu(int type) const {
void TextValueEditor::draw(RotatedDC& dc) {
if (nativeLook()) {
// clip the dc to the region of this control
dc.SetClippingRegion(style().getInternalRect());
dc.SetClippingRegion(dc.getInternalRect());
}
// update scrollbar
prepareDrawScrollbar(dc);
@@ -663,7 +663,7 @@ void TextValueEditor::redrawSelection(size_t old_selection_start_i, size_t old_s
Rotater r(dc, getRotation());
if (nativeLook()) {
// clip the dc to the region of this control
dc.SetClippingRegion(style().getInternalRect());
dc.SetClippingRegion(dc.getInternalRect());
}
// clear old selection by drawing it again
if (!old_drop_down_shown) {
@@ -727,9 +727,8 @@ bool TextValueEditor::containsPoint(const RealPoint& pos) const {
if (word_lists.empty()) return false;
return (bool) findWordList(pos);
}
RealRect TextValueEditor::boundingBox() const {
if (word_lists.empty()) return ValueViewer::boundingBox();
RealRect r = style().getInternalRect().grow(1);
RealRect TextValueEditor::boundingBoxBorder() const {
RealRect r = ValueViewer::boundingBoxBorder();
FOR_EACH_CONST(wl, word_lists) {
r.width = max(r.width, wl->rect.right() + 9);
}
@@ -1255,14 +1254,16 @@ void TextValueEditor::determineSize(bool force_fit) {
// muliline, determine scrollbar size
Rotation rot = parent.getRotation();
Rotater r(rot, getRotation());
if (!force_fit) style().height = 100;
if (!force_fit) {
style().height = bounding_box.height = 100;
}
int sbw = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
RealPoint pos = rot.tr(RealPoint(0,0));
scrollbar->SetSize(
(int)(pos.x + rot.trX(style().width) + 1 - sbw),
(int)(pos.x + rot.trX(bounding_box.width) + 1 - sbw),
(int)pos.y - 1,
(int)sbw,
(int)rot.trY(style().height) + 2);
(int)rot.trY(bounding_box.height) + 2);
v.reset(true);
} else {
// Height depends on font
@@ -1272,6 +1273,7 @@ void TextValueEditor::determineSize(bool force_fit) {
dc.SetFont(style().font.toWxFont(1.0));
style().height = dc.GetCharHeight() + 2 + style().padding_top + style().padding_bottom;
}
bounding_box.height = style().height;
}
void TextValueEditor::onShow(bool showing) {
+1 -1
View File
@@ -92,7 +92,7 @@ public:
wxCursor cursor(const RealPoint& pos) const override;
void determineSize(bool force_fit = false) override;
bool containsPoint(const RealPoint& p) const override;
RealRect boundingBox() const override;
RealRect boundingBoxBorder() const override;
void onShow(bool) override;
void draw(RotatedDC&) override;
+2 -2
View File
@@ -43,7 +43,7 @@ void DataViewer::draw(RotatedDC& dc, const Color& background) {
// prepare viewers
bool changed_content_properties = false;
FOR_EACH(v, viewers) { // draw low z index fields first
if (v->getStyle()->isVisible()) {
if (v->isVisible()) {
Rotater r(dc, v->getRotation());
try {
if (v->prepare(dc)) {
@@ -59,7 +59,7 @@ void DataViewer::draw(RotatedDC& dc, const Color& background) {
}
// draw viewers
FOR_EACH(v, viewers) { // draw low z index fields first
if (v->getStyle()->isVisible()) {// visible
if (v->isVisible()) {// visible
Rotater r(dc, v->getRotation());
try {
drawViewer(dc, *v);
+1 -1
View File
@@ -546,7 +546,7 @@ void TextViewer::prepareLinesTryScales(RotatedDC& dc, const String& text, const
// Try to fit a blank line in the masked image, move down until it fits
RealSize TextViewer::fitLineWidth(Line& line, RotatedDC& dc, const TextStyle& style) const {
RealSize line_size(line.margin_left + lineLeft(dc, style, line.top), 0);
while (line.top < style.height && line_size.width + 1 >= style.width - style.padding_right - line.margin_right) {
while (line.top < dc.getHeight() && line_size.width + 1 >= dc.getWidth() - style.padding_right - line.margin_right) {
// nothing fits on this line, move down one pixel
line.top += 1;
line_size.width = line.margin_left + lineLeft(dc, style, line.top);
+5 -5
View File
@@ -50,7 +50,7 @@ void ColorValueViewer::draw(RotatedDC& dc) {
style().top_width < style().height && style().bottom_width < style().height;
if (clip) {
// clip away the inside of the rectangle
wxRegion r = dc.trRectToRegion(style().getInternalRect());
wxRegion r = dc.trRectToRegion(dc.getInternalRect());
r.Subtract(dc.trRectToRegion(RealRect(
style().left_width,
style().top_width,
@@ -59,7 +59,7 @@ void ColorValueViewer::draw(RotatedDC& dc) {
)));
dc.getDC().SetClippingRegion(r);
}
dc.DrawRoundedRectangle(style().getInternalRect(), style().radius);
dc.DrawRoundedRectangle(dc.getInternalRect(), style().radius);
if (clip) dc.getDC().DestroyClippingRegion();
}
drawFieldBorder(dc);
@@ -69,15 +69,15 @@ void ColorValueViewer::draw(RotatedDC& dc) {
bool ColorValueViewer::containsPoint(const RealPoint& p) const {
// check against mask
const AlphaMask& alpha_mask = getMask();
if (alpha_mask.isLoaded()) {
if (alpha_mask.isLoaded() || nativeLook()) {
// check against mask
return alpha_mask.isOpaque(p, style().getSize());
return alpha_mask.isOpaque(p, bounding_box.size());
} else {
double left = p.x, right = style().width - p.x - 1;
double top = p.y, bottom = style().height - p.y - 1;
if (left < 0 || right < 0 || top < 0 || bottom < 0) return false; // outside bounding box
// check against border
return left < style().left_width || right < style().right_width // inside horizontal border
|| top < style().top_width || bottom < style().bottom_width; // inside vertical border
|| top < style().top_width || bottom < style().bottom_width; // inside vertical border
}
}
+1 -1
View File
@@ -26,7 +26,7 @@ void InfoValueViewer::draw(RotatedDC& dc) {
dc.SetFont(style().font, 1.0);
}
// draw background
RealRect rect = style().getInternalRect();
RealRect rect = dc.getInternalRect();
dc.DrawRectangle(rect.grow(2));
// draw text
rect = rect.move(
+1 -1
View File
@@ -24,7 +24,7 @@ bool MultipleChoiceValueViewer::prepare(RotatedDC& dc) {
void MultipleChoiceValueViewer::draw(RotatedDC& dc) {
drawFieldBorder(dc);
if (style().render_style & RENDER_HIDDEN) return;
RealPoint pos = align_in_rect(style().alignment, RealSize(0,0), style().getInternalRect());
RealPoint pos = align_in_rect(style().alignment, RealSize(0,0), dc.getInternalRect());
// selected choices
vector<String> selected;
value().get(selected);
+1 -1
View File
@@ -21,7 +21,7 @@ IMPLEMENT_VALUE_VIEWER(Symbol);
void SymbolValueViewer::draw(RotatedDC& dc) {
drawFieldBorder(dc);
// draw checker background
draw_checker(dc, style().getInternalRect());
draw_checker(dc, dc.getInternalRect());
double wh = min(dc.getWidth(), dc.getHeight());
// try to load symbol
if (symbols.empty() && !value().filename.empty()) {
+14 -4
View File
@@ -14,6 +14,7 @@
ValueViewer::ValueViewer(DataViewer& parent, const StyleP& style)
: StyleListener(style), parent(parent)
, bounding_box(style->getExternalRect())
{}
Package& ValueViewer::getStylePackage() const { return parent.getStylePackage(); }
@@ -27,14 +28,21 @@ void ValueViewer::setValue(const ValueP& value) {
}
bool ValueViewer::containsPoint(const RealPoint& p) const {
return getMask().isOpaque(p, styleP->getSize());
return getMask().isOpaque(p, bounding_box.size());
}
RealRect ValueViewer::boundingBox() const {
return styleP->getExternalRect().grow(1);
RealRect ValueViewer::boundingBoxBorder() const {
return bounding_box.grow(1);
}
bool ValueViewer::isVisible() const {
return getStyle()->visible
&& bounding_box.width > 0
&& bounding_box.height > 0
&& fabs(bounding_box.x) < 100000
&& fabs(bounding_box.y) < 100000;
}
Rotation ValueViewer::getRotation() const {
return Rotation(deg_to_rad(getStyle()->angle), getStyle()->getExternalRect(), 1.0, getStretch());
return Rotation(deg_to_rad(getStyle()->angle), bounding_box, 1.0, getStretch());
}
#if defined(__WXMSW__)
@@ -113,4 +121,6 @@ void ValueViewer::onStyleChange(int changes) {
if (!(changes & CHANGE_ALREADY_PREPARED)) {
parent.redraw(*this);
}
// update bounding box
if (!nativeLook()) bounding_box = getStyle()->getExternalRect();
}
+4 -1
View File
@@ -50,7 +50,9 @@ public:
/// Does this field contian the given point?
virtual bool containsPoint(const RealPoint& p) const;
/// Get a bounding rectangle for this field (including any border it may have)
virtual RealRect boundingBox() const;
virtual RealRect boundingBoxBorder() const;
/// Is this field visible?
bool isVisible() const;
/// Rotation to use for drawing this field
virtual Rotation getRotation() const;
@@ -73,6 +75,7 @@ public:
public:
DataViewer& parent; ///< Our parent object
RealRect bounding_box; ///< The bounding box of this viewer. Corresponds to styleP->getExternalRect(), except for native look editor
protected:
ValueP valueP; ///< The value we are currently viewing
+1 -1
View File
@@ -159,7 +159,7 @@ public:
inline RealPoint bottomRight() const { return RealPoint(x + width, y + height); }
/// Return a rectangle that is amount larger to all sides
inline RealRect grow(double amount) {
inline RealRect grow(double amount) const {
return RealRect(x - amount, y - amount, width + 2 * amount, height + 2 * amount);
}
/// Move the coordinates by some amount