OSDN Git Service

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