summaryrefslogtreecommitdiffstats
path: root/twin/kcmtwin/twindecoration/twindecoration.cpp
blob: 788a0d3e7e875bd1faed2d20dd65390b7117f437 (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
/*
	This is the new twindecoration kcontrol module

	Copyright (c) 2001
		Karol Szwed <gallium@kde.org>
		http://gallium.n3.net/

	Supports new twin configuration plugins, and titlebar button position
	modification via dnd interface.

	Based on original "twintheme" (Window Borders)
	Copyright (C) 2001 Rik Hemsley (rikkus) <rik@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

*/

#include <assert.h>
#include <math.h>

#include <tqdir.h>
#include <tqfileinfo.h>
#include <tqlayout.h>
#include <tqwhatsthis.h>
#include <tqgroupbox.h>
#include <tqcheckbox.h>
#include <tqtabwidget.h>
#include <tqvbox.h>
#include <tqlabel.h>
#include <tqfile.h>
#include <tqslider.h>
#include <tqspinbox.h>

#include <kapplication.h>
#include <kcolorbutton.h>
#include <kcombobox.h>
#include <kdebug.h>
#include <kdesktopfile.h>
#include <kstandarddirs.h>
#include <kglobal.h>
#include <klocale.h>
#include <kdialog.h>
#include <kgenericfactory.h>
#include <kaboutdata.h>
#include <kprocess.h>
#include <dcopclient.h>

#include "twindecoration.h"
#include "preview.h"
#include <kdecoration_plugins_p.h>
#include <kdecorationfactory.h>

// TDECModule plugin interface
// =========================
typedef KGenericFactory<KWinDecorationModule, TQWidget> KWinDecoFactory;
K_EXPORT_COMPONENT_FACTORY( kcm_twindecoration, KWinDecoFactory("kcmtwindecoration") )

KWinDecorationModule::KWinDecorationModule(TQWidget* parent, const char* name, const TQStringList &)
	: DCOPObject("KWinClientDecoration"),
	  TDECModule(KWinDecoFactory::instance(), parent, name),
          twinConfig("twinrc"),
          pluginObject(0)
{
	twinConfig.setGroup("Style");
        plugins = new KDecorationPreviewPlugins( &twinConfig );

	TQVBoxLayout* layout = new TQVBoxLayout(this, 0, KDialog::spacingHint()); 

// Save this for later...
//	cbUseMiniWindows = new TQCheckBox( i18n( "Render mini &titlebars for all windows"), checkGroup );
//	TQWhatsThis::add( cbUseMiniWindows, i18n( "Note that this option is not available on all styles yet!" ) );

	tabWidget = new TQTabWidget( this );
	layout->addWidget( tabWidget );

	// Page 1 (General Options)
	pluginPage = new TQWidget( tabWidget );

	TQVBoxLayout* pluginLayout = new TQVBoxLayout(pluginPage, KDialog::marginHint(), KDialog::spacingHint());

	// decoration chooser
	decorationList = new KComboBox( pluginPage );
	TQString whatsThis = i18n("Select the window decoration. This is the look and feel of both "
                             "the window borders and the window handle.");
	TQWhatsThis::add(decorationList, whatsThis);
	pluginLayout->addWidget(decorationList);

	TQGroupBox *pluginSettingsGrp = new TQGroupBox( i18n("Decoration Options"), pluginPage );
	pluginSettingsGrp->setColumnLayout( 0, Qt::Vertical );
	pluginSettingsGrp->setFlat( true );
	pluginSettingsGrp->layout()->setMargin( 0 );
	pluginSettingsGrp->layout()->setSpacing( KDialog::spacingHint() );
	pluginLayout->addWidget( pluginSettingsGrp );

	pluginLayout->addStretch();

	// Border size chooser
	lBorder = new TQLabel (i18n("B&order size:"), pluginSettingsGrp);
	cBorder = new TQComboBox(pluginSettingsGrp);
	lBorder->setBuddy(cBorder);
	TQWhatsThis::add( cBorder, i18n( "Use this combobox to change the border size of the decoration." ));
	lBorder->hide();
	cBorder->hide();
	TQHBoxLayout *borderSizeLayout = new TQHBoxLayout(pluginSettingsGrp->layout() );
	borderSizeLayout->addWidget(lBorder);
	borderSizeLayout->addWidget(cBorder);
	borderSizeLayout->addStretch();

	pluginConfigWidget = new TQVBox(pluginSettingsGrp);
	pluginSettingsGrp->layout()->add( pluginConfigWidget );

	// Page 2 (Button Selector)
	buttonPage = new TQWidget( tabWidget );
	TQVBoxLayout* buttonLayout = new TQVBoxLayout(buttonPage, KDialog::marginHint(), KDialog::spacingHint());

	cbShowToolTips = new TQCheckBox(
			i18n("&Show window button tooltips"), buttonPage );
	TQWhatsThis::add( cbShowToolTips,
			i18n(  "Enabling this checkbox will show window button tooltips. "
				   "If this checkbox is off, no window button tooltips will be shown."));

	cbUseCustomButtonPositions = new TQCheckBox(
			i18n("Use custom titlebar button &positions"), buttonPage );
	TQWhatsThis::add( cbUseCustomButtonPositions,
			i18n(  "The appropriate settings can be found in the \"Buttons\" Tab; "
				   "please note that this option is not available on all styles yet." ) );

	buttonLayout->addWidget( cbShowToolTips );
	buttonLayout->addWidget( cbUseCustomButtonPositions );

	// Add nifty dnd button modification widgets
	buttonPositionWidget = new ButtonPositionWidget(buttonPage, "button_position_widget");
	buttonPositionWidget->setDecorationFactory(plugins->factory() );
	TQHBoxLayout* buttonControlLayout = new TQHBoxLayout(buttonLayout);
	buttonControlLayout->addSpacing(20);
	buttonControlLayout->addWidget(buttonPositionWidget);
// 	buttonLayout->addStretch();

	// preview
	TQVBoxLayout* previewLayout = new TQVBoxLayout(layout, KDialog::spacingHint() );
	previewLayout->setMargin( KDialog::marginHint() );

	disabledNotice = new TQLabel("<b>" + i18n("NOTICE:") + "</b><br>" + i18n("A third party Window Manager has been selected for use with TDE.") + "<br>" + i18n("As a result, the built-in Window Manager configuration system will not function and has been disabled."), this);
	previewLayout->addWidget(disabledNotice);
	disabledNotice->hide();

	preview = new KDecorationPreview( this );
	previewLayout->addWidget(preview);

	preview->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
	tabWidget->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Maximum);

	// Page 3 (Window Shadows)
	TQHBox *inactiveShadowColourHBox, *shadowColourHBox;
	TQHBox *inactiveShadowOpacityHBox, *shadowOpacityHBox;
	TQHBox *inactiveShadowXOffsetHBox, *shadowXOffsetHBox;
	TQHBox *inactiveShadowYOffsetHBox, *shadowYOffsetHBox;
	TQHBox *inactiveShadowThicknessHBox, *shadowThicknessHBox;
	TQLabel *inactiveShadowColourLabel, *shadowColourLabel;
	TQLabel *inactiveShadowOpacityLabel, *shadowOpacityLabel;
	TQLabel *inactiveShadowXOffsetLabel, *shadowXOffsetLabel;
	TQLabel *inactiveShadowYOffsetLabel, *shadowYOffsetLabel;
	TQLabel *inactiveShadowThicknessLabel, *shadowThicknessLabel;

	shadowPage = new TQVBox(tabWidget);
	shadowPage->setSpacing(KDialog::spacingHint());
	shadowPage->setMargin(KDialog::marginHint());

	cbWindowShadow = new TQCheckBox(
			i18n("&Draw a drop shadow under windows"), shadowPage);
	TQWhatsThis::add(cbWindowShadow,
			i18n("Enabling this checkbox will allow you to choose a kind of "
				 "drop shadow to draw under each window."));

	activeShadowSettings = new TQGroupBox(1, Qt::Horizontal,
			i18n("Active Window Shadow"), shadowPage);
	inactiveShadowSettings = new TQGroupBox(1, Qt::Horizontal,
			i18n("Inactive Window Shadows"), shadowPage);
	whichShadowSettings = new TQGroupBox(3, Qt::Horizontal,
			i18n("Draw Shadow Under Normal Windows And..."), shadowPage);

	cbShadowDocks = new TQCheckBox(i18n("Docks and &panels"),
			whichShadowSettings);
	connect(cbShadowDocks, TQT_SIGNAL(toggled(bool)),
			TQT_SLOT(slotSelectionChanged()));
	cbShadowOverrides = new TQCheckBox(i18n("O&verride windows"),
			whichShadowSettings);
	connect(cbShadowOverrides, TQT_SIGNAL(toggled(bool)),
			TQT_SLOT(slotSelectionChanged()));
	cbShadowTopMenus = new TQCheckBox(i18n("&Top menu"),
			whichShadowSettings);
	connect(cbShadowTopMenus, TQT_SIGNAL(toggled(bool)),
			TQT_SLOT(slotSelectionChanged()));
	cbInactiveShadow = new TQCheckBox(
			i18n("Draw shadow under &inactive windows"), inactiveShadowSettings);
	connect(cbInactiveShadow, TQT_SIGNAL(toggled(bool)),
			TQT_SLOT(slotSelectionChanged()));

	shadowColourHBox = new TQHBox(activeShadowSettings);
	shadowColourHBox->setSpacing(KDialog::spacingHint());
	shadowColourLabel = new TQLabel(i18n("Colour:"), shadowColourHBox);
	shadowColourButton = new KColorButton(shadowColourHBox);
	connect(shadowColourButton, TQT_SIGNAL(changed(const TQColor &)), TQT_SLOT(slotSelectionChanged()));

	inactiveShadowColourHBox = new TQHBox(inactiveShadowSettings);
	inactiveShadowColourHBox->setSpacing(KDialog::spacingHint());
	inactiveShadowColourLabel = new TQLabel(i18n("Colour:"), inactiveShadowColourHBox);
	inactiveShadowColourButton = new KColorButton(inactiveShadowColourHBox);
	connect(inactiveShadowColourButton, TQT_SIGNAL(changed(const TQColor &)), TQT_SLOT(slotSelectionChanged()));

	shadowOpacityHBox = new TQHBox(activeShadowSettings);
	shadowOpacityHBox->setSpacing(KDialog::spacingHint());
	shadowOpacityLabel = new TQLabel(i18n("Maximum opacity:"), shadowOpacityHBox);
	shadowOpacitySlider = new TQSlider(1, 100, 10, 50, Qt::Horizontal,
			shadowOpacityHBox);
	shadowOpacitySlider->setTickmarks(TQSlider::Below);
	shadowOpacitySlider->setTickInterval(10);
	shadowOpacitySpinBox = new TQSpinBox(1, 100, 1, shadowOpacityHBox);
	shadowOpacitySpinBox->setSuffix(" %");
	connect(shadowOpacitySlider, TQT_SIGNAL(valueChanged(int)), shadowOpacitySpinBox,
			TQT_SLOT(setValue(int)));
	connect(shadowOpacitySpinBox, TQT_SIGNAL(valueChanged(int)), shadowOpacitySlider,
			TQT_SLOT(setValue(int)));
	connect(shadowOpacitySlider, TQT_SIGNAL(valueChanged(int)),
			TQT_SLOT(slotSelectionChanged()));

	inactiveShadowOpacityHBox = new TQHBox(inactiveShadowSettings);
	inactiveShadowOpacityHBox->setSpacing(KDialog::spacingHint());
	inactiveShadowOpacityLabel = new TQLabel(i18n("Maximum opacity:"),
			inactiveShadowOpacityHBox);
	inactiveShadowOpacitySlider = new TQSlider(1, 100, 10, 50, Qt::Horizontal,
			inactiveShadowOpacityHBox);
	inactiveShadowOpacitySlider->setTickmarks(TQSlider::Below);
	inactiveShadowOpacitySlider->setTickInterval(10);
	inactiveShadowOpacitySpinBox = new TQSpinBox(1, 100, 1,
			inactiveShadowOpacityHBox);
	inactiveShadowOpacitySpinBox->setSuffix(" %");
	connect(inactiveShadowOpacitySlider, TQT_SIGNAL(valueChanged(int)),
			inactiveShadowOpacitySpinBox,
			TQT_SLOT(setValue(int)));
	connect(inactiveShadowOpacitySpinBox, TQT_SIGNAL(valueChanged(int)),
			inactiveShadowOpacitySlider,
			TQT_SLOT(setValue(int)));
	connect(inactiveShadowOpacitySlider, TQT_SIGNAL(valueChanged(int)),
			TQT_SLOT(slotSelectionChanged()));

	shadowXOffsetHBox = new TQHBox(activeShadowSettings);
	shadowXOffsetHBox->setSpacing(KDialog::spacingHint());
	shadowXOffsetLabel = new TQLabel(
			i18n("Offset rightward (may be negative):"),
			shadowXOffsetHBox);
	shadowXOffsetSpinBox = new TQSpinBox(-1024, 1024, 1, shadowXOffsetHBox);
	shadowXOffsetSpinBox->setSuffix(i18n(" pixels"));
	connect(shadowXOffsetSpinBox, TQT_SIGNAL(valueChanged(int)),
			TQT_SLOT(slotSelectionChanged()));

	inactiveShadowXOffsetHBox = new TQHBox(inactiveShadowSettings);
	inactiveShadowXOffsetHBox->setSpacing(KDialog::spacingHint());
	inactiveShadowXOffsetLabel = new TQLabel(
			i18n("Offset rightward (may be negative):"),
			inactiveShadowXOffsetHBox);
	inactiveShadowXOffsetSpinBox = new TQSpinBox(-1024, 1024, 1,
			inactiveShadowXOffsetHBox);
	inactiveShadowXOffsetSpinBox->setSuffix(i18n(" pixels"));
	connect(inactiveShadowXOffsetSpinBox, TQT_SIGNAL(valueChanged(int)),
			TQT_SLOT(slotSelectionChanged()));

	shadowYOffsetHBox = new TQHBox(activeShadowSettings);
	shadowYOffsetHBox->setSpacing(KDialog::spacingHint());
	shadowYOffsetLabel = new TQLabel(
			i18n("Offset downward (may be negative):"),
			shadowYOffsetHBox);
	shadowYOffsetSpinBox = new TQSpinBox(-1024, 1024, 1, shadowYOffsetHBox);
	shadowYOffsetSpinBox->setSuffix(i18n(" pixels"));
	connect(shadowYOffsetSpinBox, TQT_SIGNAL(valueChanged(int)),
			TQT_SLOT(slotSelectionChanged()));

	inactiveShadowYOffsetHBox = new TQHBox(inactiveShadowSettings);
	inactiveShadowYOffsetHBox->setSpacing(KDialog::spacingHint());
	inactiveShadowYOffsetLabel = new TQLabel(
			i18n("Offset downward (may be negative):"),
			inactiveShadowYOffsetHBox);
	inactiveShadowYOffsetSpinBox = new TQSpinBox(-1024, 1024, 1,
			inactiveShadowYOffsetHBox);
	inactiveShadowYOffsetSpinBox->setSuffix(i18n(" pixels"));
	connect(inactiveShadowYOffsetSpinBox, TQT_SIGNAL(valueChanged(int)),
			TQT_SLOT(slotSelectionChanged()));

	shadowThicknessHBox = new TQHBox(activeShadowSettings);
	shadowThicknessHBox->setSpacing(KDialog::spacingHint());
	shadowThicknessLabel = new TQLabel(
			i18n("Thickness to either side of window:"),
			shadowThicknessHBox);
	shadowThicknessSpinBox = new TQSpinBox(1, 100, 1,
			shadowThicknessHBox);
	shadowThicknessSpinBox->setSuffix(i18n(" pixels"));
	connect(shadowThicknessSpinBox, TQT_SIGNAL(valueChanged(int)),
			TQT_SLOT(slotSelectionChanged()));

	inactiveShadowThicknessHBox = new TQHBox(inactiveShadowSettings);
	inactiveShadowThicknessHBox->setSpacing(KDialog::spacingHint());
	inactiveShadowThicknessLabel = new TQLabel(
			i18n("Thickness to either side of window:"),
			inactiveShadowThicknessHBox);
	inactiveShadowThicknessSpinBox = new TQSpinBox(1, 100, 1,
			inactiveShadowThicknessHBox);
	inactiveShadowThicknessSpinBox->setSuffix(i18n(" pixels"));
	connect(inactiveShadowThicknessSpinBox, TQT_SIGNAL(valueChanged(int)),
			TQT_SLOT(slotSelectionChanged()));

	// Page 4 (WM selector)
	windowmanagerPage = new TQWidget( tabWidget );

	TQVBoxLayout* windowmanagerLayout = new TQVBoxLayout(windowmanagerPage, KDialog::marginHint(), KDialog::spacingHint());

	// WM chooser
	thirdpartyWMList = new KComboBox( windowmanagerPage );
	whatsThis = i18n("Select the window manager. Selecting a window manager "
                             "other than \"twin\" will require you to use a third party program for configuration and may increase the risk of system crashes or security problems.");
	TQWhatsThis::add(thirdpartyWMList, whatsThis);
	TQLabel* thirdpartyWMLabel = new TQLabel(i18n("Window Manager to use in your TDE session:"), windowmanagerPage);
	windowmanagerLayout->addWidget(thirdpartyWMLabel);
	windowmanagerLayout->addWidget(thirdpartyWMList);
	thirdpartyWMArguments = new KLineEdit( windowmanagerPage );
	whatsThis = i18n("Specify any command line arguments to be passed to the selected WM on startup, separated with whitespace.  A common example is --replace");
	TQWhatsThis::add(thirdpartyWMArguments, whatsThis);
	TQLabel* thirdpartyWMArgumentsLabel = new TQLabel(i18n("Command line arguments to pass to the Window Manager (should remain blank unless needed):"), windowmanagerPage);
	windowmanagerLayout->addWidget(thirdpartyWMArgumentsLabel);
	windowmanagerLayout->addWidget(thirdpartyWMArguments);

	windowmanagerLayout->addStretch();

	// Load all installed decorations into memory
	// Set up the decoration lists and other UI settings
	findDecorations();
	createDecorationList();
	createThirdPartyWMList();
	readConfig( &twinConfig );
	resetPlugin( &twinConfig );

	tabWidget->insertTab( pluginPage, i18n("&Window Decoration") );
	tabWidget->insertTab( buttonPage, i18n("&Buttons") );
	tabWidget->insertTab( shadowPage, i18n("&Shadows") );
	tabWidget->insertTab( windowmanagerPage, i18n("&Window Manager") );

	connect( buttonPositionWidget, TQT_SIGNAL(changed()), this, TQT_SLOT(slotButtonsChanged()) ); // update preview etc.
	connect( buttonPositionWidget, TQT_SIGNAL(changed()), this, TQT_SLOT(slotSelectionChanged()) ); // emit changed()...
	connect( decorationList, TQT_SIGNAL(activated(const TQString&)), TQT_SLOT(slotSelectionChanged()) );
	connect( decorationList, TQT_SIGNAL(activated(const TQString&)),
								TQT_SLOT(slotChangeDecoration(const TQString&)) );
	connect( cbUseCustomButtonPositions, TQT_SIGNAL(clicked()), TQT_SLOT(slotSelectionChanged()) );
	connect(cbUseCustomButtonPositions, TQT_SIGNAL(toggled(bool)), buttonPositionWidget, TQT_SLOT(setEnabled(bool)));
	connect(cbUseCustomButtonPositions, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(slotButtonsChanged()) );
 	connect(cbWindowShadow, TQT_SIGNAL(toggled(bool)), activeShadowSettings, TQT_SLOT(setEnabled(bool)));
 	connect(cbWindowShadow, TQT_SIGNAL(toggled(bool)), inactiveShadowSettings, TQT_SLOT(setEnabled(bool)));
 	connect(cbWindowShadow, TQT_SIGNAL(toggled(bool)), whichShadowSettings, TQT_SLOT(setEnabled(bool)));

	connect( cbShowToolTips, TQT_SIGNAL(clicked()), TQT_SLOT(slotSelectionChanged()) );
	connect( cbWindowShadow, TQT_SIGNAL(clicked()), TQT_SLOT(slotSelectionChanged()) );
	connect( cBorder, TQT_SIGNAL( activated( int )), TQT_SLOT( slotBorderChanged( int )));
//	connect( cbUseMiniWindows, TQT_SIGNAL(clicked()), TQT_SLOT(slotSelectionChanged()) );

	connect( thirdpartyWMList, TQT_SIGNAL(activated(const TQString&)), TQT_SLOT(slotSelectionChanged()) );
	connect( thirdpartyWMArguments, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(slotSelectionChanged()) );

	// Allow twin dcop signal to update our selection list
	connectDCOPSignal("twin", 0, "dcopResetAllClients()", "dcopUpdateClientList()", false);
	
	TDEAboutData *about =
		new TDEAboutData(I18N_NOOP("kcmtwindecoration"),
				I18N_NOOP("Window Decoration Control Module"),
				0, 0, TDEAboutData::License_GPL,
				I18N_NOOP("(c) 2001 Karol Szwed"));
	about->addAuthor("Karol Szwed", 0, "gallium@kde.org");
	setAboutData(about);
}


