first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / application / window_classist.h
1 #ifndef WINDOW_CLASSIST_CLASS
2 #define WINDOW_CLASSIST_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : window_classist                                                   *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 2007-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 //! An add-in file providing window class registration and a window procedure.
19 /*!
20   This file makes it easier to add a very simple window to any console or
21   win32 application that might need it (possibly because the app does not
22   create any windows itself, but for crazy insane reasons, a window is still
23   needed by an external agent, ahem installshield).  It implements a very
24   important part of this process, which is setting a window procedure and
25   registering a window class.  Sometime in 2005 or 2006, a windows update
26   came through that made these formerly optional practices mandatory (and
27   broke many of our applications that created windows without a window
28   procedure or class registration).  That occurrence prompted the creation
29   of this class which tries to provide the bare minimum needed to make
30   things work again.
31
32   Example Usage:
33
34   // include our code file to embed the window procedure and register class
35   // methods in whoever needs them.  this should only be needed once per
36   // program.
37
38   #include <application/windows_classist.h>
39
40   // create our simple window...
41
42   basis::astring window_title = "my_freaky_window";
43   basis::astring class_name = "jumbo_stompy_update_crudburger";
44
45   window_handle f_window = create_simplistic_window(window_title, class_name);
46
47   // and then much later, after the window is no longer needed...
48
49   whack_simplistic_window(f_window);
50
51 */
52
53 #include "windoze_helper.h"
54
55 #include <basis/utf_conversion.h>
56
57 namespace application {
58
59 #ifndef __WIN32__
60
61 // this is a placeholder implementation for other platforms.
62 window_handle create_simplistic_window(const basis::astring &formal(window_title),
63     const basis::astring &formal(class_name)) { return NIL; }
64 void whack_simplistic_window(window_handle formal(f_window)) {}
65
66 #else
67
68 //! this is the very simple window procedure used by register_class() below.
69
70 LRESULT CALLBACK window_procedure(HWND hWnd, UINT message,
71     WPARAM wParam, LPARAM lParam)
72 {
73
74   switch (message) {
75     case WM_COMMAND: {
76       int identifier, event;
77       identifier = LOWORD(wParam);
78       event = HIWORD(wParam);
79       return DefWindowProc(hWnd, message, wParam, lParam);
80       break;
81     }
82     case WM_PAINT: {
83       HDC hdc;
84       PAINTSTRUCT ps;
85       hdc = BeginPaint(hWnd, &ps);
86       // hmmm: Add any drawing code here...
87       EndPaint(hWnd, &ps);
88       break;
89     }
90     case WM_DESTROY: {
91       PostQuitMessage(0);
92       break;
93     }
94     default: {
95       return DefWindowProc(hWnd, message, wParam, lParam);
96     }
97   }
98   return 0;
99 }
100
101 //! returns the registered class as a windows atom.
102
103 ATOM register_class(const basis::astring &name)
104 {
105   WNDCLASSEX wcex;
106   wcex.cbSize = sizeof(WNDCLASSEX);
107
108   wcex.style = CS_HREDRAW | CS_VREDRAW;
109   wcex.lpfnWndProc = (WNDPROC)window_procedure;
110   wcex.cbClsExtra = 0;
111   wcex.cbWndExtra = 0;
112   wcex.hInstance = GET_INSTANCE_HANDLE();
113   wcex.hIcon = 0;
114   wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
115   wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
116   wcex.lpszMenuName = 0;
117   basis::to_unicode_persist(temp_class, name);
118   wcex.lpszClassName = temp_class;
119   wcex.hIconSm = 0;
120
121   return RegisterClassEx(&wcex);
122 }
123
124 window_handle create_simplistic_window(const basis::astring &window_title,
125     const basis::astring &class_name)
126 {
127   register_class(class_name);
128   window_handle f_window = CreateWindow(basis::to_unicode_temp(class_name), 
129       basis::to_unicode_temp(window_title), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
130       0, CW_USEDEFAULT, 0, NIL, NIL, GET_INSTANCE_HANDLE(), NIL);
131   ShowWindow(f_window, SW_HIDE);
132   UpdateWindow(f_window);
133   return f_window;
134 }
135
136 void whack_simplistic_window(window_handle f_window)
137 {
138   SendMessage(f_window, WM_CLOSE, NIL, NIL);
139 //hmmm: is this enough?
140 }
141
142 #endif // win32
143
144 } // namespace.
145
146 #endif // outer guard
147