Skip to content

Commit b458c9b

Browse files
committed
Fix HaikuArchives#1. A lot of UI improvements
1 parent 93f0654 commit b458c9b

7 files changed

+158
-73
lines changed

DeskNoteApp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ DeskNoteApp::DeskNoteApp ():BApplication (app_signature)
7878
}
7979

8080
} else {
81-
rect.Set (100,80,240,160); // Inital size for the window.
81+
rect.Set (100,80,340,260); // Inital size for the window.
8282
}
8383

8484
myNote = new DeskNoteWindow (rect);

DeskNoteTextView.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ status_t DeskNoteTextView::Archive (BMessage *data, bool deep) const
3030
void DeskNoteTextView::MouseDown (BPoint point)
3131
{
3232
if (!Window () -> IsActive ()) Window () -> Activate (true);
33+
BPoint mousePoint;
34+
uint32 mouseButtons;
35+
GetMouse (&mousePoint, &mouseButtons, false);
36+
if (mouseButtons != B_SECONDARY_MOUSE_BUTTON)
37+
BTextView::MouseDown (point);
3338
Parent () -> MouseDown (point);
34-
BTextView::MouseDown (point);
3539
}
3640

3741

DeskNoteView.cpp

+50-23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "DeskNoteView.h"
88
#include "FontColourWindow.h"
99
#include <Invoker.h>
10+
#include <LayoutBuilder.h>
1011
#include <Message.h>
1112
#include <SupportKit.h>
1213
#include <OS.h>
@@ -53,9 +54,8 @@ DeskNoteView::DeskNoteView(BRect rect)
5354
textView -> SetText (defaultText, strlen (defaultText));
5455
AddChild (textView);
5556
SetViewColor (B_TRANSPARENT_COLOR);
56-
background.red = background.green = 255;
57-
background.blue = 0;
58-
foreground.red = foreground.green = foreground.blue = 0;
57+
background = kDefaultBackgroundColor;
58+
foreground = kDefaultForegroundColor;
5959
CascadeFontAndColour();
6060
propertiesWindow = NULL;
6161
}
@@ -214,30 +214,57 @@ void DeskNoteView::MouseDown(BPoint point)
214214
if (resizeThread > 0) resume_thread (resizeThread);
215215

216216
} else if (mouseButtons == B_SECONDARY_MOUSE_BUTTON) {
217-
popupMenu = new BPopUpMenu ("Popup Menu!");
218-
popupMenu -> SetTargetForItems ((BHandler *)this);
219-
msg = new BMessage (B_ABOUT_REQUESTED);
220-
menuItem = new BMenuItem ("About Desknotes", msg);
221-
popupMenu -> AddItem (menuItem);
222-
msg = new BMessage (DN_FNT_CLR);
223-
menuItem = new BMenuItem ("Properties", msg);
224-
popupMenu -> AddItem (menuItem);
225-
226-
// If we are replicant add the launch desknotes command to the menu.
227-
if (WeAreAReplicant) {
228-
msg = new BMessage (DN_LAUNCH);
229-
menuItem = new BMenuItem ("Launch DeskNotes", msg);
230-
popupMenu -> AddItem (menuItem);
231-
}
232-
menuItem = popupMenu -> Go (ConvertToScreen (mousePoint), false, false, false);
233-
if (menuItem) {
234-
Window () -> PostMessage (menuItem -> Message(), (BHandler *)this);
235-
}
236-
delete popupMenu;
217+
_ShowContextMenu(mousePoint);
237218
}
238219
}
239220

240221

