[Gtk-sharp-list] Re: [daemon@gigolo.li: [Mono-list] TextIter, TextBuffer issue]

Charles Iliya Krempeaux charles@reptile.ca
25 Feb 2003 09:44:57 -0800


I don't know if you've subscribed to the Gtk# Mailing List (yet), but
I've CC'ed this to it too.  (So everyone, remember to do a "Reply All".)

On Mon, 2003-02-24 at 14:07, Josef El-Rayes wrote:
> ----- Forwarded message from Josef El-Rayes <daemon@gigolo.li> -----
> From: Josef El-Rayes <daemon@gigolo.li>
> To: mono-list@lists.ximian.com
> User-Agent: Mutt/1.4i
> Subject: [Mono-list] TextIter, TextBuffer issue
> Date: Sat, 22 Feb 2003 19:32:06 +0100
> hi!
> today i noticed some strange behaviour of TextIter.
> I did not know that an TextIter object has to be created by
> initialising it with the return value of the StartIter method from
> TextBuffer.

Well, the first thing you may want to refer to is the C API 
documentation for GtkTextIter.  (I.e., the C version of Gtk.TextIter.)

You can refer to this at:


The docs for this says:

  You may wish to begin by reading the text widget conceptual
  overview which gives an overview of all the objects and data
  types related to the text widget and how they work together.

And give the following link:


(I suggest you read this.)

At the bottom of this, there is an example of using all this.

>From what I can tell, you have to get your Gtk.TextIter's from
a Gtk.TextBuff.  So... do that.

(I do also see that the C# version needs some tweeking of the
API... I'll do that and send a patch.)

> i declared a TextIter variable with 'TextIter ti' and initialized it with
> new. (TextIter ti = new TextIter();) The compiler did not complain so i
> thought this is okay (to call it with standard constructor).

You should get your Gtk.TextIter's from a Gtk.TextBuff.  For example,

    Gtk.TextBuff buff;

    // ...

    Gtk.TextIter iter;

    // ...

    int offset = 3;
    buff.GetIterAtOffset(out iter, offset);

Or something like that.

Note, I'm going to be creating a patch that will let you get a
Gtk.TextIter with something like:

    iter = buff.ObtainIterAtOffset(offset);

(This is probably how most people expect this to work.)

And if the Patch gets accepted, you can (eventually) do this
this way.

> as i wanted to access the TextIter Property 'LineIndex' i got a
> System.NullReferenceException. This was a bit confusing because i thought i
> should get something like '0'. 

I think the problem is that the Gtk.TextIter isn't attached to a 
Gtk.TextBuff, so....  It can't return '0' because, what would that
mean?  The zeroth index of what???  (It needs an associated Gtk.TextBuff
to be an index into.)

Remember, an "Iterator" (like Gtk.TextIter) is something completely
different than an "Index".

And "Iterator" is a combination of an "Index" and the thing which
the "Index" refers to.

> I thought maybe the field which is returned by get part of this property
> doesnt get initalised so i did a 'ti.LineIndex = 0;' before i did the get
> but that did not change anything.

Again, I think the Gtk.TextIter needs to be associated with a 
Gtk.TextBuff for it to work.  And, you get it associated with
a Gtk.TextBuff through a method similar to the code I previously
showed you.

> after quite some time, with the help of Johannes and rapha in #mono, we
> found out that TextBuffer can not be called with standard constructor
> (although this is allowed by compiler) because TextIter needs some input
> from TextBuffer (this is how it is implemented in Gtk+).
> now i ask myself to question:
> - is it possible to change constructors of TextIter so it is not possible to
> call it with standardconstructor,

Ya... we could make it a protected or private constructor.  (If we have
to have that constructor.)

I'll let others give their opinions on that first, before writing a
Patch to do it.

> but call it with an textbuffer object, so
> it works correctly and not get such a strange behaviour.

You can already do that... just look at the code I wrote above.

> - is it really necessary to have a seperate TextIter object when it is so
> thightly bound with TextBuffer? Why cant this TextIter get integrated in the
> TextBuffer class - which would also fix this mssbehaviour.

It is done this way thus far, because this is the way the C version of
Gtk does it.

Doing it differently would likely confuse people used to the C version
of this.

> i have to admit i am new to gtk# and i know that it is not easy to get some
> OOP into C code. i also want to add that i appreciate your work.

Hope that helps.

See ya

     Charles Iliya Krempeaux, BSc

 Reptile Consulting & Services    604-REPTILE    http://www.reptile.ca/