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 and this permission notice shall be included
28 * in all copies or substantial portions of the Software.
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
31 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
35 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
36 * DEALINGS IN THE SOFTWARE.
38 * ---------------------------------------------------------------------------
45 #include "wtkexcept.h"
51 /* A utility class to facilitate retrieval of string data
52 * from a program module's resource database; instantiated
53 * by passing the program module's instance handle and the
54 * numeric resource ID, its effective "value" is a const
55 * character pointer to a copy of the resource string.
58 StringResource( HINSTANCE, unsigned int );
59 operator const char *() const { return value; }
60 ~StringResource(){ free( (void *)(value) ); }
68 /* A simple class to facilitate display of a dialogue box,
69 * from a resource template. The default dialogue procedure
70 * will accept only a simple dismissal request, (similar to the
71 * behaviour of a MessageBox() dialogue); users may derive from
72 * this class, overriding the Dismiss() method, to implement
73 * more sophisticated dialogues.
76 GenericDialogue( HINSTANCE app, HWND parent, int ID )
77 { DialogBox( app, MAKEINTRESOURCE( ID ), parent, Dismiss ); }
78 static BOOL CALLBACK Dismiss( HWND, unsigned, WPARAM, LPARAM );
83 /* An abstract base class, from which all regular window object
84 * classes are derived; it implements the "window procedure", which
85 * directs windows messages to the appropriate handlers, which are
86 * themselves implemented in the derived classes.
90 HINSTANCE AppInstance;
91 GenericWindow( HINSTANCE appid ): AppInstance( appid ){}
92 static long CALLBACK WindowProcedure( HWND, unsigned, WPARAM, LPARAM );
93 virtual long Controller( unsigned, WPARAM, LPARAM );
96 /* This hook is provided to facilitate the implementation of
97 * sash window controls, (not standard in MS-Windows-API).
99 virtual long AdjustLayout(){ return 1L; }
102 /* The following (incomplete) list identifies the windows
103 * messages which this framework can currently handle, and
104 * implements a default "do nothing" handler for each.
106 * In most cases, the mapping of these handler names to their
107 * equivalent windows message IDs should be self evident. In
108 * case of doubt, see wndproc.cpp for the definitive list.
110 * FIXME: Only a small subset of possible window messages is
111 * currently supported; this list should be extended to cover
112 * all possible message types.
115 virtual long OnCreate(){ return 1L; }
116 virtual long OnCommand( WPARAM ){ return 1L; }
117 virtual long OnDestroy(){ return 0L; }
118 virtual long OnSize( WPARAM, int, int ){ return 1L; }
119 virtual long OnHorizontalScroll( int, int, HWND ){ return 1L; }
120 virtual long OnVerticalScroll( int, int, HWND ){ return 1L; }
121 virtual long OnNotify( WPARAM, LPARAM ){ return 1L; }
122 virtual long OnPaint(){ return 1L; }
123 virtual long OnLeftButtonDown(){ return 1L; }
124 virtual long OnLeftButtonUp(){ return 1L; }
125 virtual long OnMouseMove( WPARAM ){ return 1L; }
128 class WindowClassMaker: protected WNDCLASS, protected GenericWindow
130 /* A utility class to facilitate the registration of window
131 * "class" names, (in the MS-Windows-API sense); it provides
132 * an interface to the RegisterClass() function, together with
133 * a complement of inline methods for initialisation of the
134 * attributes of the registered WNDCLASS structure.
137 WindowClassMaker( HINSTANCE, unsigned = 0 );
138 inline void SetHandler( WNDPROC MessageHandler )
140 /* Bind a specified "window procedure" to the registered
141 * class; nominally it is the standard implementation from
142 * the GenericWindow base class.
144 lpfnWndProc = MessageHandler;
146 inline void SetIcon( HICON icon )
148 /* Associate an icon with the registered class.
152 inline void SetCursor( HCURSOR Cursor )
154 /* Specify the default cursor to use in windows of
155 * the registered class.
159 inline void SetMenuName( const char *MenuName )
161 /* Associate a named menu with windows of the registered
164 lpszMenuName = MenuName;
166 inline void SetBackground( HBRUSH colour )
168 /* Set the background colour for windows of the
171 hbrBackground = colour;
173 inline int Register( const char *ClassName )
175 /* Register the named window class...
177 lpszClassName = ClassName;
178 if( int retval = RegisterClass( this ) ) return retval;
180 /* ...bailing out, in the event of any error.
182 throw( runtime_error( "Window Class Registration FAILED" ) );
186 class WindowMaker: protected GenericWindow
188 /* An intermediate class, implementing some common methods for
189 * use by derived window classes.
192 WindowMaker( HINSTANCE inst ): GenericWindow( inst ){}
193 HWND Create( const char *, const char * );
194 int Show( int mode ){ return ShowWindow( AppWindow, mode ); }
195 int Update(){ return UpdateWindow( AppWindow ); }
198 class MainWindowMaker: public WindowMaker
200 /* A stock window class, suitable for providing the implementation
201 * of an application's main window.
204 MainWindowMaker( HINSTANCE instance ): WindowMaker( instance ){}
205 virtual int Invoked();
208 virtual long OnDestroy();
211 class ChildWindowMaker: public WindowMaker
213 /* A stock window class, suitable for providing the implementation
214 * of a general purpose child window.
217 ChildWindowMaker( HINSTANCE inst ): WindowMaker( inst ){}
218 HWND Create( int, HWND, const char *, unsigned long = 0 );
221 inline GenericWindow *WindowObjectReference( HWND window )
223 /* A helper function; it returns a pointer to the C++ class
224 * object which is associated with a specified window handle.
226 return (GenericWindow *)(GetWindowLongPtr( window, GWLP_USERDATA ));
229 class SashWindowMaker: public ChildWindowMaker
231 /* An abstract base class, providing the basis for implementation
232 * of both horizontal and vertical sash window controls.
235 long OnLeftButtonDown();
236 long OnMouseMove( WPARAM );
237 long OnLeftButtonUp();
240 double ScaleFactor, DisplacementFactor, MinRangeFactor, MaxRangeFactor;
241 SashWindowMaker( HINSTANCE, double, double, double );
243 inline long GetFrameHeight(){ return (frame.bottom - frame.top); }
244 inline long GetFrameWidth(){ return (frame.right - frame.left); }
246 virtual const char *CursorStyle( void ) = 0;
247 virtual void SetClippingRegion( long, long, long ) = 0;
248 virtual void SetDisplacementFactor( unsigned long ) = 0;
249 void RegisterWindowClassName( const char * );
250 void ValidateDisplacementFactor( void );
253 int Displacement( int span = 1 ){ return (int)(DisplacementFactor * span); }
256 class HorizontalSashWindowMaker: public SashWindowMaker
258 /* The concrete class implementing a sash bar control, which
259 * divides its owner window into two horizontally adjacent panes
260 * of complementary adjustable width.
263 HorizontalSashWindowMaker
264 ( int id, HWND owner, HINSTANCE app,
265 double minval, double initval, double maxval
266 ): SashWindowMaker( app, minval, initval, maxval )
267 { Create( id, owner, RegisteredClassName(), WS_BORDER ); }
270 static const char *ClassName;
271 const char *RegisteredClassName( void );
272 inline const char *CursorStyle( void ){ return IDC_SIZEWE; }
273 void SetDisplacementFactor( unsigned long );
274 void SetClippingRegion( long, long, long );
277 class VerticalSashWindowMaker: public SashWindowMaker
279 /* The concrete class implementing a sash bar control, which
280 * divides its owner window into two vertically adjacent panes
281 * of complementary adjustable height.
284 VerticalSashWindowMaker
285 ( int id, HWND owner, HINSTANCE app,
286 double minval, double initval, double maxval
287 ): SashWindowMaker( app, minval, initval, maxval )
288 { Create( id, owner, RegisteredClassName(), WS_BORDER ); }
291 static const char *ClassName;
292 const char *RegisteredClassName( void );
293 inline const char *CursorStyle( void ){ return IDC_SIZENS; }
294 void SetClippingRegion( long, long, long );
295 void SetDisplacementFactor( unsigned long );
299 #endif /* ! WTKPLUS_H: $RCSfile$: end of file */