File: SFileDialog.cpp

package info (click to toggle)
yudit 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 18,472 kB
  • sloc: cpp: 76,344; perl: 5,630; makefile: 989; ansic: 823; sh: 441
file content (858 lines) | stat: -rw-r--r-- 21,929 bytes parent folder | download
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
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 1997-2023  Gaspar Sinai <gaspar@yudit.org>  
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "swidget/SFileDialog.h"
#include "swidget/SIcon.h"
#include "swidget/SIconFactory.h"
#include "stoolkit/SUtil.h"
#include "stoolkit/SEncoder.h"


/**
 * Hidden  Filter        Type
 * Directories Files    Types
 * +--------+ +-------+ +---+ 
 * |        | |       | |   |
 * |        | |       | |   |
 * |        | |       | |   |
 * +--------+ +-------+ +---+ 
 * ______directory       
 * ______pathname___________       
 * OK   CD   mkdir   Cancel
 */
SFileDialog::SFileDialog (void)
{
  setFrameListener (this);
  hiddenButton = new SButton (translate ("Show Hidden"), 
        SIconFactory::getIcon("CircleOff"));
  hiddenButton->setButtonListener (this);
  hiddenButton->setAlignment (SD_Center);
  hiddenButton->setIcon(SIconFactory::getIcon("CircleOff"));

  homeButton = new SButton (translate ("Home"), SIconFactory::getIcon("Home"));
  homeButton->setButtonListener (this);
  homeButton->setAlignment (SD_Center);
  add (homeButton);
   
  isFocused = false;
  isCancel = true;
  showHidden = false;
  setHidden ();
  add (hiddenButton);
  yesButton = new SButton (translate ("OK"), SIconFactory::getIcon("Yes"));
  yesButton->setAlignment (SD_Center);
  yesButton->setButtonListener (this);

  cdButton = new SButton (translate ("CD to"), SIconFactory::getIcon("Folder"));
  cdButton->setAlignment (SD_Center);
  cdButton->setButtonListener (this);

  createButton = new SButton (translate ("Make Folder"),  SIconFactory::getIcon("CreateFolder"));
  createButton->setAlignment (SD_Center);
  createButton->setButtonListener (this);

  cancelButton = new SButton (translate ("Cancel"), SIconFactory::getIcon("Cancel"));
  cancelButton->setAlignment (SD_Center);
  cancelButton->setButtonListener (this);
  add (yesButton);
  add (cdButton);
  add (createButton);
  add (cancelButton);
  folderListBox = new SListBox(translate ("Folder"));
  folderListBox->setListListener (this);
  add (folderListBox);
  fileListBox = new SListBox(translate ("File"));
  fileListBox->setListListener (this);
  add (fileListBox);
  fileTypeListBox = new SListBox(translate ("File Type"));
  fileTypeListBox->setListListener (this);
  add (fileTypeListBox);


  fileTitleLabel = new SLabel (translate ("File:"));
  add (fileTitleLabel);
  fileTextEdit = new STextEdit("");
  fileTextEdit->setMultiline(false);
  fileTextEdit->setLineEndMark(false);
  fileTextEdit->addTextEditLS(this);
  add (fileTextEdit);

  folderTitleLabel = new SLabel (translate ("Folder:"));
  add (folderTitleLabel);
  folderNameLabel = new SLabel ("");
  add (folderNameLabel);

  filterTitleLabel  = new SLabel (translate ("Filter:"));
  add (filterTitleLabel);
  fileFilter = "*";
  filterTextEdit = new STextEdit(fileFilter);
  filterTextEdit->addTextEditLS(this);

  add (filterTextEdit);
  filterTextEdit->setMultiline(false);
  filterTextEdit->setLineEndMark(false);
  fileTypeButton = new SButton (
     translate ("Best File Type"), SIconFactory::getIcon("Best"));
  fileTypeButton->setButtonListener (this);
  add (fileTypeButton);
  fileTypeLabel = new SLabel ("[]");
  add (fileTypeLabel);

  textDialog = new STextDialog();
  textDialog->setModal (this);
  recalc();
}

bool
SFileDialog::setFileType (const SString& _fileType)
{
  bool exists = true;
  if (!fileTypeListBox->selectText (_fileType))
  {
    fileTypeVector.append (_fileType);
    fileTypeVector.sort ();
    fileTypeListBox->setText(fileTypeVector);
    exists = false;
  }
  fileTypeListBox->selectText (_fileType);
  fileTypeChanged (_fileType);
  return exists;
}

