summaryrefslogtreecommitdiff
path: root/sigalatvision/lib/TDirListBox.cc
blob: f183d3c725c484bbcf9eed66e3da5ad203d3c715 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * TDirListBox.cc
 *
 * Turbo Vision - Version 2.0
 *
 * Copyright (c) 1994 by Borland International
 * All Rights Reserved.
 *
 * Modified by Sergio Sigala <sergio@sigala.it>
 */

#define Uses_TDirListBox
#define Uses_TEvent
#define Uses_TDirCollection
#define Uses_TChDirDialog
#define Uses_TDirEntry
#define Uses_TButton
#include <tvision/tv.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

TDirListBox::TDirListBox( const TRect& bounds, TScrollBar *aScrollBar ) :
    TListBox( bounds, 1, aScrollBar ),
    cur( 0 )
{
    *dir = EOS;
}

TDirListBox::~TDirListBox()
{
   if ( list() )
      destroy( list() );
}

void TDirListBox::getText( char *text, short item, short maxChars )
{
    strncpy( text, list()->at(item)->text(), maxChars );
    text[maxChars] = '\0';
}

void TDirListBox::selectItem( short item )
{
    message( owner, evCommand, cmChangeDir, list()->at(item) );
}

/*
void TDirListBox::handleEvent( TEvent& event )
{
    if( event.what == evMouseDown && (event.mouse.eventFlags & meDoubleClick) )
        {
        event.what = evCommand;
        event.message.command = cmChangeDir;
        putEvent( event );
        clearEvent( event );
        }
    else
       TListBox::handleEvent( event );
}
*/

Boolean TDirListBox::isSelected( short item )
{
    return Boolean( item == cur );
}

void TDirListBox::showDrives( TDirCollection* )
{
	/* SS: do nothing */
}

void TDirListBox::showDirs( TDirCollection *dirs )
{
	/* SS: changed */

	char buf[PATH_MAX * 2];
	char *curDir = dir;
	char *end;
	char *name = buf + sizeof(buf) / 2;
	const int indentSize = 2;
	int indent = 0, len;

	/* extract directories from path string */

	memset(buf, ' ', sizeof(buf));
	strcpy(name, pathDir);
	len = strlen(pathDir);
	while((end = strchr(curDir, '/' )) != NULL)
	{
		/* special case: root directory */

		if (end == dir) dirs->insert(new TDirEntry("/", ""));
		else
		{
			memcpy(name + len, curDir, end - curDir);
			name[len + end - curDir] = EOS;
			*end = EOS;
			dirs->insert(new TDirEntry(name - indent, dir));
			*end = '/';
			indent += indentSize;
		}
		curDir = end + 1;
	}
	cur = dirs->getCount() - 1;

	/* read subdirectories in the current directory */

	Boolean isFirst = True;
	DIR *dp;
	char path[PATH_MAX];
	dirent *de;
	struct stat s;

	sprintf(path, "%s.", dir);
	if ((dp = opendir(path)) != NULL)
	{
		while ((de = readdir(dp)) != NULL)
		{
			/* we don't want these directories */

			if (strcmp(de->d_name, ".") == 0 ||
				strcmp(de->d_name, "..") == 0) continue;

			/* is it a directory ? */

			sprintf(path, "%s%s", dir, de->d_name);
			if (stat(path, &s) == 0 && S_ISDIR(s.st_mode))
			{
				if (isFirst)
				{
					isFirst = False;
					strcpy(name, firstDir);
					len = strlen(firstDir);
				}
				else
				{
					strcpy(name, middleDir);
					len = strlen(middleDir);
				}
				strcpy(name + len, de->d_name);
				dirs->insert(new TDirEntry(name - indent,
					path));
			}
		}
		closedir(dp);
	}

	/* old code */

    char *p = dirs->at(dirs->getCount()-1)->text();
    char *i = strchr( p, graphics[0] );
    if( i == 0 )
        {
        i = strchr( p, graphics[1] );
        if( i != 0 )
            *i = graphics[0];
        }
    else
        {
        *(i+1) = graphics[2];
        *(i+2) = graphics[2];
        }
}

void TDirListBox::newDirectory( const char *str )
{
	/* SS: changed */

	strcpy( dir, str );
	TDirCollection *dirs = new TDirCollection( 5, 5 );
	showDirs( dirs );
	newList( dirs );
	focusItem( cur );
}

void TDirListBox::setState( ushort nState, Boolean enable )
{
    TListBox::setState( nState, enable );
    if( (nState & sfFocused) != 0 )
#ifndef __UNPATCHED
        message(owner, evCommand, cmDirSelection, (void *)enable);  //!!
#else
        ((TChDirDialog *)owner)->chDirButton->makeDefault( enable );
#endif
}

#if !defined(NO_STREAMABLE)

TStreamable *TDirListBox::build()
{
    return new TDirListBox( streamableInit );
}

#endif