File: hyperlink_demo.py

package info (click to toggle)
kiwi 1.9.22-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 11,908 kB
  • ctags: 5,549
  • sloc: python: 15,779; ansic: 193; xml: 77; makefile: 57; sh: 17
file content (165 lines) | stat: -rw-r--r-- 5,936 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2005 Async Open Source
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Author(s): Lorenzo Gil Sanchez <lgs@sicem.biz>
#            Johan Dahlin <jdahlin@async.com.br>
#
# This module: (C) Ali Afshar <aafshar@gmail.com>
# (contact if you require release under a different license)


""" HyperLink demo application. """


import time

import gtk

from kiwi.ui import hyperlink

class App(object):

    def __init__(self):
        self._window = gtk.Window()
        self._window.connect('destroy', self._on_window__destroy)
        self._window.set_title('Hyperlink Widget Demo')
        vb = gtk.VBox(spacing=3)
        vb.set_border_width(12)
        self._window.add(vb)
        self._build_basic_hyperlink(vb)
        vb.pack_start(gtk.HSeparator(), expand=False, padding=6)
        self._build_formatted_hyperlink(vb)
        vb.pack_start(gtk.HSeparator(), expand=False, padding=6)
        self._build_menu_hyperlink(vb)
        vb.pack_start(gtk.HSeparator(), expand=False, padding=6)
        sw = gtk.ScrolledWindow()
        vb.pack_start(sw)
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
        self._logger = gtk.TextView()
        sw.add(self._logger)
        self._logger.set_editable(False)
        self._window.show_all()

    def _build_basic_hyperlink(self, vb):
        self._hl1 = hyperlink.HyperLink('basic hyperlink')
        vb.pack_start(self._hl1, expand=False)
        self._hl1.connect('clicked', self._on_hl1__clicked)
        self._hl1.connect('right-clicked', self._on_hl1__right_clicked)
        d1 = gtk.Label(
            'I am a basic hyperlink. The signals I emit are "clicked" and'
            ' "right-clicked". Try me out')
        d1.set_line_wrap(True)
        vb.pack_start(d1, expand=False)

    def _build_formatted_hyperlink(self, vb):
        self._hl2 = hyperlink.HyperLink('hyperlink with formatting')
        vb.pack_start(self._hl2, expand=False)
        self._hl2.normal_color = '#c000c0'
        self._hl2.hover_color = '#00c0c0'
        self._hl2.active_color = '#000000'
        self._hl2.hover_bold = True
        self._hl2.active_bold = True
        self._hl2.connect('clicked', self._on_hl2__clicked)
        self._hl2.connect('right-clicked', self._on_hl2__right_clicked)
        d1 = gtk.Label(
            'I am a formatted hyperlink. I can be modified by setting'
            ' my properties like normal-color. Click me to change me!')
        d1.set_line_wrap(True)
        vb.pack_start(d1, expand=False)

    def _build_menu_hyperlink(self, vb):
        self._hl3 = hyperlink.HyperLink('menu hyperlink')
        vb.pack_start(self._hl3, expand=False)
        self._hl3.connect('clicked', self._on_hl3__clicked)
        self._hl3.connect('right-clicked', self._on_hl3__right_clicked)
        self._hl3.hover_underline = False
        self._hl3.active_underline = False
        menu = gtk.Menu()
        m1 = gtk.MenuItem()
        m1.add(gtk.Label('toggle bold'))
        menu.add(m1)
        m1.connect('activate', self._on_m1_activated)
        m2 = gtk.MenuItem()
        m2.add(gtk.Label('toggle underline'))
        menu.add(m2)
        m2.connect('activate', self._on_m2_activated)
        menu.show_all()
        self._hl3.set_menu(menu)
        d1 = gtk.Label(
            'I am a hyperlink with a menu. Right click me to pop it up.')
        d1.set_line_wrap(True)
        vb.pack_start(d1, expand=False)

    def start(self):
        gtk.main()

    def stop(self):
        gtk.main_quit()

    def _on_window__destroy(self, window):
        self.stop()

    def _on_hl1__clicked(self, hyperlink):
        self.log('basic hyperlink clicked')

    def _on_hl1__right_clicked(self, hyperlink):
        self.log('basic hyperlink right-clicked')

    def _on_hl2__clicked(self, hyperlink):
        self.log('formatted hyperlink clicked')
        hyperlink.normal_color = '#c00000'
        hyperlink.normal_bold = True
        if not hyperlink.text.startswith('au'):
            hyperlink.text = 'automatically! by setting self.text = foo'
        else:
            hyperlink.text = 'and changed back again'

    def _on_hl2__right_clicked(self, hyperlink):
        self.log('formatted hyperlink right-clicked')

    def _on_hl3__clicked(self, hyperlink):
        self.log('menu hyperlink clicked')

    def _on_hl3__right_clicked(self, hyperlink):
        self.log('menu hyperlink right-clicked')

    def _on_m1_activated(self, menuitem):
        self.log('menuitem 1 activated')
        self._hl3.normal_bold = not self._hl3.normal_bold
        self._hl3.hover_bold = not self._hl3.hover_bold
        self._hl3.active_bold = not self._hl3.active_bold

    def _on_m2_activated(self, menuitem):
        self.log('menuitem 2 activated')
        self._hl3.normal_underline = not self._hl3.normal_underline
        self._hl3.hover_underline = not self._hl3.hover_underline
        self._hl3.active_underline = not self._hl3.active_underline

    def log(self, msg):
        buf = self._logger.get_buffer()
        timestr = time.strftime('%H:%M:%S')
        buf.insert(buf.get_start_iter(), '%s...%s\n' % (timestr, msg))

def main():
    a = App()
    a.start()

if __name__ == '__main__':
    main()