summaryrefslogtreecommitdiff
path: root/src/Xinerama.hh
blob: b0ff70e32e497b46a8da7f1e2b3a0403bd56f59d (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
// Xinerama.hh for Fluxbox - helpful tools for multiple heads
// Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//                and Simon Bowden    (rathnor at users.sourceforge.net)
//
// 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.

#ifndef XINERAMA_HH
#define XINERAMA_HH

#include "FbMenu.hh"
#include "fluxbox.hh"
#include "Screen.hh"

#include "FbTk/RefCount.hh"
#include "FbTk/SimpleCommand.hh"
#include "FbTk/RadioMenuItem.hh"

// provides a generic way for giving an object a xinerama head menu
// The object must have two functions:
// int getOnHead(), and
// void setOnHead(int)

/// this class holds the xinerama items
template <typename ItemType> 
class XineramaHeadMenuItem : public FbTk::RadioMenuItem {
public:
    XineramaHeadMenuItem(const FbTk::FbString &label, ItemType &object, int headnum,
                  FbTk::RefCount<FbTk::Command<void> > &cmd):
        FbTk::RadioMenuItem(label,cmd), m_object(object), m_headnum(headnum) {}
    XineramaHeadMenuItem(const FbTk::FbString &label, ItemType &object, int headnum):
        FbTk::RadioMenuItem(label), m_object(object), m_headnum(headnum) {}

    bool isSelected() const { return m_object.getOnHead() == m_headnum; } 
    void click(int button, int time, unsigned int mods) {
        m_object.saveOnHead(m_headnum);
        FbTk::RadioMenuItem::click(button, time, mods);
    }
    
private:
    ItemType &m_object;
    int m_headnum;
};


/// Create a xinerama menu
template <typename ItemType>
class XineramaHeadMenu : public ToggleMenu {
public:
    XineramaHeadMenu(FbTk::ThemeProxy<FbTk::MenuTheme> &tm, BScreen &screen,
                     FbTk::ImageControl &imgctrl, FbTk::Layer &layer,
                     ItemType &item, const FbTk::FbString & title = "");
    void reloadHeads();

private:
    ItemType &m_object;
    BScreen &m_screen;
};


template <typename ItemType>
XineramaHeadMenu<ItemType>::XineramaHeadMenu(
        FbTk::ThemeProxy<FbTk::MenuTheme> &tm, BScreen &screen,
        FbTk::ImageControl &imgctrl, FbTk::Layer &layer, ItemType &item,
        const FbTk::FbString & title):
    ToggleMenu(tm, imgctrl, layer),
    m_object(item), m_screen(screen)
{
    setLabel(title);
    reloadHeads();
}

template <typename ItemType>
void XineramaHeadMenu<ItemType>::reloadHeads()
{
    removeAll();
    FbTk::RefCount<FbTk::Command<void> > saverc_cmd(new FbTk::SimpleCommand<Fluxbox>(
                                     *Fluxbox::instance(), 
                                     &Fluxbox::save_rc));
    for (int i=1; i <= m_screen.numHeads(); ++i) {
        // TODO: nls
/*
        sprintf(tname, I18n::instance()->
                getMessage(
                    FBNLS::ScreenSet, 
                    FBNLS::XineramaDefaultHeadFormat,
                    "Head %d"), i); //m_id starts at 0
*/
        std::string tname("Head ");
        tname += FbTk::StringUtil::number2String(i);
        insert(new XineramaHeadMenuItem<ItemType>(
                   tname.c_str(), m_object, i, saverc_cmd));
    }
    // TODO: nls
    insert(new XineramaHeadMenuItem<ItemType>(
               "All Heads", m_object, 0, saverc_cmd));
    updateMenu();
}

#endif // XINERAMA_HH