KWinDecorationModule::~KWinDecorationModule()
{
        delete preview; // needs to be destroyed before plugins
        delete plugins;
}


// Find all theme desktop files in all 'data' dirs owned by twin.
// And insert these into a DecorationInfo structure
void KWinDecorationModule::findDecorations()
{
	TQStringList dirList = TDEGlobal::dirs()->findDirs("data", "twin");
	TQStringList::ConstIterator it;

	for (it = dirList.begin(); it != dirList.end(); it++)
	{
		TQDir d(*it);
		if (d.exists())
			for (TQFileInfoListIterator it(*d.entryInfoList()); it.current(); ++it)
			{
				TQString filename(it.current()->absFilePath());
				if (KDesktopFile::isDesktopFile(filename))
				{
					KDesktopFile desktopFile(filename);
					TQString libName = desktopFile.readEntry("X-TDE-Library");

					if (!libName.isEmpty() && libName.startsWith( "twin3_" ))
					{
						DecorationInfo di;
						di.name = desktopFile.readName();
						di.libraryName = libName;
						decorations.append( di );
					}
				}
			}
	}
}


// Fills the decorationList with a list of available twin decorations
void KWinDecorationModule::createDecorationList()
{
	TQValueList<DecorationInfo>::ConstIterator it;

	// Sync with twin hardcoded KDE2 style which has no desktop item
    TQStringList decorationNames;
	decorationNames.append( i18n("KDE 2") );
	for (it = decorations.begin(); it != decorations.end(); ++it)
	{
		decorationNames.append((*it).name);
	}
	decorationNames.sort();
    decorationList->insertStringList(decorationNames);
}


