xref: /haiku/src/servers/app/WindowList.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 /*
2  * Copyright (c) 2005, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 
10 #include "DesktopSettings.h"
11 #include "WindowLayer.h"
12 
13 
14 const BPoint kInvalidWindowPosition = BPoint(INFINITY, INFINITY);
15 
16 
17 window_anchor::window_anchor()
18 	 :
19 	 next(NULL),
20 	 previous(NULL),
21 	 position(kInvalidWindowPosition)
22 {
23 }
24 
25 
26 //	#pragma mark -
27 
28 
29 WindowList::WindowList(int32 index)
30 	:
31 	fIndex(index),
32 	fFirstWindow(NULL),
33 	fLastWindow(NULL)
34 {
35 }
36 
37 
38 WindowList::~WindowList()
39 {
40 }
41 
42 
43 void
44 WindowList::SetIndex(int32 index)
45 {
46 	fIndex = index;
47 }
48 
49 
50 void
51 WindowList::AddWindow(WindowLayer* window, WindowLayer* before)
52 {
53 	window_anchor& windowAnchor = window->Anchor(fIndex);
54 
55 	if (before != NULL) {
56 		window_anchor& beforeAnchor = before->Anchor(fIndex);
57 
58 		// add view before this one
59 		windowAnchor.next = before;
60 		windowAnchor.previous = beforeAnchor.previous;
61 		if (windowAnchor.previous != NULL)
62 			windowAnchor.previous->Anchor(fIndex).next = window;
63 
64 		beforeAnchor.previous = window;
65 		if (fFirstWindow == before)
66 			fFirstWindow = window;
67 	} else {
68 		// add view to the end of the list
69 		if (fLastWindow != NULL) {
70 			fLastWindow->Anchor(fIndex).next = window;
71 			windowAnchor.previous = fLastWindow;
72 		} else {
73 			fFirstWindow = window;
74 			windowAnchor.previous = NULL;
75 		}
76 
77 		windowAnchor.next = NULL;
78 		fLastWindow = window;
79 	}
80 
81 	if (fIndex < kMaxWorkspaces)
82 		window->SetWorkspaces(window->Workspaces() | (1UL << fIndex));
83 }
84 
85 
86 void
87 WindowList::RemoveWindow(WindowLayer* window)
88 {
89 	window_anchor& windowAnchor = window->Anchor(fIndex);
90 
91 	if (fFirstWindow == window) {
92 		// it's the first child
93 		fFirstWindow = windowAnchor.next;
94 	} else {
95 		// it must have a previous sibling, then
96 		windowAnchor.previous->Anchor(fIndex).next = windowAnchor.next;
97 	}
98 
99 	if (fLastWindow == window) {
100 		// it's the last child
101 		fLastWindow = windowAnchor.previous;
102 	} else {
103 		// then it must have a next sibling
104 		windowAnchor.next->Anchor(fIndex).previous = windowAnchor.previous;
105 	}
106 
107 	if (fIndex < kMaxWorkspaces)
108 		window->SetWorkspaces(window->Workspaces() & ~(1UL << fIndex));
109 
110 	windowAnchor.previous = NULL;
111 	windowAnchor.next = NULL;
112 }
113 
114 
115 bool
116 WindowList::HasWindow(WindowLayer* window) const
117 {
118 	if (window == NULL)
119 		return false;
120 
121 	return window->Anchor(fIndex).next != NULL
122 		|| window->Anchor(fIndex).previous != NULL
123 		|| fFirstWindow == window
124 		|| fLastWindow == window;
125 }
126 
127