xref: /haiku/src/apps/webpositive/ConsoleWindow.cpp (revision 18027fff34af4a666c1e62254b462cbaeae1859e)
1 /*
2  * Copyright 2014 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Zhuowei Zhang
7  */
8 #include "ConsoleWindow.h"
9 
10 #include <Catalog.h>
11 #include <Message.h>
12 #include <Button.h>
13 #include <GroupLayout.h>
14 #include <GroupLayoutBuilder.h>
15 #include <LayoutBuilder.h>
16 #include <SeparatorView.h>
17 #include <TextControl.h>
18 #include <ListView.h>
19 #include <ScrollView.h>
20 
21 #include "BrowserWindow.h"
22 #include "BrowserApp.h"
23 #include "WebViewConstants.h"
24 
25 
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "Console Window"
28 
29 
30 enum {
31 	EVAL_CONSOLE_WINDOW_COMMAND = 'ecwc',
32 	CLEAR_CONSOLE_MESSAGES = 'ccms'
33 };
34 
35 
36 ConsoleWindow::ConsoleWindow(BRect frame)
37 	:
38 	BWindow(frame, B_TRANSLATE("Script console"), B_TITLED_WINDOW,
39 		B_NORMAL_WINDOW_FEEL, B_AUTO_UPDATE_SIZE_LIMITS
40 			| B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE)
41 {
42 	SetLayout(new BGroupLayout(B_VERTICAL, 0.0));
43 
44 	fMessagesListView = new BListView("Console messages");
45 	fClearMessagesButton = new BButton(B_TRANSLATE("Clear"),
46 		new BMessage(CLEAR_CONSOLE_MESSAGES));
47 
48 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0.0)
49 		.Add(new BScrollView("Console messages scroll",
50 			fMessagesListView, 0, true, true))
51 		.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER))
52 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, B_USE_SMALL_SPACING)
53 			.Add(fClearMessagesButton)
54 			.SetInsets(0, 5, 0, 5)
55 		)
56 		.SetInsets(5, 5, 5, 5)
57 	);
58 	if (!frame.IsValid())
59 		CenterOnScreen();
60 }
61 
62 
63 void
64 ConsoleWindow::MessageReceived(BMessage* message)
65 {
66 	switch (message->what) {
67 		case ADD_CONSOLE_MESSAGE:
68 		{
69 			BString source = message->FindString("source");
70 			int32 lineNumber = message->FindInt32("line");
71 			int32 columnNumber = message->FindInt32("column");
72 			BString text = message->FindString("string");
73 			BString finalText;
74 			finalText.SetToFormat("%s:%" B_PRIi32 ":%" B_PRIi32 ": %s\n",
75 				source.String(), lineNumber, columnNumber, text.String());
76 			fMessagesListView->AddItem(new BStringItem(finalText.String()));
77 			break;
78 		}
79 		case CLEAR_CONSOLE_MESSAGES:
80 		{
81 			int count = fMessagesListView->CountItems();
82 			for (int i = count - 1; i >= 0; i--) {
83 				delete fMessagesListView->RemoveItem(i);
84 			}
85 			break;
86 		}
87 		default:
88 			BWindow::MessageReceived(message);
89 			break;
90 	}
91 }
92 
93 
94 bool
95 ConsoleWindow::QuitRequested()
96 {
97 	if (!IsHidden())
98 		Hide();
99 	return false;
100 }
101