-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
ExpandoMenuBar.cpp
959 lines (790 loc) · 23.2 KB
/
ExpandoMenuBar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
/*
Open Tracker License
Terms and Conditions
Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice applies to all licensees
and shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Be Incorporated shall not be
used in advertising or otherwise to promote the sale, use or other dealings in
this Software without prior written authorization from Be Incorporated.
Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered
trademarks of Be Incorporated in the United States and other countries. Other
brand product names are registered trademarks or trademarks of their respective
holders.
All rights reserved.
*/
#include "ExpandoMenuBar.h"
#include <string.h>
#include <Autolock.h>
#include <Bitmap.h>
#include <ControlLook.h>
#include <Debug.h>
#include <NodeInfo.h>
#include <Roster.h>
#include <Screen.h>
#include "icons.h"
#include "BarApp.h"
#include "BarMenuTitle.h"
#include "BarView.h"
#include "BarWindow.h"
#include "DeskbarMenu.h"
#include "DeskbarUtils.h"
#include "InlineScrollView.h"
#include "ResourceSet.h"
#include "ShowHideMenuItem.h"
#include "StatusView.h"
#include "TeamMenuItem.h"
#include "WindowMenu.h"
#include "WindowMenuItem.h"
const float kMinMenuItemWidth = 50.0f;
const float kSepItemWidth = 5.0f;
const float kIconPadding = 8.0f;
const uint32 kMinimizeTeam = 'mntm';
const uint32 kBringTeamToFront = 'bftm';
bool TExpandoMenuBar::sDoMonitor = false;
thread_id TExpandoMenuBar::sMonThread = B_ERROR;
BLocker TExpandoMenuBar::sMonLocker("expando monitor");
TExpandoMenuBar::TExpandoMenuBar(TBarView* bar, BRect frame, const char* name,
bool vertical, bool drawLabel)
:
BMenuBar(frame, name, B_FOLLOW_NONE,
vertical ? B_ITEMS_IN_COLUMN : B_ITEMS_IN_ROW),
fVertical(vertical),
fOverflow(false),
fDrawLabel(drawLabel),
fShowTeamExpander(static_cast<TBarApp*>(be_app)->Settings()->superExpando),
fExpandNewTeams(static_cast<TBarApp*>(be_app)->Settings()->expandNewTeams),
fDeskbarMenuWidth(kMinMenuItemWidth),
fBarView(bar),
fPreviousDragTargetItem(NULL),
fLastClickItem(NULL)
{
SetItemMargins(0.0f, 0.0f, 0.0f, 0.0f);
SetFont(be_plain_font);
if (fVertical)
SetMaxContentWidth(sMinimumWindowWidth);
else {
// Make more room for the icon in horizontal mode
int32 iconSize = static_cast<TBarApp*>(be_app)->IconSize();
float maxContentWidth = sMinimumWindowWidth + iconSize
- kMinimumIconSize;
SetMaxContentWidth(maxContentWidth);
}
}
int
TExpandoMenuBar::CompareByName(const void* first, const void* second)
{
return strcasecmp((*(static_cast<BarTeamInfo* const*>(first)))->name,
(*(static_cast<BarTeamInfo* const*>(second)))->name);
}
void
TExpandoMenuBar::AttachedToWindow()
{
BMessenger self(this);
BList teamList;
TBarApp::Subscribe(self, &teamList);
int32 iconSize = static_cast<TBarApp*>(be_app)->IconSize();
desk_settings* settings = static_cast<TBarApp*>(be_app)->Settings();
float itemWidth = -0.1f;
if (fVertical)
itemWidth = Frame().Width();
else {
itemWidth = iconSize;
if (fDrawLabel)
itemWidth += sMinimumWindowWidth - kMinimumIconSize;
else
itemWidth += kIconPadding * 2;
}
float itemHeight = -1.0f;
// top or bottom mode, add deskbar menu and sep for menubar tracking
// consistency
if (!fVertical) {
const BBitmap* logoBitmap = AppResSet()->FindBitmap(B_MESSAGE_TYPE,
R_LeafLogoBitmap);
if (logoBitmap != NULL)
fDeskbarMenuWidth = logoBitmap->Bounds().Width() + 16;
}
if (settings->sortRunningApps)
teamList.SortItems(CompareByName);
int32 count = teamList.CountItems();
for (int32 i = 0; i < count; i++) {
BarTeamInfo* barInfo = (BarTeamInfo*)teamList.ItemAt(i);
if ((barInfo->flags & B_BACKGROUND_APP) == 0
&& strcasecmp(barInfo->sig, kDeskbarSignature) != 0) {
if (settings->trackerAlwaysFirst
&& !strcmp(barInfo->sig, kTrackerSignature)) {
AddItem(new TTeamMenuItem(barInfo->teams, barInfo->icon,
barInfo->name, barInfo->sig, itemWidth, itemHeight,
fDrawLabel, fVertical), 0);
} else {
AddItem(new TTeamMenuItem(barInfo->teams, barInfo->icon,
barInfo->name, barInfo->sig, itemWidth, itemHeight,
fDrawLabel, fVertical));
}
barInfo->teams = NULL;
barInfo->icon = NULL;
barInfo->name = NULL;
barInfo->sig = NULL;
}
delete barInfo;
}
BMenuBar::AttachedToWindow();
if (CountItems() == 0) {
// If we're empty, BMenuBar::AttachedToWindow() resizes us to some
// weird value - we just override it again
ResizeTo(itemWidth, 0);
}
if (fVertical) {
sDoMonitor = true;
sMonThread = spawn_thread(monitor_team_windows,
"Expando Window Watcher", B_LOW_PRIORITY, this);
resume_thread(sMonThread);
}
}
void
TExpandoMenuBar::DetachedFromWindow()
{
BMenuBar::DetachedFromWindow();
if (sMonThread != B_ERROR) {
sDoMonitor = false;
status_t returnCode;
wait_for_thread(sMonThread, &returnCode);
sMonThread = B_ERROR;
}
BMessenger self(this);
BMessage message(kUnsubscribe);
message.AddMessenger("messenger", self);
be_app->PostMessage(&message);
RemoveItems(0, CountItems(), true);
}
void
TExpandoMenuBar::MessageReceived(BMessage* message)
{
int32 index;
TTeamMenuItem* item;
switch (message->what) {
case B_SOME_APP_LAUNCHED:
{
BList* teams = NULL;
message->FindPointer("teams", (void**)&teams);
BBitmap* icon = NULL;
message->FindPointer("icon", (void**)&icon);
const char* signature;
if (message->FindString("sig", &signature) == B_OK
&&strcasecmp(signature, kDeskbarSignature) == 0) {
delete teams;
delete icon;
break;
}
uint32 flags;
if (message->FindInt32("flags", ((int32*) &flags)) == B_OK
&& (flags & B_BACKGROUND_APP) != 0) {
delete teams;
delete icon;
break;
}
const char* name = NULL;
message->FindString("name", &name);
AddTeam(teams, icon, strdup(name), strdup(signature));
break;
}
case B_MOUSE_WHEEL_CHANGED:
{
float deltaY = 0;
message->FindFloat("be:wheel_delta_y", &deltaY);
if (deltaY == 0)
return;
TInlineScrollView* scrollView
= dynamic_cast<TInlineScrollView*>(Parent());
if (scrollView == NULL)
return;
float largeStep;
float smallStep;
scrollView->GetSteps(&smallStep, &largeStep);
// pressing the option/command/control key scrolls faster
if (modifiers() & (B_OPTION_KEY | B_COMMAND_KEY | B_CONTROL_KEY))
deltaY *= largeStep;
else
deltaY *= smallStep;
scrollView->ScrollBy(deltaY);
break;
}
case kAddTeam:
AddTeam(message->FindInt32("team"), message->FindString("sig"));
break;
case kRemoveTeam:
{
team_id team = -1;
message->FindInt32("team", &team);
RemoveTeam(team, true);
break;
}
case B_SOME_APP_QUIT:
{
team_id team = -1;
message->FindInt32("team", &team);
RemoveTeam(team, false);
break;
}
case kMinimizeTeam:
{
index = message->FindInt32("itemIndex");
item = dynamic_cast<TTeamMenuItem*>(ItemAt(index));
if (item == NULL)
break;
TShowHideMenuItem::TeamShowHideCommon(B_MINIMIZE_WINDOW,
item->Teams(),
item->Menu()->ConvertToScreen(item->Frame()),
true);
break;
}
case kBringTeamToFront:
{
index = message->FindInt32("itemIndex");
item = dynamic_cast<TTeamMenuItem*>(ItemAt(index));
if (item == NULL)
break;
TShowHideMenuItem::TeamShowHideCommon(B_BRING_TO_FRONT,
item->Teams(), item->Menu()->ConvertToScreen(item->Frame()),
true);
break;
}
default:
BMenuBar::MessageReceived(message);
break;
}
}
void
TExpandoMenuBar::MouseDown(BPoint where)
{
BMessage* message = Window()->CurrentMessage();
BMenuItem* menuItem;
TTeamMenuItem* item = TeamItemAtPoint(where, &menuItem);
if (message == NULL || item == NULL || fBarView->Dragging()) {
BMenuBar::MouseDown(where);
return;
}
int32 modifiers = 0;
message->FindInt32("modifiers", &modifiers);
// check for three finger salute, a.k.a. Vulcan Death Grip
if ((modifiers & B_COMMAND_KEY) != 0
&& (modifiers & B_CONTROL_KEY) != 0
&& (modifiers & B_SHIFT_KEY) != 0) {
const BList* teams = item->Teams();
int32 teamCount = teams->CountItems();
team_id teamID;
for (int32 team = 0; team < teamCount; team++) {
teamID = (addr_t)teams->ItemAt(team);
kill_team(teamID);
RemoveTeam(teamID, false);
// remove the team from display immediately
}
return;
// absorb the message
}
// control click - show all/hide all shortcut
if ((modifiers & B_CONTROL_KEY) != 0) {
// show/hide item's teams
BMessage showMessage((modifiers & B_SHIFT_KEY) != 0
? kMinimizeTeam : kBringTeamToFront);
showMessage.AddInt32("itemIndex", IndexOf(item));
Window()->PostMessage(&showMessage, this);
return;
// absorb the message
}
// Check the bounds of the expand Team icon
if (fVertical && fShowTeamExpander) {
if (item->ExpanderBounds().Contains(where)) {
BAutolock locker(sMonLocker);
// let the update thread wait...
item->ToggleExpandState(true);
// toggle the item
item->Draw();
return;
// absorb the message
}
}
// double-click on an item brings the team to front
int32 clicks;
if (message->FindInt32("clicks", &clicks) == B_OK && clicks > 1
&& item == menuItem && item == fLastClickItem) {
be_roster->ActivateApp((addr_t)item->Teams()->ItemAt(0));
// activate this team
return;
// absorb the message
}
fLastClickItem = item;
BMenuBar::MouseDown(where);
}
void
TExpandoMenuBar::MouseMoved(BPoint where, uint32 code, const BMessage* message)
{
if (message == NULL) {
// force a cleanup
_FinishedDrag();
switch (code) {
case B_ENTERED_VIEW:
case B_INSIDE_VIEW:
{
TTeamMenuItem* item = TeamItemAtPoint(where);
if (item == fLastMousedOverItem) {
// already set the tooltip for this item, break out
break;
}
if (item == NULL) {
// item is NULL, remove the tooltip and break out
fLastMousedOverItem = NULL;
SetToolTip((const char*)NULL);
break;
}
if (item->HasLabel()) {
// item has a visible label, remove the tooltip and break out
fLastMousedOverItem = item;
SetToolTip((const char*)NULL);
break;
}
// new item, set the tooltip to the item name
SetToolTip(item->Name());
// save the current item for the next MouseMoved() call
fLastMousedOverItem = item;
break;
}
}
BMenuBar::MouseMoved(where, code, message);
return;
}
uint32 buttons;
if (Window()->CurrentMessage() == NULL
|| Window()->CurrentMessage()->FindInt32("buttons", (int32*)&buttons)
< B_OK) {
buttons = 0;
}
if (buttons == 0)
return;
switch (code) {
case B_ENTERED_VIEW:
// fPreviousDragTargetItem should always be NULL here anyways.
if (fPreviousDragTargetItem)
_FinishedDrag();
fBarView->CacheDragData(message);
fPreviousDragTargetItem = NULL;
break;
case B_OUTSIDE_VIEW:
// NOTE: Should not be here, but for the sake of defensive
// programming...
case B_EXITED_VIEW:
_FinishedDrag();
break;
case B_INSIDE_VIEW:
if (fBarView->Dragging()) {
TTeamMenuItem* item = NULL;
int32 itemCount = CountItems();
for (int32 i = 0; i < itemCount; i++) {
BMenuItem* _item = ItemAt(i);
if (_item->Frame().Contains(where)) {
item = dynamic_cast<TTeamMenuItem*>(_item);
break;
}
}
if (item == fPreviousDragTargetItem)
break;
if (fPreviousDragTargetItem != NULL)
fPreviousDragTargetItem->SetOverrideSelected(false);
if (item != NULL)
item->SetOverrideSelected(true);
fPreviousDragTargetItem = item;
}
break;
}
}
void
TExpandoMenuBar::MouseUp(BPoint where)
{
if (!fBarView->Dragging()) {
BMenuBar::MouseUp(where);
return;
}
_FinishedDrag(true);
}
bool
TExpandoMenuBar::InDeskbarMenu(BPoint loc) const
{
TBarWindow* window = dynamic_cast<TBarWindow*>(Window());
if (window) {
if (TDeskbarMenu* bemenu = window->DeskbarMenu()) {
bool inDeskbarMenu = false;
if (bemenu->LockLooper()) {
inDeskbarMenu = bemenu->Frame().Contains(loc);
bemenu->UnlockLooper();
}
return inDeskbarMenu;
}
}
return false;
}
/*! Returns the team menu item that belongs to the item under the
specified \a point.
If \a _item is given, it will return the exact menu item under
that point (which might be a window item when the expander is on).
*/
TTeamMenuItem*
TExpandoMenuBar::TeamItemAtPoint(BPoint point, BMenuItem** _item)
{
TTeamMenuItem* lastApp = NULL;
int32 count = CountItems();
for (int32 i = 0; i < count; i++) {
BMenuItem* item = ItemAt(i);
if (dynamic_cast<TTeamMenuItem*>(item) != NULL)
lastApp = (TTeamMenuItem*)item;
if (item && item->Frame().Contains(point)) {
if (_item != NULL)
*_item = item;
return lastApp;
}
}
// no item found
if (_item != NULL)
*_item = NULL;
return NULL;
}
void
TExpandoMenuBar::AddTeam(BList* team, BBitmap* icon, char* name,
char* signature)
{
desk_settings* settings = static_cast<TBarApp*>(be_app)->Settings();
int32 iconSize = static_cast<TBarApp*>(be_app)->IconSize();
float itemWidth = -1.0f;
if (fVertical)
itemWidth = fBarView->Bounds().Width();
else {
itemWidth = iconSize;
if (fDrawLabel)
itemWidth += sMinimumWindowWidth - kMinimumIconSize;
else
itemWidth += kIconPadding * 2;
}
float itemHeight = -1.0f;
TTeamMenuItem* item = new TTeamMenuItem(team, icon, name, signature,
itemWidth, itemHeight, fDrawLabel, fVertical);
if (settings->trackerAlwaysFirst && !strcmp(signature, kTrackerSignature))
AddItem(item, 0);
else if (settings->sortRunningApps) {
TTeamMenuItem* teamItem
= dynamic_cast<TTeamMenuItem*>(ItemAt(0));
int32 firstApp = 0;
// if Tracker should always be the first item, we need to skip it
// when sorting in the current item
if (settings->trackerAlwaysFirst && teamItem != NULL
&& !strcmp(teamItem->Signature(), kTrackerSignature)) {
firstApp++;
}
int32 i = firstApp;
int32 itemCount = CountItems();
while (i < itemCount) {
teamItem = dynamic_cast<TTeamMenuItem*>(ItemAt(i));
if (teamItem != NULL && strcasecmp(teamItem->Name(), name) > 0) {
AddItem(item, i);
break;
}
i++;
}
// was the item added to the list yet?
if (i == itemCount)
AddItem(item);
} else
AddItem(item);
if (fVertical) {
if (item && fShowTeamExpander && fExpandNewTeams)
item->ToggleExpandState(false);
}
SizeWindow(1);
Window()->UpdateIfNeeded();
}
void
TExpandoMenuBar::AddTeam(team_id team, const char* signature)
{
int32 count = CountItems();
for (int32 i = 0; i < count; i++) {
// Only add to team menu items
if (TTeamMenuItem* item = dynamic_cast<TTeamMenuItem*>(ItemAt(i))) {
if (strcasecmp(item->Signature(), signature) == 0) {
if (!(item->Teams()->HasItem((void*)(addr_t)team)))
item->Teams()->AddItem((void*)(addr_t)team);
break;
}
}
}
}
void
TExpandoMenuBar::RemoveTeam(team_id team, bool partial)
{
int32 count = CountItems();
for (int32 i = 0; i < count; i++) {
if (TTeamMenuItem* item = dynamic_cast<TTeamMenuItem*>(ItemAt(i))) {
if (item->Teams()->HasItem((void*)(addr_t)team)) {
item->Teams()->RemoveItem(team);
if (partial)
return;
#ifdef DOUBLECLICKBRINGSTOFRONT
if (fLastClickItem == i)
fLastClickItem = -1;
#endif
RemoveItem(i);
SizeWindow(-1);
Window()->UpdateIfNeeded();
delete item;
return;
}
}
}
}
void
TExpandoMenuBar::CheckItemSizes(int32 delta)
{
if (fBarView->Vertical())
return;
float maxWidth = fBarView->DragRegion()->Frame().left
- fDeskbarMenuWidth - kSepItemWidth;
int32 iconSize = static_cast<TBarApp*>(be_app)->IconSize();
float iconOnlyWidth = kIconPadding + iconSize + kIconPadding;
float minItemWidth = fDrawLabel ? iconOnlyWidth + kMinMenuItemWidth
: iconOnlyWidth - kIconPadding;
float maxItemWidth = fDrawLabel ? sMinimumWindowWidth + iconSize
- kMinimumIconSize : iconOnlyWidth;
float menuWidth = maxItemWidth * CountItems() + fDeskbarMenuWidth
+ kSepItemWidth;
bool reset = false;
float newWidth = 0.0f;
if (delta >= 0 && menuWidth > maxWidth) {
fOverflow = true;
reset = true;
newWidth = floorf(maxWidth / CountItems());
} else if (delta < 0 && fOverflow) {
reset = true;
if (menuWidth > maxWidth)
newWidth = floorf(maxWidth / CountItems());
else
newWidth = maxItemWidth;
}
if (newWidth > maxItemWidth)
newWidth = maxItemWidth;
else if (newWidth < minItemWidth)
newWidth = minItemWidth;
if (reset) {
SetMaxContentWidth(newWidth);
if (newWidth == maxItemWidth)
fOverflow = false;
InvalidateLayout();
for (int32 index = 0; ; index++) {
TTeamMenuItem* item = (TTeamMenuItem*)ItemAt(index);
if (item == NULL)
break;
item->SetOverrideWidth(newWidth);
}
Invalidate();
Window()->UpdateIfNeeded();
}
fBarView->CheckForScrolling();
}
menu_layout
TExpandoMenuBar::MenuLayout() const
{
return Layout();
}
void
TExpandoMenuBar::Draw(BRect updateRect)
{
BMenu::Draw(updateRect);
}
void
TExpandoMenuBar::DrawBackground(BRect updateRect)
{
if (fVertical)
return;
BRect bounds(Bounds());
rgb_color menuColor = LowColor();
rgb_color hilite = tint_color(menuColor, B_DARKEN_1_TINT);
rgb_color vlight = tint_color(menuColor, B_LIGHTEN_2_TINT);
int32 count = CountItems() - 1;
if (count >= 0)
bounds.left = ItemAt(count)->Frame().right + 1;
else
bounds.left = 0;
if (be_control_look != NULL) {
SetHighColor(tint_color(menuColor, 1.22));
StrokeLine(bounds.LeftTop(), bounds.LeftBottom());
bounds.left++;
uint32 borders = BControlLook::B_TOP_BORDER
| BControlLook::B_BOTTOM_BORDER | BControlLook::B_RIGHT_BORDER;
be_control_look->DrawButtonBackground(this, bounds, bounds, menuColor,
0, borders);
} else {
SetHighColor(vlight);
StrokeLine(bounds.LeftTop(), bounds.RightTop());
StrokeLine(BPoint(bounds.left, bounds.top + 1), bounds.LeftBottom());
SetHighColor(hilite);
StrokeLine(BPoint(bounds.left + 1, bounds.bottom),
bounds.RightBottom());
}
}
/*! Something to help determine if we are showing too many apps
need to add in scrolling functionality.
*/
bool
TExpandoMenuBar::CheckForSizeOverrun()
{
if (fVertical) {
BRect screenFrame = (BScreen(Window())).Frame();
return Window()->Frame().bottom > screenFrame.bottom;
}
// horizontal
int32 count = CountItems() - 1;
if (count < 0)
return false;
int32 iconSize = static_cast<TBarApp*>(be_app)->IconSize();
float iconOnlyWidth = kIconPadding + iconSize + kIconPadding;
float minItemWidth = fDrawLabel ? iconOnlyWidth + kMinMenuItemWidth
: iconOnlyWidth - kIconPadding;
float menuWidth = minItemWidth * CountItems() + fDeskbarMenuWidth
+ kSepItemWidth;
float maxWidth = fBarView->DragRegion()->Frame().left
- fDeskbarMenuWidth - kSepItemWidth;
return menuWidth > maxWidth;
}
void
TExpandoMenuBar::SizeWindow(int32 delta)
{
// instead of resizing the window here and there in the
// code the resize method will be centered in one place
// thus, the same behavior (good or bad) will be used
// wherever window sizing is done
if (fVertical) {
BRect screenFrame = (BScreen(Window())).Frame();
fBarView->SizeWindow(screenFrame);
fBarView->PositionWindow(screenFrame);
fBarView->CheckForScrolling();
} else
CheckItemSizes(delta);
}
int32
TExpandoMenuBar::monitor_team_windows(void* arg)
{
TExpandoMenuBar* teamMenu = (TExpandoMenuBar*)arg;
while (teamMenu->sDoMonitor) {
sMonLocker.Lock();
if (teamMenu->Window()->LockWithTimeout(50000) == B_OK) {
int32 totalItems = teamMenu->CountItems();
// Set all WindowMenuItems to require an update.
TWindowMenuItem* item = NULL;
for (int32 i = 0; i < totalItems; i++) {
if (!teamMenu->SubmenuAt(i)) {
item = static_cast<TWindowMenuItem*>(teamMenu->ItemAt(i));
item->SetRequireUpdate();
}
}
// Perform SetTo() on all the items that still exist as well as add
// new items.
bool itemModified = false, resize = false;
TTeamMenuItem* teamItem = NULL;
for (int32 i = 0; i < totalItems; i++) {
if (teamMenu->SubmenuAt(i) == NULL)
continue;
teamItem = static_cast<TTeamMenuItem*>(teamMenu->ItemAt(i));
if (teamItem->IsExpanded()) {
int32 teamCount = teamItem->Teams()->CountItems();
for (int32 j = 0; j < teamCount; j++) {
// The following code is almost a copy/paste from
// WindowMenu.cpp
team_id theTeam = (addr_t)teamItem->Teams()->ItemAt(j);
int32 count = 0;
int32* tokens = get_token_list(theTeam, &count);
for (int32 k = 0; k < count; k++) {
client_window_info* wInfo
= get_window_info(tokens[k]);
if (wInfo == NULL)
continue;
if (TWindowMenu::WindowShouldBeListed(wInfo)) {
// Check if we have a matching window item...
item = teamItem->ExpandedWindowItem(
wInfo->server_token);
if (item) {
item->SetTo(wInfo->name,
wInfo->server_token, wInfo->is_mini,
((1 << current_workspace())
& wInfo->workspaces) != 0);
if (strcmp(wInfo->name,
item->Label()) != 0)
item->SetLabel(wInfo->name);
if (item->ChangedState())
itemModified = true;
} else if (teamItem->IsExpanded()) {
// Add the item
item = new TWindowMenuItem(wInfo->name,
wInfo->server_token, wInfo->is_mini,
((1 << current_workspace())
& wInfo->workspaces) != 0, false);
item->ExpandedItem(true);
teamMenu->AddItem(item,
TWindowMenuItem::InsertIndexFor(
teamMenu, i + 1, item));
resize = true;
}
}
free(wInfo);
}
free(tokens);
}
}
}
// Remove any remaining items which require an update.
for (int32 i = 0; i < totalItems; i++) {
if (!teamMenu->SubmenuAt(i)) {
item = static_cast<TWindowMenuItem*>(teamMenu->ItemAt(i));
if (item && item->RequiresUpdate()) {
item = static_cast<TWindowMenuItem*>
(teamMenu->RemoveItem(i));
delete item;
totalItems--;
resize = true;
}
}
}
// If any of the WindowMenuItems changed state, we need to force a
// repaint.
if (itemModified || resize) {
teamMenu->Invalidate();
if (resize)
teamMenu->SizeWindow(1);
}
teamMenu->Window()->Unlock();
}
sMonLocker.Unlock();
// sleep for a bit...
snooze(150000);
}
return B_OK;
}
void
TExpandoMenuBar::_FinishedDrag(bool invoke)
{
if (fPreviousDragTargetItem != NULL) {
if (invoke)
fPreviousDragTargetItem->Invoke();
fPreviousDragTargetItem->SetOverrideSelected(false);
fPreviousDragTargetItem = NULL;
}
if (!invoke && fBarView->Dragging())
fBarView->DragStop(true);
}