summaryrefslogtreecommitdiff
path: root/src/Tab.cc
blob: 1a791c862a816ab910b6d37f3be82d50722f76ab (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
// Tab.cc for Fluxbox
// Copyright (c) 2001 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.

#include "Tab.hh"
#include <iostream>
#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif // HAVE_CONFIG_H
#include "i18n.hh"

#include "misc.hh"

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 = Fluxbox::instance()->getXDisplay();
	
	if ((m_win->getScreen()->getTabPlacement() == PLeft ||
			m_win->getScreen()->getTabPlacement() == PRight) &&
			m_win->getScreen()->isTabRotateVertical()) {
		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.getPixel();
	attrib.colormap = m_win->getScreen()->getColormap();
	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);
		
	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 *first = 0;
	first = getFirst(this);
	//raise tabs
	for (; first!=0; first = first->m_next)
		m_win->getScreen()->raiseWindows(&first->m_tabwin, 1);
}

//-------------- decorate --------------------
// decorates the tab with current theme
// TODO optimize this
//--------------------------------------------
void Tab::decorate() {
	
	BImageControl *image_ctrl = m_win->getScreen()->getImageControl();
	Pixmap tmp = m_focus_pm;
	BTexture *texture = &(m_win->getScreen()->getWindowStyle()->tab.l_focus);

 	if (texture->getTexture() & BImage_ParentRelative ) {
		BTexture *pt = &(m_win->getScreen()->getWindowStyle()->tab.t_focus);
		if (pt->getTexture() == (BImage_Flat | BImage_Solid)) {
  		  m_focus_pm = None;
	    m_focus_pixel = pt->getColor()->getPixel();
  	} else
    	m_focus_pm =
	      image_ctrl->renderImage(m_size_w, m_size_h, pt);
  	if (tmp) image_ctrl->removeImage(tmp);
		
	} else {
		if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
  		  m_focus_pm = None;
	    m_focus_pixel = texture->getColor()->getPixel();
  	} 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->getTexture() & BImage_ParentRelative ) {
		BTexture *pt = &(m_win->getScreen()->getWindowStyle()->tab.t_unfocus);
		if (pt->getTexture() == (BImage_Flat | BImage_Solid)) {
			m_unfocus_pm = None;
			m_unfocus_pixel = pt->getColor()->getPixel();
  		} else
			m_unfocus_pm =
			image_ctrl->renderImage(m_size_w, m_size_h, pt);
	} else {
		if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
			m_unfocus_pm = None;
			m_unfocus_pixel = texture->getColor()->getPixel();
  		} else
			m_unfocus_pm =
				image_ctrl->renderImage(m_size_w, m_size_h, texture);
	}
	
	if (tmp) image_ctrl->removeImage(tmp);
	
	XSetWindowBorderWidth(m_display, m_tabwin,
		m_win->getScreen()->getWindowStyle()->tab.border_width);
	XSetWindowBorder(m_display, m_tabwin,
		m_win->getScreen()->getWindowStyle()->tab.border_color.getPixel());
}


//-------------- 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();
}

//------------ 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() {
 
	//get first tab
	Tab *first = 0;
	first = getFirst(this);
	
	//now do stick for all windows in the list
	for (; first!=0; first = first->m_next) {
		FluxboxWindow *win = first->m_win; //just for convenient
		if (win->isStuck()) {
			win->blackbox_attrib.flags ^= AttribOmnipresent;
			win->blackbox_attrib.attrib ^= AttribOmnipresent;
			win->stuck = false;
			if (!win->iconic)
      	win->getScreen()->reassociateWindow(win, -1, true);
				    
		} else {
			win->stuck = true;
			win->blackbox_attrib.flags |= AttribOmnipresent;
			win->blackbox_attrib.attrib |= AttribOmnipresent;
		}
		
		win->setState(win->current_state);
	}	

}

