aboutsummaryrefslogtreecommitdiff
path: root/src/FocusControl.hh
blob: c55aa6a4e96319a7208158f8fa074ae5325f5d9e (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
// FocusControl.hh
// 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.

// $Id$

#ifndef FOCUSCONTROL_HH
#define FOCUSCONTROL_HH

#include <list>

#include "FbTk/Resource.hh"

class WinClient;
class FluxboxWindow;
class BScreen;

/**
 * Handles window focus for a specific screen.
 * It also holds the static "global" focused window
 */
class FocusControl {
public:
    typedef std::list<WinClient *> FocusedWindows;
    /// main focus model
    enum FocusModel { 
        MOUSEFOCUS = 0, ///< focus follows mouse
        CLICKFOCUS      ///< focus on click
    };

    /// focus model for tabs
    enum TabFocusModel { 
        MOUSETABFOCUS = 0, ///< tab focus follows mouse
        CLICKTABFOCUS  ///< tab focus on click
    };

    /// focus direction for windows
    enum FocusDir { 
        FOCUSUP,    ///< window is above
        FOCUSDOWN,  ///< window is down
        FOCUSLEFT,  ///< window is left
        FOCUSRIGHT  ///< window is right
    };

    /// prevFocus/nextFocus option bits
    enum FocusOption { 
        CYCLEGROUPS = 0x01,      ///< cycle groups
        CYCLESKIPSTUCK = 0x02,   ///< skip stuck windows
        CYCLESKIPSHADED = 0x04,  ///< skip shaded windows
        CYCLELINEAR = 0x08,      ///< linear cycle
        CYCLESKIPICONIC = 0x10,  ///< skip iconified windows
        CYCLEDEFAULT = 0x00      ///< default
    };

    /// @param screen the screen to control focus for
    explicit FocusControl(BScreen &screen);

    /// cycle previous focuable 
    void prevFocus() { cycleFocus(&m_focused_list, 0, true); }
    /// cycle next focusable
    void nextFocus() { cycleFocus(&m_focused_list, 0, false); }
    /**
     * Cycle focus for a set of windows.
     * @param winlist the windowlist to cycle through
     * @param options cycle options @see FocusOption
     * @param reverse reverse the cycle order
     */
    void cycleFocus(FocusedWindows *winlist, int options, bool reverse = false);
    /// sets the focused window on a screen
    void setScreenFocusedWindow(WinClient &win_client);
    /// sets the main focus model
    void setFocusModel(FocusModel model);
    /// sets tab focus model
   void setTabFocusModel(TabFocusModel model);

    /// stop cycling mode
    void stopCyclingFocus();
    /** 
     * Do directional focus mode.
     * @param win current window
     * @param dir direction from current window to focus.
     */
    void dirFocus(FluxboxWindow &win, FocusDir dir);
    /// @return true if focus mode is mouse focus
    bool isMouseFocus() const { return focusModel() == MOUSEFOCUS; }
    /// @return true if tab focus mode is mouse tab focus
    bool isMouseTabFocus() const { return tabFocusModel() == MOUSETABFOCUS; }
    /// @return true if cycling is in progress
    bool isCycling() const { return m_cycling_list != 0; }
    /// Appends a client to the back of the focus list
    void addFocusBack(WinClient &client);
    /// Appends a client to the front of the focus list
    void addFocusFront(WinClient &client);
    void setFocusBack(FluxboxWindow *fbwin);
    /// @return main focus model
    FocusModel focusModel() const { return *m_focus_model; }
    /// @return tab focus model
    TabFocusModel tabFocusModel() const { return *m_tab_focus_model; }
    /// @return true if newly created windows are focused
    bool focusNew() const { return *m_focus_new; }
    /// @return last focused client in a specific workspace, or NULL.
    WinClient *lastFocusedWindow(int workspace);
    
    WinClient *lastFocusedWindow(FluxboxWindow &group, WinClient *ignore_client);

    /// @return focus list in creation order
    FocusedWindows &creationOrderList() { return m_creation_order_list; }
    /// @return the focus list
    FocusedWindows &focusedOrderList() { return m_focused_list; }
    /// removes a client from the focus list
    void removeClient(WinClient &client);
    /// starts terminating this control
    void shutdown();
    /// do fallback focus for screen if normal focus control failed.
    static void revertFocus(BScreen &screen);
    // like revertFocus, but specifically related to this window (transients etc)
    static void unfocusWindow(WinClient &client, bool full_revert = true, bool unfocus_frame = false);
    static void setFocusedWindow(WinClient *focus_to);
    static void setFocusedFbWindow(FluxboxWindow *focus_to) { s_focused_fbwindow = focus_to; }
    static WinClient *focusedWindow() { return s_focused_window; }
    static FluxboxWindow *focusedFbWindow() { return s_focused_fbwindow; }
private:

    BScreen &m_screen;

    FbTk::Resource<FocusModel> m_focus_model;    
    FbTk::Resource<TabFocusModel> m_tab_focus_model;
    FbTk::Resource<bool> m_focus_new;

    // This list keeps the order of window focusing for this screen
    // Screen global so it works for sticky windows too.
    FocusedWindows m_focused_list;
    FocusedWindows m_creation_order_list;

    FocusedWindows::iterator m_cycling_window;
    FocusedWindows *m_cycling_list;
    WinClient *m_was_iconic;
    WinClient *m_cycling_last;

    static WinClient *s_focused_window;
    static FluxboxWindow *s_focused_fbwindow;
    static bool s_reverting;
};

#endif // FOCUSCONTROL_HH