// Fills the thirdpartyWMList with a list of available window managers
void KWinDecorationModule::createThirdPartyWMList()
{
	TQStringList::Iterator it;

	// FIXME
	// This list SHOULD NOT be hard coded
	// It should detect the available WMs through a standard mechanism of some sort
	TQString wmExecutable;
	TQStringList wmNames;
	TQStringList wmAvailableNames;
	wmNames << TQString("kwin ").append(i18n("(KDE4's window manager)")) << TQString("compiz ").append(i18n("(Compiz Effects Manager)")) << TQString("icewm ").append(i18n("(Simple, fast window manager)"));
	wmNames.sort();
	wmNames.prepend(TQString("twin ").append(i18n("(Default TDE window manager)")));
	for (it = wmNames.begin(); it != wmNames.end(); ++it)
	{
		wmExecutable = *it;
		int descStart = wmExecutable.find(" ");
		if (descStart >= 0) {
			wmExecutable.truncate(descStart);
		}
		if (KStandardDirs::findExe(wmExecutable) != TQString::null) {
			wmAvailableNames.append(*it);
		}
	}

	thirdpartyWMList->insertStringList(wmAvailableNames);
}


// Reset the decoration plugin to what the user just selected
void KWinDecorationModule::slotChangeDecoration( const TQString & text)
{
	TDEConfig twinConfig("twinrc");
	twinConfig.setGroup("Style");

	// Let the user see config options for the currently selected decoration
	resetPlugin( &twinConfig, text );
}