const SStringVector&
SFileDialog::getFileTypes() const
{
  return fileTypeVector;
}

void
SFileDialog::setApplicationImage (const SImage& image)
{
  getComponentWindow()->setApplicationImage (image);
  textDialog->setApplicationImage (image);
}

void
SFileDialog::fileTypeChanged (const SString& _filetype)
{
  if (_filetype == "utf-8") {
    fileTypeButton->setIcon (SIconFactory::getIcon("Best"));
  } else {
    fileTypeButton->setIcon (SIconFactory::getIcon("BestEmpty"));
  }
  //SString a="[";
  SString a;
  a.append(_filetype);
  //a.append("]");
  fileTypeLabel->setText (a);
  fileTypeText = _filetype;
}

void
SFileDialog::rereadDir()
{
  filterChanged ();
  /* get directories and files */
  while (!currentDirectory.exists() || !currentDirectory.readable())
  {
    currentDirectory.cd ("..");
    if (currentDirectory.getUnixName() == "/") break;
  }
  SStringVector files = currentDirectory.list("*");
  SStringVector combedFiles;
  unsigned int i;
  if (showHidden)
  {
     combedFiles = files;
  }
  else
  {
    for (i=0; i<files.size(); i++)
    {
      SString s = files[i];
      if (s.size() == 0 || s[0] == '.') continue;
      if (fileFilter.size()==0 || s.match (fileFilter))
      {
        combedFiles.append (s);
      }
    }
  }
  combedFiles.sort();
  fileListBox->setText(combedFiles);

  SStringVector dirs = currentDirectory.list(SDir::SE_DIR);
  SStringVector combedDirs;
  if (showHidden)
  {
     combedDirs = dirs;
  }
  else
  {
    for (i=0; i<dirs.size(); i++)
    {
      SString s = dirs[i];
      if (s.size() == 0 || s[0] == '.') continue;
      combedDirs.append (s);
    }
  }
  combedDirs.sort();
  if (currentDirectory.getUnixName () != "/")
  {
    combedDirs.insert (0, "..");
  }
  folderListBox->setText(combedDirs);
  folderNameLabel->setText (currentDirectory.getUnixName ());
  // This bug has been with us for some time now :)
  fileListBox->selectText (fileTextEdit->getText());
}

void
SFileDialog::setFileTypes(const SStringVector& v)
{
  fileTypeVector = v;
  fileTypeVector.sort();
  fileTypeListBox->setText(fileTypeVector);
}


SFileDialog::~SFileDialog ()
{
}


/**
 * recalculate the geometry
 * it is based upon 300x200 layout.
 * preferredSize will be calculated here.
 */
