aboutsummaryrefslogtreecommitdiff
path: root/src/ScreenPlacement.cc
blob: 2a743ceb12dce2831d6aca0409051bcb3df18f62 (plain)
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
// ScreenPlacement.cc
// Copyright (c) 2006 Fluxbox Team (fluxgen at fluxbox dot org)
//
// 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 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 MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS 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.

#include "ScreenPlacement.hh"

#include "RowSmartPlacement.hh"
#include "MinOverlapPlacement.hh"
#include "UnderMousePlacement.hh"
#include "ColSmartPlacement.hh"
#include "CascadePlacement.hh"

#include "Screen.hh"
#include "Window.hh"

#include "FbTk/Menu.hh"
#include "FbTk/Luamm.hh"

#include <iostream>
#include <exception>
#ifdef HAVE_CSTRING
  #include <cstring>
#else
  #include <string.h>
#endif
using std::cerr;
using std::endl;

ScreenPlacement::ScreenPlacement(BScreen &screen):
    m_row_direction(screen.resourceManager(), LEFTRIGHT, 
                    screen.name()+".rowPlacementDirection", 
                    screen.altName()+".RowPlacementDirection"),
    m_col_direction(screen.resourceManager(), TOPBOTTOM, 
                    screen.name()+".colPlacementDirection", 
                    screen.altName()+".ColPlacementDirection"),
    m_placement_policy(screen.resourceManager(), ROWMINOVERLAPPLACEMENT, 
                       screen.name()+".windowPlacement", 
                       screen.altName()+".WindowPlacement"),
    m_old_policy(ROWSMARTPLACEMENT),
    m_strategy(0),
    m_screen(screen)
{
}

bool ScreenPlacement::placeWindow(const FluxboxWindow &win, int head,
                                  int &place_x, int &place_y) {


    // check the resource placement and see if has changed
    // and if so update the strategy
    if (m_old_policy != *m_placement_policy || !m_strategy.get()) {
        m_old_policy = *m_placement_policy;
        switch (*m_placement_policy) {
        case ROWSMARTPLACEMENT:
            m_strategy.reset(new RowSmartPlacement());
            break;
        case COLSMARTPLACEMENT:
            m_strategy.reset(new ColSmartPlacement());
            break;
        case ROWMINOVERLAPPLACEMENT:
        case COLMINOVERLAPPLACEMENT:
            m_strategy.reset(new MinOverlapPlacement());
            break;
        case CASCADEPLACEMENT:
            m_strategy.reset(new CascadePlacement(win.screen()));
            break;
        case UNDERMOUSEPLACEMENT:
            m_strategy.reset(new UnderMousePlacement());
            break;
        }
    }

    // view (screen + head) constraints
    int head_left = (signed) win.screen().maxLeft(head);
    int head_right = (signed) win.screen().maxRight(head);
    int head_top = (signed) win.screen().maxTop(head);
    int head_bot = (signed) win.screen().maxBottom(head);

    // start placement, top left corner
    place_x = head_left;
    place_y = head_top;

    bool placed = false;
    try {
        placed = m_strategy->placeWindow(win, head, place_x, place_y);
    } catch (std::bad_cast cast) {
        // This should not happen. 
        // If for some reason we change the PlacementStrategy in Screen
        // from ScreenPlacement to something else then we might get 
        // bad_cast from some placement strategies.
        cerr<<"Failed to place window: "<<cast.what()<<endl;
    }

    if (!placed) {
        // Create fallback strategy, when we need it the first time
        // This strategy must succeed!
        if (m_fallback_strategy.get() == 0)
            m_fallback_strategy.reset(new CascadePlacement(win.screen()));

        m_fallback_strategy->placeWindow(win, head, place_x, place_y);
    }



    int win_w = win.normalWidth() + win.fbWindow().borderWidth()*2 + win.widthOffset(),
        win_h = win.normalHeight() + win.fbWindow().borderWidth()*2 + win.heightOffset();

    // make sure the window is inside our screen(head) area
    if (place_x + win_w - win.xOffset() > head_right)
        place_x = head_left + (head_right - head_left - win_w) / 2 +
                  win.xOffset();
    if (place_y + win_h - win.yOffset() > head_bot)
        place_y = head_top + (head_bot - head_top - win_h) / 2 + win.yOffset();

    return true;
}

void ScreenPlacement::placeAndShowMenu(FbTk::Menu& menu, int x, int y, bool respect_struts) {

    int head = m_screen.getHead(x, y);

    menu.setScreen(m_screen.getHeadX(head),
        m_screen.getHeadY(head),
        m_screen.getHeadWidth(head),
        m_screen.getHeadHeight(head));

    menu.updateMenu(); // recalculate the size

    x = x - (menu.width() / 2);
    if (menu.isTitleVisible())
        y = y - (menu.titleWindow().height() / 2);

    // adjust (x, y) to fit on the screen
    if (!respect_struts) {

        int bw = 2 * menu.fbwindow().borderWidth();
        std::pair<int, int> pos = m_screen.clampToHead(head, x, y, menu.width() + bw, menu.height() + bw);
        x = pos.first;
        y = pos.second;

    } else { // do not cover toolbar if no title

        int top = static_cast<signed>(m_screen.maxTop(head));
        int bottom = static_cast<signed>(m_screen.maxBottom(head));
        int left = static_cast<signed>(m_screen.maxLeft(head));
        int right = static_cast<signed>(m_screen.maxRight(head));

        if (y < top)
            y = top;
        else if (y + static_cast<signed>(menu.height()) >= bottom)
            y = bottom - menu.height() - 1 - menu.fbwindow().borderWidth();

        if (x < left)
            x = left;
        else if (x + static_cast<signed>(menu.width()) >= right)
            x = right - static_cast<int>(menu.width()) - 1;
    }

    menu.move(x, y);
    menu.show();
    menu.grabInputFocus();
}

////////////////////// Placement Resources
namespace FbTk {

template <>
const EnumTraits<ScreenPlacement::PlacementPolicy>::Pair EnumTraits<ScreenPlacement::PlacementPolicy>::s_map[] = {
    { "RowSmartPlacement",      ScreenPlacement::ROWSMARTPLACEMENT },
    { "ColSmartPlacement",      ScreenPlacement::COLSMARTPLACEMENT },
    { "RowMinOverlapPlacement", ScreenPlacement::ROWMINOVERLAPPLACEMENT },
    { "ColMinOverlapPlacement", ScreenPlacement::COLMINOVERLAPPLACEMENT },
    { "UnderMousePlacement",    ScreenPlacement::UNDERMOUSEPLACEMENT },
    { "CascadePlacement",       ScreenPlacement::CASCADEPLACEMENT },
    { NULL,                     ScreenPlacement::CASCADEPLACEMENT }
};

template <>
const EnumTraits<ScreenPlacement::RowDirection>::Pair EnumTraits<ScreenPlacement::RowDirection>::s_map[] = {
    { "LeftToRight",            ScreenPlacement::LEFTRIGHT },
    { "RightToLeft",            ScreenPlacement::RIGHTLEFT },
    { NULL,                     ScreenPlacement::RIGHTLEFT },
};

template <>
const EnumTraits<ScreenPlacement::ColumnDirection>::Pair EnumTraits<ScreenPlacement::ColumnDirection>::s_map[] = {
    { "TopToBottom",            ScreenPlacement::TOPBOTTOM },
    { "BottomToTop",            ScreenPlacement::BOTTOMTOP },
    { NULL,                     ScreenPlacement::BOTTOMTOP },
};

} // end namespace FbTk