/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
1
# Copyright (C) 2006 Szilveszter Farkas <szilveszter.farkas@gmail.com>
225 by Jelmer Vernooij
Add docstrings, remove unused code.
2
# Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
3
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19
"""GTK UI
20
"""
21
22
import gtk
23
import sys
24
25
import bzrlib.progress
26
from bzrlib.ui import UIFactory
27
28
29
class PromptDialog(gtk.Dialog):
30
    """ Prompt the user for a yes/no answer. """
31
    def __init__(self, prompt):
32
        gtk.Dialog.__init__(self)
33
        
34
        label = gtk.Label(prompt)
35
        self.vbox.pack_start(label, padding=10)
36
        
37
        self.vbox.show_all()
38
225 by Jelmer Vernooij
Add docstrings, remove unused code.
39
        self.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES, gtk.STOCK_NO, 
40
                         gtk.RESPONSE_NO)
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
41
42
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
43
class GtkProgressBar(gtk.ProgressBar):
44
    def __init__(self, stack):
45
        super(GtkProgressBar, self).__init__()
46
        self.set_fraction(0.0)
47
        self._stack = stack
48
49
    def finished(self):
50
        self._stack.remove(self)
51
52
    def clear(self):
53
        pass
54
55
    def tick(self):
56
        self.pulse()
57
58
    def update(self, msg=None, current=None, total=None):
59
        if msg is not None:
60
            self.set_text(msg)
450.1.1 by Aaron Bentley
Avoid division error when current or total is None
61
        if None not in (current, total):
62
            self.set_fraction(1.0 * current / total)
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
63
        while gtk.events_pending():
64
            gtk.main_iteration()
65
66
67
class GtkProgressBarStack(gtk.Window):
68
    def __init__(self):
69
        super(GtkProgressBarStack, self).__init__(type=gtk.WINDOW_TOPLEVEL)
70
        self.set_border_width(0)
71
        self.set_title("Progress")
72
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
73
        self.vbox = gtk.VBox()
74
        self.add(self.vbox)
75
        self.set_resizable(False)
76
77
    def _adapt_size(self):
78
        self.resize(250, 15 * len(self.vbox.get_children()))
79
80
    def get_nested(self):
81
        nested = GtkProgressBar(self)
82
        self.vbox.pack_start(nested)
83
        self._adapt_size()
84
        self.show_all()
85
        return nested
86
87
    def remove(self, pb):
88
        self.vbox.remove(pb)
89
90
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
91
class PasswordDialog(gtk.Dialog):
92
    """ Prompt the user for a password. """
93
    def __init__(self, prompt):
94
        gtk.Dialog.__init__(self)
95
        
96
        label = gtk.Label(prompt)
97
        self.vbox.pack_start(label, padding=10)
98
        
99
        self.entry = gtk.Entry()
100
        self.entry.set_visibility(False)
101
        self.vbox.pack_end(self.entry, padding=10)
102
        
103
        self.vbox.show_all()
104
        
105
        self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
106
    
107
    def _get_passwd(self):
108
        return self.entry.get_text()
109
110
    passwd = property(_get_passwd)
111
112
113
class GtkUIFactory(UIFactory):
0.13.12 by Jelmer Vernooij
Bunch of other small updates, add more items to
114
    """A UI factory for GTK user interfaces."""
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
115
116
    def __init__(self,
117
                 stdout=None,
118
                 stderr=None):
119
        """Create a GtkUIFactory.
120
121
        """
122
        super(GtkUIFactory, self).__init__()
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
123
        self._progress_bar_stack = None
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
124
125
    def get_boolean(self, prompt):
126
        """GtkDialog with yes/no answers"""
127
        dialog = PromptDialog(prompt)
128
        response = dialog.run()
129
        dialog.destroy()
0.13.7 by Jelmer Vernooij
Bunch of random cleanups
130
        return (response == gtk.RESPONSE_YES)
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
131
        
132
    def get_password(self, prompt='', **kwargs):
133
        """Prompt the user for a password.
134
135
        :param prompt: The prompt to present the user
136
        :param kwargs: Arguments which will be expanded into the prompt.
137
                       This lets front ends display different things if
138
                       they so choose.
139
        :return: The password string, return None if the user 
140
                 canceled the request.
141
        """
142
        dialog = PasswordDialog(prompt % kwargs)
143
        response = dialog.run()
144
        passwd = dialog.passwd
145
        dialog.destroy()
146
        if response == gtk.RESPONSE_OK:
147
            return passwd
148
        else:
149
            return None
150
151
    def nested_progress_bar(self):
152
        """Return a nested progress bar.
153
        """
154
        if self._progress_bar_stack is None:
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
155
            self._progress_bar_stack = GtkProgressBarStack()
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
156
        return self._progress_bar_stack.get_nested()
157
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
158
    def set_progress_bar_vbox(self, vbox):
159
        """Change the vbox to put progress bars in.
160
        """
161
        self._progress_bar_stack = vbox
162
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
163
    def clear_term(self):
164
        """Prepare the terminal for output.
165
166
        It has no sense when talking about GTK."""
167
        pass