Ticket #15254: ViewMessageTest.cpp

File ViewMessageTest.cpp, 3.4 KB (added by simonsouth, 5 years ago)

Indicates which messages are actually received by BView.

Line 
1// Tests whether certain messages are received by a BView instance.
2//
3// Compile and execute with:
4//
5// g++ -lbe -o ViewMessageTest ViewMessageTest.cpp
6// ./ViewMessageTest
7//
8// Please run this on a real BeOS instance and using the keyboard and mouse,
9// try to check as many boxes as possible. Then post a screenshot with the
10// results.
11//
12// Simon South <simon@simonsouth.net>
13// 15 August 2019
14
15#include <stdio.h>
16
17#include <map>
18#include <vector>
19
20#include <Application.h>
21#include <CheckBox.h>
22#include <View.h>
23#include <Window.h>
24
25
26#define MESSAGE_NAME_PAIR(message) pair<uint32,const char *>(message, #message)
27
28
29class ViewMessageTestView : public BView {
30 public:
31 ViewMessageTestView(BRect frame);
32
33 protected:
34 void MessageReceived(BMessage* message);
35
36 map<uint32,BCheckBox*> fMessageCheckBoxes;
37};
38
39class ViewMessageTestApp : public BApplication {
40 public:
41 ViewMessageTestApp();
42 private:
43 ViewMessageTestView* fView;
44 BWindow* fWindow;
45};
46
47// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48
49ViewMessageTestView::ViewMessageTestView(BRect frame)
50 :
51 BView(frame, "View Message Test", B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
52 fMessageCheckBoxes()
53{
54 float y = 10.0;
55
56 // The list of messages we're interested in capturing, together with their
57 // name
58 vector< pair<uint32,const char *> > messages;
59 messages.reserve(8);
60 messages.push_back(MESSAGE_NAME_PAIR(B_INVALIDATE));
61 messages.push_back(MESSAGE_NAME_PAIR(B_KEY_DOWN));
62 messages.push_back(MESSAGE_NAME_PAIR(B_KEY_UP));
63 messages.push_back(MESSAGE_NAME_PAIR(B_MOUSE_DOWN));
64 messages.push_back(MESSAGE_NAME_PAIR(B_MOUSE_MOVED));
65 messages.push_back(MESSAGE_NAME_PAIR(B_MOUSE_UP));
66 messages.push_back(MESSAGE_NAME_PAIR(B_UNMAPPED_KEY_DOWN));
67 messages.push_back(MESSAGE_NAME_PAIR(B_UNMAPPED_KEY_UP));
68
69 // Using this list, create a checkbox associated with each message
70 for (vector< pair<uint32,const char *> >::iterator p = messages.begin();
71 p != messages.end(); ++p) {
72 const char *messageName = (*p).second;
73 float height, width;
74
75 BCheckBox *checkBox = new BCheckBox(messageName);
76 checkBox->SetEnabled(false);
77 checkBox->SetValue(0);
78 checkBox->GetPreferredSize(&width, &height);
79 checkBox->ResizeToPreferred();
80 checkBox->MoveTo(10.0, y);
81
82 fMessageCheckBoxes.insert(pair<uint32,BCheckBox*>((*p).first,
83 checkBox));
84 AddChild(checkBox);
85
86 y += height + 3;
87 }
88}
89
90void
91ViewMessageTestView::MessageReceived(BMessage* message)
92{
93 // If the message is one we're listening for, check its box
94 map<uint32,BCheckBox*>::iterator checkBoxPair =
95 fMessageCheckBoxes.find(message->what);
96 if (checkBoxPair != fMessageCheckBoxes.end())
97 (*checkBoxPair).second->SetValue(1);
98
99 BView::MessageReceived(message);
100}
101
102// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103
104ViewMessageTestApp::ViewMessageTestApp()
105 :
106 BApplication("application/x-vnd.simonsouth-viewmessagetest"),
107 fView(NULL),
108 fWindow(NULL)
109{
110 BRect frame(0, 0, 250, 250);
111
112 fWindow = new BWindow(frame, "View Message Test", B_TITLED_WINDOW,
113 B_QUIT_ON_WINDOW_CLOSE);
114
115 fView = new ViewMessageTestView(frame);
116 fWindow->AddChild(fView);
117
118 fView->ResizeToPreferred();
119 fView->MakeFocus(true);
120 fView->Show();
121
122 fWindow->CenterOnScreen();
123 fWindow->Show();
124}
125
126// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
127
128int
129main(int argc, char **argv)
130{
131 ViewMessageTestApp app;
132 app.Run();
133
134 return 0;
135}