File: selection.c

package info (click to toggle)
ebview 0.3.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,040 kB
  • sloc: ansic: 35,577; sh: 10,714; xml: 1,771; makefile: 607; yacc: 291
file content (415 lines) | stat: -rw-r--r-- 9,260 bytes parent folder | download | duplicates (6)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*  Copyright (C) 2001-2004  Kenichi Suto
 *
 *  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
 *  (at your option) 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.
*/


#include "defs.h"
#include "global.h"
#include "eb.h"
#include "dialog.h"
#include "grep.h"
#include "selection.h"
#include "headword.h"
#include "mainwindow.h"
#include "misc.h"
#include "popup.h"
#include "history.h"
#include "jcode.h"

static gint tag_timeout=0;
static gchar previous[256];
static gboolean auto_lookup_suspended = FALSE;

extern GList *current_in_result;
extern GtkWidget *hidden_window;

void bring_to_top(GtkWidget *win){
#ifdef __WIN32__
	HWND hWnd;
	int nTargetID, nForegroundID;
	BOOL res;

	hWnd = GDK_WINDOW_HWND (win->window);

	/* From  http://techtips.belution.com/ja/vc/0012/ */
	nForegroundID = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
	nTargetID = GetWindowThreadProcessId(hWnd, NULL );

	AttachThreadInput(nTargetID, nForegroundID, TRUE );

	// SPI_GETFOREGROUNDLOCKTIMEOUT will be undefined. Why ?
	/*
	SystemParametersInfo( SPI_GETFOREGROUNDLOCKTIMEOUT,0,&sp_time,0);
	SystemParametersInfo( SPI_SETFOREGROUNDLOCKTIMEOUT,0,(LPVOID)0,0);
	SetForegroundWindow(hWnd);
	SystemParametersInfo( SPI_SETFOREGROUNDLOCKTIMEOUT,0,sp_time,0);
	*/

	res = SetForegroundWindow(hWnd);

	AttachThreadInput(nTargetID, nForegroundID, FALSE);

	if(!res){
		SetFocus(hWnd);
	}
#else
	gdk_window_show(GTK_WIDGET(win)->window);
	gdk_window_focus(GTK_WIDGET(win)->window, gtk_get_current_event_time());

#endif
}

static gboolean validate_euc_str(guchar *str){
	guchar *p;

	p = str;
	while(*p){
		if (iseuc(p)) {
			p +=2;
		} else if(isprint(*p)) {
			p ++;
		} else if(isspace(*p)) {
			*p = 0x20;
			p++;
		} else {
			return(FALSE);
		}
	}
	return(TRUE);
}

static void search_selected(gchar *str)
{
	gchar *euc_str;
	gint method;
	glong len;

	LOG(LOG_DEBUG, "IN : search_selected(%s)", str);

	if(selection_mode <= SELECTION_DO_NOTHING) {
		LOG(LOG_DEBUG, "OUT : search_selected() = NOP1");
		return;
	}

	if(strcmp(previous, str) == 0){
		// Do nothing if the word is the save as before.
		LOG(LOG_DEBUG, "same as before");
		;
	} else {

		euc_str = iconv_convert("utf-8", "euc-jp", str);
		if(validate_euc_str(euc_str) == FALSE) {
			g_free(euc_str);
			LOG(LOG_DEBUG, "OUT : search_selected() = INVALID");
			return;
		}
		remove_space(euc_str);

		len = g_utf8_strlen(str, -1);

		if((auto_minchar <= len) && (len <= auto_maxchar)) {
			gtk_entry_set_text(GTK_ENTRY(word_entry), str);

			method = ebook_search_method();
			if((method == SEARCH_METHOD_INTERNET) ||
			   (method == SEARCH_METHOD_MULTI) ||
			   (method == SEARCH_METHOD_FULL_TEXT)){
				LOG(LOG_DEBUG, "OUT : search_selected() = NOP2");
				return;
			}

			if(selection_mode <= SELECTION_COPY_ONLY) {
				LOG(LOG_DEBUG, "OUT : search_selected() = COPY");
				return;
			}
			
			clear_message();
			clear_search_result();

			if(method == SEARCH_METHOD_GREP){
				grep_search(euc_str);
				show_result_tree();
				select_first_item();
				if(selection_mode == SELECTION_SEARCH_TOP)
					bring_to_top(main_window);
				save_word_history(str);
			} else {
				ebook_search_auto(euc_str, method);
				if(search_result){
					if(selection_mode == SELECTION_POPUP) {
						show_result_in_popup();
					} else {
						show_result_tree();
						select_first_item();
						if(selection_mode == SELECTION_SEARCH_TOP)
							bring_to_top(main_window);
					}
					save_word_history(str);
				} else {
					current_in_result = NULL;
					set_current_result(NULL);
					if(selection_mode == SELECTION_POPUP) {
						beep();
					} else {
						if(selection_mode == SELECTION_SEARCH_TOP)
							bring_to_top(main_window);
						push_message(_("No hit."));
					}
				}
			}
			
			sprintf(previous, "%s", str);
		} else {
			LOG(LOG_DEBUG, "OUT : search_selected() = LENGTH");
		}
		g_free(euc_str);
	}

	LOG(LOG_DEBUG, "OUT : search_selected()");
}

