Changeset 22 for branches/overos/UWindow.pas
- Timestamp:
- Dec 30, 2018, 1:01:14 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/overos/UWindow.pas
r21 r22 19 19 procedure DrawArea(Rect: TRectangle; Color: TColor); override; 20 20 procedure DrawText(P: TPosition; Color: TColor; Text: string); override; 21 function GetTextSize(Text: string): TSize; override; 21 22 end; 22 23 23 24 TWindowSide = (wsNone, wsLeft, wsTop, wsRight, wsBottom); 24 25 25 { TWindow } 26 27 TWindow = class(TControl) 26 { TTitleBar } 27 28 TTitleBar = class(TControl) 29 private 30 FWindow: TWindow; 31 procedure SetWindow(AValue: TWindow); 32 procedure ButtonCloseClick(Sender: TObject); 33 procedure ButtonMaximizeClick(Sender: TObject); 34 procedure ButtonMinimizeClick(Sender: TObject); 35 protected 36 procedure SetRectangle(AValue: TRectangle); override; 28 37 public 29 38 const 30 39 TitleHeight = 32; 40 var 41 ButtonClose: TButton; 42 ButtonMaximize: TButton; 43 ButtonMinimize: TButton; 44 constructor Create; override; 45 destructor Destroy; override; 46 property Window: TWindow read FWindow write SetWindow; 47 end; 48 49 TMessageWindowClose = class(TMessage); 50 51 { TWindow } 52 53 TWindow = class(TControl) 54 private 55 FScreen: TScreen; 56 procedure SetScreen(AValue: TScreen); 57 protected 58 procedure SetRectangle(AValue: TRectangle); override; 59 procedure SetVisible(AValue: Boolean); override; 60 public 61 const 31 62 BorderGrabWidth = 15; 32 63 var 64 TitleBar: TTitleBar; 33 65 Title: string; 34 Screen: TScreen; 66 Application: TObject; // TApplication 67 procedure MouseButtonDown(Pos: TPosition; Button: TMouseButton); override; 68 procedure MouseButtonUp(Pos: TPosition; Button: TMouseButton); override; 69 procedure Close; 35 70 procedure Focus; 36 71 procedure Paint; override; … … 38 73 constructor Create; override; 39 74 destructor Destroy; override; 75 property Screen: TScreen read FScreen write SetScreen; 40 76 end; 41 77 … … 68 104 implementation 69 105 106 uses 107 UApplication; 108 109 { TTitleBar } 110 111 procedure TTitleBar.SetWindow(AValue: TWindow); 112 begin 113 if FWindow = AValue then Exit; 114 Canvas.Free; 115 FWindow := AValue; 116 Canvas := TCanvasWindow.Create; 117 TCanvasWindow(Canvas).Window := FWindow; 118 end; 119 120 procedure TTitleBar.ButtonCloseClick(Sender: TObject); 121 begin 122 Window.Close; 123 end; 124 125 procedure TTitleBar.ButtonMaximizeClick(Sender: TObject); 126 begin 127 Rectangle := TRectangle.Create(TPosition.Create(0, 0), Window.Screen.Size); 128 Paint; 129 end; 130 131 procedure TTitleBar.ButtonMinimizeClick(Sender: TObject); 132 begin 133 134 end; 135 136 procedure TTitleBar.SetRectangle(AValue: TRectangle); 137 begin 138 inherited; 139 ButtonClose.Rectangle := TRectangle.Create(TPosition.Create(Size.Width - TitleHeight, 4), 140 TSize.Create(TitleHeight - 8, TitleHeight - 8)); 141 ButtonMaximize.Rectangle := TRectangle.Create(TPosition.Create(Size.Width - 2 * TitleHeight, 4), 142 TSize.Create(TitleHeight - 8, TitleHeight - 8)); 143 ButtonMinimize.Rectangle := TRectangle.Create(TPosition.Create(Size.Width - 3 * TitleHeight, 4), 144 TSize.Create(TitleHeight - 8, TitleHeight - 8)); 145 end; 146 147 constructor TTitleBar.Create; 148 begin 149 inherited; 150 151 ButtonClose := TButton.Create; 152 ButtonClose.Title := 'X'; 153 ButtonClose.ParentControl := Self; 154 ButtonClose.OnClick := ButtonCloseClick; 155 ButtonClose.Visible := True; 156 157 ButtonMaximize := TButton.Create; 158 ButtonMaximize.Title := '^'; 159 ButtonMaximize.ParentControl := Self; 160 ButtonMaximize.OnClick := ButtonMaximizeClick; 161 ButtonMaximize.Visible := True; 162 163 ButtonMinimize := TButton.Create; 164 ButtonMinimize.Title := '_'; 165 ButtonMinimize.ParentControl := Self; 166 ButtonMinimize.OnClick := ButtonMinimizeClick; 167 ButtonMinimize.Visible := True; 168 end; 169 170 destructor TTitleBar.Destroy; 171 begin 172 ButtonClose.Free; 173 ButtonMaximize.Free; 174 ButtonMinimize.Free; 175 inherited Destroy; 176 end; 177 70 178 { TCanvasWindow } 71 179 72 180 procedure TCanvasWindow.DrawLine(P1, P2: TPosition; Color: TColor); 73 181 begin 74 Window.Screen.Canvas.DrawLine(P1 + Window.Rectangle.Position, P2 + Window.Rectangle.Position, Color); 182 if Assigned(Window) and Assigned(Window.Screen) then 183 Window.Screen.Canvas.DrawLine(P1 + Window.Position, P2 + Window.Position, Color); 75 184 end; 76 185 77 186 procedure TCanvasWindow.DrawArea(Rect: TRectangle; Color: TColor); 78 187 begin 79 Window.Screen.Canvas.DrawArea(TRectangle.Create(Rect.Position + Window.Rectangle.Position, 80 Rect.Size), Color); 188 if Assigned(Window) and Assigned(Window.Screen) then 189 Window.Screen.Canvas.DrawArea(TRectangle.Create(Rect.Position + Window.Position, 190 Rect.Size), Color); 81 191 end; 82 192 83 193 procedure TCanvasWindow.DrawText(P: TPosition; Color: TColor; Text: string); 84 194 begin 85 Window.Screen.Canvas.DrawText(P + Window.Rectangle.Position, Color, Text); 195 if Assigned(Window) and Assigned(Window.Screen) then 196 Window.Screen.Canvas.DrawText(P + Window.Position, Color, Text); 197 end; 198 199 function TCanvasWindow.GetTextSize(Text: string): TSize; 200 begin 201 if Assigned(Window) and Assigned(Window.Screen) then 202 Result := Window.Screen.Canvas.GetTextSize(Text); 86 203 end; 87 204 88 205 { TWindow } 206 207 procedure TWindow.SetScreen(AValue: TScreen); 208 begin 209 if FScreen = AValue then Exit; 210 if Assigned(FScreen) then 211 FScreen.Windows.Remove(Self); 212 FScreen := AValue; 213 if Assigned(FScreen) then 214 FScreen.Windows.Add(Self); 215 end; 216 217 procedure TWindow.SetRectangle(AValue: TRectangle); 218 begin 219 inherited; 220 TitleBar.Rectangle := TRectangle.Create(Position, TSize.Create(Size.Width, TitleBar.TitleHeight)); 221 end; 222 223 procedure TWindow.SetVisible(AValue: Boolean); 224 begin 225 inherited; 226 if not Visible and Assigned(Screen) then 227 Screen.Paint; 228 end; 229 230 procedure TWindow.MouseButtonDown(Pos: TPosition; Button: TMouseButton); 231 begin 232 inherited; 233 TitleBar.MouseButtonDown(Pos, Button); 234 end; 235 236 procedure TWindow.MouseButtonUp(Pos: TPosition; Button: TMouseButton); 237 begin 238 inherited; 239 TitleBar.MouseButtonUp(Pos, Button); 240 end; 241 242 procedure TWindow.Close; 243 begin 244 TApplication(Application).MessageQueue.PostMessage(Self, TMessageWindowClose.Create); 245 end; 89 246 90 247 procedure TWindow.Focus; … … 98 255 procedure TWindow.Paint; 99 256 begin 100 Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clGray); 101 Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite); 102 Canvas.DrawLine(TPosition.Create(0, TitleHeight), 103 TPosition.Create(Rectangle.Size.Width, TitleHeight), clWhite); 104 Canvas.DrawText(TPosition.Create(8, 4), clWhite, Title); 257 if Visible then begin 258 Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Size), clGray); 259 Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Size), clWhite); 260 Canvas.DrawLine(TPosition.Create(0, TitleBar.TitleHeight), 261 TPosition.Create(Size.Width, TitleBar.TitleHeight), clWhite); 262 Canvas.DrawText(TPosition.Create(8, 4), clWhite, Title); 263 TitleBar.Paint; 264 end; 105 265 inherited; 106 266 end; … … 117 277 Canvas := TCanvasWindow.Create; 118 278 TCanvasWindow(Canvas).Window := Self; 279 TitleBar := TTitleBar.Create; 280 TitleBar.Window := Self; 281 TitleBar.Visible := True; 119 282 end; 120 283 121 284 destructor TWindow.Destroy; 122 285 begin 286 Visible := False; 287 TitleBar.Free; 288 Screen := nil; 123 289 inherited Destroy; 124 290 end; … … 133 299 for I := Windows.Count - 1 downto 0 do begin 134 300 Window := Windows[I]; 135 if TRectangle.Create(Window. Rectangle.Position - TPosition.Create(Window.BorderGrabWidth div 2, 0),136 TSize.Create(Window.BorderGrabWidth, Window. Rectangle.Size.Height)).Contains(Pos) then begin301 if TRectangle.Create(Window.Position - TPosition.Create(Window.BorderGrabWidth div 2, 0), 302 TSize.Create(Window.BorderGrabWidth, Window.Size.Height)).Contains(Pos) then begin 137 303 GrabMousePosition := Pos; 138 304 GrabWindowRectangle := Window.Rectangle; … … 141 307 GrabActive := True; 142 308 end else 143 if TRectangle.Create(Window. Rectangle.Position - TPosition.Create(0, Window.BorderGrabWidth div 2),144 TSize.Create(Window. Rectangle.Size.Width, Window.BorderGrabWidth)).Contains(Pos) then begin309 if TRectangle.Create(Window.Position - TPosition.Create(0, Window.BorderGrabWidth div 2), 310 TSize.Create(Window.Size.Width, Window.BorderGrabWidth)).Contains(Pos) then begin 145 311 GrabMousePosition := Pos; 146 312 GrabWindowRectangle := Window.Rectangle; … … 149 315 GrabActive := True; 150 316 end else 151 if TRectangle.Create(Window. Rectangle.Position + TPosition.Create(Window.Rectangle.Size.Width, 0) - TPosition.Create(Window.BorderGrabWidth div 2, 0),152 TSize.Create(Window.BorderGrabWidth, Window. Rectangle.Size.Height)).Contains(Pos) then begin317 if TRectangle.Create(Window.Position + TPosition.Create(Window.Size.Width, 0) - TPosition.Create(Window.BorderGrabWidth div 2, 0), 318 TSize.Create(Window.BorderGrabWidth, Window.Size.Height)).Contains(Pos) then begin 153 319 GrabMousePosition := Pos; 154 320 GrabWindowRectangle := Window.Rectangle; … … 157 323 GrabActive := True; 158 324 end else 159 if TRectangle.Create(Window. Rectangle.Position + TPosition.Create(0, Window.Rectangle.Size.Height) - TPosition.Create(0, Window.BorderGrabWidth div 2),160 TSize.Create(Window. Rectangle.Size.Width, Window.BorderGrabWidth)).Contains(Pos) then begin325 if TRectangle.Create(Window.Position + TPosition.Create(0, Window.Size.Height) - TPosition.Create(0, Window.BorderGrabWidth div 2), 326 TSize.Create(Window.Size.Width, Window.BorderGrabWidth)).Contains(Pos) then begin 161 327 GrabMousePosition := Pos; 162 328 GrabWindowRectangle := Window.Rectangle; … … 165 331 GrabActive := True; 166 332 end else 167 if TRectangle.Create(Window. Rectangle.Position, TSize.Create(Window.Rectangle.Size.Width, Window.TitleHeight)).Contains(Pos) then begin333 if TRectangle.Create(Window.Position, TSize.Create(Window.Size.Width, Window.TitleBar.TitleHeight)).Contains(Pos) then begin 168 334 GrabMousePosition := Pos; 169 335 GrabWindowRectangle := Window.Rectangle; … … 174 340 if Window.Rectangle.Contains(Pos) then begin 175 341 Window.Focus; 176 Window.MouseButtonDown(Pos - Window. Rectangle.Position, Button);342 Window.MouseButtonDown(Pos - Window.Position, Button); 177 343 Break; 178 344 end; … … 192 358 Window := Windows[I]; 193 359 if Window.Rectangle.Contains(Pos) then begin 194 Window.MouseButtonUp(Pos - Window. Rectangle.Position, Button);360 Window.MouseButtonUp(Pos - Window.Position, Button); 195 361 Break; 196 362 end; … … 204 370 if GrabActive then begin 205 371 if GrabWindowSide = wsNone then 206 GrabWindow. Rectangle.Position := GrabWindowRectangle.Position + (Pos - GrabMousePosition)372 GrabWindow.Position := GrabWindowRectangle.Position + (Pos - GrabMousePosition) 207 373 else if GrabWindowSide = wsLeft then 208 374 GrabWindow.Rectangle := TRectangle.Create(GrabWindowRectangle.Position + TPosition.Create(Pos.Left - GrabMousePosition.Left, 0), … … 221 387 for I := Windows.Count - 1 downto 0 do begin 222 388 if Windows[I].Rectangle.Contains(Pos) then begin 223 Windows[I].MouseMove(Pos - Windows[I]. Rectangle.Position);389 Windows[I].MouseMove(Pos - Windows[I].Position); 224 390 Break; 225 391 end; … … 255 421 begin 256 422 Result := TWindow.Create; 257 Windows.Add(Result);258 423 Result.Screen := Self; 259 424 Result.Title := Title; … … 265 430 begin 266 431 Windows := TFPGObjectList<TWindow>.Create; 432 Windows.FreeObjects := False; 267 433 Canvas := TCanvas.Create; 268 434 end; 269 435 270 436 destructor TScreen.Destroy; 271 begin 437 var 438 I: Integer; 439 begin 440 for I := 0 to Windows.Count - 1 do 441 Windows[I].Free; 272 442 Windows.Free; 273 443 Canvas.Free;
Note:
See TracChangeset
for help on using the changeset viewer.