aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/api/wnd/wndclass/labelwnd.cpp
blob: d9aedd0f111a68bd082b350870a640c0d3b67580 (plain) (blame)
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
#include <precomp.h>

#include "labelwnd.h"

#include <api/locales/xlatstr.h>
#include <api/wnd/paintsets.h>

#include <tataki/canvas/bltcanvas.h>
#include <tataki/color/skinclr.h>
#include <api/wnd/notifmsg.h>
#include <tataki/region/region.h>
#include <api/wnd/PaintCanvas.h>

static SkinColor labelfg(L"wasabi.labelwnd.foreground");
static SkinColor labelbg(L"wasabi.labelwnd.background", L"Text backgrounds");

#define DEF_LABEL_HEIGHT 0
#define DEF_LABEL_FONTSIZE 16

LabelWnd::LabelWnd() {
  show_label = FALSE;
  labelsize = DEF_LABEL_FONTSIZE;
  labelHeight = 0;
  margin=0;
  setVirtual(0);
}

void LabelWnd::getClientRect(RECT *r) {
  LABELWND_PARENT::getClientRect(r);
  r->top += getLabelHeight();
}

int LabelWnd::onResize() {
  LABELWND_PARENT::onResize();
  invalidateLabel();
/*  if (getLabelHeight() <= 0) return 0;
  RECT r,ir;
  LABELWND_PARENT::getClientRect(&r);
  LABELWND_PARENT::getNonClientRect(&ir);
  ir.bottom = ir.top + getLabelHeight()+margin;
  invalidateRect(&ir);*/
  return 1;
}

int LabelWnd::onPaint(Canvas *canvas) {
  if (getLabelHeight() <= 0) return LABELWND_PARENT::onPaint(canvas);

  PaintBltCanvas paintcanvas;
  if (canvas == NULL) {
    if (!paintcanvas.beginPaintNC(this)) return 0;
    canvas = &paintcanvas;
  }

  RECT r;
  LabelWnd::getNonClientRect(&r);

  if (canvas->isFixedCoords()) { // handle possible double buffer
    // convert to canvas coords
    r.right -= r.left; r.left = 0;
    r.bottom -= r.top; r.top = 0;
  }

  r.bottom = r.top + getLabelHeight();

  if (margin) {
    r.left+=margin;
    r.right-=margin;
    r.bottom+=margin*2;
  }

  LABELWND_PARENT::onPaint(canvas);
  int got_focus = gotFocus() || forceFocus();

  if (wantRenderBaseTexture()) {
    WASABI_API_WND->skin_renderBaseTexture(getBaseTextureWindow(), canvas, r, this);
  }

#ifdef WASABI_COMPILE_PAINTSETS
  WASABI_API_WND->paintset_render(Paintset::LABEL, canvas, &r, got_focus ? 255 : 64);
#endif
	Wasabi::FontInfo fontInfo;
	fontInfo.opaque=false;
  fontInfo.pointSize = getLabelHeight()-1;
  const wchar_t *name = getName();
  if (name == NULL || *name == '\0')
		name = L"Label";
#define LEFTMARGIN 3
	fontInfo.color = labelbg;
	const wchar_t *xname = name;

	switch(wantTranslation())
	{
	case 1:
		xname = _(name);
		break;
	case 2:
		xname = __(name);
		break;
	}

	canvas->textOut(r.left+LEFTMARGIN+1, r.top+1, xname, &fontInfo);
	fontInfo.color = labelfg;
	canvas->textOut(r.left+LEFTMARGIN, r.top, xname, &fontInfo);
	
  return 1;
}

void LabelWnd::onSetName() {
  LABELWND_PARENT::onSetName();
  // make sure label gets repainted
  if (isInited()) {
    RECT r;
    LabelWnd::getNonClientRect(&r);
    r.bottom = r.top + getLabelHeight();
    invalidateRect(&r);
  }
}

//CUTint LabelWnd::childNotify(api_window *child, int msg, intptr_t param1, intptr_t param2) {
//CUT  switch (msg) {
//CUT    case CHILD_WINDOWSHADE_CAPABLE: return show_label;
//CUT    case CHILD_WINDOWSHADE_ENABLE: return TRUE;
//CUT  }
//CUT  return LABELWND_PARENT::childNotify(child, msg, param1, param2);
//CUT}

int LabelWnd::showLabel(int show) {
  show_label = show;
  setFontSize(-1);
  if (isPostOnInit()) {
    onResize();
  }
  return 1;
}

int LabelWnd::getLabelHeight() {
  return show_label ? labelHeight : 0;
}

void LabelWnd::setMargin(int newmargin) {
  margin = newmargin;
  RECT r;
  getNonClientRect(&r);
  r.bottom = getLabelHeight()+margin;
  invalidateRect(&r);
}

int LabelWnd::onGetFocus() {
  LABELWND_PARENT::onGetFocus();
  invalidateLabel();
  return 1;
}

int LabelWnd::onKillFocus() {
  LABELWND_PARENT::onKillFocus();
  invalidateLabel();
  return 1;
}

void LabelWnd::invalidateLabel() {
  if (labelHeight <= 0) return;
  RECT ncr;
  RECT cr;
//  RECT lr;
  LabelWnd::getNonClientRect(&ncr);
  LabelWnd::getClientRect(&cr);
  RegionI nonClientRgn(&ncr);
  RegionI clientRgn(&cr);
  nonClientRgn.subtractRgn(&clientRgn);
  invalidateRgn(&nonClientRgn);
//  SubtractRect(&lr, &ncr, &cr); // PORT ME
//  invalidateRect(&lr);
}

int LabelWnd::wantFocus() {
  return (labelHeight > 0);
}

void LabelWnd::reloadResources() 
{
  LABELWND_PARENT::reloadResources();
  if (isPostOnInit())
    onResize();
  invalidateLabel();
}

int LabelWnd::setFontSize(int size) 
{
  LABELWND_PARENT::setFontSize(size);
  TextInfoCanvas blt(this); 
	Wasabi::FontInfo fontInfo;
#ifndef WASABINOMAINAPI
	fontInfo.pointSize = labelsize+api->metrics_getDelta();
#else
	fontInfo.pointSize = labelsize;
  //MULTIAPI-FIXME: not handling delta
#endif
  labelHeight = blt.getTextHeight(&fontInfo) + 1;
  invalidate();
  if (isPostOnInit()) onResize();
  return 1;
}