OSDN Git Service

Licence wording enhancements.
[mingw/wtklite.git] / wtkplus.h
1 #ifndef WTKPLUS_H
2 /*
3  * wtkplus.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 WTKPLUS_H  1
43
44 #include <stdlib.h>
45 #include <windows.h>
46 #include "wtkexcept.h"
47
48 namespace WTK
49 {
50   class StringResource
51   {
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.
57      */
58     public:
59       StringResource( HINSTANCE, unsigned int );
60       operator const char *() const { return value; }
61       ~StringResource(){ free( (void *)(value) ); }
62
63     private:
64       const char *value;
65   };
66
67   class GenericDialogue
68   {
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.
75      */
76     public:
77       GenericDialogue( HINSTANCE app, HWND parent, int ID )
78       { DialogBox( app, MAKEINTRESOURCE( ID ), parent, Dismiss ); }
79       static BOOL CALLBACK Dismiss( HWND, unsigned, WPARAM, LPARAM );
80   };
81
82   class GenericWindow
83   {
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.
88      */
89     protected:
90       HWND AppWindow;
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 );
95
96     public:
97       /* This hook is provided to facilitate the implementation of
98        * sash window controls, (not standard in MS-Windows-API).
99        */
100       virtual long AdjustLayout(){ return 1L; }
101
102     private:
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.
106        *
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.
110        *
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.
114        *
115        */
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; }
127   };
128
129   class WindowClassMaker: protected WNDCLASS, protected GenericWindow
130   {
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.
136      */
137     public:
138       WindowClassMaker( HINSTANCE, unsigned = 0 );
139       inline void SetHandler( WNDPROC MessageHandler )
140       {
141         /* Bind a specified "window procedure" to the registered
142          * class; nominally it is the standard implementation from
143          * the GenericWindow base class.
144          */
145         lpfnWndProc = MessageHandler;
146       }
147       inline void SetIcon( HICON icon )
148       {
149         /* Associate an icon with the registered class.
150          */
151         hIcon = icon;
152       }
153       inline void SetCursor( HCURSOR Cursor )
154       {
155         /* Specify the default cursor to use in windows of
156          * the registered class.
157          */
158         hCursor = Cursor;
159       }
160       inline void SetMenuName( const char *MenuName )
161       {
162         /* Associate a named menu with windows of the registered
163          * class.
164          */
165         lpszMenuName = MenuName;
166       }
167       inline void SetBackground( HBRUSH colour )
168       {
169         /* Set the background colour for windows of the
170          * registered class.
171          */
172         hbrBackground = colour;
173       }
174       inline int Register( const char *ClassName )
175       {
176         /* Register the named window class...
177          */
178         lpszClassName = ClassName;
179         if( int retval = RegisterClass( this ) ) return retval;
180
181         /* ...bailing out, in the event of any error.
182          */
183         throw( runtime_error( "Window Class Registration FAILED" ) );
184       }
185   };
186
187   class WindowMaker: protected GenericWindow
188   {
189     /* An intermediate class, implementing some common methods for
190      * use by derived window classes.
191      */
192     public:
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 ); }
197   };
198
199   class MainWindowMaker: public WindowMaker
200   {
201     /* A stock window class, suitable for providing the implementation
202      * of an application's main window.
203      */
204     public:
205       MainWindowMaker( HINSTANCE instance ): WindowMaker( instance ){}
206       virtual int Invoked();
207
208     private:
209       virtual long OnDestroy();
210   };
211
212   class ChildWindowMaker: public WindowMaker
213   {
214     /* A stock window class, suitable for providing the implementation
215      * of a general purpose child window.
216      */
217     public:
218       ChildWindowMaker( HINSTANCE inst ): WindowMaker( inst ){}
219       HWND Create( int, HWND, const char *, unsigned long = 0 );
220   };
221
222   inline GenericWindow *WindowObjectReference( HWND window )
223   {
224     /* A helper function; it returns a pointer to the C++ class
225      * object which is associated with a specified window handle.
226      */
227     return (GenericWindow *)(GetWindowLongPtr( window, GWLP_USERDATA ));
228   }
229
230   class SashWindowMaker: public ChildWindowMaker
231   {
232     /* An abstract base class, providing the basis for implementation
233      * of both horizontal and vertical sash window controls.
234      */
235     protected:
236       long OnLeftButtonDown();
237       long OnMouseMove( WPARAM );
238       long OnLeftButtonUp();
239
240       RECT frame;
241       double ScaleFactor, DisplacementFactor, MinRangeFactor, MaxRangeFactor;
242       SashWindowMaker( HINSTANCE, double, double, double );
243
244       inline long GetFrameHeight(){ return (frame.bottom - frame.top); }
245       inline long GetFrameWidth(){ return (frame.right - frame.left); }
246
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 );
252
253     public:
254       int Displacement( int span = 1 ){ return (int)(DisplacementFactor * span); }
255   };
256
257   class HorizontalSashWindowMaker: public SashWindowMaker
258   {
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.
262      */
263     public:
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 ); }
269
270     private:
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 );
276   };
277
278   class VerticalSashWindowMaker: public SashWindowMaker
279   {
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.
283      */
284     public:
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 ); }
290
291     private:
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 );
297   };
298 }
299
300 #endif /* ! WTKPLUS_H: $RCSfile$: end of file */