-
Notifications
You must be signed in to change notification settings - Fork 0
/
NEWCTL.CPP
303 lines (258 loc) · 10.4 KB
/
NEWCTL.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
/***************************************************************/
/* Filename: newctl.cpp */
/* */
/* Purpose: Main source file for C++ control NewControl. */
/* */
/* Program name: cppctl.exe Title: C++ PM Control Test App */
/* OS/2 Developer Magazine, Issue: Sept. '94, page */
/* Article title: Writing OS/2 PM Controls in C++ */
/* Authors: Eric Snell and Lori Ruffing */
/* */
/* Description: This example shows how to implement an OS/2 PM */
/* control window in C++. */
/* */
/* Program Requirements: This example requires the IBM C Set++ */
/* compiler and libraries. */
/***************************************************************/
extern "C"
{
#define INCL_WINSYS
#define INCL_WINWINDOWMGR
#define INCL_GPICONTROL
#define INCL_GPILOGCOLORTABLE
#define INCL_GPIPRIMITIVES
#include <os2.h>
}
#ifndef _IEXCEPT_
#include <iexcept.hpp>
#endif
#define MIN(a, b) ( ( (a) <= (b) ) ? (a) : (b) )
#define MAX(a, b) ( ( (a) >= (b) ) ? (a) : (b) )
#include "newctl.hpp"
// Class variables and constants
const NewControl::Style NewControl::notifyMB1 = 1;
const NewControl::Style NewControl::notifyMB2 = (1 << 1);
const NewControl::Style NewControl::classDefaultStyle =
IWindow::visible | notifyMB1 | notifyMB2;
NewControl::Style NewControl::currentDefaultStyle =
NewControl::classDefaultStyle;
// Define the NewControl class name for use in constructing IAbstractWindow
const char* NewControl::newControlClass = "NewControlClass";
// Define the the class style to be CS_SIZEREDRAW so that the text in
// the control will be scaled to the window size.
const unsigned long NewControl::newControlClassStyle = CS_SIZEREDRAW;
// Define the text to be displayed in the control.
const char* NewControl::dummyText = "Mouse clicks: ";
// Define the default colors for background, foreground, and hilite.
const IColor NewControl::defclrBackground(IColor::paleGray);
const IColor NewControl::defclrForeground(IColor::blue);
const IColor NewControl::defclrHilite(IColor::red);
//----------------------------------------------------------------------------
// Method: NewControl::establishColor
//
// Description: 'establishColor' sets the the logical color table
// for the hps into RGB mode and sets 'clr' to the
// color value for 'clrArea' or 'defClr' if there
// is an exception.
//----------------------------------------------------------------------------
inline NewControl& NewControl::establishColor(IColor& clr,
NewControl::ColorArea clrArea,
const IColor& defClr)
{
// Need to bracket the call with try/catch since an exception will
// be thrown if someone removed the presparam.
try
{
clr = color(clrArea);
}
catch (IException& exc)
{
// Reassign default to make sure it wasn't hosed up somehow.
clr = defClr;
}
return *this;
}
//----------------------------------------------------------------------------
// Method: NewControl::NewControl
//
// Description: 'NewControl' is the constructor for the NewControl class.
// It takes all the standard window creation parameters
// plus the class name, class style, and extra window words.
//----------------------------------------------------------------------------
NewControl::NewControl(unsigned long id,
IWindow* parent,
IWindow* owner,
const IRectangle& initial,
const Style& style)
: IAbstractWindow(newControlClass,
newControlClassStyle,
0,
id,
parent,
owner,
initial,
style.asUnsignedLong()),
clicks(0),
cx(initial.width()),
cy(initial.height())
{
// Set color presparams to try to avoid exceptions being
// thrown.
setColor(background, defclrBackground);
setColor(foreground, defclrForeground);
setColor(hilite, defclrHilite);
// Attach the mouse click handler because we want to be
// notified of any click.
IMouseClickHandler::handleEventsFor(this);
}
//----------------------------------------------------------------------------
// Method: NewControl::~NewControl
//
// Description: '~NewControl' is the destructor for the NewControl class.
//----------------------------------------------------------------------------
NewControl::~NewControl()
{
}
NewControl::Style NewControl::defaultStyle()
{
return currentDefaultStyle;
}
void NewControl::setDefaultStyle(const Style& style)
{
currentDefaultStyle = style;
}
NewControl& NewControl::setColor(ColorArea area, const IColor& color)
{
IWindow::setColor(area, color);
return *this;
}
IColor NewControl::color(ColorArea area) const
{
return IWindow::color(area);
}
//----------------------------------------------------------------------------
// Method: NewControl::paintWindow
//
// Description: 'paintWindow' is called in response to a paint event, 'evt'.
// It establishes the colors to paint as well as determines
// how large and where to draw the text.
//----------------------------------------------------------------------------
Boolean NewControl::paintWindow(IPaintEvent& evt)
{
IColor clrBackground(defclrBackground);
IColor clrForeground(defclrForeground);
IColor clrHilite(defclrHilite);
IPresSpaceHandle hps = evt.presSpaceHandle();
// Should make a separate method which sets the logical
// color table for the HPS into RGB mode and sets all our
// colors. Also, colors could be cached in private data
// and only updated when presparams change.
establishColor(clrBackground, background, defclrBackground);
establishColor(clrForeground, foreground, defclrForeground);
establishColor(clrHilite, hilite, defclrHilite);
evt.clearBackground(clrBackground);
// Create the text to draw
IString theText(dummyText);
IString theNumber(clicks);
theNumber.rightJustify(4);
IString both(theText + theNumber);
POINTL aptl[TXTBOX_COUNT];
POINTL ptl = {0,0};
// Find where we're going to draw our text and how big it will be.
GpiQueryTextBox(hps,
strlen((char*)both),
(char*)both,
TXTBOX_COUNT,
aptl);
long textWidth = MAX(aptl[TXTBOX_TOPRIGHT].x,
aptl[TXTBOX_BOTTOMRIGHT].x);
long textHeight = MAX(aptl[TXTBOX_TOPLEFT].y,
aptl[TXTBOX_TOPRIGHT].y);
// Center the text before drawing it.
long x = (cx - textWidth) / 2;
long y = (cy - textHeight) / 2;
evt.drawText(theText, IPoint(x, y), clrForeground);
GpiQueryTextBox(hps,
strlen((char*)theText),
(char*)theText,
TXTBOX_COUNT,
aptl);
// Draw the number to the side of the text
x += aptl[TXTBOX_CONCAT].x;
evt.drawText(theNumber, IPoint(x, y), clrHilite);
return true;
}
//----------------------------------------------------------------------------
// Method: NewControl::windowResize
//
// Description: 'windowResize' is called in response to a resize event, 'evt'.
// It stores the current width and height of the control.
//----------------------------------------------------------------------------
Boolean NewControl::windowResize(IResizeEvent& evt)
{
// Keep track of control's width and height.
cx = evt.newSize().width();
cy = evt.newSize().height();
return false;
}
//----------------------------------------------------------------------------
// Method: NewControl::gainingFocus
//
// Description: 'gainingFocus' is called in response to a focus gain event,
// 'evt'. It doesn't do anything, but you can fill in any code
// you feel appropriate for when the control gains focus.
//----------------------------------------------------------------------------
Boolean NewControl::gainingFocus(IEvent &evt)
{
evt;
// You could create a cursor here if you like.
return true;
}
//----------------------------------------------------------------------------
// Method: NewControl::losingFocus
//
// Description: 'losingFocus' is called in response to a focus loss event,
// 'evt'. It doesn't do anything, but you can fill in any code
// you feel appropriate for when the control loses focus.
//----------------------------------------------------------------------------
Boolean NewControl::losingFocus(IEvent &evt)
{
evt;
// You could destroy a cursor here if you like.
return true;
}
//----------------------------------------------------------------------------
// Method: NewControl::mouseClicked
//
// Description: 'mouseClicked' is called in response to a mouse click event,
// 'evt'. It sends notifications to the owner describing which
// mouse button was clicked and refreshes the window when
// appropriate.
//----------------------------------------------------------------------------
Boolean NewControl::mouseClicked(IMouseClickEvent& evt)
{
// If the mouse is clicked down and NewControl doesn't already
// have the focus, set the focus to it.
if (IMouseClickEvent::down == evt.mouseAction() &&
!hasFocus())
setFocus();
// If the mouse is clicked and released...
if (IMouseClickEvent::click == evt.mouseAction())
{
// If it was button 1, update the click count, paint, and notify owner.
if (IMouseClickEvent::button1 == evt.mouseNumber())
{
++clicks;
refresh();
owner()->sendEvent(WM_CONTROL,
IEventParameter1(id(), IMouseClickEvent::button1));
}
// If it was button 2, just notify owner.
else if (IMouseClickEvent::button2 == evt.mouseNumber())
{
owner()->sendEvent(WM_CONTROL,
IEventParameter1(id(), IMouseClickEvent::button2));
}
}
return true;
}