OSDN Git Service

Add facility for control of child window placement.
[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, 2013, 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 #include "wtkdefs.h"
48
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.
53  */
54 #ifdef __cplusplus
55
56 namespace WTK
57 {
58   class StringResource
59   {
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.
65      */
66     public:
67       StringResource( HINSTANCE, unsigned int );
68       operator const char *() const { return value; }
69       ~StringResource(){ free( (void *)(value) ); }
70
71     private:
72       const char *value;
73   };
74
75   class GenericDialogue
76   {
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.
83      */
84     public:
85       GenericDialogue( HINSTANCE app, HWND parent, int ID )
86       { DialogBox( app, MAKEINTRESOURCE( ID ), parent, Dismiss ); }
87       static BOOL CALLBACK Dismiss( HWND, unsigned, WPARAM, LPARAM );
88   };
89
90   class GenericWindow
91   {
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.
96      */
97     protected:
98       HWND AppWindow;
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 );
103
104     public:
105       /* This hook is provided to facilitate the implementation of
106        * sash window controls, (not standard in MS-Windows-API).
107        */
108       virtual long AdjustLayout(){ return 1L; }
109
110     private:
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.
114        *
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.
118        *
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.
122        *
123        */
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; }
136   };
137
138   class WindowClassMaker: protected WNDCLASS, protected GenericWindow
139   {
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.
145      */
146     public:
147       WindowClassMaker( HINSTANCE, unsigned = 0 );
148       inline void SetHandler( WNDPROC MessageHandler )
149       {
150         /* Bind a specified "window procedure" to the registered
151          * class; nominally it is the standard implementation from
152          * the GenericWindow base class.
153          */
154         lpfnWndProc = MessageHandler;
155       }
156       inline void SetIcon( HICON icon )
157       {
158         /* Associate an icon with the registered class.
159          */
160         hIcon = icon;
161       }
162       inline void SetCursor( HCURSOR Cursor )
163       {
164         /* Specify the default cursor to use in windows of
165          * the registered class.
166          */
167         hCursor = Cursor;
168       }
169       inline void SetMenuName( const char *MenuName )
170       {
171         /* Associate a named menu with windows of the registered
172          * class.
173          */
174         lpszMenuName = MenuName;
175       }
176       inline void SetBackground( HBRUSH colour )
177       {
178         /* Set the background colour for windows of the
179          * registered class.
180          */
181         hbrBackground = colour;
182       }
183       inline int Register( const char *ClassName )
184       {
185         /* Register the named window class...
186          */
187         lpszClassName = ClassName;
188         if( int retval = RegisterClass( this ) ) return retval;
189
190         /* ...bailing out, in the event of any error.
191          */
192         throw( runtime_error( "Window Class Registration FAILED" ) );
193       }
194   };
195
196   class WindowMaker: protected GenericWindow
197   {
198     /* An intermediate class, implementing some common methods for
199      * use by derived window classes.
200      */
201     public:
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 ); }
206   };
207
208   class MainWindowMaker: public WindowMaker
209   {
210     /* A stock window class, suitable for providing the implementation
211      * of an application's main window.
212      */
213     public:
214       MainWindowMaker( HINSTANCE instance ): WindowMaker( instance ){}
215       virtual int Invoked();
216
217     private:
218       virtual long OnDestroy();
219   };
220
221   class ChildWindowMaker: public WindowMaker
222   {
223     /* A stock window class, suitable for providing the implementation
224      * of a general purpose child window.
225      */
226     public:
227       ChildWindowMaker( HINSTANCE inst ): WindowMaker( inst ){}
228       HWND Create( int, HWND, const char *, unsigned long = 0 );
229   };
230
231   inline GenericWindow *WindowObjectReference( HWND window )
232   {
233     /* A helper function; it returns a pointer to the C++ class
234      * object which is associated with a specified window handle.
235      */
236     return (GenericWindow *)(GetWindowLongPtr( window, GWLP_USERDATA ));
237   }
238
239   class SashWindowMaker: public ChildWindowMaker
240   {
241     /* An abstract base class, providing the basis for implementation
242      * of both horizontal and vertical sash window controls.
243      */
244     protected:
245       long OnLeftButtonDown();
246       long OnMouseMove( WPARAM );
247       long OnLeftButtonUp();
248
249       RECT frame;
250       double ScaleFactor, DisplacementFactor, MinRangeFactor, MaxRangeFactor;
251       SashWindowMaker( HINSTANCE, double, double, double );
252
253       inline long GetFrameHeight(){ return (frame.bottom - frame.top); }
254       inline long GetFrameWidth(){ return (frame.right - frame.left); }
255
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 );
261
262     public:
263       int Displacement( int span = 1 ){ return (int)(DisplacementFactor * span); }
264   };
265
266   class HorizontalSashWindowMaker: public SashWindowMaker
267   {
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.
271      */
272     public:
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 ); }
278
279     private:
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 );
285   };
286
287   class VerticalSashWindowMaker: public SashWindowMaker
288   {
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.
292      */
293     public:
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 ); }
299
300     private:
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 );
306   };
307 }
308 #endif /* __cplusplus */
309
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...
314  */
315 BEGIN_NAMESPACE( WTK )
316   /*
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++.
320    */
321   EXTERN_C int RaiseAppWindow( HINSTANCE, unsigned int );
322
323 END_NAMESPACE( WTK )
324
325 #endif /* ! WTKLITE_H: $RCSfile$: end of file */