summaryrefslogtreecommitdiffstats
path: root/ksirc/KSTicker/kspainter.cpp
blob: 90ffac0d8dea6a50cb06c117813c0fd5ffc68669 (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
#include "kspainter.h"
#include "../ksopts.h"
#include <stdlib.h>   

const int KSPainter::optcolours = 8;
const int KSPainter::startspecial = 16;
const int KSPainter::maxcolour = 16+optcolours;

const TQColor KSPainter::brown    ( 165,  42,  42 );
const TQColor KSPainter::orange	 ( 255, 165,   0 );
const TQColor KSPainter::lightCyan( 224, 255, 255 );
const TQColor KSPainter::lightBlue( 173, 216, 230 );
const TQColor KSPainter::pink	 ( 255, 192, 203 );

TQColor KSPainter::num2colour[KSPainter::maxcolour] = { Qt::white,
							     Qt::black,
							     Qt::darkBlue,
							     Qt::darkGreen,
							     Qt::red,
							     brown,
							     Qt::darkMagenta,
							     orange,
							     Qt::yellow,
							     Qt::green,
							     Qt::darkCyan,
							     Qt::cyan,
							     Qt::blue,
							     pink,
							     Qt::gray,
							     Qt::lightGray };

void KSPainter::initOptColours()
{
    num2colour[startspecial+0] = ksopts->textColor;
    num2colour[startspecial+1] = ksopts->infoColor;
    num2colour[startspecial+2] = ksopts->channelColor;
    num2colour[startspecial+3] = ksopts->errorColor;
    num2colour[startspecial+4] = ksopts->ownNickColor;
    num2colour[startspecial+5] = ksopts->nickForeground;
    num2colour[startspecial+6] = ksopts->nickBackground;
    num2colour[startspecial+7] = ksopts->backgroundColor;
}

int KSPainter::colour2num(const TQColor &colour)
{
  for(int i = 0; i < maxcolour; i++)
    if(num2colour[i] == colour)
      return i;

  return -1;
}

void KSPainter::colourDrawText(TQPainter *p, int startx, int starty,
				 char *str, int length)
{
  int offset = 0;
  int pcolour;
  char buf[3];
  int loc = 0, i;
  buf[2] = 0;
  bool ReverseText = FALSE;

  // Default pen (colour)

  const TQPen qpDefPen = p->pen();

  for(loc = 0; (str[loc] != 0x00) && (loc != length) ; loc++){
    if(str[loc] == 0x03 || str[loc] == '~'){
      i = loc;
      p->drawText(startx, starty, str + offset, i-offset);
      startx += p->fontMetrics().width(str + offset, i-offset);
      offset = i;
      //      lastp = i;
      if((str[i+1] >= 0x30) && (str[i+1] <= 0x39)){
	i++;
	buf[0] = str[i];
	i++;
	if((str[i] >= 0x30) && (str[i] <= 0x39)){
	  buf[1] = str[i];
	  i++;
	}
	else{
	  buf[1] = 0;
	}
	
	pcolour = atoi(buf);
	if(pcolour < maxcolour)
	  p->setPen(num2colour[pcolour]);
	else
	  i = loc;
	if(str[i] == ','){
	  i++;
	  if((str[i] >= 0x30) && (str[i] <= 0x39)){
	    buf[0] = str[i];
	    i++;
	    if((str[i] >= 0x30) && (str[i] <= 0x39)){
	      buf[1] = str[i];
	      i++;
	    }
	    else{
	      buf[1] = 0;
	    }
	    int bcolour = atoi(buf);
	    if(pcolour == bcolour){
	      if(bcolour + 1 < maxcolour)
		bcolour += 1;
	      else
		bcolour -= 1;
	    }
	    if(bcolour < maxcolour ){
	      p->setBackgroundColor(num2colour[bcolour]);
	      p->setBackgroundMode(Qt::OpaqueMode);
	    }

	  }
	}
      }
      else if(str[i] == 0x03){
	i++;
	p->setPen(qpDefPen);
	p->setBackgroundMode(Qt::TransparentMode);
      }
      else if((str[i] == '~') && ((str[i+1] >= 0x61) || (str[i+1] <= 0x7a))){
	TQFont fnt = p->font();
        TQColor temppen;
	switch(str[i+1]){
	case 'c':
	  p->setPen(qpDefPen);
	  p->setBackgroundMode(Qt::TransparentMode);
	  break;
	case 'C':
	  p->setPen(qpDefPen);
	  p->setBackgroundMode(Qt::TransparentMode);
	  fnt.setBold(FALSE);
	  fnt.setItalic(FALSE);
	  fnt.setUnderline(FALSE);
	  ReverseText = TRUE; // Force reverse removed, all fall through.
	  // FALL THROUGH.
        case 'r':
          if(ReverseText == TRUE) {
            ReverseText = FALSE;
            p->setBackgroundMode(Qt::TransparentMode);
          }
          else {
            ReverseText = TRUE;
            p->setBackgroundMode(Qt::OpaqueMode);
          }
          temppen = p->pen().color();
          p->setPen( p->backgroundColor() );
          p->setBackgroundColor( temppen );
          break;
	case 'b':
	  if(fnt.bold() == TRUE)
	    fnt.setBold(FALSE);
	  else
	    fnt.setBold(TRUE);
	  break;
	case 'i':
	  if(fnt.italic() == TRUE)
	    fnt.setItalic(FALSE);
	  else
	    fnt.setItalic(TRUE);
	  break;
	case 'u':
	  if(fnt.underline() == TRUE)
	    fnt.setUnderline(FALSE);
	  else
	    fnt.setUnderline(TRUE);
	  break;
	case '~':
	  loc++; // Skip ahead 2 characters
	  break;
	default:
	  i-=1;
	  offset -= 1;
	}
	p->setFont(fnt);
	i += 2;
      }
      offset += i - loc;
    }
  }
  p->drawText(startx, starty, str + offset, loc-offset);

}