summaryrefslogtreecommitdiff
path: root/src/Tab.cc
blob: d8ebcfb4faf6ade8ee255eaeb2579366df88704a (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
// Tab.cc for Fluxbox Window Manager
// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.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: Tab.cc,v 1.44 2002/11/27 13:03:04 fluxgen Exp $

#include "Tab.hh"

#include "i18n.hh"
#include "DrawUtil.hh"
#include "Screen.hh"
#include "fluxbox.hh"
#include "Rootmenu.hh"

#include <iostream>
using namespace std;

bool Tab::m_stoptabs = false;
Tab::t_tabplacementlist Tab::m_tabplacementlist[] = {
		{PTOP, "Top"},
		{PBOTTOM, "Bottom"},
		{PLEFT, "Left"},
		{PRIGHT, "Right"},
		{PNONE, "none"}
	};

Tab::t_tabplacementlist Tab::m_tabalignmentlist[] = {
		{ALEFT, "Left"},
		{ACENTER, "Center"},
		{ARIGHT, "Right"},
		{ARELATIVE, "Relative"},
		{ANONE, "none"}
	};

Tab::Tab(FluxboxWindow *win, Tab *prev, Tab *next) { 
	//set default values
	
	m_focus = m_moving = false;
	m_configured = true; // only set to false before Fluxbox::reconfigure
	m_move_x = m_move_y = 0;	
	m_prev = prev; m_next = next; 
	m_win = win;
	m_display = BaseDisplay::getXDisplay();
	
	if ((m_win->getScreen()->getTabPlacement() == PLEFT ||
			m_win->getScreen()->getTabPlacement() == PRIGHT) &&
			m_win->getScreen()->isTabRotateVertical() &&
			!m_win->isShaded()) {
		m_size_w = m_win->getScreen()->getTabHeight();
		m_size_h = m_win->getScreen()->getTabWidth();
	} else {
		m_size_w = m_win->getScreen()->getTabWidth();
		m_size_h = m_win->getScreen()->getTabHeight();
	}

	createTabWindow();

	calcIncrease();
}

Tab::~Tab() {

	disconnect();	
	
	Fluxbox::instance()->removeTabSearch(m_tabwin);
	XDestroyWindow(m_display, m_tabwin);
}


//----------------  createTabWindow ---------------
// (private)
// Creates the Window for tab to be above the title window.
// This should only be called by the constructor.
//-------------------------------------------------
void Tab::createTabWindow() {
	unsigned long attrib_mask = CWBackPixmap | CWBackPixel | CWBorderPixel |
			CWColormap | CWOverrideRedirect | CWEventMask;
	XSetWindowAttributes attrib;
	attrib.background_pixmap = None;
	attrib.background_pixel = attrib.border_pixel =
		m_win->getScreen()->getWindowStyle()->tab.border_color.pixel();
	attrib.colormap = m_win->getScreen()->colormap();
	attrib.override_redirect = True;
	attrib.event_mask = ButtonPressMask | ButtonReleaseMask |
		ButtonMotionMask | ExposureMask | EnterWindowMask;
	//Notice that m_size_w gets the TOTAL width of tabs INCLUDING borders
	m_tabwin = XCreateWindow(m_display, m_win->getScreen()->getRootWindow(), 
		-30000, -30000, //TODO: So that it wont flicker or
			// appear before the window do
		m_size_w - m_win->getScreen()->getWindowStyle()->tab.border_width_2x,
		m_size_h - m_win->getScreen()->getWindowStyle()->tab.border_width_2x,
			m_win->getScreen()->getWindowStyle()->tab.border_width,
			m_win->getScreen()->getDepth(), InputOutput,
			m_win->getScreen()->getVisual(), attrib_mask, &attrib);

	//set grab
	XGrabButton(m_display, Button1, Mod1Mask, m_tabwin, True,
		ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
		GrabModeAsync, None, Fluxbox::instance()->getMoveCursor());

	//save to tabsearch
	Fluxbox::instance()->saveTabSearch(m_tabwin, this);	

	XMapSubwindows(m_display, m_tabwin);
	// don't show if the window is iconified
	if (!m_win->isIconic())
		XMapWindow(m_display, m_tabwin);

	decorate();
}

//-------------- focus --------------------
// Called when the focus changes in m_win 
// updates pixmap or color and draws the tab
//-----------------------------------------
void Tab::focus() {

	if (m_win->isFocused()) {
		if (m_focus_pm)
			XSetWindowBackgroundPixmap(m_display, m_tabwin, m_focus_pm);
		else
			XSetWindowBackground(m_display, m_tabwin, m_focus_pixel);
	} else {
		if (m_unfocus_pm)
			XSetWindowBackgroundPixmap(m_display, m_tabwin, m_unfocus_pm);
		else
			XSetWindowBackground(m_display, m_tabwin, m_unfocus_pixel);
	}
	XClearWindow(m_display, m_tabwin);	
	draw(false);
}

//-------------- raise --------------------
// Raises the tabs in the tablist
//-----------------------------------------
void Tab::raise() {
	//get first tab
	Tab *tab = 0;
	//raise tabs
	Workspace::Stack st;
	for (tab = getFirst(this); tab!=0; tab = tab->m_next) {
		st.push_back(tab->m_tabwin);
	}
	m_win->getScreen()->raiseWindows(st);
}

//-------------- lower --------------------
// Lowers the tabs in the tablist AND
// the windows the tabs relate to
//-----------------------------------------
void Tab::lower() {
	Tab *current = this;
	FluxboxWindow *win = 0; //convenience
	//this have to be done in the correct order, otherwise we'll switch the window
	//being ontop in the group
	do { 
		XLowerWindow(m_display, current->m_tabwin); //lower tabwin and tabs window
		win = current->getWindow(); 
		win->getScreen()->getWorkspace(win->getWorkspaceNumber())->lowerWindow(win);

		current = current->next(); //get next
		if (current == 0)
			current = getFirst(this); //there weren't any after, get the first

	} while (current != this);
}

//-------------- loadTheme -----------------
// loads the texture with the correct
// width and height, this is necessary in
// vertical and relative tab modes
// TODO optimize this
//------------------------------------------
void Tab::loadTheme() {
	BImageControl *image_ctrl = m_win->getScreen()->getImageControl();
	Pixmap tmp = m_focus_pm;
	FbTk::Texture *texture = &(m_win->getScreen()->getWindowStyle()->tab.l_focus);

	if (texture->type() & FbTk::Texture::PARENTRELATIVE ) {
		FbTk::Texture *pt = &(m_win->getScreen()->getWindowStyle()->tab.t_focus);
		if (pt->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
			m_focus_pm = None;
			m_focus_pixel = pt->color().pixel();
		} else
			m_focus_pm =
				image_ctrl->renderImage(m_size_w, m_size_h, pt);

		if (tmp) image_ctrl->removeImage(tmp);

	} else {
		if (texture->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
			m_focus_pm = None;
			m_focus_pixel = texture->color().pixel();
		} else
			m_focus_pm =
				image_ctrl->renderImage(m_size_w, m_size_h, texture);
		if (tmp) image_ctrl->removeImage(tmp);
	}

	tmp = m_unfocus_pm;
	texture = &(m_win->getScreen()->getWindowStyle()->tab.l_unfocus);

	if (texture->type() & FbTk::Texture::PARENTRELATIVE ) {
		FbTk::Texture *pt = &(m_win->getScreen()->getWindowStyle()->tab.t_unfocus);
		if (pt->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
			m_unfocus_pm = None;
			m_unfocus_pixel = pt->color().pixel();
		} else
			m_unfocus_pm =
			image_ctrl->renderImage(m_size_w, m_size_h, pt);
	} else {
		if (texture->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
			m_unfocus_pm = None;
			m_unfocus_pixel = texture->color().pixel();
		} else
			m_unfocus_pm =
				image_ctrl->renderImage(m_size_w, m_size_h, texture);
	}
	
	if (tmp) image_ctrl->removeImage(tmp);
}

//-------------- decorate --------------------
// decorates the tab with current theme
//--------------------------------------------
void Tab::decorate() {
	loadTheme();

	XSetWindowBorderWidth(m_display, m_tabwin,
		m_win->getScreen()->getWindowStyle()->tab.border_width);
	XSetWindowBorder(m_display, m_tabwin,
		m_win->getScreen()->getWindowStyle()->tab.border_color.pixel());
}

//-------------- deiconify -----------------
// Deiconifies the tab
// Used from FluxboxWindow to deiconify the tab when the window is deiconfied
//------------------------------------------
void Tab::deiconify() {
	XMapWindow(m_display, m_tabwin);
}

//------------- iconify --------------------
// Iconifies the tab.
// Used from FluxboxWindow to hide tab win when window is iconified
// disconnects itself from the list
//------------------------------------------
void Tab::iconify() {
	disconnect();
	withdraw();
	if(!Fluxbox::instance()->useTabs() && !m_next && !m_prev)//if we don't want to use tabs that much
		m_win->setTab(false);//let's get rid of this loner tab
}

//------------ withdraw --------------
// Unmaps the tab from display
//------------------------------------
void Tab::withdraw() {
	XUnmapWindow(m_display, m_tabwin);	
}

//------------ stick --------------------
// Set/reset the the sticky on all windows in the list
//---------------------------------------
void Tab::stick() {
	Tab *tab;
 
 	bool wasstuck = m_win->isStuck();
 
	//now do stick for all windows in the list
	for (tab = getFirst(this); tab != 0; tab = tab->m_next) {
		FluxboxWindow *win = tab->m_win; //just for convenience
		if (wasstuck) {
			win->blackbox_attrib.flags ^= BaseDisplay::ATTRIB_OMNIPRESENT;
			win->blackbox_attrib.attrib ^= BaseDisplay::ATTRIB_OMNIPRESENT;
			win->stuck = false;

		} else {
			win->stuck = true;
			BScreen *screen = win->getScreen();
			if (!win->isIconic() && !(win->getWorkspaceNumber() !=
					screen->getCurrentWorkspaceID())) {
				screen->reassociateWindow(win, screen->getCurrentWorkspaceID(), true);
			}

			win->blackbox_attrib.flags |= BaseDisplay::ATTRIB_OMNIPRESENT;
			win->blackbox_attrib.attrib |= BaseDisplay::ATTRIB_OMNIPRESENT;
		}
		
		win->setState(win->current_state);
	}	

}

//------------- resize -------------
// Resize the window's in the tablist
//----------------------------------
void Tab::resize() {
	Tab *tab;

	//now move and resize the windows in the list
	for (tab = getFirst(this); tab != 0; tab = tab->m_next) {
		if (tab!=this) {
			tab->m_win->configure(m_win->getXFrame(), m_win->getYFrame(),
								m_win->getWidth(), m_win->getHeight());
		}
	}

	// need to resize tabs if in relative mode
	if (m_win->getScreen()->getTabAlignment() == ARELATIVE) {
		calcIncrease();
		setPosition();
	}
}

//----------- shade --------------
// Shades the windows in the tablist
//--------------------------------
void Tab::shade() {
	Tab *tab;

	for(tab = getFirst(this); tab != 0; tab = tab->m_next) {
		if (tab==this)
			continue;
		tab->m_win->shade();
	}

	if (m_win->getScreen()->getTabPlacement() == PLEFT ||
			m_win->getScreen()->getTabPlacement() == PRIGHT) { 
		resizeGroup();
		calcIncrease();
	}

	if (!(m_win->getScreen()->getTabPlacement() == PTOP))
		setPosition();
}

//------------ draw -----------------
// Draws the tab 
// if pressed = true then it draws the tab in pressed 
// mode else it draws it in normal mode
// TODO: the "draw in pressed mode" 
//-----------------------------------
void Tab::draw(bool pressed) const {	
	XClearWindow(m_display, m_tabwin);
	
	if (m_win->getTitle().size() == 0) // we don't have anything to draw
		return;

	GC gc = ((m_win->isFocused()) ? m_win->getScreen()->getWindowStyle()->tab.l_text_focus_gc :
		m_win->getScreen()->getWindowStyle()->tab.l_text_unfocus_gc);

	Theme::WindowStyle *winstyle = m_win->getScreen()->getWindowStyle();
	size_t dlen = m_win->getTitle().size();
	
	size_t max_width = m_size_w; // special cases in rotated mode
	if (winstyle->tab.font.isRotated())
		max_width = m_size_h;

	int dx = DrawUtil::doAlignment(max_width, m_win->frame.bevel_w,
		winstyle->tab.justify,
		winstyle->tab.font,
		m_win->getTitle().c_str(), m_win->getTitle().size(), dlen);
	
	int dy = winstyle->tab.font.ascent() + m_win->frame.bevel_w;
	// swap dx and dy if we're rotated
	if (winstyle->tab.font.isRotated()) {
		int tmp = dy;
		dy = m_size_h - dx; // upside down (reverse direction)
		dx = tmp;
	}
	
	winstyle->tab.font.drawText(
		m_tabwin,
		m_win->getScreen()->getScreenNumber(),
		gc,
		m_win->getTitle().c_str(), dlen,
		dx, dy);

}

//-----------------------------------------------
//Helper for the Tab::setPosition() call
//returns the y position component correctly
//according to shading in cases PBOTTOM and 
//isShaded()
//-----------------------------------------------
int Tab::setPositionShadingHelper(bool shaded) {
	if (shaded) {
		return m_win->getYFrame() + m_win->getTitleHeight() + 
			m_win->getScreen()->getBorderWidth2x();
	} else {
		return m_win->getYFrame() + m_win->getHeight() +
			m_win->getScreen()->getBorderWidth2x();
	}
}

//-----------------------------------------------
//Helpers for correct alignment of tabs used
//by the setPosition() call
//return x/y positions correctly according to
//alignment, the 1st for cases PTOP and PBOTTOM
//the 2nd for cases PLEFT and PRIGHT
//-----------------------------------------------
int Tab::setPositionTBAlignHelper(Alignment align) {
	switch(align) {

	case ARELATIVE:
	case ALEFT:
		return m_win->getXFrame(); 
		break;
	case ACENTER:
		return calcCenterXPos(); 
		break;
	case ARIGHT:
		return m_win->getXFrame() + m_win->getWidth() +
			m_win->getScreen()->getBorderWidth2x() - m_size_w;
	default:
#ifdef DEBUG
		cerr << __FILE__ << ":" <<__LINE__ << ": " <<
			"Unsupported Alignment" << endl;
#endif //DEBUG
		return 0;
		break;
	}
}

int Tab::setPositionLRAlignHelper(Alignment align) {
	switch(align) {
	case ALEFT:
		return m_win->getYFrame() - m_size_h + m_win->getHeight() +
				m_win->getScreen()->getBorderWidth2x();
		break;
	case ACENTER:
		return calcCenterYPos();
		break;
	case ARELATIVE:
	case ARIGHT:
		return m_win->getYFrame();
		break;
	default:
#ifdef DEBUG
		cerr << __FILE__ << ":"<< __LINE__ << ": " <<
			"Unsupported Alignment" << endl;
#endif //DEBUG
		return 0;
		break;
	}
}

//------------- setPosition -----------------
// Position tab ( follow the m_win pos ).
// (and resize)
// Set new position of the other tabs in the chain
//-------------------------------------------
void Tab::setPosition() {	
	//don't do anything if the tablist is freezed
	if (m_stoptabs)
		return;

	Tab *tab;
	int pos_x = 0, pos_y = 0;
	
	m_stoptabs = true; //freeze tablist
	
	//and check for max tabs

	//Tab placement + alignment
	switch (m_win->getScreen()->getTabPlacement()) {
	case PTOP:
		pos_y = m_win->getYFrame() - m_size_h;
		pos_x = setPositionTBAlignHelper(
				m_win->getScreen()->getTabAlignment());
		break;
	case PBOTTOM:
		pos_y = setPositionShadingHelper(m_win->isShaded());
		pos_x = setPositionTBAlignHelper(
				m_win->getScreen()->getTabAlignment());
		break;
	case PLEFT:
		pos_x = m_win->isShaded() ?
			setPositionTBAlignHelper(m_win->getScreen()->getTabAlignment()) :
			m_win->getXFrame() - m_size_w;
		pos_y = m_win->isShaded() ?
			setPositionShadingHelper(true) :
			setPositionLRAlignHelper(m_win->getScreen()->getTabAlignment());
		break;
	case PRIGHT:
		pos_x = m_win->isShaded() ?
			setPositionTBAlignHelper(m_win->getScreen()->getTabAlignment()) :
			m_win->getXFrame() + m_win->getWidth() +
				m_win->getScreen()->getBorderWidth2x();
		pos_y = m_win->isShaded() ?
			setPositionShadingHelper(true) :
			setPositionLRAlignHelper(m_win->getScreen()->getTabAlignment());
		break;
	default:
		if(m_win->isShaded()) {
			pos_y = setPositionShadingHelper(true);
			pos_x = setPositionTBAlignHelper(
				m_win->getScreen()->getTabAlignment());
		} else {
			setPositionShadingHelper(false);
		}
		break;
	}

	for (tab = getFirst(this);
			tab!=0; 
			pos_x += tab->m_inc_x, pos_y += tab->m_inc_y,
			tab = tab->m_next){
		
		XMoveWindow(m_display, tab->m_tabwin, pos_x, pos_y);
				
		//dont move FluxboxWindow if the iterator = this
		if (tab!=this) {
			tab->m_win->configure(m_win->getXFrame(), m_win->getYFrame(), 
					m_win->getWidth(), m_win->getHeight());
		}	
	}	
	
	m_stoptabs = false;//thaw tablist
}

//Moves the tab to the left
void Tab::movePrev() {
	insert(m_prev);
}

//Moves the tab to the next tab if m_next != 0
void Tab::moveNext() {
	if(m_next == 0)
		return;
	Tab *tmp = m_next; 
	disconnect();
	tmp->insert(this);
}


//------------- calcIncrease ----------------
// calculates m_inc_x and m_inc_y for tabs
// used for positioning the tabs.
//-------------------------------------------
void Tab::calcIncrease() {
	Tab *tab;
	int inc_x = 0, inc_y = 0;
	unsigned int i = 0, tabs = numObjects();

	if (m_win->getScreen()->getTabPlacement() == PTOP ||
			m_win->getScreen()->getTabPlacement() == PBOTTOM ||
			m_win->isShaded()) {
		inc_y = 0;

		switch(m_win->getScreen()->getTabAlignment()) {
			case ALEFT:
				inc_x = m_size_w;
				break;
			case ACENTER:
				inc_x = m_size_w;
				break;
			case ARIGHT:
				inc_x = -m_size_w;
				break;
			case ARELATIVE:
				inc_x = calcRelativeWidth();
				break;
			default:
				break;
		}
	} else if (m_win->getScreen()->getTabPlacement() == PLEFT ||
					m_win->getScreen()->getTabPlacement() == PRIGHT) {
		inc_x = 0;

		switch(m_win->getScreen()->getTabAlignment()) {
			case ALEFT:
				inc_y = -m_size_h;
				break;
			case ACENTER:
				inc_y = m_size_h;
				break;
			case ARIGHT:
				inc_y = m_size_h;
				break;
			case ARELATIVE:
				inc_y = calcRelativeHeight();
				break;
			default:
				break;
		}
	}

	for (tab = getFirst(this); tab!=0; tab = tab->m_next, i++) {

	//TODO: move this out from here?
		if ((m_win->getScreen()->getTabPlacement() == PTOP ||
				m_win->getScreen()->getTabPlacement() == PBOTTOM ||
				m_win->isShaded()) &&
				m_win->getScreen()->getTabAlignment() == ARELATIVE) {
			if (!((m_win->getWidth() +
					m_win->getScreen()->getBorderWidth2x()) % tabs) ||
					i >= ((m_win->getWidth() +
					m_win->getScreen()->getBorderWidth2x()) % tabs)) {
				tab->setTabWidth(inc_x);
				tab->m_inc_x = inc_x;
			} else { // adding 1 extra pixel to get tabs like win width
				tab->setTabWidth(inc_x + 1);
				tab->m_inc_x = inc_x + 1;
			}
			tab->m_inc_y = inc_y;
		} else if (m_win->getScreen()->getTabAlignment() == ARELATIVE) {
			if (!((m_win->getHeight() +
				m_win->getScreen()->getBorderWidth2x()) % tabs) ||
				i >= ((m_win->getHeight() +
				m_win->getScreen()->getBorderWidth2x()) % tabs)) {

				tab->setTabHeight(inc_y);
				tab->m_inc_y = inc_y;
			} else {
				// adding 1 extra pixel to get tabs match window width
				tab->setTabHeight(inc_y + 1);
				tab->m_inc_y = inc_y + 1;
			}
			tab->m_inc_x = inc_x;
		} else { // non relative modes
			tab->m_inc_x = inc_x;
			tab->m_inc_y = inc_y;
		}
	}
}

//------------- buttonPressEvent -----------
// Handle button press event here.
//------------------------------------------
void Tab::buttonPressEvent(XButtonEvent *be) {	
	//draw in pressed mode
	draw(true);
	
	//invoke root menu with auto-glueing?
	if (be->button == 3) {
		BScreen *screen = m_win->getScreen();
		Rootmenu *rootmenu = screen->getRootmenu();
		if (! rootmenu->isVisible()) {
			Fluxbox::instance()->checkMenu();
			screen->getRootmenu()->move(be->x_root, be->y_root-rootmenu->titleHeight());
			rootmenu->setAutoGroupWindow(m_win->getClientWindow());
			rootmenu->show();
		}
	}
	//otherwise let the window handle the event
	else {
		//set window to titlewindow so we can take advantage of drag function
		be->window = m_win->frame.title;
	
		//call windows buttonpress eventhandler
		m_win->buttonPressEvent(be);
	}
}

//----------- buttonReleaseEvent ----------
// Handle button release event here.
// If tab is dropped then it should try to find
// the window where the tab where dropped.
//-----------------------------------------
void Tab::buttonReleaseEvent(XButtonEvent *be) {		
	
	if (m_moving) {
		
		m_moving = false; 
		
		//erase tabmoving rectangle
		XDrawRectangle(m_display, m_win->getScreen()->getRootWindow(),
			m_win->getScreen()->getOpGC(),
			m_move_x, m_move_y, 
			m_size_w, m_size_h);
		
		Fluxbox::instance()->ungrab();
		XUngrabPointer(m_display, CurrentTime);
		
		//storage of window and pos of window where we dropped the tab
		Window child;
		int dest_x = 0, dest_y = 0;
		
		//find window on coordinates of buttonReleaseEvent
		if (XTranslateCoordinates(m_display, m_win->getScreen()->getRootWindow(), 
							m_win->getScreen()->getRootWindow(),
							be->x_root, be->y_root, &dest_x, &dest_y, &child)) {
		        
			Tab *tab = Fluxbox::instance()->searchTab(child);
			FluxboxWindow *win = Fluxbox::instance()->searchWindow(child);
			if(win!=0 && m_win->getScreen()->isSloppyWindowGrouping())
				win->setTab(true);
			//search tablist for a tabwindow
			if ( (tab!=0) || (m_win->getScreen()->isSloppyWindowGrouping() &&
					(win!=0) && (tab = win->getTab())!=0)) {

				if (tab == this) // inserting ourself to ourself causes a disconnect
					return;

				// do only attach a hole chain if we dropped the
				// first tab in the dropped chain...
				if (m_prev)
					disconnect();
				
				// attach this tabwindow chain to the tabwindow chain we found.
				tab->insert(this);

			} else {	//Dropped nowhere
				disconnect();

				// convenience
				unsigned int placement = m_win->getScreen()->getTabPlacement();

				// (ab)using dest_x and dest_y
				dest_x = be->x_root;
				dest_y = be->y_root;

				if (placement == PTOP || placement == PBOTTOM || m_win->isShaded()) {
					if (placement == PBOTTOM && !m_win->isShaded())
						dest_y -= m_win->getHeight();
					else if (placement != PTOP && m_win->isShaded())
						dest_y -= m_win->getTitleHeight();
					else // PTOP
						dest_y += m_win->getTitleHeight();

					switch(m_win->getScreen()->getTabAlignment()) {
						case ACENTER:
							dest_x -= (m_win->getWidth() / 2) - (m_size_w / 2);
							break;
						case ARIGHT:
							dest_x -= m_win->getWidth() - m_size_w;
							break;
						default:
							break;
					}

				} else { // PLEFT & PRIGHT
					if (placement == PRIGHT)
						dest_x = be->x_root - m_win->getWidth();

					switch(m_win->getScreen()->getTabAlignment()) {
						case ACENTER:
							dest_y -= (m_win->getHeight() / 2) - (m_size_h / 2);
							break;
						case ALEFT:
							dest_y -= m_win->getHeight() - m_size_h;
							break;
						default:
							break;
					}
				}
				//TODO: this causes an calculate increase event, even if we
				// only are moving a window
				m_win->configure(dest_x, dest_y, m_win->getWidth(), m_win->getHeight());
				if(!Fluxbox::instance()->useTabs())
					m_win->setTab(0);//Remove tab from window, as it is now alone...
			}
		}
	} else {
		
		//raise this tabwindow
		raise();
		
		//set window to title window soo we can use m_win handler for menu
		be->window = m_win->frame.title;
		
		//call windows buttonrelease event handler so it can popup a menu if needed
		m_win->buttonReleaseEvent(be);
	}
	
}

//------------- exposeEvent ------------
// Handle expose event here.
// Draws the tab unpressed
//--------------------------------------
void Tab::exposeEvent(XExposeEvent *ee) {
	draw(false);
}

//----------- motionNotifyEvent --------
// Handles motion event here
// Draws the rectangle of moving tab
//--------------------------------------
void Tab::motionNotifyEvent(XMotionEvent *me) {
	
	Fluxbox *fluxbox = Fluxbox::instance();
	
	//if mousebutton 2 is pressed
	if (me->state & Button2Mask) {
		if (!m_moving) {
			m_moving = true; 
			
			XGrabPointer(m_display, me->window, False, Button2MotionMask |
								ButtonReleaseMask, GrabModeAsync, GrabModeAsync,
								None, fluxbox->getMoveCursor(), CurrentTime);

			fluxbox->grab();

			m_move_x = me->x_root - 1;
			m_move_y = me->y_root - 1;
				
			XDrawRectangle(m_display, m_win->getScreen()->getRootWindow(), 
					m_win->getScreen()->getOpGC(),
					m_move_x, m_move_y,
					m_size_w, m_size_h);
	
		} else {
		
			int dx = me->x_root - 1, dy = me->y_root - 1;

			dx -= m_win->getScreen()->getBorderWidth();
			dy -= m_win->getScreen()->getBorderWidth();

			if (m_win->getScreen()->getEdgeSnapThreshold()) {
				int drx = m_win->getScreen()->getWidth() - (dx + 1);

				if (dx > 0 && dx < drx && dx < m_win->getScreen()->getEdgeSnapThreshold()) 
					dx = 0;
				else if (drx > 0 && drx < m_win->getScreen()->getEdgeSnapThreshold())
					dx = m_win->getScreen()->getWidth() - 1;

				int dtty, dbby, dty, dby;
		
				switch (m_win->getScreen()->getToolbarPlacement()) {
				case Toolbar::TOPLEFT:
				case Toolbar::TOPCENTER:
				case Toolbar::TOPRIGHT:
					dtty = m_win->getScreen()->getToolbar()->exposedHeight() +
							m_win->getScreen()->getBorderWidth();
					dbby = m_win->getScreen()->getHeight();
					break;

				default:
					dtty = 0;
					dbby = m_win->getScreen()->getToolbar()->y();
					break;
				}
		
				dty = dy - dtty;
				dby = dbby - (dy + 1);

				if (dy > 0 && dty < m_win->getScreen()->getEdgeSnapThreshold())
					dy = dtty;
				else if (dby > 0 && dby < m_win->getScreen()->getEdgeSnapThreshold())
					dy = dbby - 1;
		
			}
		
			//erase rectangle
			XDrawRectangle(m_display, m_win->getScreen()->getRootWindow(),
					m_win->getScreen()->getOpGC(),
					m_move_x, m_move_y, 
					m_size_w, m_size_h);

			//redraw rectangle at new pos
			m_move_x = dx;
			m_move_y = dy;			
			XDrawRectangle(m_display, m_win->getScreen()->getRootWindow(), 
					m_win->getScreen()->getOpGC(),
					m_move_x, m_move_y,
					m_size_w, m_size_h);					
			
		}
	} 
}

//-------------- getFirst() ---------
// Returns the first Tab in the chain 
// of currentchain. 
//-----------------------------------
Tab *Tab::getFirst(Tab *current) {
	if (!current)
		return 0;
		
	Tab *i=current;
	
	for (; i->m_prev != 0; i = i->m_prev);
	return i;
}

//-------------- getLast() ---------
// Returns the last Tab in the chain 
// of currentchain. 
//-----------------------------------
Tab *Tab::getLast(Tab *current) {
	if (!current)
		return 0;
	Tab *i=current;
	
	for (; i->m_next != 0; i = i->m_next);
	return i;
}

//-------------- insert ------------
// (private)
// Inserts a tab in the chain
//----------------------------------
void Tab::insert(Tab *tab) {
	
	if (!tab || tab == this) //dont insert if the tab = 0 or the tab = this
		return;

	Tab *first = getFirst(this);
	
	//if the tab already in chain then disconnect it
	for (; first!=0; first = first->m_next) {
		if (first==tab) {
			#ifdef DEBUG
			cerr<<"Tab already in chain. Disconnecting!"<<endl;			
			#endif // DEBUG
			tab->disconnect();
			break;
		}
	}

	//get last tab in the chain to be inserted
	Tab *last = tab;
	for (; last->m_next!=0; last=last->m_next); 
	//do sticky before we connect it to the chain
	//sticky bit on window
	if (m_win->isStuck() != tab->m_win->isStuck()) {
			tab->m_win->stuck = !m_win->stuck; // it will toggle
			tab->stick(); //this will set all the m_wins in the list
	}
	
	//connect the tab to this chain
	
	if (m_next)	
		m_next->m_prev = last;
	tab->m_prev = this; 
	last->m_next = m_next; 

	m_next = tab;	

	bool resize_tabs = false;

	//TODO: cleanup and optimize
	//move and resize all windows in the tablist we inserted
	//only from first tab of the inserted chain to the last
	for (; tab!=last->m_next; tab=tab->m_next) {
		if (m_win->isShaded() != tab->m_win->isShaded()) {
			tab->m_stoptabs = true; // we don't want any actions performed on the
															// tabs, just the tab windows!
			if (m_win->getScreen()->getTabPlacement() == PLEFT ||
					m_win->getScreen()->getTabPlacement() == PRIGHT)
				resize_tabs = true;

			// if the window we are grouping to, we need to shade the tab window
			// _after_ reconfigure
			if(m_win->isShaded()) {
					tab->m_win->configure(m_win->getXFrame(), m_win->getYFrame(),
							m_win->getWidth(), m_win->getHeight());
					tab->m_win->shade();
			} else {
					tab->m_win->shade(); // switch to correct shade state
					tab->m_win->configure(m_win->getXFrame(), m_win->getYFrame(),
						m_win->getWidth(), m_win->getHeight());
			}

			tab->m_stoptabs = false;

		// both window have the same shaded state and have different sizes,
		// checking this so that I'll only do shade on windows if configure did
		// anything.
		} else if ((m_win->getWidth() != tab->m_win->getWidth()) ||
				(m_win->getHeight() != tab->m_win->getHeight())) {

			tab->m_win->configure(m_win->getXFrame(), m_win->getYFrame(),
				m_win->getWidth(), m_win->getHeight());

			// need to shade the tab window as configure will mess it up
			if (m_win->isShaded())
				tab->m_win->shade();
		}
	}

	// resize if in relative mode or resize_tabs is true
	if(m_win->getScreen()->getTabAlignment() == ARELATIVE ||
			resize_tabs) {
		resizeGroup();
		calcIncrease();
	}
	// reposition tabs
	setPosition();
}

//---------- disconnect() --------------
// Disconnects the tab from any chain
//--------------------------------------
void Tab::disconnect() {
	Tab *tmp = 0;

	Fluxbox *fluxbox = Fluxbox::instance();
	if (m_prev) {	//if this have a chain to "the left" (previous tab) then set it's next to this next
		m_prev->m_next = m_next;
		if(!m_next && !fluxbox->useTabs())//Only two tabs in list, remove tab from remaining window
			m_prev->m_win->setTab(false);
		else
			tmp = m_prev;
	} 
	if (m_next) {	//if this have a chain to "the right" (next tab) then set it's prev to this prev
		m_next->m_prev = m_prev;
		if(!m_prev && !fluxbox->useTabs())//Only two tabs in list, remove tab from remaining window
			m_next->m_win->setTab(false);
		else
			tmp = m_next;
	}

	//mark as no chain, previous and next.
	m_prev = 0;
	m_next = 0;
	
	//reposition the tabs
	if (tmp) {
		if (m_win->getScreen()->getTabAlignment() == ARELATIVE)
			tmp->calcIncrease();
		tmp->setPosition();
	}

	if (m_win->getScreen()->getTabAlignment() == ARELATIVE)
		calcIncrease();
	
	setPosition();
}

// ------------ setTabWidth --------------
// Sets Tab width _including_ borders
// ---------------------------------------
void Tab::setTabWidth(unsigned int w) {
	if (w > m_win->getScreen()->getWindowStyle()->tab.border_width_2x &&
			w != m_size_w) {
		m_size_w = w;
		XResizeWindow(m_display, m_tabwin,
			m_size_w - m_win->getScreen()->getWindowStyle()->tab.border_width_2x,
			m_size_h - m_win->getScreen()->getWindowStyle()->tab.border_width_2x);

		loadTheme(); // rerender themes to right size
		focus(); // redraw the window
	}
}

// ------------ setTabHeight ---------
// Sets Tab height _including_ borders
// ---------------------------------------
void Tab::setTabHeight(unsigned int h) {
	if (h > m_win->getScreen()->getWindowStyle()->tab.border_width_2x &&
			h != m_size_h) {
		m_size_h = h;
		XResizeWindow(m_display, m_tabwin, 
			m_size_w - m_win->getScreen()->getWindowStyle()->tab.border_width_2x,
			m_size_h - m_win->getScreen()->getWindowStyle()->tab.border_width_2x);

		loadTheme(); // rerender themes to right size
		focus(); // redraw the window
	} 
}

// ------------ resizeGroup --------------
// This function is used when (un)shading
// to get right size/width of tabs when
// PLeft || PRight && isTabRotateVertical
// ---------------------------------------
void Tab::resizeGroup() {
#ifdef DEBUG
	cerr <<__FILE__<<"("<<__LINE__<<"): Resizing group"<<endl;
#endif //DEBUG
	Tab *first;
	for (first = getFirst(this); first != 0; first = first->m_next) {
		if ((m_win->getScreen()->getTabPlacement() == PLEFT ||
				m_win->getScreen()->getTabPlacement() == PRIGHT) &&
				m_win->getScreen()->isTabRotateVertical() &&
				!m_win->isShaded()) {
			first->setTabWidth(m_win->getScreen()->getTabHeight());
			first->setTabHeight(m_win->getScreen()->getTabWidth());
		} else {
			first->setTabWidth(m_win->getScreen()->getTabWidth());
			first->setTabHeight(m_win->getScreen()->getTabHeight());
		}
		//TODO: do I have to set this all the time?
		first->m_configured = true; //used in Fluxbox::reconfigure()
	}
}

//------------- calcRelativeWidth --------
// Returns: Calculated width for relative 
// alignment
//----------------------------------------
unsigned int Tab::calcRelativeWidth() {
	unsigned int num=0;
	//calculate num objs in list (extract this to a function?)
	for (Tab *first=getFirst(this); first!=0; first=first->m_next, num++);	

	return ((m_win->getWidth() + m_win->getScreen()->getBorderWidth2x())/num);
}

//--------------- numObjects -------------------
// Returns the number of objects in
// the TabGroup. 
//-----------------------------------------------
unsigned int Tab::numObjects() {
	unsigned int num = 0;
	for (Tab *tab = getFirst(this); tab != 0; tab = tab->m_next, num++);
	return num;
}

//------------- calcRelativeHeight -------
// Returns: Calculated height for relative 
// alignment
//----------------------------------------
unsigned int Tab::calcRelativeHeight() {
	return ((m_win->getHeight() + 
			m_win->getScreen()->getBorderWidth2x())/numObjects());
}

//------------- calcCenterXPos -----------
// Returns: Calculated x position for 
// centered alignment
//----------------------------------------
unsigned int Tab::calcCenterXPos() {
	return (m_win->getXFrame() + ((m_win->getWidth() - 
			(m_size_w * numObjects())) / 2));
}

//------------- calcCenterYPos -----------
// Returns: Calculated y position for 
// centered alignment
//----------------------------------------
unsigned int Tab::calcCenterYPos() {
	return (m_win->getYFrame() + ((m_win->getHeight() - 
			(m_size_h * numObjects())) / 2));
}


//------- getTabPlacementString ----------
// Returns the tabplacement string of the 
// tabplacement number on success else 0.
//----------------------------------------
const char *Tab::getTabPlacementString(Tab::Placement placement) {	
	for (int i=0; i<(PNONE / 5); i++) {
		if (m_tabplacementlist[i] == placement)
			return m_tabplacementlist[i].string;
	}
	return 0;
}

//------- getTabPlacementNum -------------
// Returns the tabplacement number of the 
// tabplacement string on success else
// the type none on failure.
//----------------------------------------
Tab::Placement Tab::getTabPlacementNum(const char *string) {
	for (int i=0; i<(PNONE / 5); i ++) {
		if (m_tabplacementlist[i] == string) {
			return static_cast<Tab::Placement>(m_tabplacementlist[i].tp);
		}
	}
	return PNONE;
}

//------- getTabAlignmentString ----------
// Returns the tabplacement string of the 
// tabplacement number on success else 0.
//----------------------------------------
const char *Tab::getTabAlignmentString(Tab::Alignment alignment) {	
	for (int i=0; i<ANONE; i++) {
		if (m_tabalignmentlist[i] == alignment)
			return m_tabalignmentlist[i].string;
	}
	return 0;
}

//------- getTabAlignmentNum -------------
// Returns the tabplacement number of the 
// tabplacement string on success else
// the type none on failure.
//----------------------------------------
Tab::Alignment Tab::getTabAlignmentNum(const char *string) {
	for (int i=0; i<ANONE; i++) {
		if (m_tabalignmentlist[i] == string) {
			return static_cast<Tab::Alignment>(m_tabalignmentlist[i].tp);
		}
	}
	return ANONE;
}

//---------- addWindowToGroup ------------
// Add a window the the tabbed group
//----------------------------------------
bool Tab::addWindowToGroup(FluxboxWindow *otherWindow)
{
	if (!otherWindow || otherWindow == m_win)
		return false;
	Tab *otherTab = otherWindow->getTab();
	if (!otherTab)
		return false;
	insert(otherTab);
	return true;
}