xref: /haiku/src/apps/webpositive/ConsoleWindow.cpp (revision 4a32f48e70297d9a634646f01e08c2f451ecd6bd)
1 /*
2  * Copyright 2014-2021 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Zhuowei Zhang
7  *		Humdinger
8  */
9 #include "ConsoleWindow.h"
10 
11 #include <Catalog.h>
12 #include <Clipboard.h>
13 #include <Message.h>
14 #include <Button.h>
15 #include <GroupLayout.h>
16 #include <GroupLayoutBuilder.h>
17 #include <LayoutBuilder.h>
18 #include <SeparatorView.h>
19 #include <TextControl.h>
20 #include <ListView.h>
21 #include <ScrollView.h>
22 
23 #include "BrowserWindow.h"
24 #include "BrowserApp.h"
25 #include "WebViewConstants.h"
26 
27 
28 #undef B_TRANSLATION_CONTEXT
29 #define B_TRANSLATION_CONTEXT "Console Window"
30 
31 
32 enum {
33 	EVAL_CONSOLE_WINDOW_COMMAND = 'ecwc',
34 	CLEAR_CONSOLE_MESSAGES = 'ccms'
35 };
36 
37 
38 ConsoleWindow::ConsoleWindow(BRect frame)
39 	:
40 	BWindow(frame, B_TRANSLATE("Script console"), B_TITLED_WINDOW,
41 		B_NORMAL_WINDOW_FEEL, B_AUTO_UPDATE_SIZE_LIMITS
42 			| B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE)
43 {
44 	SetLayout(new BGroupLayout(B_VERTICAL, 0.0));
45 
46 	fMessagesListView = new BListView("Console messages",
47 		B_MULTIPLE_SELECTION_LIST);
48 	fClearMessagesButton = new BButton(B_TRANSLATE("Clear"),
49 		new BMessage(CLEAR_CONSOLE_MESSAGES));
50 
51 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0.0)
52 		.Add(new BScrollView("Console messages scroll",
53 			fMessagesListView, 0, true, true))
54 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, B_USE_SMALL_SPACING)
55 			.Add(fClearMessagesButton)
56 			.SetInsets(0, B_USE_SMALL_SPACING, 0, 0))
57 		.SetInsets(B_USE_SMALL_SPACING, B_USE_SMALL_SPACING,
58 			B_USE_SMALL_SPACING, B_USE_SMALL_SPACING)
59 	);
60 	if (!frame.IsValid())
61 		CenterOnScreen();
62 }
63 
64 
65 void
66 ConsoleWindow::MessageReceived(BMessage* message)
67 {
68 	switch (message->what) {
69 		case ADD_CONSOLE_MESSAGE:
70 		{
71 			BString source = message->FindString("source");
72 			int32 lineNumber = message->FindInt32("line");
73 			int32 columnNumber = message->FindInt32("column");
74 			BString text = message->FindString("string");
75 			BString finalText;
76 			finalText.SetToFormat("%s:%" B_PRIi32 ":%" B_PRIi32 ": %s\n",
77 				source.String(), lineNumber, columnNumber, text.String());
78 			fMessagesListView->AddItem(new BStringItem(finalText.String()));
79 			break;
80 		}
81 		case CLEAR_CONSOLE_MESSAGES:
82 		{
83 			int count = fMessagesListView->CountItems();
84 			for (int i = count - 1; i >= 0; i--) {
85 				delete fMessagesListView->RemoveItem(i);
86 			}
87 			break;
88 		}
89 		case B_COPY:
90 		{
91 			_CopyToClipboard();
92 			break;
93 		}
94 		default:
95 			BWindow::MessageReceived(message);
96 			break;
97 	}
98 }
99 
100 
101 bool
102 ConsoleWindow::QuitRequested()
103 {
104 	if (!IsHidden())
105 		Hide();
106 	return false;
107 }
108 
109 
110 void
111 ConsoleWindow::_CopyToClipboard()
112 {
113 	if (fMessagesListView->CurrentSelection() == -1)
114 		return;
115 
116 	BString text;
117 	int32 index;
118 	for (int32 i = 0; (index = fMessagesListView->CurrentSelection(i)) >= 0;
119 			i++) {
120 		BStringItem* item = (BStringItem*)fMessagesListView->ItemAt(index);
121 		text << item->Text();
122 	}
123 
124 	ssize_t textLen = text.Length();
125 	if (be_clipboard->Lock()) {
126 		be_clipboard->Clear();
127 		BMessage* clip = be_clipboard->Data();
128 		if (clip != NULL) {
129 			clip->AddData("text/plain", B_MIME_TYPE, text.String(), textLen);
130 			be_clipboard->Commit();
131 		}
132 		be_clipboard->Unlock();
133 	}
134 }
135