xref: /haiku/src/servers/debug/DebugWindow.cpp (revision 9d010ea47db677131e385b5e7855d38fd0c8103f)
1 /*
2  * Copyright 2019, Adrien Destugues, pulkomandy@pulkomandy.tk.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "DebugWindow.h"
8 
9 #include <algorithm>
10 #include <stdio.h>
11 
12 #include <Button.h>
13 #include <Catalog.h>
14 #include <GroupLayout.h>
15 #include <LayoutBuilder.h>
16 #include <Locale.h>
17 #include <IconUtils.h>
18 #include <Resources.h>
19 #include <RadioButton.h>
20 #include <StripeView.h>
21 #include <TextView.h>
22 
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "DebugServer"
25 
26 
27 DebugWindow::DebugWindow(const char* appName)
28 	:
29 	BWindow(BRect(0, 0, 100, 50), "Crashed program", B_MODAL_WINDOW,
30 		B_CLOSE_ON_ESCAPE | B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS),
31 	fBitmap(IconSize(), B_RGBA32),
32 	fSemaphore(create_sem(0, "DebugWindow")),
33 	fAction(kActionKillTeam)
34 {
35 	BString buffer(B_TRANSLATE(
36 		"The application:\n\n      %app\n\n"
37 		"has encountered an error which prevents it from continuing. Haiku "
38 		"will terminate the application and clean up."));
39 	buffer.ReplaceFirst("%app", appName);
40 
41 	BResources resources;
42 	resources.SetToImage(B_TRANSLATION_CONTEXT);
43 	size_t size;
44 	const uint8* iconData = (const uint8*)resources.LoadResource('VICN', 2,
45 		&size);
46 	BIconUtils::GetVectorIcon(iconData, size, &fBitmap);
47 	BStripeView *stripeView = new BStripeView(fBitmap);
48 
49 	BTextView *message = new BTextView("_tv_");
50 	message->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
51 	rgb_color textColor = ui_color(B_PANEL_TEXT_COLOR);
52 	message->SetFontAndColor(be_plain_font, B_FONT_ALL, &textColor);
53 	message->MakeEditable(false);
54 	message->MakeSelectable(false);
55 	message->SetWordWrap(true);
56 	message->SetText(buffer);
57 	message->SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNSET));
58 	float width = message->StringWidth(appName)
59 		+ message->StringWidth(" ") * 12;
60 	width = std::max(width, message->StringWidth("W") * 30);
61 	message->SetExplicitMinSize(BSize(width, B_SIZE_UNSET));
62 
63 	BRadioButton *terminate = new BRadioButton("terminate",
64 		B_TRANSLATE("Terminate"), new BMessage(kActionKillTeam));
65 #ifdef HANDOVER_USE_DEBUGGER
66 	BRadioButton *report = new BRadioButton("report",
67 		B_TRANSLATE("Save report"), new BMessage(kActionSaveReportTeam));
68 #endif
69 	BRadioButton *debug = new BRadioButton("debug",
70 		B_TRANSLATE("Debug"), new BMessage(kActionDebugTeam));
71 	BRadioButton *core = new BRadioButton("core",
72 		B_TRANSLATE("Write core file"), new BMessage(kActionWriteCoreFile));
73 
74 	fOKButton = new BButton("close", B_TRANSLATE("Oh no!"),
75 		new BMessage(B_QUIT_REQUESTED));
76 
77 	terminate->SetValue(B_CONTROL_ON);
78 
79 	BLayoutBuilder::Group<>(this)
80 		.AddGroup(B_HORIZONTAL)
81 			.Add(stripeView)
82 			.AddGroup(B_VERTICAL)
83 				.SetInsets(B_USE_SMALL_SPACING)
84 				.Add(message)
85 				.AddGroup(B_VERTICAL, 0)
86 					.Add(terminate)
87 					.Add(debug)
88 					.Add(report)
89 					.Add(core)
90 				.End()
91 				.AddGroup(B_HORIZONTAL)
92 					.AddGlue()
93 					.Add(fOKButton)
94 				.End()
95 			.End()
96 		.End();
97 
98 	ResizeToPreferred();
99 	CenterOnScreen();
100 }
101 
102 
103 DebugWindow::~DebugWindow()
104 {
105 	delete_sem(fSemaphore);
106 }
107 
108 
109 void
110 DebugWindow::MessageReceived(BMessage* message)
111 {
112 	switch(message->what) {
113 		case B_QUIT_REQUESTED:
114 			release_sem(fSemaphore);
115 			break;
116 
117 		case kActionKillTeam:
118 		case kActionDebugTeam:
119 		case kActionWriteCoreFile:
120 		case kActionSaveReportTeam:
121 			fAction = message->what;
122 			fOKButton->SetLabel(fAction == kActionDebugTeam
123 				? B_TRANSLATE("Oh yeah!") : B_TRANSLATE("Oh no!"));
124 			return;
125 	}
126 
127 	BWindow::MessageReceived(message);
128 }
129 
130 
131 int32
132 DebugWindow::Go()
133 {
134 	Show();
135 
136 	// Wait for user to close the window
137 	acquire_sem(fSemaphore);
138 	return fAction;
139 }
140 
141 
142 BRect
143 DebugWindow::IconSize()
144 {
145 	int32 size = std::max((int32)1, ((int32)be_plain_font->Size() + 15) / 16)
146 		* 32 - 1;
147 	return BRect(0, 0, size, size);
148 }
149