/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: Scott James Remnant
  • Date: 2005-10-17 01:07:49 UTC
  • Revision ID: scott@netsplit.com-20051017010749-15fa95fc2cf09289
Commit the first version of bzrk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Szilveszter Farkas <szilveszter.farkas@gmail.com>
2
 
# Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
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
 
 
39
 
        self.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES, gtk.STOCK_NO, 
40
 
                         gtk.RESPONSE_NO)
41
 
 
42
 
 
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)
61
 
        self.set_fraction(1.0 * current / total)
62
 
        while gtk.events_pending():
63
 
            gtk.main_iteration()
64
 
 
65
 
 
66
 
class GtkProgressBarStack(gtk.Window):
67
 
    def __init__(self):
68
 
        super(GtkProgressBarStack, self).__init__(type=gtk.WINDOW_TOPLEVEL)
69
 
        self.set_border_width(0)
70
 
        self.set_title("Progress")
71
 
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
72
 
        self.vbox = gtk.VBox()
73
 
        self.add(self.vbox)
74
 
        self.set_resizable(False)
75
 
 
76
 
    def _adapt_size(self):
77
 
        self.resize(250, 15 * len(self.vbox.get_children()))
78
 
 
79
 
    def get_nested(self):
80
 
        nested = GtkProgressBar(self)
81
 
        self.vbox.pack_start(nested)
82
 
        self._adapt_size()
83
 
        self.show_all()
84
 
        return nested
85
 
 
86
 
    def remove(self, pb):
87
 
        self.vbox.remove(pb)
88
 
 
89
 
 
90
 
class PasswordDialog(gtk.Dialog):
91
 
    """ Prompt the user for a password. """
92
 
    def __init__(self, prompt):
93
 
        gtk.Dialog.__init__(self)
94
 
        
95
 
        label = gtk.Label(prompt)
96
 
        self.vbox.pack_start(label, padding=10)
97
 
        
98
 
        self.entry = gtk.Entry()
99
 
        self.entry.set_visibility(False)
100
 
        self.vbox.pack_end(self.entry, padding=10)
101
 
        
102
 
        self.vbox.show_all()
103
 
        
104
 
        self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
105
 
    
106
 
    def _get_passwd(self):
107
 
        return self.entry.get_text()
108
 
 
109
 
    passwd = property(_get_passwd)
110
 
 
111
 
 
112
 
class GtkUIFactory(UIFactory):
113
 
    """A UI factory for GTK user interfaces."""
114
 
 
115
 
    def __init__(self,
116
 
                 stdout=None,
117
 
                 stderr=None):
118
 
        """Create a GtkUIFactory.
119
 
 
120
 
        """
121
 
        super(GtkUIFactory, self).__init__()
122
 
        self._progress_bar_stack = None
123
 
 
124
 
    def get_boolean(self, prompt):
125
 
        """GtkDialog with yes/no answers"""
126
 
        dialog = PromptDialog(prompt)
127
 
        response = dialog.run()
128
 
        dialog.destroy()
129
 
        return (response == gtk.RESPONSE_YES)
130
 
        
131
 
    def get_password(self, prompt='', **kwargs):
132
 
        """Prompt the user for a password.
133
 
 
134
 
        :param prompt: The prompt to present the user
135
 
        :param kwargs: Arguments which will be expanded into the prompt.
136
 
                       This lets front ends display different things if
137
 
                       they so choose.
138
 
        :return: The password string, return None if the user 
139
 
                 canceled the request.
140
 
        """
141
 
        dialog = PasswordDialog(prompt % kwargs)
142
 
        response = dialog.run()
143
 
        passwd = dialog.passwd
144
 
        dialog.destroy()
145
 
        if response == gtk.RESPONSE_OK:
146
 
            return passwd
147
 
        else:
148
 
            return None
149
 
 
150
 
    def nested_progress_bar(self):
151
 
        """Return a nested progress bar.
152
 
        """
153
 
        if self._progress_bar_stack is None:
154
 
            self._progress_bar_stack = GtkProgressBarStack()
155
 
        return self._progress_bar_stack.get_nested()
156
 
 
157
 
    def set_progress_bar_vbox(self, vbox):
158
 
        """Change the vbox to put progress bars in.
159
 
        """
160
 
        self._progress_bar_stack = vbox
161
 
 
162
 
    def clear_term(self):
163
 
        """Prepare the terminal for output.
164
 
 
165
 
        It has no sense when talking about GTK."""
166
 
        pass