⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clipboard.zc

📁 实现树形结构
💻 ZC
字号:
//[c]Clipboard Objects
//[c]
//[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
//[c]
import "base/types"
import "base/memory"
import "base/memory-allocator"
import "base/memory-buffer"
import "text/string"
import "text/string-buffer"
import "collection/vector"

import "glib/glib"
import "glib/glib-object"
import "gdk/gdk"
import "gtk/gtk"
//[c]
//[cf]
//[of]:constants
//[c]
public def cf text : GdkAtom
//[cf]
//[of]:structures
//[c]
public typedef clipboard format = GdkAtom
//[cf]
//[c]
//[of]:initialize
//[of]:initialize clipboard
//[c]
public func initialize clipboard

	cf text = gdk_atom_intern ("STRING", FALSE)

	content id = 0
	initialize (clipboard formats, 16)
	initialize (clipboard contents, 16)

end
//[cf]
//[of]:release clipboard
public func release clipboard

	remove all clipboard contents
	release (clipboard contents)
	release (clipboard formats)

end
//[cf]
//[c]
//[of]:register clipboard format (name)
//[c]Register a private clipboard format
//[c]
public func register clipboard format (name: string)

	return gdk_atom_intern (name, FALSE)

end
//[cf]
//[cf]
//[of]:enumerating
//[of]:each clipboard format
//[c]Enumerates all formats available in the clipboard
//[c]This method is fully synchronous, the main loop is
//[c]not reentered.
//[c]
public equ each clipboard format

	each (clipboard formats) ? t
		yield (t : clipboard format)
	end
	
end
//[cf]
//[cf]
//[of]:accessing
//[of]:get clipboard data (clipboard format)
//[c]Puts the content of the clipboard into the memory buffer
//[c]
public func get clipboard data (f: clipboard format, return b: memory buffer)

	initialize (b)
	def clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD)

	if f == cf text

		/*def text = gtk_clipboard_wait_for_text (clipboard)
		if not nil (text)
			append (b, text : [] byte, size (text:string)+1)
			g_free (text)
		end*/
		
		def data = gtk_clipboard_wait_for_contents (clipboard, f)
		if not nil (data)
			append (b, data (data), length (data))
			append (b, 0:byte)
			gtk_selection_data_free (data)
		end

	else

		def data = gtk_clipboard_wait_for_contents (clipboard, f)
		if not nil (data)
			append (b, data (data), length (data))
			gtk_selection_data_free (data)
		end

	end

end
//[cf]
//[of]:set clipboard data (clipboard format, base, size)
//[c]
public func set clipboard data (m: clipboard format, buf: [] byte, size: size)

	add (clipboard formats, m)
	add (clipboard contents, new clipboard data (m, buf, size))

end
//[cf]
//[of]:set clipboard content
//[c]Sets the clipboard content
//[c]
//[c]	The clipboard is emptied and a parameter blocks must set
//[c]	its new content.
//[c]	
//[c]Example of use:
//[c]	
//[c]	set clipboard content
//[c]		set clipboard data (format1, base1, size1)
//[c]		set clipboard data (format2, base2, size2)
//[c]	end
//[c]
public equ set clipboard content

	def id = ++ content id

	// reset clipboard
	remove all (clipboard formats)
	remove all clipboard contents

	// call user block to fill in the clipboard
	yield

	// pass info to gtk
	def clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD)
	def n = size (clipboard contents)
	def entries = allocate memory (sizeof local GtkTargetEntry * n) : [] local GtkTargetEntry
	def i = 0
	each (clipboard contents) ? e
		equ cd = e : clipboard data
		entries[i].target = gdk_atom_name (format (cd))
		entries[i].flags = 0
		entries[i].info = i
		++i
	end

	def res = gtk_clipboard_set_with_data (
		clipboard,
		entries,
		n,
		^get (GtkClipboard, GtkSelectionData, guint, gpointer),
		^clear (GtkClipboard, gpointer),
		id : gpointer)

	free memory (entries)

end
//[cf]
//[cf]
//[c]
//[of]:restricted
//[c]
public def clipboard need update = true
//[c]
public func update targets

	remove all (clipboard formats)

	def clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD)

	// Retrieves the list of targets
	def data = 
		gtk_clipboard_wait_for_contents (
			clipboard, 
			gdk_atom_intern ("TARGETS", FALSE))

	if not nil (data)
	
		// Retrieve the data
		def targets : [1] [] GdkAtom
		def n : [1] gint
		if gtk_selection_data_get_targets (data, targets, n) == TRUE

			def first text = true
			def i = 0
			while i < n[]

				def fmt = 0
				def target = targets[][i]
				
				if (
					target == gdk_atom_intern ("STRING", FALSE) ||
					target == gdk_atom_intern ("TEXT", FALSE) ||
					target == gdk_atom_intern ("COMPOUND_TEXT", FALSE) ||
					target == gdk_atom_intern ("UTF8_STRING", FALSE))

					if first text
						add (clipboard formats, cf text)
						first text = false
					end
						
				else
					add (clipboard formats, target)
				end

				++i
			end
			
			g_free (targets[])
		end

		gtk_selection_data_free (data)
	end

end
//[cf]
//[of]:private
//[c]
private struct clipboard data
	format : clipboard format
	data : [] byte
	size : size
end

private func new clipboard data (
		format: clipboard format,
		data: [] byte, 
		size: size)

	def m = allocate memory (sizeof local clipboard data) : clipboard data
	def dst = allocate memory (size)
	copy (dst, data, size)
	
	format (m) = format
	data (m) = dst
	size (m) = size

	return m
end

private func delete (m: clipboard data)
	free memory (data (m))
	free memory (m)
end

private def clipboard formats : local vector
private def clipboard contents : local vector
private def content id : int

private func remove all clipboard contents
	each (clipboard contents) ? cd
		delete (cd:clipboard data)
	end
	remove all (clipboard contents)
end

private func get (
		clipboard: GtkClipboard, 
		data: GtkSelectionData, 
		info: guint, 
		user data: gpointer)

	// check info is not out of bounds
	if info >= size (clipboard contents)
		return
	end

	def cd = clipboard contents [info] : clipboard data
	gtk_selection_data_set(
		data,
		format (cd),
		8,
		data (cd),
		size (cd):gint)

end

private func clear (
		clipboard: GtkClipboard, 
		user data: gpointer)
		
	if user data : int == content id
		remove all clipboard contents
	end
	
end
//[cf]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -