/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>
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.symbol_versioning import (deprecated_method, 
26
        zero_eight)
27
from bzrlib.ui import UIFactory
28
29
30
class PromptDialog(gtk.Dialog):
31
    """ Prompt the user for a yes/no answer. """
32
    def __init__(self, prompt):
33
        gtk.Dialog.__init__(self)
34
        
35
        label = gtk.Label(prompt)
36
        self.vbox.pack_start(label, padding=10)
37
        
38
        self.vbox.show_all()
39
40
        self.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES, gtk.STOCK_NO, gtk.RESPONSE_NO)
41
42
43
class PasswordDialog(gtk.Dialog):
44
    """ Prompt the user for a password. """
45
    def __init__(self, prompt):
46
        gtk.Dialog.__init__(self)
47
        
48
        label = gtk.Label(prompt)
49
        self.vbox.pack_start(label, padding=10)
50
        
51
        self.entry = gtk.Entry()
52
        self.entry.set_visibility(False)
53
        self.vbox.pack_end(self.entry, padding=10)
54
        
55
        self.vbox.show_all()
56
        
57
        self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
58
    
59
    def _get_passwd(self):
60
        return self.entry.get_text()
61
62
    passwd = property(_get_passwd)
63
64
65
class GtkUIFactory(UIFactory):
0.13.12 by Jelmer Vernooij
Bunch of other small updates, add more items to
66
    """A UI factory for GTK user interfaces."""
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
67
68
    def __init__(self,
69
                 bar_type=None,
70
                 stdout=None,
71
                 stderr=None):
72
        """Create a GtkUIFactory.
73
74
        :param bar_type: The type of progress bar to create. It defaults to 
75
                         letting the bzrlib.progress.ProgressBar factory auto
76
                         select.
77
        """
78
        super(GtkUIFactory, self).__init__()
79
        self._bar_type = bar_type
80
        if stdout is None:
81
            self.stdout = sys.stdout
82
        else:
83
            self.stdout = stdout
84
        if stderr is None:
85
            self.stderr = sys.stderr
86
        else:
87
            self.stderr = stderr
88
89
    def get_boolean(self, prompt):
90
        """GtkDialog with yes/no answers"""
91
        dialog = PromptDialog(prompt)
92
        response = dialog.run()
93
        dialog.destroy()
0.13.7 by Jelmer Vernooij
Bunch of random cleanups
94
        return (response == gtk.RESPONSE_YES)
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
95
        
96
    @deprecated_method(zero_eight)
97
    def progress_bar(self):
98
        """See UIFactory.nested_progress_bar()."""
99
        # this in turn is abstract, and creates either a tty or dots
100
        # bar depending on what we think of the terminal
101
        return bzrlib.progress.ProgressBar()
102
103
    def get_password(self, prompt='', **kwargs):
104
        """Prompt the user for a password.
105
106
        :param prompt: The prompt to present the user
107
        :param kwargs: Arguments which will be expanded into the prompt.
108
                       This lets front ends display different things if
109
                       they so choose.
110
        :return: The password string, return None if the user 
111
                 canceled the request.
112
        """
113
        dialog = PasswordDialog(prompt % kwargs)
114
        response = dialog.run()
115
        passwd = dialog.passwd
116
        dialog.destroy()
117
        if response == gtk.RESPONSE_OK:
118
            return passwd
119
        else:
120
            return None
121
122
    def nested_progress_bar(self):
123
        """Return a nested progress bar.
124
        
125
        The actual bar type returned depends on the progress module which
126
        may return a tty or dots bar depending on the terminal.
127
        
128
        FIXME: It should return a GtkProgressBar actually.
129
        """
130
        if self._progress_bar_stack is None:
131
            self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
132
                klass=self._bar_type)
133
        return self._progress_bar_stack.get_nested()
134
135
    def clear_term(self):
136
        """Prepare the terminal for output.
137
138
        It has no sense when talking about GTK."""
139
        pass