RE: Design question: GtkTextView/Buffer



Given a textual representation of a structure how can I map pieces of
the text back to the internal representation?

Here is an example:  Given this XML:

     <frame id=1234>
     A fair bit of cdata text.
     </frame>

I want a textual representation that looks like:

     FRAME:

       A fair bit of cdata text.

The "FRAME:" should not be modifiable and I want to hide the value of
the id attribute.  It is the hiding of the attributes that is why I
need to map from the piece of text to the internal representation.
Further, the whole point of the text buffer approach is that the cdata
text should be easy to edit.

Setting pieces of text to be editable and none editable, is straight
forward:

  gtk_text_buffer_create_tag (buffer, "not_editable",
                              "editable", FALSE, NULL);

  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
                                            "This line can't be edited by
the user.\n", -1,
                                            "not_editable", NULL);

Looking up this frame id we be tricky, you could probably get away with
using searching through the buffer for the given tags near the key word
"FRAME" and using a tag for the invisible text.

  gtk_text_buffer_create_tag (buffer, "not_invisible",
                              "invisible", TRUE, NULL);
  gtk_text_buffer_create_tag (buffer, "left_margin",
                              "left-margin", 25, NULL);

  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
                                            "FRAME:", -1,
                                            "not_editable", NULL);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
                                            "id=69\n", -1,
                                            "not_visible", NULL);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
                                            "This is the CDATA\n", -1,
                                            "left_margin", NULL);

i.e.

        <tag='not_editable'>FRAME:<tag='invisible'>id=69\n</tag></tag>
                <tag='left_margin'>cdata\n</tag>

so the user would see:

        FRAME:
                cdata

and when looking up the id, you would search for the word "FRAME" in the
buffer using:
http://developer.gnome.org/doc/API/2.0/gtk/gtk-GtkTextIter.html#gtk-text-ite
r-forward-search

and with the returned _end_ iter you could then start using the
gtk_text_iter_has_tag() with gtk_text_iter_forward_char() to get the bounds
of the invisible text to get the ID.


I hope that I have given enough background for you to understand the
question that I am asking and I hope that you will take the time to
post some ideas.

Thanks in advance,


This may sound like a silly question, but why use a GtkTextView? Why not a
GtkTreeView? That way you can model your data (both hidden and visible) and
show what you want.  Then the only problem you would have, is knowing which
cells could be edited and which couldn't (which shouldn't be too hard to get
going).

Personally I would attempt to use a GtkTreeView first.

Regards,
Martyn



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]