Statistiques
| Révision :

root / ase / gui / widgets.py @ 15

Historique | Voir | Annoter | Télécharger (4,23 ko)

1
import gtk
2

    
3
from ase.gui.languages import translate as _
4

    
5

    
6
class Number(gtk.SpinButton):
7
    def __init__(self, value=0,
8
                 lower=0, upper=10000,
9
                 step_incr=1, page_incr=10,
10
                 climb_rate=0.5, digits=0):
11
        self.adj = gtk.Adjustment(value, lower, upper, step_incr, page_incr, 0)
12
        gtk.SpinButton.__init__(self, self.adj, climb_rate, digits)
13

    
14
    def connect(self, *args):
15
        return self.adj.connect(*args)
16

    
17

    
18
class Menu:
19
    def __init__(self, menubar, name, items):
20
        self.items = {}
21
        menu = gtk.Menu()
22
        for data in items:
23
            text = data[0]
24
            callback = data[1]
25
            args = data[2:]
26
            menuitem = gtk.MenuItem(_(text))
27
            menu.append(menuitem)
28
            menuitem.connect('activate', callback, *args)
29
            menuitem.show()
30
            self.items[text] = menuitem
31
        menuitem = gtk.MenuItem(_(name))
32
        menubar.append(menuitem)
33
        menuitem.set_submenu(menu)
34
        menuitem.show()
35

    
36

    
37
class Help(gtk.Window):
38
    def __init__(self, text):
39
        gtk.Window.__init__(self)
40
        vbox = gtk.VBox()
41
        self.add(vbox)
42
        label = pack(vbox, gtk.Label())
43
        label.set_line_wrap(True)
44
        text = _(text).replace('<c>', '<span foreground="blue">')
45
        text = text.replace('</c>', '</span>')
46
        label.set_markup(text)
47
        close = pack(vbox, gtk.Button(_('Close')))
48
        close.connect('clicked', lambda widget: self.destroy())
49
        self.show_all()
50

    
51
def help(text):
52
    button = gtk.Button(_('Help'))
53
    button.connect('clicked', lambda widget, text=text: Help(text))
54
    return button
55

    
56

    
57
class Window(gtk.Window):
58
    def __init__(self, gui):
59
        self.gui = gui
60
        gtk.Window.__init__(self)
61
        self.set_title(_('Constraints'))
62
        vbox = gtk.VBox()
63
        b = pack(vbox, [gtk.Button(_('Constrain')),
64
                        gtk.Label(_(' selected atoms'))])[0]
65
        b.connect('clicked', self.selected)
66
        b = pack(vbox, [gtk.Button(_('Constrain')),
67
                        gtk.Label(_(' immobile atoms:'))])[0]
68
        b.connect('clicked', self.immobile)
69
        b = pack(vbox, gtk.Button('Clear constraint'))
70
        b.connect('clicked', self.clear)
71
        close = pack(vbox, gtk.Button(_('Close')))
72
        close.connect('clicked', lambda widget: self.destroy())
73
        self.add(vbox)
74
        vbox.show()
75
        self.show()
76

    
77
def pack(vbox, widgets, end=False, bottom=False):
78
    if not isinstance(widgets, list):
79
        widgets.show()
80
        if bottom:
81
            vbox.pack_end(widgets, 0, 0)
82
        else:
83
            vbox.pack_start(widgets, 0, 0)
84
        return widgets
85
    hbox = gtk.HBox(0, 0)
86
    hbox.show()
87
    if bottom:
88
        vbox.pack_end(hbox, 0, 0)
89
    else:
90
        vbox.pack_start(hbox, 0, 0)
91
    for widget in widgets:
92
        if type(widget) is gtk.Entry:
93
            widget.set_size_request(widget.get_max_length() * 9, 24)
94
        widget.show()
95
        if end and widget is widgets[-1]:
96
            hbox.pack_end(widget, 0, 0)
97
        else:
98
            hbox.pack_start(widget, 0, 0)
99
    return widgets
100

    
101
class cancel_apply_ok(gtk.HButtonBox):
102
    "Widget with Cancel, Apply and OK buttons.  The arguments are callbacks."
103
    def __init__(self, cancel, apply, ok):
104
        gtk.HButtonBox.__init__(self)
105
        cancel_but = gtk.Button(stock=gtk.STOCK_CANCEL)
106
        cancel_but.connect('clicked', cancel)
107
        apply_but = gtk.Button(stock=gtk.STOCK_APPLY)
108
        apply_but.connect('clicked', apply)
109
        ok_but = gtk.Button(stock=gtk.STOCK_OK)
110
        ok_but.connect('clicked', ok)
111
        for w in (cancel_but, apply_but, ok_but):
112
            self.pack_start(w, 0, 0)
113
            w.show()
114
        #self.show_all()
115
        
116
def oops(message, message2=None):
117
    dialog = gtk.MessageDialog(flags=gtk.DIALOG_MODAL,
118
                               type=gtk.MESSAGE_WARNING,
119
                               buttons=gtk.BUTTONS_CLOSE,
120
                               message_format=message)
121
    try:
122
        dialog.format_secondary_text(message2)
123
    except AttributeError:
124
        print >>sys.stderr, message
125
        print >>sys.stderr, message2
126
    dialog.connect('response', lambda x, y, dialog=dialog: dialog.destroy())
127
    dialog.show()
128

    
129
class AseGuiCancelException(Exception):
130
    pass
131