Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need to be able to display images and text (images resizable and text editable). I was thinking maybe a HTML type thing could do this. For a really bad example like how a word processor works. Typing and editing text and pasting and editing images.

Thanks in advance, sorry for the impossible (to me) task.

EDIT: I would also like to be able to save this somehow.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You can actually do this using a TextView widget. First you should set editable to true:

textview.set_editable(true)

You will need to get its textbuffer which is the textviews data:

buffer = textview.get_buffer()

Then using that buffer you can get and set the data in it. to get the text currently in the buffer use:

buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter(), false)

The first and second arguments are the range to get the text, in this case all off it. The last argument specifies whether you would like to get hidden characters, in this case false. To insert text you can use the following:

buffer.insert(buffer.get_end_iter(), "Sample text")

The first argument specifies where to insert the text, in this case at the end. the second argument is the text to insert. To replace all the data in the buffer you can use:

buffer.set_text("Sample text")

This will replace everything in the buffer with "Sample text". To insert an image you need to create a pixbuf to do this use the following:

pixbuf = gtk.gdk.pixbuf_new_from_file(filename)

To resize the pixbuf use:

pixbuf.scale_simple(dest_width, dest_height, gtk.gdk.INTERP_BILINEAR)

The first two arguments are the new width and height, the third argument is the interpolation type which controls how it scales (there are four types which can be found here). You can then add this pixbuf to the buffer using:

buffer.insert_pixbuf(buffer.get_end_iter(), pixbuf)

This will insert an image at the end of the buffer.

You can also do things like add widgets, set margins, copy and paste from clipboard, insert at the cursor, etc. but you can look at the links below for that.

Edit: For a formatted save/load you can use the textbuffers serialization method. First you should use its register_serialize_tagset() method:

format = buffer.register_serialize_tagset()

Next you can use its serialize method to get the serialized data:

data buffer.serialize(buffer, format, buffer.get_start_iter(), buffer.get_end_iter())

This will return the data in textbuffer's internal format, which you can save to a file. To load this data first call textbuffer's register_deserialize_tagset() method:

format = emptyBuffer.register_deserialize_tagset()

Then you can load the data using its deserialize() method:

emptyBuffer.deserialize(emptyBuffer, format, emptyBuffer.get_end_iter(), data)

This will load the data into the textbuffer.

Edit2: if you want the user to be able to drag to move and resize you will have to instead load the pixbuf into an image widget, the image widget into a event box, the event box into a child anchor to put into the the textview you can handle x events for that widget and write code in the handlers to that will move or resize the image.

Resources:

share|improve this answer
    
Looks interesting. Thanks for the help. Any idea on how I can save this to a file? I was considering making my own mark-up and rendering it based on reading that. –  njallam Jun 22 '12 at 6:31
    
I will try this and once it works will accept this answer –  njallam Jun 22 '12 at 6:32
    
@njallam I updated my answer with info about saving and loading –  Sam Bickley Jun 22 '12 at 14:39
    
Also are the images able to be resized by the user (like dragging the corners)? The methods are there in the Pixbuf but how could I implement them. Sorry. I may be asking a bit much here. –  njallam Jun 22 '12 at 14:46
    
Actually more important, how can the user move the image. –  njallam Jun 22 '12 at 14:51
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.