File: GenericCanvas.pas

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (317 lines) | stat: -rw-r--r-- 8,905 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
unit GenericCanvas;

// Copyright (C) 2003, 2004 MySQL AB
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//----------------------------------------------------------------------------------------------------------------------
//
// Proof-of-concept implementation of an OpenGL canvas for database design purposes.

//----------------------------------------------------------------------------------------------------------------------

interface

uses
  Windows, Messages, Controls, Classes, Graphics, Sysutils;

type
  TViewport = record
    Left, Top,
    Width, Height: Integer;
  end;

  TGenericCanvasViewer = class(TWinControl)
  private
    FRenderingContext: HGLRC;
    FDeviceContext: HDC; 
    FViewport: TViewport;
    FOnRender,
    FOnSetup: TNotifyEvent;
    FBackground: TColor;
    FCanRender: Boolean;
    procedure SetBackground(const Value: TColor);

    procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); Message WM_ERASEBKGND;
    procedure WMPaint(var Message: TWMPaint); Message WM_PAINT;
    procedure WMSize(var Message: TWMSize); Message WM_SIZE;
  protected
    procedure ApplyBackground;
    procedure ApplyPerspective;
    procedure ClearBuffers;
    procedure CreateParams(var Params: TCreateParams); override;
    procedure CreateWnd; override;
    procedure DestroyWnd; override;
  public
    constructor Create(AOwner: TComponent); override;

    procedure DisableContext;
    procedure EnableContext;
    procedure RenderScene;

    property DeviceContext: HDC read FDeviceContext;
    property Font;
    property RenderingContext: HGLRC read FRenderingContext;
  published
    property Anchors;
    property Align;
    property Background: TColor read FBackground write SetBackground;
    property Constraints;
    property DragCursor;
    property DragMode;
    property Enabled;
    property HelpContext;
    property Hint;
    property PopupMenu;
    property Visible;

    property OnClick;
    property OnDragDrop;
    property OnDragOver;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnRender: TNotifyEvent read FOnRender write FOnRender;
    property OnSetup: TNotifyEvent read FOnSetup write FOnSetup;
  end;
  
//----------------------------------------------------------------------------------------------------------------------

implementation

//----------------------------------------------------------------------------------------------------------------------

constructor TGenericCanvasViewer.Create(AOwner: TComponent);

begin
  inherited;
  
  Width := 150;
  Height := 150;
  FCanRender := True;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.SetBackground(const Value: TColor);

begin
  if FBackground <> Value then
  begin
    FBackground := Value;
    if not (csLoading in ComponentState) and (FRenderingContext <> 0) and  FCanRender then
      ApplyBackground;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.WMEraseBkgnd(var Message: TWMEraseBkgnd);

begin
  Message.Result := 1;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.WMPaint(var Message: TWMPaint);

var
  PS: TPaintStruct;

begin
  BeginPaint(Handle, PS);
  if FCanRender then
    RenderScene;
  EndPaint(Handle, PS);
  Message.Result := 0;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.WMSize(var Message: TWMSize);

begin
  // Update viewport (here always the entire window).
  with FViewPort do
  begin
    Width := Message.Width;
    Height := Message.Height;
    if Height = 0 then
      Height := 1;
  end;

  if (FRenderingContext <> 0) and  FCanRender then
  begin
    ActivateRenderingContext(FDeviceContext, FRenderingContext);
    ApplyPerspective;
    DeactivateRenderingContext;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.ApplyBackground;

var
  Color: COLORREF;
  R, G, B: Single;

begin
  ActivateRenderingContext(FDeviceContext, FRenderingContext);
  Color := ColorToRGB(FBackground);
  R := GetRValue(Color) / 255;
  G := GetGValue(Color) / 255;
  B := GetBValue(Color) / 255;
  glClearColor(R, G, B, 1);
  DeactivateRenderingContext;
  Refresh;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.ApplyPerspective;

begin
  with FViewport do
  begin
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity;

    //gluPerspective(35, Width / Height, 1, 100);

    glOrtho(Left, Left + Width, Top + Height, Top, -100, 100);
    glMatrixMode(GL_MODELVIEW);
    glViewport(0, 0, Width, Height);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.ClearBuffers;

begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.CreateParams(var Params: TCreateParams);

begin
  inherited;

  with Params do
  begin
    Style := Style or WS_CLIPCHILDREN or WS_CLIPSIBLINGS;
    WindowClass.Style :=  CS_VREDRAW or CS_HREDRAW or CS_OWNDC;
  end
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.CreateWnd;

var
  Dummy: HPALETTE;
  
begin
  inherited;

  FDeviceContext := GetDC(Handle);
  FRenderingContext := CreateRenderingContext(FDeviceContext, [opDoubleBuffered], 32, 0, 0, 0, 0, Dummy);
  if FRenderingContext = 0 then
    raise Exception.Create('Creation of OpenGL rendering context failed.');

  ActivateRenderingContext(FDeviceContext, FRenderingContext);
  if not GL_VERSION_1_1 then
  begin
    DestroyRenderingContext(FRenderingContext);
    raise Exception.Create('Your version of OpenGL is outdated.');
  end;

  // Set the viewport to the entire window size.
  with FViewPort do
  begin
    Left := 0;
    Top := 0;
    Width := Self.Width;
    Height := Self.Height;
    ApplyPerspective;
  end;

  ApplyBackground;

  if Assigned(FOnSetup) then
    FOnSetup(Self);
  DeactivateRenderingContext;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.DestroyWnd;

begin
  ReleaseDC(Handle, FDeviceContext);
  
  inherited;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.DisableContext;

begin
  FCanRender := False;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.EnableContext;

begin
  if not FCanRender then
  begin
    FCanRender := True;
    if FRenderingContext <> 0 then
    begin
      ActivateRenderingContext(FDeviceContext, FRenderingContext);
      ApplyPerspective;
      DeactivateRenderingContext;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TGenericCanvasViewer.RenderScene;

// This is the actual paint routine. All initialization must already have been done.
// The event FOnRender is triggered to let the application draw whatever is needed.

begin
  ActivateRenderingContext(FDeviceContext, FRenderingContext);
  ClearBuffers;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity;
  if Assigned(FOnRender) then
    FOnRender(Self);
  SwapBuffers(FDeviceContext);
  DeactivateRenderingContext;
end;

//----------------------------------------------------------------------------------------------------------------------

end.