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