Ejemplo n.º 1
0
bool nuiPopupMenu::KeyDown(const nglKeyEvent& rEvent)
{
  mSelectionTimer.Stop();
  nuiTreeNode* pNode = (mpNewSelectedNode ? mpNewSelectedNode : mpSelectedNode);
  if (pNode)
  {
    nuiRect WidgetRect = pNode->GetElement()->GetRect();
    nuiRect r = WidgetRect;
    nuiMenuRect*  pRect= mRects[pNode->GetDepth()-1]; /// get the current menuRect
    nuiSize TreeHandleSize = pRect->mHasNonEmpty ? NUI_POPUP_TREE_HANDLE_SIZE : 0.f;
    r.SetSize(WidgetRect.GetWidth()+TreeHandleSize, WidgetRect.GetHeight());
    Invalidate(); //(r);
  }

  if (rEvent.mKey == NK_UP)
  {
    if (mpSelectedNode && !mpSelectedNode->IsOpened())
      pNode = GetNextNode(GetParentNode(mpTree, mpSelectedNode), mpSelectedNode, -1);
    else
    { ///< find the first non disabled element node from end
      nuiTreeNode* pParent = mpSelectedNode ? mpSelectedNode : mpTree;
      uint32 count = pParent->GetChildrenCount();
      for (int i = count - 1; i >= 0; i--)
      {
        pNode = dynamic_cast<nuiTreeNode*>(pParent->GetChild(i));
        if (pNode->GetElement()->IsDisabled())
        {
          pNode=NULL;
          continue;
        }
        else
          break;
      }
      pNode = pNode ? pNode : mpSelectedNode ? mpSelectedNode : NULL;
    }
    if (!pNode)
      return true;

    nuiRect WidgetRect = pNode->GetElement()->GetRect();
    nuiRect r = WidgetRect;
    nuiMenuRect*  pRect= mRects[pNode->GetDepth()-1]; /// get the current menuRect
    nuiSize TreeHandleSize = pRect->mHasNonEmpty ? NUI_POPUP_TREE_HANDLE_SIZE : 0.f;
    r.SetSize(WidgetRect.GetWidth()+TreeHandleSize, WidgetRect.GetHeight());
    Invalidate(); //(r);

    if (mpSelectedNode && !mpSelectedNode->IsOpened())
    {
      mpSelectedNode->Select(false);
      nuiRect WidgetRect = mpSelectedNode->GetElement()->GetRect();
      nuiRect r = WidgetRect;
      nuiMenuRect*  pRect= mRects[mpSelectedNode->GetDepth()-1]; /// get the current menuRect
      nuiSize TreeHandleSize = pRect->mHasNonEmpty ? NUI_POPUP_TREE_HANDLE_SIZE : 0.f;
      r.SetSize(WidgetRect.GetWidth()+TreeHandleSize, WidgetRect.GetHeight());
      Invalidate(); //(r); 
    }

    mpSelectedNode = pNode;
    pNode->Select(true);

    AdjustScrolling(pNode);
    FillSelectedNodes();
    return true;
  }
  else if (rEvent.mKey == NK_DOWN)
  {
    if (mpSelectedNode && !mpSelectedNode->IsOpened())
      pNode = GetNextNode(GetParentNode(mpTree, mpSelectedNode), mpSelectedNode, 1);
    else
    { ///< find the first non disabled element node from top
      nuiTreeNode* pParent = mpSelectedNode ? mpSelectedNode : mpTree;
      uint32 count = pParent->GetChildrenCount();
      for (uint32 i = 0; i < count; i++)
      {
        pNode = dynamic_cast<nuiTreeNode*>(pParent->GetChild(i));
        if (pNode->GetElement()->IsDisabled())
        {
          pNode=NULL;
          continue;
        }
        else
          break;
      }
      pNode = pNode ? pNode : mpSelectedNode ? mpSelectedNode : NULL;
    }
    if (!pNode)
      return true;

    nuiRect WidgetRect = pNode->GetElement()->GetRect();
    nuiRect r = WidgetRect;
    nuiMenuRect*  pRect= mRects[pNode->GetDepth()-1]; /// get the current menuRect
    nuiSize TreeHandleSize = pRect->mHasNonEmpty ? NUI_POPUP_TREE_HANDLE_SIZE : 0.f;
    r.SetSize(WidgetRect.GetWidth()+TreeHandleSize, WidgetRect.GetHeight());
    Invalidate(); //(r);

    if (mpSelectedNode && !mpSelectedNode->IsOpened())
    {
      mpSelectedNode->Select(false);

      nuiRect WidgetRect = mpSelectedNode->GetElement()->GetRect();
      nuiRect r = WidgetRect;
      nuiMenuRect*  pRect= mRects[mpSelectedNode->GetDepth()-1]; /// get the current menuRect
      nuiSize TreeHandleSize = pRect->mHasNonEmpty ? NUI_POPUP_TREE_HANDLE_SIZE : 0.f;
      r.SetSize(WidgetRect.GetWidth()+TreeHandleSize, WidgetRect.GetHeight());
      Invalidate(); //(r);
    }
    mpSelectedNode = pNode;

    pNode->Select(true);
    AdjustScrolling(pNode);
    FillSelectedNodes();
    return true;  
  }
  else if (rEvent.mKey == NK_LEFT)
  {
    if (mpSelectedNode)
    {
      if (mpSelectedNode->IsOpened())
      {
        mpSelectedNode->Open(false);
        FillSelectedNodes();
        InvalidateLayout();
        Invalidate();
        return true;
      }
      else if ((pNode = GetParentNode(mpTree, mpSelectedNode)) != mpTree)
      {
        mpSelectedNode->Select(false);
        mpSelectedNode = pNode;
        mpSelectedNode->Open(false);
        FillSelectedNodes();
        InvalidateLayout();
        Invalidate();
        return true;
      }
    }
    Invalidate();
    InvalidateLayout();
    return false;
  }
  else if (rEvent.mKey == NK_RIGHT)
  {
    if (mpSelectedNode && !mpSelectedNode->IsEmpty())
    {
      mpSelectedNode->Open(true);
      if (!mpSelectedNode->GetChildrenCount())
      {
        mpSelectedNode->Open(false);
        return false;
      }
      ResetScrolling(mpSelectedNode->GetDepth());
      mpSelectedNode = dynamic_cast<nuiTreeNode*>(mpSelectedNode->GetChild(0));
      mpSelectedNode->Select(true);
      Invalidate();
      InvalidateLayout();
      FillSelectedNodes();
      return true;
    }
    Invalidate();
    InvalidateLayout();
    return false;
  }
  else if (rEvent.mKey == NK_ENTER || rEvent.mKey == NK_PAD_ENTER || rEvent.mKey == NK_SPACE)
  {
    if (mpSelectedNode && !mpSelectedNode->IsEmpty()) // maybe open
    {
      mpSelectedNode->Open(true);
      if (!mpSelectedNode->GetChildrenCount())
      {
        mpSelectedNode->Open(false);
        return false;
      }
      else
      {
        ResetScrolling(mpSelectedNode->GetDepth());
        mpSelectedNode = dynamic_cast<nuiTreeNode*>(mpSelectedNode->GetChild(0));
        mpSelectedNode->Select(true);
        Invalidate();
        InvalidateLayout();
        FillSelectedNodes();
        return true;
      }
    }
    // selection done
    FillSelectedNodes();
    if (mpSelectedNode)
      mpSelectedNode->Activated();
    if (!MenuDone())
      Trash();
    else
      RemoveMenu();
    return true;
  }
  else if (rEvent.mKey == NK_ESC)
  {
    mpTree->OpenAllChildren(false);
    mpTree->SelectAllChildren(false);
    mpSelectedNode = NULL;

    FillSelectedNodes();
    if (!MenuDone())
      Trash();
    else
      RemoveMenu();
    return true;
  }
  else if (rEvent.mChar)
  {
    int index = -1;
    nuiTreeNodePtr pParent = mpTree;
    if (mpSelectedNode)
    {
      pParent = (nuiTreeNodePtr)mpSelectedNode->GetParent();
      uint count = pParent->GetChildrenCount();
      for (uint i = 0; i < count && index == -1; i++)
      {
        if (mpSelectedNode == pParent->GetChild(i))
        {
          index = i;
        }
      }
    }

    if (pParent)
    {
      index++;

      nuiTreeNodePtr pNode = NULL;
      uint count = pParent->GetChildrenCount();
      NGL_OUT(_T("Start search\n"));
      for (uint i = 0; i < count && !pNode; i++)
      {
        nuiTreeNodePtr pN = (nuiTreeNodePtr)pParent->GetChild((i + index) % count);
        nuiWidget* pItem = pN->GetElement();
        if (pItem->IsEnabled())
        {
          if (mKey.IsEmpty())
          {
            nuiLabel* pLabel = SearchLabel(pItem);
            if (pLabel)
            {
              const nglString& rKey = pLabel->GetText();
              if (toupper(rKey[0]) == rEvent.mChar)
              {
                pNode = pN;
              }
            }
          }
          else
          {
            const nglString& rKey = pN->GetProperty(mKey);
            if (toupper(rKey[0]) == rEvent.mChar)
            {
              pNode = pN;
            }
          }
        }
      }

      if (pNode)
      {
        if (mpSelectedNode)
        {
          mpSelectedNode->Select(false);
          if (mpSelectedNode->IsOpened())
            mpSelectedNode->Open(false);
        }
        mpSelectedNode = pNode;

        pNode->Select(true);
        AdjustScrolling(pNode);
        FillSelectedNodes();
      }
    }
    return true;
  }
  return false;
}
Ejemplo n.º 2
0
/**
 * Search a label by name in label list.
 */
Label* SearchLabelByName(LabelList *lblList, const char *name)
{
	return SearchLabel(lblList, __SearchLabelByName(name));
}