// This is the selection handler setting
void KWinDecorationModule::slotSelectionChanged()
{
	emit TDECModule::changed(true);

	processEnabledDisabledTabs();
}

// Handle WM selection-related disable/enable of tabs
void KWinDecorationModule::processEnabledDisabledTabs()
{
	TQString wmExecutableName = thirdpartyWMList->currentText();
	int descStart = wmExecutableName.find(" ");
	if (descStart >= 0) {
		wmExecutableName.truncate(descStart);
	}
	if (wmExecutableName == "twin") {
		pluginPage->setEnabled(true);
		buttonPage->setEnabled(true);
		shadowPage->setEnabled(true);
		disabledNotice->hide();
		preview->show();
	}
	else {
		pluginPage->setEnabled(false);
		buttonPage->setEnabled(false);
		shadowPage->setEnabled(false);
		disabledNotice->show();
		preview->hide();
	}
}

static const char* const border_names[ KDecorationDefines::BordersCount ] =
    {
    I18N_NOOP( "Tiny" ),
    I18N_NOOP( "Normal" ),
    I18N_NOOP( "Large" ),
    I18N_NOOP( "Very Large" ),
    I18N_NOOP( "Huge" ),
    I18N_NOOP( "Very Huge" ),
    I18N_NOOP( "Oversized" )
    };

