From e88a89e6765f96b7d88411cf73cefcf9d908f393 Mon Sep 17 00:00:00 2001 From: John Scipione Date: Wed, 4 Jul 2018 19:54:07 -0700 Subject: [PATCH] Appearance Prefs: drag and drop between ColorWhichItems Fixes final piece of #8618 Already added support for list items to drag colors out and you can drag and drop between the list items and preview. but, what was missing was drag and drop between list items. Updated ColorWhichListItem to also accept color drops through their parent ColorWhichListView. Also included some related style fixes, use B_RGB_COLOR_TYPE constant in place of (type_code)'RGBC'. 80-char limit fixes. Simplify similar code in ColorPreview class to parse out rgb_color from message. ColorPreview passes dropped color along to APRView APRView no longer accepts color drops, this is handled by ListView and ColorPreview now. Consolidated "RGBColor" and "which" message name strings into constants defined in defs.h. Change-Id: I88ec2a4ffe077620ec4cc3b032196cbff0f09615 --- src/preferences/appearance/APRView.cpp | 51 ++++++++++++++----- src/preferences/appearance/APRView.h | 1 + src/preferences/appearance/ColorPreview.cpp | 20 +++++--- .../appearance/ColorWhichListView.cpp | 45 +++++++++++++++- .../appearance/ColorWhichListView.h | 2 + src/preferences/appearance/defs.h | 7 +++ 6 files changed, 102 insertions(+), 24 deletions(-) diff --git a/src/preferences/appearance/APRView.cpp b/src/preferences/appearance/APRView.cpp index 4a4a526e85..434bf2232c 100644 --- a/src/preferences/appearance/APRView.cpp +++ b/src/preferences/appearance/APRView.cpp @@ -130,19 +130,35 @@ APRView::AttachedToWindow() void APRView::MessageReceived(BMessage *msg) { - if (msg->WasDropped()) { - rgb_color* color = NULL; - ssize_t size = 0; - - if (msg->FindData("RGBColor", (type_code)'RGBC', (const void**)&color, - &size) == B_OK) { - _SetCurrentColor(*color); - - Window()->PostMessage(kMsgUpdate); - } - } - switch (msg->what) { + case SET_COLOR: + { + rgb_color* color; + ssize_t size; + color_which which; + + if (msg->FindData(kRGBColor, B_RGB_COLOR_TYPE, + (const void**)&color, &size) == B_OK + && msg->FindUInt32(kWhich, (uint32*)&which) == B_OK) { + _SetColor(which, *color); + Window()->PostMessage(kMsgUpdate); + } + break; + } + + case SET_CURRENT_COLOR: + { + rgb_color* color; + ssize_t size; + + if (msg->FindData(kRGBColor, B_RGB_COLOR_TYPE, + (const void**)&color, &size) == B_OK) { + _SetCurrentColor(*color); + Window()->PostMessage(kMsgUpdate); + } + break; + } + case UPDATE_COLOR: { // Received from the color fPicker when its color changes @@ -232,11 +248,18 @@ APRView::IsRevertable() } +void +APRView::_SetColor(color_which which, rgb_color color) +{ + set_ui_color(which, color); + fCurrentColors.SetColor(ui_color_name(which), color); +} + + void APRView::_SetCurrentColor(rgb_color color) { - set_ui_color(fWhich, color); - fCurrentColors.SetColor(ui_color_name(fWhich), color); + _SetColor(fWhich, color); int32 currentIndex = fAttrList->CurrentSelection(); ColorWhichItem* item = (ColorWhichItem*)fAttrList->ItemAt(currentIndex); diff --git a/src/preferences/appearance/APRView.h b/src/preferences/appearance/APRView.h index 598d385b13..8aadbc87b1 100644 --- a/src/preferences/appearance/APRView.h +++ b/src/preferences/appearance/APRView.h @@ -50,6 +50,7 @@ public: bool IsRevertable(); private: + void _SetColor(color_which which, rgb_color color); void _SetCurrentColor(rgb_color color); void _SetUIColors(const BMessage& colors); void _UpdatePreviews(const BMessage& colors); diff --git a/src/preferences/appearance/ColorPreview.cpp b/src/preferences/appearance/ColorPreview.cpp index 94bfbf283f..df82cd585d 100644 --- a/src/preferences/appearance/ColorPreview.cpp +++ b/src/preferences/appearance/ColorPreview.cpp @@ -21,6 +21,8 @@ #include #include +#include "defs.h" + static const int32 kMsgMessageRunner = 'MsgR'; @@ -116,13 +118,14 @@ ColorPreview::MessageReceived(BMessage* message) { // If we received a dropped message, see if it contains color data if (message->WasDropped()) { - rgb_color* col; - uint8* ptr; + rgb_color* color; ssize_t size; - if (message->FindData("RGBColor", (type_code)'RGBC', - (const void**)&ptr,&size) == B_OK) { - col = (rgb_color*)ptr; - SetHighColor(*col); + if (message->FindData(kRGBColor, B_RGB_COLOR_TYPE, + (const void**)&color, &size) == B_OK) { + BMessage setColorMessage(SET_CURRENT_COLOR); + setColorMessage.AddData(kRGBColor, B_RGB_COLOR_TYPE, color, + sizeof(color)); + Invoke(&setColorMessage); } } else if ((int32)message->what == kMsgMessageRunner) { BPoint where; @@ -232,8 +235,9 @@ ColorPreview::_DragColor(BPoint where) hexStr.SetToFormat("#%.2X%.2X%.2X", fColor.red, fColor.green, fColor.blue); BMessage message(B_PASTE); - message.AddData("text/plain", B_MIME_TYPE, hexStr.String(), hexStr.Length()); - message.AddData("RGBColor", B_RGB_COLOR_TYPE, &fColor, sizeof(fColor)); + message.AddData("text/plain", B_MIME_TYPE, hexStr.String(), + hexStr.Length()); + message.AddData(kRGBColor, B_RGB_COLOR_TYPE, &fColor, sizeof(fColor)); BRect rect(0.0f, 0.0f, 20.0f, 20.0f); diff --git a/src/preferences/appearance/ColorWhichListView.cpp b/src/preferences/appearance/ColorWhichListView.cpp index 67b0b71aee..9545c62f88 100644 --- a/src/preferences/appearance/ColorWhichListView.cpp +++ b/src/preferences/appearance/ColorWhichListView.cpp @@ -18,6 +18,7 @@ #include #include "ColorWhichItem.h" +#include "defs.h" // golden ratio @@ -57,8 +58,9 @@ ColorWhichListView::InitiateDrag(BPoint where, int32 index, bool wasSelected) hexStr.SetToFormat("#%.2X%.2X%.2X", color.red, color.green, color.blue); BMessage message(B_PASTE); - message.AddData("text/plain", B_MIME_TYPE, hexStr.String(), hexStr.Length()); - message.AddData("RGBColor", B_RGB_COLOR_TYPE, &color, sizeof(color)); + message.AddData("text/plain", B_MIME_TYPE, hexStr.String(), + hexStr.Length()); + message.AddData(kRGBColor, B_RGB_COLOR_TYPE, &color, sizeof(color)); float itemHeight = colorWhichItem->Height() - 5; BRect rect(0.0f, 0.0f, roundf(itemHeight * M_PHI) - 1, itemHeight - 1); @@ -105,3 +107,42 @@ ColorWhichListView::InitiateDrag(BPoint where, int32 index, bool wasSelected) return true; } + + +void +ColorWhichListView::MessageReceived(BMessage* message) +{ + // if we received a dropped message, see if it contains color data + if (message->WasDropped()) { + BPoint dropPoint = message->DropPoint(); + ConvertFromScreen(&dropPoint); + int32 index = IndexOf(dropPoint); + ColorWhichItem* item = dynamic_cast(ItemAt(index)); + rgb_color* color; + ssize_t size; + if (item != NULL && message->FindData(kRGBColor, B_RGB_COLOR_TYPE, + (const void**)&color, &size) == B_OK) { + // build message to send to APRView + int32 command = index == CurrentSelection() + ? SET_CURRENT_COLOR : SET_COLOR; + BMessage setColorMessage = BMessage(command); + setColorMessage.AddData(kRGBColor, B_RGB_COLOR_TYPE, color, size); + // if setting different color, add which color to set + if (command == SET_COLOR) + setColorMessage.AddUInt32(kWhich, (uint32)item->ColorWhich()); + + // build messenger and send message + BMessenger messenger = BMessenger(Parent()); + if (messenger.IsValid()) { + messenger.SendMessage(&setColorMessage); + if (command == SET_COLOR) { + // redraw item, SET_CURRENT_COLOR does this for us + item->SetColor(*color); + InvalidateItem(index); + } + } + } + } + + BListView::MessageReceived(message); +} diff --git a/src/preferences/appearance/ColorWhichListView.h b/src/preferences/appearance/ColorWhichListView.h index 0bbcd546a7..f069406bf4 100644 --- a/src/preferences/appearance/ColorWhichListView.h +++ b/src/preferences/appearance/ColorWhichListView.h @@ -24,6 +24,8 @@ public: virtual bool InitiateDrag(BPoint where, int32 index, bool wasSelected); + virtual void MessageReceived(BMessage* message); }; + #endif // COLORWHICH_LIST_VIEW_H diff --git a/src/preferences/appearance/defs.h b/src/preferences/appearance/defs.h index d14a0f19fe..2249439dda 100644 --- a/src/preferences/appearance/defs.h +++ b/src/preferences/appearance/defs.h @@ -19,10 +19,13 @@ #define APPEARANCE_APP_SIGNATURE "application/x-vnd.Haiku-Appearance" +// message commands #define APPLY_SETTINGS 'aply' #define TRY_SETTINGS 'trys' #define ATTRIBUTE_CHOSEN 'atch' +#define SET_COLOR 'sclr' +#define SET_CURRENT_COLOR 'sccl' #define UPDATE_COLOR 'upcl' #define DECORATOR_CHOSEN 'dcch' #define UPDATE_DECORATOR 'updc' @@ -34,6 +37,10 @@ #define SET_UI_COLORS 'suic' #define PREFS_CHOSEN 'prch' +// constants +static const char* const kRGBColor = "RGBColor"; +static const char* const kWhich = "which"; + // user interface const uint32 kBorderSpace = 10; const uint32 kItemSpace = 7;