-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextInfoDialog.cpp
216 lines (150 loc) · 4.22 KB
/
TextInfoDialog.cpp
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright 2013, Kacper Kasper, kacperkasper@gmail.com
* Copyright 2010, Milos Pejovic
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include <stdio.h>
#include <storage/File.h>
#include "TextInfoDialog.h"
#include "utils.h"
extern char kClose[];
const char kTextInfoTitle[] = "Text View";
EditBox::EditBox(BRect viewFrame, bool editable, bool fixedFont)
: BTextView(viewFrame, "editBox", viewFrame,
B_FOLLOW_ALL, B_FRAME_EVENTS | B_WILL_DRAW)
{
MakeEditable(editable);
if (fixedFont)
SetFontAndColor(be_fixed_font);
}
void
EditBox::FrameResized(float width, float height)
{
BTextView::FrameResized(width, height);
const float TEXT_INSET = 2.0;
BRect textRect;
textRect = Bounds();
textRect.OffsetTo(B_ORIGIN);
textRect.InsetBy(TEXT_INSET, TEXT_INSET);
SetTextRect(textRect);
}
TextInfoDialog*
TextInfoDialog::fInstance = NULL;
thread_id
TextInfoDialog::fThreadId = 0;
TextInfoDialog::TextInfoDialog(BString const & title, float width, float height,
BString const & fileName, bool editable, bool fixedFont, BString const & windowIcon)
:
Dialog(title, windowIcon, width, height)
{
if (title.Length() == 0)
SetTitle(kTextInfoTitle);
fFileName = fileName;
fEditable = editable;
fFixedFont = fixedFont;
if (width < 1.0)
width = 300;
if (height < 1.0)
height = 400;
//AddShortcut(B_ESCAPE, 0, new BMessage(MSG_CANCEL_CLICKED));
CreateViews();
ResizeTo(width, height);
InitControls();
// If filename is not given, get input from stdin
if (fFileName == NULL) {
fThreadId = spawn_thread(TextInfoDialog::_ReadInputThread, "readInput", B_NORMAL_PRIORITY, NULL);
if (fThreadId >= B_OK) {
resume_thread(fThreadId);
}
} else
fThreadId = B_OK - 1;
fInstance = this;
}
void
TextInfoDialog::CreateViews()
{
BButton* closeButton = new BButton(
BRect(0, 1, 80, 1), "closeButton", kClose, new BMessage(MSG_OK_CLICKED),
B_FOLLOW_RIGHT);
fEditBox = new EditBox(BRect(0, 0, 1, 1), fEditable, fFixedFont);
BScrollView* scrollView = new BScrollView("scrollview", fEditBox, B_FOLLOW_ALL, 0, false, true);
scrollView->SetExplicitMaxSize(
BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
AddChild(BGroupLayoutBuilder(B_VERTICAL, 5)
.Add(scrollView)
.Add(BGroupLayoutBuilder(B_HORIZONTAL, 5)
.AddGlue()
.Add(closeButton)
)
.SetInsets(5, 5, 5, 5)
);
}
void
TextInfoDialog::InitControls()
{
Lock();
if (fFileName != NULL) {
BFile file;
if (file.SetTo(fFileName, B_READ_ONLY) == B_OK) {
off_t len;
file.GetSize(&len);
char* text = new char[len];
if (file.Read(text, len) >= B_OK) {
fEditBox->SetText(text);
}
delete text;
}
}
fEditBox->MakeFocus(true);
CenterOnScreen();
Unlock();
}
int32
TextInfoDialog::_ReadInputThread(void* data)
{
const size_t len = 8192;
char buffer[len];
// Read the input in a non-blocking way.
ssize_t n;
while ((n = read_stdin_nonblocking(buffer, len - 1)) != -1) {
buffer[n] = '\0';
fInstance->Lock();
fInstance->fEditBox->Insert(buffer);
fInstance->Unlock();
}
return 0;
}
void
TextInfoDialog::_EndThread()
{
if (fThreadId >= B_OK)
send_data(fThreadId, 0, NULL, 0);
}
bool
TextInfoDialog::QuitRequested()
{
_EndThread();
return Dialog::QuitRequested();
}
void
TextInfoDialog::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_OK_CLICKED:
if (fEditable) {
fprintf(stdout, "%s\n", fEditBox->Text());
}
_EndThread();
be_app->PostMessage(MSG_OK_CLICKED);
Quit();
break;
case MSG_CANCEL_CLICKED:
_EndThread();
be_app->PostMessage(MSG_CANCEL_CLICKED);
Quit();
break;
default:
Dialog::MessageReceived(message);
break;
}
}