fluxbox/src/Workspace.cc

432 lines
13 KiB
C++
Raw Normal View History

2002-02-09 16:41:53 +00:00
// Workspace.cc for Fluxbox
2006-02-16 06:53:05 +00:00
// Copyright (c) 2001 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
2002-02-09 16:41:53 +00:00
//
2001-12-11 20:47:02 +00:00
// Workspace.cc for Blackbox - an X11 Window manager
2003-01-12 18:08:05 +00:00
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.net)
2001-12-11 20:47:02 +00:00
//
// 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,
2003-01-12 18:08:05 +00:00
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2001-12-11 20:47:02 +00:00
// 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.
2004-11-19 11:37:27 +00:00
// $Id$
2002-01-20 02:08:12 +00:00
2002-08-11 22:35:40 +00:00
#include "Workspace.hh"
2001-12-11 20:47:02 +00:00
#include "fluxbox.hh"
#include "Screen.hh"
#include "Window.hh"
2003-04-14 15:01:55 +00:00
#include "WinClient.hh"
2003-05-19 15:32:47 +00:00
#include "FbWinFrame.hh"
#include "WindowCmd.hh"
#include "FocusControl.hh"
#include "PlacementStrategy.hh"
2001-12-11 20:47:02 +00:00
2004-06-07 11:46:05 +00:00
#include "FbTk/I18n.hh"
2003-12-30 18:16:51 +00:00
#include "FbTk/MenuItem.hh"
#include "FbTk/StringUtil.hh"
2002-08-11 22:35:40 +00:00
// use GNU extensions
2003-08-04 16:28:10 +00:00
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
2002-08-11 22:35:40 +00:00
#endif // _GNU_SOURCE
#ifdef HAVE_CONFIG_H
2002-10-13 21:52:00 +00:00
#include "config.h"
2002-08-11 22:35:40 +00:00
#endif // HAVE_CONFIG_H
2001-12-11 20:47:02 +00:00
2002-01-20 02:08:12 +00:00
#include <X11/Xlib.h>
#include <X11/Xatom.h>
2004-08-31 15:26:40 +00:00
#ifdef HAVE_CSTDIO
#include <cstdio>
#else
#include <stdio.h>
#endif
#ifdef HAVE_CSTRING
#include <cstring>
#else
#include <string.h>
#endif
2002-08-11 22:35:40 +00:00
2002-02-08 14:06:35 +00:00
#include <algorithm>
2002-02-10 19:05:12 +00:00
#include <iostream>
2003-04-14 15:01:55 +00:00
#include <iterator>
2002-08-11 22:35:40 +00:00
2002-02-10 19:05:12 +00:00
using namespace std;
2002-02-08 14:06:35 +00:00
namespace { // anonymous
int countTransients(const WinClient &client) {
if (client.transientList().empty())
2002-12-01 13:42:15 +00:00
return 0;
// now go throu the entire tree and count transients
size_t ret = client.transientList().size();
WinClient::TransientList::const_iterator it = client.transientList().begin();
WinClient::TransientList::const_iterator it_end = client.transientList().end();
2002-12-01 13:42:15 +00:00
for (; it != it_end; ++it)
ret += countTransients(*(*it));
return ret;
}
2003-04-14 15:01:55 +00:00
class ClientMenuItem:public FbTk::MenuItem {
public:
ClientMenuItem(WinClient &client):
FbTk::MenuItem(client.title().c_str(), &client.screen().windowMenu()),
m_client(client) {
2003-04-14 15:01:55 +00:00
}
FbTk::Menu *submenu() { return &m_client.screen().windowMenu(); }
const FbTk::Menu *submenu() const { return &m_client.screen().windowMenu(); }
void showSubmenu() {
WindowCmd<void>::setWindow(m_client.fbwindow());
FbTk::MenuItem::showSubmenu();
}
2003-04-14 15:01:55 +00:00
void click(int button, int time) {
if (m_client.fbwindow() == 0)
return;
FluxboxWindow &win = *m_client.fbwindow();
2005-04-23 10:03:06 +00:00
// fetch the window to the current workspace
if (button == 2 && win.screen().currentWorkspaceID() != win.workspaceNumber()) {
win.menu().hide();
win.screen().sendToWorkspace(win.screen().currentWorkspaceID(), &win, true);
} else { // warp to the workspace of the window
win.screen().changeWorkspaceID(win.workspaceNumber());
win.setCurrentClient(m_client);
win.raiseAndFocus();
}
2003-04-14 15:01:55 +00:00
}
const std::string &label() const { return m_client.title(); }
bool isSelected() const {
if (m_client.fbwindow() == 0)
return false;
if (m_client.fbwindow()->isFocused() == false)
return false;
return (&(m_client.fbwindow()->winClient()) == &m_client);
}
private:
2003-04-14 15:01:55 +00:00
WinClient &m_client;
};
};
2002-08-11 22:35:40 +00:00
Workspace::GroupList Workspace::m_groups;
2002-05-07 13:57:09 +00:00
Workspace::Workspace(BScreen &scrn, FbTk::MultLayers &layermanager,
const std::string &name, unsigned int id):
2003-05-11 15:35:03 +00:00
m_screen(scrn),
2003-05-11 17:14:41 +00:00
m_lastfocus(0),
2003-12-18 18:03:23 +00:00
m_clientmenu(scrn.menuTheme(), scrn.imageControl(),
*scrn.layerManager().getLayer(Fluxbox::instance()->getMenuLayer())),
m_layermanager(layermanager),
m_name(name),
m_id(id) {
2001-12-11 20:47:02 +00:00
2003-12-18 18:03:23 +00:00
menu().setInternalMenu();
setName(name);
2001-12-11 20:47:02 +00:00
}
2002-01-20 02:08:12 +00:00
Workspace::~Workspace() {
2001-12-11 20:47:02 +00:00
}
2002-09-21 16:02:22 +00:00
void Workspace::setLastFocusedWindow(FluxboxWindow *win) {
2002-12-01 13:42:15 +00:00
// make sure we have this window in the list
if (std::find(m_windowlist.begin(), m_windowlist.end(), win) != m_windowlist.end())
2003-05-11 17:14:41 +00:00
m_lastfocus = win;
2002-12-01 13:42:15 +00:00
else
2003-05-11 17:14:41 +00:00
m_lastfocus = 0;
2002-09-21 16:02:22 +00:00
}
2001-12-11 20:47:02 +00:00
2003-06-24 14:57:54 +00:00
void Workspace::addWindow(FluxboxWindow &w, bool place) {
2003-04-14 15:01:55 +00:00
// we don't need to add a window that already exist in our list
2003-12-30 18:16:51 +00:00
if (find(m_windowlist.begin(), m_windowlist.end(), &w) != m_windowlist.end())
2003-06-24 14:57:54 +00:00
return;
2002-12-01 13:42:15 +00:00
2003-04-14 15:01:55 +00:00
w.setWorkspace(m_id);
2003-12-07 17:49:07 +00:00
// attach signals
w.titleSig().attach(this);
2003-04-14 15:01:55 +00:00
2002-12-01 13:42:15 +00:00
if (place)
placeWindow(w);
2003-04-14 15:01:55 +00:00
m_windowlist.push_back(&w);
updateClientmenu();
if (!w.isStuck()) {
FluxboxWindow::ClientList::iterator client_it =
w.clientList().begin();
FluxboxWindow::ClientList::iterator client_it_end =
w.clientList().end();
for (; client_it != client_it_end; ++client_it)
2003-05-11 15:35:03 +00:00
screen().updateNetizenWindowAdd((*client_it)->window(), m_id);
}
2001-12-11 20:47:02 +00:00
}
2004-03-21 09:00:25 +00:00
// still_alive is true if the window will continue to exist after
// this event. Particularly, this isn't the removeWindow for
// the destruction of the window. Because if so, the focus revert
// is done in another place
int Workspace::removeWindow(FluxboxWindow *w, bool still_alive) {
2003-04-14 15:01:55 +00:00
2002-12-01 13:42:15 +00:00
if (w == 0)
return -1;
2003-12-07 17:49:07 +00:00
// detach from signals
w->titleSig().detach(this);
2003-05-11 17:14:41 +00:00
if (m_lastfocus == w) {
m_lastfocus = 0;
2002-12-01 13:42:15 +00:00
}
2004-03-21 09:00:25 +00:00
if (w->isFocused() && still_alive)
FocusControl::unfocusWindow(w->winClient(), true, true);
2001-12-11 20:47:02 +00:00
// we don't remove it from the layermanager, as it may be being moved
2003-04-14 15:01:55 +00:00
Windows::iterator erase_it = remove(m_windowlist.begin(),
m_windowlist.end(), w);
if (erase_it != m_windowlist.end())
m_windowlist.erase(erase_it);
2001-12-11 20:47:02 +00:00
updateClientmenu();
2003-04-14 15:01:55 +00:00
2003-05-11 17:14:41 +00:00
if (m_lastfocus == w || m_windowlist.empty())
m_lastfocus = 0;
2003-04-14 15:01:55 +00:00
2003-08-19 16:16:28 +00:00
if (!w->isStuck()) {
FluxboxWindow::ClientList::iterator client_it =
w->clientList().begin();
FluxboxWindow::ClientList::iterator client_it_end =
w->clientList().end();
for (; client_it != client_it_end; ++client_it)
2003-05-11 15:35:03 +00:00
screen().updateNetizenWindowDel((*client_it)->window());
}
2002-12-01 13:42:15 +00:00
return m_windowlist.size();
2001-12-11 20:47:02 +00:00
}
2003-01-13 12:59:26 +00:00
void Workspace::showAll() {
Windows::iterator it = m_windowlist.begin();
Windows::iterator it_end = m_windowlist.end();
2003-08-24 11:19:45 +00:00
for (; it != it_end; ++it)
2002-12-01 13:42:15 +00:00
(*it)->deiconify(false, false);
2001-12-11 20:47:02 +00:00
}
void Workspace::hideAll(bool interrupt_moving) {
Windows::reverse_iterator it = m_windowlist.rbegin();
Windows::reverse_iterator it_end = m_windowlist.rend();
2002-12-01 13:42:15 +00:00
for (; it != it_end; ++it) {
if (! (*it)->isStuck())
(*it)->withdraw(interrupt_moving);
2002-12-01 13:42:15 +00:00
}
2001-12-11 20:47:02 +00:00
}
2003-01-13 12:59:26 +00:00
void Workspace::removeAll() {
2002-12-01 13:42:15 +00:00
Windows::iterator it = m_windowlist.begin();
Windows::const_iterator it_end = m_windowlist.end();
2003-08-24 11:19:45 +00:00
for (; it != it_end; ++it)
2002-12-01 13:42:15 +00:00
(*it)->iconify();
2001-12-11 20:47:02 +00:00
}
2002-11-03 15:02:21 +00:00
void Workspace::reconfigure() {
2003-12-18 18:03:23 +00:00
menu().reconfigure();
2002-12-01 13:42:15 +00:00
Windows::iterator it = m_windowlist.begin();
Windows::iterator it_end = m_windowlist.end();
for (; it != it_end; ++it) {
if ((*it)->winClient().validateClient())
2002-12-01 13:42:15 +00:00
(*it)->reconfigure();
}
2001-12-11 20:47:02 +00:00
}
2003-05-11 17:14:41 +00:00
int Workspace::numberOfWindows() const {
2002-12-01 13:42:15 +00:00
return m_windowlist.size();
2001-12-11 20:47:02 +00:00
}
2002-08-11 22:35:40 +00:00
namespace {
// helper class for checkGrouping
class FindInGroup {
public:
2002-12-01 13:42:15 +00:00
FindInGroup(const FluxboxWindow &w):m_w(w) { }
2003-08-24 11:19:45 +00:00
bool operator ()(const string &name) const {
2003-06-15 19:34:34 +00:00
return (name == m_w.winClient().getWMClassName());
2002-12-01 13:42:15 +00:00
}
2002-08-11 22:35:40 +00:00
private:
2002-12-01 13:42:15 +00:00
const FluxboxWindow &m_w;
2002-08-11 22:35:40 +00:00
};
};
//Note: this function doesn't check if the window is groupable
bool Workspace::checkGrouping(FluxboxWindow &win) {
2003-06-15 19:34:34 +00:00
if (win.numClients() == 0)
return false;
2002-08-11 22:35:40 +00:00
#ifdef DEBUG
2003-06-15 19:34:34 +00:00
cerr<<__FILE__<<"("<<__LINE__<<"): Checking grouping. ("<<win.title()<<")"<<endl;
2002-08-11 22:35:40 +00:00
#endif // DEBUG
2002-12-01 13:42:15 +00:00
if (!win.isGroupable()) { // make sure this window can hold a tab
2002-10-22 14:47:22 +00:00
#ifdef DEBUG
2002-12-01 13:42:15 +00:00
cerr<<__FILE__<<"("<<__LINE__<<"): window can't use a tab"<<endl;
2002-10-22 14:47:22 +00:00
#endif // DEBUG
return false;
2002-12-01 13:42:15 +00:00
}
2003-06-15 19:34:34 +00:00
string instance_name = win.winClient().getWMClassName();
// go through every group and search for matching win instancename
2002-12-01 13:42:15 +00:00
GroupList::iterator g(m_groups.begin());
GroupList::iterator g_end(m_groups.end());
for (; g != g_end; ++g) {
Group::iterator name((*g).begin());
Group::iterator name_end((*g).end());
for (; name != name_end; ++name) {
2003-06-15 19:34:34 +00:00
if ((*name) != instance_name)
2002-12-01 13:42:15 +00:00
continue;
// find a window with the specific name
Windows::iterator wit(m_windowlist.begin());
Windows::iterator wit_end(m_windowlist.end());
for (; wit != wit_end; ++wit) {
2002-08-11 22:35:40 +00:00
#ifdef DEBUG
2003-06-15 19:34:34 +00:00
cerr<<__FILE__<<" check group with : "<<(*wit)->winClient().getWMClassName()<<endl;
2002-08-11 22:35:40 +00:00
#endif // DEBUG
if (find_if((*g).begin(),
(*g).end(),
FindInGroup(*(*wit))) != (*g).end()) {
2002-12-01 13:42:15 +00:00
// make sure the window is groupable
// and don't group with ourself
if ( !(*wit)->isGroupable() || (*wit)->winClient().fbwindow() == &win)
2002-12-01 13:42:15 +00:00
break; // try next name
#ifdef DEBUG
cerr<<__FILE__<<"("<<__FUNCTION__<<"): window ("<<*wit<<") attaching window ("<<&win<<")"<<endl;
#endif // DEBUG
(*wit)->attachClient(win.winClient());
return true; // grouping done
2003-06-15 19:34:34 +00:00
2002-12-01 13:42:15 +00:00
}
2003-06-15 19:34:34 +00:00
2002-12-01 13:42:15 +00:00
}
}
}
return false;
2002-08-11 22:35:40 +00:00
}
bool Workspace::loadGroups(const std::string &filename) {
2004-02-20 09:29:07 +00:00
string real_filename = FbTk::StringUtil::expandFilename(filename);
FbTk::StringUtil::removeTrailingWhitespace(real_filename);
ifstream infile(real_filename.c_str());
2002-12-01 13:42:15 +00:00
if (!infile)
return false;
2002-12-01 13:42:15 +00:00
m_groups.clear(); // erase old groups
// load new groups
while (!infile.eof()) {
string line;
vector<string> names;
getline(infile, line);
2003-04-26 18:58:30 +00:00
FbTk::StringUtil::stringtok(names, line);
2002-12-01 13:42:15 +00:00
m_groups.push_back(names);
}
2002-08-11 22:35:40 +00:00
2002-12-01 13:42:15 +00:00
return true;
2002-08-11 22:35:40 +00:00
}
2003-12-07 17:49:07 +00:00
void Workspace::update(FbTk::Subject *subj) {
menu().updateMenu();
2001-12-11 20:47:02 +00:00
}
2002-08-11 22:35:40 +00:00
void Workspace::setName(const std::string &name) {
if (!name.empty() && name != "") {
2002-12-01 13:42:15 +00:00
m_name = name;
} else { //if name == 0 then set default name from nls
2004-06-07 11:46:05 +00:00
_FB_USES_NLS;
2002-12-01 13:42:15 +00:00
char tname[128];
2004-06-07 11:46:05 +00:00
sprintf(tname,
_FBTEXT(Workspace, DefaultNameFormat,
"Workspace %d", "Default workspace names, with a %d for the workspace number"),
m_id + 1); //m_id starts at 0
2002-12-01 13:42:15 +00:00
m_name = tname;
}
2003-05-11 15:35:03 +00:00
screen().updateWorkspaceNamesAtom();
2001-12-11 20:47:02 +00:00
2003-12-18 18:03:23 +00:00
menu().setLabel(m_name.c_str());
menu().updateMenu();
2001-12-11 20:47:02 +00:00
}
2003-01-13 12:59:26 +00:00
/**
Calls restore on all windows
on the workspace and then
clears the m_windowlist
*/
void Workspace::shutdown() {
2002-12-01 13:42:15 +00:00
// note: when the window dies it'll remove it self from the list
while (!m_windowlist.empty()) {
2003-04-14 15:01:55 +00:00
// restore with remap on all clients in that window
m_windowlist.back()->restore(true);
//delete window (the window removes it self from m_windowlist)
delete m_windowlist.back();
2002-12-01 13:42:15 +00:00
}
2001-12-11 20:47:02 +00:00
}
void Workspace::updateClientmenu() {
2003-04-14 15:01:55 +00:00
// remove all items and then add them again
2003-12-18 18:03:23 +00:00
menu().removeAll();
2003-04-14 15:01:55 +00:00
// for each fluxboxwindow add every client in them to our clientlist
Windows::iterator win_it = m_windowlist.begin();
Windows::iterator win_it_end = m_windowlist.end();
for (; win_it != win_it_end; ++win_it) {
2003-04-14 15:01:55 +00:00
// add every client in this fluxboxwindow to menu
FluxboxWindow::ClientList::iterator client_it =
(*win_it)->clientList().begin();
FluxboxWindow::ClientList::iterator client_it_end =
(*win_it)->clientList().end();
2003-04-15 12:22:52 +00:00
for (; client_it != client_it_end; ++client_it)
2003-12-18 18:03:23 +00:00
menu().insert(new ClientMenuItem(*(*client_it)));
}
2003-04-15 12:22:52 +00:00
menu().updateMenu();
}
2001-12-11 20:47:02 +00:00
2003-04-14 15:01:55 +00:00
void Workspace::placeWindow(FluxboxWindow &win) {
int place_x, place_y;
// we ignore the return value,
// the screen placement strategy is guaranteed to succeed.
screen().placementStrategy().placeWindow(m_windowlist,
win,
place_x, place_y);
2003-04-14 15:01:55 +00:00
2003-05-11 13:36:12 +00:00
win.moveResize(place_x, place_y, win.width(), win.height());
2001-12-11 20:47:02 +00:00
}