void
SFileDialog::recalc ()
{
  unsigned int bw = 1;
  // MARGIN 
  int MGN = (int) (SAwt::getScale() * 5); 

  if (yesButton->getPreferredSize().width > bw )
  {
     bw = yesButton->getPreferredSize().width;
  }
  if (cdButton->getPreferredSize().width > bw )
  {
     bw = cdButton->getPreferredSize().width;
  }
  if (createButton->getPreferredSize().width > bw )
  {
     bw = createButton->getPreferredSize().width;
  }
  if (cancelButton->getPreferredSize().width > bw )
  {
     bw = cancelButton->getPreferredSize().width;
  }
  unsigned int bh= cancelButton->getPreferredSize().height;
  int currentx= MGN;

  SDimension hms = homeButton->getPreferredSize();
  SDimension hs = hiddenButton->getPreferredSize();
  SDimension bs = fileTypeButton->getPreferredSize();
  SDimension fs = filterTitleLabel->getPreferredSize();
  int filterWidth = (int) (SAwt::getScale() * 80.0); //* scale.

  unsigned int bestWidthBottom = bw * 4 + MGN*MGN + 3 *MGN;
  /* top */
  unsigned int bestWidthTop = MGN + hms.width + MGN + hs.width 
          + MGN +fs.width + filterWidth + bs.width + MGN + 2*MGN;
  unsigned int bestWidth = (bestWidthTop > bestWidthBottom) ?
        bestWidthTop : bestWidthBottom;
  preferredSize = SDimension (bestWidth+2*MGN,  (bestWidth+2*MGN)*3/5);

  unsigned int PW = preferredSize.width * 2;
  unsigned int PH = preferredSize.height * 2;

  int gap = (PW - MGN - MGN - ((int)bw * 4))/3;

//fprintf (stderr, "currentX[0] - %d\n", currentx);
  yesButton->setLayout (
    SLayout (
      SLocation (currentx, PH-MGN-bh),
      SLocation (currentx+(int)bw, PH-MGN),
      SLocation (0, 100),
      SLocation (0, 100)
    )
  );
  currentx = currentx + (int) bw + gap;
//fprintf (stderr, "currentX[1] - %d\n", currentx);
  cdButton->setLayout (
    SLayout (
      SLocation (currentx, PH-MGN-bh),
      SLocation (currentx+(int)bw, PH-MGN),
      SLocation (33, 100),
      SLocation (33, 100)
    )
  );
  currentx = currentx + (int) bw + gap;
//fprintf (stderr, "currentX[4] - %d\n", currentx);
  createButton->setLayout (
    SLayout (
      SLocation (currentx, PH-MGN-bh),
      SLocation (currentx+(int)bw, PH-MGN),
      SLocation (66, 100),
      SLocation (66, 100)
    )
  );
  currentx = currentx + (int) bw + gap;
//fprintf (stderr, "currentX[4] - %d\n", currentx);
  cancelButton->setLayout (
    SLayout (
      SLocation (currentx, PH-MGN-bh),
      SLocation (currentx+(int)bw, PH-MGN),
      SLocation (99, 100),
      SLocation (100, 100)
    )
  );
  homeButton->setLayout (
    SLayout (
      SLocation (MGN, MGN),
      SLocation ((int)hms.width+MGN, (int)hms.height+MGN),
      SLocation (0, 0),
      SLocation (0, 0)
    )
  );
  hiddenButton->setLayout (
    SLayout (
      SLocation ((int)hms.width+MGN+MGN, MGN),
      SLocation ((int)hms.width + hs.width+10, (int)hs.height+MGN),
      SLocation (0, 0),
      SLocation (0, 0)
    )
  );
  fileTypeButton->setLayout (
    SLayout (
      SLocation (PW-MGN-(int)bs.width, MGN),
      SLocation (PW-MGN, (int)bs.height+MGN),
      SLocation (100, 0),
      SLocation (100, 0)
    )
  );
  int PX12 = (int) (SAwt::getScale() * 12); //* scale.
  filterTitleLabel->setLayout (
    SLayout (
      SLocation ((int)hs.width+(int)hms.width+PX12, MGN),
      SLocation ((int) fs.width + (int)hs.width+(int)hms.width+PX12, (int) fs.height+MGN),
      SLocation (0, 0),
      SLocation (0, 0)
    )
  );

  filterTextEdit->setLayout (
    SLayout (
      SLocation ((int) fs.width + (int)hs.width+(int)hms.width+PX12, MGN),
      SLocation ((int) fs.width + filterWidth + (int)hs.width+(int)hms.width+PX12, 
             (int) fs.height+MGN),
      SLocation (0, 0),
      SLocation (0, 0)
    )
  );
 
  /* this will be the text field height */
  unsigned int tfh = fileTextEdit->getPreferredSize().height;
  unsigned int phw = fileTitleLabel->getPreferredSize().width;

  unsigned int dph = folderTitleLabel->getPreferredSize().height;
  unsigned int dpw = folderTitleLabel->getPreferredSize().width;

  folderTitleLabel->setLayout (
     SLayout (
        SLocation (MGN, PH-(int)bh-(int)tfh -(int) dph - MGN-MGN),
        SLocation (MGN + dpw, PH-(int)bh -(int)tfh - MGN-MGN),
        SLocation (0, 100),
        SLocation (0, 100)
     )
  );

  folderNameLabel->setLayout (
     SLayout (
        SLocation (MGN+dpw, PH-(int)bh-(int)tfh -(int) dph - MGN-MGN),
        SLocation (PW-MGN, PH-(int)bh -(int)tfh - MGN-MGN),
        SLocation (0, 100),
        SLocation (100, 100)
     )
  );

  fileTitleLabel->setLayout (
     SLayout (
        SLocation (MGN, PH-(int)bh-(int)tfh - MGN-MGN),
        SLocation (MGN + phw, PH-(int)bh - MGN-MGN),
        SLocation (0, 100),
        SLocation (0, 100)
     )
  );
  int fileTypeWidth = (int) (SAwt::getScale() * 115.0); //* scale.

  fileTextEdit->setLayout (
     SLayout (
        SLocation (MGN+phw, PH-(int)bh-(int)tfh - MGN-MGN),
        SLocation (PW-MGN-fileTypeWidth, PH-(int)bh - MGN-MGN),
        SLocation (0, 100),
        SLocation (100, 100)
     )
  );
  fileTypeLabel->setLayout (
     SLayout (
        SLocation (PW-MGN-fileTypeWidth, PH-(int)bh-(int)tfh - MGN-MGN),
        SLocation (PW-MGN, PH-(int)bh - MGN-MGN),
        SLocation (100, 100),
        SLocation (100, 100)
     )
  );

  unsigned int incr = (PW-MGN-MGN)/3;
  folderListBox->setLayout (
    SLayout (
      SLocation (MGN, (int)hs.height+MGN+MGN),
      SLocation (MGN+incr-2, PH-(int)bh-(int)tfh -(int) dph - MGN-MGN),
      SLocation (0, 0),
      SLocation (33, 100)
    )
  );

  fileListBox->setLayout (
    SLayout (
      SLocation (MGN+incr+1, (int)hs.height+MGN+MGN),
      SLocation (PW-MGN-incr-1, PH-(int)bh-(int)tfh-(int) dph  - MGN-MGN),
      SLocation (33, 0),
      SLocation (66, 100)
    )
  );

  fileTypeListBox->setLayout (
    SLayout (
      SLocation (PW-MGN-incr+2, (int)hs.height+MGN+MGN),
      SLocation (PW-MGN, PH-(int)bh-(int)tfh-(int) dph  - MGN-MGN),
      SLocation (66, 0),
      SLocation (100, 100)
    )
  );

  /* This was our layout... */
  forceLayout (SLayout (SDimension (PW,PH)));

  /* But this one is the one that works */
  //orig setLayout (SLayout (preferredSize));
  setLayout (SLayout (SDimension (PW,PH)));

  setMinimumSize (preferredSize);

  resize (SDimension (preferredSize.width, preferredSize.height));

}

