chamesle

chamesle.org: the site of Chamesle, a small Linux distribution
git clone https://github.com/tortr-rs/chamesle
Log | Files | Refs | README

dialogs.py (2000B)


      1 
      2 import gi
      3 gi.require_version('Gtk', '3.0')
      4 from gi.repository import Gtk
      5 
      6 class Dialog(Gtk.MessageDialog):
      7     def __init__(self, style, buttons, title, text, text2=None, parent=None):
      8         Gtk.MessageDialog.__init__(self, parent, 0, style, buttons)
      9         self.set_position(Gtk.WindowPosition.CENTER)
     10         self.set_icon_name("live-installer")
     11         self.set_title(title)
     12         self.set_markup(text)
     13         self.desc = text[:30] + ' ...' if len(text) > 30 else text
     14         if text2: self.format_secondary_markup(text2)
     15         if parent:
     16             self.set_transient_for(parent)
     17             self.set_modal(True)
     18 
     19     def show(self):
     20         try:
     21             response = self.run()
     22             if response in (Gtk.ResponseType.YES, Gtk.ResponseType.APPLY, Gtk.ResponseType.OK, Gtk.ResponseType.ACCEPT):
     23                 return True
     24             else:
     25                 return False
     26         finally:
     27             self.destroy()
     28 
     29 def MessageDialog(*args):
     30     dialog = Dialog(Gtk.MessageType.INFO, Gtk.ButtonsType.NONE, *args)
     31     dialog.add_button(_("OK"), Gtk.ResponseType.OK)
     32     return dialog.show()
     33 
     34 def QuestionDialog(*args):
     35     dialog = Dialog(Gtk.MessageType.QUESTION, Gtk.ButtonsType.NONE, *args)
     36     dialog.add_button(_("No"), Gtk.ResponseType.NO)
     37     dialog.add_button(_("Yes"), Gtk.ResponseType.YES)
     38     return dialog.show()
     39 
     40 def WarningDialog(*args):
     41     dialog = Dialog(Gtk.MessageType.WARNING, Gtk.ButtonsType.NONE, *args)
     42     dialog.add_button(_("OK"), Gtk.ResponseType.OK)
     43     return dialog.show()
     44 
     45 def ErrorDialog(*args):
     46     dialog = Dialog(Gtk.MessageType.ERROR, Gtk.ButtonsType.NONE, *args)
     47     dialog.add_button(_("OK"), Gtk.ResponseType.OK)
     48     return dialog.show()
     49 
     50 def ConfirmDialog(title, text, text2, ok_label, cancel_label, style=Gtk.MessageType.WARNING):
     51     dialog = Dialog(style, Gtk.ButtonsType.NONE, title, text, text2)
     52     dialog.add_button(cancel_label, Gtk.ResponseType.CANCEL)
     53     dialog.add_button(ok_label, Gtk.ResponseType.OK)
     54     return dialog.show()