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