Steve_BZ has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I'm trying to load from an XML or HTML file into Wx::RichTextCtrl.

I have 2 pieces of code:

  1. (see richtext_test.pl below) is the Perl file with Wx::RichTextHTMLHandler->LoadFile
  2. (see output.xml below) is the XML file I am trying to load (created by ->SaveFile)

The relevant piece of code is:

my $XML_handler = Wx::RichTextXMLHandler->new; $XML_handler->LoadFile ( my $XML_buffer, "output.xml"); $self->{rich_text_ctrl_1}->SetValue ( $XML_buffer );

Which creates a handler (no error), tries to load the file and crashes the Perl interpreter with no error message.

Whether I use this code, or the HTML equivalent, which does nothing, except not crash, and no error message. Either way $XML_buffer/$HTML_buffer remains resolutely empty.

What am I doing wrong?

All suggestions gratefully received.

Regards

Steve

Attachment 1 - richtext_test.pl

#!/usr/bin/perl -w -- use Wx 0.15 qw[:allclasses]; use strict; package MyFrame; use Wx qw[:everything]; use base qw(Wx::Frame); use base qw(Class::Accessor::Fast); use Wx qw(:richtextctrl :textctrl :font :sizer :color); __PACKAGE__->mk_accessors( qw(richtext stylesheet control) ); sub new { my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_ +; $style = wxDEFAULT_FRAME_STYLE unless defined $style; $self = $self->SUPER::new( undef, wxID_ANY, "Rich Text Control", w +xDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, "" ); $self->SetTitle("Rich Text Control"); $self->{Ctl_Post_Exam_Videos_Sizer_2} = Wx::BoxSizer->new(wxVERTIC +AL); # Top-level left-hand sizer (contains media) $self->{rich_text_ctrl_1} = Wx::RichTextCtrl->new( $self, -1, '', +[-1, -1], [800, 600] ); $self->richtext( $self->{rich_text_ctrl_1} ); my $sizer = Wx::BoxSizer->new( wxHORIZONTAL ); $sizer->Add( $self->{rich_text_ctrl_1}, 1, wxGROW|wxALL, 5 ); $self->SetSizerAndFit( $sizer ); $self->control ( $self ); my $XML_handler = Wx::RichTextXMLHandler->new; $XML_handler->LoadFile ( my $XML_buffer, "output.xml"); $self->{rich_text_ctrl_1}->SetValue ( $XML_buffer ); $self->SetSizer ($self->{Ctl_Post_Exam_Videos_Sizer_2}); $self->Layout(); return $self; ###### Updated, sorry I missed this bit out. package main; unless(caller){ local *Wx::App::OnInit = sub{1}; my $app = Wx::App->new(); Wx::InitAllImageHandlers(); my $frame_1 = MyFrame->new(); $app->SetTopWindow($frame_1); $frame_1->Show(1); $app->MainLoop(); } }

Attachment 2 - output.xml

<?xml version="1.0" encoding="UTF-8"?> <richtext version="1.0.0.0" xmlns="http://www.wxwidgets.org"> <paragraphlayout textcolor="#000000" fontsize="8" fontstyle="90" fon +tweight="90" fontunderlined="0" fontface="MS Shell Dlg 2" alignment=" +1" parspacingafter="10" parspacingbefore="0" linespacing="10"> <paragraph> <text>Test </text> </paragraph> </paragraphlayout> </richtext>

Replies are listed 'Best First'.
Re: Wx::RichTextXMLHandler->LoadFile fails to load file into buffer.
by ikegami (Patriarch) on Jan 13, 2010 at 20:29 UTC
    I think it's expecting a Wx::RichTextBuffer object, not an undefined scalar.

    Update: Try:

    my $xml_handler = Wx::RichTextXMLHandler->new(); $xml_handler->LoadFile( $self->{rich_text_ctrl_1}->GetBuffer(), "output.xml", );

      Hi ikegami

      This is what I thought it should be. Did it work for you? I could only get the AddHandler staff working that Anon sent in.

      Regards

      Steve

        It didn't crash, but that's the extent of my testing since the code you gave isn't runnable
Re: Wx::RichTextXMLHandler->LoadFile fails to load file into buffer.
by Anonymous Monk on Jan 13, 2010 at 21:14 UTC
    What am I doing wrong?

    You seem to be guessing.

    wxRichTextCtrl overview →⇉⇨ http://svn.wxwidgets.org/viewvc/wx/wxWidgets/trunk/samples/richtext/richtext.cpp?view=markup

    // Add extra handlers (plain text is automatically added) wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler); wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler); m_richTextCtrl->LoadFile(path, fileType); m_richTextCtrl->SaveFile(path); if (htmlHandler.SaveFile(& m_richTextCtrl->GetBuffer(), strStream) +) { win->SetPage(text); } wxGetApp().GetPrinting()->PrintBuffer(m_richTextCtrl->GetBuffer()); wxGetApp().GetPrinting()->PreviewBuffer(m_richTextCtrl->GetBuffer( +));
      In other words,
      $self->{rich_text_ctrl_1}->Load("output.xml");

      Hey Anon! Spot on. The correct code is

      my $XML_handler = Wx::RichTextXMLHandler->new; my $RTC_Buffer = $self->{rich_text_ctrl_1}->GetBuffer(); $RTC_Buffer ->AddHandler($XML_handler); $self->{rich_text_ctrl_1}->LoadFile("output.xml");

      Thanks and regards