int KWinDecorationModule::borderSizeToIndex( BorderSize size, TQValueList< BorderSize > sizes )
{
        int pos = 0;
        for( TQValueList< BorderSize >::ConstIterator it = sizes.begin();
             it != sizes.end();
             ++it, ++pos )
            if( size <= *it )
                break;
        return pos;
}

KDecorationDefines::BorderSize KWinDecorationModule::indexToBorderSize( int index,
    TQValueList< BorderSize > sizes )
{
        TQValueList< BorderSize >::ConstIterator it = sizes.begin();
        for(;
             it != sizes.end();
             ++it, --index )
            if( index == 0 )
                break;
        return *it;
}

void KWinDecorationModule::slotBorderChanged( int size )
{
        if( lBorder->isHidden())
            return;
        emit TDECModule::changed( true );
        TQValueList< BorderSize > sizes;
        if( plugins->factory() != NULL )
            sizes = plugins->factory()->borderSizes();
        assert( sizes.count() >= 2 );
        border_size = indexToBorderSize( size, sizes );

	// update preview
	preview->setTempBorderSize(plugins, border_size);
}

void KWinDecorationModule::slotButtonsChanged()
{
	// update preview
	preview->setTempButtons(plugins, cbUseCustomButtonPositions->isChecked(), buttonPositionWidget->buttonsLeft(), buttonPositionWidget->buttonsRight() );
}

