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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
// IconbarTool.cc
// 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.
#include "IconbarTool.hh"
#include "fluxbox.hh"
#include "WindowCmd.hh"
#include "Screen.hh"
#include "IconbarTheme.hh"
#include "Window.hh"
#include "IconButton.hh"
#include "Workspace.hh"
#include "FbMenu.hh"
#include "FbTk/CommandParser.hh"
#include "WinClient.hh"
#include "FocusControl.hh"
#include "FbCommands.hh"
#include "Layer.hh"
#include "Debug.hh"
#include "FbTk/STLUtil.hh"
#include "FbTk/I18n.hh"
#include "FbTk/Menu.hh"
#include "FbTk/RadioMenuItem.hh"
#include "FbTk/BoolMenuItem.hh"
#include "FbTk/RefCount.hh"
#include "FbTk/SimpleCommand.hh"
#include "FbTk/ImageControl.hh"
#include "FbTk/MacroCommand.hh"
#include "FbTk/MenuSeparator.hh"
#include "FbTk/Util.hh"
#include "FbTk/STLUtil.hh"
#include "FbTk/Select2nd.hh"
#include "FbTk/Compose.hh"
#include <typeinfo>
#include <iterator>
#ifdef HAVE_CSTRING
#include <cstring>
#else
#include <string.h>
#endif
using std::string;
using std::list;
using std::endl;
namespace FbTk {
template<>
void FbTk::Resource<FbTk::Container::Alignment>::setDefaultValue() {
m_value = FbTk::Container::RELATIVE;
}
template<>
string FbTk::Resource<FbTk::Container::Alignment>::getString() const {
if (m_value == FbTk::Container::LEFT)
return string("Left");
if (m_value == FbTk::Container::RIGHT)
return string("Right");
return string("Relative");
}
template<>
void FbTk::Resource<FbTk::Container::Alignment>::setFromString(const char *str) {
if (strcasecmp(str, "Left") == 0)
m_value = FbTk::Container::LEFT;
else if (strcasecmp(str, "Right") == 0)
m_value = FbTk::Container::RIGHT;
else if (strcasecmp(str, "Relative") == 0)
m_value = FbTk::Container::RELATIVE;
else
setDefaultValue();
}
} // end namespace FbTk
namespace {
class ToolbarModeMenuItem : public FbTk::RadioMenuItem {
public:
ToolbarModeMenuItem(const FbTk::FbString &label, IconbarTool &handler,
string mode,
FbTk::RefCount<FbTk::Command<void> > &cmd):
FbTk::RadioMenuItem(label, cmd), m_handler(handler), m_mode(mode) {
setCloseOnClick(false);
}
bool isSelected() const { return m_handler.mode() == m_mode; }
void click(int button, int time, unsigned int mods) {
m_handler.setMode(m_mode);
FbTk::RadioMenuItem::click(button, time, mods);
}
private:
IconbarTool &m_handler;
string m_mode;
};
class ToolbarAlignMenuItem: public FbTk::RadioMenuItem {
public:
ToolbarAlignMenuItem(const FbTk::FbString &label, IconbarTool &handler,
FbTk::Container::Alignment mode,
FbTk::RefCount<FbTk::Command<void> > &cmd):
FbTk::RadioMenuItem(label, cmd), m_handler(handler), m_mode(mode) {
setCloseOnClick(false);
}
bool isSelected() const { return m_handler.alignment() == m_mode; }
void click(int button, int time, unsigned int mods) {
m_handler.setAlignment(m_mode);
FbTk::RadioMenuItem::click(button, time, mods);
}
private:
IconbarTool &m_handler;
FbTk::Container::Alignment m_mode;
};
void setupModeMenu(FbTk::Menu &menu, IconbarTool &handler) {
using namespace FbTk;
_FB_USES_NLS;
menu.setLabel(_FB_XTEXT(Toolbar, IconbarMode, "Iconbar Mode", "Menu title - chooses which set of icons are shown in the iconbar"));
RefCount<Command<void> > saverc_cmd(new FbCommands::SaveResources());
menu.insert(new ToolbarModeMenuItem(_FB_XTEXT(Toolbar, IconbarModeNone,
"None", "No icons are shown in the iconbar"),
handler,
"none", saverc_cmd));
menu.insert(new ToolbarModeMenuItem(
_FB_XTEXT(Toolbar, IconbarModeIcons,
"Icons", "Iconified windows from all workspaces are shown"),
handler,
"{static groups} (minimized=yes)", saverc_cmd));
menu.insert(new ToolbarModeMenuItem(
_FB_XTEXT(Toolbar, IconbarModeNoIcons,
"NoIcons", "No iconified windows from all workspaces are shown"),
handler,
"{static groups} (minimized=no)", saverc_cmd));
menu.insert(new ToolbarModeMenuItem(
_FB_XTEXT(Toolbar, IconbarModeWorkspaceIcons,
"WorkspaceIcons", "Iconified windows from this workspace are shown"),
handler,
"{static groups} (minimized=yes) (workspace)", saverc_cmd));
menu.insert(new ToolbarModeMenuItem(
_FB_XTEXT(Toolbar, IconbarModeWorkspaceNoIcons,
"WorkspaceNoIcons", "No iconified windows from this workspace are shown"),
handler,
"{static groups} (minimized=no) (workspace)", saverc_cmd));
menu.insert(new ToolbarModeMenuItem(
_FB_XTEXT(Toolbar, IconbarModeWorkspace,
"Workspace", "Normal and iconified windows from this workspace are shown"),
handler,
"{static groups} (workspace)", saverc_cmd));
menu.insert(new ToolbarModeMenuItem(
_FB_XTEXT(Toolbar, IconbarModeAllWindows, "All Windows", "All windows are shown"),
handler,
"{static groups}", saverc_cmd));
menu.insert(new FbTk::MenuSeparator());
menu.insert(new ToolbarAlignMenuItem(
_FB_XTEXT(Align, Left, "Left", "Align to the left"),
handler,
FbTk::Container::LEFT, saverc_cmd));
menu.insert(new ToolbarAlignMenuItem(
_FB_XTEXT(Align, Relative, "Relative", "Align relative to the width"),
handler,
FbTk::Container::RELATIVE, saverc_cmd));
menu.insert(new ToolbarAlignMenuItem(
_FB_XTEXT(Align, Right, "Right", "Align to the right"),
handler,
FbTk::Container::RIGHT, saverc_cmd));
menu.insert(new FbTk::MenuSeparator());
menu.updateMenu();
}
typedef FbTk::RefCount<FbTk::Command<void> > RefCmd;
class ShowMenu: public FbTk::Command<void> {
public:
explicit ShowMenu(FluxboxWindow &win):m_win(win) { }
void execute() {
// get last button pos
const XEvent &e = Fluxbox::instance()->lastEvent();
m_win.popupMenu(e.xbutton.x_root, e.xbutton.y_root);
}
private:
FluxboxWindow &m_win;
};
class FocusCommand: public FbTk::Command<void> {
public:
explicit FocusCommand(Focusable &win): m_win(win) { }
void execute() {
// this needs to be a local variable, as this object could be destroyed
// if the workspace is changed.
FluxboxWindow *fbwin = m_win.fbwindow();
if (!fbwin)
return;
if (m_win.isFocused())
fbwin->iconify();
else {
m_win.focus();
fbwin->raise();
}
}
private:
Focusable &m_win;
};
} // end anonymous namespace
IconbarTool::IconbarTool(const FbTk::FbWindow &parent, IconbarTheme &theme,
FbTk::ThemeProxy<IconbarTheme> &focused_theme,
FbTk::ThemeProxy<IconbarTheme> &unfocused_theme,
BScreen &screen, FbTk::Menu &menu):
ToolbarItem(ToolbarItem::RELATIVE),
m_screen(screen),
m_icon_container(parent, false),
m_theme(theme),
m_focused_theme(focused_theme),
m_unfocused_theme(unfocused_theme),
m_empty_pm( screen.imageControl() ),
m_winlist(new FocusableList(screen)),
m_mode("none"),
m_rc_mode(screen.resourceManager(), "{static groups} (workspace)",
screen.name() + ".iconbar.mode", screen.altName() + ".Iconbar.Mode"),
m_rc_alignment(screen.resourceManager(), FbTk::Container::RELATIVE,
screen.name() + ".iconbar.alignment", screen.altName() + ".Iconbar.Alignment"),
m_rc_client_width(screen.resourceManager(), 128,
screen.name() + ".iconbar.iconWidth", screen.altName() + ".Iconbar.IconWidth"),
m_rc_client_padding(screen.resourceManager(), 10,
screen.name() + ".iconbar.iconTextPadding", screen.altName() + ".Iconbar.IconTextPadding"),
m_rc_use_pixmap(screen.resourceManager(), true,
screen.name() + ".iconbar.usePixmap", screen.altName() + ".Iconbar.UsePixmap"),
m_menu(screen.menuTheme(), screen.imageControl(),
*screen.layerManager().getLayer(ResourceLayer::MENU)),
m_alpha(255) {
// setup mode menu
setupModeMenu(m_menu, *this);
_FB_USES_NLS;
using namespace FbTk;
// setup use pixmap item to reconfig iconbar and save resource on click
MacroCommand *save_and_reconfig = new MacroCommand();
RefCount<Command<void> > reconfig(new SimpleCommand<IconbarTool>(*this, &IconbarTool::renderTheme));
RefCount<Command<void> > save(FbTk::CommandParser<void>::instance().parse("saverc"));
save_and_reconfig->add(reconfig);
save_and_reconfig->add(save);
RefCount<Command<void> > s_and_reconfig(save_and_reconfig);
m_menu.insert(new FbTk::BoolMenuItem(_FB_XTEXT(Toolbar, ShowIcons,
"Show Pictures", "chooses if little icons are shown next to title in the iconbar"),
m_rc_use_pixmap, s_and_reconfig));
m_menu.updateMenu();
// must be internal menu, otherwise toolbar main menu tries to delete it.
m_menu.setInternalMenu();
// add iconbar menu to toolbar menu
menu.insert(m_menu.label().logical(), &m_menu);
// setup signals
theme.reconfigSig().attach(this);
focused_theme.reconfigSig().attach(this);
unfocused_theme.reconfigSig().attach(this);
setMode(*m_rc_mode);
}
IconbarTool::~IconbarTool() {
deleteIcons();
}
void IconbarTool::move(int x, int y) {
m_icon_container.move(x, y);
}
void IconbarTool::resize(unsigned int width, unsigned int height) {
m_icon_container.resize(width, height);
m_icon_container.setMaxTotalSize(m_icon_container.orientation() == FbTk::ROT0 || m_icon_container.orientation() == FbTk::ROT180 ? width : height);
renderTheme();
}
void IconbarTool::moveResize(int x, int y,
unsigned int width, unsigned int height) {
m_icon_container.moveResize(x, y, width, height);
m_icon_container.setMaxTotalSize(m_icon_container.orientation() == FbTk::ROT0 || m_icon_container.orientation() == FbTk::ROT180 ? width : height);
renderTheme();
}
void IconbarTool::show() {
m_icon_container.show();
}
void IconbarTool::hide() {
m_icon_container.hide();
}
void IconbarTool::setAlignment(FbTk::Container::Alignment align) {
*m_rc_alignment = align;
update(0);
m_menu.reconfigure();
}
void IconbarTool::setMode(string mode) {
if (mode == m_mode)
return;
*m_rc_mode = m_mode = mode;
// lock graphics update
m_icon_container.setUpdateLock(true);
if (m_winlist.get()) {
m_winlist->addSig().detach(this);
m_winlist->removeSig().detach(this);
m_winlist->orderSig().detach(this);
m_winlist->resetSig().detach(this);
}
if (mode == "none")
m_winlist.reset(new FocusableList(m_screen));
else
m_winlist.reset(new FocusableList(m_screen,
mode + " (iconhidden=no)"));
if (m_winlist.get()) {
m_winlist->addSig().attach(this);
m_winlist->removeSig().attach(this);
m_winlist->orderSig().attach(this);
m_winlist->resetSig().attach(this);
}
reset();
// unlock graphics update
m_icon_container.setUpdateLock(false);
m_icon_container.update();
m_icon_container.showSubwindows();
renderTheme();
m_menu.reconfigure();
}
unsigned int IconbarTool::width() const {
return m_icon_container.width();
}
unsigned int IconbarTool::height() const {
return m_icon_container.height();
}
unsigned int IconbarTool::borderWidth() const {
return m_icon_container.borderWidth();
}
void IconbarTool::update(FbTk::Subject *subj) {
// ignore updates if we're shutting down
if (m_screen.isShuttingdown()) {
if (!m_icons.empty())
deleteIcons();
return;
}
m_icon_container.setAlignment(*m_rc_alignment);
*m_rc_client_width = FbTk::Util::clamp(*m_rc_client_width, 10, 400);
m_icon_container.setMaxSizePerClient(*m_rc_client_width);
if (subj == &m_focused_theme.reconfigSig() ||
subj == &m_unfocused_theme.reconfigSig() ||
subj == &m_theme.reconfigSig()) {
setMode(*m_rc_mode);
return;
}
// lock graphic update
m_icon_container.setUpdateLock(true);
if (subj && typeid(*subj) == typeid(FocusableList::FocusableListSubject)) {
FocusableList::FocusableListSubject *fsubj =
static_cast<FocusableList::FocusableListSubject *>(subj);
if (subj == &m_winlist->addSig())
insertWindow(*fsubj->win());
else if (subj == &m_winlist->removeSig())
removeWindow(*fsubj->win());
else if (subj == &m_winlist->resetSig())
reset();
else if (subj == &m_winlist->orderSig())
insertWindow(*fsubj->win());
}
// unlock container and update graphics
m_icon_container.setUpdateLock(false);
m_icon_container.update();
m_icon_container.showSubwindows();
// another renderTheme we hopefully shouldn't need? These renders
// should be done individually above
// nope, we still need it (or at least I'm not bothering to fix it yet)
// a new IconButton doesn't get resized properly until the
// m_icon_container.update() above; then, it never runs drawText() again,
// so text can end up behind program icons
renderTheme();
}
void IconbarTool::insertWindow(Focusable &win, int pos) {
IconButton *button = 0;
IconMap::iterator icon_it = m_icons.find(&win);
if (icon_it != m_icons.end())
button = icon_it->second;
if (button)
m_icon_container.removeItem(button);
else
button = makeButton(win);
if (!button) return;
if (pos == -2) {
pos = 0;
list<Focusable *>::iterator it = m_winlist->clientList().begin(),
it_end = m_winlist->clientList().end();
for (; it != it_end && *it != &win; ++it)
pos++;
}
m_icon_container.insertItem(button, pos);
}
void IconbarTool::reset() {
deleteIcons();
updateList();
}
void IconbarTool::updateSizing() {
m_icon_container.setBorderWidth(m_theme.border().width());
m_icon_container.setBorderColor(m_theme.border().color());
FbTk::STLUtil::forAll(m_icons,
FbTk::Compose(std::mem_fun(&IconButton::reconfigTheme),
FbTk::Select2nd<IconMap::value_type>()));
}
void IconbarTool::renderTheme(int alpha) {
m_alpha = alpha;
renderTheme();
}
void IconbarTool::renderTheme() {
// update button sizes before we get max width per client!
updateSizing();
// if we dont have any icons then we should render empty texture
if (!m_theme.emptyTexture().usePixmap()) {
m_empty_pm.reset( 0 );
m_icon_container.setBackgroundColor(m_theme.emptyTexture().color());
} else {
m_empty_pm.reset(m_screen.imageControl().
renderImage(m_icon_container.width(),
m_icon_container.height(),
m_theme.emptyTexture(), orientation()));
m_icon_container.setBackgroundPixmap(m_empty_pm);
}
m_icon_container.setAlpha(m_alpha);
// update buttons
IconMap::iterator icon_it = m_icons.begin();
const IconMap::iterator icon_it_end = m_icons.end();
for (; icon_it != icon_it_end; ++icon_it)
renderButton(*icon_it->second);
}
void IconbarTool::renderButton(IconButton &button, bool clear) {
button.setPixmap(*m_rc_use_pixmap);
button.setTextPadding(*m_rc_client_padding);
button.reconfigTheme();
if (clear)
button.clear(); // the clear also updates transparent
}
void IconbarTool::deleteIcons() {
m_icon_container.removeAll();
FbTk::STLUtil::destroyAndClearSecond(m_icons);
}
void IconbarTool::removeWindow(Focusable &win) {
// got window die signal, lets find and remove the window
IconMap::iterator it = m_icons.find(&win);
if (it == m_icons.end())
return;
fbdbg<<"IconbarTool::"<<__FUNCTION__<<"( 0x"<<&win<<" title = "<<win.title().logical()<<") found!"<<endl;
// remove from list and render theme again
IconButton *button = it->second;
m_icons.erase(it);
m_icon_container.removeItem(button);
delete button;
}
IconButton *IconbarTool::makeButton(Focusable &win) {
// we just want windows that have clients
FluxboxWindow *fbwin = win.fbwindow();
if (!fbwin || fbwin->clientList().empty())
return 0;
fbdbg<<"IconbarTool::addWindow(0x"<<&win<<" title = "<<win.title().logical()<<")"<<endl;
IconButton *button = new IconButton(m_icon_container, m_focused_theme,
m_unfocused_theme, win);
RefCmd focus_cmd(new ::FocusCommand(win));
RefCmd menu_cmd(new ::ShowMenu(*fbwin));
button->setOnClick(focus_cmd, 1);
button->setOnClick(menu_cmd, 3);
renderButton(*button, false); // update the attributes, but don't clear it
m_icons[&win] = button;
return button;
}
void IconbarTool::updateList() {
list<Focusable *>::iterator it = m_winlist->clientList().begin();
list<Focusable *>::iterator it_end = m_winlist->clientList().end();
for (; it != it_end; ++it) {
if ((*it)->fbwindow())
insertWindow(**it, -1);
}
renderTheme();
}
void IconbarTool::setOrientation(FbTk::Orientation orient) {
m_icon_container.setOrientation(orient);
ToolbarItem::setOrientation(orient);
}
|