File: sdpgtkclipboard.cpp

package info (click to toggle)
k3d 0.4.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 51,716 kB
  • ctags: 81,610
  • sloc: cpp: 283,698; ansic: 64,095; xml: 61,533; sh: 9,026; makefile: 5,282; python: 431; perl: 308; awk: 130
file content (153 lines) | stat: -rw-r--r-- 3,848 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
// SDPGTK Library
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// 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
// 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

/** \file
		\brief Implements the sdpGtkClipboard class, which makes it easy to synchronously retrieve data from the clipboard (primary X selection)
		\author Timothy M. Shead (tshead@k-3d.com)
*/

#include "sdpgtkclipboard.h"
#include "sdpgtkevents.h"
#include "sdpgtkutility.h"

#ifdef	SDPWIN32
#include <windows.h>
#endif

#include <cassert>

namespace sdpgtk
{

sdpxml::Document& clipboard_template()
{
	static sdpxml::Document gtkml("empty");
	if(gtkml.Name() == "empty")
		{
			std::istringstream uitemplate(
				"<gtkml>"
					"<window type=\"toplevel\" show=\"false\">"
						"<event signal=\"selection-received\" name=\"selectionreceived\"/>"
					"</window>"
				"</gtkml>\n");
			assert(gtkml.Load(uitemplate, "sdpGtkClipboard builtin template"));
		}
	
	return gtkml;
}

} // namespace sdpgtk

/////////////////////////////////////////////////////////////////////////////////////////////////
// sdpGtkClipboard

sdpGtkClipboard::sdpGtkClipboard() :
	sdpGtkClipboardBase(),
	m_Waiting(false),
	m_ContainsData(false)
{
	// Create and load our UI template ...
	g_return_if_fail(Load(sdpgtk::clipboard_template()));
}

sdpGtkClipboard::~sdpGtkClipboard()
{
	RootWidget().Destroy();
}

bool sdpGtkClipboard::StartSelection()
{
	// Get ready to loop, waiting for results ...
	m_Waiting = true;
	// Don't assume success until we're sure ...
	m_ContainsData = false;

	// Start the selection process ...
	g_return_val_if_fail(RequestSelection(), false);

	// Enter our loop, waiting for results ...
	while(m_Waiting)
		sdpGtkHandlePendingEvents();

	return m_ContainsData;
}

void sdpGtkClipboard::OnEvent(sdpGtkEvent* Event)
{
	// Sanity checks ...
	g_assert(Event);

	if(Event->Name() == "selectionreceived")
		{
			m_Waiting = false;
			m_ContainsData = ReceiveSelection(Event);
		}
	else
		sdpGtkClipboardBase::OnEvent(Event);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// sdpGtkClipboardText

const sdpString& sdpGtkClipboardText::GetText()
{
	StartSelection();
	return m_Text;
}

bool sdpGtkClipboardText::RequestSelection()
{
	// Cleanup leftovers ...
	m_Text.clear();

#ifdef SDPWIN32

	g_return_val_if_fail(OpenClipboard(0), false);
	g_return_val_if_fail(GetClipboardData(CF_TEXT), false);

	m_Text = static_cast<char*>(GetClipboardData(CF_TEXT));

	g_return_val_if_fail(CloseClipboard(), false);

	m_Waiting = false;
	m_ContainsData = true;

#else // SDPWIN32

	// Request the primary X selection as text ...
	g_return_val_if_fail(gtk_selection_convert(RootWidget(), gdk_atom_intern("CLIPBOARD", false), gdk_atom_intern("STRING", false), GDK_CURRENT_TIME), false);

#endif // !SDPWIN32

	return true;
}

bool sdpGtkClipboardText::ReceiveSelection(sdpGtkEvent* Event)
{
	// Get the selection as text ...
	sdpGtkEventWidgetSelectionReceived* event = static_cast<sdpGtkEventWidgetSelectionReceived*>(Event);
	GtkSelectionData* selection = event->Selection();
	g_return_val_if_fail(selection->length >= 0, false);

	m_Text = reinterpret_cast<char*>(selection->data);

	return true;
}