-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorPickerPanel.cpp
206 lines (168 loc) · 4.45 KB
/
ColorPickerPanel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright 2012-2013 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus, [email protected]
* John Scipione, [email protected]
*/
#include "ColorPickerPanel.h"
#include <stdio.h>
#include <Alignment.h>
#include <Application.h>
#include <Box.h>
#include <Button.h>
#include <Catalog.h>
#include <Control.h>
#include <InterfaceDefs.h>
#include <LayoutBuilder.h>
#include <Locale.h>
#include <Message.h>
#include <Rect.h>
#include <Size.h>
#include <View.h>
#include "ColorPicker.h"
#include "Protocol.h"
enum {
MSG_CANCEL = 'cncl',
MSG_DONE = 'done',
};
const int32 kColorChanged = 'clch';
const int32 kColorDropped = 'PSTE';
ColorPickerPanel::ColorPickerPanel(ColorPicker* view, BMessage* message,
color_control_layout layout)
:
BWindow(BRect(100, 100, 100, 100), "Pick a color",
B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS
| B_CLOSE_ON_ESCAPE),
fColorPicker(view),
fMessage(message)
{
BBox* divider = new BBox(
BRect(0, 0, 0, 0), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
B_FRAME_EVENTS, B_FANCY_BORDER);
divider->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
ColorContainersView* containersView = new ColorContainersView(layout);
BButton* defaultButton = new BButton("ok button", "OK",
new BMessage(MSG_DONE));
SetDefaultButton(defaultButton);
defaultButton->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT,
B_ALIGN_VERTICAL_CENTER));
BButton* cancelButton = new BButton("cancel button", "Cancel",
new BMessage(MSG_CANCEL));
BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_SMALL_SPACING)
.Add(fColorPicker)
.Add(divider)
.Add(containersView)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.AddGlue()
.Add(cancelButton)
.Add(defaultButton)
.End()
.SetInsets(B_USE_DEFAULT_SPACING)
.End();
if (message != NULL) {
// set the window title based on the client
const char* title;
if (message->FindString(kTargetName, &title) == B_OK) {
BString newTitle;
newTitle << "Select \"" << title << "\" color";
SetTitle(newTitle.String());
}
// Move window under the mouse cursor
BPoint where;
if (message->FindPoint(kInvokePoint, &where) == B_OK)
MoveTo(where + BPoint(30, 0));
// set the initial color value
const rgb_color *color;
ssize_t size;
if (message && message->FindData(kInitialValue, B_RGB_COLOR_TYPE,
reinterpret_cast<const void **>(&color), &size) == B_OK) {
fInitialColor = *color;
fColorPicker->SetColor(fInitialColor);
}
// save the address of of the client
BMessenger target;
if (message->FindMessenger(kClientAddress, &target) == B_OK) {
fTarget = target;
// let the client know we are ready to serve it
BMessage reply(kOpenConnection);
reply.AddMessenger(kServerAddress, BMessenger(this));
reply.AddInt32(kProvidedValues, B_RGB_COLOR_TYPE);
// and that we can supply color values
fTarget.SendMessage(&reply);
}
}
}
ColorPickerPanel::~ColorPickerPanel()
{
fTarget.SendMessage(kServerQuitting);
}
void
ColorPickerPanel::MessageReceived(BMessage* message)
{
switch (message->what) {
case kActivateWindow:
Activate();
break;
case kColorChanged:
case kColorDropped:
{
// inform the target that the color has changed
message->what = B_VALUE_CHANGED;
fTarget.SendMessage(message);
break;
}
case B_VALUE_CHANGED:
{
// client color changed without our intervention,
// sync up
rgb_color* color;
ssize_t size;
if (message->FindData("be:value", B_RGB_COLOR_TYPE,
(const void **)(&color), &size) == B_OK) {
fColorPicker->SetColor(*color);
}
break;
}
case MSG_CANCEL:
Cancel();
break;
case MSG_DONE:
Done();
break;
case kCloseConnection:
be_app->PostMessage(B_QUIT_REQUESTED);
break;
default:
BWindow::MessageReceived(message);
break;
}
}
bool
ColorPickerPanel::QuitRequested()
{
be_app->PostMessage(B_QUIT_REQUESTED);
// when window closes, quit the app
return true;
}
// #pragma mark -
void
ColorPickerPanel::Cancel()
{
// reset color back to initial color and quit
BMessage* message = new BMessage(B_VALUE_CHANGED);
message->AddData("be:value", B_RGB_COLOR_TYPE, &fInitialColor,
sizeof(fInitialColor));
fTarget.SendMessage(message);
delete message;
fTarget.SendMessage(B_CANCEL);
be_app->PostMessage(B_QUIT_REQUESTED);
}
void
ColorPickerPanel::Done()
{
fTarget.SendMessage(kApplyValue);
be_app->PostMessage(B_QUIT_REQUESTED);
}