aboutsummaryrefslogtreecommitdiffstats
path: root/src/emc/usr_intf/touchy/filechooser.py
blob: c4fa79d2932b111517d035eee1169a8a6ee7a25c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Touchy is Copyright (c) 2009  Chris Radek <chris@timeguy.com>
#
# Touchy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Touchy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

import os

#we need this _soley_ to define colours.
from gi.repository import Gdk

class filechooser:
    def __init__(self, gtk, emc, labels, eventboxes, listing):
        self.labels = labels
        self.eventboxes = eventboxes
        self.numlabels = len(labels)
        self.listing = listing
        self.gtk = gtk
        self.emc = emc
        self.emccommand = emc.command()
        self.fileoffset = 0
        self.dir = os.path.join(os.getenv('HOME'), 'linuxcnc', 'nc_files')
        self.reload(0)

    def populate(self):
        files = self.files[self.fileoffset:]
        for i in range(self.numlabels):
            l = self.labels[i]
            e = self.eventboxes[i]
            if i < len(files):
                l.set_text(files[i])
            else:
                l.set_text('')
            if self.selected == self.fileoffset + i:
                e.modify_bg(self.gtk.StateFlags.NORMAL, Gdk.color_parse('#fff'))
            else:
                e.modify_bg(self.gtk.StateFlags.NORMAL, Gdk.color_parse('#ccc'))

    def select(self, eventbox, event):
        n = int(self.gtk.Buildable.get_name(eventbox)[20:])
        fn = self.labels[n].get_text()
        if len(fn) == 0: return(fn)
        self.selected = self.fileoffset + n
        self.emccommand.mode(self.emc.MODE_MDI)
        fn = os.path.join(self.dir, fn)
        self.emccommand.program_open(fn)
        self.listing.readfile(fn)
        self.populate()
        return(fn)

    def select_and_show(self,fn):
        self.reload(0)
        numfiles = len(self.files)
        fn = os.path.basename(fn)
        self.fileoffset = 0
        found = False
        while True:
            for k in range(self.numlabels):
                n = k + self.fileoffset
                if n >= numfiles: return # notfound
                if self.files[n] == fn:
                    found = True
                    break # from for
            if found: break # from while
            self.fileoffset += self.numlabels

        self.selected = n
        fn = os.path.join(self.dir, fn)
        self.listing.readfile(fn)
        self.populate()

    def up(self, b):
        self.fileoffset -= self.numlabels
        if self.fileoffset < 0:
            self.fileoffset = 0
        self.populate()

    def down(self, b):
        self.fileoffset += self.numlabels
        self.populate()

    def reload(self, b):
        self.files = os.listdir(self.dir)
        self.files = [i for i in self.files if i.endswith('.ngc') and
                      os.path.isfile(os.path.join(self.dir, i))]
        self.files.sort()
        self.selected = -1
        self.populate()
bues.ch cgit interface