Line 58: |
Line 58: |
| == Text and Graphics for Sugar Activities == | | == Text and Graphics for Sugar Activities == |
| * [[Development Team/Almanac/Pango|Pango]] | | * [[Development Team/Almanac/Pango|Pango]] |
| + | === How do I create a text box for code editing? === |
| + | You can use gtksourceview2 |
| + | <pre> |
| + | import gtk |
| + | import gtksourceview2 |
| + | from sugar.graphics import style |
| + | |
| + | ... |
| + | |
| + | # set up the buffer |
| + | buffer = gtksourceview2.Buffer() |
| + | if hasattr(buffer, 'set_highlight'): # handle different API versions |
| + | buffer.set_highlight(True) |
| + | else: |
| + | buffer.set_highlight_syntax(True) |
| + | |
| + | # set mime type for the buffer |
| + | lang_manager = gtksourceview2.language_manager_get_default() |
| + | if hasattr(lang_manager, 'list_languages'): # again, handle different APIs |
| + | langs = lang_manager.list_languages() |
| + | else: |
| + | lang_ids = lang_manager.get_language_ids() |
| + | langs = [lang_manager.get_language(lang_id) |
| + | for lang_id in lang_ids] |
| + | for lang in langs: |
| + | for m in lang.get_mime_types(): |
| + | if m == mime_type: # <-- this is the mime type you want |
| + | buffer.set_language(lang) |
| + | |
| + | # set up the view object, use it like gtk.TextView |
| + | view = gtksourceview2.View(buffer) |
| + | view.set_size_request(300, 450) |
| + | view.set_editable(True) |
| + | view.set_cursor_visible(True) |
| + | view.set_show_line_numbers(True) |
| + | view.set_wrap_mode(gtk.WRAP_CHAR) |
| + | view.set_right_margin_position(80) |
| + | #view.set_highlight_current_line(True) #FIXME: Ugly color |
| + | view.set_auto_indent(True) |
| + | view.modify_font(pango.FontDescription("Monospace " + |
| + | str(style.FONT_SIZE))) |
| + | </pre> |
| + | |
| + | To set the text in the buffer: |
| + | <pre> |
| + | buffer.set_text(text) |
| + | </pre> |
| + | To get all the text: |
| + | <pre> |
| + | text = buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter()) |
| + | </pre> |
| + | |
| + | You can find more in the Pippy source and in jarabe.view.sourceview. |
| | | |
| == Audio & Video == | | == Audio & Video == |