TQString KWinDecorationModule::decorationName( TQString& libName )
{
	TQString decoName;

	TQValueList<DecorationInfo>::Iterator it;
	for( it = decorations.begin(); it != decorations.end(); ++it )
		if ( (*it).libraryName == libName )
		{
			decoName = (*it).name;
			break;
		}

	return decoName;
}


TQString KWinDecorationModule::decorationLibName( const TQString& name )
{
	TQString libName;

	// Find the corresponding library name to that of
	// the current plugin name
	TQValueList<DecorationInfo>::Iterator it;
	for( it = decorations.begin(); it != decorations.end(); ++it )
		if ( (*it).name == name )
		{
			libName = (*it).libraryName;
			break;
		}

	if (libName.isEmpty())
		libName = "twin_default";	// KDE 2

	return libName;
}


// Loads/unloads and inserts the decoration config plugin into the
// pluginConfigWidget, allowing for dynamic configuration of decorations
void KWinDecorationModule::resetPlugin( TDEConfig* conf, const TQString& currentDecoName )
{
	// Config names are "twin_icewm_config"
	// for "twin3_icewm" twin client

	TQString oldName = styleToConfigLib( oldLibraryName );

	TQString currentName;
	if (!currentDecoName.isEmpty())
		currentName = decorationLibName( currentDecoName ); // Use what the user selected
	else
		currentName = currentLibraryName; // Use what was read from readConfig()

        if( plugins->loadPlugin( currentName )
            && preview->recreateDecoration( plugins ))
            preview->enablePreview();
        else
            preview->disablePreview();
        plugins->destroyPreviousPlugin();

        checkSupportedBorderSizes();

	// inform buttonPositionWidget about the new factory...
	buttonPositionWidget->setDecorationFactory(plugins->factory() );

	currentName = styleToConfigLib( currentName );

	// Delete old plugin widget if it exists
	delete pluginObject;
	pluginObject = 0;

	// Use klibloader for library manipulation
	KLibLoader* loader = KLibLoader::self();

	// Free the old library if possible
	if (!oldLibraryName.isNull())
		loader->unloadLibrary( TQFile::encodeName(oldName) );

	KLibrary* library = loader->library( TQFile::encodeName(currentName) );
	if (library != NULL)
	{
		void* alloc_ptr = library->symbol("allocate_config");

		if (alloc_ptr != NULL)
		{
			allocatePlugin = (TQObject* (*)(TDEConfig* conf, TQWidget* parent))alloc_ptr;
			pluginObject = (TQObject*)(allocatePlugin( conf, pluginConfigWidget ));

			// connect required signals and slots together...
			connect( pluginObject, TQT_SIGNAL(changed()), this, TQT_SLOT(slotSelectionChanged()) );
			connect( this, TQT_SIGNAL(pluginLoad(TDEConfig*)), pluginObject, TQT_SLOT(load(TDEConfig*)) );
			connect( this, TQT_SIGNAL(pluginSave(TDEConfig*)), pluginObject, TQT_SLOT(save(TDEConfig*)) );
			connect( this, TQT_SIGNAL(pluginDefaults()), pluginObject, TQT_SLOT(defaults()) );
			pluginConfigWidget->show();
			return;
		}
	}

	pluginConfigWidget->hide();
}