void
selection_received (GtkWidget *widget, GtkSelectionData *data)
{
	gchar *str;
	gchar **list;
	gint count;

	LOG(LOG_DEBUG, "IN : selection_received()");

	if((data == NULL) || (data->data == NULL) || (data->length < 0)){
		LOG(LOG_DEBUG, "no data");
		goto END;
	}

	// No conversion required for STRING type.
	if (data->type == GDK_TARGET_STRING){
		str = g_strndup(data->data, data->length);

	// Convert to UTF-8 for COMPOUND_TEXT type.
	} else if ((data->type == gdk_atom_intern ("COMPOUND_TEXT", FALSE)) ||
		   (data->type == gdk_atom_intern ("TEXT", FALSE))){
		count = gdk_text_property_to_utf8_list (data->type,
							data->format, 
							data->data,
							data->length,
							&list);


		if((count == 0) || (list == NULL)){
			goto END;
		}
		str = g_strdup(list[0]);
		g_strfreev(list);

	} else {
		LOG(LOG_DEBUG, "unknown data type");
		goto END;
	}

	remove_space(str);
	search_selected(str);
	g_free(str);

 END:

	if(selection_mode != SELECTION_DO_NOTHING){
		auto_lookup_start();
	}

	LOG(LOG_DEBUG, "OUT : selection_received()");
	return;
}


gint copy_clipboard_win(gpointer data){
	gchar *str=NULL;
	GtkClipboard* clipboard;

	LOG(LOG_DEBUG, "IN : copy_clipboard()");

#ifdef __WIN32__
	clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
#else
	clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
#endif

	str = gtk_clipboard_wait_for_text(clipboard);
	if(str == NULL){
		goto END;
	}

	remove_space(str);
	search_selected(str);
	g_free(str);

 END:
	if(selection_mode != SELECTION_DO_NOTHING){
		auto_lookup_start();
	}

	LOG(LOG_DEBUG, "OUT : copy_clipboard()");
	return (FALSE);
}

gint copy_clipboard_x(gpointer data){
	static GdkAtom ctext_atom = GDK_NONE;

	LOG(LOG_DEBUG, "IN : copy_clipboard()");

	/*
	xwindow = XGetSelectionOwner (gdk_display_get_default(), GDK_SELECTION_PRIMARY);
	if (xwindow == None){
		return(TRUE);
	}
	*/

	gtk_entry_set_text(GTK_ENTRY(hidden_entry), "");

	// Ask for COMPOUND_TEXT
	if (ctext_atom == GDK_NONE){
		ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
	}

#ifdef __WIN32__
	gtk_selection_convert (hidden_entry,
			       GDK_SELECTION_CLIPBOARD,
			       ctext_atom, 
			       GDK_CURRENT_TIME);
#else
	gtk_selection_convert (hidden_entry,
			       GDK_SELECTION_PRIMARY,
			       ctext_atom, 
			       GDK_CURRENT_TIME);
#endif
	
	LOG(LOG_DEBUG, "OUT : copy_clipboard()");
	return (FALSE);
}


#ifdef __WIN32__
static gboolean registered=FALSE;
static WNDPROC OrgWndProc = NULL;
static HWND next_hwnd = NULL;
static HWND hidden_hwnd;

LRESULT CALLBACK
HiddenWndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    LRESULT retval;
    HANDLE hText;
    char *pText;
    gchar *str;


    switch (msg) {
    case WM_DESTROY:
	    ChangeClipboardChain(hwnd , next_hwnd);
	    PostQuitMessage(0);
	    break;
    case WM_DRAWCLIPBOARD:
	    OpenClipboard(hwnd);
	    hText = GetClipboardData(CF_TEXT);

	    if(hText != NULL) {
		    pText = GlobalLock(hText);
		    GlobalUnlock(hText);
	    }
	    CloseClipboard();

	    //remove_space(pText);
	    str = iconv_convert(fs_codeset, "utf-8", pText);
	    search_selected(str);
	    g_free(str);

	    if(next_hwnd != NULL)
		    SendMessage(next_hwnd, msg, wParam, lParam);
	    break;
    case WM_CHANGECBCHAIN:
	    if((HWND)wParam == next_hwnd)
		    next_hwnd = (HWND)lParam;
	    break;
    default:
	    break;
    }

    retval = OrgWndProc(hwnd, msg, wParam, lParam);
    return retval;
}

#endif

void auto_lookup_start()
{
	
	if(selection_mode == SELECTION_DO_NOTHING)
		return;
	
#ifdef __WIN32__
	if(registered == FALSE) {
		if(OrgWndProc == NULL){
			hidden_hwnd = GDK_WINDOW_HWND (hidden_window->window);
			OrgWndProc = (WNDPROC)GetWindowLong(hidden_hwnd, GWL_WNDPROC);
			SetWindowLong(hidden_hwnd, GWL_WNDPROC, (LONG)HiddenWndProc);
		}
		next_hwnd = SetClipboardViewer(hidden_hwnd);
	}
	registered = TRUE;
#else

	if(tag_timeout != 0)
		gtk_timeout_remove(tag_timeout);
	tag_timeout = gtk_timeout_add(auto_interval, copy_clipboard_x, NULL);

#endif
	auto_lookup_suspended = FALSE;
}

void auto_lookup_stop()
{

#ifdef __WIN32__
	if(registered == TRUE) {
		hidden_hwnd = GDK_WINDOW_HWND (hidden_window->window);
		ChangeClipboardChain(hidden_hwnd, next_hwnd);
		next_hwnd = NULL;
		registered = FALSE;
	}
#else
	if(tag_timeout != 0)
		gtk_timeout_remove(tag_timeout);
	tag_timeout = 0;
#endif

	auto_lookup_suspended = FALSE;
}

void auto_lookup_suspend()
{
	if((selection_mode != SELECTION_DO_NOTHING) && (auto_lookup_suspended == FALSE)) {
		auto_lookup_stop();
		auto_lookup_suspended = TRUE;
	}
}

void auto_lookup_resume()
{
	if((selection_mode != SELECTION_DO_NOTHING) && (auto_lookup_suspended == TRUE)) {
		auto_lookup_start();
		auto_lookup_suspended = FALSE;
	}
}