horizontal_layout.cpp 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
#include "horizontal_layout.h"
#include "empty_layout.h"
extern "C" {
#include <assert.h>
#include <kandinsky.h>
#include <stdlib.h>
}

namespace Poincare {

ExpressionLayout * HorizontalLayout::clone() const {
  HorizontalLayout * layout = new HorizontalLayout(const_cast<ExpressionLayout **>(children()), numberOfChildren(), true);
  return layout;
}

void HorizontalLayout::deleteBeforeCursor(ExpressionLayoutCursor * cursor) {
  if (cursor->pointedExpressionLayout() == this
      && cursor->position() == ExpressionLayoutCursor::Position::Left
      && m_parent == nullptr)
  {
    // Case: Left and this is the main layout. Return.
    return;
  }
  if (cursor->pointedExpressionLayout() == this
      && cursor->position() == ExpressionLayoutCursor::Position::Right
      && m_parent == nullptr
      && numberOfChildren() == 0)
  {
    // Case: Right and this is the main layout with no children. Return.
    return;
  }
  if (cursor->position() == ExpressionLayoutCursor::Position::Left) {
    int indexOfPointedExpression = indexOfChild(cursor->pointedExpressionLayout());
    if (indexOfPointedExpression >= 0) {
      /* Case: Left of a child.
       * Point Right of the previous child. If there is no previous child, point
       * Left of this. Perform another backspace. */
      if (indexOfPointedExpression == 0) {
        cursor->setPointedExpressionLayout(this);
      } else if (indexOfPointedExpression > 0) {
        cursor->setPointedExpressionLayout(editableChild(indexOfPointedExpression - 1));
        cursor->setPosition(ExpressionLayoutCursor::Position::Right);
      }
      cursor->performBackspace();
      return;
    }
  }
  assert(cursor->pointedExpressionLayout() == this);
  if (cursor->position() == ExpressionLayoutCursor::Position::Right) {
    // Case: Right. Point to the last child and perform backspace.
    cursor->setPointedExpressionLayout(editableChild(numberOfChildren() - 1));
    cursor->performBackspace();
    return;
  }
  ExpressionLayout::deleteBeforeCursor(cursor);
}

void HorizontalLayout::replaceChild(const ExpressionLayout * oldChild, ExpressionLayout * newChild, bool deleteOldChild) {
  privateReplaceChild(oldChild, newChild, deleteOldChild, nullptr);
}

void HorizontalLayout::replaceChildAndMoveCursor(const ExpressionLayout * oldChild, ExpressionLayout * newChild, bool deleteOldChild, ExpressionLayoutCursor * cursor) {
  privateReplaceChild(oldChild, newChild, deleteOldChild, cursor);
}

void HorizontalLayout::privateReplaceChild(const ExpressionLayout * oldChild, ExpressionLayout * newChild, bool deleteOldChild, ExpressionLayoutCursor * cursor) {
  bool oldWasAncestorOfNewLayout = false;
  if (newChild->hasAncestor(this)) {
    newChild->editableParent()->detachChild(newChild);
    oldWasAncestorOfNewLayout = true;
  }
  int oldChildIndex = indexOfChild(oldChild);
  if (newChild->isEmpty()) {
    if (numberOfChildren() > 1) {
      /* If the new layout is empty and the horizontal layout has other
       * children, just delete the old child. */
      if (!newChild->hasAncestor(oldChild)) {
        delete newChild;
      }
      removeChildAtIndex(oldChildIndex, deleteOldChild);
      if (cursor == nullptr) {
        return;
      }
      if (oldChildIndex == 0) {
        cursor->setPointedExpressionLayout(this);
        cursor->setPosition(ExpressionLayoutCursor::Position::Left);
        return;
      }
      cursor->setPointedExpressionLayout(editableChild(oldChildIndex -1));
      cursor->setPosition(ExpressionLayoutCursor::Position::Right);
      return;
    }
    /* If the new layout is empty and it was the only horizontal layout child,
     * replace the horizontal layout with this empty layout (only if this is not
     * the main layout, so only if the layout has a parent). */
    if (m_parent) {
      if (!deleteOldChild) {
        removeChildAtIndex(indexOfChild(oldChild), false);
      }
      if (cursor) {
        replaceWithAndMoveCursor(newChild, true, cursor);
        return;
      }
      replaceWith(newChild, deleteOldChild);
      return;
    }
    /* If this is the main horizontal layout, the old child its only child and
     * the new child is Empty, remove the old child and delete the new child. */
    assert(m_parent == nullptr);
    removeChildAtIndex(0, deleteOldChild);
    delete newChild;
    if (cursor == nullptr) {
      return;
    }
    cursor->setPointedExpressionLayout(this);
    cursor->setPosition(ExpressionLayoutCursor::Position::Left);
    return;
  }
  /* If the new child is also an horizontal layout, steal the children of the
   * new layout then destroy it. */
  if (newChild->isHorizontal()) {
    int indexForInsertion = indexOfChild(oldChild);
    if (cursor != nullptr) {
      /* If the old layout is not an ancestor of the new layout, or if the
       * cursor was on the right of the new layout, place the cursor on the
       * right of the new layout, which is left of the next sibling or right of
       * the parent. */
      if (!oldWasAncestorOfNewLayout || cursor->position() == ExpressionLayoutCursor::Position::Right) {
        if (oldChildIndex == numberOfChildren() - 1) {
          cursor->setPointedExpressionLayout(this);
          cursor->setPosition(ExpressionLayoutCursor::Position::Right);
        } else {
          cursor->setPointedExpressionLayout(editableChild(oldChildIndex + 1));
          cursor->setPosition(ExpressionLayoutCursor::Position::Left);
        }
      } else {
        /* Else place the cursor on the left of the new layout, which is right
         * of the previous sibling or left of the parent. */
        if (oldChildIndex == 0) {
          cursor->setPointedExpressionLayout(this);
          cursor->setPosition(ExpressionLayoutCursor::Position::Left);
        } else {
          cursor->setPointedExpressionLayout(editableChild(oldChildIndex - 1));
          cursor->setPosition(ExpressionLayoutCursor::Position::Right);
        }
      }
    }
    bool oldChildRemovedAtMerge = oldChild->isEmpty();
    mergeChildrenAtIndex(static_cast<HorizontalLayout *>(newChild), indexForInsertion + 1, true);
    if (!oldChildRemovedAtMerge) {
      removeChildAtIndex(indexForInsertion, deleteOldChild);
    }
    return;
  }
  // Else, just replace the child.
  if (cursor != nullptr && !oldWasAncestorOfNewLayout) {
    cursor->setPosition(ExpressionLayoutCursor::Position::Right);
  }
  ExpressionLayout::replaceChild(oldChild, newChild, deleteOldChild);
  if (cursor == nullptr) {
    return;
  }
  cursor->setPointedExpressionLayout(newChild);
}

void HorizontalLayout::addOrMergeChildAtIndex(ExpressionLayout * eL, int index, bool removeEmptyChildren) {
  if (eL->isHorizontal()) {
    mergeChildrenAtIndex(static_cast<HorizontalLayout *>(eL), index, removeEmptyChildren);
  } else {
    addChildAtIndex(eL, index);
  }
}

ExpressionLayoutCursor HorizontalLayout::cursorLeftOf(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout) {
  // Case: Left. Ask the parent.
  if (cursor.pointedExpressionLayout() == this) {
    if (cursor.position() == ExpressionLayoutCursor::Position::Left) {
      if (m_parent) {
        return m_parent->cursorLeftOf(cursor, shouldRecomputeLayout);
      }
      return ExpressionLayoutCursor();
    }
    assert(cursor.position() == ExpressionLayoutCursor::Position::Right);
    /* Case: Right.
     * Go to the last child if there is one, and move Left.
     * Else go Left and ask the parent. */
    if (numberOfChildren() < 1) {
      cursor.setPosition(ExpressionLayoutCursor::Position::Left);
      if (m_parent) {
        return m_parent->cursorLeftOf(cursor, shouldRecomputeLayout);
      }
      return ExpressionLayoutCursor();
    }
    ExpressionLayout * lastChild = editableChild(numberOfChildren()-1);
    assert(lastChild != nullptr);
    cursor.setPointedExpressionLayout(lastChild);
    return lastChild->cursorLeftOf(cursor, shouldRecomputeLayout);
  }

  // Case: The cursor is Left of a child.
  assert(cursor.position() == ExpressionLayoutCursor::Position::Left);
  int childIndex = indexOfChild(cursor.pointedExpressionLayout());
  assert(childIndex >= 0);
  if (childIndex == 0) {
    // Case: the child is the leftmost. Ask the parent.
    if (m_parent) {
      cursor.setPointedExpressionLayout(this);
      return m_parent->cursorLeftOf(cursor, shouldRecomputeLayout);
    }
    return ExpressionLayoutCursor();
  }
  // Case: the child is not the leftmost. Go to its left sibling and move Left.
  cursor.setPointedExpressionLayout(editableChild(childIndex-1));
  cursor.setPosition(ExpressionLayoutCursor::Position::Right);
  return editableChild(childIndex-1)->cursorLeftOf(cursor, shouldRecomputeLayout);
}

ExpressionLayoutCursor HorizontalLayout::cursorRightOf(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout) {
  // Case: Right. Ask the parent.
  if (cursor.pointedExpressionLayout() == this) {
    if (cursor.position() == ExpressionLayoutCursor::Position::Right) {
      if (m_parent) {
        return m_parent->cursorRightOf(cursor, shouldRecomputeLayout);
      }
      return ExpressionLayoutCursor();
    }
    assert(cursor.position() == ExpressionLayoutCursor::Position::Left);
    /* Case: Left.
     * Go to the first child if there is one, and move Right.
     * Else go Right and ask the parent. */
    if (numberOfChildren() < 1) {
      cursor.setPosition(ExpressionLayoutCursor::Position::Right);
      if (m_parent) {
        return m_parent->cursorRightOf(cursor, shouldRecomputeLayout);
      }
      return ExpressionLayoutCursor();
    }
    ExpressionLayout * firstChild = editableChild(0);
    assert(firstChild != nullptr);
    cursor.setPointedExpressionLayout(firstChild);
    return firstChild->cursorRightOf(cursor, shouldRecomputeLayout);
  }

  // Case: The cursor is Right of a child.
  assert(cursor.position() == ExpressionLayoutCursor::Position::Right);
  int childIndex = indexOfChild(cursor.pointedExpressionLayout());
  assert(childIndex >= 0);
  if (childIndex == numberOfChildren() - 1) {
    // Case: the child is the rightmost. Ask the parent.
    if (m_parent) {
      cursor.setPointedExpressionLayout(this);
      return m_parent->cursorRightOf(cursor, shouldRecomputeLayout);
    }
    return ExpressionLayoutCursor();
  }
  /* Case: the child is not the rightmost. Go to its right sibling and move
   * Right. */
  cursor.setPointedExpressionLayout(editableChild(childIndex+1));
  cursor.setPosition(ExpressionLayoutCursor::Position::Left);
  return editableChild(childIndex+1)->cursorRightOf(cursor, shouldRecomputeLayout);
}

void HorizontalLayout::addChildrenAtIndex(const ExpressionLayout * const * operands, int numberOfOperands, int indexForInsertion, bool removeEmptyChildren) {
  int newIndex = removeEmptyChildBeforeInsertionAtIndex(indexForInsertion, !operands[0]->mustHaveLeftSibling());
  DynamicLayoutHierarchy::addChildrenAtIndex(operands, numberOfOperands, newIndex, removeEmptyChildren);
}

bool HorizontalLayout::addChildAtIndex(ExpressionLayout * operand, int index) {
  int newIndex = removeEmptyChildBeforeInsertionAtIndex(index, !operand->mustHaveLeftSibling());
  return DynamicLayoutHierarchy::addChildAtIndex(operand, newIndex);
}

void HorizontalLayout::removeChildAtIndex(int index, bool deleteAfterRemoval) {
  privateRemoveChildAtIndex(index, deleteAfterRemoval, false);
}

void HorizontalLayout::mergeChildrenAtIndex(DynamicLayoutHierarchy * eL, int index, bool removeEmptyChildren) {
  /* Before the merge, remove ay empty child that would be on the left or on the
   * right of the inserted layout.
   * If the layout to insert starts with a vertical offset layout, any empty
   * layout child directly on the left of the inserted layout (if there is one)
   * should not be removed: it will be the base for the VerticalOffsetLayout. */
  bool shouldRemoveOnLeft = eL->numberOfChildren() > 0 ? !(eL->child(0)->mustHaveLeftSibling()) : true;
  int newIndex = removeEmptyChildBeforeInsertionAtIndex(index, shouldRemoveOnLeft);
  DynamicLayoutHierarchy::mergeChildrenAtIndex(eL, newIndex, removeEmptyChildren);
}

int HorizontalLayout::writeTextInBuffer(char * buffer, int bufferSize) const {
  if (numberOfChildren() == 0) {
    if (bufferSize == 0) {
      return -1;
    }
    buffer[0] = 0;
    return 0;
  }
  return LayoutEngine::writeInfixExpressionLayoutTextInBuffer(this, buffer, bufferSize, "");
}

ExpressionLayoutCursor HorizontalLayout::equivalentCursor(ExpressionLayoutCursor cursor) {
  ExpressionLayoutCursor result;
  ExpressionLayout * newPointedLayout = nullptr;
  ExpressionLayoutCursor::Position newPosition = ExpressionLayoutCursor::Position::Left;
  if (cursor.pointedExpressionLayout() == this) {
    // First or last child, if any
    if(numberOfChildren() == 0) {
      return result;
    } else {
      newPointedLayout = editableChild(cursor.position() == ExpressionLayoutCursor::Position::Left ? 0 : numberOfChildren() - 1);
      newPosition = cursor.position();
    }
  } else {
    // Left or right child
    int indexOfPointedLayout = indexOfChild(cursor.pointedExpressionLayout());
    if (indexOfPointedLayout < 0) {
      return result;
    } else if (cursor.position() == ExpressionLayoutCursor::Position::Left) {
      if (indexOfPointedLayout == 0) {
        newPointedLayout = this;
        newPosition = ExpressionLayoutCursor::Position::Left;
      } else {
        newPointedLayout = editableChild(indexOfPointedLayout - 1);
        newPosition = ExpressionLayoutCursor::Position::Right;
      }
    } else {
      assert(cursor.position() == ExpressionLayoutCursor::Position::Right);
      if (indexOfPointedLayout == numberOfChildren() - 1) {
        newPointedLayout = this;
        newPosition = ExpressionLayoutCursor::Position::Right;
      } else {
        newPointedLayout = editableChild(indexOfPointedLayout + 1);
        newPosition = ExpressionLayoutCursor::Position::Left;
      }
    }
  }
  result.setPointedExpressionLayout(newPointedLayout);
  result.setPosition(newPosition);
  return result;
}

KDSize HorizontalLayout::computeSize() {
  KDCoordinate totalWidth = 0;
  int i = 0;
  KDCoordinate max_under_baseline = 0;
  KDCoordinate max_above_baseline = 0;
  while (ExpressionLayout * c = editableChild(i++)) {
    KDSize childSize = c->size();
    totalWidth += childSize.width();
    if (childSize.height() - c->baseline() > max_under_baseline) {
      max_under_baseline = childSize.height() - c->baseline() ;
    }
    if (c->baseline() > max_above_baseline) {
      max_above_baseline = c->baseline();
    }
  }
  return KDSize(totalWidth, max_under_baseline + max_above_baseline);
}

void HorizontalLayout::computeBaseline() {
  m_baseline = 0;
  for (int i = 0; i < numberOfChildren(); i++) {
    if (editableChild(i)->baseline() > m_baseline) {
      m_baseline = editableChild(i)->baseline();
    }
  }
  m_baselined = true;
}

KDPoint HorizontalLayout::positionOfChild(ExpressionLayout * child) {
  KDCoordinate x = 0;
  KDCoordinate y = 0;
  int index = indexOfChild(child);
  if (index > 0) {
    ExpressionLayout * previousChild = editableChild(index-1);
    assert(previousChild != nullptr);
    x = previousChild->origin().x() + previousChild->size().width();
  }
  y = baseline() - child->baseline();
  return KDPoint(x, y);
}

void HorizontalLayout::privateAddSibling(ExpressionLayoutCursor * cursor, ExpressionLayout * sibling, bool moveCursor) {
  // Add the "sibling" as a child.
  if (cursor->position() == ExpressionLayoutCursor::Position::Left) {
    int indexForInsertion = 0;
    /* If the first child is empty, remove it before adding the layout, unless
     * the new sibling needs the empty layout as a base. */
    if (numberOfChildren() > 0 && editableChild(0)->isEmpty()) {
      if (sibling->mustHaveLeftSibling()) {
        indexForInsertion = 1;
      } else {
        /* We force the removing of the child even followed by a neighbourg
         * requiring a left sibling as we are about to add a sibling in first
         * position anyway. */
        privateRemoveChildAtIndex(0, true, true);
      }
    }
    if (moveCursor) {
      if (numberOfChildren() > indexForInsertion) {
        cursor->setPointedExpressionLayout(editableChild(indexForInsertion));
      } else {
        cursor->setPointedExpressionLayout(this);
        cursor->setPosition(ExpressionLayoutCursor::Position::Right);
      }
    }
    addOrMergeChildAtIndex(sibling, indexForInsertion, false);
    return;
  }
  assert(cursor->position() == ExpressionLayoutCursor::Position::Right);
  // If the last child is empty, remove it before adding the layout.
  int childrenCount = numberOfChildren();
  if (childrenCount > 0 && editableChild(childrenCount - 1)->isEmpty() && !sibling->mustHaveLeftSibling()) {
    /* Force remove the last child. */
    privateRemoveChildAtIndex(childrenCount - 1, true, true);
  }
  addOrMergeChildAtIndex(sibling, numberOfChildren(), false);
  if (moveCursor) {
    cursor->setPointedExpressionLayout(this);
  }
}

void HorizontalLayout::privateRemoveChildAtIndex(int index, bool deleteAfterRemoval, bool forceRemove) {
  /* Remove the child before potentially adding an EmptyLayout. Indeed, adding
   * a new child would remove any EmptyLayout surrounding the new child and in
   * the case the child to be removed was an Empty layout, it would result in
   * removing it twice if we remove it afterwards. */
  DynamicLayoutHierarchy::removeChildAtIndex(index, deleteAfterRemoval);
  /* If the child to remove is at index 0 and its right sibling must have a left
   * sibling (e.g. it is a VerticalOffsetLayout), replace the child with an
   * EmptyLayout instead of removing it. */
  if (!forceRemove && index == 0 && numberOfChildren() > 0 && child(0)->mustHaveLeftSibling()) {
    addChildAtIndex(new EmptyLayout(), 0);
  }
}

int HorizontalLayout::removeEmptyChildBeforeInsertionAtIndex(int index, bool shouldRemoveOnLeft) {
  int newIndex = index;
  /* If empty, remove the child that would be on the right of the inserted
   * layout. */
  if (newIndex < numberOfChildren()
      && child(newIndex)->isEmpty())
  {
    privateRemoveChildAtIndex(newIndex, true, true);
  }
  /* If empty, remove the child that would be on the left of the inserted
   * layout. */
  if (shouldRemoveOnLeft
      && newIndex - 1 >= 0
      && newIndex - 1 <= numberOfChildren() -1
      && child(newIndex - 1)->isEmpty())
  {
    privateRemoveChildAtIndex(newIndex-1, true, true);
    newIndex = index - 1;
  }
  return newIndex;
}

}