4
4
// Date: $Date: 2011/03/20 21:57:39 $
5
5
#include " FontColourWindow.h"
6
6
// Bring the maths library in.
7
+
7
8
#include < math.h>
9
+ #include < MenuField.h>
10
+ #include < GroupLayoutBuilder.h>
11
+ #include < LayoutBuilder.h>
8
12
9
13
FontColourWindow::FontColourWindow (
10
14
BRect rect, BMessenger *msg, BMessage *initial)
11
15
:
12
16
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 )
14
18
{
15
19
// B_TITLED_WINDOW_LOOK
16
- BRect viewSize, otherSize;
17
- font_height fntHeight;
18
- int y;
19
20
messenger = new BMessenger (*msg);
20
- BMessage *modelMsg;
21
- const char *pointer;
22
21
ssize_t dataSize;
23
22
const void *dataPointer;
24
23
25
24
// 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);
35
26
// Now find out what font we start with, and what colour we use.
36
27
initial -> FindData (" background_colour" , B_RGB_COLOR_TYPE, &dataPointer, &dataSize);
37
28
background = (*(rgb_color *)dataPointer);
29
+ originalBackground = background;
38
30
39
31
initial -> FindData (" foreground_colour" , B_RGB_COLOR_TYPE, &dataPointer, &dataSize);
40
32
foreground = (*(rgb_color *)dataPointer);
33
+ originalForeground = foreground;
41
34
42
35
initial -> FindData (" font_family_name" , B_STRING_TYPE, &dataPointer, &dataSize);
43
36
strncpy (fontFamily, (const char *)dataPointer, dataSize);
44
37
45
38
initial -> FindData (" font_face" , B_UINT16_TYPE, &dataPointer, &dataSize);
46
39
fontFace = (*(uint16 *)dataPointer);
47
40
48
- viewSize.OffsetBy (3 , viewSize.bottom + 2 );
49
- colourMenu = new BMenuBar (viewSize, " Colour Menu" , 0 );
50
41
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
+
56
45
colourPopupMenu -> AddItem (backgroundColourItem);
57
46
colourPopupMenu -> AddItem (foregroundColourItem);
47
+ backgroundColourItem -> SetMarked (true );
48
+
49
+ colourMenu = new BMenuField (rect, " " , " " , colourPopupMenu);
50
+
51
+ colourMenu->SetDivider (0 );
58
52
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));
67
55
colourControl -> SetValue (background);
68
56
57
+ defaultsButton = new BButton (" Defaults" , " Defaults" , new BMessage (DN_PROPERTIES_DEFAULTS));
69
58
// 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);
76
106
}
77
107
78
108
@@ -82,34 +112,48 @@ void FontColourWindow::MessageReceived (BMessage *msg)
82
112
BMessage *newMsg;
83
113
switch (msg -> what) {
84
114
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)
89
116
colourControl -> SetValue (background);
90
- if (currentSelection == foregroundColourItem)
117
+ if (colourPopupMenu-> FindMarked () == foregroundColourItem)
91
118
colourControl -> SetValue (foreground);
92
- }
93
119
break ;
94
120
95
121
case DN_COLOUR_CHANGE:
96
122
newMsg = new BMessage (orginalSettings->what );
97
- if (currentSelection == backgroundColourItem)
123
+ if (colourPopupMenu-> FindMarked () == backgroundColourItem)
98
124
background = colourControl -> ValueAsColor ();
99
- if (currentSelection == foregroundColourItem)
125
+ if (colourPopupMenu-> FindMarked () == foregroundColourItem)
100
126
foreground = colourControl -> ValueAsColor ();
101
127
newMsg -> AddData (" background_colour" ,
102
128
B_RGB_COLOR_TYPE, &background, sizeof (rgb_color));
103
129
newMsg -> AddData (" foreground_colour" ,
104
130
B_RGB_COLOR_TYPE, &foreground, sizeof (rgb_color));
105
131
messenger -> SendMessage (newMsg);
106
132
delete newMsg;
133
+ _CheckButtons ();
107
134
break ;
108
135
109
136
case DN_PROPERTIES_REVERT:
110
137
// Restore the original settings.
111
138
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 ();
112
155
break ;
156
+ }
113
157
114
158
case DN_PROPERTIES_URGENT_CLOSE:
115
159
// We need to close quickly - no messages should be sent back.
0 commit comments