OSDN Git Service

Add default handler for WM_CLOSE message.
[mingw/wtklite.git] / wtklite.h
1 #ifndef WTKLITE_H
2 /*
3  * wtklite.h
4  *
5  * ---------------------------------------------------------------------------
6  *
7  * Implementation of a minimal C++ class framework for use with the
8  * Microsoft Windows Application Programming Interface.
9  *
10  * $Id$
11  *
12  * This header file is to be included by all users of this MS-Windows
13  * C++ class framework.
14  *
15  * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
16  * Copyright (C) 2012, MinGW.org Project.
17  *
18  * ---------------------------------------------------------------------------
19  *
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:
26  *
27  * The above copyright notice, this permission notice, and the following
28  * disclaimer shall be included in all copies or substantial portions of
29  * the Software.
30  *
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.
38  *
39  * ---------------------------------------------------------------------------
40  *
41  */
42 #define WTKLITE_H  1
43
44 #include <stdlib.h>
45 #include <windows.h>
46 #include "wtkexcept.h"
47
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.
52  */
53 #ifdef __cplusplus
54
55 namespace WTK
56 {
57   class StringResource
58   {
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.
64      */
65     public:
66       StringResource( HINSTANCE, unsigned int );
67       operator const char *() const { return value; }
68       ~StringResource(){ free( (void *)(value) ); }
69
70     private:
71       const char *value;
72   };
73
74   class GenericDialogue
75   {
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.
82      */
83     public:
84       GenericDialogue( HINSTANCE app, HWND parent, int ID )
85       { DialogBox( app, MAKEINTRESOURCE( ID ), parent, Dismiss ); }
86       static BOOL CALLBACK Dismiss( HWND, unsigned, WPARAM, LPARAM );
87   };
88
89   class GenericWindow
90   {
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.
95      */
96     protected:
97       HWND AppWindow;
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 );
102
103     public:
104       /* This hook is provided to facilitate the implementation of
105        * sash window controls, (not standard in MS-Windows-API).
106        */
107       virtual long AdjustLayout(){ return 1L; }
108
109     private:
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.
113        *
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.
117        *
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.
121        *
122        */
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; }
135   };
136
137   class WindowClassMaker: protected WNDCLASS, protected GenericWindow
138   {
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.
144      */
145     public:
146       WindowClassMaker( HINSTANCE, unsigned = 0 );
147       inline void SetHandler( WNDPROC MessageHandler )
148       {
149         /* Bind a specified "window procedure" to the registered
150          * class; nominally it is the standard implementation from
151          * the GenericWindow base class.
152          */
153         lpfnWndProc = MessageHandler;
154       }
155       inline void SetIcon( HICON icon )
156       {
157         /* Associate an icon with the registered class.
158          */
159         hIcon = icon;
160       }
161       inline void SetCursor( HCURSOR Cursor )
162       {
163         /* Specify the default cursor to use in windows of
164          * the registered class.
165          */
166         hCursor = Cursor;
167       }
168       inline void SetMenuName( const char *MenuName )
169       {
170         /* Associate a named menu with windows of the registered
171          * class.
172          */
173         lpszMenuName = MenuName;
174       }
175       inline void SetBackground( HBRUSH colour )
176       {
177         /* Set the background colour for windows of the
178          * registered class.
179          */
180         hbrBackground = colour;
181       }
182       inline int Register( const char *ClassName )
183       {
184         /* Register the named window class...
185          */
186         lpszClassName = ClassName;
187         if( int retval = RegisterClass( this ) ) return retval;
188
189         /* ...bailing out, in the event of any error.
190          */
191         throw( runtime_error( "Window Class Registration FAILED" ) );
192       }
193   };
194
195   class WindowMaker: protected GenericWindow
196   {
197     /* An intermediate class, implementing some common methods for
198      * use by derived window classes.
199      */
200     public:
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 ); }
205   };
206
207   class MainWindowMaker: public WindowMaker
208   {
209     /* A stock window class, suitable for providing the implementation
210      * of an application's main window.
211      */
212     public:
213       MainWindowMaker( HINSTANCE instance ): WindowMaker( instance ){}
214       virtual int Invoked();
215
216     private:
217       virtual long OnDestroy();
218   };
219
220   class ChildWindowMaker: public WindowMaker
221   {
222     /* A stock window class, suitable for providing the implementation
223      * of a general purpose child window.
224      */
225     public:
226       ChildWindowMaker( HINSTANCE inst ): WindowMaker( inst ){}
227       HWND Create( int, HWND, const char *, unsigned long = 0 );
228   };
229
230   inline GenericWindow *WindowObjectReference( HWND window )
231   {
232     /* A helper function; it returns a pointer to the C++ class
233      * object which is associated with a specified window handle.
234      */
235     return (GenericWindow *)(GetWindowLongPtr( window, GWLP_USERDATA ));
236   }
237
238   class SashWindowMaker: public ChildWindowMaker
239   {
240     /* An abstract base class, providing the basis for implementation
241      * of both horizontal and vertical sash window controls.
242      */
243     protected:
244       long OnLeftButtonDown();
245       long OnMouseMove( WPARAM );
246       long OnLeftButtonUp();
247
248       RECT frame;
249       double ScaleFactor, DisplacementFactor, MinRangeFactor, MaxRangeFactor;
250       SashWindowMaker( HINSTANCE, double, double, double );
251
252       inline long GetFrameHeight(){ return (frame.bottom - frame.top); }
253       inline long GetFrameWidth(){ return (frame.right - frame.left); }
254
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 );
260
261     public:
262       int Displacement( int span = 1 ){ return (int)(DisplacementFactor * span); }
263   };
264
265   class HorizontalSashWindowMaker: public SashWindowMaker
266   {
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.
270      */
271     public:
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 ); }
277
278     private:
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 );
284   };
285
286   class VerticalSashWindowMaker: public SashWindowMaker
287   {
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.
291      */
292     public:
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 ); }
298
299     private:
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 );
305   };
306 }
307
308 #endif /* __cplusplus */
309 #endif /* ! WTKLITE_H: $RCSfile$: end of file */