-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
DefaultWindowBehaviour.cpp
1312 lines (1079 loc) · 32.3 KB
/
DefaultWindowBehaviour.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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2001-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Authors:
* DarkWyrm <bpmagic@columbus.rr.com>
* Adi Oanca <adioanca@gmail.com>
* Stephan Aßmus <superstippi@gmx.de>
* Axel Dörfler <axeld@pinc-software.de>
* Brecht Machiels <brecht@mos6581.org>
* Clemens Zeidler <haiku@clemens-zeidler.de>
* Ingo Weinhold <ingo_weinhold@gmx.de>
*/
#include "DefaultWindowBehaviour.h"
#include <math.h>
#include <WindowPrivate.h>
#include "ClickTarget.h"
#include "Desktop.h"
#include "DefaultDecorator.h"
#include "DrawingEngine.h"
#include "Window.h"
//#define DEBUG_WINDOW_CLICK
#ifdef DEBUG_WINDOW_CLICK
# define STRACE_CLICK(x) printf x
#else
# define STRACE_CLICK(x) ;
#endif
// The span between mouse down
static const bigtime_t kWindowActivationTimeout = 500000LL;
// #pragma mark - State
struct DefaultWindowBehaviour::State {
State(DefaultWindowBehaviour& behavior)
:
fBehavior(behavior),
fWindow(behavior.fWindow),
fDesktop(behavior.fDesktop)
{
}
virtual ~State()
{
}
virtual void EnterState(State* previousState)
{
}
virtual void ExitState(State* nextState)
{
}
virtual bool MouseDown(BMessage* message, BPoint where, bool& _unhandled)
{
return true;
}
virtual void MouseUp(BMessage* message, BPoint where)
{
}
virtual void MouseMoved(BMessage* message, BPoint where, bool isFake)
{
}
virtual void ModifiersChanged(BPoint where, int32 modifiers)
{
}
protected:
DefaultWindowBehaviour& fBehavior;
Window* fWindow;
Desktop* fDesktop;
};
// #pragma mark - ManageWindowState Definition
struct DefaultWindowBehaviour::ManageWindowState : State {
ManageWindowState(DefaultWindowBehaviour& behavior,
BPoint where);
ManageWindowState(DefaultWindowBehaviour& behavior,
BPoint where, int8 horizontal, int8 vertical);
virtual void EnterState(State* previousState);
virtual void ExitState(State* nextState);
virtual bool MouseDown(BMessage* message, BPoint where,
bool& _unhandled);
virtual void MouseMoved(BMessage* message, BPoint where, bool isFake);
virtual void ModifiersChanged(BPoint where, int32 modifiers);
private:
void _UpdateResizeArrows(BPoint where);
private:
BPoint fLastMousePosition;
int8 fHorizontal;
int8 fVertical;
};
// #pragma mark - MouseTrackingState
struct DefaultWindowBehaviour::MouseTrackingState : State {
MouseTrackingState(DefaultWindowBehaviour& behavior, BPoint where,
bool windowActionOnMouseUp, bool minimizeCheckOnMouseUp,
int32 mouseButton = B_PRIMARY_MOUSE_BUTTON)
:
State(behavior),
fMouseButton(mouseButton),
fWindowActionOnMouseUp(windowActionOnMouseUp),
fMinimizeCheckOnMouseUp(minimizeCheckOnMouseUp),
fLastMousePosition(where),
fMouseMoveDistance(0),
fLastMoveTime(system_time())
{
}
virtual void MouseUp(BMessage* message, BPoint where)
{
// ignore, if it's not our mouse button
int32 buttons = message->FindInt32("buttons");
if ((buttons & fMouseButton) != 0)
return;
if (fMinimizeCheckOnMouseUp) {
// If the modifiers haven't changed in the meantime and not too
// much time has elapsed, we're supposed to minimize the window.
fMinimizeCheckOnMouseUp = false;
if (message->FindInt32("modifiers") == fBehavior.fLastModifiers
&& (fWindow->Flags() & B_NOT_MINIMIZABLE) == 0
&& system_time() - fLastMoveTime < kWindowActivationTimeout) {
fWindow->ServerWindow()->NotifyMinimize(true);
}
}
// Perform the window action in case the mouse was not moved.
if (fWindowActionOnMouseUp) {
// There is a time window for this feature, i.e. click and press
// too long, nothing will happen.
if (system_time() - fLastMoveTime < kWindowActivationTimeout)
MouseUpWindowAction();
}
fBehavior._NextState(NULL);
}
virtual void MouseMoved(BMessage* message, BPoint where, bool isFake)
{
// Limit the rate at which "mouse moved" events are handled that move
// or resize the window. At the moment this affects also tab sliding,
// but 1/75 s is a pretty fine granularity anyway, so don't bother.
bigtime_t now = system_time();
if (now - fLastMoveTime < 13333) {
// TODO: add a "timed event" to query for
// the then current mouse position
return;
}
if (fWindowActionOnMouseUp || fMinimizeCheckOnMouseUp) {
if (now - fLastMoveTime >= kWindowActivationTimeout) {
// This click is too long already for window activation/
// minimizing.
fWindowActionOnMouseUp = false;
fMinimizeCheckOnMouseUp = false;
fLastMoveTime = now;
}
} else
fLastMoveTime = now;
BPoint delta = where - fLastMousePosition;
// NOTE: "delta" is later used to change fLastMousePosition.
// If for some reason no change should take effect, delta
// is to be set to (0, 0) so that fLastMousePosition is not
// adjusted. This way the relative mouse position to the
// item being changed (border during resizing, tab during
// sliding...) stays fixed when the mouse is moved so that
// changes are taking effect again.
// If the window was moved enough, it doesn't come to
// the front in FFM mode when the mouse is released.
if (fWindowActionOnMouseUp || fMinimizeCheckOnMouseUp) {
fMouseMoveDistance += delta.x * delta.x + delta.y * delta.y;
if (fMouseMoveDistance > 16.0f) {
fWindowActionOnMouseUp = false;
fMinimizeCheckOnMouseUp = false;
} else
delta = B_ORIGIN;
}
// perform the action (this also updates the delta)
MouseMovedAction(delta, now);
// set the new mouse position
fLastMousePosition += delta;
}
virtual void MouseMovedAction(BPoint& delta, bigtime_t now)
{
}
virtual void MouseUpWindowAction()
{
// default is window activation
fDesktop->ActivateWindow(fWindow);
}
protected:
int32 fMouseButton;
bool fWindowActionOnMouseUp : 1;
bool fMinimizeCheckOnMouseUp : 1;
BPoint fLastMousePosition;
float fMouseMoveDistance;
bigtime_t fLastMoveTime;
};
// #pragma mark - DragState
struct DefaultWindowBehaviour::DragState : MouseTrackingState {
DragState(DefaultWindowBehaviour& behavior, BPoint where,
bool activateOnMouseUp, bool minimizeCheckOnMouseUp)
:
MouseTrackingState(behavior, where, activateOnMouseUp,
minimizeCheckOnMouseUp)
{
}
virtual void EnterState(State* previousState)
{
}
virtual void ExitState(State* nextState)
{
}
virtual bool MouseDown(BMessage* message, BPoint where, bool& _unhandled)
{
// right-click while dragging shall bring the window to front
int32 buttons = message->FindInt32("buttons");
if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0) {
if (fWindow == fDesktop->BackWindow())
fDesktop->ActivateWindow(fWindow);
else
fDesktop->SendWindowBehind(fWindow);
return true;
}
return MouseTrackingState::MouseDown(message, where, _unhandled);
}
virtual void MouseUp(BMessage* message, BPoint where)
{
if (fBehavior._IsWindowModifier(message->FindInt32("modifiers"))) {
// If the window modifiers are held return to the window
// management state.
fBehavior._NextState(new(std::nothrow) ManageWindowState(
fBehavior, where));
} else
fBehavior._NextState(NULL);
}
virtual void ModifiersChanged(BPoint where, int32 modifiers)
{
// Exit if user lifted window management modifiers
fBehavior.fLastModifiers = modifiers;
if (!fBehavior._IsWindowModifier(modifiers))
fBehavior._NextState(NULL);
}
virtual void MouseMovedAction(BPoint& delta, bigtime_t now)
{
if (!(fWindow->Flags() & B_NOT_MOVABLE)) {
BPoint oldLeftTop = fWindow->Frame().LeftTop();
fBehavior.AlterDeltaForSnap(fWindow, delta, now);
fDesktop->MoveWindowBy(fWindow, delta.x, delta.y);
// constrain delta to true change in position
delta = fWindow->Frame().LeftTop() - oldLeftTop;
} else
delta = BPoint(0, 0);
}
};
// #pragma mark - ResizeState
struct DefaultWindowBehaviour::ResizeState : MouseTrackingState {
ResizeState(DefaultWindowBehaviour& behavior, BPoint where,
bool activateOnMouseUp)
:
MouseTrackingState(behavior, where, activateOnMouseUp, false)
{
}
virtual void MouseMovedAction(BPoint& delta, bigtime_t now)
{
if (!(fWindow->Flags() & B_NOT_RESIZABLE)) {
if (fWindow->Flags() & B_NOT_V_RESIZABLE)
delta.y = 0;
if (fWindow->Flags() & B_NOT_H_RESIZABLE)
delta.x = 0;
BPoint oldRightBottom = fWindow->Frame().RightBottom();
fDesktop->ResizeWindowBy(fWindow, delta.x, delta.y);
// constrain delta to true change in size
delta = fWindow->Frame().RightBottom() - oldRightBottom;
} else
delta = BPoint(0, 0);
}
};
// #pragma mark - SlideTabState
struct DefaultWindowBehaviour::SlideTabState : MouseTrackingState {
SlideTabState(DefaultWindowBehaviour& behavior, BPoint where)
:
MouseTrackingState(behavior, where, false, false)
{
}
virtual
~SlideTabState()
{
fDesktop->SetWindowTabLocation(fWindow, fWindow->TabLocation(), false);
}
virtual void MouseMovedAction(BPoint& delta, bigtime_t now)
{
float location = fWindow->TabLocation();
// TODO: change to [0:1]
location += delta.x;
AdjustMultiTabLocation(location, true);
if (fDesktop->SetWindowTabLocation(fWindow, location, true))
delta.y = 0;
else
delta = BPoint(0, 0);
}
void AdjustMultiTabLocation(float location, bool isShifting)
{
::Decorator* decorator = fWindow->Decorator();
if (decorator == NULL || decorator->CountTabs() <= 1)
return;
// TODO does not work for none continuous shifts
int32 windowIndex = fWindow->PositionInStack();
DefaultDecorator::Tab* movingTab = static_cast<DefaultDecorator::Tab*>(
decorator->TabAt(windowIndex));
int32 neighbourIndex = windowIndex;
if (movingTab->tabOffset > location)
neighbourIndex--;
else
neighbourIndex++;
DefaultDecorator::Tab* neighbourTab
= static_cast<DefaultDecorator::Tab*>(decorator->TabAt(
neighbourIndex));
if (neighbourTab == NULL)
return;
if (movingTab->tabOffset > location) {
if (location >
neighbourTab->tabOffset + neighbourTab->tabRect.Width() / 2)
return;
} else {
if (location + movingTab->tabRect.Width() <
neighbourTab->tabOffset + neighbourTab->tabRect.Width() / 2)
return;
}
fWindow->MoveToStackPosition(neighbourIndex, isShifting);
}
};
// #pragma mark - ResizeBorderState
struct DefaultWindowBehaviour::ResizeBorderState : MouseTrackingState {
ResizeBorderState(DefaultWindowBehaviour& behavior, BPoint where,
Decorator::Region region)
:
MouseTrackingState(behavior, where, true, false,
B_SECONDARY_MOUSE_BUTTON),
fHorizontal(NONE),
fVertical(NONE)
{
switch (region) {
case Decorator::REGION_TAB:
fHorizontal = NONE;
fVertical = NONE;
break;
case Decorator::REGION_LEFT_BORDER:
fHorizontal = LEFT;
break;
case Decorator::REGION_RIGHT_BORDER:
fHorizontal = RIGHT;
break;
case Decorator::REGION_TOP_BORDER:
fVertical = TOP;
break;
case Decorator::REGION_BOTTOM_BORDER:
fVertical = BOTTOM;
break;
case Decorator::REGION_LEFT_TOP_CORNER:
fHorizontal = LEFT;
fVertical = TOP;
break;
case Decorator::REGION_LEFT_BOTTOM_CORNER:
fHorizontal = LEFT;
fVertical = BOTTOM;
break;
case Decorator::REGION_RIGHT_TOP_CORNER:
fHorizontal = RIGHT;
fVertical = TOP;
break;
case Decorator::REGION_RIGHT_BOTTOM_CORNER:
fHorizontal = RIGHT;
fVertical = BOTTOM;
break;
default:
break;
}
}
ResizeBorderState(DefaultWindowBehaviour& behavior, BPoint where,
int8 horizontal, int8 vertical)
:
MouseTrackingState(behavior, where, true, false,
B_SECONDARY_MOUSE_BUTTON),
fHorizontal(horizontal),
fVertical(vertical)
{
}
virtual void EnterState(State* previousState)
{
fBehavior._SetResizeCursor(fHorizontal, fVertical);
}
virtual void ExitState(State* nextState)
{
}
virtual void MouseUp(BMessage* message, BPoint where)
{
if (fBehavior._IsWindowModifier(message->FindInt32("modifiers"))) {
// If the window modifiers are still held return to window
// management state.
fBehavior._NextState(new(std::nothrow) ManageWindowState(
fBehavior, where, fHorizontal, fVertical));
} else
fBehavior._NextState(NULL);
}
virtual void ModifiersChanged(BPoint where, int32 modifiers)
{
// Exit if user lifted window management modifiers
fBehavior.fLastModifiers = modifiers;
if (!fBehavior._IsWindowModifier(modifiers))
fBehavior._NextState(NULL);
}
virtual void MouseMovedAction(BPoint& delta, bigtime_t now)
{
if (fHorizontal == NONE)
delta.x = 0;
if (fVertical == NONE)
delta.y = 0;
if (delta.x == 0 && delta.y == 0)
return;
// Resize first -- due to the window size limits this is not unlikely
// to turn out differently from what we request.
BPoint oldRightBottom = fWindow->Frame().RightBottom();
fDesktop->ResizeWindowBy(fWindow, delta.x * fHorizontal,
delta.y * fVertical);
// constrain delta to true change in size
delta = fWindow->Frame().RightBottom() - oldRightBottom;
delta.x *= fHorizontal;
delta.y *= fVertical;
// see, if we have to move, too
float moveX = fHorizontal == LEFT ? delta.x : 0;
float moveY = fVertical == TOP ? delta.y : 0;
if (moveX != 0 || moveY != 0)
fDesktop->MoveWindowBy(fWindow, moveX, moveY);
}
virtual void MouseUpWindowAction()
{
fDesktop->SendWindowBehind(fWindow);
}
private:
int8 fHorizontal;
int8 fVertical;
};
// #pragma mark - DecoratorButtonState
struct DefaultWindowBehaviour::DecoratorButtonState : State {
DecoratorButtonState(DefaultWindowBehaviour& behavior,
int32 tab, Decorator::Region button)
:
State(behavior),
fTab(tab),
fButton(button)
{
}
virtual void EnterState(State* previousState)
{
_RedrawDecorator(NULL);
}
virtual void MouseUp(BMessage* message, BPoint where)
{
// ignore, if it's not the primary mouse button
int32 buttons = message->FindInt32("buttons");
if ((buttons & B_PRIMARY_MOUSE_BUTTON) != 0)
return;
// redraw the decorator
if (Decorator* decorator = fWindow->Decorator()) {
BRegion* visibleBorder = fWindow->RegionPool()->GetRegion();
fWindow->GetBorderRegion(visibleBorder);
visibleBorder->IntersectWith(&fWindow->VisibleRegion());
DrawingEngine* engine = decorator->GetDrawingEngine();
engine->LockParallelAccess();
engine->ConstrainClippingRegion(visibleBorder);
int32 tab;
switch (fButton) {
case Decorator::REGION_CLOSE_BUTTON:
decorator->SetClose(fTab, false);
if (fBehavior._RegionFor(message, tab) == fButton)
fWindow->ServerWindow()->NotifyQuitRequested();
break;
case Decorator::REGION_ZOOM_BUTTON:
decorator->SetZoom(fTab, false);
if (fBehavior._RegionFor(message, tab) == fButton)
fWindow->ServerWindow()->NotifyZoom();
break;
case Decorator::REGION_MINIMIZE_BUTTON:
decorator->SetMinimize(fTab, false);
if (fBehavior._RegionFor(message, tab) == fButton)
fWindow->ServerWindow()->NotifyMinimize(true);
break;
default:
break;
}
engine->UnlockParallelAccess();
fWindow->RegionPool()->Recycle(visibleBorder);
}
fBehavior._NextState(NULL);
}
virtual void MouseMoved(BMessage* message, BPoint where, bool isFake)
{
_RedrawDecorator(message);
}
private:
void _RedrawDecorator(const BMessage* message)
{
if (Decorator* decorator = fWindow->Decorator()) {
BRegion* visibleBorder = fWindow->RegionPool()->GetRegion();
fWindow->GetBorderRegion(visibleBorder);
visibleBorder->IntersectWith(&fWindow->VisibleRegion());
DrawingEngine* engine = decorator->GetDrawingEngine();
engine->LockParallelAccess();
engine->ConstrainClippingRegion(visibleBorder);
int32 tab;
Decorator::Region hitRegion = message != NULL
? fBehavior._RegionFor(message, tab) : fButton;
switch (fButton) {
case Decorator::REGION_CLOSE_BUTTON:
decorator->SetClose(fTab, hitRegion == fButton);
break;
case Decorator::REGION_ZOOM_BUTTON:
decorator->SetZoom(fTab, hitRegion == fButton);
break;
case Decorator::REGION_MINIMIZE_BUTTON:
decorator->SetMinimize(fTab, hitRegion == fButton);
break;
default:
break;
}
engine->UnlockParallelAccess();
fWindow->RegionPool()->Recycle(visibleBorder);
}
}
protected:
int32 fTab;
Decorator::Region fButton;
};
// #pragma mark - ManageWindowState
DefaultWindowBehaviour::ManageWindowState::ManageWindowState(
DefaultWindowBehaviour& behavior, BPoint where)
:
State(behavior),
fLastMousePosition(where),
fHorizontal(NONE),
fVertical(NONE)
{
}
DefaultWindowBehaviour::ManageWindowState::ManageWindowState(
DefaultWindowBehaviour& behavior, BPoint where, int8 horizontal,
int8 vertical)
:
State(behavior),
fLastMousePosition(where),
fHorizontal(horizontal),
fVertical(vertical)
{
}
void
DefaultWindowBehaviour::ManageWindowState::EnterState(State* previousState)
{
// Update the mouse cursor
if ((fWindow->Flags() & B_NOT_MOVABLE) == 0)
fBehavior._SetMoveCursor();
else
fBehavior._SetNowAllowedCursor();
_UpdateResizeArrows(fLastMousePosition);
}
void
DefaultWindowBehaviour::ManageWindowState::ExitState(State* nextState)
{
}
bool
DefaultWindowBehaviour::ManageWindowState::MouseDown(BMessage* message,
BPoint where, bool& _unhandled)
{
int32 buttons = message->FindInt32("buttons");
if ((buttons & B_PRIMARY_MOUSE_BUTTON) != 0) {
if ((fWindow->Flags() & B_NOT_MOVABLE) == 0) {
fBehavior._SetMoveCursor();
fBehavior._NextState(new (std::nothrow) DragState(fBehavior,
where, false, false));
}
} else if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0) {
if ((fWindow->Flags() & B_NOT_RESIZABLE) == 0) {
fBehavior._NextState(new (std::nothrow) ResizeBorderState(
fBehavior, where, fHorizontal, fVertical));
} else
fBehavior._SetNowAllowedCursor();
}
return true;
}
void
DefaultWindowBehaviour::ManageWindowState::MouseMoved(BMessage* message,
BPoint where, bool isFake)
{
// Update the mouse cursor
if ((fDesktop->WindowAt(where)->Flags() & B_NOT_RESIZABLE) != 0
&& (message->FindInt32("buttons") & B_SECONDARY_MOUSE_BUTTON) != 0) {
fBehavior._SetNowAllowedCursor();
} else if ((fDesktop->WindowAt(where)->Flags() & B_NOT_MOVABLE) == 0)
fBehavior._SetMoveCursor();
else
fBehavior._SetNowAllowedCursor();
// If the cursor is still over our window, update the borders.
// Otherwise leave the state.
if (fDesktop->WindowAt(where) == fWindow) {
fLastMousePosition = where;
_UpdateResizeArrows(fLastMousePosition);
} else
fBehavior._NextState(NULL);
}
void
DefaultWindowBehaviour::ManageWindowState::ModifiersChanged(BPoint where,
int32 modifiers)
{
fBehavior.fLastModifiers = modifiers;
if (!fBehavior._IsWindowModifier(modifiers)) {
if ((fWindow->Flags() & B_NOT_RESIZABLE) == 0
&& fBehavior._IsControlModifier(modifiers)) {
fBehavior._SetBorderResizeCursor(where);
} else
fBehavior._ResetCursor();
fBehavior._NextState(NULL);
}
}
void
DefaultWindowBehaviour::ManageWindowState::_UpdateResizeArrows(BPoint where)
{
if ((fWindow->Flags() & B_NOT_RESIZABLE) != 0)
return;
// Compute the window center relative location of where. We divide by
// the width respective the height, so we compensate for the window's
// aspect ratio.
BRect frame(fWindow->Frame());
if (frame.Width() + 1 == 0 || frame.Height() + 1 == 0)
return;
float x = (where.x - (frame.left + frame.right) / 2)
/ (frame.Width() + 1);
float y = (where.y - (frame.top + frame.bottom) / 2)
/ (frame.Height() + 1);
// compute the resize direction
_ComputeResizeDirection(x, y, fHorizontal, fVertical);
// set resize direction to NONE if not resizable
if ((fWindow->Flags() & B_NOT_H_RESIZABLE) != 0)
fHorizontal = NONE;
if ((fWindow->Flags() & B_NOT_V_RESIZABLE) != 0)
fVertical = NONE;
}
// #pragma mark - DefaultWindowBehaviour
DefaultWindowBehaviour::DefaultWindowBehaviour(Window* window)
:
fWindow(window),
fDesktop(window->Desktop()),
fState(NULL),
fLastModifiers(0)
{
}
DefaultWindowBehaviour::~DefaultWindowBehaviour()
{
delete fState;
}
bool
DefaultWindowBehaviour::MouseDown(BMessage* message, BPoint where,
int32 lastHitRegion, int32& clickCount, int32& _hitRegion)
{
fLastModifiers = message->FindInt32("modifiers");
int32 buttons = message->FindInt32("buttons");
// if a state is active, let it do the job
if (fState != NULL) {
bool unhandled = false;
bool result = fState->MouseDown(message, where, unhandled);
if (!unhandled)
return result;
}
// No state active yet, or it wants us to handle the event -- determine the
// click region and decide what to do.
Decorator* decorator = fWindow->Decorator();
Decorator::Region hitRegion = Decorator::REGION_NONE;
int32 tab = -1;
Action action = ACTION_NONE;
bool inBorderRegion = false;
if (decorator != NULL)
inBorderRegion = decorator->GetFootprint().Contains(where);
bool windowModifier = _IsWindowModifier(fLastModifiers);
if (windowModifier || inBorderRegion) {
// click on the window decorator or we have the window modifier keys
// held
// get the functional hit region
if (windowModifier) {
// click with window modifier keys -- let the whole window behave
// like the border
hitRegion = Decorator::REGION_LEFT_BORDER;
} else {
// click on the decorator -- get the exact region
hitRegion = _RegionFor(message, tab);
}
// translate the region into an action
uint32 flags = fWindow->Flags();
if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0
|| (_IsControlModifier(fLastModifiers)
&& (buttons & B_PRIMARY_MOUSE_BUTTON) != 0)) {
// right mouse button or control + left mouse button
switch (hitRegion) {
case Decorator::REGION_TAB:
case Decorator::REGION_CLOSE_BUTTON:
case Decorator::REGION_ZOOM_BUTTON:
case Decorator::REGION_MINIMIZE_BUTTON:
// Right clicking on the window tab sends it behind
fDesktop->SendWindowBehind(fWindow);
break;
case Decorator::REGION_LEFT_BORDER:
case Decorator::REGION_RIGHT_BORDER:
case Decorator::REGION_TOP_BORDER:
case Decorator::REGION_BOTTOM_BORDER:
case Decorator::REGION_LEFT_TOP_CORNER:
case Decorator::REGION_LEFT_BOTTOM_CORNER:
case Decorator::REGION_RIGHT_TOP_CORNER:
case Decorator::REGION_RIGHT_BOTTOM_CORNER:
action = (flags & B_NOT_RESIZABLE) == 0
? ACTION_RESIZE_BORDER : ACTION_DRAG;
break;
default:
break;
}
} else if ((buttons & B_PRIMARY_MOUSE_BUTTON) != 0) {
// left mouse button
switch (hitRegion) {
case Decorator::REGION_TAB: {
// tab sliding in any case if either shift key is held down
// except sliding up-down by moving mouse left-right would
// look strange
if ((fLastModifiers & B_SHIFT_KEY) != 0
&& fWindow->Look() != kLeftTitledWindowLook) {
action = ACTION_SLIDE_TAB;
break;
}
action = ACTION_DRAG;
break;
}
case Decorator::REGION_LEFT_BORDER:
case Decorator::REGION_RIGHT_BORDER:
case Decorator::REGION_TOP_BORDER:
case Decorator::REGION_BOTTOM_BORDER:
action = ACTION_DRAG;
break;
case Decorator::REGION_CLOSE_BUTTON:
action = (flags & B_NOT_CLOSABLE) == 0
? ACTION_CLOSE : ACTION_DRAG;
break;
case Decorator::REGION_ZOOM_BUTTON:
action = (flags & B_NOT_ZOOMABLE) == 0
? ACTION_ZOOM : ACTION_DRAG;
break;
case Decorator::REGION_MINIMIZE_BUTTON:
action = (flags & B_NOT_MINIMIZABLE) == 0
? ACTION_MINIMIZE : ACTION_DRAG;
break;
case Decorator::REGION_LEFT_TOP_CORNER:
case Decorator::REGION_LEFT_BOTTOM_CORNER:
case Decorator::REGION_RIGHT_TOP_CORNER:
// TODO: Handle correctly!
action = ACTION_DRAG;
break;
case Decorator::REGION_RIGHT_BOTTOM_CORNER:
action = (flags & B_NOT_RESIZABLE) == 0
? ACTION_RESIZE : ACTION_DRAG;
break;
default:
break;
}
}
}
_hitRegion = (int32)hitRegion;
if (action == ACTION_NONE) {
// No action -- if this is a click inside the window's contents,
// let it be forwarded to the window.
return inBorderRegion;
}
// reset the click count, if the hit region differs from the previous one
if (hitRegion != lastHitRegion)
clickCount = 1;
DesktopSettings desktopSettings(fDesktop);
if (!desktopSettings.AcceptFirstClick()) {
// Ignore clicks on decorator buttons if the
// non-floating window doesn't have focus
if (!fWindow->IsFocus() && !fWindow->IsFloating()
&& action != ACTION_RESIZE_BORDER
&& action != ACTION_RESIZE && action != ACTION_SLIDE_TAB)
action = ACTION_DRAG;
}
bool activateOnMouseUp = false;
if (action != ACTION_RESIZE_BORDER) {
// activate window if in click to activate mode, else only focus it
if (desktopSettings.MouseMode() == B_NORMAL_MOUSE) {
fDesktop->ActivateWindow(fWindow);
} else {
fDesktop->SetFocusWindow(fWindow);
activateOnMouseUp = true;
}
}
// switch to the new state
switch (action) {
case ACTION_CLOSE:
case ACTION_ZOOM:
case ACTION_MINIMIZE:
_NextState(
new (std::nothrow) DecoratorButtonState(*this, tab, hitRegion));
STRACE_CLICK(("===> ACTION_CLOSE/ZOOM/MINIMIZE\n"));
break;
case ACTION_DRAG:
_NextState(new (std::nothrow) DragState(*this, where,
activateOnMouseUp, clickCount == 2));
STRACE_CLICK(("===> ACTION_DRAG\n"));
break;
case ACTION_RESIZE:
_NextState(new (std::nothrow) ResizeState(*this, where,
activateOnMouseUp));
STRACE_CLICK(("===> ACTION_RESIZE\n"));
break;
case ACTION_SLIDE_TAB:
_NextState(new (std::nothrow) SlideTabState(*this, where));
STRACE_CLICK(("===> ACTION_SLIDE_TAB\n"));
break;
case ACTION_RESIZE_BORDER:
_NextState(new (std::nothrow) ResizeBorderState(*this, where,
hitRegion));
STRACE_CLICK(("===> ACTION_RESIZE_BORDER\n"));
break;
default:
break;
}
return true;
}