char* TextSelection::GetText () { if (anchor.GetParent() == moving.GetParent() && anchor.GetLocation () == moving.GetLocation()) return g_strdup (""); GString *gstr = g_string_new (""); TextPointer tp = anchor; while (tp.CompareTo_np (moving) < 0) { if (tp.GetParent()->Is (Type::RUN)) { Run *run = (Run*)tp.GetParent(); if (tp.GetParent() == moving.GetParent()) { // tp and moving are in the same element, so we append the substring and set tp = moving. g_string_append_len (gstr, run->GetText() + tp.ResolveLocation(), moving.ResolveLocation() - tp.ResolveLocation()); tp = moving; } else { g_string_append (gstr, run->GetText()); tp = run->GetContentEnd_np(); tp = tp.GetPositionAtOffset_np (1, tp.GetLogicalDirection()); } } else { tp = tp.GetPositionAtOffset_np (1, tp.GetLogicalDirection()); } } printf ("returning %s from TextSelection::GetText\n", gstr->str); return g_string_free (gstr, FALSE); }
char* TextSelection::GetText () { GString *gstr; TextPointer tp; if (text) goto done; if (anchor.GetParent() == NULL || moving.GetParent() == NULL) { text = g_strdup (""); goto done; } if (anchor.GetParent() == moving.GetParent() && anchor.GetLocation () == moving.GetLocation()) { text = g_strdup (""); goto done; } gstr = g_string_new (""); tp = anchor; while (tp.CompareTo_np (moving) < 0) { DependencyObject *parent = tp.GetParent (); if (parent && parent->Is (Type::RUN)) { Run *run = (Run*)parent; if (parent == moving.GetParent()) { // tp and moving are in the same element, so we append the substring and set tp = moving. g_string_append_len (gstr, run->GetText() + tp.ResolveLocation(), moving.ResolveLocation() - tp.ResolveLocation()); tp = moving; } else { g_string_append (gstr, run->GetText()); tp = run->GetContentEnd_np(); tp = tp.GetPositionAtOffset_np (1, tp.GetLogicalDirection()); } } else { TextPointer new_tp = tp.GetPositionAtOffset_np (1, tp.GetLogicalDirection()); if (new_tp.CompareTo_np (tp) == 0) break; tp = new_tp; } } text = g_string_free (gstr, FALSE); done: return g_strdup (text); }
void TextSelection::ClearSelection () { // clear out the selection if (anchor.GetParent() == moving.GetParent()) { bool remove_element = false; if (anchor.GetLocation () != moving.GetLocation()) { if (anchor.GetParent()->Is (Type::RUN)) { Run *run = (Run*)anchor.GetParent(); char *run_text = g_strdup (run->GetText()); if (run_text) { int length_to_remove = moving.ResolveLocation() - anchor.ResolveLocation(); memmove (run_text + anchor.ResolveLocation(), run_text + moving.ResolveLocation (), strlen (run_text + moving.ResolveLocation())); *(run_text + strlen(run_text) - length_to_remove) = 0; run->SetText (run_text); // we need to remove the run if we've cleared all the text in it remove_element = !*run_text; } else { remove_element = true; } g_free (run_text); } else { IDocumentNode *node = anchor.GetParentNode(); int length_to_remove = moving.ResolveLocation() - anchor.ResolveLocation(); for (int i = 0; i < length_to_remove; i ++) node->GetDocumentChildren()->RemoveAt (anchor.ResolveLocation()); // we need to remove the element if we've removed all the children remove_element = node->GetDocumentChildren()->GetCount() == 0; } } DependencyObject *el = anchor.GetParent(); while (remove_element) { if (el->Is (Type::RICHTEXTBOX)) break; DependencyObject* parent = el->GetParent() ? el->GetParent()->GetParent() : NULL; if (!parent) { g_warning ("shouldn't happen"); return; } IDocumentNode *parent_node = IDocumentNode::CastToIDocumentNode (parent); parent_node->GetDocumentChildren()->Remove (Value(el)); el = parent; remove_element = parent_node->GetDocumentChildren()->GetCount() == 0; } } else { // harder case, anchor/moving are in different elements printf ("NIEX hard case TextSelection::ClearSelection\n"); } g_free (text); text = NULL; g_free (xaml); xaml = NULL; }