summaryrefslogtreecommitdiff
path: root/src/Toolbar.cc
blob: d3d393e255cb862c87ad6296777036c7b7306c09 (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
// Toolbar.cc for Fluxbox
// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Toolbar.cc for Blackbox - an X11 Window manager
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: Toolbar.cc,v 1.74 2003/04/25 11:19:45 fluxgen Exp $

#include "Toolbar.hh"

#include "i18n.hh"
#include "fluxbox.hh"
#include "Screen.hh"
#include "Window.hh"
#include "Workspace.hh"
#include "ImageControl.hh"
#include "ToolbarTheme.hh"
#include "EventManager.hh"
#include "Text.hh"
#include "ArrowButton.hh"
#include "SimpleCommand.hh"
#include "IntResMenuItem.hh"
#include "MacroCommand.hh"
#include "RootTheme.hh"

// use GNU extensions
#ifndef	 _GNU_SOURCE
#define	 _GNU_SOURCE
#endif // _GNU_SOURCE

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#include <X11/Xutil.h>
#include <X11/keysym.h>

#include <cstring>
#include <cstdio>

#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else // !TIME_WITH_SYS_TIME
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else // !HAVE_SYS_TIME_H
#include <time.h>
#endif // HAVE_SYS_TIME_H
#endif // TIME_WITH_SYS_TIME


#include <iostream>

using namespace std;

namespace {
class SetToolbarPlacementCmd: public FbTk::Command {
public:
    explicit SetToolbarPlacementCmd(Toolbar &tbar, Toolbar::Placement place):m_tbar(tbar), m_place(place) { }
    void execute() {
        m_tbar.setPlacement(m_place);
        m_tbar.reconfigure();
        m_tbar.screen().saveToolbarPlacement(m_place);
        Fluxbox::instance()->save_rc();
    }
private:
    Toolbar &m_tbar;
    Toolbar::Placement m_place;
};


void setupMenus(Toolbar &tbar) {
    I18n *i18n = I18n::instance();
    using namespace FBNLS;
    using namespace FbTk;

    FbTk::Menu &menu = tbar.menu();
    
    RefCount<Command> start_edit(new SimpleCommand<Toolbar>(tbar, &Toolbar::edit));
    menu.insert(i18n->getMessage(
                                 FBNLS::ToolbarSet, FBNLS::ToolbarEditWkspcName,
                                 "Edit current workspace name"),
                start_edit);

    menu.setLabel(i18n->getMessage(
                                   FBNLS::ToolbarSet, FBNLS::ToolbarToolbarTitle,
                                   "Toolbar")); 

    FbTk::MenuItem *toolbar_menuitem = new IntResMenuItem("Toolbar width percent",
                                                          tbar.screen().getToolbarWidthPercentResource(),
                                                          0, 100); // min/max value


    FbTk::RefCount<FbTk::Command> reconfig_toolbar(new FbTk::
                                                   SimpleCommand<Toolbar>
                                                   (tbar, &Toolbar::reconfigure));
    FbTk::RefCount<FbTk::Command> save_resources(new FbTk::
                                                 SimpleCommand<Fluxbox>
                                                 (*Fluxbox::instance(), &Fluxbox::save_rc));
    FbTk::MacroCommand *toolbar_menuitem_macro = new FbTk::MacroCommand();
    toolbar_menuitem_macro->add(reconfig_toolbar);
    toolbar_menuitem_macro->add(save_resources);

    FbTk::RefCount<FbTk::Command> reconfig_toolbar_and_save_resource(toolbar_menuitem_macro);
    toolbar_menuitem->setCommand(reconfig_toolbar_and_save_resource);  

    tbar.menu().insert(toolbar_menuitem);

    menu.setInternalMenu();

    menu.insert("Layer...", &tbar.layermenu());

    // setup items in placement menu
    struct {
        int set;
        int base;
        const char *default_str;
        Toolbar::Placement placement;
    } place_menu[]  = {
        {0, 0, "Top Left", Toolbar::TOPLEFT},
        {0, 0, "Left Top", Toolbar::LEFTTOP},
        {0, 0, "Left Center", Toolbar::LEFTCENTER},
        {0, 0, "Left Bottom", Toolbar::LEFTBOTTOM}, 
        {0, 0, "Bottom Left", Toolbar::BOTTOMLEFT},
        {0, 0, "Top Center", Toolbar::TOPCENTER},
        {0, 0, 0, Toolbar::TOPCENTER},
        {0, 0, 0, Toolbar::BOTTOMCENTER},
        {0, 0, 0, Toolbar::BOTTOMCENTER},
        {0, 0, "Bottom Center", Toolbar::BOTTOMCENTER},
        {0, 0, "Top Right", Toolbar::TOPRIGHT},
        {0, 0, "Right Top", Toolbar::RIGHTTOP},
        {0, 0, "Right Center", Toolbar::RIGHTCENTER},
        {0, 0, "Right Bottom", Toolbar::RIGHTBOTTOM},
        {0, 0, "Bottom Right", Toolbar::BOTTOMRIGHT}
    };
    tbar.placementMenu().setMinimumSublevels(3);
    // create items in sub menu
    for (size_t i=0; i<15; ++i) {
        if (place_menu[i].default_str == 0) {
            tbar.placementMenu().insert("");
        } else {
            const char *i18n_str = i18n->getMessage(place_menu[i].set, 
                                                    place_menu[i].base,
                                                    place_menu[i].default_str);
            RefCount<FbTk::Command> setplace(new SetToolbarPlacementCmd(tbar, place_menu[i].placement));
            tbar.placementMenu().insert(i18n_str, setplace);
                                                              
        }
    }
    menu.insert("Placement", &tbar.placementMenu());
    tbar.placementMenu().update();
    menu.update();
}

}; // end anonymous

// toolbar frame
Toolbar::Frame::Frame(FbTk::EventHandler &evh, int screen_num):
    window(screen_num, // screen (parent)
           0, 0, // pos
           10, 10, // size
           // event mask
           ButtonPressMask | ButtonReleaseMask | 
           EnterWindowMask | LeaveWindowMask,
           true), // override redirect 
    workspace_label(window, // parent
                    0, 0, //pos
                    1, 1, // size
                    // event mask
                    ButtonPressMask | ButtonReleaseMask | 
                    ExposureMask | KeyPressMask |
                    EnterWindowMask | LeaveWindowMask),
    window_label(window, // parent
                  0, 0, // pos
                  1, 1, // size
                 // event mask
                 ButtonPressMask | ButtonReleaseMask | 
                 ExposureMask |
                 EnterWindowMask | LeaveWindowMask),
    clock(window, //parent
          0, 0, // pos
          1, 1, // size
          // event mask
          ButtonPressMask | ButtonReleaseMask | 
          ExposureMask |
          EnterWindowMask | LeaveWindowMask),
    psbutton(ArrowButton::LEFT, // arrow type
             window, // parent
             0, 0, // pos
             1, 1), // size
    nsbutton(ArrowButton::RIGHT, // arrow type
             window, // parent
             0, 0, // pos
             1, 1), // size
    pwbutton(ArrowButton::LEFT, // arrow type
             window, // parent
             0, 0, // pos
             1, 1), // size
    nwbutton(ArrowButton::RIGHT, // arrow type
             window, // parent
             0, 0, // pos 
             1, 1), // size
    hour(-1), // start with invalid number to force update
    minute(-1) {

    FbTk::EventManager &evm = *FbTk::EventManager::instance();
    // add windows to eventmanager
    evm.add(evh, window);
    evm.add(evh, workspace_label);
    evm.add(evh, window_label);
    evm.add(evh, clock);

}

Toolbar::Frame::~Frame() {
    FbTk::EventManager &evm = *FbTk::EventManager::instance();
    // remove windows from eventmanager
    evm.remove(window);
    evm.remove(workspace_label);
    evm.remove(window_label);
    evm.remove(clock);
}

Toolbar::Toolbar(BScreen &scrn, FbTk::XLayer &layer, FbTk::Menu &menu, size_t width):
    editing(false),
    hidden(scrn.doToolbarAutoHide()), 
    do_auto_hide(scrn.doToolbarAutoHide()),
    frame(*this, scrn.getScreenNumber()),
    m_screen(scrn),
    image_ctrl(*scrn.getImageControl()),
    clock_timer(this), 	// get the clock updating every minute
    hide_timer(&hide_handler),
    m_toolbarmenu(menu),
    m_placementmenu(*scrn.menuTheme(),
                    scrn.getScreenNumber(), *scrn.getImageControl()),
    m_layermenu(*scrn.menuTheme(), 
                scrn.getScreenNumber(), 
                *scrn.getImageControl(),
                *scrn.layerManager().getLayer(Fluxbox::instance()->getMenuLayer()), 
                this,
                true),
    m_theme(scrn.getScreenNumber()),
    m_place(BOTTOMCENTER),
    m_themelistener(*this),
    m_layeritem(frame.window, layer) {

    // we need to get notified when the theme is reloaded
    m_theme.addListener(m_themelistener);

    setupMenus(*this);

    // geometry settings
    frame.width = width;
    frame.height = frame.label_h = 10;
    frame.window_label_w = 
        frame.workspace_label_w = frame.clock_w = width/3; 
    frame.button_w = 20;
    frame.bevel_w = 1;

    timeval delay;
    delay.tv_sec = 1;
    delay.tv_usec = 0;
    clock_timer.setTimeout(delay);
    clock_timer.start();

    hide_handler.toolbar = this;
    hide_timer.setTimeout(Fluxbox::instance()->getAutoRaiseDelay());
    hide_timer.fireOnce(true);

    frame.grab_x = frame.grab_y = 0;

    display = FbTk::App::instance()->display();

    frame.base = frame.label = frame.wlabel = frame.clk = frame.button =
        frame.pbutton = None;

    //DEL/fix -> remove useIconBar resource
//    if (Fluxbox::instance()->useIconBar())
    m_iconbar.reset(new IconBar(screen(), frame.window_label.window(), m_theme.font()));


    XMapSubwindows(display, frame.window.window());
    frame.window.show();

    // finaly: setup Commands for the buttons in the frame
    typedef FbTk::SimpleCommand<BScreen> ScreenCmd;
    FbTk::RefCount<FbTk::Command> nextworkspace(new ScreenCmd(screen(), 
                                                              &BScreen::nextWorkspace));
    FbTk::RefCount<FbTk::Command> prevworkspace(new ScreenCmd(screen(), 
                                                              &BScreen::prevWorkspace));
    FbTk::RefCount<FbTk::Command> nextwindow(new ScreenCmd(screen(), 
                                                           &BScreen::nextFocus));
    FbTk::RefCount<FbTk::Command> prevwindow(new ScreenCmd(screen(), 
                                                           &BScreen::prevFocus));
    frame.psbutton.setOnClick(prevworkspace);
    frame.nsbutton.setOnClick(nextworkspace);
    frame.pwbutton.setOnClick(prevwindow);
    frame.nwbutton.setOnClick(nextwindow);


    reconfigure();
	
}


Toolbar::~Toolbar() {

    if (frame.base) image_ctrl.removeImage(frame.base);
    if (frame.label) image_ctrl.removeImage(frame.label);
    if (frame.wlabel) image_ctrl.removeImage(frame.wlabel);
    if (frame.clk) image_ctrl.removeImage(frame.clk);
    if (frame.button) image_ctrl.removeImage(frame.button);
    if (frame.pbutton) image_ctrl.removeImage(frame.pbutton);

}

bool Toolbar::isVertical() const {
    return (m_place == RIGHTCENTER ||
            m_place == RIGHTTOP ||
            m_place == RIGHTBOTTOM ||
            m_place == LEFTCENTER ||
            m_place == LEFTTOP ||
            m_place == LEFTBOTTOM);
}

void Toolbar::addIcon(FluxboxWindow *w) {
    if (m_iconbar.get() != 0)
        FbTk::EventManager::instance()->add(*this, m_iconbar->addIcon(w));
}

void Toolbar::delIcon(FluxboxWindow *w) {
    if (m_iconbar.get() != 0)
        FbTk::EventManager::instance()->remove(m_iconbar->delIcon(w));
}

void Toolbar::delAllIcons() {
    if (m_iconbar.get() == 0)
        return;

    IconBar::WindowList *deleted = m_iconbar->delAllIcons();
    IconBar::WindowList::iterator it = deleted->begin();
    IconBar::WindowList::iterator it_end = deleted->end();
    for (; it != it_end; ++it) {
        FbTk::EventManager::instance()->remove(*it);
    }
    delete deleted;
}
    
bool Toolbar::containsIcon(FluxboxWindow &win) {
    return (m_iconbar->findIcon(&win) != 0);
}

void Toolbar::enableIconBar() {
    // already on
    if (m_iconbar.get() != 0) 
        return;
    m_iconbar.reset(new IconBar(screen(), frame.window_label.window(), m_theme.font()));
}

void Toolbar::disableIconBar() {
    // already off
    if (m_iconbar.get() == 0) 
        return;
    
    delAllIcons();

    m_iconbar.reset(0); // destroy iconbar

}


void Toolbar::reconfigure() {

    if (do_auto_hide == false && 
        do_auto_hide != screen().doToolbarAutoHide()) {
        hide_timer.start();
    }

    do_auto_hide = screen().doToolbarAutoHide();

    bool vertical = isVertical();

    if (m_iconbar.get())
        m_iconbar->setVertical(vertical);

    frame.bevel_w = screen().rootTheme().bevelWidth();

    // recallibrate size
    setPlacement(m_place);

#ifdef HAVE_STRFTIME
    time_t ttmp = time(0);
    struct tm *tt = 0;

    if (ttmp != -1) {
        tt = localtime(&ttmp);
        if (tt) {
            char t[1024], *time_string = (char *) 0;
            int len = strftime(t, 1024, screen().getStrftimeFormat(), tt);

            time_string = new char[len + 1];

            memset(time_string, '0', len);
            *(time_string + len) = '\0';

            frame.clock_w = m_theme.font().textWidth(time_string, len);
            frame.clock_w += (frame.bevel_w * 4);
			
            delete [] time_string;
        } else
            frame.clock_w = 0;
    } else
        frame.clock_w = 0;
#else // !HAVE_STRFTIME
	
    I18n *i18n = I18n::instance();
    frame.clock_w = m_theme.font().textWidth(
                                             i18n->
                                             getMessage(
                                                        FBNLS::ToolbarSet, 
                                                        FBNLS::ToolbarNoStrftimeLength,
                                                        "00:00000"),
                                             strlen(i18n->
                                                    getMessage(
                                                               FBNLS::ToolbarSet, 
                                                               FBNLS::ToolbarNoStrftimeLength,
                                                               "00:00000"))) + (frame.bevel_w * 4);
	
#endif // HAVE_STRFTIME

    unsigned int i;
    unsigned int w = 0;
    frame.workspace_label_w = 0;

    for (i = 0; i < screen().getCount(); i++) {
        w = m_theme.font().textWidth(screen().getWorkspace(i)->name().c_str(),
                                     screen().getWorkspace(i)->name().size());

        w += (frame.bevel_w * 4);

        if (w > frame.workspace_label_w)
            frame.workspace_label_w = w;
    }
    
    if (frame.workspace_label_w < frame.clock_w)
        frame.workspace_label_w = frame.clock_w;
    else if (frame.workspace_label_w > frame.clock_w)
        frame.clock_w = frame.workspace_label_w;

    frame.window_label_w =
        (frame.width - (frame.clock_w + (frame.button_w * 4) +
                        frame.workspace_label_w + (frame.bevel_w * 8) + 6));
    
    if (hidden)
        frame.window.moveResize(frame.x_hidden, frame.y_hidden,
                                frame.width, frame.height);
    else {
        frame.window.moveResize(frame.x, frame.y,
                                frame.width, frame.height);
    }


    unsigned int next_x = frame.workspace_label_w;
    unsigned int next_y = frame.window.height();
    
    if (vertical) {
        next_x = frame.window.width();
        next_y = frame.workspace_label_w;
    }
    
    frame.workspace_label.moveResize(frame.bevel_w, frame.bevel_w, next_x, next_y);
    next_x = 0;
    next_y = 0;
    if (vertical) {
        next_y += frame.workspace_label.height() + 1 + frame.bevel_w * 2;
    } else {
        next_x += frame.workspace_label.width() + 1 + frame.bevel_w * 2;
    }

    frame.psbutton.moveResize(next_x , next_y,
                              frame.button_w, frame.button_w);
    if (vertical)
        next_y += frame.psbutton.height() + 1;
    else
        next_x += frame.psbutton.width() + 1;

    frame.nsbutton.moveResize(next_x, next_y,
                              frame.button_w, frame.button_w);
    size_t label_w = frame.window_label_w;
    size_t label_h = frame.height;

    if (vertical) {
        next_y += frame.nsbutton.height() + 1;
        label_w = frame.width;
        label_h = frame.window_label_w - frame.width + frame.height;

    } else
        next_x += frame.nsbutton.width() + 1;
       

    frame.window_label.moveResize(next_x, next_y,
                                  label_w, label_h);
    if (vertical)
        next_y += frame.window_label.height() + 1;
    else
        next_x += frame.window_label.width() + 1;

    frame.pwbutton.moveResize(next_x, next_y,
                              frame.button_w, frame.button_w);
    if (vertical)
        next_y += frame.pwbutton.height() + 1;
    else
        next_x += frame.pwbutton.width() + 1;

    frame.nwbutton.moveResize(next_x, next_y,
                              frame.button_w, frame.button_w);
    size_t clock_w = frame.width - next_x - frame.nwbutton.width() - 1;
    size_t clock_h = frame.height;
    if (vertical) {
        next_y += frame.nwbutton.height() + 1;
        clock_w = frame.width;
        clock_h = frame.height - next_y;
    } else
        next_x += frame.nwbutton.width() + 1;

    frame.clock.moveResize(next_x, next_y,
                           clock_w, clock_h);

    Pixmap tmp = frame.base;
    const FbTk::Texture *texture = &(m_theme.toolbar());
    if (texture->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
        frame.base = None;
        frame.window.setBackgroundColor(texture->color());
    } else {
        frame.base = image_ctrl.renderImage(frame.window.width(), 
                                            frame.window.height(), *texture);
        frame.window.setBackgroundPixmap(frame.base);
    }
    if (tmp) 
        image_ctrl.removeImage(tmp);
    
    tmp = frame.label;
    texture = &(m_theme.window());
    if (texture->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
        frame.label = None;
        frame.window_label.setBackgroundColor(texture->color());
    } else {
        frame.label =
            image_ctrl.renderImage(frame.window_label.width(), 
                                   frame.window_label.height(), *texture);
        frame.window_label.setBackgroundPixmap(frame.label);
    }
    if (tmp) image_ctrl.removeImage(tmp);

    tmp = frame.wlabel;
    texture = &(m_theme.label());
    if (texture->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
        frame.wlabel = None;
        frame.workspace_label.setBackgroundColor(texture->color());
    } else {
        frame.wlabel =
            image_ctrl.renderImage(frame.workspace_label.width(), 
                                   frame.workspace_label.height(), *texture);
        frame.workspace_label.setBackgroundPixmap(frame.wlabel);
    }
    if (tmp) image_ctrl.removeImage(tmp);

    tmp = frame.clk;
    texture = &(m_theme.clock());
    if (texture->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
        frame.clk = None;
        frame.clock.setBackgroundColor(texture->color());
    } else {
        frame.clk =
            image_ctrl.renderImage(frame.clock.width(), frame.clock.height(), *texture);
        frame.clock.setBackgroundPixmap(frame.clk);
    }
    if (tmp) image_ctrl.removeImage(tmp);

    tmp = frame.button;
    texture = &(m_theme.button());
    if (texture->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
        frame.button = None;

        const FbTk::Color &color = texture->color();
        frame.psbutton.setBackgroundColor(color);
        frame.nsbutton.setBackgroundColor(color);
        frame.pwbutton.setBackgroundColor(color);
        frame.nwbutton.setBackgroundColor(color);
    } else {
        frame.button =
            image_ctrl.renderImage(frame.button_w, frame.button_w, *texture);
        
        frame.psbutton.setBackgroundPixmap(frame.button);
        frame.nsbutton.setBackgroundPixmap(frame.button);
        frame.pwbutton.setBackgroundPixmap(frame.button);
        frame.nwbutton.setBackgroundPixmap(frame.button);
    }
    if (tmp) 
        image_ctrl.removeImage(tmp);

    // pressed button pixmap
    tmp = frame.pbutton;
    texture = &(m_theme.pressedButton());
    if (texture->type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) {
        frame.pbutton = None;
    } else {
        frame.pbutton =
            image_ctrl.renderImage(frame.button_w, frame.button_w, *texture);
        frame.psbutton.setPressedPixmap(frame.pbutton);
        frame.nsbutton.setPressedPixmap(frame.pbutton);
        frame.pwbutton.setPressedPixmap(frame.pbutton);
        frame.nwbutton.setPressedPixmap(frame.pbutton);
    }
    // setup button gc
    frame.psbutton.setGC(m_theme.buttonPicGC());
    frame.nsbutton.setGC(m_theme.buttonPicGC());
    frame.pwbutton.setGC(m_theme.buttonPicGC());
    frame.nwbutton.setGC(m_theme.buttonPicGC());

    if (tmp) 
        image_ctrl.removeImage(tmp);

    frame.window.setBorderColor(screen().rootTheme().borderColor());
    frame.window.setBorderWidth(screen().rootTheme().borderWidth());

    frame.window.clear();

    frame.workspace_label.clear();
    frame.window_label.clear();
    frame.clock.clear();
    frame.psbutton.clear();
    frame.nsbutton.clear();
    frame.pwbutton.clear();
    frame.nwbutton.clear();
	
    redrawWindowLabel();
    redrawWorkspaceLabel();
    checkClock(true);

    m_toolbarmenu.reconfigure();

}



void Toolbar::checkClock(bool redraw, bool date) {
    time_t tmp = 0;
    struct tm *tt = 0;

    if ((tmp = time(NULL)) != -1) {
        if (! (tt = localtime(&tmp))) {
            cerr<<__FILE__<<"("<<__LINE__<<"): ! localtime(&tmp)"<<endl;
            return;
        }
        if (tt->tm_min != frame.minute || tt->tm_hour != frame.hour) {
            frame.hour = tt->tm_hour;
            frame.minute = tt->tm_min;
            frame.clock.clear();
            redraw = true;
        }
    } else
        cerr<<__FILE__<<"("<<__LINE__<<"): time(null)<0"<<endl;
	

    if (!redraw)
        return;

    frame.clock.clear();
#ifdef HAVE_STRFTIME
    char t[1024];
    if (! strftime(t, 1024, screen().getStrftimeFormat(), tt))
        return;
#else // !HAVE_STRFTIME
    I18n *i18n = I18n::instance();
    char t[9];
    if (date) {
        // format the date... with special consideration for y2k ;)
        if (screen().getDateFormat() == Fluxbox::B_EUROPEANDATE) {
            sprintf(t,
                    i18n->getMessage(
                                     FBNLS::ToolbarSet, FBNLS::ToolbarNoStrftimeDateFormatEu,
                                     "%02d.%02d.%02d"),
                    tt->tm_mday, tt->tm_mon + 1,
                    (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
        } else {
            sprintf(t,
                    i18n->getMessage(
                                     FBNLS::ToolbarSet, FBNLS::ToolbarNoStrftimeDateFormat,
                                     "%02d/%02d/%02d"),
                    tt->tm_mon + 1, tt->tm_mday,
                    (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
        }
    } else {
        if (screen().isClock24Hour()) {
            sprintf(t,
                    i18n->getMessage(
                                     FBNLS::ToolbarSet, FBNLS::ToolbarNoStrftimeTimeFormat24,
                                     "	%02d:%02d "),
                    frame.hour, frame.minute);
        } else {
            sprintf(t,
                    i18n->getMessage(
                                     FBNLS::ToolbarSet, FBNLS::ToolbarNoStrftimeTimeFormat12,
                                     "%02d:%02d %sm"),
                    ((frame.hour > 12) ? frame.hour - 12 :
                     ((frame.hour == 0) ? 12 : frame.hour)), frame.minute,
                    ((frame.hour >= 12) ?
                     i18n->getMessage(
                                      FBNLS::ToolbarSet, FBNLS::ToolbarNoStrftimeTimeFormatP,
                                      "p") :
                     i18n->getMessage(
                                      FBNLS::ToolbarSet, FBNLS::ToolbarNoStrftimeTimeFormatA,
                                      "a")));
        }
    }
#endif // HAVE_STRFTIME

    unsigned int newlen = strlen(t);
    int dx = FbTk::doAlignment(frame.clock_w,
                               frame.bevel_w*2,
                               m_theme.justify(),
                               m_theme.font(),
                               t, strlen(t), newlen);
    int dy = 1 + m_theme.font().ascent();
    if (m_theme.font().isRotated()) {
        int tmp = dy;
        dy = frame.clock.height() - dx;
        dx = tmp;
    }		
    frame.clock.clear();
    m_theme.font().drawText(
                            frame.clock.window(),
                            screen().getScreenNumber(),
                            m_theme.clockTextGC(),
                            t, newlen,
                            dx, dy);

}


void Toolbar::redrawWindowLabel(bool redraw) {
    if (Fluxbox::instance()->getFocusedWindow()) {
        if (redraw)
            frame.window_label.clear();

        FluxboxWindow *foc = Fluxbox::instance()->getFocusedWindow();
        // don't draw focused window if it's not on the same screen
        if (&foc->getScreen() != &screen() || foc->getTitle().size() == 0)
            return;
		
        unsigned int newlen = foc->getTitle().size();
        int dx = FbTk::doAlignment(frame.window_label_w, frame.bevel_w*2,
                                   m_theme.justify(),
                                   m_theme.font(),
                                   foc->getTitle().c_str(), 
                                   foc->getTitle().size(), newlen);
	int dy = 1 + m_theme.font().ascent();

        if (m_theme.font().isRotated()) {
            int tmp = dy;
            dy = frame.window_label.height() - dx;
            dx = tmp;
        }
    
        m_theme.font().drawText(
                                frame.window_label.window(),
                                screen().getScreenNumber(),
                                m_theme.windowTextGC(),
                                foc->getTitle().c_str(), newlen,
                                dx, dy);
    } else
        frame.window_label.clear();
}
 
 
void Toolbar::redrawWorkspaceLabel(bool redraw) {
    if (screen().getCurrentWorkspace()->name().size()==0)
        return;
		
    if (redraw)
        frame.workspace_label.clear();
		
    const char *text = screen().getCurrentWorkspace()->name().c_str();
    size_t textlen = screen().getCurrentWorkspace()->name().size();
    unsigned int newlen = textlen;
    int dx = FbTk::doAlignment(frame.workspace_label_w, frame.bevel_w,
                               m_theme.justify(),
                               m_theme.font(),
                               text, textlen, newlen);
    int dy = 1 + m_theme.font().ascent();
    if (m_theme.font().isRotated()) {
        int tmp = dy;
        dy = frame.workspace_label_w - dx;
        dx = tmp;
    }
    m_theme.font().drawText(
                            frame.workspace_label.window(),
                            screen().getScreenNumber(),
                            m_theme.labelTextGC(),
                            text, newlen,
                            dx, dy);

}

void Toolbar::edit() {
    Window window;
    int foo;

    editing = true;	//mark for editing
	
    //workspace label already has intput focus ?
    if (XGetInputFocus(display, &window, &foo) &&
        window == frame.workspace_label)
        return;

    //set input focus to workspace label
    XSetInputFocus(display, frame.workspace_label.window(),
                   ((screen().isSloppyFocus() || screen().isSemiSloppyFocus()) ?
                    RevertToPointerRoot : RevertToParent), CurrentTime);

    frame.workspace_label.clear();
    Fluxbox * const fluxbox = Fluxbox::instance();
    fluxbox->setNoFocus(true);
    if (fluxbox->getFocusedWindow())	//disable focus on current focused window
        fluxbox->getFocusedWindow()->setFocusFlag(false);

    XDrawRectangle(display, frame.workspace_label.window(),
                   screen().winFrameTheme().labelTextFocusGC(),
                   frame.workspace_label_w / 2, 0, 1,
                   frame.label_h - 1);
}


void Toolbar::buttonPressEvent(XButtonEvent &be) {
    FluxboxWindow *fluxboxwin=0;
    if (be.button == 1) {
        if ( m_iconbar.get() != 0 ) {
            if ( (fluxboxwin = m_iconbar->findWindow(be.window)) )
                fluxboxwin->deiconify();
        }
#ifndef	 HAVE_STRFTIME
        else if (be.window == frame.clock) {
            frame.clock.clear();
            checkClock(true, true);
        }
#endif // HAVE_STRFTIME
    } else if (be.button == 3) {
        FluxboxWindow *fluxboxwin = 0;
        // if we clicked on a icon then show window menu
        if ( m_iconbar.get() != 0 && (fluxboxwin = m_iconbar->findWindow(be.window)) ) {
            const FbTk::Menu &wm = fluxboxwin->getWindowmenu();

            int menu_y = be.y_root - wm.height();
            int menu_x = be.x_root;
            // make sure the menu is visible
            if (menu_y < 0) {
                menu_y = 0;
            }
            if (menu_x < 0) {
                menu_x = 0;
            } else if (menu_x + wm.width() > screen().getWidth()) {
                menu_x = screen().getWidth() - wm.width();
            }
            fluxboxwin->showMenu(menu_x, menu_y);

        } else if (! m_toolbarmenu.isVisible()) {
            int x, y;

            x = be.x_root - (m_toolbarmenu.width() / 2);
            y = be.y_root - (m_toolbarmenu.height() / 2);

            if (x < 0)
                x = 0;
            else if (x + m_toolbarmenu.width() > screen().getWidth())
                x = screen().getWidth() - m_toolbarmenu.width();

            if (y < 0)
                y = 0;
            else if (y + m_toolbarmenu.height() > screen().getHeight())
                y = screen().getHeight() - m_toolbarmenu.height();

            m_toolbarmenu.move(x, y);
            m_toolbarmenu.show();
        } else
            m_toolbarmenu.hide();
			
    } 
	
}


void Toolbar::buttonReleaseEvent(XButtonEvent &re) {
    if (re.button == 1) {
        if (re.window == frame.workspace_label) {
            FbTk::Menu *menu = screen().getWorkspacemenu();
            //move the workspace label and make it visible
            menu->move(re.x_root, re.y_root);
            // make sure the entire menu is visible 
            //!!TODO: this is repeated by other menus, make a function!)
            int newx = menu->x(); // new x position of menu
            int newy = menu->y(); // new y position of menu
            if (menu->x() < 0)
                newx = 0;
            else if (menu->x() + menu->width() > screen().getWidth())
                newx = screen().getWidth() - menu->width();
			
            if (menu->y() < 0)
                newy = 0;
            else if (menu->y() + menu->height() > screen().getHeight())
                newy = screen().getHeight() - menu->height();
            // move and show menu
            menu->move(newx, newy);
            menu->show();
        } else if (re.window == frame.window_label)
            screen().raiseFocus();
#ifndef	 HAVE_STRFTIME
        else if (re.window == frame.clock) {
            frame.clock.clear();
            checkClock(true);
        }
#endif // HAVE_STRFTIME
    } else if (re.button == 4) //mousewheel scroll up
        screen().nextWorkspace(1);
    else if (re.button == 5)	//mousewheel scroll down
        screen().prevWorkspace(1);
}


void Toolbar::enterNotifyEvent(XCrossingEvent &not_used) {
    if (! do_auto_hide)
        return;

    if (hidden) {
        if (! hide_timer.isTiming())
            hide_timer.start();
    } else {
        if (hide_timer.isTiming())
            hide_timer.stop();
    }
}

void Toolbar::leaveNotifyEvent(XCrossingEvent &not_used) {
    if (! do_auto_hide)
        return;

    if (hidden) {
        if (hide_timer.isTiming()) 
            hide_timer.stop();
    } else if (! m_toolbarmenu.isVisible() && ! hide_timer.isTiming()) 
        hide_timer.start();

}


void Toolbar::exposeEvent(XExposeEvent &ee) {
    if (ee.window == frame.clock) 
        checkClock(true);
    else if (ee.window == frame.workspace_label && (! editing))
        redrawWorkspaceLabel();
    else if (m_iconbar.get() != 0)
        m_iconbar->exposeEvent(&ee);
}


void Toolbar::keyPressEvent(XKeyEvent &ke) {
    if (ke.window != frame.workspace_label.window() || !editing)
        return;
		
    KeySym ks;
    char keychar[1];
    XLookupString(&ke, keychar, 1, &ks, 0);

    if (ks == XK_Return || ks == XK_Escape) {			
        editing = false;
        Fluxbox * const fluxbox = Fluxbox::instance();			
        fluxbox->setNoFocus(false);
        if (fluxbox->getFocusedWindow()) {
            fluxbox->getFocusedWindow()->setInputFocus();
            fluxbox->getFocusedWindow()->setFocusFlag(true);
        } else
            XSetInputFocus(display, PointerRoot, None, CurrentTime);
			
        if (ks == XK_Return)	//change workspace name if keypress = Return
            screen().getCurrentWorkspace()->setName(new_workspace_name.c_str());

        new_workspace_name.erase(); //erase temporary workspace name
        reconfigure();
        //save workspace names
        Fluxbox::instance()->save_rc();

    } else if (! IsModifierKey(ks) && !IsCursorKey(ks)) {

        if (ks == XK_BackSpace && new_workspace_name.size())
            new_workspace_name.erase(new_workspace_name.size()-1);
        else
            new_workspace_name += keychar[0];


        frame.workspace_label.clear();
        int l = new_workspace_name.size(), tw, x;

        tw = m_theme.font().textWidth(new_workspace_name.c_str(), l);
        x = (frame.workspace_label_w - tw) / 2;

        if (x < (signed) frame.bevel_w)
            x = frame.bevel_w;
        int dy = 1 + m_theme.font().ascent();
        if (m_theme.font().isRotated()) {
            int tmp = dy;
            dy = frame.workspace_label_w - x;
            x = tmp;
        }

        m_theme.font().drawText(frame.workspace_label.window(),
                                screen().getScreenNumber(),
                                screen().winFrameTheme().labelTextFocusGC(),
                                new_workspace_name.c_str(), l,
                                x, dy);

        XDrawRectangle(display, frame.workspace_label.window(),
                       screen().winFrameTheme().labelTextFocusGC(),
                       x + tw, 0, 1,
                       frame.label_h - 1);
    }		
}


void Toolbar::timeout() {
    checkClock(true);

    timeval delay;
    delay.tv_sec = 1;
    delay.tv_usec = 0;	
    clock_timer.setTimeout(delay);
}


void Toolbar::setPlacement(Toolbar::Placement where) {
    int head_x = 0,
        head_y = 0,
        head_w,
        head_h;

    m_place = where;

    head_w = screen().getWidth();
    head_h = screen().getHeight();

    frame.width = head_w * screen().getToolbarWidthPercent() / 100;
    frame.height = m_theme.font().height();

    frame.height += 2;
    frame.height += (frame.bevel_w * 2);


    // should we flipp sizes?
    if (isVertical()) {
        frame.width = frame.height;
        frame.height = head_h * screen().getToolbarWidthPercent() / 100;
        if (!m_theme.font().isRotated())
            m_theme.font().rotate(90); // rotate to vertical text

        frame.label_h = frame.width;
        frame.button_w = frame.width;
    } else { // horizontal toolbar
        if (m_theme.font().isRotated()) 
            m_theme.font().rotate(0); // rotate to horizontal text
        frame.label_h = frame.height;
        frame.button_w = frame.height;    
    }

    switch (where) {
    case TOPLEFT:
        frame.x = head_x;
        frame.y = head_y;
        frame.x_hidden = head_x;
        frame.y_hidden = head_y +
            screen().rootTheme().bevelWidth() - 
            screen().rootTheme().borderWidth() - frame.height;
        break;

    case BOTTOMLEFT:
        frame.x = head_x;
        frame.y = head_y + head_h - frame.height - 
            screen().rootTheme().borderWidth()*2;
        frame.x_hidden = head_x;
        frame.y_hidden = head_y + head_h - screen().rootTheme().bevelWidth() - 
            screen().rootTheme().borderWidth();
        break;

    case TOPCENTER:
        frame.x = head_x + ((head_w - frame.width) / 2);
        frame.y = head_y;
        frame.x_hidden = frame.x;
        frame.y_hidden = head_y +
            screen().rootTheme().bevelWidth() - 
            screen().rootTheme().borderWidth() - frame.height;
        break;
    case TOPRIGHT:
        frame.x = head_x + head_w - frame.width - screen().rootTheme().borderWidth()*2;
        frame.y = head_y;
        frame.x_hidden = frame.x;
        break;

    case BOTTOMRIGHT:
        frame.x = head_x + head_w - frame.width - 
            screen().rootTheme().borderWidth()*2;
        frame.y = head_y + head_h - frame.height - 
            screen().rootTheme().borderWidth()*2;
        frame.x_hidden = frame.x;
        frame.y_hidden = head_y + head_h - screen().rootTheme().bevelWidth() - 
            screen().rootTheme().borderWidth();
        break;

    case BOTTOMCENTER: // default is BOTTOMCENTER
    default:
        frame.x = head_x + (head_w - frame.width) / 2;
        frame.y = head_y + head_h - frame.height - screen().rootTheme().borderWidth()*2;
        frame.x_hidden = frame.x;
        frame.y_hidden = head_y + head_h - screen().rootTheme().bevelWidth() - 
            screen().rootTheme().borderWidth();
        break;
    case LEFTCENTER:
        frame.x = head_x;
        frame.y = head_y + (head_h - frame.height)/2;
        frame.x_hidden = frame.x - frame.width + 
            screen().rootTheme().bevelWidth() + screen().rootTheme().borderWidth();
        frame.y_hidden = frame.y;
        break;
    case LEFTTOP:
        frame.x = head_x;
        frame.y = head_y;
        frame.x_hidden = frame.x - frame.width + 
            screen().rootTheme().bevelWidth() + screen().rootTheme().borderWidth();
        frame.y_hidden = frame.y;
        break;
    case LEFTBOTTOM:
        frame.x = head_x;
        frame.y = head_y + head_h - frame.height;
        frame.x_hidden = frame.x - frame.width + 
            screen().rootTheme().bevelWidth() + screen().rootTheme().borderWidth();
        frame.y_hidden = frame.y;
        break;
    case RIGHTCENTER:
        frame.x = head_x + head_w - frame.width;
        frame.y = head_y + (head_h - frame.height)/2;
        frame.x_hidden = frame.x + frame.width - 
            screen().rootTheme().bevelWidth() - screen().rootTheme().borderWidth();
        frame.y_hidden = frame.y;
        break;
    case RIGHTTOP:
        frame.x = head_x + head_w - frame.width;
        frame.y = head_y;
        frame.x_hidden = frame.x + frame.width - 
            screen().rootTheme().bevelWidth() - screen().rootTheme().borderWidth();
        frame.y_hidden = frame.y;
        break;
    case RIGHTBOTTOM:
        frame.x = head_x + head_w - frame.width;
        frame.y = head_y + head_h - frame.height;
        frame.x_hidden = frame.x + frame.width - 
            screen().rootTheme().bevelWidth() - screen().rootTheme().borderWidth();
        frame.y_hidden = frame.y;
        break;
    }

}

void Toolbar::HideHandler::timeout() {
    if (toolbar->isEditing()) { // don't hide if we're editing workspace label
        toolbar->hide_timer.fireOnce(false);
        toolbar->hide_timer.start(); // restart timer and try next timeout
        return;
    }

    toolbar->hide_timer.fireOnce(true);

    toolbar->hidden = ! toolbar->hidden;
    if (toolbar->hidden) {
        toolbar->frame.window.move(toolbar->frame.x_hidden, toolbar->frame.y_hidden);
    } else {
        toolbar->frame.window.move(toolbar->frame.x, toolbar->frame.y);
    }
}

void Toolbar::moveToLayer(int layernum) {
    m_layeritem.moveToLayer(layernum); 
    m_screen.saveToolbarLayer((Fluxbox::Layer) layernum); 
}