/
Window.cc
4129 lines (3334 loc) · 135 KB
/
Window.cc
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Window.cc for Fluxbox Window Manager
// Copyright (c) 2001 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//
// Window.cc for Blackbox - an X11 Window manager
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.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 "Window.hh"
#include "CurrentWindowCmd.hh"
#include "WinClient.hh"
#include "fluxbox.hh"
#include "Keys.hh"
#include "Screen.hh"
#include "FbWinFrameTheme.hh"
#include "FbAtoms.hh"
#include "RootTheme.hh"
#include "Workspace.hh"
#include "FbWinFrame.hh"
#include "WinButton.hh"
#include "WinButtonTheme.hh"
#include "WindowCmd.hh"
#ifdef REMEMBER
#include "Remember.hh"
#endif
#include "MenuCreator.hh"
#include "FocusControl.hh"
#include "IconButton.hh"
#include "ScreenPlacement.hh"
#include "RectangleUtil.hh"
#include "Debug.hh"
#include "FbTk/StringUtil.hh"
#include "FbTk/Compose.hh"
#include "FbTk/CommandParser.hh"
#include "FbTk/EventManager.hh"
#include "FbTk/KeyUtil.hh"
#include "FbTk/SimpleCommand.hh"
#include "FbTk/Select2nd.hh"
#include "FbTk/MemFun.hh"
#ifdef SHAPE
#include <X11/extensions/shape.h>
#endif // SHAPE
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cassert>
#include <functional>
#include <algorithm>
using std::endl;
using std::string;
using std::vector;
using std::bind2nd;
using std::mem_fun;
using std::equal_to;
using std::max;
using std::swap;
using std::dec;
using std::hex;
using namespace FbTk;
namespace {
// X event scanner for enter/leave notifies - adapted from twm
typedef struct scanargs {
Window w;
Bool leave, inferior, enter;
} scanargs;
// look for valid enter or leave events (that may invalidate the earlier one we are interested in)
extern "C" int queueScanner(Display *, XEvent *e, char *args) {
if (e->type == LeaveNotify &&
e->xcrossing.window == ((scanargs *) args)->w &&
e->xcrossing.mode == NotifyNormal) {
((scanargs *) args)->leave = true;
((scanargs *) args)->inferior = (e->xcrossing.detail == NotifyInferior);
} else if (e->type == EnterNotify &&
e->xcrossing.mode == NotifyUngrab)
((scanargs *) args)->enter = true;
return false;
}
/// returns the deepest transientFor, asserting against a close loop
WinClient *getRootTransientFor(WinClient *client) {
while (client && client->transientFor()) {
assert(client != client->transientFor());
client = client->transientFor();
}
return client;
}
void callForAllTransient(FluxboxWindow& win, void (*callFunc)(FluxboxWindow&)) {
WinClient::TransientList::const_iterator it = win.winClient().transientList().begin();
WinClient::TransientList::const_iterator it_end = win.winClient().transientList().end();
for (; it != it_end; ++it) {
if ((*it)->fbwindow() && !(*it)->fbwindow()->isIconic())
// TODO: should we also check if it is the active client?
callFunc(*(*it)->fbwindow());
}
}
/// raise window and do the same for each transient of the current window
void raiseFluxboxWindow(FluxboxWindow &win) {
if (win.oplock)
return;
if (win.isIconic())
return;
win.oplock = true;
// we need to lock actual restacking so that raising above active transient
// won't do anything nasty
if (!win.winClient().transientList().empty())
win.screen().layerManager().lock();
win.layerItem().raise();
callForAllTransient(win, raiseFluxboxWindow);
win.oplock = false;
if (!win.winClient().transientList().empty())
win.screen().layerManager().unlock();
}
/// lower window and do the same for each transient it holds
void lowerFluxboxWindow(FluxboxWindow &win) {
if (win.oplock)
return;
if (win.isIconic())
return;
win.oplock = true;
// we need to lock actual restacking so that raising above active transient
// won't do anything nasty
if (!win.winClient().transientList().empty())
win.screen().layerManager().lock();
// lower the windows from the top down, so they don't change stacking order
const WinClient::TransientList& transients = win.winClient().transientList();
WinClient::TransientList::const_reverse_iterator it = transients.rbegin();
WinClient::TransientList::const_reverse_iterator it_end = transients.rend();
for (; it != it_end; ++it) {
if ((*it)->fbwindow() && !(*it)->fbwindow()->isIconic())
// TODO: should we also check if it is the active client?
lowerFluxboxWindow(*(*it)->fbwindow());
}
win.layerItem().lower();
win.oplock = false;
if (!win.winClient().transientList().empty())
win.screen().layerManager().unlock();
}
/// raise window and do the same for each transient it holds
void tempRaiseFluxboxWindow(FluxboxWindow &win) {
if (win.oplock) return;
win.oplock = true;
if (!win.isIconic()) {
win.layerItem().tempRaise();
}
callForAllTransient(win, tempRaiseFluxboxWindow);
win.oplock = false;
}
bool isWindowVisibleOnSomeHeadOrScreen(FluxboxWindow const& w) {
int real_x = w.frame().x();
int real_y = w.frame().y();
if (w.screen().hasXinerama()) { // xinerama available => use head info
return (0 != w.screen().getHead(real_x, real_y)); // if visible on some head
}
return RectangleUtil::insideRectangle(0, 0, w.screen().width(), w.screen().height(), real_x, real_y);
}
class SetClientCmd:public FbTk::Command<void> {
public:
explicit SetClientCmd(WinClient &client):m_client(client) { }
void execute() {
m_client.focus();
}
private:
WinClient &m_client;
};
/// helper class for some STL routines
class ChangeProperty {
public:
ChangeProperty(Display *disp, Atom prop, int mode,
unsigned char *state, int num):m_disp(disp),
m_prop(prop),
m_state(state),
m_num(num),
m_mode(mode){
}
void operator () (FbTk::FbWindow *win) {
XChangeProperty(m_disp, win->window(), m_prop, m_prop, 32, m_mode,
m_state, m_num);
}
private:
Display *m_disp;
Atom m_prop;
unsigned char *m_state;
int m_num;
int m_mode;
};
// Helper class for getResizeDirection below
// Tests whether a point is on an edge or the corner.
struct TestCornerHelper {
int corner_size_px, corner_size_pc;
bool operator()(int xy, int wh)
{
/* The % checking must be right: 0% must fail, 100% must succeed. */
return xy < corner_size_px || 100 * xy < corner_size_pc * wh;
}
};
// used in FluxboxWindow::updateAll() and FluxboxWindow::Setu
typedef FbTk::Resource<vector<WinButton::Type> > WBR;
// create a button ready to be used in the frame-titlebar
WinButton* makeButton(FluxboxWindow& win, FocusableTheme<WinButtonTheme>& btheme, WinButton::Type btype) {
FbTk::ThemeProxy<WinButtonTheme>& theme = win.screen().pressedWinButtonTheme();
FbWinFrame& frame = win.frame();
FbTk::FbWindow& parent = frame.titlebar();
const unsigned int h = frame.buttonHeight();
const unsigned int w = h;
return new WinButton(win, btheme, theme, btype, parent, 0, 0, w, h);
}
}
int FluxboxWindow::s_num_grabs = 0;
static int s_original_workspace = 0;
FluxboxWindow::FluxboxWindow(WinClient &client):
Focusable(client.screen(), this),
oplock(false),
m_creation_time(0),
moving(false), resizing(false),
m_initialized(false),
m_attaching_tab(0),
display(FbTk::App::instance()->display()),
m_button_grab_x(0), m_button_grab_y(0),
m_last_move_x(0), m_last_move_y(0),
m_last_resize_h(1), m_last_resize_w(1),
m_last_pressed_button(0),
m_workspace_number(0),
m_current_state(0),
m_old_decoration_mask(0),
m_client(&client),
m_toggled_decos(false),
m_focus_protection(Focus::NoProtection),
m_mouse_focus(BoolAcc(screen().focusControl(), &FocusControl::isMouseFocus)),
m_click_focus(true),
m_last_button_x(0), m_last_button_y(0),
m_button_theme(*this, screen().focusedWinButtonTheme(),
screen().unfocusedWinButtonTheme()),
m_theme(*this, screen().focusedWinFrameTheme(),
screen().unfocusedWinFrameTheme()),
m_frame(client.screen(), client.depth(), m_state, m_theme),
m_placed(false),
m_old_layernum(0),
m_parent(client.screen().rootWindow()),
m_resize_corner(RIGHTBOTTOM) {
join(m_theme.reconfigSig(), FbTk::MemFun(*this, &FluxboxWindow::themeReconfigured));
join(m_frame.frameExtentSig(), FbTk::MemFun(*this, &FluxboxWindow::frameExtentChanged));
init();
if (!isManaged())
return;
// add the window to the focus list
// always add to front on startup to keep the focus order the same
if (isFocused() || Fluxbox::instance()->isStartup())
screen().focusControl().addFocusWinFront(*this);
else
screen().focusControl().addFocusWinBack(*this);
Fluxbox::instance()->keys()->registerWindow(frame().window().window(),
*this, Keys::ON_WINDOW);
}
FluxboxWindow::~FluxboxWindow() {
if (WindowCmd<void>::window() == this)
WindowCmd<void>::setWindow(0);
if (FbMenu::window() == this)
FbMenu::setWindow(0);
if ( Fluxbox::instance()->keys() != 0 ) {
Fluxbox::instance()->keys()->
unregisterWindow(frame().window().window());
}
fbdbg << "starting ~FluxboxWindow(" << this << ","
<< (m_client ? m_client->title().logical().c_str() : "") << ")" << endl
<< "num clients = " << numClients() << endl
<< "curr client = "<< m_client << endl
<< "m_labelbuttons.size = " << m_labelbuttons.size() << endl;
if (moving)
stopMoving(true);
if (resizing)
stopResizing(true);
if (m_attaching_tab)
attachTo(0, 0, true);
// no longer a valid window to do stuff with
Fluxbox::instance()->removeWindowSearchGroup(frame().window().window());
Fluxbox::instance()->removeWindowSearchGroup(frame().tabcontainer().window());
Fluxbox::instance()->shortcutManager().removeWindow(this);
Client2ButtonMap::iterator it = m_labelbuttons.begin();
Client2ButtonMap::iterator it_end = m_labelbuttons.end();
for (; it != it_end; ++it)
frame().removeTab((*it).second);
m_labelbuttons.clear();
m_timer.stop();
m_tabActivationTimer.stop();
// notify die
dieSig().emit(*this);
if (m_client != 0 && !m_screen.isShuttingdown())
delete m_client; // this also removes client from our list
m_client = 0;
if (m_clientlist.size() > 1) {
fbdbg<<"(~FluxboxWindow()) WARNING! clientlist > 1"<<endl;
while (!m_clientlist.empty()) {
detachClient(*m_clientlist.back());
}
}
if (!screen().isShuttingdown())
screen().focusControl().removeWindow(*this);
fbdbg<<"~FluxboxWindow("<<this<<")"<<endl;
}
void FluxboxWindow::init() {
m_attaching_tab = 0;
// fetch client size and placement
XWindowAttributes wattrib;
if (! m_client->getAttrib(wattrib) ||
!wattrib.screen || // no screen? ??
wattrib.override_redirect || // override redirect
m_client->initial_state == WithdrawnState ||
m_client->getWMClassClass() == "DockApp") { // Slit client
return;
}
if (m_client->initial_state == IconicState)
m_state.iconic = true;
m_client->setFluxboxWindow(this);
m_client->setGroupLeftWindow(None); // nothing to the left.
if (Fluxbox::instance()->haveShape())
Shape::setShapeNotify(winClient());
//!! TODO init of client should be better
// we don't want to duplicate code here and in attachClient
m_clientlist.push_back(m_client);
fbdbg<<"FluxboxWindow::init(this="<<this<<", client="<<hex<<
m_client->window()<<", frame = "<<frame().window().window()<<dec<<")"<<endl;
Fluxbox &fluxbox = *Fluxbox::instance();
associateClient(*m_client);
frame().setFocusTitle(title());
// redirect events from frame to us
frame().setEventHandler(*this);
fluxbox.saveWindowSearchGroup(frame().window().window(), this);
fluxbox.saveWindowSearchGroup(frame().tabcontainer().window(), this);
m_workspace_number = m_screen.currentWorkspaceID();
// set default decorations but don't apply them
setDecorationMask(WindowState::getDecoMaskFromString(screen().defaultDeco()),
false);
functions.resize = functions.move = functions.iconify = functions.maximize
= functions.close = functions.tabable = true;
updateMWMHintsFromClient(*m_client);
m_timer.setTimeout(fluxbox.getAutoRaiseDelay() * FbTk::FbTime::IN_MILLISECONDS);
FbTk::RefCount<FbTk::Command<void> > raise_cmd(new FbTk::SimpleCommand<FluxboxWindow>(*this,
&FluxboxWindow::raise));
m_timer.setCommand(raise_cmd);
m_timer.fireOnce(true);
m_tabActivationTimer.setTimeout(fluxbox.getAutoRaiseDelay() * FbTk::FbTime::IN_MILLISECONDS);
FbTk::RefCount<ActivateTabCmd> activate_tab_cmd(new ActivateTabCmd());
m_tabActivationTimer.setCommand(activate_tab_cmd);
m_tabActivationTimer.fireOnce(true);
m_resizeTimer.setTimeout(screen().opaqueResizeDelay() * FbTk::FbTime::IN_MILLISECONDS);
FbTk::RefCount<FbTk::Command<void> > resize_cmd(new FbTk::SimpleCommand<FluxboxWindow>(*this,
&FluxboxWindow::updateResize));
m_resizeTimer.setCommand(resize_cmd);
m_resizeTimer.fireOnce(true);
m_reposLabels_timer.setTimeout(IconButton::updateLaziness());
m_reposLabels_timer.fireOnce(true);
FbTk::RefCount<FbTk::Command<void> > elrs(new FbTk::SimpleCommand<FluxboxWindow>(*this, &FluxboxWindow::emitLabelReposSig));
m_reposLabels_timer.setCommand(elrs);
/**************************************************/
/* Read state above here, apply state below here. */
/**************************************************/
if (m_client->isTransient() && m_client->transientFor()->fbwindow())
m_state.stuck = m_client->transientFor()->fbwindow()->isStuck();
if (!m_client->sizeHints().isResizable()) {
functions.resize = functions.maximize = false;
decorations.tab = false; //no tab for this window
}
associateClientWindow();
setWindowType(m_client->getWindowType());
auto sanitizePosition = [&]() {
int head = screen().getHead(fbWindow());
if (head == 0 && screen().hasXinerama())
head = screen().getCurrHead();
int left = screen().maxLeft(head), top = screen().maxTop(head),
btm = screen().maxBottom(head), rght = screen().maxRight(head);
const int margin = hasTitlebar() ? 32 : 8;
// ensure the window intersects with the workspace x-axis
if (int(frame().x() + frame().width()) < left) {
left += margin - frame().width();
} else if (frame().x() > rght) {
left = rght - margin;
} else {
left = frame().x();
}
if (hasTitlebar()) {
// ensure the titlebar is inside the workspace
top = std::max(top, std::min(frame().y(), btm - margin));
} else {
// ensure "something" is inside the workspace
if (int(frame().y() + frame().height()) < top)
top += margin - frame().height();
else if (frame().y() > btm)
top = btm - margin;
else
top = frame().y();
}
frame().move(left, top);
};
if (fluxbox.isStartup()) {
m_placed = true;
} else if (m_client->normal_hint_flags & (PPosition|USPosition)) {
m_placed = true;
sanitizePosition();
} else {
if (!isWindowVisibleOnSomeHeadOrScreen(*this)) {
// this probably should never happen, but if a window
// unexplicitly has its topleft corner outside any screen,
// move it to the current screen and ensure it's just placed
int cur = screen().getHead(fbWindow());
move(screen().getHeadX(cur), screen().getHeadY(cur));
m_placed = false; // allow placement strategy to fix position
}
setOnHead(screen().getCurrHead());
}
// we must do this now, or else resizing may not work properly
applyDecorations();
fluxbox.attachSignals(*this);
if (!m_state.fullscreen) {
unsigned int new_width = 0, new_height = 0;
if (m_client->width() >= screen().width()) {
m_state.maximized |= WindowState::MAX_HORZ;
new_width = 2 * screen().width() / 3;
}
if (m_client->height() >= screen().height()) {
m_state.maximized |= WindowState::MAX_VERT;
new_height = 2 * screen().height() / 3;
}
if (new_width || new_height) {
const int maximized = m_state.maximized;
m_state.maximized = WindowState::MAX_NONE;
resize(new_width ? new_width : width(), new_height ? new_height : height());
m_placed = false;
m_state.maximized = maximized;
}
}
// this window is managed, we are now allowed to modify actual state
m_initialized = true;
if (m_workspace_number >= screen().numberOfWorkspaces())
m_workspace_number = screen().currentWorkspaceID();
unsigned int real_width = frame().width();
unsigned int real_height = frame().height();
frame().applySizeHints(real_width, real_height);
// if we're a transient then we should be on the same layer and workspace
FluxboxWindow* twin = m_client->transientFor() ? m_client->transientFor()->fbwindow() : 0;
if (twin && twin != this) {
if (twin->layerNum() < ResourceLayer::DESKTOP) { // don't confine layer for desktops
layerItem().setLayer(twin->layerItem().getLayer());
m_state.layernum = twin->layerNum();
}
m_workspace_number = twin->workspaceNumber();
const int x = twin->frame().x() + int(twin->frame().width() - frame().width())/2;
const int y = twin->frame().y() + int(twin->frame().height() - frame().height())/2;
frame().move(x, y);
sanitizePosition();
m_placed = true;
} else // if no parent then set default layer
moveToLayer(m_state.layernum, m_state.layernum != ::ResourceLayer::NORMAL);
fbdbg<<"FluxboxWindow::init("<<title().logical()<<") transientFor: "<<
m_client->transientFor()<<endl;
if (twin) {
fbdbg<<"FluxboxWindow::init("<<title().logical()<<") transientFor->title(): "<<
twin->title().logical()<<endl;
}
screen().getWorkspace(m_workspace_number)->addWindow(*this);
if (m_placed)
moveResize(frame().x(), frame().y(), real_width, real_height);
else
placeWindow(getOnHead());
setFocusFlag(false); // update graphics before mapping
if (m_state.stuck) {
m_state.stuck = false;
stick();
}
if (m_state.shaded) { // start shaded
m_state.shaded = false;
shade();
}
if (m_state.iconic) {
m_state.iconic = false;
iconify();
} else if (m_workspace_number == screen().currentWorkspaceID()) {
m_state.iconic = true;
deiconify(false);
// check if we should prevent this window from gaining focus
m_focused = false; // deiconify sets this
if (!Fluxbox::instance()->isStartup() && isFocusNew()) {
Focus::Protection fp = m_focus_protection;
m_focus_protection &= ~Focus::Deny; // new windows run as "Refuse"
m_focused = focusRequestFromClient(*m_client);
m_focus_protection = fp;
if (!m_focused)
lower();
}
}
if (m_state.fullscreen) {
m_state.fullscreen = false;
setFullscreen(true);
}
if (m_state.maximized) {
int tmp = m_state.maximized;
m_state.maximized = WindowState::MAX_NONE;
setMaximizedState(tmp);
}
m_workspacesig.emit(*this);
m_creation_time = FbTk::FbTime::mono();
frame().frameExtentSig().emit();
setupWindow();
fluxbox.sync(false);
}
/// attach a client to this window and destroy old window
void FluxboxWindow::attachClient(WinClient &client, int x, int y) {
//!! TODO: check for isGroupable in client
if (client.fbwindow() == this)
return;
menu().hide();
// reparent client win to this frame
frame().setClientWindow(client);
bool was_focused = false;
WinClient *focused_win = 0;
// get the current window on the end of our client list
Window leftwin = None;
if (!clientList().empty())
leftwin = clientList().back()->window();
client.setGroupLeftWindow(leftwin);
if (client.fbwindow() != 0) {
FluxboxWindow *old_win = client.fbwindow(); // store old window
if (FocusControl::focusedFbWindow() == old_win)
was_focused = true;
ClientList::iterator client_insert_pos = getClientInsertPosition(x, y);
FbTk::TextButton *button_insert_pos = NULL;
if (client_insert_pos != m_clientlist.end())
button_insert_pos = m_labelbuttons[*client_insert_pos];
// make sure we set new window search for each client
ClientList::iterator client_it = old_win->clientList().begin();
ClientList::iterator client_it_end = old_win->clientList().end();
for (; client_it != client_it_end; ++client_it) {
// reparent window to this
frame().setClientWindow(**client_it);
moveResizeClient(**client_it);
// create a labelbutton for this client and
// associate it with the pointer
associateClient(*(*client_it));
//null if we want the new button at the end of the list
if (x >= 0 && button_insert_pos)
frame().moveLabelButtonLeftOf(*m_labelbuttons[*client_it], *button_insert_pos);
}
// add client and move over all attached clients
// from the old window to this list
m_clientlist.splice(client_insert_pos, old_win->m_clientlist);
updateClientLeftWindow();
old_win->m_client = 0;
delete old_win;
} else { // client.fbwindow() == 0
associateClient(client);
moveResizeClient(client);
// right now, this block only happens with new windows or on restart
bool is_startup = Fluxbox::instance()->isStartup();
// we use m_focused as a signal to focus the window when mapped
if (isFocusNew() && !is_startup)
m_focused = focusRequestFromClient(client);
focused_win = (isFocusNew() || is_startup) ? &client : m_client;
m_clientlist.push_back(&client);
}
// make sure that the state etc etc is updated for the new client
// TODO: one day these should probably be neatened to only act on the
// affected clients if possible
m_statesig.emit(*this);
m_workspacesig.emit(*this);
m_layersig.emit(*this);
if (was_focused) {
// don't ask me why, but client doesn't seem to keep focus in new window
// and we don't seem to get a FocusIn event from setInputFocus
client.focus();
FocusControl::setFocusedWindow(&client);
} else {
if (!focused_win)
focused_win = screen().focusControl().lastFocusedWindow(*this);
if (focused_win) {
setCurrentClient(*focused_win, false);
if (isIconic() && m_focused)
deiconify();
}
}
frame().reconfigure();
}
/// detach client from window and create a new window for it
bool FluxboxWindow::detachClient(WinClient &client) {
if (client.fbwindow() != this || numClients() <= 1)
return false;
Window leftwin = None;
ClientList::iterator client_it, client_it_after;
client_it = client_it_after =
find(clientList().begin(), clientList().end(), &client);
if (client_it != clientList().begin())
leftwin = (*(--client_it))->window();
if (++client_it_after != clientList().end())
(*client_it_after)->setGroupLeftWindow(leftwin);
removeClient(client);
screen().createWindow(client);
return true;
}
void FluxboxWindow::detachCurrentClient() {
// should only operate if we had more than one client
if (numClients() <= 1)
return;
WinClient &client = *m_client;
detachClient(*m_client);
if (client.fbwindow() != 0)
client.fbwindow()->show();
}
/// removes client from client list, does not create new fluxboxwindow for it
bool FluxboxWindow::removeClient(WinClient &client) {
if (client.fbwindow() != this || numClients() == 0)
return false;
fbdbg<<"("<<__FUNCTION__<<")["<<this<<"]"<<endl;
// if it is our active client, deal with it...
if (m_client == &client) {
WinClient *next_client = screen().focusControl().lastFocusedWindow(*this, m_client);
if (next_client != 0)
setCurrentClient(*next_client, false);
}
menu().hide();
m_clientlist.remove(&client);
if (m_client == &client) {
if (m_clientlist.empty())
m_client = 0;
else
// this really shouldn't happen
m_client = m_clientlist.back();
}
FbTk::EventManager &evm = *FbTk::EventManager::instance();
evm.remove(client.window());
IconButton *label_btn = m_labelbuttons[&client];
if (label_btn != 0) {
frame().removeTab(label_btn);
label_btn = 0;
}
m_labelbuttons.erase(&client);
frame().reconfigure();
updateClientLeftWindow();
fbdbg<<"("<<__FUNCTION__<<")["<<this<<"] numClients = "<<numClients()<<endl;
return true;
}
/// returns WinClient of window we're searching for
WinClient *FluxboxWindow::findClient(Window win) {
ClientList::iterator it = find_if(clientList().begin(),
clientList().end(),
Compose(bind2nd(equal_to<Window>(), win),
mem_fun(&WinClient::window)));
return (it == clientList().end() ? 0 : *it);
}
/// raise and focus next client
void FluxboxWindow::nextClient() {
if (numClients() <= 1)
return;
ClientList::iterator it = find(m_clientlist.begin(), m_clientlist.end(),
m_client);
if (it == m_clientlist.end())
return;
++it;
if (it == m_clientlist.end())
it = m_clientlist.begin();
setCurrentClient(**it, isFocused());
}
void FluxboxWindow::prevClient() {
if (numClients() <= 1)
return;
ClientList::iterator it = find(m_clientlist.begin(), m_clientlist.end(),
m_client);
if (it == m_clientlist.end())
return;
if (it == m_clientlist.begin())
it = m_clientlist.end();
--it;
setCurrentClient(**it, isFocused());
}
void FluxboxWindow::moveClientLeft() {
if (m_clientlist.size() == 1 ||
*m_clientlist.begin() == &winClient())
return;
// move client in clientlist to the left
ClientList::iterator oldpos = find(m_clientlist.begin(), m_clientlist.end(), &winClient());
ClientList::iterator newpos = oldpos; newpos--;
swap(*newpos, *oldpos);
frame().moveLabelButtonLeft(*m_labelbuttons[&winClient()]);
updateClientLeftWindow();
}
void FluxboxWindow::moveClientRight() {
if (m_clientlist.size() == 1 ||
*m_clientlist.rbegin() == &winClient())
return;
ClientList::iterator oldpos = find(m_clientlist.begin(), m_clientlist.end(), &winClient());
ClientList::iterator newpos = oldpos; newpos++;
swap(*newpos, *oldpos);
frame().moveLabelButtonRight(*m_labelbuttons[&winClient()]);
updateClientLeftWindow();
}
FluxboxWindow::ClientList::iterator FluxboxWindow::getClientInsertPosition(int x, int y) {
int dest_x = 0, dest_y = 0;
Window labelbutton = 0;
if (!XTranslateCoordinates(FbTk::App::instance()->display(),
parent().window(), frame().tabcontainer().window(),
x, y, &dest_x, &dest_y,
&labelbutton))
return m_clientlist.end();
WinClient* c = winClientOfLabelButtonWindow(labelbutton);
// label button not found
if (!c)
return m_clientlist.end();
Window child_return=0;
// make x and y relative to our labelbutton
if (!XTranslateCoordinates(FbTk::App::instance()->display(),
frame().tabcontainer().window(), labelbutton,
dest_x, dest_y, &x, &y,
&child_return))
return m_clientlist.end();
ClientList::iterator client = find(m_clientlist.begin(),
m_clientlist.end(),
c);
if (x > static_cast<signed>(m_labelbuttons[c]->width()) / 2)
client++;
return client;
}
void FluxboxWindow::moveClientTo(WinClient &win, int x, int y) {
int dest_x = 0, dest_y = 0;
Window labelbutton = 0;
if (!XTranslateCoordinates(FbTk::App::instance()->display(),
parent().window(), frame().tabcontainer().window(),
x, y, &dest_x, &dest_y,
&labelbutton))
return;
WinClient* client = winClientOfLabelButtonWindow(labelbutton);
if (!client)
return;
Window child_return = 0;
//make x and y relative to our labelbutton
if (!XTranslateCoordinates(FbTk::App::instance()->display(),
frame().tabcontainer().window(), labelbutton,
dest_x, dest_y, &x, &y,
&child_return))
return;
if (x > static_cast<signed>(m_labelbuttons[client]->width()) / 2)
moveClientRightOf(win, *client);
else
moveClientLeftOf(win, *client);
}
void FluxboxWindow::moveClientLeftOf(WinClient &win, WinClient &dest) {
frame().moveLabelButtonLeftOf(*m_labelbuttons[&win], *m_labelbuttons[&dest]);
ClientList::iterator it = find(m_clientlist.begin(),
m_clientlist.end(),
&win);
ClientList::iterator new_pos = find(m_clientlist.begin(),
m_clientlist.end(),
&dest);
// make sure we found them
if (it == m_clientlist.end() || new_pos==m_clientlist.end())
return;
//moving a button to the left of itself results in no change
if (new_pos == it)
return;
//remove from list
m_clientlist.erase(it);
//insert on the new place
m_clientlist.insert(new_pos, &win);
updateClientLeftWindow();
}
void FluxboxWindow::moveClientRightOf(WinClient &win, WinClient &dest) {
frame().moveLabelButtonRightOf(*m_labelbuttons[&win], *m_labelbuttons[&dest]);
ClientList::iterator it = find(m_clientlist.begin(),
m_clientlist.end(),
&win);
ClientList::iterator new_pos = find(m_clientlist.begin(),
m_clientlist.end(),
&dest);
// make sure we found them
if (it == m_clientlist.end() || new_pos==m_clientlist.end())
return;
//moving a button to the right of itself results in no change
if (new_pos == it)
return;
//remove from list
m_clientlist.erase(it);
//need to insert into the next position
new_pos++;
//insert on the new place
if (new_pos == m_clientlist.end())
m_clientlist.push_back(&win);