File: SPasswordDialog.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 (300 lines) | stat: -rw-r--r-- 6,917 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
/** 
 *  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/SPasswordDialog.h"
#include "swidget/SIcon.h"
#include "swidget/SIconFactory.h"
#include "stoolkit/SUtil.h"
#include "stoolkit/SEncoder.h"


SPasswordDialog::SPasswordDialog (void)
{
  setFrameListener (this);
  isCancel = true;

  yesButton = new SButton (translate ("OK"), SIconFactory::getIcon("Yes"));
  yesButton->setAlignment (SD_Center);
  yesButton->setButtonListener (this);

  cancelButton = new SButton (translate ("Cancel"), SIconFactory::getIcon("Cancel"));
  cancelButton->setAlignment (SD_Center);
  cancelButton->setButtonListener (this);
  add (yesButton);
  add (cancelButton);

  passwordLabel = new SLabel (translate ("Password:"));
  add (passwordLabel);
  fileNameLabel = new SLabel ("", SIconFactory::getIcon("SedyRed")); 
  add (fileNameLabel);

  passwordTextEdit = new STextEdit("");
  passwordTextEdit->setMultiline(false);
  passwordTextEdit->setLineEndMark(false);
  passwordTextEdit->addTextEditLS(this);
  passwordTextEdit->setHideText(true);

  add (passwordTextEdit);

  isFocused = false;
  recalc();
}

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

SPasswordDialog::~SPasswordDialog ()
{
}

void
SPasswordDialog::setFileName (const SString& fileName)
{
    fileNameLabel->setText(fileName);
    recalc();
}
/**
 * recalculate the geometry
 * it is based upon 300x200 layout.
 * preferredSize will be calculated here.
 */
void
SPasswordDialog::recalc ()
{
  unsigned int bw = 1;
  if (yesButton->getPreferredSize().width > bw )
  {
     bw = yesButton->getPreferredSize().width;
  }
  if (cancelButton->getPreferredSize().width > bw )
  {
     bw = cancelButton->getPreferredSize().width;
  }
  unsigned int bh= cancelButton->getPreferredSize().height;

  unsigned int MGN = (unsigned int) (SAwt::getScale() * 5.0);
  unsigned int WD = (unsigned int) (SAwt::getScale() * 300);
  unsigned int HT = (unsigned int) (SAwt::getScale() * 200);

  int prefWidth = MGN + bw + MGN + bw + MGN;
  int prefHeight = MGN + bh + MGN;

  int gap = (WD-((int)bw * 4 + 2*MGN))/3;
  int currentx= MGN;

  yesButton->setLayout (
    SLayout (
      SLocation (currentx, HT-MGN-bh),
      SLocation (currentx+(int)bw, HT-MGN),
      SLocation (0, 100),
      SLocation (0, 100)
    )
  );
  currentx = currentx + (int) bw + gap;
  cancelButton->setLayout (
    SLayout (
      SLocation (WD-MGN-(int)bw, HT-MGN-bh),
      SLocation (WD-MGN, HT-MGN),
      SLocation (100, 100),
      SLocation (100, 100)
    )
  );
  SDimension fnd = fileNameLabel->getPreferredSize();
  int fndY = fnd.height + MGN;
  SDimension pd = passwordLabel->getPreferredSize();
  SDimension pte = passwordTextEdit->getPreferredSize();

  int pheight = pd.height > pte.height ? pd.height : pte.height;
  int pwidth = pd.width * 2;
  fileNameLabel->setLayout (
    SLayout (
      SLocation (MGN, MGN),
      SLocation (WD-MGN, MGN + fnd.height),
      SLocation (0,0),
      SLocation (100,0)
    )
  );

  passwordLabel->setLayout (
    SLayout (
      SLocation (MGN, MGN+fndY),
      SLocation (MGN+pd.width, MGN + pheight + fndY),
      SLocation (0,0),
      SLocation (0, 0)
    )
  );

  passwordTextEdit->setLayout (
    SLayout (
      SLocation (MGN+pd.width+MGN, MGN+fndY),
      SLocation (WD-MGN, MGN + pheight+fndY),
      SLocation (0, 0),
      SLocation (100, 0)
    )
  );
  pwidth = MGN+pwidth+MGN+pwidth+MGN;
  if (pwidth > prefWidth)
  {
    prefWidth = pwidth;
  }
  prefHeight = MGN + pheight + prefHeight + MGN + fndY;

  preferredSize = SDimension (prefWidth,  prefHeight);

  /* This was out layout... */
  forceLayout (SLayout (SDimension (WD,HT)));

  /* But this one is the one that works */
  setLayout (SLayout (preferredSize));

  setMinimumSize (preferredSize);

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

}

SString
SPasswordDialog::getPassword ()
{
  return passwordTextEdit->getText();
    
}

bool
SPasswordDialog::getInput (const SString& titleString) 
{
  setTitle (titleString);
  SString saved = passwordTextEdit->getText();
  isCancel = true;
  isFocused = false;
  passwordTextEdit->clear();
  center();
  show();
  passwordTextEdit->setFocus();
  wait();
  hide();
  if (isCancel) {
     passwordTextEdit->setText(saved);
  }
  return !isCancel;
}

/**
 * buttons call this
 */
void
SPasswordDialog::buttonPressedAccel (void* source, const SAccelerator* acc)
{
  if (source == cancelButton)
  {
    isCancel = true;
    hide();
    return;
  }
  if (source == yesButton)
  {
    isCancel = false;
    hide();
    return;
  }
}


/**
 * STextEditLS
 */
void
SPasswordDialog::textEntered (void *source)
{
  if (source == passwordTextEdit)
  {
    isCancel = false;
    hide();
  }
}

bool
SPasswordDialog::close (SPanel* comp)
{
  isCancel =true;
  hide();
  return false;
}

void
SPasswordDialog::setTitleForeground (const SColor& fg)
{
  yesButton->setForeground (fg);
  cancelButton->setForeground (fg);
}

void 
SPasswordDialog::setLabelForeground (const SColor& fg)
{
  passwordLabel->setForeground (fg);
  fileNameLabel->setForeground (fg);
}

void
SPasswordDialog::setBackground (const SColor& bg)
{
  SFrame::setBackground (bg);
  passwordTextEdit->setTextBackground (SColor("white"));
}

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

void
SPasswordDialog::setFont (const SString& font, double fontSize)
{
  passwordLabel->setFont (font, fontSize);
  fileNameLabel->setFont (font, fontSize);
  passwordTextEdit->setFont (font, fontSize);
  yesButton->setFont (font, fontSize);
  cancelButton->setFont (font, fontSize);
  recalc();
}

void
SPasswordDialog::setFontSize (double fontSize)
{
  passwordLabel->setFontSize (fontSize);
  fileNameLabel->setFontSize (fontSize);
  passwordTextEdit->setFontSize (fontSize);
  yesButton->setFontSize (fontSize);
  cancelButton->setFontSize (fontSize);
  recalc();
}
bool 
SPasswordDialog::gainedKeyboardFocus (SWindow* w)
{
  if (isFocused) return true;
  isFocused = true;
  passwordTextEdit->setFocus();
  return true;
}