222+
void DeskNoteView::_ShowContextMenu(BPoint where)
223+
{
224+
bool isRedo;
225+
undo_state state = textView->UndoState(&isRedo);
226+
bool isUndo = state != B_UNDO_UNAVAILABLE && !isRedo;
227+
228+
int32 start;
229+
int32 finish;
230+
textView->GetSelection(&start, &finish);
231+
232+
bool canEdit = textView->IsEditable();
233+
int32 length = textView->TextLength();
234+
235+
BPopUpMenu* menu = new BPopUpMenu(B_EMPTY_STRING, false, false);
236+
237+
BLayoutBuilder::Menu<>(menu)
238+
.AddItem("Undo", B_UNDO/*, 'Z'*/)
239+
.SetEnabled(canEdit && isUndo)
240+
.AddItem("Redo", B_UNDO/*, 'Z', B_SHIFT_KEY*/)
241+
.SetEnabled(canEdit && isRedo)
242+
.AddSeparator()
243+
.AddItem("Cut", B_CUT, 'X')
244+
.SetEnabled(canEdit && start != finish)
245+
.AddItem("Copy", B_COPY, 'C')
246+
.SetEnabled(start != finish)
247+
.AddItem("Paste", B_PASTE, 'V')
248+
.SetEnabled(canEdit && be_clipboard->SystemCount() > 0)
249+
.AddSeparator()
250+
.AddItem("Select All", B_SELECT_ALL, 'A')
251+
.SetEnabled(!(start == 0 && finish == length))
252+
// custom menu
253+
.AddSeparator()
254+
.AddItem("About Desknotes" B_UTF8_ELLIPSIS, new BMessage (B_ABOUT_REQUESTED))
255+
.AddItem("Properties" B_UTF8_ELLIPSIS, new BMessage (DN_FNT_CLR))
256+
;
257+
258+
// If we are replicant add the launch desknotes command to the menu.
259+
if (WeAreAReplicant) {
260+
menu -> AddItem (new BMenuItem ("Launch DeskNotes" , new BMessage (DN_LAUNCH)));
261+
}
262+
263+
menu->SetTargetForItems(textView);
264+
ConvertToScreen(&where);
265+
menu->Go(where, true, true, true);
266+
}
267+
241268
void DeskNoteView::CascadeFontAndColour (void)
242269
{
243270
BFont fnt;

DeskNoteView.h

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class DeskNoteView : public BView
4949
static const char aboutText[];
5050

5151
private:
52+
void _ShowContextMenu(BPoint where);
5253
bool WeAreAReplicant;
5354
DeskNoteTextView *textView;
5455
BRect ourSize;

DeskNoteWindow.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "DeskNoteWindow.h"
88

99
DeskNoteWindow::DeskNoteWindow (BRect rect):BWindow (rect,
10-
"DeskNotes", B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, B_NOT_RESIZABLE)
10+
"DeskNotes", B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, B_AUTO_UPDATE_SIZE_LIMITS)
1111
{
1212
// B_TITLED_WINDOW_LOOK
1313

FontColourWindow.cpp

+88-44
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,105 @@
44
// Date: $Date: 2011/03/20 21:57:39 $
55
#include "FontColourWindow.h"
66
// Bring the maths library in.
7+
78
#include <math.h>
9+
#include <MenuField.h>
10+
#include <GroupLayoutBuilder.h>
11+
#include <LayoutBuilder.h>
812

913
FontColourWindow::FontColourWindow(
1014
BRect rect, BMessenger *msg, BMessage *initial)
1115
:
1216
BWindow (rect, "DeskNotes",
13-
B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, B_NOT_RESIZABLE)
17+
B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_ASYNCHRONOUS_CONTROLS)
1418
{
1519
// B_TITLED_WINDOW_LOOK
16-
BRect viewSize, otherSize;
17-
font_height fntHeight;
18-
int y;
1920
messenger = new BMessenger (*msg);
20-
BMessage *modelMsg;
21-
const char *pointer;
2221
ssize_t dataSize;
2322
const void *dataPointer;
2423

2524
// Take a copy of the inital values to restore to if we cancel.
26-
orginalSettings = new BMessage (*initial);
27-
be_plain_font -> GetHeight (&fntHeight);
28-
y = (int)ceil (fntHeight.ascent + fntHeight.descent + fntHeight.leading) + 2;
29-
viewSize.Set (0, 0 , (rect.right - rect.left), (int)ceil (fntHeight.ascent) + 2);
30-
initial -> FindString ("title", &pointer);
31-
title = new BStringView (viewSize, "The Title", pointer);
32-
title -> SetAlignment (B_ALIGN_CENTER);
33-
AddChild (title);
34-
25+
orginalSettings = new BMessage(*initial);
3526
// Now find out what font we start with, and what colour we use.
3627
initial -> FindData ("background_colour", B_RGB_COLOR_TYPE, &dataPointer, &dataSize);
3728
background = (*(rgb_color *)dataPointer);
29+
originalBackground = background;
3830

3931
initial -> FindData ("foreground_colour", B_RGB_COLOR_TYPE, &dataPointer, &dataSize);
4032
foreground = (*(rgb_color *)dataPointer);
33+
originalForeground = foreground;
4134

4235
initial -> FindData ("font_family_name", B_STRING_TYPE, &dataPointer, &dataSize);
4336
strncpy(fontFamily, (const char*)dataPointer, dataSize);
4437

4538
initial -> FindData ("font_face", B_UINT16_TYPE, &dataPointer, &dataSize);
4639
fontFace = (*(uint16 *)dataPointer);
4740

48-
viewSize.OffsetBy (3, viewSize.bottom + 2);
49-
colourMenu = new BMenuBar (viewSize, "Colour Menu", 0);
5041
colourPopupMenu = new BPopUpMenu ("Background Colour");
51-
modelMsg = new BMessage (DN_COLOUR_MENU);
52-
backgroundColourItem = new BMenuItem ("Background Colour", modelMsg);
53-
modelMsg = new BMessage (DN_COLOUR_MENU);
54-
foregroundColourItem = new BMenuItem ("Foreground Colour", modelMsg);
55-
currentSelection = backgroundColourItem;
42+
backgroundColourItem = new BMenuItem ("Background Colour", new BMessage (DN_COLOUR_MENU));
43+
foregroundColourItem = new BMenuItem ("Foreground Colour", new BMessage (DN_COLOUR_MENU));
44+
5645
colourPopupMenu -> AddItem (backgroundColourItem);
5746
colourPopupMenu -> AddItem (foregroundColourItem);
47+
backgroundColourItem -> SetMarked(true);
48+
49+
colourMenu = new BMenuField(rect, "", "", colourPopupMenu);
50+
51+
colourMenu->SetDivider(0);
5852
colourPopupMenu -> SetTargetForItems (this);
59-
colourMenu -> AddItem (colourPopupMenu);
60-
AddChild (colourMenu);
61-
otherSize = colourMenu -> Bounds();
62-
viewSize.OffsetBy (0, otherSize.bottom - otherSize.top + 2);
63-
modelMsg = new BMessage (DN_COLOUR_CHANGE);
64-
colourControl = new BColorControl (viewSize.LeftTop (),
65-
B_CELLS_32x8, 2, "Colour Selector", modelMsg);
66-
AddChild (colourControl);
53+
colourControl = new BColorControl (B_ORIGIN,
54+
B_CELLS_32x8, 8.0, "Colour Selector", new BMessage (DN_COLOUR_CHANGE));
6755
colourControl -> SetValue (background);
6856

57+
defaultsButton = new BButton ("Defaults", "Defaults", new BMessage (DN_PROPERTIES_DEFAULTS));
6958
// Add the revert button, taking away the border added earlier.
70-
viewSize.OffsetTo (-3, colourControl -> Frame().bottom + 2);
71-
viewSize.left += 70;
72-
viewSize.right -= 70;
73-
modelMsg = new BMessage (DN_PROPERTIES_REVERT);
74-
revertButton = new BButton (viewSize, "Revert", "Revert", modelMsg);
75-
AddChild (revertButton);
59+
revertButton = new BButton ("Revert", "Revert", new BMessage (DN_PROPERTIES_REVERT));
60+
61+
SetLayout(new BGroupLayout(B_VERTICAL));
62+
63+
AddChild(BLayoutBuilder::Group<>(B_VERTICAL)
64+
.Add(colourMenu)
65+
.Add(colourControl)
66+
.AddGroup(B_HORIZONTAL)
67+
.Add(defaultsButton)
68+
.Add(revertButton)
69+
.AddGlue()
70+
.End()
71+
.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
72+
B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
73+
);
74+
_CheckButtons();
75+
}
76+
77+
78+
bool FontColourWindow::IsDefaultsColor ()
79+
{
80+
return background == kDefaultBackgroundColor
81+
&& foreground == kDefaultForegroundColor;
82+
}
83+
84+
85+
bool FontColourWindow::IsRevertableColor ()
86+
{
87+
return background != originalBackground
88+
|| foreground != originalForeground;
89+
90+
}
91+
92+
93+
void FontColourWindow::_CheckButtons()
94+
{
95+
defaultsButton->SetEnabled(!IsDefaultsColor());
96+
revertButton->SetEnabled(IsRevertableColor());
97+
}
98+
99+
100+
void FontColourWindow::_UpdateColorControl()
101+
{
102+
if (colourPopupMenu->FindMarked() == backgroundColourItem)
103+
colourControl -> SetValue (background);
104+
if (colourPopupMenu->FindMarked() == foregroundColourItem)
105+
colourControl -> SetValue (foreground);
76106
}
77107

