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); }