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, 2013, 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"
49 /* This header file is primarily intended to be used only for C++. However,
50 * configure scripts may try to compile it as C, when checking availability;
51 * thus, we ignore C++ specific content when compiling as C, to ensure that
52 * configure doesn't choke in such cases.
60 /* A utility class to facilitate retrieval of string data
61 * from a program module's resource database; instantiated
62 * by passing the program module's instance handle and the
63 * numeric resource ID, its effective "value" is a const
64 * character pointer to a copy of the resource string.
67 StringResource( HINSTANCE, unsigned int );
68 operator const char *() const { return value; }
69 ~StringResource(){ free( (void *)(value) ); }
77 /* A simple class to facilitate display of a dialogue box,
78 * from a resource template. The default dialogue procedure
79 * will accept only a simple dismissal request, (similar to the
80 * behaviour of a MessageBox() dialogue); users may derive from
81 * this class, overriding the Dismiss() method, to implement
82 * more sophisticated dialogues.
85 GenericDialogue( HINSTANCE app, HWND parent, int ID )
86 { DialogBox( app, MAKEINTRESOURCE( ID ), parent, Dismiss ); }
87 static BOOL CALLBACK Dismiss( HWND, unsigned, WPARAM, LPARAM );
92 /* An abstract base class, from which all regular window object
93 * classes are derived; it implements the "window procedure", which
94 * directs windows messages to the appropriate handlers, which are
95 * themselves implemented in the derived classes.
99 HINSTANCE AppInstance;
100 GenericWindow( HINSTANCE appid ): AppInstance( appid ){}
101 static long CALLBACK WindowProcedure( HWND, unsigned, WPARAM, LPARAM );
102 virtual long Controller( unsigned, WPARAM, LPARAM );
105 /* This hook is provided to facilitate the implementation of
106 * sash window controls, (not standard in MS-Windows-API).
108 virtual long AdjustLayout(){ return 1L; }
111 /* The following (incomplete) list identifies the windows
112 * messages which this framework can currently handle, and
113 * implements a default "do nothing" handler for each.
115 * In most cases, the mapping of these handler names to their
116 * equivalent windows message IDs should be self evident. In
117 * case of doubt, see wndproc.cpp for the definitive list.
119 * FIXME: Only a small subset of possible window messages is
120 * currently supported; this list should be extended to cover
121 * all possible message types.
124 virtual long OnCreate(){ return 1L; }
125 virtual long OnCommand( WPARAM ){ return 1L; }
126 virtual long OnSize( WPARAM, int, int ){ return 1L; }
127 virtual long OnHorizontalScroll( int, int, HWND ){ return 1L; }
128 virtual long OnVerticalScroll( int, int, HWND ){ return 1L; }
129 virtual long OnNotify( WPARAM, LPARAM ){ return 1L; }
130 virtual long OnPaint(){ return 1L; }
131 virtual long OnLeftButtonDown(){ return 1L; }
132 virtual long OnLeftButtonUp(){ return 1L; }
133 virtual long OnMouseMove( WPARAM ){ return 1L; }
134 virtual long OnDestroy(){ return 0L; }
135 virtual long OnClose(){ return 1L; }
138 class WindowClassMaker: protected WNDCLASS, protected GenericWindow
140 /* A utility class to facilitate the registration of window
141 * "class" names, (in the MS-Windows-API sense); it provides
142 * an interface to the RegisterClass() function, together with
143 * a complement of inline methods for initialisation of the
144 * attributes of the registered WNDCLASS structure.
147 WindowClassMaker( HINSTANCE, unsigned = 0 );
148 inline void SetHandler( WNDPROC MessageHandler )
150 /* Bind a specified "window procedure" to the registered
151 * class; nominally it is the standard implementation from
152 * the GenericWindow base class.
154 lpfnWndProc = MessageHandler;
156 inline void SetIcon( HICON icon )
158 /* Associate an icon with the registered class.
162 inline void SetCursor( HCURSOR Cursor )
164 /* Specify the default cursor to use in windows of
165 * the registered class.
169 inline void SetMenuName( const char *MenuName )
171 /* Associate a named menu with windows of the registered
174 lpszMenuName = MenuName;
176 inline void SetBackground( HBRUSH colour )
178 /* Set the background colour for windows of the
181 hbrBackground = colour;
183 inline int Register( const char *ClassName )
185 /* Register the named window class...
187 lpszClassName = ClassName;
188 if( int retval = RegisterClass( this ) ) return retval;
190 /* ...bailing out, in the event of any error.
192 throw( runtime_error( "Window Class Registration FAILED" ) );
196 class WindowMaker: protected GenericWindow
198 /* An intermediate class, implementing some common methods for
199 * use by derived window classes.
202 WindowMaker( HINSTANCE inst ): GenericWindow( inst ){}
203 HWND Create( const char *, const char * );
204 int Show( int mode ){ return ShowWindow( AppWindow, mode ); }
205 int Update(){ return UpdateWindow( AppWindow ); }
208 class MainWindowMaker: public WindowMaker
210 /* A stock window class, suitable for providing the implementation
211 * of an application's main window.
214 MainWindowMaker( HINSTANCE instance ): WindowMaker( instance ){}
215 virtual int Invoked();
218 virtual long OnDestroy();
221 class ChildWindowMaker: public WindowMaker
223 /* A stock window class, suitable for providing the implementation
224 * of a general purpose child window.
227 ChildWindowMaker( HINSTANCE inst ): WindowMaker( inst ){}
228 HWND Create( int, HWND, const char *, unsigned long = 0 );
231 inline GenericWindow *WindowObjectReference( HWND window )
233 /* A helper function; it returns a pointer to the C++ class
234 * object which is associated with a specified window handle.
236 return (GenericWindow *)(GetWindowLongPtr( window, GWLP_USERDATA ));
239 class SashWindowMaker: public ChildWindowMaker
241 /* An abstract base class, providing the basis for implementation
242 * of both horizontal and vertical sash window controls.
245 long OnLeftButtonDown();
246 long OnMouseMove( WPARAM );
247 long OnLeftButtonUp();
250 double ScaleFactor, DisplacementFactor, MinRangeFactor, MaxRangeFactor;
251 SashWindowMaker( HINSTANCE, double, double, double );
253 inline long GetFrameHeight(){ return (frame.bottom - frame.top); }
254 inline long GetFrameWidth(){ return (frame.right - frame.left); }
256 virtual const char *CursorStyle( void ) = 0;
257 virtual void SetClippingRegion( long, long, long ) = 0;
258 virtual void SetDisplacementFactor( unsigned long ) = 0;
259 void RegisterWindowClassName( const char * );
260 void ValidateDisplacementFactor( void );
263 int Displacement( int span = 1 ){ return (int)(DisplacementFactor * span); }
266 class HorizontalSashWindowMaker: public SashWindowMaker
268 /* The concrete class implementing a sash bar control, which
269 * divides its owner window into two horizontally adjacent panes
270 * of complementary adjustable width.
273 HorizontalSashWindowMaker
274 ( int id, HWND owner, HINSTANCE app,
275 double minval, double initval, double maxval
276 ): SashWindowMaker( app, minval, initval, maxval )
277 { Create( id, owner, RegisteredClassName(), WS_BORDER ); }
280 static const char *ClassName;
281 const char *RegisteredClassName( void );
282 inline const char *CursorStyle( void ){ return IDC_SIZEWE; }
283 void SetDisplacementFactor( unsigned long );
284 void SetClippingRegion( long, long, long );
287 class VerticalSashWindowMaker: public SashWindowMaker
289 /* The concrete class implementing a sash bar control, which
290 * divides its owner window into two vertically adjacent panes
291 * of complementary adjustable height.
294 VerticalSashWindowMaker
295 ( int id, HWND owner, HINSTANCE app,
296 double minval, double initval, double maxval
297 ): SashWindowMaker( app, minval, initval, maxval )
298 { Create( id, owner, RegisteredClassName(), WS_BORDER ); }
301 static const char *ClassName;
302 const char *RegisteredClassName( void );
303 inline const char *CursorStyle( void ){ return IDC_SIZENS; }
304 void SetClippingRegion( long, long, long );
305 void SetDisplacementFactor( unsigned long );
308 #endif /* __cplusplus */
310 /* We also provide a small collection of functions which, for C++
311 * applications, are considered to live within the WTK namespace,
312 * but may also be invoked from C; to facilitate this, we reopen
313 * the namespace in a C transparent manner...
315 BEGIN_NAMESPACE( WTK )
317 * ...which allows us to declare extern "C" function prototypes,
318 * each of which will be directly visible in C, but subsumed into
319 * the namespace when compiling C++.
321 EXTERN_C int RaiseAppWindow( HINSTANCE, unsigned int );
324 int ChangeCaption( HWND window, const char *caption )
326 /* Helper to change the title bar caption on a specified window.
328 return SendMessage( window, WM_SETTEXT, 0, (LPARAM)(caption) );
333 #endif /* ! WTKLITE_H: $RCSfile$: end of file */