xref: /haiku/src/apps/packageinstaller/PackageTextViewer.cpp (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
1 /*
2  * Copyright (c) 2007, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Author:
6  *		Łukasz 'Sil2100' Zemczak <sil2100@vexillium.org>
7  */
8 
9 
10 #include "PackageTextViewer.h"
11 
12 #include <Button.h>
13 #include <ScrollView.h>
14 
15 #include <GroupLayout.h>
16 #include <GroupLayoutBuilder.h>
17 
18 
19 enum {
20 	P_MSG_ACCEPT = 'pmac',
21 	P_MSG_DECLINE
22 };
23 
24 // Reserved
25 #define T(x) x
26 
27 
28 PackageTextViewer::PackageTextViewer(const char *text, bool disclaimer)
29 	:	BWindow(BRect(125, 125, 675, 475), T("Disclaimer"), B_MODAL_WINDOW,
30 			B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_NOT_CLOSABLE),
31 		fValue(0)
32 {
33 	_InitView(text, disclaimer);
34 }
35 
36 
37 PackageTextViewer::~PackageTextViewer()
38 {
39 }
40 
41 
42 int32
43 PackageTextViewer::Go()
44 {
45 	// Since this class can be thought of as a modified BAlert window, no use
46 	// to reinvent a well fledged wheel. This concept has been borrowed from
47 	// the current BAlert implementation
48 	fSemaphore = create_sem(0, "TextViewer");
49 	if (fSemaphore < B_OK) {
50 		Quit();
51 		return B_ERROR;
52 	}
53 
54 	BWindow *parent =
55 		dynamic_cast<BWindow *>(BLooper::LooperForThread(find_thread(NULL)));
56 	Show();
57 
58 	if (parent) {
59 		status_t ret;
60 		for (;;) {
61 			do {
62 				ret = acquire_sem_etc(fSemaphore, 1, B_RELATIVE_TIMEOUT, 50000);
63 			} while (ret == B_INTERRUPTED);
64 
65 			if (ret == B_BAD_SEM_ID)
66 				break;
67 			parent->UpdateIfNeeded();
68 		}
69 	}
70 	else {
71 		// Since there are no spinlocks, wait until the semaphore is free
72 		while (acquire_sem(fSemaphore) == B_INTERRUPTED) {
73 		}
74 	}
75 
76 	int32 value = fValue;
77 	if (Lock())
78 		Quit();
79 
80 	return value;
81 }
82 
83 
84 void
85 PackageTextViewer::MessageReceived(BMessage *msg)
86 {
87 	if (msg->what == P_MSG_ACCEPT) {
88 		if (fSemaphore >= B_OK) {
89 			fValue = 1;
90 			delete_sem(fSemaphore);
91 			fSemaphore = -1;
92 		}
93 	}
94 	else if (msg->what == P_MSG_DECLINE) {
95 		if (fSemaphore >= B_OK) {
96 			fValue = 0;
97 			delete_sem(fSemaphore);
98 			fSemaphore = -1;
99 		}
100 	}
101 	else
102 		BWindow::MessageReceived(msg);
103 }
104 
105 
106 // #pragma mark -
107 
108 
109 void
110 PackageTextViewer::_InitView(const char *text, bool disclaimer)
111 {
112 	fBackground = new BView(Bounds(), "background_view", 0, 0);
113 	fBackground->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
114 
115 	BRect bounds;
116 	BRect rect = Bounds();
117 	if (disclaimer) {
118 		BButton *button = new BButton(BRect(0, 0, 1, 1), "accept", T("Accept"),
119 				new BMessage(P_MSG_ACCEPT));
120 		button->ResizeToPreferred();
121 
122 		bounds = button->Bounds();
123 		rect.top = rect.bottom - bounds.bottom - 5.0f;
124 		rect.left = rect.right - bounds.right - 5.0f;
125 		rect.bottom = bounds.bottom;
126 		rect.right = bounds.right;
127 		button->MoveTo(rect.LeftTop());
128 		button->MakeDefault(true);
129 		fBackground->AddChild(button);
130 
131 		button = new BButton(BRect(0, 0, 1, 1), "decline", T("Decline"),
132 				new BMessage(P_MSG_DECLINE));
133 		button->ResizeToPreferred();
134 
135 		bounds = button->Bounds();
136 		rect.left -= bounds.right + 7.0f;
137 		button->MoveTo(rect.LeftTop());
138 		fBackground->AddChild(button);
139 	}
140 	else {
141 		BButton *button = new BButton(BRect(0, 0, 1, 1), "accept", T("Continue"),
142 				new BMessage(P_MSG_ACCEPT));
143 		button->ResizeToPreferred();
144 
145 		bounds = button->Bounds();
146 		rect.top = rect.bottom - bounds.bottom - 5.0f;
147 		rect.left = rect.right - bounds.right - 5.0f;
148 		rect.bottom = bounds.bottom;
149 		rect.right = bounds.right;
150 		button->MoveTo(rect.LeftTop());
151 		button->MakeDefault(true);
152 		fBackground->AddChild(button);
153 	}
154 
155 	bounds = Bounds().InsetBySelf(5.0f, 5.0f);
156 	bounds.bottom = rect.top - 6.0f;
157 	bounds.right -= B_V_SCROLL_BAR_WIDTH;
158 
159 	fText = new BTextView(bounds, "text_view", BRect(0, 0, bounds.Width(),
160 				bounds.Height()), B_FOLLOW_NONE, B_WILL_DRAW);
161 	fText->MakeEditable(false);
162 	fText->MakeSelectable(true);
163 	fText->SetText(text);
164 
165 	BScrollView *scroll = new BScrollView("scroll_view", fText,
166 			B_FOLLOW_LEFT | B_FOLLOW_TOP, 0, false, true);
167 
168 	fBackground->AddChild(scroll);
169 
170 	AddChild(fBackground);
171 }
172 
173 
174 /*void
175 PackageTextViewer::_InitView(const char *text, bool disclaimer)
176 {
177 	SetLayout(new BGroupLayout(B_HORIZONTAL));
178 
179 	fText = new BTextView(BRect(0, 0, 1, 1), "text_view", BRect(0, 0, 1, 1),
180 			B_FOLLOW_NONE, B_WILL_DRAW | B_SUPPORTS_LAYOUT);
181 	fText->MakeEditable(false);
182 	fText->MakeSelectable(true);
183 	BScrollView *scroll = new BScrollView("scroll_view", fText,
184 			B_FOLLOW_LEFT | B_FOLLOW_TOP, 0, false, true);
185 
186 	if (disclaimer) {
187 		BButton *accept = new BButton("accept", T("Accept"),
188 				new BMessage(P_MSG_ACCEPT));
189 
190 		BButton *decline = new BButton("decline", T("Decline"),
191 				new BMessage(P_MSG_DECLINE));
192 
193 		fBackground = BGroupLayoutBuilder(B_VERTICAL)
194 			.Add(scroll)
195 			.AddGroup(B_HORIZONTAL, 5.0f)
196 				.AddGlue()
197 				.Add(accept)
198 				.Add(decline)
199 			.End();
200 	}
201 	else {
202 		BButton *button = new BButton("accept", T("Continue"),
203 				new BMessage(P_MSG_ACCEPT));
204 
205 		fBackground = BGroupLayoutBuilder(B_VERTICAL)
206 			.Add(scroll)
207 			.AddGroup(B_HORIZONTAL, 5.0f)
208 				.AddGlue()
209 				.Add(button)
210 			.End();
211 	}
212 
213 	AddChild(fBackground);
214 
215 	fBackground->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
216 	fText->SetText(text);
217 }*/
218 
219