// Reads the twin config settings, and sets all UI controls to those settings
// Updating the config plugin if required
void KWinDecorationModule::readConfig( TDEConfig* conf )
{
	// General tab
	// ============
	cbShowToolTips->setChecked( conf->readBoolEntry("ShowToolTips", true ));
//	cbUseMiniWindows->setChecked( conf->readBoolEntry("MiniWindowBorders", false));

	// Find the corresponding decoration name to that of
	// the current plugin library name

	oldLibraryName = currentLibraryName;
	currentLibraryName = conf->readEntry("PluginLib",
					((TQPixmap::defaultDepth() > 8) ? "twin_plastik" : "twin_quartz"));
	TQString decoName = decorationName( currentLibraryName );

	// If we are using the "default" kde client, use the "default" entry.
	if (decoName.isEmpty())
		decoName = i18n("KDE 2");

    int numDecos = decorationList->count();
	for (int i = 0; i < numDecos; ++i)
    {
		 if (decorationList->text(i) == decoName)
		 {
		 		 decorationList->setCurrentItem(i);
		 		 break;
		 }
	}

	// Buttons tab
	// ============
	bool customPositions = conf->readBoolEntry("CustomButtonPositions", false);
	cbUseCustomButtonPositions->setChecked( customPositions );
	buttonPositionWidget->setEnabled( customPositions );
	// Menu and onAllDesktops buttons are default on LHS
	buttonPositionWidget->setButtonsLeft( conf->readEntry("ButtonsOnLeft", "MS") );
	// Help, Minimize, Maximize and Close are default on RHS
	buttonPositionWidget->setButtonsRight( conf->readEntry("ButtonsOnRight", "HIAX") );

        int bsize = conf->readNumEntry( "BorderSize", BorderNormal );
        if( bsize >= BorderTiny && bsize < BordersCount )
            border_size = static_cast< BorderSize >( bsize );
        else
            border_size = BorderNormal;
        checkSupportedBorderSizes();

	// Shadows tab
	// ===========
	bool shadowEnabled = conf->readBoolEntry("ShadowEnabled", false);
 	cbWindowShadow->setChecked(shadowEnabled);
	activeShadowSettings->setEnabled(shadowEnabled);
	inactiveShadowSettings->setEnabled(shadowEnabled);
	whichShadowSettings->setEnabled(shadowEnabled);
	shadowColourButton->setColor(conf->readColorEntry("ShadowColour", &TQt::black));
 	shadowOpacitySlider->setValue((int)ceil(conf->readDoubleNumEntry("ShadowOpacity", 0.70) * 100));
 	shadowXOffsetSpinBox->setValue(conf->readNumEntry("ShadowXOffset", 0));
 	shadowYOffsetSpinBox->setValue(conf->readNumEntry("ShadowYOffset", 10));
 	cbShadowDocks->setChecked(conf->readBoolEntry("ShadowDocks", false));
 	cbShadowOverrides->setChecked(conf->readBoolEntry("ShadowOverrides", false));
 	cbShadowTopMenus->setChecked(conf->readBoolEntry("ShadowTopMenus", false));
 	shadowThicknessSpinBox->setValue(conf->readNumEntry("ShadowThickness", 10));
 	cbInactiveShadow->setChecked(conf->readBoolEntry("InactiveShadowEnabled", false));
 	inactiveShadowColourButton->setColor(conf->readColorEntry("InactiveShadowColour", &TQt::black));
 	inactiveShadowOpacitySlider->setValue((int)ceil(conf->readDoubleNumEntry("InactiveShadowOpacity", 0.70) * 100));
 	inactiveShadowXOffsetSpinBox->setValue(conf->readNumEntry("InactiveShadowXOffset", 0));
 	inactiveShadowYOffsetSpinBox->setValue(conf->readNumEntry("InactiveShadowYOffset", 5));
 	inactiveShadowThicknessSpinBox->setValue(conf->readNumEntry("InactiveShadowThickness", 5));

	// Third party WM
	// ==============
	conf->setGroup("ThirdPartyWM");
	TQString selectedWM = conf->readEntry("WMExecutable", "twin");
	TQString wmArguments = conf->readEntry("WMAdditionalArguments", "");

	bool found;
	int swm;
	for ( swm = 0; swm < thirdpartyWMList->count(); ++swm ) {
		if ( thirdpartyWMList->text( swm ).startsWith(selectedWM + " ") ) {
			found = TRUE;
			break;
		}
	}
	if (found == FALSE) {
		thirdpartyWMList->setCurrentItem(0);
	}
	else {
		thirdpartyWMList->setCurrentItem(swm);
	}
	thirdpartyWMArguments->setText(wmArguments);

	processEnabledDisabledTabs();

	emit TDECModule::changed(false);
}


// Writes the selected user configuration to the twin config file
void KWinDecorationModule::writeConfig( TDEConfig* conf )
{
	TQString name = decorationList->currentText();
	TQString libName = decorationLibName( name );

	TDEConfig twinConfig("twinrc");
	twinConfig.setGroup("Style");

	// General settings
	conf->writeEntry("PluginLib", libName);
	conf->writeEntry("CustomButtonPositions", cbUseCustomButtonPositions->isChecked());
	conf->writeEntry("ShowToolTips", cbShowToolTips->isChecked());
//	conf->writeEntry("MiniWindowBorders", cbUseMiniWindows->isChecked());

	// Button settings
	conf->writeEntry("ButtonsOnLeft", buttonPositionWidget->buttonsLeft() );
	conf->writeEntry("ButtonsOnRight", buttonPositionWidget->buttonsRight() );
        conf->writeEntry("BorderSize", border_size );

	// Shadow settings
	conf->writeEntry("ShadowEnabled", cbWindowShadow->isChecked());
	conf->writeEntry("ShadowColour", shadowColourButton->color());
	conf->writeEntry("ShadowOpacity", shadowOpacitySlider->value() / 100.0);
	conf->writeEntry("ShadowXOffset", shadowXOffsetSpinBox->value());
	conf->writeEntry("ShadowYOffset", shadowYOffsetSpinBox->value());
	conf->writeEntry("ShadowThickness", shadowThicknessSpinBox->value());
	conf->writeEntry("ShadowDocks", cbShadowDocks->isChecked());
	conf->writeEntry("ShadowOverrides", cbShadowOverrides->isChecked());
	conf->writeEntry("ShadowTopMenus", cbShadowTopMenus->isChecked());
	conf->writeEntry("InactiveShadowEnabled", cbInactiveShadow->isChecked());
	conf->writeEntry("InactiveShadowColour", inactiveShadowColourButton->color());
	conf->writeEntry("InactiveShadowOpacity",
			inactiveShadowOpacitySlider->value() / 100.0);
	conf->writeEntry("InactiveShadowXOffset",
			inactiveShadowXOffsetSpinBox->value());
	conf->writeEntry("InactiveShadowYOffset",
			inactiveShadowYOffsetSpinBox->value());
	conf->writeEntry("InactiveShadowThickness",
			inactiveShadowThicknessSpinBox->value());

	conf->setGroup("ThirdPartyWM");
	TQString wmExecutableName = thirdpartyWMList->currentText();
	int descStart = wmExecutableName.find(" ");
	if (descStart >= 0) {
		wmExecutableName.truncate(descStart);
	}
	if (conf->readEntry("WMExecutable", "twin") != wmExecutableName) {
		TDEProcess newWMProc;
		TQStringList wmstartupcommand;
		wmstartupcommand.split(" ", thirdpartyWMArguments->text());
		wmstartupcommand.prepend(wmExecutableName);
		wmstartupcommand.append("--replace");
		newWMProc << wmstartupcommand;
		newWMProc.start(TDEProcess::DontCare, TDEProcess::NoCommunication);
		newWMProc.detach();
	}
	conf->writeEntry("WMExecutable", wmExecutableName);
	conf->writeEntry("WMAdditionalArguments", thirdpartyWMArguments->text());

	oldLibraryName = currentLibraryName;
	currentLibraryName = libName;

	// We saved, so tell tdecmodule that there have been  no new user changes made.
	emit TDECModule::changed(false);
}