//------------- resize -------------
// Resize the window's in the tablist
//----------------------------------
void Tab::resize() {
	Tab *first = getFirst(this);

	//now move and resize the windows in the list
	for (; first != 0; first = first->m_next) {
		if (first!=this) {
			first->m_win->configure(m_win->frame.x, m_win->frame.y,
								m_win->frame.width, m_win->frame.height);
		}
	}

	// 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() {
	for(Tab *first = getFirst(this); first != 0; first = first->m_next) {
		if (first==this)
			continue;
		first->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) {	
	unsigned int tabtext_w;

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

	// Different routines for drawing rotated text
	if ((m_win->getScreen()->getTabPlacement() == PLeft ||
			m_win->getScreen()->getTabPlacement() == PRight) &&
			(!m_win->isShaded() && m_win->getScreen()->isTabRotateVertical())) {

		tabtext_w = Misc::XRotTextWidth(m_win->getScreen()->getWindowStyle()->tab.rot_font,
						m_win->client.title, m_win->client.title_len);
		tabtext_w += (m_win->frame.bevel_w * 4);

		Misc::DrawRotString(m_display, m_tabwin, gc,
				m_win->getScreen()->getWindowStyle()->tab.rot_font,
				m_win->getScreen()->getWindowStyle()->tab.font.justify,
				tabtext_w, m_size_w, m_size_h,
				m_win->frame.bevel_w, m_win->client.title);

	} else {
		if (I18n::instance()->multibyte()) { // TODO: maybe move this out from here?
			XRectangle ink, logical;
			XmbTextExtents(m_win->getScreen()->getWindowStyle()->tab.font.set,
					m_win->client.title, m_win->client.title_len,
					&ink, &logical);
			tabtext_w = logical.width;
		} else {
			tabtext_w = XTextWidth(
					m_win->getScreen()->getWindowStyle()->tab.font.fontstruct,
					m_win->client.title, m_win->client.title_len);
		}
		tabtext_w += (m_win->frame.bevel_w * 4);

		Misc::DrawString(m_display, m_tabwin, gc,
				&m_win->getScreen()->getWindowStyle()->tab.font,
				tabtext_w, m_size_w,
				m_win->frame.bevel_w, m_win->client.title);
	}
}

//------------- 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 *first = 0;
	int pos_x = 0, pos_y = 0;
	
	m_stoptabs = true; //freeze tablist
	
	//and check for max tabs
	//TODO: optimize! There is many ways to implement this...		
	//posible movement to a static member function

	//Tab placement
	if (m_win->getScreen()->getTabPlacement() == PTop) {
		pos_y = m_win->frame.y - m_size_h; 

	} else if (m_win->getScreen()->getTabPlacement() == PBottom ||
				m_win->isShaded()) { 
		if (m_win->isShaded())
			pos_y = m_win->frame.y + m_win->getTitleHeight() +
					m_win->getScreen()->getBorderWidth2x();
		else
			pos_y = m_win->frame.y + m_win->getHeight() +
					m_win->getScreen()->getBorderWidth2x();

	} else if (m_win->getScreen()->getTabPlacement() == PLeft) {
		pos_x = m_win->frame.x - m_size_w;
			
	} else if (m_win->getScreen()->getTabPlacement() == PRight) {
		pos_x = m_win->frame.x + m_win->frame.width +
				m_win->getScreen()->getBorderWidth2x();	
	}

	//Tab alignment
	if (m_win->getScreen()->getTabPlacement() == PTop ||
			m_win->getScreen()->getTabPlacement() == PBottom ||
			m_win->isShaded()) {
		switch(m_win->getScreen()->getTabAlignment()) {
			case ARelative:
			case ALeft:
				pos_x = m_win->frame.x; 
				break;
			case ACenter:
				pos_x = calcCenterXPos(); 
				break;
			case ARight:
				pos_x = m_win->frame.x + m_win->frame.width +
						m_win->getScreen()->getBorderWidth2x() - m_size_w;
				break;
		}
	} else { //PLeft | PRight
		switch(m_win->getScreen()->getTabAlignment()) {
			case ALeft:
				pos_y = m_win->frame.y - m_size_h + m_win->frame.height +
						m_win->getScreen()->getBorderWidth2x();
				break;
			case ACenter:
				pos_y = calcCenterYPos();
				break;
			case ARelative:
			case ARight:
				pos_y = m_win->frame.y;
				break;
		}
	}
	
	for (first = getFirst(this);
			first!=0; 
			pos_x += first->m_inc_x, pos_y += first->m_inc_y,
			first = first->m_next){
		
		XMoveWindow(m_display, first->m_tabwin, pos_x, pos_y);
				
		//dont move fluxboxwindow if the itterator = this
		if (first!=this) {
			first->m_win->configure(m_win->frame.x, m_win->frame.y, 
					m_win->frame.width, m_win->frame.height);
		}	
	}	

	m_stoptabs = false;
}

