📄 basic.cpp
字号:
void wxShape::ClearAttachments()
{
wxNode *node = m_attachmentPoints.GetFirst();
while (node)
{
wxAttachmentPoint *point = (wxAttachmentPoint *)node->GetData();
delete point;
node = node->GetNext();
}
m_attachmentPoints.Clear();
}
void wxShape::ClearText(int regionId)
{
if (regionId == 0)
{
m_text.DeleteContents(true);
m_text.Clear();
m_text.DeleteContents(false);
}
wxNode *node = m_regions.Item(regionId);
if (!node)
return;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
region->ClearText();
}
void wxShape::ClearRegions()
{
wxNode *node = m_regions.GetFirst();
while (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
wxNode *next = node->GetNext();
delete region;
delete node;
node = next;
}
}
void wxShape::AddRegion(wxShapeRegion *region)
{
m_regions.Append(region);
}
void wxShape::SetDefaultRegionSize()
{
wxNode *node = m_regions.GetFirst();
if (!node) return;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
double w, h;
GetBoundingBoxMin(&w, &h);
region->SetSize(w, h);
}
bool wxShape::HitTest(double x, double y, int *attachment, double *distance)
{
// if (!sensitive)
// return false;
double width = 0.0, height = 0.0;
GetBoundingBoxMin(&width, &height);
if (fabs(width) < 4.0) width = 4.0;
if (fabs(height) < 4.0) height = 4.0;
width += (double)4.0; height += (double)4.0; // Allowance for inaccurate mousing
double left = (double)(m_xpos - (width/2.0));
double top = (double)(m_ypos - (height/2.0));
double right = (double)(m_xpos + (width/2.0));
double bottom = (double)(m_ypos + (height/2.0));
int nearest_attachment = 0;
// If within the bounding box, check the attachment points
// within the object.
if (x >= left && x <= right && y >= top && y <= bottom)
{
int n = GetNumberOfAttachments();
double nearest = 999999.0;
// GetAttachmentPosition[Edge] takes a logical attachment position,
// i.e. if it's rotated through 90%, position 0 is East-facing.
for (int i = 0; i < n; i++)
{
double xp, yp;
if (GetAttachmentPositionEdge(i, &xp, &yp))
{
double l = (double)sqrt(((xp - x) * (xp - x)) +
((yp - y) * (yp - y)));
if (l < nearest)
{
nearest = l;
nearest_attachment = i;
}
}
}
*attachment = nearest_attachment;
*distance = nearest;
return true;
}
else return false;
}
// Format a text string according to the region size, adding
// strings with positions to region text list
static bool GraphicsInSizeToContents = false; // Infinite recursion elimination
void wxShape::FormatText(wxDC& dc, const wxString& s, int i)
{
double w, h;
ClearText(i);
if (m_regions.GetCount() < 1)
return;
wxNode *node = m_regions.Item(i);
if (!node)
return;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
// region->SetText(s); // don't set the formatted text yet, it will be done below
region->m_regionText = s;
dc.SetFont(* region->GetFont());
region->GetSize(&w, &h);
wxStringList *stringList = oglFormatText(dc, s, (w-2*m_textMarginX), (h-2*m_textMarginY), region->GetFormatMode());
node = (wxNode*)stringList->GetFirst();
while (node)
{
wxChar *s = (wxChar *)node->GetData();
wxShapeTextLine *line = new wxShapeTextLine(0.0, 0.0, s);
region->GetFormattedText().Append((wxObject *)line);
node = node->GetNext();
}
delete stringList;
double actualW = w;
double actualH = h;
// Don't try to resize an object with more than one image (this case should be dealt
// with by overriden handlers)
if ((region->GetFormatMode() & FORMAT_SIZE_TO_CONTENTS) &&
(region->GetFormattedText().GetCount() > 0) &&
(m_regions.GetCount() == 1) && !GraphicsInSizeToContents)
{
oglGetCentredTextExtent(dc, &(region->GetFormattedText()), m_xpos, m_ypos, w, h, &actualW, &actualH);
if ((actualW+2*m_textMarginX != w ) || (actualH+2*m_textMarginY != h))
{
// If we are a descendant of a composite, must make sure the composite gets
// resized properly
wxShape *topAncestor = GetTopAncestor();
if (topAncestor != this)
{
// Make sure we don't recurse infinitely
GraphicsInSizeToContents = true;
wxCompositeShape *composite = (wxCompositeShape *)topAncestor;
composite->Erase(dc);
SetSize(actualW+2*m_textMarginX, actualH+2*m_textMarginY);
Move(dc, m_xpos, m_ypos);
composite->CalculateSize();
if (composite->Selected())
{
composite->DeleteControlPoints(& dc);
composite->MakeControlPoints();
composite->MakeMandatoryControlPoints();
}
// Where infinite recursion might happen if we didn't stop it
composite->Draw(dc);
GraphicsInSizeToContents = false;
}
else
{
Erase(dc);
SetSize(actualW+2*m_textMarginX, actualH+2*m_textMarginY);
Move(dc, m_xpos, m_ypos);
}
SetSize(actualW+2*m_textMarginX, actualH+2*m_textMarginY);
Move(dc, m_xpos, m_ypos);
EraseContents(dc);
}
}
oglCentreText(dc, &(region->GetFormattedText()), m_xpos, m_ypos, actualW-2*m_textMarginX, actualH-2*m_textMarginY, region->GetFormatMode());
m_formatted = true;
}
void wxShape::Recentre(wxDC& dc)
{
double w, h;
GetBoundingBoxMin(&w, &h);
int noRegions = m_regions.GetCount();
for (int i = 0; i < noRegions; i++)
{
wxNode *node = m_regions.Item(i);
if (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
oglCentreText(dc, &(region->GetFormattedText()), m_xpos, m_ypos, w-2*m_textMarginX, h-2*m_textMarginY, region->GetFormatMode());
}
}
}
bool wxShape::GetPerimeterPoint(double WXUNUSED(x1), double WXUNUSED(y1),
double WXUNUSED(x2), double WXUNUSED(y2),
double *WXUNUSED(x3), double *WXUNUSED(y3))
{
return false;
}
void wxShape::SetPen(const wxPen *the_pen)
{
m_pen = the_pen;
}
void wxShape::SetBrush(const wxBrush *the_brush)
{
m_brush = the_brush;
}
// Get the top-most (non-division) ancestor, or self
wxShape *wxShape::GetTopAncestor()
{
if (!GetParent())
return this;
if (GetParent()->IsKindOf(CLASSINFO(wxDivisionShape)))
return this;
else return GetParent()->GetTopAncestor();
}
/*
* Region functions
*
*/
void wxShape::SetFont(wxFont *the_font, int regionId)
{
m_font = the_font;
wxNode *node = m_regions.Item(regionId);
if (!node)
return;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
region->SetFont(the_font);
}
wxFont *wxShape::GetFont(int n) const
{
wxNode *node = m_regions.Item(n);
if (!node)
return NULL;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
return region->GetFont();
}
void wxShape::SetFormatMode(int mode, int regionId)
{
wxNode *node = m_regions.Item(regionId);
if (!node)
return;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
region->SetFormatMode(mode);
}
int wxShape::GetFormatMode(int regionId) const
{
wxNode *node = m_regions.Item(regionId);
if (!node)
return 0;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
return region->GetFormatMode();
}
void wxShape::SetTextColour(const wxString& the_colour, int regionId)
{
m_textColour = wxTheColourDatabase->Find(the_colour);
m_textColourName = the_colour;
wxNode *node = m_regions.Item(regionId);
if (!node)
return;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
region->SetColour(the_colour);
}
wxString wxShape::GetTextColour(int regionId) const
{
wxNode *node = m_regions.Item(regionId);
if (!node)
return wxEmptyString;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
return region->GetColour();
}
void wxShape::SetRegionName(const wxString& name, int regionId)
{
wxNode *node = m_regions.Item(regionId);
if (!node)
return;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
region->SetName(name);
}
wxString wxShape::GetRegionName(int regionId)
{
wxNode *node = m_regions.Item(regionId);
if (!node)
return wxEmptyString;
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
return region->GetName();
}
int wxShape::GetRegionId(const wxString& name)
{
wxNode *node = m_regions.GetFirst();
int i = 0;
while (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
if (region->GetName() == name)
return i;
node = node->GetNext();
i ++;
}
return -1;
}
// Name all m_regions in all subimages recursively.
void wxShape::NameRegions(const wxString& parentName)
{
int n = GetNumberOfTextRegions();
wxString buff;
for (int i = 0; i < n; i++)
{
if (parentName.Length() > 0)
buff << parentName << wxT(".") << i;
else
buff << i;
SetRegionName(buff, i);
}
wxNode *node = m_children.GetFirst();
int j = 0;
while (node)
{
buff.Empty();
wxShape *child = (wxShape *)node->GetData();
if (parentName.Length() > 0)
buff << parentName << wxT(".") << j;
else
buff << j;
child->NameRegions(buff);
node = node->GetNext();
j ++;
}
}
// Get a region by name, possibly looking recursively into composites.
wxShape *wxShape::FindRegion(const wxString& name, int *regionId)
{
int id = GetRegionId(name);
if (id > -1)
{
*regionId = id;
return this;
}
wxNode *node = m_children.GetFirst();
while (node)
{
wxShape *child = (wxShape *)node->GetData();
wxShape *actualImage = child->FindRegion(name, regionId);
if (actualImage)
return actualImage;
node = node->GetNext();
}
return NULL;
}
// Finds all region names for this image (composite or simple).
// Supply empty string list.
void wxShape::FindRegionNames(wxStringList& list)
{
int n = GetNumberOfTextRegions();
for (int i = 0; i < n; i++)
{
wxString name(GetRegionName(i));
list.Add(name);
}
wxNode *node = m_children.GetFirst();
while (node)
{
wxShape *child = (wxShape *)node->GetData();
child->FindRegionNames(list);
node = node->GetNext();
}
}
void wxShape::AssignNewIds()
{
// if (m_id == 0)
m_id = wxNewId();
wxNode *node = m_children.GetFirst();
while (node)
{
wxShape *child = (wxShape *)node->GetData();
child->AssignNewIds();
node = node->GetNext();
}
}
void wxShape::OnDraw(wxDC& WXUNUSED(dc))
{
}
void wxShape::OnMoveLinks(wxDC& dc)
{
// Want to set the ends of all attached links
// to point to/from this object
wxNode *current = m_lines.GetFirst();
while (current)
{
wxLineShape *line = (wxLineShape *)current->GetData();
line->GetEventHandler()->OnMoveLink(dc);
current = current->GetNext();
}
}
void wxShape::OnDrawContents(wxDC& dc)
{
double bound_x, bound_y;
GetBoundingBoxMin(&bound_x, &bound_y);
if (m_regions.GetCount() < 1) return;
if (m_pen) dc.SetPen(* m_pen);
wxShapeRegion *region = (wxShapeRegion *)m_regions.GetFirst()->GetData();
if (region->GetFont()) dc.SetFont(* region->GetFont());
dc.SetTextForeground(region->GetActualColourObject());
dc.SetBackgroundMode(wxTRANSPARENT);
if (!m_formatted)
{
oglCentreText(dc, &(region->GetFormattedText()), m_xpos, m_ypos, bound_x-2*m_textMarginX, bound_y-2*m_textMarginY, region->GetFormatMode());
m_formatted = true;
}
if (!GetDisableLabel())
{
oglDrawFormattedText(dc, &(region->GetFormattedText()), m_xpos, m_ypos, bound_x-2*m_textMarginX, bound_y-2*m_textMarginY, region->GetFormatMode());
}
}
void wxShape::DrawContents(wxDC& dc)
{
GetEventHandler()->OnDrawContents(dc);
}
void wxShape::OnSize(double WXUNUSED(x), double WXUNUSED(y))
{
}
bool wxShape::OnMovePre(wxDC& WXUNUSED(dc), double WXUNUSED(x), double WXUNUSED(y), double WXUNUSED(old_x), double WXUNUSED(old_y), bool WXUNUSED(display))
{
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -