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"
52 /* A utility class to facilitate retrieval of string data
53 * from a program module's resource database; instantiated
54 * by passing the program module's instance handle and the
55 * numeric resource ID, its effective "value" is a const
56 * character pointer to a copy of the resource string.
59 StringResource( HINSTANCE, unsigned int );
60 operator const char *() const { return value; }
61 ~StringResource(){ free( (void *)(value) ); }
69 /* A simple class to facilitate display of a dialogue box,
70 * from a resource template. The default dialogue procedure
71 * will accept only a simple dismissal request, (similar to the
72 * behaviour of a MessageBox() dialogue); users may derive from
73 * this class, overriding the Dismiss() method, to implement
74 * more sophisticated dialogues.
77 GenericDialogue( HINSTANCE app, HWND parent, int ID )
78 { DialogBox( app, MAKEINTRESOURCE( ID ), parent, Dismiss ); }
79 static BOOL CALLBACK Dismiss( HWND, unsigned, WPARAM, LPARAM );
84 /* An abstract base class, from which all regular window object
85 * classes are derived; it implements the "window procedure", which
86 * directs windows messages to the appropriate handlers, which are
87 * themselves implemented in the derived classes.
91 HINSTANCE AppInstance;
92 GenericWindow( HINSTANCE appid ): AppInstance( appid ){}
93 static long CALLBACK WindowProcedure( HWND, unsigned, WPARAM, LPARAM );
94 virtual long Controller( unsigned, WPARAM, LPARAM );
97 /* This hook is provided to facilitate the implementation of
98 * sash window controls, (not standard in MS-Windows-API).
100 virtual long AdjustLayout(){ return 1L; }
103 /* The following (incomplete) list identifies the windows
104 * messages which this framework can currently handle, and
105 * implements a default "do nothing" handler for each.
107 * In most cases, the mapping of these handler names to their
108 * equivalent windows message IDs should be self evident. In
109 * case of doubt, see wndproc.cpp for the definitive list.
111 * FIXME: Only a small subset of possible window messages is
112 * currently supported; this list should be extended to cover
113 * all possible message types.
116 virtual long OnCreate(){ return 1L; }
117 virtual long OnCommand( WPARAM ){ return 1L; }
118 virtual long OnDestroy(){ return 0L; }
119 virtual long OnSize( WPARAM, int, int ){ return 1L; }
120 virtual long OnHorizontalScroll( int, int, HWND ){ return 1L; }
121 virtual long OnVerticalScroll( int, int, HWND ){ return 1L; }
122 virtual long OnNotify( WPARAM, LPARAM ){ return 1L; }
123 virtual long OnPaint(){ return 1L; }
124 virtual long OnLeftButtonDown(){ return 1L; }
125 virtual long OnLeftButtonUp(){ return 1L; }
126 virtual long OnMouseMove( WPARAM ){ return 1L; }
129 class WindowClassMaker: protected WNDCLASS, protected GenericWindow
131 /* A utility class to facilitate the registration of window
132 * "class" names, (in the MS-Windows-API sense); it provides
133 * an interface to the RegisterClass() function, together with
134 * a complement of inline methods for initialisation of the
135 * attributes of the registered WNDCLASS structure.
138 WindowClassMaker( HINSTANCE, unsigned = 0 );
139 inline void SetHandler( WNDPROC MessageHandler )
141 /* Bind a specified "window procedure" to the registered
142 * class; nominally it is the standard implementation from
143 * the GenericWindow base class.
145 lpfnWndProc = MessageHandler;
147 inline void SetIcon( HICON icon )
149 /* Associate an icon with the registered class.
153 inline void SetCursor( HCURSOR Cursor )
155 /* Specify the default cursor to use in windows of
156 * the registered class.
160 inline void SetMenuName( const char *MenuName )
162 /* Associate a named menu with windows of the registered
165 lpszMenuName = MenuName;
167 inline void SetBackground( HBRUSH colour )
169 /* Set the background colour for windows of the
172 hbrBackground = colour;
174 inline int Register( const char *ClassName )
176 /* Register the named window class...
178 lpszClassName = ClassName;
179 if( int retval = RegisterClass( this ) ) return retval;
181 /* ...bailing out, in the event of any error.
183 throw( runtime_error( "Window Class Registration FAILED" ) );
187 class WindowMaker: protected GenericWindow
189 /* An intermediate class, implementing some common methods for
190 * use by derived window classes.
193 WindowMaker( HINSTANCE inst ): GenericWindow( inst ){}
194 HWND Create( const char *, const char * );
195 int Show( int mode ){ return ShowWindow( AppWindow, mode ); }
196 int Update(){ return UpdateWindow( AppWindow ); }
199 class MainWindowMaker: public WindowMaker
201 /* A stock window class, suitable for providing the implementation
202 * of an application's main window.
205 MainWindowMaker( HINSTANCE instance ): WindowMaker( instance ){}
206 virtual int Invoked();
209 virtual long OnDestroy();
212 class ChildWindowMaker: public WindowMaker
214 /* A stock window class, suitable for providing the implementation
215 * of a general purpose child window.
218 ChildWindowMaker( HINSTANCE inst ): WindowMaker( inst ){}
219 HWND Create( int, HWND, const char *, unsigned long = 0 );
222 inline GenericWindow *WindowObjectReference( HWND window )
224 /* A helper function; it returns a pointer to the C++ class
225 * object which is associated with a specified window handle.
227 return (GenericWindow *)(GetWindowLongPtr( window, GWLP_USERDATA ));
230 class SashWindowMaker: public ChildWindowMaker
232 /* An abstract base class, providing the basis for implementation
233 * of both horizontal and vertical sash window controls.
236 long OnLeftButtonDown();
237 long OnMouseMove( WPARAM );
238 long OnLeftButtonUp();
241 double ScaleFactor, DisplacementFactor, MinRangeFactor, MaxRangeFactor;
242 SashWindowMaker( HINSTANCE, double, double, double );
244 inline long GetFrameHeight(){ return (frame.bottom - frame.top); }
245 inline long GetFrameWidth(){ return (frame.right - frame.left); }
247 virtual const char *CursorStyle( void ) = 0;
248 virtual void SetClippingRegion( long, long, long ) = 0;
249 virtual void SetDisplacementFactor( unsigned long ) = 0;
250 void RegisterWindowClassName( const char * );
251 void ValidateDisplacementFactor( void );
254 int Displacement( int span = 1 ){ return (int)(DisplacementFactor * span); }
257 class HorizontalSashWindowMaker: public SashWindowMaker
259 /* The concrete class implementing a sash bar control, which
260 * divides its owner window into two horizontally adjacent panes
261 * of complementary adjustable width.
264 HorizontalSashWindowMaker
265 ( int id, HWND owner, HINSTANCE app,
266 double minval, double initval, double maxval
267 ): SashWindowMaker( app, minval, initval, maxval )
268 { Create( id, owner, RegisteredClassName(), WS_BORDER ); }
271 static const char *ClassName;
272 const char *RegisteredClassName( void );
273 inline const char *CursorStyle( void ){ return IDC_SIZEWE; }
274 void SetDisplacementFactor( unsigned long );
275 void SetClippingRegion( long, long, long );
278 class VerticalSashWindowMaker: public SashWindowMaker
280 /* The concrete class implementing a sash bar control, which
281 * divides its owner window into two vertically adjacent panes
282 * of complementary adjustable height.
285 VerticalSashWindowMaker
286 ( int id, HWND owner, HINSTANCE app,
287 double minval, double initval, double maxval
288 ): SashWindowMaker( app, minval, initval, maxval )
289 { Create( id, owner, RegisteredClassName(), WS_BORDER ); }
292 static const char *ClassName;
293 const char *RegisteredClassName( void );
294 inline const char *CursorStyle( void ){ return IDC_SIZENS; }
295 void SetClippingRegion( long, long, long );
296 void SetDisplacementFactor( unsigned long );
300 #endif /* ! WTKPLUS_H: $RCSfile$: end of file */