PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tortoisehg/hgtk/dialog.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 89 lines | 72 code | 2 blank | 15 comment | 0 complexity | 2a04d28d9ed9c07e128ca71c9b2c3b5a MD5 | raw file
 1# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <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
17import gtk
18from tortoisehg.util.i18n import _
19from tortoisehg.util import hglib
20from tortoisehg.hgtk import gtklib
21
22def entry_dialog(parent, msg, visible=True, default='', respfunc=None):
23    """ Allow a user to enter a text string (username/password)
24    :param message: the message you want to display.
25    :param visible: should reponse be visible to user
26    :param default: default response text
27    :param respfunc: callback function for when dialog exits
28    :returns if respfunc returns dialog, else return response text
29    """
30    buttons = (gtk.STOCK_OK, gtk.RESPONSE_OK,
31            gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
32    dialog = gtk.Dialog(parent=parent, flags=gtk.DIALOG_MODAL,
33            buttons=buttons)
34    dialog.set_title(_('TortoiseHg Prompt'))
35    dialog.set_has_separator(False)
36    entry = gtk.Entry()
37    entry.set_text(default or '')
38    entry.set_visibility(visible)
39    entry.set_activates_default(True)
40    lbl = gtk.Label(hglib.toutf(msg))
41    lbl.set_alignment(0, 0.5)
42    dialog.vbox.pack_start(lbl, True, True, 6)
43    dialog.vbox.pack_start(entry, False, False, 6)
44    dialog.set_default_response(gtk.RESPONSE_OK)
45    dialog.show_all()
46    if respfunc:
47        dialog.connect('response', respfunc)
48        dialog.entry = entry
49        return dialog
50    else:
51        response = dialog.run()
52        if response == gtk.RESPONSE_OK:
53            text = entry.get_text()
54        else:
55            text = None
56        dialog.destroy()
57        return text
58
59# TODO: Deprecate and remove these
60
61def _message_dialog(parent, type, primary, secondary, buttons=gtk.BUTTONS_OK,
62                    title="TortoiseHg"):
63    """ Display a given type of MessageDialog with the given message.
64
65    :param type: message dialog type
66
67    :param message: the message you want to display.
68    """
69    dialog = gtklib.MessageDialog(parent, flags=gtk.DIALOG_MODAL, type=type,
70                               buttons=buttons)
71    dialog.set_title(title)
72    dialog.set_markup('<big><b>' + primary + '</b></big>')
73    dialog.format_secondary_text(secondary)
74    dialog.set_position(gtk.WIN_POS_MOUSE)
75    response = dialog.run()
76    dialog.destroy()
77    return response
78
79def error_dialog(parent, primary, secondary):
80    """ Display an error dialog with the given message. """
81    return _message_dialog(parent, gtk.MESSAGE_ERROR, primary, secondary)
82
83def info_dialog(parent, primary, secondary):
84    """ Display an info dialog with the given message. """
85    return _message_dialog(parent, gtk.MESSAGE_INFO, primary, secondary)
86
87def warning_dialog(parent, primary, secondary):
88    """ Display a warning dialog with the given message. """
89    return _message_dialog(parent, gtk.MESSAGE_WARNING, primary, secondary)