Skip to content

Commit 8495a03

Browse files
committed
ColorPalette: rearranged and added field for color set names
- removed extra FillRect to lessen flicker when palette is resized. - added key controls to palette - left/right/up/down to choose color, page up/page down to change color set
1 parent 31949a9 commit 8495a03

File tree

3 files changed

+157
-28
lines changed

3 files changed

+157
-28
lines changed

artpaint/controls/ColorFloatSlider.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,16 @@ ColorFloatSlider::ColorFloatSlider(const char* label, const char* text, BMessage
5353

5454
fFormat.SetToFormat("%%0.%df", resolution);
5555
fFloatControl = new (std::nothrow) FloatControl(label, text,
56-
new BMessage(kNumberControlFinished), 5, minRange < 0);
56+
new BMessage(kNumberControlFinished), 4, minRange < 0);
5757
fSlider = new (std::nothrow) ColorSlider(NULL, NULL,
5858
new BMessage(kSliderModificationFinished), (int32)minRange * fMult,
5959
(int32)maxRange * fMult, B_HORIZONTAL, thumbStyle);
6060

6161
if (fFloatControl != NULL && fSlider != NULL && layout) {
6262
SetLayout(new BGroupLayout(B_VERTICAL));
63-
AddChild(BGroupLayoutBuilder(B_HORIZONTAL, 10.0)
64-
.Add(fFloatControl)
63+
AddChild(BGroupLayoutBuilder(B_HORIZONTAL, 1.0)
64+
.Add(fFloatControl->CreateLabelLayoutItem())
65+
.Add(fFloatControl->CreateTextViewLayoutItem())
6566
.Add(fSlider)
6667
);
6768

artpaint/controls/ColorPalette.cpp

Lines changed: 148 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ HexStringToUInt32(BString hexColor, uint32& color_word)
120120
ColorPaletteWindow::ColorPaletteWindow(BRect frame, int32 mode)
121121
:
122122
BWindow(frame, B_TRANSLATE("Colors"), B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
123-
B_NOT_V_RESIZABLE | B_NOT_ZOOMABLE | B_WILL_ACCEPT_FIRST_CLICK | B_AVOID_FRONT
123+
B_NOT_ZOOMABLE | B_WILL_ACCEPT_FIRST_CLICK | B_AVOID_FRONT
124124
| B_AUTO_UPDATE_SIZE_LIMITS),
125125
open_panel(NULL),
126126
save_panel(NULL)
@@ -156,6 +156,11 @@ ColorPaletteWindow::ColorPaletteWindow(BRect frame, int32 mode)
156156
color_container->SetDraggingEnabled(TRUE);
157157

158158
BFont font;
159+
font_height fontHt;
160+
font.GetHeight(&fontHt);
161+
162+
color_container->SetExplicitMinSize(BSize(B_SIZE_UNSET,
163+
(fontHt.ascent + fontHt.descent + fontHt.leading) * 6));
159164

160165
// Here add the buttons that control the color-set.
161166
previous_set = new BButton("\xe2\xaf\x87", new BMessage(HS_PREVIOUS_PALETTE));
@@ -172,13 +177,14 @@ ColorPaletteWindow::ColorPaletteWindow(BRect frame, int32 mode)
172177
next_set->SetToolTip(B_TRANSLATE_COMMENT("Next color set", "In Color Palette window"));
173178
next_set->SetEnabled(FALSE);
174179

175-
BGridLayout* colorSetGrid = BLayoutBuilder::Grid<>(container_box, 2, B_USE_SMALL_SPACING)
176-
.Add(color_container, 0, 0, 1, 3)
177-
.Add(previous_set, 1, 0)
178-
.Add(next_set, 1, 2)
179-
.SetInsets(3, 3, 8, 3);
180+
colorSetName = new BTextControl("", "color-set-name", new BMessage(HS_SET_NAME_CHANGED));
180181

181-
colorSetGrid->SetMinColumnWidth(0, font.StringWidth("PALETTEPALETTE"));
182+
BGridLayout* colorSetGrid = BLayoutBuilder::Grid<>(container_box, 2, B_USE_SMALL_SPACING)
183+
.Add(previous_set, 0, 0)
184+
.Add(colorSetName, 1, 0)
185+
.Add(next_set, 2, 0)
186+
.Add(color_container, 0, 1, 3, 1)
187+
.SetInsets(3, 3, 3, 3);
182188

183189
BMessage* message = new BMessage(HS_RGB_CONTROL_INVOKED);
184190
message->AddInt32("buttons", 0);
@@ -207,11 +213,11 @@ ColorPaletteWindow::ColorPaletteWindow(BRect frame, int32 mode)
207213
.Add(hsvSlider)
208214
.Add(color_control);
209215

210-
rgbSlider->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDER"), B_SIZE_UNSET));
211-
cmySlider->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDER"), B_SIZE_UNSET));
212-
labSlider->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDER"), B_SIZE_UNSET));
213-
hsvSlider->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDER"), B_SIZE_UNSET));
214-
color_control->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDER"), B_SIZE_UNSET));
216+
rgbSlider->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDERSLI"), B_SIZE_UNSET));
217+
cmySlider->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDERSLI"), B_SIZE_UNSET));
218+
labSlider->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDERSLI"), B_SIZE_UNSET));
219+
hsvSlider->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDERSLI"), B_SIZE_UNSET));
220+
color_control->SetExplicitMinSize(BSize(font.StringWidth("XX12345SLIDERSLIDERSLI"), B_SIZE_UNSET));
215221

216222
colorPreview = new ColorChip("chippy");
217223
colorPreview->SetColor(RGBColorToBGRA(c));
@@ -247,16 +253,15 @@ ColorPaletteWindow::ColorPaletteWindow(BRect frame, int32 mode)
247253
hexColorField->SetTarget(this);
248254

249255
BGridLayout* colorLayout = BLayoutBuilder::Grid<>(5, 3)
250-
.Add(container_box, 0, 0)
251-
.Add(sliderLayout, 1, 0)
252-
.AddGroup(B_VERTICAL, B_USE_SMALL_SPACING, 2, 0)
256+
.Add(sliderLayout, 0, 0)
257+
.AddGroup(B_VERTICAL, B_USE_SMALL_SPACING, 1, 0)
253258
.Add(colorPreview)
254259
.Add(hexColorField)
255260
.End()
256261
.SetInsets(5, 5, 5, 5);
257262

258263
BGroupLayout* mainLayout
259-
= BLayoutBuilder::Group<>(this, B_VERTICAL, 0).Add(menu_bar).Add(colorLayout);
264+
= BLayoutBuilder::Group<>(this, B_VERTICAL, 0).Add(menu_bar).Add(colorLayout).Add(container_box);
260265

261266
// call some function that initializes the views depending on the mode
262267
openControlViews(mode);
@@ -265,6 +270,8 @@ ColorPaletteWindow::ColorPaletteWindow(BRect frame, int32 mode)
265270
if (ColorSet::currentSetIndex() > 0)
266271
previous_set->SetEnabled(TRUE);
267272

273+
colorSetName->SetText(ColorSet::currentSet()->getName());
274+
268275
Show();
269276

270277
if (Lock()) {
@@ -425,6 +432,8 @@ ColorPaletteWindow::MessageReceived(BMessage* message)
425432
}
426433
ColorSet::moveToSet(numSets - 1);
427434
next_set->SetEnabled(FALSE);
435+
436+
colorSetName->SetText(ColorSet::currentSet()->getName());
428437
} break;
429438
// this comes from the menubar->"Set"->"Delete Current Set" and indicates that the current
430439
// set should be deleted
@@ -446,6 +455,9 @@ ColorPaletteWindow::MessageReceived(BMessage* message)
446455
(new BAlert("", B_TRANSLATE("Cannot delete the only color set."),
447456
B_TRANSLATE("OK")))->Go();
448457
}
458+
459+
colorSetName->SetText(ColorSet::currentSet()->getName());
460+
colorSetName->MakeFocus(false);
449461
} break;
450462
// this comes from a button that is named "next set button", the button is in this window
451463
// the message indicates that we should change the colorcontainers to display next color set
@@ -462,6 +474,15 @@ ColorPaletteWindow::MessageReceived(BMessage* message)
462474