SString
SFileDialog::getFileName ()
{
  SString fn = fileTextEdit->getText();
  if (fn.size()==0) return SString(fn);

  SString filePath = currentDirectory.getName();
  if (filePath != "/") filePath.append ("/"); 
  filePath.append (fn);
  return SString(filePath);
}

SString
SFileDialog::getUnixFileName ()
{
  SString fn = fileTextEdit->getText();
  if (fn.size()==0) return SString(fn);
  SString filePath = currentDirectory.getUnixName();
  if (filePath != "/") filePath.append ("/"); 
  filePath.append (fn);
  return SString(filePath);
}

SString
SFileDialog::getFileType ()
{
  return fileTypeText;
}

bool
SFileDialog::getInput (const SString& titleString, 
  bool shouldExist, bool warnExist)
{
  setTitle (titleString);
  fileFilter = "*";
  filterTextEdit->setText (fileFilter);
  rereadDir ();
  fileListBox->selectText (fileTextEdit->getText());
  center();
  isCancel = true;
  isFocused = false;
  show();
  wait();
  textDialog->hide();
  hide();
  return !isCancel;
}

/**
 * buttons call this
 */
void
SFileDialog::buttonPressedAccel (void* source, const SAccelerator* acc)
{
  if (source == createButton)
  {
    SString before = currentDirectory.getName();
    SDir d(before);
    SString s(fileTextEdit->getText());
    unsigned int ck = 0;
    bool hasSlash = false;
    for (ck=0; ck<s.size(); ck++)
    {
       if ((int) (unsigned char) s[ck] <= ' ') break;
       if (s[ck] == '/') hasSlash = true;
    }
    if (ck==0 || ck+1 == s.size())
    {
      textDialog->getInput ("Error", 
           translate ("Will not create\nspecified folder\n"),
      STextDialog::SS_ERR);
      return;
    }
    if (hasSlash)
    {
      textDialog->getInput ("Error", 
           translate ("Folders can be created\nin current directory only."),
      STextDialog::SS_ERR);
      return;
    }
    if (d.cd (s))
    {
      textDialog->getInput ("Warning", 
           translate ("Specified folder\nalready exists.\n"),
      STextDialog::SS_WARN);
      return;
    }
    SString cdr = currentDirectory.getName();
    if (cdr != "/") cdr.append ("/");
    cdr.append (fileTextEdit->getText());
    d = SDir (cdr);
    if (!d.create())
    {
      textDialog->getInput ("Error", 
           translate ("Can not create\nspecified folder\n"),
      STextDialog::SS_ERR);
      return;
    }
    currentDirectory = d;
    rereadDir ();
    fileTextEdit->setText("");
    return;
  }
  if (source == cdButton)
  {
    SString before = currentDirectory.getName();
    SDir d(before);
    if (!d.cd (fileTextEdit->getText()))
    {
      textDialog->getInput ("Error", 
           translate ("Can not go to\nspecified folder.\n"),
       STextDialog::SS_ERR);
      return;
    }
    currentDirectory = d;
    rereadDir ();
    fileTextEdit->setText("");
    return;
  }
  if (source == cancelButton)
  {
    isCancel = true;
    hide();
    return;
  }
  if (source == yesButton)
  {
    isCancel = false;
    hide();
    return;
  }
  if (source == fileTypeButton)
  {
    setFileType("utf-8");
    return;
  }
  if (source == hiddenButton)
  {
    showHidden = !showHidden;
    setHidden();
    rereadDir();
    return;
  }
  if (source == homeButton)
  {
    currentDirectory.cd (getHome());
    rereadDir();
    return;
  }
}

