You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
4.7 KiB
C++

#include "StdAfx.h"
#include ".\itemalign.h"
#include "SigmaDoc.h"
#include "SigmaView.h"
#include "ItemSelect.h"
#include "ActionOffsetItem.h"
CItemAlign::CItemAlign(CSigmaDoc * ppDoc)
:CItem(ppDoc)
{
this->SetType(ITEM_ALIGN);
}
CItemAlign::~CItemAlign(void)
{
}
int CItemAlign::Align(CPositionList &list, int nMode)
{
if(list.GetCount()<=0) return 0;
//»ñµÃ×Ü·¶Î§
CRect8 rect(1e100,-1e100,-1e100,1e100);
POSITION pos,pt;
COne* pOne;
pos=list.GetHeadPosition();
while(pos)
{
pt=list.GetNext(pos);
pOne=GetDoc()->GetDraw()->GetAt(pt);
pOne->GetRange(rect);
}
CPoint2D cp=rect.CenterPoint();
CPoint2D ocp;
int num=(int)list.GetCount();
CPoint2D* pOffseValuet=new CPoint2D[num];
//¶ÔÆë
int i=0;
double dx=0;
double dy=0;
CRect8 range;
pos=list.GetHeadPosition();
while(pos)
{
pt=list.GetNext(pos);
pOne=GetDoc()->GetDraw()->GetAt(pt);
range=pOne->GetRect();
ocp=range.CenterPoint();
switch(nMode)
{
case ITEM_ALIGN_LEFT:
dx=rect.left-range.left;
dy=0;
break;
case ITEM_ALIGN_TOP:
dx=0;
dy=rect.top-range.top;
break;
case ITEM_ALIGN_RIGHT:
dx=rect.right-range.right;
dy=0;
break;
case ITEM_ALIGN_BOTTOM:
dx=0;
dy=rect.bottom-range.bottom;
break;
case ITEM_ALIGN_CENTER_H:
dx = cp.x0 - ocp.x0;
dy = 0;
break;
case ITEM_ALIGN_CENTER_V:
dx = 0;
dy = cp.y0 - ocp.y0;
break;
}
pOne->Offset(dx,dy);
pOffseValuet[i].x0=dx;
pOffseValuet[i].y0=dy;
i++;
}
//ΪUNDOºÍREDO
UINT nID=IDS_STRING_ACTION_ALIGN;
switch(nMode)
{
case ITEM_ALIGN_LEFT: nID=IDS_STRING_ACTION_ALIGN_LEFT; break;
case ITEM_ALIGN_TOP: nID=IDS_STRING_ACTION_ALIGN_TOP; break;
case ITEM_ALIGN_RIGHT: nID=IDS_STRING_ACTION_ALIGN_RIGHT; break;
case ITEM_ALIGN_BOTTOM: nID=IDS_STRING_ACTION_ALIGN_BOTTOM; break;
case ITEM_ALIGN_CENTER_H:nID=IDS_STRING_ACTION_ALIGN_H_CENTER; break;
case ITEM_ALIGN_CENTER_V:nID=IDS_STRING_ACTION_ALIGN_V_CENTER; break;
}
GetDoc()->SetActionItem(new CActionOffsetItem(GetDoc(),nID,list,pOffseValuet));
//delete []pOffseValuet;
return 1;
}
int CItemAlign::Align(int nMode)
{
CItemSelect * itemSelect = m_pDoc->GetSelectItem();
if (itemSelect == NULL)
return 0;
if (itemSelect->m_selection.GetSize() == 0)
return 0;
//CList<POSITION,POSITION>* ps = GetView()->GetSelection();
//if(ps==NULL) return 0;
int r=Align(itemSelect->m_selection, nMode);
//CItemSelect* pSelectItem=GetDoc()->GetSelectItem();
//if(pSelectItem)
//{
// CRect rt=GetDoc()->GetTracker().GetInvalidateRect();
// GetDoc()->Invalidate(&rt);
// pSelectItem->ReloadTrackerPath();
//}
return r;
}
int CItemAlign::AlignRowAndColumn(CSize size)
{
CItemSelect * itemSelect = m_pDoc->GetSelectItem();
if (itemSelect == NULL)
return 0;
if (itemSelect->m_selection.GetSize() == 0)
return 0;
//CList<POSITION,POSITION> * ps=GetView()->GetSelection();
CList<POSITION, POSITION> * ps = &itemSelect->m_selection;
if(ps==NULL) return 0;
if(ps->GetCount()<=0) return 0;
int i=(int)ps->GetCount();
if(size.cx>1 && size.cy==1)//°´ÁÐÅÅÁÐ
{
size.cy=i/size.cx;
if((i%size.cx)>0) size.cy++;
}
else if(size.cx==1 && size.cy>1)//°´ÐÐÅÅÁÐ
{
size.cx=i/size.cy;
if((i%size.cy)>0) size.cx++;
}
//»ñµÃ×î´ó¸ß¿í,×î´ó·¶Î§
CSize8 step;
CRect8 rect;
CRect8 range(1e100,-1e100,-1e100,1e100);
POSITION pos,pt;
COne* pOne;
i=0;
pos=ps->GetHeadPosition();
while(pos)
{
pt=ps->GetNext(pos);
pOne=GetDoc()->GetDraw()->GetAt(pt);
pOne->GetRange(range);
rect=pOne->GetRect();
if(i==0)
{
step=rect.GetSize();
step.cy=fabs(step.cy);
}
else
{
CSize8 ct=rect.GetSize();
ct.cy=fabs(ct.cy);
if(step.cx<ct.cx) step.cx=ct.cx;
if(step.cy<ct.cy) step.cy=ct.cy;
}
i++;
}
//½øÐÐÅÅÁÐ
CPoint2D point;
int row,col;
CSize8 space(0,0); //Ö®¼äµÄ¼ä¸ô
if(size.cx>1)
space.cx=(range.Width()-step.cx*size.cx)/(size.cx-1);
if(size.cy>1)
space.cy=(fabs(range.Height())-step.cy*size.cy)/(size.cy-1);
CRect8 rt;
CPoint2D offset;
//CActionListItem* pItem=new CActionListItem(GetDoc(), ID_OBJECT_ALIGN_ROWS_COLUMNS);
pos=ps->GetHeadPosition();
point.x0=range.left;
for(row=0;row<size.cx;++row)
{
point.y0=range.top;
for(col=0;col<size.cy;++col)
{
if(pos==NULL) break;
pt=ps->GetNext(pos);
pOne=GetDoc()->GetDraw()->GetAt(pt);
rt=pOne->GetRect();
offset.x0=point.x0-rt.left;
offset.y0=point.y0-rt.top;
pOne->Offset(offset.x0, offset.y0);
//pItem->AddOffsetItem(pOne, offset);//for UNDO,REDO
point.y0-=(space.cy+step.cy);
}
if(pos==NULL) break;
point.x0+=(space.cx+step.cx);
}
//GetDoc()->SetActionItem(pItem);
//CItemSelect* pSelectItem=GetDoc()->GetSelectItem();
//if(pSelectItem)
//{
// CRect rt=GetDoc()->GetTracker().GetInvalidateRect();
// GetDoc()->Invalidate(&rt);
// pSelectItem->ReloadTrackerPath();
//}
return i;
}