/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: 2006-09-05 02:17:19 UTC
  • Revision ID: jelmer@samba.org-20060905021719-90e2eda34c41604b
Handle empty files more gracefully. Fixes #58951.

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
 
        self.current = None
49
 
        self.total = None
50
 
 
51
 
    def finished(self):
52
 
        self._stack.remove(self)
53
 
 
54
 
    def clear(self):
55
 
        pass
56
 
 
57
 
    def tick(self):
58
 
        self.pulse()
59
 
 
60
 
    def update(self, msg=None, current=None, total=None):
61
 
        if current:
62
 
            self.current = current
63
 
        if total:
64
 
            self.total = total
65
 
        if msg is not None:
66
 
            self.set_text(msg)
67
 
        if None not in (self.current, self.total):
68
 
            self.set_fraction(1.0 * self.current / self.total)
69
 
        while gtk.events_pending():
70
 
            gtk.main_iteration()
71
 
 
72
 
 
73
 
class GtkWindowProgressBarStack(gtk.Window):
74
 
    def __init__(self):
75
 
        super(GtkWindowProgressBarStack, self).__init__(type=gtk.WINDOW_TOPLEVEL)
76
 
        self.set_border_width(0)
77
 
        self.set_title("Progress")
78
 
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
79
 
        self.vbox = gtk.VBox()
80
 
        self.add(self.vbox)
81
 
        self.set_resizable(False)
82
 
 
83
 
    def _adapt_size(self):
84
 
        self.resize(250, 15 * len(self.vbox.get_children()))
85
 
 
86
 
    def get_nested(self):
87
 
        nested = GtkProgressBar(self)
88
 
        self.vbox.pack_start(nested)
89
 
        self._adapt_size()
90
 
        self.show_all()
91
 
        return nested
92
 
 
93
 
    def remove(self, pb):
94
 
        self.vbox.remove(pb)
95
 
        if len(self.vbox.get_children()) == 0: # If there is nothing to show, don't leave a ghost window here
96
 
             self.destroy()
97
 
 
98
 
 
99
 
class PasswordDialog(gtk.Dialog):
100
 
    """ Prompt the user for a password. """
101
 
    def __init__(self, prompt):
102
 
        gtk.Dialog.__init__(self)
103
 
        
104
 
        label = gtk.Label(prompt)
105
 
        self.vbox.pack_start(label, padding=10)
106
 
        
107
 
        self.entry = gtk.Entry()
108
 
        self.entry.set_visibility(False)
109
 
        self.vbox.pack_end(self.entry, padding=10)
110
 
        
111
 
        self.vbox.show_all()
112
 
        
113
 
        self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
114
 
    
115
 
    def _get_passwd(self):
116
 
        return self.entry.get_text()
117
 
 
118
 
    passwd = property(_get_passwd)
119
 
 
120
 
 
121
 
class GtkUIFactory(UIFactory):
122
 
    """A UI factory for GTK user interfaces."""
123
 
 
124
 
    def __init__(self,
125
 
                 stdout=None,
126
 
                 stderr=None):
127
 
        """Create a GtkUIFactory.
128
 
 
129
 
        """
130
 
        super(GtkUIFactory, self).__init__()
131
 
        self._progress_bar_stack = None
132
 
 
133
 
    def get_boolean(self, prompt):
134
 
        """GtkDialog with yes/no answers"""
135
 
        dialog = PromptDialog(prompt)
136
 
        response = dialog.run()
137
 
        dialog.destroy()
138
 
        return (response == gtk.RESPONSE_YES)
139
 
        
140
 
    def get_password(self, prompt='', **kwargs):
141
 
        """Prompt the user for a password.
142
 
 
143
 
        :param prompt: The prompt to present the user
144
 
        :param kwargs: Arguments which will be expanded into the prompt.
145
 
                       This lets front ends display different things if
146
 
                       they so choose.
147
 
        :return: The password string, return None if the user 
148
 
                 canceled the request.
149
 
        """
150
 
        dialog = PasswordDialog(prompt % kwargs)
151
 
        response = dialog.run()
152
 
        passwd = dialog.passwd
153
 
        dialog.destroy()
154
 
        if response == gtk.RESPONSE_OK:
155
 
            return passwd
156
 
        else:
157
 
            return None
158
 
 
159
 
    def nested_progress_bar(self):
160
 
        """Return a nested progress bar.
161
 
        """
162
 
        if self._progress_bar_stack is None:
163
 
            self._progress_bar_stack = GtkWindowProgressBarStack()
164
 
        return self._progress_bar_stack.get_nested()
165
 
 
166
 
    def set_progress_bar_vbox(self, vbox):
167
 
        """Change the vbox to put progress bars in.
168
 
        """
169
 
        self._progress_bar_stack = vbox
170
 
 
171
 
    def clear_term(self):
172
 
        """Prepare the terminal for output.
173
 
 
174
 
        It has no sense when talking about GTK."""
175
 
        pass