/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 olive/backend/ui.py

  • Committer: Jelmer Vernooij
  • Date: 2006-05-19 16:56:46 UTC
  • mfrom: (0.1.25 gannotate)
  • Revision ID: jelmer@samba.org-20060519165646-0d867938fdbc9097
Merge in Dan Loda's gannotate plugin and put it in annotate/

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.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):
66
 
    """A UI factory for GTK user interefaces."""
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()
94
 
        if response == gtk.RESPONSE_YES:
95
 
            return True
96
 
        else:
97
 
            return False
98
 
        
99
 
    @deprecated_method(zero_eight)
100
 
    def progress_bar(self):
101
 
        """See UIFactory.nested_progress_bar()."""
102
 
        # this in turn is abstract, and creates either a tty or dots
103
 
        # bar depending on what we think of the terminal
104
 
        return bzrlib.progress.ProgressBar()
105
 
 
106
 
    def get_password(self, prompt='', **kwargs):
107
 
        """Prompt the user for a password.
108
 
 
109
 
        :param prompt: The prompt to present the user
110
 
        :param kwargs: Arguments which will be expanded into the prompt.
111
 
                       This lets front ends display different things if
112
 
                       they so choose.
113
 
        :return: The password string, return None if the user 
114
 
                 canceled the request.
115
 
        """
116
 
        dialog = PasswordDialog(prompt % kwargs)
117
 
        response = dialog.run()
118
 
        passwd = dialog.passwd
119
 
        dialog.destroy()
120
 
        if response == gtk.RESPONSE_OK:
121
 
            return passwd
122
 
        else:
123
 
            return None
124
 
 
125
 
    def nested_progress_bar(self):
126
 
        """Return a nested progress bar.
127
 
        
128
 
        The actual bar type returned depends on the progress module which
129
 
        may return a tty or dots bar depending on the terminal.
130
 
        
131
 
        FIXME: It should return a GtkProgressBar actually.
132
 
        """
133
 
        if self._progress_bar_stack is None:
134
 
            self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
135
 
                klass=self._bar_type)
136
 
        return self._progress_bar_stack.get_nested()
137
 
 
138
 
    def clear_term(self):
139
 
        """Prepare the terminal for output.
140
 
 
141
 
        It has no sense when talking about GTK."""
142
 
        pass