463475
if (ColorSet::numberOfSets() > 1)
464476
previous_set->SetEnabled(TRUE);
477+
478+
colorSetName->SetText(ColorSet::currentSet()->getName());
479+
colorSetName->MakeFocus(false);
480+
481+
SelectedColorsView::sendMessageToAll(new BMessage(HS_COLOR_CHANGED));
482+
483+
// give the window the information that selection has changed
484+
rgb_color c = ColorSet::currentSet()->currentColor();
485+
ColorPaletteWindow::ChangePaletteColor(c);
465486
} break;
466487
// this comes from a button that is named "previous set button", the button is in this
467488
// window the message indicates that we should change the colorcontainers to display
@@ -481,6 +502,13 @@ ColorPaletteWindow::MessageReceived(BMessage* message)
481502
next_set->SetEnabled(TRUE);
482503
else
483504
next_set->SetEnabled(FALSE);
505+
colorSetName->SetText(ColorSet::currentSet()->getName());
506+
colorSetName->MakeFocus(false);
507+
SelectedColorsView::sendMessageToAll(new BMessage(HS_COLOR_CHANGED));
508+
509+
// give the window the information that selection has changed
510+
rgb_color c = ColorSet::currentSet()->currentColor();
511+
ColorPaletteWindow::ChangePaletteColor(c);
484512
} break;
485513
// this is sent from ColorContainer::MouseDown and it's purpose is to
486514
// let us change the corresponding color to the color-controller also
@@ -580,6 +608,15 @@ ColorPaletteWindow::MessageReceived(BMessage* message)
580608
{
581609
handlePaletteSave(message);
582610
} break;
611+
case HS_SET_NAME_CHANGED:
612+
{
613+
if (colorSetName->TextLength() > 0)
614+
ColorSet::currentSet()->setName(colorSetName->Text());
615+
else
616+
colorSetName->SetText(ColorSet::currentSet()->getName());
617+
618+
colorSetName->MakeFocus(false);
619+
} break;
583620
// this comes from the menubar->"Mode"->"RGB-Mode" and indicates that
584621
// the color selector should be changed to a RGBControl, this is used
585622
// also for other purposes than just a message-constant
@@ -1172,7 +1209,7 @@ BList* ColorContainer::container_list = new BList();
11721209
ColorContainer::ColorContainer(
11731210
BRect frame, int32 amount_of_colors, uint32 resizingMode, bool highlight, bool add_arrows)
11741211
:
1175-
BView("color container", B_FRAME_EVENTS | B_WILL_DRAW)
1212+
BView("color container", B_FRAME_EVENTS | B_WILL_DRAW | B_NAVIGABLE)
11761213
{
11771214
// here initialize the important variables
11781215
highlight_selected = highlight;
@@ -1218,8 +1255,6 @@ ColorContainer::Draw(BRect drawRect)
12181255
// every instance of this class should also draw whenever
12191256
// a palette entry changes, how should we achieve that ????
12201257

1221-
FillRect(drawRect);
1222-
12231258
BRect rect;
12241259

12251260
for (int32 i = 0; i < color_count; i++) {
@@ -1245,6 +1280,7 @@ ColorContainer::MouseDown(BPoint point)
12451280
uint32 buttons = 0;
12461281
uint32 original_button;
12471282

1283+
MakeFocus(true);
12481284
GetMouse(&point, &buttons, true);
12491285
int32 index, prev_index = ColorSet::currentSet()->currentColorIndex();
12501286

@@ -1483,6 +1519,90 @@ ColorContainer::MessageReceived(BMessage* message)
14831519
}
14841520

14851521

1522+
void
1523+
ColorContainer::KeyDown(const char* bytes, int32 numBytes)
1524+
{
1525+
switch(bytes[0]) {
1526+
case B_PAGE_UP:
1527+
{
1528+
Window()->PostMessage(new BMessage(HS_PREVIOUS_PALETTE));
1529+
} break;
1530+
case B_PAGE_DOWN:
1531+
{
1532+
Window()->PostMessage(new BMessage(HS_NEXT_PALETTE));
1533+
} break;
1534+
case B_LEFT_ARROW:
1535+
{
1536+
int32 colorIdx = ColorSet::currentSet()->currentColorIndex();
1537+
colorIdx = max_c(0, colorIdx - 1);
1538+
ColorSet::currentSet()->setCurrentColorIndex(colorIdx);
1539+
BMessage a_message(HS_PALETTE_SELECTION_CHANGED);
1540+
a_message.AddInt32("index", colorIdx);
1541+
ColorContainer::sendMessageToAllContainers(&a_message);
1542+
SelectedColorsView::sendMessageToAll(new BMessage(HS_COLOR_CHANGED));
1543+
1544+
// give the window the information that selection has changed
1545+
rgb_color c = ColorSet::currentSet()->currentColor();
1546+
ColorPaletteWindow::ChangePaletteColor(c);
1547+
Window()->PostMessage(HS_PALETTE_SELECTION_CHANGED, Window());
1548+
} break;
1549+
case B_RIGHT_ARROW:
1550+
{
1551+
int32 colorIdx = ColorSet::currentSet()->currentColorIndex();
1552+
colorIdx = min_c(ColorSet::currentSet()->sizeOfSet() - 1, colorIdx + 1);
1553+
ColorSet::currentSet()->setCurrentColorIndex(colorIdx);
1554+
BMessage a_message(HS_PALETTE_SELECTION_CHANGED);
1555+
a_message.AddInt32("index", colorIdx);
1556+
ColorContainer::sendMessageToAllContainers(&a_message);
1557+
SelectedColorsView::sendMessageToAll(new BMessage(HS_COLOR_CHANGED));
1558+
1559+
// give the window the information that selection has changed
1560+
rgb_color c = ColorSet::currentSet()->currentColor();
1561+
ColorPaletteWindow::ChangePaletteColor(c);
1562+
Window()->PostMessage(HS_PALETTE_SELECTION_CHANGED, Window());
1563+
} break;
1564+
case B_DOWN_ARROW:
1565+
{
1566+
int32 colorIdx = ColorSet::currentSet()->currentColorIndex();
1567+
colorIdx += column_count;
1568+
if (colorIdx <= ColorSet::currentSet()->sizeOfSet() - 1) {
1569+
ColorSet::currentSet()->setCurrentColorIndex(colorIdx);
1570+
BMessage a_message(HS_PALETTE_SELECTION_CHANGED);
1571+
a_message.AddInt32("index", colorIdx);
1572+
ColorContainer::sendMessageToAllContainers(&a_message);
1573+
1574+
SelectedColorsView::sendMessageToAll(new BMessage(HS_COLOR_CHANGED));
1575+
1576+
// give the window the information that selection has changed
1577+
rgb_color c = ColorSet::currentSet()->currentColor();
1578+
ColorPaletteWindow::ChangePaletteColor(c);
1579+
Window()->PostMessage(HS_PALETTE_SELECTION_CHANGED, Window());
1580+
}
1581+
} break;
1582+
case B_UP_ARROW:
1583+
{
1584+
int32 colorIdx = ColorSet::currentSet()->currentColorIndex();
1585+
colorIdx -= column_count;
1586+
if (colorIdx >= 0) {
1587+
ColorSet::currentSet()->setCurrentColorIndex(colorIdx);
1588+
BMessage a_message(HS_PALETTE_SELECTION_CHANGED);
1589+
a_message.AddInt32("index", colorIdx);
1590+
ColorContainer::sendMessageToAllContainers(&a_message);
1591+
1592+
SelectedColorsView::sendMessageToAll(new BMessage(HS_COLOR_CHANGED));
1593+
1594+
// give the window the information that selection has changed
1595+
rgb_color c = ColorSet::currentSet()->currentColor();
1596+
ColorPaletteWindow::ChangePaletteColor(c);
1597+
Window()->PostMessage(HS_PALETTE_SELECTION_CHANGED, Window());
1598+
}
1599+
} break;
1600+
default:
1601+
BView::KeyDown(bytes, numBytes);
1602+
}
1603+
}
1604+
1605+
14861606
void
14871607
ColorContainer::setUpContainer(BRect frame, int32 number_of_colors, bool add_arrows)
14881608
{
@@ -1696,13 +1816,19 @@ ColorSet::ColorSet(int32 amount_of_colors, ColorSet* copy_this_palette)
16961816
// store the color count
16971817
color_count = amount_of_colors;
16981818

1699-
// create a default name
1700-
strcpy(name, "default palette");
1701-
17021819
// put the current color to 0
17031820
current_color_index = 0;
17041821

17051822
color_set_list->AddItem(this);
1823+
1824+
int32 index = color_set_list->CountItems();
1825+
1826+
if (copy_this_palette == NULL)
1827+
// create a default name
1828+
sprintf(name, "%s %d", B_TRANSLATE("Color set"), index);
1829+
else
1830+
sprintf(name, "%s %s", copy_this_palette->getName(), "copy");
1831+
17061832
}
17071833

17081834

artpaint/controls/ColorPalette.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class ColorPaletteWindow : public BWindow {
9999

100100
ColorChip* colorPreview;
101101
BTextControl* hexColorField;
102+
BTextControl* colorSetName;
102103

103104
void SetHexColor(const rgb_color c);
104105

@@ -189,16 +190,17 @@ class ColorContainer : public BView {
189190
void AttachedToWindow();
190191
void Draw(BRect);
191192
void MouseDown(BPoint point);
192-
void MouseMoved(BPoint,uint32,const BMessage*);
193-
void MessageReceived(BMessage *message);
193+
void MouseMoved(BPoint, uint32, const BMessage*);
194+
void MessageReceived(BMessage* message);
194195
void FrameResized(float width, float height);
196+
void KeyDown(const char* bytes, int32 numBytes);
195197

196198
void colorChanged(int32 color_index);
197199

198200
void SetDraggingEnabled(bool e) { dragging_enabled = e; }
199201

200202
// this function sends a message to all color containers
201-
static void sendMessageToAllContainers(BMessage *message);
203+
static void sendMessageToAllContainers(BMessage* message);
202204
};
203205

204206

0 commit comments

Comments
 (0)