home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import pygtk
- import gtk
- import pango
- import subprocess
- import os
-
- class ReleaseNotesViewer(gtk.TextView):
-
- def __init__(self, notes):
- '''Init the ReleaseNotesViewer as an Inheritance of the gtk.TextView.
- Load the notes into the buffer and make links clickable'''
- gtk.TextView.__init__(self)
- self.hovering = False
- self.first = True
- self.set_property('editable', False)
- self.set_cursor_visible(False)
- self.buffer = gtk.TextBuffer()
- self.set_buffer(self.buffer)
- self.buffer.set_text(notes)
- self.connect('event-after', self.event_after)
- self.connect('motion-notify-event', self.motion_notify_event)
- self.connect('visibility-notify-event', self.visibility_notify_event)
- self.search_links()
-
-
- def tag_link(self, start, end, url):
- '''Apply the tag that marks links to the specified buffer selection'''
- tag = self.buffer.create_tag(None, foreground = 'blue', underline = pango.UNDERLINE_SINGLE)
- tag.set_data('url', url)
- self.buffer.apply_tag(tag, start, end)
-
-
- def search_links(self):
- '''Search for http URLs in the buffer and call the tag_link method
- for each one to tag them as links'''
- iter = self.buffer.get_iter_at_offset(0)
- while None:
- ret = iter.forward_search('http://', gtk.TEXT_SEARCH_VISIBLE_ONLY, None)
- if not ret:
- break
-
- (match_start, match_end) = ret
- match_tmp = match_end.copy()
- while match_tmp.forward_char():
- text = match_end.get_text(match_tmp)
- if text in (' ', ')', ']', '\n', '\t'):
- break
-
- break
- match_end = match_tmp.copy()
- continue
- url = match_start.get_text(match_end)
- self.tag_link(match_start, match_end, url)
- iter = match_end
- continue
- return None
-
-
- def event_after(self, text_view, event):
- '''callback for mouse click events'''
- if event.type != gtk.gdk.BUTTON_RELEASE:
- return False
- if event.button != 1:
- return False
-
- try:
- (start, end) = self.buffer.get_selection_bounds()
- except ValueError:
- event.button != 1
- event.button != 1
- event.type != gtk.gdk.BUTTON_RELEASE
- except:
- event.button != 1
-
- if start.get_offset() != end.get_offset():
- return False
- (x, y) = self.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
- iter = self.get_iter_at_location(x, y)
- tags = iter.get_tags()
- for tag in tags:
- url = tag.get_data('url')
- if url != '':
- self.open_url(url)
- break
- continue
- start.get_offset() != end.get_offset()
-
-
-
- def open_url(self, url):
- '''Open the specified URL in a browser'''
- if os.path.exists('/usr/bin/exo-open'):
- command = [
- 'exo-open',
- url]
- elif os.path.exists('usr/bin/gnome-open'):
- command = [
- 'gnome-open',
- url]
- else:
- command = [
- 'x-www-browser',
- url]
- if os.getuid() == 0 and os.environ.has_key('SUDO_USER'):
- command = [
- 'sudo',
- '-u',
- os.environ['SUDO_USER']] + command
-
- subprocess.Popen(command)
-
-
- def motion_notify_event(self, text_view, event):
- '''callback for the mouse movement event, that calls the
- check_hovering method with the mouse postition coordiantes'''
- (x, y) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
- self.check_hovering(x, y)
- self.window.get_pointer()
- return False
-
-
- def visibility_notify_event(self, text_view, event):
- '''callback if the widgets gets visible (e.g. moves to the foreground)
- that calls the check_hovering method with the mouse position
- coordinates'''
- (wx, wy, mod) = text_view.window.get_pointer()
- (bx, by) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, wx, wy)
- self.check_hovering(bx, by)
- return False
-
-
- def check_hovering(self, x, y):
- '''Check if the mouse is above a tagged link and if yes show
- a hand cursor'''
- _hovering = False
- iter = self.get_iter_at_location(x, y)
- tags = iter.get_tags()
- for tag in tags:
- url = tag.get_data('url')
- if url != '':
- _hovering = True
- break
- continue
-
- if _hovering != self.hovering or self.first == True:
- self.first = False
- self.hovering = _hovering
- if self.hovering:
- self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND2))
- else:
- self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.LEFT_PTR))
-
-
-
- if __name__ == '__main__':
- win = gtk.Window()
- rv = ReleaseNotesViewer(open('../DistUpgrade/ReleaseAnnouncement').read())
- win.add(rv)
- win.show_all()
- gtk.main()
-
-