/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to ui.py

  • Committer: Jelmer Vernooij
  • Date: 2007-07-15 15:05:06 UTC
  • Revision ID: jelmer@samba.org-20070715150506-1uemecr5kt7d4kvg
Fix whitespace, add comment.

Show diffs side-by-side

added added

removed removed

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