void KWinDecorationModule::dcopUpdateClientList()
{
	// Changes the current active ListBox item, and
	// Loads a new plugin configuration tab if required.
	TDEConfig twinConfig("twinrc");
	twinConfig.setGroup("Style");

	readConfig( &twinConfig );
	resetPlugin( &twinConfig );
}


// Virutal functions required by TDECModule
void KWinDecorationModule::load()
{
	TDEConfig twinConfig("twinrc");
	twinConfig.setGroup("Style");

	// Reset by re-reading the config
	readConfig( &twinConfig );
        resetPlugin( &twinConfig );
}


void KWinDecorationModule::save()
{
	TDEConfig twinConfig("twinrc");
	twinConfig.setGroup("Style");

	writeConfig( &twinConfig );
	emit pluginSave( &twinConfig );

	twinConfig.sync();
	resetKWin();
	// resetPlugin() will get called via the above DCOP function
}


void KWinDecorationModule::defaults()
{
	// Set the KDE defaults
	cbUseCustomButtonPositions->setChecked( false );
	buttonPositionWidget->setEnabled( false );
	cbShowToolTips->setChecked( true );
	cbWindowShadow->setChecked( false );
//	cbUseMiniWindows->setChecked( false);
// Don't set default for now
//	decorationList->setSelected(
//		decorationList->findItem( i18n("KDE 2") ), true );  // KDE classic client

	buttonPositionWidget->setButtonsLeft("MS");
	buttonPositionWidget->setButtonsRight("HIAX");

        border_size = BorderNormal;
        checkSupportedBorderSizes();

	shadowColourButton->setColor(Qt::black);
	shadowOpacitySlider->setValue(70);
	shadowXOffsetSpinBox->setValue(0);
	shadowYOffsetSpinBox->setValue(10);
	shadowThicknessSpinBox->setValue(10);
	cbShadowDocks->setChecked(false);
	cbShadowOverrides->setChecked(false);
	cbShadowTopMenus->setChecked(false);
	cbInactiveShadow->setChecked(false);
	inactiveShadowColourButton->setColor(Qt::black);
	inactiveShadowOpacitySlider->setValue(70);
	inactiveShadowXOffsetSpinBox->setValue(0);
	inactiveShadowYOffsetSpinBox->setValue(5);
	inactiveShadowThicknessSpinBox->setValue(5);

	// Set plugin defaults
	emit pluginDefaults();
}

void KWinDecorationModule::checkSupportedBorderSizes()
{
        TQValueList< BorderSize > sizes;
        if( plugins->factory() != NULL )
            sizes = plugins->factory()->borderSizes();
	if( sizes.count() < 2 ) {
		lBorder->hide();
		cBorder->hide();
	} else {
		cBorder->clear();
		for (TQValueList<BorderSize>::const_iterator it = sizes.begin(); it != sizes.end(); ++it) {
			BorderSize size = *it;
			cBorder->insertItem(i18n(border_names[size]), borderSizeToIndex(size,sizes) );
		}
		int pos = borderSizeToIndex( border_size, sizes );
		lBorder->show();
		cBorder->show();
		cBorder->setCurrentItem(pos);
		slotBorderChanged( pos );
	}
}

TQString KWinDecorationModule::styleToConfigLib( TQString& styleLib )
{
        if( styleLib.startsWith( "twin3_" ))
            return "twin_" + styleLib.mid( 6 ) + "_config";
        else
            return styleLib + "_config";
}

TQString KWinDecorationModule::quickHelp() const
{
	return i18n( "<h1>Window Manager Decoration</h1>"
		"<p>This module allows you to choose the window border decorations, "
		"as well as titlebar button positions and custom decoration options.</p>"
		"To choose a theme for your window decoration click on its name and apply your choice by clicking the \"Apply\" button below."
		" If you do not want to apply your choice you can click the \"Reset\" button to discard your changes."
		"<p>You can configure each theme in the \"Configure [...]\" tab. There are different options specific for each theme.</p>"
		"<p>In \"General Options (if available)\" you can activate the \"Buttons\" tab by checking the \"Use custom titlebar button positions\" box."
		" In the \"Buttons\" tab you can change the positions of the buttons to your liking.</p>" );
}


void KWinDecorationModule::resetKWin()
{
	bool ok = kapp->dcopClient()->send("twin*", "KWinInterface",
                        "reconfigure()", TQByteArray());
	if (!ok)
		kdDebug() << "kcmtwindecoration: Could not reconfigure twin" << endl;
}

#include "twindecoration.moc"
// vim: ts=4
// kate: space-indent off; tab-width 4;