File: xEdit.cpp

package info (click to toggle)
xirc 2.0-3
  • links: PTS
  • area: contrib
  • in suites: hamm, slink
  • size: 2,624 kB
  • ctags: 2,093
  • sloc: cpp: 21,094; makefile: 1,033
file content (414 lines) | stat: -rw-r--r-- 13,824 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
/***************************************************************************
**    xEdit.cpp  $Revision: 1.17 $ - $Name: V2-18 $ 
**    QEdit w/ Macro Replacement
**
**    Copyright (C) 1996 Joseph Croft <jcroft@unicomp.net>
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Library General Public
**    License as published by the Free Software Foundation; either
**    version 2 of the License, or (at your option) any later version.
**
**    This library 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
**    Library General Public License for more details.
**
**    You should have received a copy of the GNU Library General Public
**    License along with this library; if not, write to the Free
**    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
 ***************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include "xDefaults.h"
#include "xEdit.h"
#include <qlined.h>

extern xDefaults Defaults;

static int dbg = 0;

static const char *pInitialResources[] =
{
   "*BaseColor: light yellow",
   "*BaseHighlight: dark blue",
   "*TextColor: black",
   "*TextHighlight: White",
   NULL
};

xEdit::xEdit(xWidgetResInfo *pPRes, QWidget *pParent, const char *pName) :
        QLineEdit(pParent, pName)
{
   QPalette p;
   QColor baseColor;
   QColor baseHighlight;
   QColor textColor;
   QColor textHighlight;
   QColor textEnabled;
   QColorGroup n, a, d;
   const char *cp, *cp1;

   if (dbg) fprintf(stdout, "xEdit::xEdit():Enter\n");
   wdtRes = new xWidgetResInfo(pPRes, QString("input"), QString("Input"));
   Resources->setWidgetInit(pInitialResources);
   if (dbg) fprintf(stdout, "xEdit::xEdit():Width = %d, height = %d\n",
                            width(), height());
   if (dbg) fflush(stdout);
   setDefPallet(this, wdtRes);
   setDefFont(this, wdtRes);
   if (dbg) fprintf(stdout, "xEdit::xEdit():Defaults Set\n");
   if (dbg) fflush(stdout);
   
   highlight = FALSE;

   if (dbg) fprintf(stdout, "xEdit::xEdit():Setting Colors and such\n");
   if (dbg) fflush(stdout);
   p = palette();
   cp = Resources->get(wdtRes, "basecolor", "BaseColor");
   setStrColor(baseColor, cp);

   cp = Resources->get(wdtRes, "basehighlight", "BaseHighlight");
   setStrColor(baseHighlight, cp);

   cp = Resources->get(wdtRes, "texthighlight", "TextHighlight");
   setStrColor(textHighlight, cp);

   cp = Resources->get(wdtRes, "textcolor", "TextColor");
   setStrColor(textColor, cp);

   cp1 = Resources->get(wdtRes, "textenabled", "TextEnabled");
   if (cp1 != NULL)
      setStrColor(textEnabled, cp1);
   else
      setStrColor(textEnabled, cp);

   n = p.normal();
   a = p.active();
   d = p.disabled();
   QColorGroup n1(n.foreground(), n.background(),  n.light(), 
                  n.mid(), n.dark(), textEnabled, baseColor);
   QColorGroup a1(a.foreground(), a.background(),  a.light(), 
                  a.mid(), a.dark(), textEnabled, baseColor);
   QColorGroup d1(d.foreground(), d.background(),  d.light(), 
                  d.mid(), d.dark(), textColor, n.background());
   paletteEnabled.setNormal(n1);
   paletteEnabled.setActive(a1);
   paletteEnabled.setDisabled(d1);

   QColorGroup n2(n.foreground(), n.background(),  n.light(), 
                  n.mid(), n.dark(), textColor, n.background());
   QColorGroup a2(a.foreground(), a.background(),  a.light(), 
                  a.mid(), a.dark(), textColor, n.background());
   QColorGroup d2(d.foreground(), d.background(),  d.light(), 
                  d.mid(), d.dark(), textColor, n.background());
   paletteDisabled.setNormal(n2);
   paletteDisabled.setActive(a2);
   paletteDisabled.setDisabled(d2);

   QColorGroup n3(n.foreground(), n.background(),  n.light(), 
                  n.mid(), n.dark(), textHighlight, baseHighlight);
   QColorGroup a3(a.foreground(), a.background(),  a.light(), 
                  a.mid(), a.dark(), textHighlight, baseHighlight);
   QColorGroup d3(d.foreground(), d.background(),  d.light(), 
                  d.mid(), d.dark(), textHighlight, baseHighlight);
   paletteHighlight.setNormal(n3);
   paletteHighlight.setActive(a3);
   paletteHighlight.setDisabled(d3);

   if (dbg) fprintf(stdout, "xEdit::xEdit():Getting Font Metrics\n");
   if (dbg) fflush(stdout);
   QFontMetrics fm = fontMetrics();
   if (dbg) fprintf(stdout, "xEdit::xEdit():Setting to Width = %d, height = %d\n",
                            width(), fm.lineSpacing() + 8);
   resize(width(), fm.lineSpacing() + 8);
   if (dbg) fprintf(stdout, "xEdit::xEdit():New Width = %d, height = %d\n",
                            width(), height());
//   frameStyleEnabled = QFrame::Panel | QFrame::Sunken;
//   frameStyleDisabled = QFrame::Panel | QFrame::Plain;
   setEnabled(TRUE);
   QLineEdit::setEnabled(TRUE);
   setAcceptFocus(TRUE);
   if (dbg) fprintf(stdout, "xEdit::xEdit():Exit\n");
}

void xEdit::setFrameStyles(int styleEnabled, int styleDisabled)
{
   if (styleEnabled != -1)
      frameStyleEnabled = styleEnabled;
   if (styleDisabled != -1)
      frameStyleDisabled = styleDisabled;
//   setFrameStyle((enabled == TRUE) ?
//                 frameStyleEnabled : frameStyleDisabled);
}

void xEdit::setWidth(int width)
{
   QFontMetrics fm = fontMetrics();
   if (dbg) fprintf(stdout, "xEdit::setWidth(%d):Enter\n", width);
   if (dbg) fflush(stdout);
   if (dbg) fprintf(stdout, "xEdit::setWidth():maxWidth = %d, width = %d\n",
                            fm.maxWidth(), fm.maxWidth() * width);
   if (dbg) fflush(stdout);
   resize(fm.maxWidth() * width + 8,  fm.lineSpacing() + 8);
   if (dbg) fprintf(stdout, "xEdit::setWidth():Exit\n");
   if (dbg) fflush(stdout);
}

void xEdit::setEnabled(bool f)
{
   if (dbg) fprintf(stdout, "xEdit::setEnabled():Enter\n");
   if (dbg) fflush(stdout);
   enabled = f;
   if (f == FALSE && highlight == FALSE)
   {
      if (dbg) fprintf(stdout, "xEdit::setEnabled():palette disabled, framestyle disabled\n");
      if (dbg) fflush(stdout);
      setPalette(paletteDisabled);
//      setFrameStyle(frameStyleDisabled);
   }
   else if (f == FALSE && highlight == TRUE) 
   {
      if (dbg) fprintf(stdout, "xEdit::setEnabled():palette highlight, framestyle disabled\n");
      if (dbg) fflush(stdout);
      setPalette(paletteHighlight);
//      setFrameStyle(frameStyleDisabled);
   }
   else
   {
      if (dbg) fprintf(stdout, "xEdit::setEnabled():palette enabled, framestyle enabled\n");
      if (dbg) fflush(stdout);
      setPalette(paletteEnabled);
//      setFrameStyle(frameStyleEnabled);
   }
   if (f == FALSE && hasFocus() == TRUE)
      clearFocus();
   setAcceptFocus(f);
   if (dbg) fprintf(stdout, "xEdit::setEnabled():Exit\n");
   if (dbg) fflush(stdout);
//   QLineEdit::setEnabled(f);
}

void xEdit::setHighlight(bool f)
{
   if (dbg) fprintf(stdout, "xEdit::setHighlight():Enter\n");
   if (dbg) fflush(stdout);
   highlight = f;
   if (f == FALSE && enabled == FALSE)
   {
      if (dbg) fprintf(stdout, "xEdit::setHighlight():palette disabled, framestyle disabled\n");
      if (dbg) fflush(stdout);
      setPalette(paletteDisabled);
//      setFrameStyle(frameStyleDisabled);
   }
   else if (enabled == TRUE)
   {
      if (dbg) fprintf(stdout, "xEdit::setHighlight():palette enabled, framestyle enabled\n");
      if (dbg) fflush(stdout);
      setPalette(paletteEnabled);
//      setFrameStyle(frameStyleEnabled);
   }
   else
   {
      if (dbg) fprintf(stdout, "xEdit::setHighlight():palette highlight, framestyle enabled\n");
      if (dbg) fflush(stdout);
      setPalette(paletteHighlight);
//      setFrameStyle(frameStyleEnabled);
   }
   if (dbg) fprintf(stdout, "xEdit::setHighlight():Exit\n");
   if (dbg) fflush(stdout);
}

QString xEdit::macroReplace(const char *cp)
{
   QString rv, strTmp1, strTmp2;
   char ch, ch1, ch2;
   int base;

   if (dbg) fprintf(stdout, "xEdit::macroReplace():Enter\n");
   if (cp == NULL)
   {
      rv = "";
      return(rv);
   }
   for (ch = ' ', rv = ""; *cp;)
   {
      if (ch != '\\' && ch != '$' && *cp == '$')
      {
         if (*(cp + 1) != '$')
         {
            if (dbg) fprintf(stdout, "xEdit::macroReplace():Test for Macro in: |%s|\n", cp);
            for (strTmp1 = "", cp++; *cp && isalnum(*cp) || *cp == '.' || *cp == '_'; cp++)
               strTmp1 += *cp;
            if (dbg) fprintf(stdout, "xEdit::macroReplace():Tag =  |%s|\n", (const char *)strTmp1);
            if ((strTmp2 = macroReplace(Defaults.get(strTmp1))) != NULL)
            {
               if (dbg) fprintf(stdout, "xEdit::macroReplace():Found Entry!! = |%s|\n", (const char *)strTmp1);
               rv += strTmp2;
            }
            if (dbg) fprintf(stdout, "xEdit::macroReplace():Remaining string to test = |%s|\n", cp);
         }
         else
            ch = *(cp++);
      }
      else if (ch != '\\' && *cp == '\\')
      {
         if (dbg) fprintf(stdout, "xEdit::macroReplace():Found escape char!\n");
         ch = *(cp++);
      }
      else if (ch == '\\')
      {
         if (dbg) fprintf(stdout, "xEdit::macroReplace():Interpreting escape char!\n");
         switch (toupper(*cp))
         {
            case 'B':
               if (dbg) fprintf(stdout, "xEdit::macroReplace():Found bold char!\n");
               ch = '\x02';
               rv += ch;
               cp++;
               break;

            case 'U':
               if (dbg) fprintf(stdout, "xEdit::macroReplace():Found underline char!\n");
               ch = '_' - 0x40;
               rv += ch;
               cp++;
               break;

            case 'X':
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
               if (dbg) fprintf(stdout, "xEdit::macroReplace():Found Number!!!!!\n");
               if (toupper(*cp) == 'X')
               {
                  base = 16;
                  cp++;
               }
               else if (*cp == '0')
                  base = 8;
               else
                  base = 10;
               for (ch1 = 0; base != 0;)
               {
                  if (base == 16)
                  {
                     if (isxdigit(*cp))
                     {
                        ch1 *= 16;
                        if ((ch2 = toupper(*cp) - '0') > 9)
                           ch1 += ch2 - 7;
                        else
                           ch1 += ch2;
                        cp++;
                     }
                     else
                        base = 0;
                  }
                  else if (base == 8)
                  {
                     if (dbg) printf("ctoi():base 8, ch = 0x%x, %c\n\r", ch, ch);
                     if (*cp >= '0' && *cp <= '7')
                     {
                        ch1 *= 8;
                        ch1 += *cp - '0';
                        cp++;
                        if (dbg) printf("ctoi():base 8, rslt = 0%x\n\r", ch1);
                     }
                     else
                        base = 0;
                  }
                  else
                  {
                     if (dbg) printf("ctoi():base 10, *cp = 0x%x, %c\n\r", *cp, *cp);
                     if (isdigit(*cp))
                     {
                        ch1 *= 10;
                        ch1 += *cp - '0';
                        cp++;
                        if (dbg) printf("ctoi():base 10, rslt = 0x%x\n\r", ch1);
                     }
                     else
                        base = 0;
                  }
               }
               ch = ch1;
               rv += ch1;
               break;

            default:
               if (dbg) fprintf(stdout, "xEdit::macroReplace():Found defaults!!!!!!!!\n");
               ch = ' ';
               rv += *(cp++);
               break;
         }
      }
      else
      {
         ch = *cp;
         rv += *(cp++);
      }
   }
   if (dbg) fprintf(stdout, "xEdit::macroReplace():Exit |%s|\n", (const char *)rv);
   return(rv);
}

const char *xEdit::text()
{
   const char *cp;
   QString strTmp;

   if (dbg) fprintf(stdout, "xEdit::text():Enter\n");
   cp = QLineEdit::text();
   if (dbg) fprintf(stdout, "xEdit::text():Intermediate cp = |%s|\n", cp);
   strTmp = cp;
   txtStr = Defaults.expandEntry(strTmp);
   if (dbg) fprintf(stdout, "xEdit::text():Exit |%s|\n", (const char *)txtStr);
   return((const char *)txtStr);
}

void xEdit::mousePressEvent(QMouseEvent *pEvt)
{
   xMouseEvent evt(this, pEvt);
   if (dbg) fprintf(stdout, "xEdit::mousePressEvent():Enter\n");
   QLineEdit::mousePressEvent(pEvt);
   emit mousePressed(&evt);
   if (dbg) fprintf(stdout, "xEdit::mousePressEvent():Exit\n");
}

void xEdit::mouseReleaseEvent(QMouseEvent *pEvt)
{
   xMouseEvent evt(this, pEvt);
   if (dbg) fprintf(stdout, "xEdit::mouseReleaseEvent():Enter\n");
   QLineEdit::mouseReleaseEvent(pEvt);
   emit mouseReleased(&evt);
   if (dbg) fprintf(stdout, "xEdit::mouseReleaseEvent():Exit\n");
}

void xEdit::mouseDoubleClickEvent(QMouseEvent *pEvt)
{
   xMouseEvent evt(this, pEvt);
   if (dbg) fprintf(stdout, "xEdit::mouseDoubleClickEvent():Enter\n");
   QLineEdit::mouseDoubleClickEvent(pEvt);
   emit mouseDoubleClicked(&evt);
   if (dbg) fprintf(stdout, "xEdit::mouseDoubleClickEvent():Exit\n");
}

void xEdit::mouseMoveEvent(QMouseEvent *pEvt)
{
   xMouseEvent evt(this, pEvt);
   if (dbg) fprintf(stdout, "xEdit::mouseEvent():Enter\n");
   QLineEdit::mouseMoveEvent(pEvt);
   emit mouseMoved(&evt);
   if (dbg) fprintf(stdout, "xEdit::mouseEvent():Exit\n");
}

#include "xEdit.moc"