/
app_06_open_and_load.d
190 lines (134 loc) · 3.51 KB
/
app_06_open_and_load.d
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
/*
* pass file name(s) via the command line so the application can open it/them.
*/
import gio.Application : GioApplication = Application;
import gtk.Application : GtkApplication = Application;
import gtk.ApplicationWindow;
import gio.ApplicationCommandLine;
import gio.FileIF;
import gtk.Box;
import gtk.Widget;
import gtk.ScrolledWindow;
import gtk.TextView;
import gtk.TextBuffer;
import std.stdio;
void main(string[] args)
{
MyApplication thisApp = new MyApplication(args);
} // main()
class MyApplication : GtkApplication
{
// HANDLES_OPEN is for opening files from the command line.
ApplicationFlags flags = ApplicationFlags.HANDLES_OPEN;
string id = "com.gtkdcoding.app.app_020_06_open_and_load"; // rules
this(string[] args)
{
super(id, flags);
addOnActivate(&onActivate);
addOnStartup(&onStartup);
addOnShutdown(&onShutdown);
addOnOpen(&onOpen);
run(args);
} // this()
// Override gio.Application.activate() so we can process arguments.
// Add a TextView, open the file, load it into the TextView.
void activate(string filename)
{
writeln("activate called");
AppWindow appWindow = new AppWindow(this, filename);
} // activate()
void onActivate(GioApplication app) // non-advanced syntax
{
AppWindow appWindow = new AppWindow(this, null);
writeln("triggered onActivate...");
writeln("\tApplication ID: ", getApplicationId());
} // onActivate()
void onStartup(GioApplication app) // non-advanced syntax
{
writeln("triggered onStartup...");
writeln("\tThis is where you'd read a config file.");
} // onStartup()
void onShutdown(GioApplication app) // non-advanced syntax
{
writeln("triggered onShutdown...");
writeln("\tThis is where you'd write a config file.");
} // onShutdown()
void onOpen(FileIF[] files, string hint, GioApplication app)
{
writeln("triggered onOpen...");
foreach(file; files)
{
string name = file.getPath();
writeln("file name: ", name);
activate(name);
}
} // onOpen()
} // class MyApplication
class AppWindow : ApplicationWindow
{
int width = 400, height = 200;
string title = "Application Open Signal";
AppBox appBox;
this(MyApplication myApp, string filename)
{
super(myApp);
setSizeRequest(width, height);
if(filename !is null)
{
setTitle(title ~ "- " ~ filename);
}
else
{
setTitle(title);
}
appBox = new AppBox(filename);
add(appBox);
showAll();
} // this()
} // class AppWindow
class AppBox : Box
{
bool expand = true, fill = true;
uint globalPadding = 10, localPadding = 5;
ScrolledTextWindow scrolledTextWindow;
this(string filename)
{
super(Orientation.VERTICAL, globalPadding);
scrolledTextWindow = new ScrolledTextWindow(filename);
packStart(scrolledTextWindow, expand, fill, localPadding); // TOP justify
} // this()
} // class AppBox
class ScrolledTextWindow : ScrolledWindow
{
MyTextView myTextView;
this(string filename)
{
super();
myTextView = new MyTextView(filename);
add(myTextView);
} // this()
} // class ScrolledTextWindow
class MyTextView : TextView
{
TextBuffer textBuffer;
string content;
File file;
this(string filename)
{
super();
textBuffer = getBuffer();
// Make this conditional so a window will still open even
// if no file names are given on the command line.
if(filename)
{
file = File(filename, "r");
while (!file.eof())
{
string line = file.readln();
content ~= line;
}
textBuffer.setText(content);
file.close();
}
} // this()
} // class MyTextView