I've been struggling with this for almost a day now and can't seem to find a fix. I have a wxwidget app that I need determine which word was clicked on. I'm able to do this with the exception of the first word after the first row. It always gets the suffix but the prefix is truncated depending on where I click in the word. I've posted my example code below.
#!c:/perl/bin/perl.exe use strict; use warnings; use Wx qw[:everything]; package main; unless(caller){ local *Wx::App::OnInit = sub{1}; my $app = Wx::App->new(); Wx::InitAllImageHandlers(); my $ui = Test_ui->new(); $app->SetTopWindow($ui); $ui->Show(1); $app->MainLoop(); } package Test_ui; use Wx qw[:everything :allclasses]; use Wx::Event qw(EVT_LEFT_UP); our $mainframe; our @keywords=(); use base qw(Wx::Frame); sub new { my( $self, $parent, $id, $title, $pos, $size, $style, $name ) += @_; $parent = undef unless defined $parent; $id = -1 unless defined $id; $title = "" unless defined $title; $pos = wxDefaultPosition unless defined $pos; $size = wxDefaultSize unless defined $size; $name = "" unless defined $name; $style = wxDEFAULT_FRAME_STYLE unless defined $style; $self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $style, $name ); $mainframe=$self; my $main_sizer=Wx::BoxSizer->new(wxVERTICAL); $self->{keywords_text} = Wx::TextCtrl->new($self, -1, "This is a string that contains wrods and other goodie +s for my example. My problem is extracting the first word with multi +ple lines", wxDefaultPosition, wxDefaultSize, wxTE_READONLY|wxTE_MULTILINE|wxTE_WORDWRAP); $main_sizer->Add( $self->{keywords_text}, 1, wxEXPAND|wxALL, 2 +); $self->SetAutoLayout(1); $self->SetSizer($main_sizer); $self->Layout(); EVT_LEFT_UP( $self->{keywords_text}, sub { my ($self, $event) = @_; my $ip=$self->GetInsertionPoint(); my ($col,$row)=$self->PositionToXY($ip); print "row: $row\n"; print "col: $col\n"; $self->ReleaseMouse(); my $clickedline=$self->GetLineText($row); #print "clickedline : $clickedline\n"; my ($prefix) = ""; if ( $row > 0 ) { my $new_col = ($col > 0) ? ($col - $row) : $col; ($prefix) = substr($clickedline,$row,$new_col) =~ /([(\/|\ +:|\.|\n)?\w+]+)$/; } else { ($prefix) = substr($clickedline,$row,$col) =~ /([(\/|\:|\. +|\@)?\w+]+)$/; } my ($suffix)= substr($clickedline,$col,1000) =~ /^([(\/|\:|\.) +?\w+'`]+)/; my $clickedword=$prefix.$suffix; print "Prefix: $prefix\n"; print "suffix: $suffix\n"; print "clicked word: $clickedword\n"; }); return $self; } 1;
Any ideas? am I calculating the substring wrong or is there a hidden character in the wordwrap string that I'm missing?

Any help would be greatly appreciated.


In reply to substring and wordwrap issue by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.