//------------- calcIncrease ----------------
// calculates m_inc_x and m_inc_y for tabs
// used for positioning the tabs.
//-------------------------------------------
void Tab::calcIncrease(void) {
	#ifdef DEBUG
	cerr << "Calculating tab increase" << endl;
	#endif // DEBUG

	Tab *first;
	int inc_x = 0, inc_y = 0;
	unsigned int tabs = 0, i = 0;

	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;
		}
	} 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;
		}
	}

	for (first = getFirst(this); first!=0; first = first->m_next, tabs++);

	for (first = getFirst(this); first!=0; first = first->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->frame.width +
					m_win->getScreen()->getBorderWidth2x()) % tabs) ||
					i >= ((m_win->frame.width +
					m_win->getScreen()->getBorderWidth2x()) % tabs)) {
				first->setTabWidth(inc_x);
				first->m_inc_x = inc_x;
			} else { // adding 1 extra pixel to get tabs like win width
				first->setTabWidth(inc_x + 1);
				first->m_inc_x = inc_x + 1;
			}
			first->m_inc_y = inc_y;
		} else if (m_win->getScreen()->getTabAlignment() == ARelative) {
			if (!((m_win->frame.height +
				m_win->getScreen()->getBorderWidth2x()) % tabs) ||
				i >= ((m_win->frame.height +
				m_win->getScreen()->getBorderWidth2x()) % tabs)) {

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

//------------- buttonPressEvent -----------
// Handle button press event here.
//------------------------------------------
void Tab::buttonPressEvent(XButtonEvent *be) {	
	//draw in pressed mode
	draw(true);
	
	//set window to titlewindow so we can take advatage 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 = 0;
			//search tablist for a tabwindow
			if ((tab = Fluxbox::instance()->searchTab(child))!=0)  {
				// 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 {	
				disconnect();

				// (ab)using dest_x and dest_y
				switch(m_win->getScreen()->getTabPlacement()) {
					case PTop:
						dest_x = be->x_root;
						dest_y = be->y_root;
						switch(m_win->getScreen()->getTabAlignment()) {
						case ACenter:
							dest_x -= (m_win->frame.width / 2) - (m_size_w / 2);
							break;
						case ARight:
							dest_x -=  m_win->frame.width - m_size_w;
						break;
						}
					break;
					case PBottom:
						dest_x = be->x_root;
						dest_y = be->y_root - m_win->frame.height;
						switch(m_win->getScreen()->getTabAlignment()) {
						case ACenter:
							dest_x -= (m_win->frame.width / 2) - (m_size_w / 2);
							break;
						case ARight:
							dest_x -=  m_win->frame.width - m_size_w;
						break;
						}
					break;
					case PLeft:
						dest_x = be->x_root;
						dest_y = be->y_root;
						switch(m_win->getScreen()->getTabAlignment()) {
						case ACenter:
							dest_y -= (m_win->frame.height / 2) - (m_size_h / 2);
							break;
						case ALeft:
							dest_y -= m_win->frame.height - m_size_h;
							break;
						}
					break;
					case PRight:
						dest_x = be->x_root - m_win->frame.width;
						dest_y = be->y_root;
						switch(m_win->getScreen()->getTabAlignment()) {
						case ACenter:
							dest_y -= (m_win->frame.height / 2) - (m_size_h / 2);
							break;
						case ALeft:
							dest_y -= m_win->frame.height - m_size_h;
							break;
						}
					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->frame.width, m_win->frame.height);
			}
		}
	} 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()->getExposedHeight() +
							m_win->getScreen()->getBorderWidth();
					dbby = m_win->getScreen()->getHeight();
					break;

				default:
					dtty = 0;
					dbby = m_win->getScreen()->getToolbar()->getY();
					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;
}

//-------------- getFirst() ---------
// Returns the first 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() ||
			!m_win->isStuck() && tab->m_win->isStuck())
			tab->m_win->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->frame.x, m_win->frame.y,
							m_win->frame.width, m_win->frame.height);
					tab->m_win->shade();
			} else {
					tab->m_win->shade(); // switch to correct shade state
					tab->m_win->configure(m_win->frame.x, m_win->frame.y,
						m_win->frame.width, m_win->frame.height);
			}

			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->frame.width != tab->m_win->frame.width) ||
				(m_win->frame.height != tab->m_win->frame.height)) {

			tab->m_win->configure(m_win->frame.x, m_win->frame.y,
				m_win->frame.width, m_win->frame.height);

			// 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;
	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;
		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;
		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) {
		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);
	}
}

// ------------ setTabHeight ---------
// Sets Tab height _including_ borders
// ---------------------------------------
void Tab::setTabHeight(unsigned int h) {
	if (h > m_win->getScreen()->getWindowStyle()->tab.border_width_2x) {
		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);
	} 
}

// ------------ resizeGroup --------------
// This function is used when (un)shading
// to get right size/width of tabs when
// PLeft || PRight && isTabRotateVertical
// ---------------------------------------
void Tab::resizeGroup(void) {
	#ifdef DEBUG
	cerr << "Resising 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->frame.width + m_win->getScreen()->getBorderWidth2x())/num);
}

//------------- calcRelativeHeight -------
// Returns: Calculated height for relative 
// alignment
//----------------------------------------
unsigned int Tab::calcRelativeHeight() {
	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->frame.height + m_win->getScreen()->getBorderWidth2x())/num);
}

//------------- calcCenterXPos -----------
// Returns: Calculated x position for 
// centered alignment
//----------------------------------------
unsigned int Tab::calcCenterXPos() {
	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->frame.x + ((m_win->frame.width - (m_size_w * num)) / 2));
}

//------------- calcCenterYPos -----------
// Returns: Calculated y position for 
// centered alignment
//----------------------------------------
unsigned int Tab::calcCenterYPos() {
	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->frame.y + ((m_win->frame.height - (m_size_h * num)) / 2));
}


//------- getTabPlacementString ----------
// Returns the tabplacement string of the 
// tabplacement number on success else 0.
//----------------------------------------
const char *Tab::getTabPlacementString(int 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.
//----------------------------------------
int Tab::getTabPlacementNum(const char *string) {
	for (int i=0; i<(pnone / 5); i ++) {
		if (m_tabplacementlist[i] == string) {
			return m_tabplacementlist[i].tp;
		}
	}
	return pnone;
}

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

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