/**
 * STextListListener
 */
void
SFileDialog::itemSelected (void* source, const SAccelerator* acc)
{
  if (source == folderListBox->textList)
  {
     SString s = folderListBox->textList->getLastSelectedText();
     SString before = currentDirectory.getName();
//fprintf (stderr, "before %*.*s [%*.*s]\n", SSARGS (s), SSARGS(before));
     currentDirectory.cd (s);
     SString after  = currentDirectory.getName();
//fprintf (stderr, "after %*.*s [%*.*s]\n", SSARGS (s), SSARGS(after));
     if (after != before) rereadDir ();
     return;
  }
  if (source == fileListBox->textList)
  {
     SString s = fileListBox->textList->getLastSelectedText();
     if (s != fileTextEdit->getText()) {
         fileTextEdit->setText (s);
     }
     return;
  }
  if (source == fileTypeListBox->textList)
  {
    SString s = fileTypeListBox->textList->getLastSelectedText();
//fprintf (stderr, "typeselected %*.*s\n", SSARGS(s)); 
    fileTypeChanged (s);
  }
}

/**
 * STextEditLS
 */
void
SFileDialog::textEntered (void *source)
{
  if (source == filterTextEdit)
  {
    rereadDir ();
  }
  /* ok */
  if (source == fileTextEdit)
  {
    isCancel = false;
    hide();
    return;
  }
}

void
SFileDialog::textChanged (void *source)
{
  if (source == fileTextEdit)
  {
     fileListBox->selectText (fileTextEdit->getText());
  }
}

void
SFileDialog::filterChanged ()
{
  fileFilter = filterTextEdit->getText();
  /* trim what is not there */
  while (fileFilter.size()>0 && 
     (fileFilter[0]==' '||fileFilter[0]=='\t'))
  {
    fileFilter.remove (0);
  }
  while (fileFilter.size()>0 && 
      (fileFilter[fileFilter.size()-1] == ' ' 
           || fileFilter[fileFilter.size()-1] == '\t'))
  {
    fileFilter.truncate (fileFilter.size()-1);
  }
  //fprintf (stderr,"filter changed to '%*.*s'\n");
}

/**
 * set icon according to showHidden
 */
void
SFileDialog::setHidden()
{
  /* create icons here */
  const char * str = showHidden?"CircleOn" : "CircleOff";
  hiddenButton->setIcon (SIconFactory::getIcon(str));
}
bool
SFileDialog::close (SPanel* comp)
{
  isCancel =true;
  hide();
  textDialog->hide();
  return false;
}

void
SFileDialog::focusChanged (void *source, bool in)
{
  if (!in && source == filterTextEdit)
  {
    rereadDir ();
  }
}

/**
 * set the filename - cut off the dirpart
 */
