5 * ---------------------------------------------------------------------------
7 * Implementation of a minimal C++ class framework for use with the
8 * Microsoft Windows Application Programming Interface.
12 * This header file is to be included by all users of this MS-Windows
13 * C++ class framework.
15 * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
16 * Copyright (C) 2012, MinGW.org Project.
18 * ---------------------------------------------------------------------------
20 * Permission is hereby granted, free of charge, to any person obtaining a
21 * copy of this software and associated documentation files (the "Software"),
22 * to deal in the Software without restriction, including without limitation
23 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
24 * and/or sell copies of the Software, and to permit persons to whom the
25 * Software is furnished to do so, subject to the following conditions:
27 * The above copyright notice, this permission notice, and the following
28 * disclaimer shall be included in all copies or substantial portions of
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
36 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37 * DEALINGS IN THE SOFTWARE.
39 * ---------------------------------------------------------------------------
46 #include "wtkexcept.h"
48 /* This header file is primarily intended to be used only for C++. However,
49 * configure scripts may try to compile it as C, when checking availability;
50 * thus, we ignore C++ specific content when compiling as C, to ensure that
51 * configure doesn't choke in such cases.
59 /* A utility class to facilitate retrieval of string data
60 * from a program module's resource database; instantiated
61 * by passing the program module's instance handle and the
62 * numeric resource ID, its effective "value" is a const
63 * character pointer to a copy of the resource string.
66 StringResource( HINSTANCE, unsigned int );
67 operator const char *() const { return value; }
68 ~StringResource(){ free( (void *)(value) ); }
76 /* A simple class to facilitate display of a dialogue box,
77 * from a resource template. The default dialogue procedure
78 * will accept only a simple dismissal request, (similar to the
79 * behaviour of a MessageBox() dialogue); users may derive from
80 * this class, overriding the Dismiss() method, to implement
81 * more sophisticated dialogues.
84 GenericDialogue( HINSTANCE app, HWND parent, int ID )
85 { DialogBox( app, MAKEINTRESOURCE( ID ), parent, Dismiss ); }
86 static BOOL CALLBACK Dismiss( HWND, unsigned, WPARAM, LPARAM );
91 /* An abstract base class, from which all regular window object
92 * classes are derived; it implements the "window procedure", which
93 * directs windows messages to the appropriate handlers, which are
94 * themselves implemented in the derived classes.
98 HINSTANCE AppInstance;
99 GenericWindow( HINSTANCE appid ): AppInstance( appid ){}
100 static long CALLBACK WindowProcedure( HWND, unsigned, WPARAM, LPARAM );
101 virtual long Controller( unsigned, WPARAM, LPARAM );
104 /* This hook is provided to facilitate the implementation of
105 * sash window controls, (not standard in MS-Windows-API).
107 virtual long AdjustLayout(){ return 1L; }
110 /* The following (incomplete) list identifies the windows
111 * messages which this framework can currently handle, and
112 * implements a default "do nothing" handler for each.
114 * In most cases, the mapping of these handler names to their
115 * equivalent windows message IDs should be self evident. In
116 * case of doubt, see wndproc.cpp for the definitive list.
118 * FIXME: Only a small subset of possible window messages is
119 * currently supported; this list should be extended to cover
120 * all possible message types.
123 virtual long OnCreate(){ return 1L; }
124 virtual long OnCommand( WPARAM ){ return 1L; }
125 virtual long OnSize( WPARAM, int, int ){ return 1L; }
126 virtual long OnHorizontalScroll( int, int, HWND ){ return 1L; }
127 virtual long OnVerticalScroll( int, int, HWND ){ return 1L; }
128 virtual long OnNotify( WPARAM, LPARAM ){ return 1L; }
129 virtual long OnPaint(){ return 1L; }
130 virtual long OnLeftButtonDown(){ return 1L; }
131 virtual long OnLeftButtonUp(){ return 1L; }
132 virtual long OnMouseMove( WPARAM ){ return 1L; }
133 virtual long OnDestroy(){ return 0L; }
134 virtual long OnClose(){ return 1L; }
137 class WindowClassMaker: protected WNDCLASS, protected GenericWindow
139 /* A utility class to facilitate the registration of window
140 * "class" names, (in the MS-Windows-API sense); it provides
141 * an interface to the RegisterClass() function, together with
142 * a complement of inline methods for initialisation of the
143 * attributes of the registered WNDCLASS structure.
146 WindowClassMaker( HINSTANCE, unsigned = 0 );
147 inline void SetHandler( WNDPROC MessageHandler )
149 /* Bind a specified "window procedure" to the registered
150 * class; nominally it is the standard implementation from
151 * the GenericWindow base class.
153 lpfnWndProc = MessageHandler;
155 inline void SetIcon( HICON icon )
157 /* Associate an icon with the registered class.
161 inline void SetCursor( HCURSOR Cursor )
163 /* Specify the default cursor to use in windows of
164 * the registered class.
168 inline void SetMenuName( const char *MenuName )
170 /* Associate a named menu with windows of the registered
173 lpszMenuName = MenuName;
175 inline void SetBackground( HBRUSH colour )
177 /* Set the background colour for windows of the
180 hbrBackground = colour;
182 inline int Register( const char *ClassName )
184 /* Register the named window class...
186 lpszClassName = ClassName;
187 if( int retval = RegisterClass( this ) ) return retval;
189 /* ...bailing out, in the event of any error.
191 throw( runtime_error( "Window Class Registration FAILED" ) );
195 class WindowMaker: protected GenericWindow
197 /* An intermediate class, implementing some common methods for
198 * use by derived window classes.
201 WindowMaker( HINSTANCE inst ): GenericWindow( inst ){}
202 HWND Create( const char *, const char * );
203 int Show( int mode ){ return ShowWindow( AppWindow, mode ); }
204 int Update(){ return UpdateWindow( AppWindow ); }
207 class MainWindowMaker: public WindowMaker
209 /* A stock window class, suitable for providing the implementation
210 * of an application's main window.
213 MainWindowMaker( HINSTANCE instance ): WindowMaker( instance ){}
214 virtual int Invoked();
217 virtual long OnDestroy();
220 class ChildWindowMaker: public WindowMaker
222 /* A stock window class, suitable for providing the implementation
223 * of a general purpose child window.
226 ChildWindowMaker( HINSTANCE inst ): WindowMaker( inst ){}
227 HWND Create( int, HWND, const char *, unsigned long = 0 );
230 inline GenericWindow *WindowObjectReference( HWND window )
232 /* A helper function; it returns a pointer to the C++ class
233 * object which is associated with a specified window handle.
235 return (GenericWindow *)(GetWindowLongPtr( window, GWLP_USERDATA ));
238 class SashWindowMaker: public ChildWindowMaker
240 /* An abstract base class, providing the basis for implementation
241 * of both horizontal and vertical sash window controls.
244 long OnLeftButtonDown();
245 long OnMouseMove( WPARAM );
246 long OnLeftButtonUp();
249 double ScaleFactor, DisplacementFactor, MinRangeFactor, MaxRangeFactor;
250 SashWindowMaker( HINSTANCE, double, double, double );
252 inline long GetFrameHeight(){ return (frame.bottom - frame.top); }
253 inline long GetFrameWidth(){ return (frame.right - frame.left); }
255 virtual const char *CursorStyle( void ) = 0;
256 virtual void SetClippingRegion( long, long, long ) = 0;
257 virtual void SetDisplacementFactor( unsigned long ) = 0;
258 void RegisterWindowClassName( const char * );
259 void ValidateDisplacementFactor( void );
262 int Displacement( int span = 1 ){ return (int)(DisplacementFactor * span); }
265 class HorizontalSashWindowMaker: public SashWindowMaker
267 /* The concrete class implementing a sash bar control, which
268 * divides its owner window into two horizontally adjacent panes
269 * of complementary adjustable width.
272 HorizontalSashWindowMaker
273 ( int id, HWND owner, HINSTANCE app,
274 double minval, double initval, double maxval
275 ): SashWindowMaker( app, minval, initval, maxval )
276 { Create( id, owner, RegisteredClassName(), WS_BORDER ); }
279 static const char *ClassName;
280 const char *RegisteredClassName( void );
281 inline const char *CursorStyle( void ){ return IDC_SIZEWE; }
282 void SetDisplacementFactor( unsigned long );
283 void SetClippingRegion( long, long, long );
286 class VerticalSashWindowMaker: public SashWindowMaker
288 /* The concrete class implementing a sash bar control, which
289 * divides its owner window into two vertically adjacent panes
290 * of complementary adjustable height.
293 VerticalSashWindowMaker
294 ( int id, HWND owner, HINSTANCE app,
295 double minval, double initval, double maxval
296 ): SashWindowMaker( app, minval, initval, maxval )
297 { Create( id, owner, RegisteredClassName(), WS_BORDER ); }
300 static const char *ClassName;
301 const char *RegisteredClassName( void );
302 inline const char *CursorStyle( void ){ return IDC_SIZENS; }
303 void SetClippingRegion( long, long, long );
304 void SetDisplacementFactor( unsigned long );
308 #endif /* __cplusplus */
309 #endif /* ! WTKLITE_H: $RCSfile$: end of file */