📄 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"
import "text/string-buffer"
//[c]
import "graphics/geometry"
//[c]
import "private/sys-graphics"
import "glib/glib"
import "gdk/gdk"
import "gtk/gtk"
import "pango/pango"
//[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)
release (super (m))
end
//[cf]
//[cf]
//[of]:clipping
//[of]:save
//[c]Save 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]Draws rectangle
//[c]
public func fill rectangle (m: canvas, r: rectangle, b: brush)
def window = drawable (m) : GdkWindow
gdk_window_set_back_pixmap (window, pixmap (b), FALSE)
gdk_window_clear_area (
window,
x (r),
y (r),
w (r),
h (r))
// restore to nil
gdk_window_set_back_pixmap (window, nil, FALSE)
end
//[cf]
//[of]:fill rectangle (rectangle, color)
//[c]Draws rectangle
//[c]
public func fill rectangle (m: canvas, r: rectangle, col: color)
def c := GdkColor (col)
gdk_gc_set_rgb_fg_color (gc (m), c)
gdk_draw_rectangle (
drawable (m),
gc (m),
TRUE,
x (r),
y (r),
w (r),
h (r))
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 font = the font (m) : font
def desc = desc (font)
if not nil (desc)
def bytes : [1] gsize
def s = to UTF8 (str, len, bytes)
def layout = layout (m)
pango_layout_set_text (layout, s, bytes[]:int)
gdk_gc_set_rgb_fg_color (gc (m), back color (m))
gdk_draw_rectangle (
drawable (m),
gc (m),
TRUE,
x,
y,
w,
height (m))
gdk_gc_set_rgb_fg_color (gc (m), text color (m))
def line = pango_layout_get_line (layout, 0)
gdk_draw_layout_line (drawable (m), gc (m), x, y + ascent (m), line)
return
else
gdk_gc_set_rgb_fg_color (gc (m), back color (m))
gdk_draw_rectangle (
drawable (m),
gc (m),
TRUE,
x,
y,
w,
h)
gdk_gc_set_rgb_fg_color (gc (m), text color (m))
gdk_draw_text (
drawable (m),
id (font),
gc (m),
x,
y + ascent (m),
str,
len:gint)
end
end
//[cf]
//[of]:text extent (string, len, area)
//[c]
public func text extent (m: canvas, str: string, len: dword, a: area)
def font = the font (m) : font
def desc = desc (font)
if not nil (desc)
def bytes : [1] gsize
def s = to UTF8 (str, len, bytes)
def layout = layout (m)
pango_layout_set_text (layout, s, bytes[]:int)
def w : [1] int
def h : [1] int
pango_layout_get_pixel_size (layout, w, h)
w (a) = w[]
h (a) = h[]
else
w (a) = gdk_text_width (id (font), str, len: gint)
h (a) = height (font)
end
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 the 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
def id = id (f)
if not nil (id)
height (m) = height (f)
ascent (m) = ascent (f)
descent (m) = descent (f)
ave char width (m) = ave char width (f)
if is nil (gc (m))
return
end
gdk_gc_set_font (gc (m), id)
return
end
def desc = desc (f)
if not nil (desc)
def context = context (m)
pango_context_set_font_description (context, desc)
pango_layout_set_font_description (layout (m), desc)
def language = pango_context_get_language (context)
def metrics = pango_context_get_metrics (context, desc, language)
ascent (m) = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics))
descent (m) = PANGO_PIXELS (pango_font_metrics_get_descent (metrics))
ave char width (m) = PANGO_PIXELS (pango_font_metrics_get_approximate_char_width (metrics))
height (m) = ascent (m) + descent (m)
end
end
//[cf]
//[of]:set text color (color)
//[c]Changes the current text color
//[c]
public func set text color (m: canvas, color: color)
GdkColor (color, text color (m))
if not nil (gc (m))
gdk_gc_set_rgb_fg_color (gc (m), text color (m))
end
end
//[cf]
//[of]:set back color (color)
//[c]Changes the background color
//[c]
public func set back color (m: canvas, color: color)
GdkColor (color, back color (m))
gdk_gc_set_rgb_bg_color (gc (m), back color (m))
end
//[cf]
//[cf]
//[cf]
//[of]:brush
//[of]:type
public struct brush: local sys brush
// empty
end
//[cf]
//[c]
//[of]:initialize - release
//[of]:brush from system color (index)
//[c]
public func brush from system color (index: system color, return m: brush)
def style = gtk_widget_get_default_style
pixmap (m) = gtk_style_get_pixmap (style, GTK_STATE_NORMAL:int)
end
//[cf]
//[of]:release (m)
//[c]
public func release (m: brush)
// no resource to free
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]
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)
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
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 ok = create font (m, face, size, style)
if ok
return ok
end
// provide a default one
return create font (m, "-*-fixed-*-r-normal-*-13-*-*-*-*-*-iso8859-1", 0, 0)
end
//[c]
//[c]SUB-FUNCTIONS
//[of]: create font (font, face, size, style)
private func create font (m: font, face: string, size: int, style: int)
id (m) = nil
desc (m) = nil
if starts with (face, $!)
face += 1
def weight = PANGO_WEIGHT_NORMAL
def italic = PANGO_STYLE_NORMAL
if (style & fontstyle italic) <> 0; italic = PANGO_STYLE_ITALIC; end
if (style & fontstyle bold) <> 0; weight = PANGO_WEIGHT_BOLD; end
def desc = pango_font_description_new
pango_font_description_set_family (desc, face)
pango_font_description_set_size (desc, size * PANGO_SCALE)
pango_font_description_set_weight (desc, weight)
pango_font_description_set_style (desc, italic)
face (m) = new string (face)
size (m) = size
desc (m) = desc
return true
end
def s := temp string buffer
def font desc : string
if starts with (face, $-)
face (m) = new string (face)
size (m) = size
font desc = face
else
face (m) = new string (face)
size (m) = size
s << "-*-" << face << "-"
s << ((style & fontstyle bold) <> 0 -> "bold", "medium")
s << "-"
s << ((style & fontstyle italic) <> 0 -> $i, $r)
s << "-normal-*-*-" << size << "0-*-*-*-*-iso8859-1"
font desc = as string (s)
end
def id = gdk_font_load (font desc)
release (s)
if is nil (id)
delete (face (m))
return false
end
id (m) = id
height (m) = ascent (id) + descent (id)
ascent (m) = ascent (id)
descent (m) = descent (id)
internal leading (m) = 0
external leading (m) = 0
ave char width (m) = gdk_char_width (id, $n)
max char width (m) = gdk_char_width (id, $m)
return true
end
//[cf]
//[cf]
//[of]:release
//[c]Releases the font
//[c]
public func release (m: font)
delete (face (m))
def id = id (m)
if not nil (id)
gdk_font_unref (id)
end
def desc = desc (m)
if not nil (desc)
pango_font_description_free (desc)
end
end
//[cf]
//[cf]
//[cf]
//[of]:cursor
//[of]:type
//[c]
public struct cursor : local sys cursor
// empty
end
//[cf]
//[of]:constants
//[c]Cursor types
//[c]
public enum cursor type
cursor arrow = GDK_ARROW : int
cursor ibeam = GDK_XTERM : int
cursor wait = GDK_WATCH : int
cursor no = GDK_ICON : int
cursor hand = GDK_HAND2 : 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)
id (m) = gdk_cursor_new (type : GdkCursorType)
end
//[cf]
//[of]:release
//[c]
public func release (m: cursor)
gdk_cursor_unref (id (m))
end
//[cf]
//[cf]
//[cf]
//[of]:color
//[of]:type
//[c]
//[c]The encoding of the color depends on the platform
//[c]
public typedef color = dword
//[cf]
//[of]:constants
//[c]
//[c]System Colors
//[c]
public enum system color
color scrollbar
color background
color active caption
color caption text
color inactive caption
color inactive caption text
color menu
color menu text
color window
color window text
color window frame
color active border
color inactive border
color app workspace
color highlight
color highlight text
color gray text
color button face
color button shadow
color button text
color button highlight
color 3d dark shadow
color 3d light
color info text
color info background
end
//[cf]
//[c]
//[of]:color from RGB (r, g, b)
//[c]
public equ color from RGB (r: byte, g: byte, b: byte) =
( (r:dword<<16) | (g:dword<<8) | b:dword ) : color
//[cf]
//[of]:red (c)
//[c]
public equ red (c: color) = (c >> 16) : 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 : byte
//[cf]
//[c]
//[of]:color from dword (dword)
//[c]
public equ color from dword (d: dword) = d : color
//[cf]
//[of]:dword from color (color)
//[c]
public equ dword from color (c: color) = c : dword
//[cf]
//[c]
//[of]:system color (index)
//[c]Get a color from system presets
//[c]
public func system color (index: system color)
def style = gtk_widget_get_default_style
def g : GdkColor
switch index
//case color scrollbar
//case color background
//case color active caption
//case color caption text
//case color inactive caption
//case color inactive caption text
//case color menu
//case color menu text
case color window
g = gtk_style_get_base (style, GTK_STATE_NORMAL:int)
case color window text
g = gtk_style_get_text (style, GTK_STATE_NORMAL:int)
//case color window frame
//case color active border
//case color inactive border
//case color app workspace
case color highlight
g = gtk_style_get_bg (style, GTK_STATE_SELECTED:int)
case color highlight text
g = gtk_style_get_fg (style, GTK_STATE_SELECTED:int)
case color gray text
g = gtk_style_get_fg (style, GTK_STATE_INSENSITIVE:int)
case color button face
g = gtk_style_get_bg (style, GTK_STATE_NORMAL:int)
case color button shadow
g = gtk_style_get_dark (style, GTK_STATE_NORMAL:int)
case color button text
g = gtk_style_get_text (style, GTK_STATE_NORMAL:int)
case color button highlight
g = gtk_style_get_light (style, GTK_STATE_NORMAL:int)
case color 3d dark shadow
g = gtk_style_get_dark (style, GTK_STATE_NORMAL:int)
case color 3d light
g = gtk_style_get_light (style, GTK_STATE_NORMAL:int)
//case color info text
//case color info background
else
g = gtk_style_get_bg (style, GTK_STATE_NORMAL: int)
end
return color (g)
end
//[cf]
//[c]
//[c]private:
//[of]:GdkColor (color)
private func GdkColor (color: color, return c: GdkColor)
pixel (c) = 0
red (c) = red (color) : guint16 << 8
green (c) = green (color) : guint16 << 8
blue (c) = blue (color) : guint16 << 8
end
//[cf]
//[of]:color (GdkColor)
private func color (c: GdkColor)
return (
((red (c) & 0xFF00:w ) : dword << 8) |
((green (c) & 0xFF00:w ) : dword) |
((blue (c) & 0xFF00:w ) : dword >> 8) )
end
//[cf]
//[c]
//[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 + -