78108

@@ -82,34 +112,48 @@ void FontColourWindow::MessageReceived (BMessage *msg)
82112
BMessage *newMsg;
83113
switch (msg -> what) {
84114
case DN_COLOUR_MENU:
85-
msg -> FindPointer ("source", &pointer);
86-
if ((BMenuItem *) pointer != currentSelection) {
87-
currentSelection = (BMenuItem *) pointer;
88-
if (currentSelection == backgroundColourItem)
115+
if (colourPopupMenu->FindMarked() == backgroundColourItem)
89116
colourControl -> SetValue (background);
90-
if (currentSelection == foregroundColourItem)
117+
if (colourPopupMenu->FindMarked() == foregroundColourItem)
91118
colourControl -> SetValue (foreground);
92-
}
93119
break;
94120

95121
case DN_COLOUR_CHANGE:
96122
newMsg = new BMessage (orginalSettings->what);
97-
if (currentSelection == backgroundColourItem)
123+
if (colourPopupMenu->FindMarked() == backgroundColourItem)
98124
background = colourControl -> ValueAsColor();
99-
if (currentSelection == foregroundColourItem)
125+
if (colourPopupMenu->FindMarked() == foregroundColourItem)
100126
foreground = colourControl -> ValueAsColor();
101127
newMsg -> AddData ("background_colour",
102128
B_RGB_COLOR_TYPE, &background, sizeof (rgb_color));
103129
newMsg -> AddData ("foreground_colour",
104130
B_RGB_COLOR_TYPE, &foreground, sizeof (rgb_color));
105131
messenger -> SendMessage (newMsg);
106132
delete newMsg;
133+
_CheckButtons();
107134
break;
108135

109136
case DN_PROPERTIES_REVERT:
110137
// Restore the original settings.
111138
messenger -> SendMessage (orginalSettings);
139+
background = originalBackground;
140+
foreground = originalForeground;
141+
_UpdateColorControl();
142+
_CheckButtons();
143+
break;
144+
145+
case DN_PROPERTIES_DEFAULTS: {
146+
// Restore the default settings.
147+
BMessage defaultSettings(*orginalSettings);
148+
background = kDefaultBackgroundColor;
149+
foreground = kDefaultForegroundColor;
150+
defaultSettings.ReplaceData ("background_colour", B_RGB_COLOR_TYPE, &background, sizeof (rgb_color));
151+
defaultSettings.ReplaceData ("foreground_colour", B_RGB_COLOR_TYPE, &foreground, sizeof (rgb_color));
152+
messenger -> SendMessage (&defaultSettings);
153+
_UpdateColorControl();
154+
_CheckButtons();
112155
break;
156+
}
113157

114158
case DN_PROPERTIES_URGENT_CLOSE:
115159
// We need to close quickly - no messages should be sent back.

FontColourWindow.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
#define DN_COLOUR_MENU 'dnCM'
1111
#define DN_COLOUR_CHANGE 'dnCH'
1212
#define DN_PROPERTIES_REVERT 'dnRT'
13+
#define DN_PROPERTIES_DEFAULTS 'dnDF'
1314
#define DN_PROPERTIES_CLOSE 'dnCL'
1415
#define DN_PROPERTIES_URGENT_CLOSE 'dnUC'
1516

17+
const rgb_color kDefaultBackgroundColor = { 255, 255, 0, 255 };
18+
const rgb_color kDefaultForegroundColor = { 0, 0, 0, 255 };
19+
1620

1721
class FontColourWindow : public BWindow
1822
{
@@ -22,17 +26,22 @@ class FontColourWindow : public BWindow
2226
~FontColourWindow ();
2327

2428
bool QuitRequested ();
29+
bool IsDefaultsColor ();
30+
bool IsRevertableColor ();
31+
2532

2633
static void CalculateWindowFrame (BRect *windowFrame, BRect parentFrame);
2734

2835
private:
36+
void _CheckButtons();
37+
void _UpdateColorControl();
2938
BMessenger *messenger;
3039
BMessage *orginalSettings;
3140
int fontSize;
3241
font_family fontFamily;
3342
uint16 fontFace;
3443
rgb_color background, foreground;
35-
44+
rgb_color originalBackground, originalForeground;
3645
// The interface components go here.
3746
// The Font family
3847
BMenuBar *fontFamilyMenu;
@@ -41,17 +50,17 @@ class FontColourWindow : public BWindow
4150
// The font size.
4251
BMenuBar *fontSizeMenu;
4352
// Whether to change the foreground or background colours.
44-
BMenuBar *colourMenu;
53+
BMenuField *colourMenu;
4554
BPopUpMenu *colourPopupMenu;
4655
BMenuItem *backgroundColourItem;
4756
BMenuItem *foregroundColourItem;
48-
BMenuItem *currentSelection;
4957
BColorControl *colourControl;
5058

5159
BStringView *title;
5260
BTextView *exampleText;
5361

5462
BButton *revertButton;
63+
BButton *defaultsButton;
5564
};
5665

5766
#endif

0 commit comments

Comments
 (0)