📄 graphics.zc
字号:
//[of]:description
//[c]Includes graphic objects
//[c]
//[c]canvas
//[c]brush
//[c]font
//[c]cursor
//[c]
//[c]plus utility function to converts coolors
//[cf]
//[of]:license
//[c]Code Browser - a folding text editor for programmers
//[c]Copyright (C) 2003-07 Marc Kerbiquet
//[c]
//[c]This program is free software; you can redistribute it and/or modify
//[c]it under the terms of the GNU General Public License as published by
//[c]the Free Software Foundation; either version 2 of the License, or
//[c](at your option) any later version.
//[c]
//[c]This program is distributed in the hope that it will be useful,
//[c]but WITHOUT ANY WARRANTY; without even the implied warranty of
//[c]MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//[c]GNU General Public License for more details.
//[c]
//[c]You should have received a copy of the GNU General Public License
//[c]along with this program; if not, write to the Free Software
//[c]Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//[cf]
//[of]:imports
import "base/types"
import "base/memory-allocator"
import "text/string"
//[c]
import "graphics/geometry"
import "win32/windows"
import "private/sys-graphics"
//[cf]
//[c]
//[of]:canvas
//[of]:type
public struct canvas: local sys canvas
public height: int
public ascent: int
public descent: int
public ave char width: int
end
//[cf]
//[c]
//[of]:initialize - release
//[of]:release
//[c]
public func release (m: canvas)
SelectObject (hdc (m), hOldPen (m))
DeleteObject (hNullPen (m))
if release dc (m)
ReleaseDC(nil, hdc (m))
end
end
//[cf]
//[cf]
//[of]:clipping
//[of]:save
//[c]Saves the current state of the canvas
//[c]
public func save (m: canvas)
SaveDC (hdc (m))
end
//[cf]
//[of]:restore
//[c]Restores the previous state of the canvas
//[c]
public func restore (m: canvas)
RestoreDC (hdc (m), -1)
end
//[cf]
//[of]:intersect (rectangle)
//[c]Intersects the current clipping region with the rectangle
//[c]
public func intersect (m: canvas, r: rectangle)
IntersectClipRect (hdc (m), r.left, r.top, r.right, r.bottom)
end
//[cf]
//[cf]
//[of]:drawing
//[of]:fill rectangle (rectangle, brush)
//[c]
public func fill rectangle (m: canvas, r: rectangle, b: brush)
def rect: RECT
rect.left = r.left
rect.right = r.right
rect.top = r.top
rect.bottom = r.bottom
FillRect (hdc (m), rect, hbr (b))
end
//[cf]
//[of]:fill rectangle (rectangle, color)
//[c]
public func fill rectangle (m: canvas, r: rectangle, c: color)
def hbr = CreateSolidBrush (c)
def rect: RECT
rect.left = r.left
rect.right = r.right
rect.top = r.top
rect.bottom = r.bottom
FillRect (hdc (m), rect, hbr)
DeleteObject (hbr)
end
//[cf]
//[of]:fill ellipse (rectangle, brush)
//[c]
public func fill ellipse (m: canvas, r: rectangle, b: brush)
def old = SelectObject(hdc (m), b.hbr)
Ellipse(hdc (m), r.left, r.top, r.right, r.bottom)
SelectObject(hdc (m), old)
end
//[cf]
//[of]:draw text (string, rectangle, alignment)
//[c]
public func draw text (m: canvas, str: string, r: rectangle, alignment: int)
SetBkMode (hdc (m), TRANSPARENT)
def rect: RECT
rect.left = r.left
rect.right = r.right
rect.top = r.top
rect.bottom = r.bottom
def format = DT_SINGLELINE | DT_VCENTER
switch alignment
case align left
format |= DT_LEFT
case align right
format |= DT_RIGHT
else
format |= DT_CENTER
end
DrawTextA (hdc (m), str, -1, rect, format)
end
//[cf]
//[of]:text out (x, y, w, h, string, len)
//[c]
public func text out (m: canvas, x: int, y: int, w: int, h: int, str: string, len: dword)
def rect: RECT
rect.left = x
rect.right = x+ w
rect.top = y
rect.bottom = y + h
ExtTextOutA (hdc (m), x, y, ETO_CLIPPED /*|ETO_OPAQUE*/, rect, str, len:int, nil)
end
//[cf]
//[of]:text extent (string, len, area)
//[c]
public func text extent (m: canvas, str: string, len: dword, return a: area)
def size: SIZE
GetTextExtentPoint32A(hdc (m), str, len:int, size)
set(a, cx (size), cy (size))
end
//[cf]
//[of]:text width (buf, len)
//[c]Returns the width of the given string
//[c]
public func text width (c: canvas, buf: string, size: size)
def a: local area
text extent (c, buf, size, a)
return width (a)
end
//[cf]
//[cf]
//[of]:accessing
//[of]:font
//[c]Returns current font
//[c]
public func font (m: canvas)
return the font (m) : font
end
//[cf]
//[of]:set font (font)
//[c]Changes the current font
//[c]
public func set font (m: canvas, f: font)
the font (m) = f
height (m) = height (f)
ascent (m) = ascent (f)
descent (m) = descent (f)
ave char width (m) = ave char width (f)
SelectObject (hdc (m), hFont (f))
end
//[cf]
//[of]:set text color (color)
public func set text color (m: canvas, color: color)
SetTextColor (hdc (m), color)
end
//[cf]
//[of]:set back color (color)
//[c]Sets the background color
//[c]
public func set back color (m: canvas, color: color)
SetBkColor (hdc (m), color)
end
//[cf]
//[cf]
//[cf]
//[of]:brush
//[of]:type
public struct brush: local sys brush
// empty
end
//[cf]
//[c]
//[of]:initialize - release
//[of]:brush from color (color)
//[c]
public func brush from color (color: color, return m: brush)
hbr (m) = CreateSolidBrush (color)
to delete (m) = true
end
//[cf]
//[of]:brush from system color (index)
//[c]
public func brush from system color (index: int, return m: brush)
brush from handle (GetSysColorBrush (index), m)
end
//[cf]
//[c]
//[of]:release
//[c]
public func release (m: brush)
if to delete (m)
DeleteObject (hbr (m))
end
end
//[cf]
//[cf]
//[cf]
//[of]:font
//[of]:type
//[c]
public struct font : local sys font
public face: string
public size: int
private height: int
private ascent: int
private descent: int
private internal leading: int
private external leading: int
private ave char width: int
private max char width: int
end
//[cf]
//[of]:constants
//[c]Style flags
//[c]
public equ fontstyle bold = 1
public equ fontstyle italic = 2
public equ fontstyle underline = 4
//[cf]
//[c]
//[of]:instance creation
//[of]:new font (face, size, style)
//[c]
public func new font (face: string, size: int, style: int)
def m = allocate memory (sizeof local font): font
initialize (m, face, size, style)
return m
end
//[cf]
//[of]:delete
//[c]
public func delete (m: font)
release (m)
free memory (m)
end
//[cf]
//[cf]
//[of]:initialize - release
//[of]:initialize (face, size, style)
//[c]Initializes a newly created font
//[c]
public func initialize (m: font, face: string, size: int, style: int)
def hdc = GetDC (nil)
def height = - (size * GetDeviceCaps(hdc, LOGPIXELSY) + 36) / 72
ReleaseDC (nil, hdc)
def underline = 0
def italic = 0
def weight = FW_NORMAL
if (style & fontstyle italic) <> 0; italic = 1; end
if (style & fontstyle underline) <> 0; underline = 1; end
if (style & fontstyle bold) <> 0; weight = FW_BOLD; end
def hFont = CreateFontA (
height,
0,
0,
0,
weight,
italic,
underline,
0,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE,
face)
if hFont==nil
return false
end
// Get the screen DC
def screen dc = GetDC (nil)
if screen dc == nil
return false
end
// Select the font
def old object = SelectObject (screen dc, hFont)
// Get text metrics
def ok = false
def metrics: local LPTEXTMETRICA
def res = GetTextMetricsA (screen dc, metrics)
if res<>0
face (m) = new string (face)
size (m) = size
hFont (m) = hFont
height (m) = tmHeight (metrics)
ascent (m) = tmAscent (metrics)
descent (m) = tmDescent (metrics)
internal leading (m) = tmInternalLeading (metrics)
external leading (m) = tmExternalLeading (metrics)
ave char width (m) = tmAveCharWidth (metrics)
max char width (m) = tmMaxCharWidth (metrics)
ok = true
end
ReleaseDC(nil, screen dc)
return ok
end
//[cf]
//[of]:release
//[c]
public func release (m: font)
delete (face (m))
DeleteObject (hFont (m))
end
//[cf]
//[cf]
//[cf]
//[of]:cursor
//[of]:type
public struct cursor : local sys cursor
// empty
end
//[cf]
//[of]:constants
//[c]Cursor types
//[c]
public enum cursor type
cursor arrow = IDC_ARROW : int
cursor ibeam = IDC_IBEAM : int
cursor wait = IDC_WAIT : int
cursor no = IDC_NO : int
cursor hand = IDC_HAND : int
end
//[cf]
//[c]
//[of]:instance creation
//[of]:new cursor (type)
//[c]
public func new font (type: cursor type)
def m = allocate memory (sizeof local cursor): cursor
initialize (m, type)
return m
end
//[cf]
//[of]:delete
//[c]
public func delete (m: cursor)
release (m)
free memory (m)
end
//[cf]
//[cf]
//[of]:initialize - release
//[of]:initialize (face, size, style)
//[c]Initializes a newly created cursor
//[c]
public func initialize (m: cursor, type: cursor type)
hCursor (m) = LoadCursorA (nil, type : LPCTSTR)
end
//[cf]
//[of]:release
//[c]
public func release (m: cursor)
// no resource to release, it is a shared cursor
end
//[cf]
//[cf]
//[cf]
//[of]:color
//[of]:type
//[c]The encoding of the color depends on the platform
//[c]
public typedef color = dword
//[cf]
//[of]:constants
//[c]
//[c]System Colors
//[c]
public
equ color scrollbar = COLOR_SCROLLBAR
equ color background = COLOR_BACKGROUND
equ color active caption = COLOR_ACTIVECAPTION
equ color caption text = COLOR_CAPTIONTEXT
equ color inactive caption = COLOR_INACTIVECAPTION
equ color inactive caption text = COLOR_INACTIVECAPTIONTEXT
equ color menu = COLOR_MENU
equ color menu text = COLOR_MENUTEXT
equ color window = COLOR_WINDOW
equ color window text = COLOR_WINDOWTEXT
equ color window frame = COLOR_WINDOWFRAME
equ color active border = COLOR_ACTIVEBORDER
equ color inactive border = COLOR_INACTIVEBORDER
equ color app workspace = COLOR_APPWORKSPACE
equ color highlight = COLOR_HIGHLIGHT
equ color highlight text = COLOR_HIGHLIGHTTEXT
equ color gray text = COLOR_GRAYTEXT
equ color button face = COLOR_BTNFACE
equ color button shadow = COLOR_BTNSHADOW
equ color button text = COLOR_BTNTEXT
equ color button highlight = COLOR_BTNHIGHLIGHT
equ color 3d dark shadow = COLOR_3DDKSHADOW
equ color 3d light = COLOR_3DLIGHT
equ color info text = COLOR_INFOTEXT
equ color info background = COLOR_INFOBK
end
//[cf]
//[c]
//[of]:color from RGB (r, g, b)
//[c]Converts rgb colors to a color for windows
//[c]
public equ color from RGB (r: byte, g: byte, b: byte) =
( r:dword | (g:dword<<8) | (b:dword<<16) ) : color
//[cf]
//[of]:red (c)
//[c]
public equ red (c: color) = c : byte
//[cf]
//[of]:green (c)
//[c]
public equ green (c: color) = (c >> 8) : byte
//[cf]
//[of]:blue (c)
//[c]
public equ blue (c: color) = (c >> 16) : byte
//[cf]
//[c]
//[of]:color from dword (dword)
//[c]
public equ color from dword (d: dword) =
( (d>>16) | (d&0x00FF00) | ((d&0x0000FF)<<16) ) : color
//[cf]
//[of]:dword from color (color)
//[c]
public equ dword from color (d: color) =
( (d>>16) | (d&0x00FF00) | ((d&0x0000FF)<<16) ) : dword
//[cf]
//[c]
//[of]:system color (index)
//[c]Gets a color from system presets
//[c]
public func system color (index: int)
return GetSysColor (index) : color
end
//[cf]
//[cf]
//[of]:alignment
//[c]Alignment of text
//[c]
public enum alignment
align left
align center
align right
end
//[cf]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -