Creating Softwares is an art

When I add new features to my UltraFluid Modeler, I need icons for the ribbon. And When I found something good, I jump into GIMP to enhance my icons.

gimp_1

It is calculated to be 16×16 pixels width and height. Precision is important here. Then I export the world toolbar as PNG : writesmall.png. It’s my toolbar for small icons. My last icons are positionned at 38th, 39th, 40th, 41th block of 16 pixels wide. Here is how it is converted into the Ribbon code creation:

pPanelFormat->Add(new CMFCRibbonButton(ID_FORMAT_ALIGN_LEFT, _T("Align Left\nal"), 38));
pPanelFormat->Add(new CMFCRibbonButton(ID_FORMAT_ALIGN_RIGHT, _T("Align Right\nar"), 39));
pPanelFormat->Add(new CMFCRibbonButton(ID_FORMAT_ALIGN_TOP, _T("Align Top\nat"), 40));
pPanelFormat->Add(new CMFCRibbonButton(ID_FORMAT_ALIGN_BOTTOM, _T("Align Bottom\nab"), 41));

Then I need to add the ID for the event corresponding to my ribbon icons:

#define ID_ID_FORMAT_ALIGN_LEFT 421
#define ID_ID_FORMAT_ALIGN_RIGHT 422
#define ID_ID_FORMAT_ALIGN_TOP 423
#define ID_ID_FORMAT_ALIGN_BOTTOM 424

Then, I need to add the UI handlers to this events in the messages map in cpp file and header file:

ON_COMMAND(ID_FORMAT_ALIGN_LEFT, &CModeler1View::OnFormatAlignLeft)
ON_UPDATE_COMMAND_UI(ID_FORMAT_ALIGN_LEFT, OnUpdateFormatAlignLeft)
ON_COMMAND(ID_FORMAT_ALIGN_RIGHT, &CModeler1View::OnFormatAlignRight)
ON_UPDATE_COMMAND_UI(ID_FORMAT_ALIGN_RIGHT, OnUpdateFormatAlignRight)
ON_COMMAND(ID_FORMAT_ALIGN_TOP, &CModeler1View::OnFormatAlignTop)
ON_UPDATE_COMMAND_UI(ID_FORMAT_ALIGN_TOP, OnUpdateFormatAlignTop)
ON_COMMAND(ID_FORMAT_ALIGN_BOTTOM, &CModeler1View::OnFormatAlignBottom)
ON_UPDATE_COMMAND_UI(ID_FORMAT_ALIGN_BOTTOM, OnUpdateFormatAlignBottom)
 afx_msg void OnFormatAlignLeft();
afx_msg void OnUpdateFormatAlignLeft(CCmdUI* pCmdUI);
afx_msg void OnFormatAlignRight();
afx_msg void OnUpdateFormatAlignRight(CCmdUI* pCmdUI);
afx_msg void OnFormatAlignTop();
afx_msg void OnUpdateFormatAlignTop(CCmdUI* pCmdUI);
afx_msg void OnFormatAlignBottom();
afx_msg void OnUpdateFormatAlignBottom(CCmdUI* pCmdUI);

You can see there is a handler and an update handler that determines if it is possible that handler to be enable or not. In our case, it will enabled only if there is an active selection of objects. Look at the code:

void CModeler1View::OnFormatAlignLeft()
{
    GetManager()->AlignLeft(this);
}

void CModeler1View::OnUpdateFormatAlignLeft(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(GetManager()->HasSelection());
}

void CModeler1View::OnFormatAlignRight()
{
    GetManager()->AlignRight(this);
}

void CModeler1View::OnUpdateFormatAlignRight(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(GetManager()->HasSelection());
}

void CModeler1View::OnFormatAlignTop()
{
    GetManager()->AlignTop(this);
}

void CModeler1View::OnUpdateFormatAlignTop(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(GetManager()->HasSelection());
}

void CModeler1View::OnFormatAlignBottom()
{
    GetManager()->AlignBottom(this);
}

void CModeler1View::OnUpdateFormatAlignBottom(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(GetManager()->HasSelection());
}

Here is the code for AlignLeft() routine:

void CElementManager::AlignLeft(CModeler1View* pView)
{
 if (HasSelection())
 {
  shared_ptr<CElement> pElementBase = m_selection.m_objects[0];

  for (vector<std::shared_ptr<CElement>>::const_iterator itSel = m_selection.m_objects.begin(); itSel != m_selection.m_objects.end(); itSel++)
  {
    std::shared_ptr<CElement> pObj = *itSel;

    int width = pObj->m_rect.Width();
    pObj->m_rect.left = pElementBase->m_rect.left;
    pObj->m_rect.right = pObj->m_rect.left + width;
    pObj->m_point = pObj->m_rect.TopLeft();
    InvalObj(pView, pObj);
   }

  pView->GetDocument()->SetModifiedFlag();
 }
}

When a selection is made, you can align objects ! It works !

ulnt_1

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: