xref: /haiku/src/tests/kits/interface/balert/alert_manual_test/AlertTestWindow.cpp (revision a55deaea91d64802ed655d4ffcb41a3519338144)
1 // AlertTestWindow.cpp
2 
3 #include <Application.h>
4 #include <Roster.h>
5 #include <Alert.h>
6 #include <TextView.h>
7 #include <Entry.h>
8 #include <Path.h>
9 #include <String.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "AlertTestWindow.h"
13 
14 const char *k20X = "XXXXXXXXXXXXXXXXXXXX";
15 const char *k40X = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
16 const char *k60X = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
17 
18 AlertTestWindow::AlertTestWindow(BRect frame)
19 	: BWindow(frame, "AlertTestWindow", B_TITLED_WINDOW,
20 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
21 {
22 	fAlertType = 'H';
23 
24 	BString strLabel = "Alert Manual Test";
25 	app_info info;
26 	if (be_app->GetAppInfo(&info) >= B_OK) {
27 		BEntry entry(&info.ref);
28 		if (entry.InitCheck() >= B_OK) {
29 			BPath path(&entry);
30 			if (path.InitCheck() >= B_OK) {
31 				strLabel.Append(" (");
32 				strLabel.Append(path.Leaf());
33 				strLabel.Append(")");
34 				printf(": Version: %s\n", path.Leaf());
35 
36 				if (path.Leaf()[0] == 'b')
37 					fAlertType = 'B';
38 				else
39 					fAlertType = 'H';
40 			}
41 		}
42 	}
43 
44 	fTitleView = new BStringView(BRect(10, 10, Bounds().Width() - 10, 30),
45 		"title", strLabel.String());
46 	fTitleView->SetFontSize(16);
47 
48 	fRunButton = new BButton(BRect(10, 40, 100, 60),
49 		"runbtn", "Run", new BMessage(MSG_RUN_BUTTON));
50 
51 	AddChild(fTitleView);
52 	AddChild(fRunButton);
53 }
54 
55 void
56 AlertTestWindow::MessageReceived(BMessage *message)
57 {
58 	switch (message->what) {
59 		case MSG_RUN_BUTTON:
60 			printf("%c<Run Button\n", fAlertType);
61 			Test();
62 			break;
63 		default:
64 			break;
65 	}
66 }
67 
68 void which_label(const char *text, BString &outString)
69 {
70 	int nX = 0;
71 	if (strcmp(text, k60X) == 0)
72 		nX = 60;
73 	else if (strcmp(text, k40X) == 0)
74 		nX = 40;
75 	else if (strcmp(text, k20X) == 0)
76 		nX = 20;
77 
78 	outString = "";
79 	if (nX == 0) {
80 		outString << '"' << text << '"';
81 	} else {
82 		outString << 'k' << nX << 'X';
83 	}
84 }
85 
86 void
87 AlertTestWindow::Test()
88 {
89 	BAlert *pAlert = new BAlert(
90 		"alert1",
91 		k60X,
92 		k20X, "OK", "Cancel",
93 		B_WIDTH_AS_USUAL, // widthStyle
94 		B_OFFSET_SPACING,
95 		B_EMPTY_ALERT		// alert_type
96 	);
97 	if (fAlertType == 'H') {
98 		BView *master = pAlert->ChildAt(0);
99 		master->SetViewUIColor(B_MENU_BACKGROUND_COLOR);
100 	}
101 
102 	BPoint pt;
103 	BString strLabel;
104 	BButton *pBtns[3] = { NULL };
105 	pBtns[0] = pAlert->ButtonAt(0);
106 	pBtns[1] = pAlert->ButtonAt(1);
107 	pBtns[2] = pAlert->ButtonAt(2);
108 
109 	BTextView *pTextView = pAlert->TextView();
110 
111 	// Window info
112 	printf("wi.width = %.1ff;\n"
113 		"wi.height = %.1ff;\n"
114 		"ati.SetWinInfo(wi);\n",
115 		pAlert->Bounds().Width(), pAlert->Bounds().Height());
116 
117 	// TextView info
118 	printf("\n");
119 	which_label(pTextView->Text(), strLabel);
120 	pt = pTextView->ConvertToParent(BPoint(0, 0));
121 	printf("ti.label = %s;\n"
122 		"ti.width = %.1ff;\n"
123 		"ti.height = %.1ff;\n"
124 		"ti.topleft.Set(%.1ff, %.1ff);\n"
125 		"ati.SetTextViewInfo(ti);\n",
126 		strLabel.String(), pTextView->Bounds().Width(),
127 		pTextView->Bounds().Height(), pt.x, pt.y);
128 
129 	// Button info
130 	printf("\n");
131 	int32 i = 0;
132 	while (i < 3 && pBtns[i] != NULL) {
133 		BButton *pb = pBtns[i];
134 		which_label(pb->Label(), strLabel);
135 		pt = pb->ConvertToParent(BPoint(0, 0));
136 		printf("bi.label = %s;\n"
137 			"bi.width = %.1ff;\n"
138 			"bi.height = %.1ff;\n"
139 			"bi.topleft.Set(%.1ff, %.1ff);\n"
140 			"ati.SetButtonInfo(%d, bi);\n",
141 			strLabel.String(), pb->Bounds().Width(),
142 			pb->Bounds().Height(), pt.x, pt.y,
143 			(int)i);
144 		i++;
145 	}
146 
147 	int32 result = pAlert->Go();
148 	printf("%c<Clicked: %d\n", fAlertType, static_cast<int>(result));
149 	pAlert = NULL;
150 }
151 
152 bool
153 AlertTestWindow::QuitRequested()
154 {
155 	printf("%c<Quit\n", fAlertType);
156 	be_app->PostMessage(B_QUIT_REQUESTED);
157 	return true;
158 }
159