void
SFileDialog::setFileName (const SString& _filename)
{
  SDir d;
  /* this will chop off filename */
  d.cd (_filename);
  SStringVector v(_filename, "/");
  SString fn;
  if (v.size())
  {
    fn.append (v[v.size()-1]);
  }
  currentDirectory = d;
  rereadDir ();
  fileTextEdit->setText (fn);
  fileListBox->selectText (fn);
}

void
SFileDialog::setTitleForeground (const SColor& fg)
{
  textDialog->setTitleForeground (fg);
  fileTypeButton->setForeground (fg);
  hiddenButton->setForeground (fg);
  homeButton->setForeground (fg);
  cdButton->setForeground (fg);
  yesButton->setForeground (fg);
  cancelButton->setForeground (fg);
  createButton->setForeground (fg);
  folderTitleLabel->setForeground (fg);
  fileTitleLabel->setForeground (fg);
  filterTitleLabel->setForeground (fg);

  folderListBox->setLabelForeground (fg);
  fileListBox->setLabelForeground (fg);
  fileTypeListBox->setLabelForeground (fg);
}

void 
SFileDialog::setLabelForeground (const SColor& fg)
{
  textDialog->setLabelForeground (fg);
  folderNameLabel->setForeground (fg);
  fileTypeLabel->setForeground (fg);
}

void
SFileDialog::setBackground (const SColor& bg)
{
  SFrame::setBackground (bg);
  textDialog->setBackground (bg);
  fileTextEdit->setTextBackground (SColor("white"));
  filterTextEdit->setTextBackground (SColor("white"));
}

void
SFileDialog::setForeground (const SColor& lrfg, const SColor& rlfg)
{
  SFrame::setForeground (lrfg, rlfg);
  setLabelForeground (lrfg);
  setTitleForeground (lrfg);
}

void
SFileDialog::setSliderBackground (const SColor& bg)
{
  folderListBox->setSliderBackground (bg);
  fileListBox->setSliderBackground (bg);
  fileTypeListBox->setSliderBackground (bg);
}

void
SFileDialog::setFont (const SString& font, double fontSize)
{
  textDialog->setFont (font, fontSize);
  folderNameLabel->setFont (font, fontSize);
  fileTypeLabel->setFont (font, fontSize);
  textDialog->setFont (font, fontSize);
  fileTypeButton->setFont (font, fontSize);
  hiddenButton->setFont (font, fontSize);
  homeButton->setFont (font, fontSize);
  cdButton->setFont (font, fontSize);
  yesButton->setFont (font, fontSize);
  cancelButton->setFont (font, fontSize);
  createButton->setFont (font, fontSize);
  folderTitleLabel->setFont (font, fontSize);
  fileTitleLabel->setFont (font, fontSize);
  filterTitleLabel->setFont (font, fontSize);
  folderListBox->setFont (font, fontSize);
  fileListBox->setFont (font, fontSize);
  fileTypeListBox->setFont (font, fontSize);

  fileTextEdit->setFont (font, fontSize);
  filterTextEdit->setFont (font, fontSize);
  recalc();
}

void
SFileDialog::setFontSize (double fontSize)
{
  textDialog->setFontSize (fontSize);
  folderNameLabel->setFontSize (fontSize);
  fileTypeLabel->setFontSize (fontSize);
  textDialog->setFontSize (fontSize);
  fileTypeButton->setFontSize (fontSize);
  hiddenButton->setFontSize (fontSize);
  cdButton->setFontSize (fontSize);
  yesButton->setFontSize (fontSize);
  cancelButton->setFontSize (fontSize);
  createButton->setFontSize (fontSize);
  folderTitleLabel->setFontSize (fontSize);
  fileTitleLabel->setFontSize (fontSize);
  filterTitleLabel->setFontSize (fontSize);
  folderListBox->setFontSize (fontSize);
  fileListBox->setFontSize (fontSize);
  fileTypeListBox->setFontSize (fontSize);

  fileTextEdit->setFontSize (fontSize);
  filterTextEdit->setFontSize (fontSize);
  recalc();
}
/**
 * keyboard focus gained.
 */
bool 
SFileDialog::gainedKeyboardFocus (SWindow* w)
{
  if (isFocused) return true;
  isFocused = true;
  fileTextEdit->setFocus();
  return true;
}