vfs: check userland buffers before reading them.
[haiku.git] / src / preferences / appearance / ColorWhichListView.cpp
blob6bd4e9654dc51b928503e87fee61a9d374f39c1b
1 /*
2 * Copyright 2016 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * John Scipione, jscipione@gmail.com
7 */
10 #include "ColorWhichListView.h"
12 #include <algorithm>
14 #include <math.h>
15 #include <stdio.h>
17 #include <Bitmap.h>
19 #include "ColorWhichItem.h"
22 // golden ratio
23 #ifdef M_PHI
24 # undef M_PHI
25 #endif
26 #define M_PHI 1.61803398874989484820
29 // #pragma mark - ColorWhichListView
32 ColorWhichListView::ColorWhichListView(const char* name,
33 list_view_type type, uint32 flags)
35 BListView(name, type, flags)
40 ColorWhichListView::~ColorWhichListView()
45 bool
46 ColorWhichListView::InitiateDrag(BPoint where, int32 index, bool wasSelected)
48 ColorWhichItem* colorWhichItem
49 = dynamic_cast<ColorWhichItem*>(ItemAt(index));
50 if (colorWhichItem == NULL)
51 return false;
53 rgb_color color = colorWhichItem->Color();
55 char hexString[7];
56 sprintf(hexString, "#%.2X%.2X%.2X", color.red, color.green, color.blue);
58 BMessage message(B_PASTE);
59 message.AddData("text/plain", B_MIME_TYPE, &hexString, sizeof(hexString));
60 message.AddData("RGBColor", B_RGB_COLOR_TYPE, &color, sizeof(color));
62 float itemHeight = colorWhichItem->Height() - 5;
63 BRect rect(0.0f, 0.0f, roundf(itemHeight * M_PHI) - 1, itemHeight - 1);
65 BBitmap* bitmap = new BBitmap(rect, B_RGB32, true);
66 if (bitmap->Lock()) {
67 BView* view = new BView(rect, "", B_FOLLOW_NONE, B_WILL_DRAW);
68 bitmap->AddChild(view);
70 view->SetHighColor(B_TRANSPARENT_COLOR);
71 view->FillRect(view->Bounds());
73 ++rect.top;
74 ++rect.left;
76 view->SetHighColor(0, 0, 0, 100);
77 view->FillRect(rect);
78 rect.OffsetBy(-1.0f, -1.0f);
80 view->SetHighColor(std::min(255, (int)(1.2 * color.red + 40)),
81 std::min(255, (int)(1.2 * color.green + 40)),
82 std::min(255, (int)(1.2 * color.blue + 40)));
83 view->StrokeRect(rect);
85 ++rect.left;
86 ++rect.top;
88 view->SetHighColor((int32)(0.8 * color.red),
89 (int32)(0.8 * color.green),
90 (int32)(0.8 * color.blue));
91 view->StrokeRect(rect);
93 --rect.right;
94 --rect.bottom;
96 view->SetHighColor(color.red, color.green, color.blue);
97 view->FillRect(rect);
98 view->Sync();
100 bitmap->Unlock();
103 DragMessage(&message, bitmap, B_OP_ALPHA, BPoint(14.0f, 14.0f));
105 return true;