-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBmpCheckBox.cpp
304 lines (234 loc) · 6.37 KB
/
BmpCheckBox.cpp
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
/////////////////////////////////////////////////////////////////////////////
// Name: BmpCheckBox.cpp
// Purpose: wxIndustrialControls Library
// Author: Marco Cavallini <m.cavallini AT koansoftware.com>
// Modified by:
// Copyright: (C)2004-2006 Copyright by Koan s.a.s. - www.koansoftware.com
// Licence: KWIC License http://www.koansoftware.com/kwic/kwic-license.htm
/////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
//#include "kprec.h" //#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include <wx/image.h>
#include <wx/log.h>
#include "BmpCheckBox.h"
#include <wx/event.h>
IMPLEMENT_DYNAMIC_CLASS(kwxBmpCheckBox, wxControl)
BEGIN_EVENT_TABLE(kwxBmpCheckBox,wxControl)
EVT_PAINT(kwxBmpCheckBox::OnPaint)
EVT_MOUSE_EVENTS(kwxBmpCheckBox::OnMouse)
END_EVENT_TABLE()
kwxBmpCheckBox::~kwxBmpCheckBox()
{
if (membitmap)
delete membitmap ;
}
kwxBmpCheckBox::kwxBmpCheckBox(wxWindow* parent,
const wxWindowID id,
wxBitmap& OnBitmap,
wxBitmap& OffBitmap,
wxBitmap& OnSelBitmap,
wxBitmap& OffSelBitmap,
const wxPoint& pos,
const wxSize& size,
const long int style)
: wxControl(parent, id, pos, size, style)
{
if (parent)
SetBackgroundColour(parent->GetBackgroundColour());
else
SetBackgroundColour(*wxLIGHT_GREY);
mOnBitmap = &OnBitmap;
mOffBitmap = &OffBitmap;
mOnSelBitmap = &OnSelBitmap;
mOffSelBitmap = &OffSelBitmap;
m_id = id;
int total_width = 0, total_height = 0 ;
/*
// se c'e' un bitmap ne uso le dimensioni
if (mOffBitmap)
{
total_width = mOffBitmap->GetWidth(); // NB! Should use largest bm
total_height = mOffBitmap->GetHeight(); // NB! Should use largest bm
}
*/
total_width = size.GetWidth() ;
total_height = size.GetHeight();
SetSize(total_width, total_height);
//SetSize(size);
SetAutoLayout(TRUE);
Refresh();
m_stato = 0;
m_oldstato = 0;
m_bPress = false ;
m_bBord = true ;
m_nStyle = wxDOT;
membitmap = new wxBitmap(total_width, total_height) ;
}
void kwxBmpCheckBox::SetLabel(wxString label)
{
mLabelStr = label;
}
void kwxBmpCheckBox::OnPaint(wxPaintEvent& WXUNUSED(event))
{
wxPaintDC old_dc(this);
int w,h;
int bdraw = 0 ;
GetClientSize(&w,&h);
/////////////////
// Create a memory DC
wxMemoryDC dc;
dc.SelectObject(*membitmap);
dc.SetBackground(*wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(),wxSOLID));
dc.Clear();
///////////////////
// se impostato n bitmap lo disegno
//if (mOffBitmap)
if(m_stato == 0)
{
if(m_bPress)
dc.DrawBitmap(*mOnBitmap, 0, 0, TRUE);
else
dc.DrawBitmap(*mOffBitmap, 0, 0, TRUE);
bdraw = 0 ;
}
else if(m_stato == 1)
{
if(m_bPress)
dc.DrawBitmap(*mOnSelBitmap, 0, 0, TRUE);
else
dc.DrawBitmap(*mOffSelBitmap, 0, 0, TRUE);
bdraw = 1 ;
}
else if(m_stato == 2)
{
if(m_bPress)
dc.DrawBitmap(*mOffSelBitmap, 0, 0, TRUE);
else
dc.DrawBitmap(*mOnSelBitmap, 0, 0, TRUE);
bdraw = 1 ;
}
if(m_bBord)
{
if(bdraw)
{
// Cornice intorno
dc.SetPen(*wxThePenList->FindOrCreatePen(*wxRED, 1, m_nStyle ));
dc.DrawLine(0, 0, 0, h - 1);
dc.DrawLine(0, 0, w, 0);
dc.DrawLine(0, h - 1, w, h - 1);
dc.DrawLine(w - 1, 0, w - 1, h - 1);
}
}
// We can now draw into the memory DC...
// Copy from this DC to another DC.
old_dc.Blit(0, 0, w, h, &dc, 0, 0);
}
void kwxBmpCheckBox::OnMouse(wxMouseEvent& event)
{
if (m_stato == 0 && event.Entering())
{
m_stato = 1; // mouse sul bottone
wxCommandEvent ev(event.GetEventType(),GetId());
ev.SetEventType(wxEVT_ENTER_WINDOW);
event.SetEventObject(this);
GetEventHandler()->ProcessEvent(ev);
}
else if (m_stato == 1 && event.LeftDown())
m_stato = 2; // uscita click sul bottone
else if (m_stato >= 1 && event.Leaving())
{
m_stato = 0; // uscita mouse dal bottone
wxCommandEvent ev(event.GetEventType(),GetId());
ev.SetEventType(wxEVT_LEAVE_WINDOW);
event.SetEventObject(this);
GetEventHandler()->ProcessEvent(ev);
}
else if (m_stato == 2 && event.LeftUp())
{
m_bPress = !m_bPress ;
Click(); // rilascio sul bottone genera evento
m_stato = 1;
}
if(m_oldstato != m_stato)
Refresh();
m_oldstato=m_stato;
event.Skip();
}
void kwxBmpCheckBox::Click()
{
// wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetID()) ;
// event.SetEventObject(this);
// GetEventHandler()->ProcessEvent(event) ;
// wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetID()) ;
// InitCommandEvent(event);
// Command(event);
wxCommandEvent event(kwxEVT_BITBUTTON_CLICK, GetId());
event.SetEventObject(this);
ProcessCommand(event);
}
void kwxBmpCheckBox::SetState(bool newstate)
{
m_bPress = newstate ;
Refresh();
}
/*
void kwxBmpCheckBox::Focus()
{
// ENTER/LEAVE
wxCommandEvent ev(event.GetEventType(),GetId());
if (mHasFocus)
ev.SetEventType(wxEVT_ENTER_WINDOW);
else
ev.SetEventType(wxEVT_LEAVE_WINDOW);
GetEventHandler()->ProcessEvent(ev); // Neccessary?
}
*/
/*
void kwxBmpCheckBox::OnMouse(wxMouseEvent& event)
{
if(MouseIsOnButton())
{
if (event.LeftIsDown())
{
m_stato=2; //bottone premuto
Click() ;
}
else
{
m_stato=1; //mouse sul bottone
}
}
if(event.Leaving())
{
m_stato=0; //uscita mouse dal bottone
}
if(m_oldstato!=m_stato)
{
Refresh();
}
m_oldstato=m_stato;
event.Skip();
}
bool kwxBmpCheckBox::MouseIsOnButton()
{
int cx=0,cy=0;
int wbit,hbit;
ClientToScreen(&cx,&cy);
int cw=0,ch=0;
this->GetClientSize(&cw,&ch);
//wxLogTrace("ClientSize cw=%d ch=%d", cw, ch) ;
GetSize(&cw,&ch);
//wxLogTrace("Size cw=%d ch=%d", cw, ch) ;
int mpx,mpy;
::wxGetMousePosition(&mpx,&mpy);
wbit = mOffBitmap->GetWidth(); // NB! Should use largest bm
hbit = mOffBitmap->GetHeight(); // NB! Should use largest bm
return(mpx >= cx && mpx <= cx + wbit &&
mpy >= cy && mpy <= cy + hbit);
}
*/