File: popup.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 (589 lines) | stat: -rw-r--r-- 15,420 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*  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 "link.h"
#include "render.h"
#include "eb.h"
#include "history.h"
#include "popup.h"
#include "textview.h"
#include "jcode.h"
#include "pixmap.h"

GtkWidget *popup=NULL;
static GtkWidget *popup_scroll=NULL;
static GtkWidget *title_label=NULL;
static GtkWidget *image_pushpin=NULL;
GtkWidget *popup_view=NULL;

static const int title_height = 22;
static gboolean bbutton_down=FALSE;
static gboolean bpushpin_down=FALSE;
static gfloat previous_x;
static gfloat previous_y;
static gint prev_x;
static gint prev_y;

static gint align_x = 10;
static gint align_y = 10;

GList *current_in_result=NULL;
extern GtkWidget *main_view;
extern GtkTextBuffer *text_buffer;
extern GtkTextTagTable *tag_table;

//static CONTENT_AREA *popup_area=NULL;


static void update_result(RESULT *result);

gint close_popup(GtkWidget *widget, gpointer data)
{

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

	if(popup != NULL){
		gtk_text_view_set_buffer(GTK_TEXT_VIEW(popup_view), NULL);
		gtk_widget_destroy(popup_view);
		gtk_widget_destroy(popup);
	
		popup = NULL;
		bpushpin_down = FALSE;
	}

	LOG(LOG_DEBUG, "OUT : close_popup()");
	return(TRUE);
}


static gint popup_button_press_event(GtkWidget *widget, GdkEventButton *event)
{
	RESULT *rp;
	GtkTextIter iter;
	guint offset;
	gint buffer_x, buffer_y;

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

	if(event->type == GDK_BUTTON_PRESS){
		if (event->button == 1){
			gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
							      GTK_TEXT_WINDOW_TEXT,
							      (gint)(event->x),
							      (gint)(event->y),
							      &buffer_x,
							      &buffer_y);
			gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(widget),
							   &iter,
							   buffer_x,
							   buffer_y);

			offset = gtk_text_iter_get_offset(&iter);
	
			if(follow_link(offset) == TRUE){
				LOG(LOG_DEBUG, "OUT : popup_button_press_event() = TRUE");
				return(TRUE);
			} else {
				if(bpushpin_down == FALSE){
					gtk_widget_destroy(popup);
					popup = NULL;
				}
			}
		} else if ((event->button == 2) || (event->button == 3)){
			if(!current_in_result)
				return(TRUE);

			if (event->button == 2) {
				if(g_list_previous(current_in_result) == NULL){
					return(TRUE);
				}
				current_in_result = g_list_previous(current_in_result);
			} else {
				if(g_list_next(current_in_result) == NULL){
					return(TRUE);
				}
				current_in_result = g_list_next(current_in_result);
			}
			if(current_in_result == NULL)
				return(TRUE);
			rp = (RESULT *)(current_in_result->data);
			if(current_in_result){
				show_popup(rp);
			}
		}
	}

	LOG(LOG_DEBUG, "OUT : popup_button_press_event()");
	return(TRUE);
}

static gint title_click_event (GtkWidget *widget, GdkEventButton *event, gpointer data)
{
	RESULT *rp;
	GdkModifierType mask;
	static GdkWindow *root_win = NULL;

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


	if(event->type == GDK_BUTTON_PRESS){
		if ((event->button == 2) || (event->button == 3)){
			return(FALSE);
		}

		if((strcmp(data, "<") == 0) || 
		   (strcmp(data, ">") == 0)){

			if(strcmp(data, "<") == 0){
				if(g_list_previous(current_in_result) == NULL){
					return(FALSE);
				}
				current_in_result = g_list_previous(current_in_result);
			} else {
				if(g_list_next(current_in_result) == NULL){
					return(FALSE);
				}
				current_in_result = g_list_next(current_in_result);
			}

			if(current_in_result == NULL)
				return(0);
			rp = (RESULT *)(current_in_result->data);
			if(current_in_result){
				update_result(rp);
			}

		} else if(strcmp(data, "X") == 0){
			gtk_widget_destroy(popup);
			popup = NULL;
		} else if(strcmp(data, "t") == 0){
			bbutton_down = TRUE;
			root_win = gdk_window_foreign_new (GDK_ROOT_WINDOW ());
			gdk_window_get_pointer (root_win, &prev_x, &prev_y, &mask);
			previous_x = event->x;
			previous_y = event->y;
		} else if(strcmp(data, "p") == 0){
			GdkPixbuf *pixbuf;
			bbutton_down = TRUE;

			if(bpushpin_down == FALSE){
				bpushpin_down = TRUE;

				pixbuf = create_pixbuf(IMAGE_PUSH_ON);
				gtk_image_set_from_pixbuf(GTK_IMAGE(image_pushpin), pixbuf);
				destroy_pixbuf(pixbuf);
			} else {
				bpushpin_down = FALSE;

				pixbuf = create_pixbuf(IMAGE_PUSH_OFF);
				gtk_image_set_from_pixbuf(GTK_IMAGE(image_pushpin), pixbuf);
				destroy_pixbuf(pixbuf);

				gtk_widget_destroy(popup);
				popup = NULL;

			}
		}

	} else if((event->button == 1) && (event->type == GDK_2BUTTON_PRESS)){
		// Double click
	}
	
	LOG(LOG_DEBUG, "OUT : title_click_event()");
	return(TRUE);
}

static gint title_release_event (GtkWidget *widget, GdkEventButton *event, gpointer data)
{
	bbutton_down = FALSE;
	return(FALSE);
}

gint title_motion_event(GtkWidget *widget, GdkEventMotion *event)
{
	gint mov_x, mov_y;
	gint win_x, win_y;

	gint xp, yp;
	GdkModifierType mask;
	static GdkWindow *root_win = NULL;

	//LOG(LOG_DEBUG, "IN : title_motion_event()");

	if((event->state & GDK_BUTTON1_MASK) &&
	   bbutton_down){

		root_win = gdk_window_foreign_new (GDK_ROOT_WINDOW ());
		gdk_window_get_pointer (root_win, &xp, &yp, &mask);

	        mov_x = xp - prev_x;
	        mov_y = yp - prev_y;

 	        gdk_window_get_root_origin(popup->window, &win_x, &win_y);

		gtk_window_move(GTK_WINDOW(popup),
				win_x + mov_x,
				win_y + mov_y);
		prev_x = xp;
		prev_y = yp;
	}

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

static void move_popup_window() {
	GdkModifierType mask;
	gint pos_x, pos_y;
	gint pointer_x, pointer_y;
	gint root_x, root_y;
	gint window_width, window_height;
	GdkWindow *root_win = NULL;

#ifdef __WIN32__
	root_x = GetSystemMetrics(SM_CXSCREEN);
	root_y = GetSystemMetrics(SM_CYSCREEN);
#else
	root_win = gdk_window_foreign_new (GDK_ROOT_WINDOW ());
	gdk_window_get_size(root_win, &root_x, &root_y);
#endif

	window_width = popup_width;
	window_height = popup_height;

	gdk_window_get_pointer(root_win, &pointer_x, &pointer_y, &mask);
	pos_x = pointer_x + align_x;
	pos_y = pointer_y + align_y;

	if(pos_x + window_width > root_x){
		pos_x = root_x - window_width;
	}
	
	if(bshow_popup_title) {
		if(pos_y + window_height + title_height > root_y){
			pos_y = root_y - window_height - title_height;
		}
	} else {
		if(pos_y + window_height > root_y){
			pos_y = root_y - window_height;
		}
	}

	gtk_window_move(GTK_WINDOW(popup), pos_x, pos_y);
}

static void create_popup_window(){

	gint window_width, window_height;

	GtkWidget *vbox;
	GtkWidget *hbox;
	GtkWidget *eventbox;
	GtkWidget *image;
	GtkWidget *frame;
	GtkWidget *separator;

#ifdef __WIN32__
	HWND hWnd;
	long nStyle;
#endif

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

	// If there already is an window, use that position.
	// Move if the window is out of the screen.
//	gdk_window_get_position(popup->window, &pos_x, &pos_y);
//	gtk_widget_destroy(popup);
//	redraw = TRUE;

	window_width = popup_width;
	window_height = popup_height;
/*
	popup = gtk_widget_new (GTK_TYPE_WINDOW,
				"type", GTK_WINDOW_TOPLEVEL,
				 "allow-shrink", TRUE,
				 "allow-grow", TRUE,
				 "default-width", window_width,
				 "default-height", window_height,
				NULL);
*/
	popup = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_resizable(GTK_WINDOW(popup), TRUE);
	gtk_window_set_default_size(GTK_WINDOW(popup), window_width, window_height);
	gtk_window_set_accept_focus(GTK_WINDOW(popup), FALSE);
	move_popup_window();
	gtk_window_set_wmclass(GTK_WINDOW(popup), "Popup", "EBView");
	g_signal_connect(G_OBJECT(popup), "delete_event",
			 G_CALLBACK(close_popup), NULL);


	vbox = gtk_vbox_new(FALSE, 0);
	gtk_container_add (GTK_CONTAINER (popup), vbox);


	if(bshow_popup_title){

		frame = gtk_frame_new(NULL);
		gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_IN);
		gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);

		hbox = gtk_hbox_new(FALSE, 0);
		gtk_container_add( GTK_CONTAINER(frame), hbox);


		eventbox = gtk_event_box_new();
		gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, FALSE, 2);
		g_signal_connect(G_OBJECT(eventbox),"button_press_event",
				 G_CALLBACK(title_click_event), (gpointer)"p");
		image_pushpin = create_image(IMAGE_PUSH_OFF);
		gtk_container_add( GTK_CONTAINER(eventbox), image_pushpin);

		separator = gtk_vseparator_new();
		gtk_box_pack_start(GTK_BOX(hbox), separator, FALSE, FALSE, 0);


		eventbox = gtk_event_box_new();
		gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, FALSE, 2);
		g_signal_connect(G_OBJECT(eventbox),"button_press_event",
				 G_CALLBACK(title_click_event), (gpointer)"<");
		image = create_image(IMAGE_SMALL_LEFT);
		gtk_container_add( GTK_CONTAINER(eventbox), image);

		separator = gtk_vseparator_new();
		gtk_box_pack_start(GTK_BOX(hbox), separator, FALSE, FALSE, 0);

		eventbox = gtk_event_box_new();
		gtk_box_pack_start(GTK_BOX(hbox), eventbox, TRUE, TRUE, 2);
		g_signal_connect(G_OBJECT(eventbox),"button_press_event",
				 G_CALLBACK(title_click_event), (gpointer)"t");
		g_signal_connect(G_OBJECT(eventbox),"button_release_event",
				 G_CALLBACK(title_release_event), (gpointer)NULL);
		g_signal_connect(G_OBJECT(eventbox),"motion_notify_event",
				 G_CALLBACK(title_motion_event), (gpointer)NULL);

		title_label = gtk_label_new("x of x");
		gtk_container_add( GTK_CONTAINER(eventbox), title_label);

		separator = gtk_vseparator_new();
		gtk_box_pack_start(GTK_BOX(hbox), separator, FALSE, FALSE, 0);

		eventbox = gtk_event_box_new();
		gtk_box_pack_end(GTK_BOX(hbox), eventbox, FALSE, FALSE, 2);
		g_signal_connect(G_OBJECT(eventbox),"button_press_event",
				 G_CALLBACK(title_click_event), (gpointer)"X");

		image = create_image(IMAGE_SMALL_CLOSE);
		gtk_container_add( GTK_CONTAINER(eventbox), image);

		separator = gtk_vseparator_new();
		gtk_box_pack_end(GTK_BOX(hbox), separator, FALSE, FALSE, 0);

		eventbox = gtk_event_box_new();
		gtk_box_pack_end(GTK_BOX(hbox), eventbox, FALSE, FALSE, 2);
		g_signal_connect(G_OBJECT(eventbox),"button_press_event",
				 G_CALLBACK(title_click_event), (gpointer)">");

		image = create_image(IMAGE_SMALL_RIGHT);
		gtk_container_add( GTK_CONTAINER(eventbox), image);

	}

	frame = gtk_frame_new(NULL);
	gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_IN);
	gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);

	popup_scroll = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (popup_scroll),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);

	gtk_container_add(GTK_CONTAINER(frame), popup_scroll);

	popup_view = gtk_text_view_new_with_buffer(text_buffer);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(popup_view), FALSE);
	gtk_text_view_set_left_margin(GTK_TEXT_VIEW(popup_view), 5);
	gtk_text_view_set_right_margin(GTK_TEXT_VIEW(popup_view), 5);
	gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(popup_view), 3);
	gtk_text_view_set_pixels_inside_wrap(GTK_TEXT_VIEW(popup_view), 3);
	gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(popup_view), FALSE);
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(popup_view), GTK_WRAP_WORD);


	g_signal_connect(G_OBJECT(popup_view),"motion_notify_event",
			 G_CALLBACK(motion_notify_event), (gpointer)NULL);
	
	g_signal_connect(G_OBJECT(popup_view),"button_press_event",
			 G_CALLBACK(popup_button_press_event), (gpointer)NULL);

	gtk_container_add (GTK_CONTAINER (popup_scroll), popup_view);

#ifndef __WIN32__
	gtk_widget_realize(popup);
	gdk_window_set_decorations(popup->window, 0);
#endif

	gtk_widget_show_all(popup);

#ifdef __WIN32__
	hWnd = GDK_WINDOW_HWND(popup->window);
	nStyle = GetWindowLong(hWnd, GWL_STYLE );
	nStyle &= ~WS_CAPTION;
	SetWindowLong(hWnd, GWL_STYLE, nStyle );
	SetWindowPos(hWnd,
		     HWND_TOPMOST,
		     pos_x, pos_y,
		     window_width, window_height,
		     SWP_FRAMECHANGED);
#endif

	bbutton_down = FALSE;
	bpushpin_down = FALSE;

	LOG(LOG_DEBUG, "OUT : create_popup_window()");

}


static gint scroll_to_top()
{
	GtkTextIter iter;
	GtkTextMark *mark;

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

	gtk_text_buffer_get_start_iter (text_buffer, &iter);

	mark =  gtk_text_buffer_create_mark(text_buffer,
					    "start",
					    &iter,
					    TRUE);

	gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(popup_view),
				     mark,
				     0.0,
				     TRUE,
				     0.0, 0.0);

	gtk_text_buffer_delete_mark(text_buffer, mark);


	LOG(LOG_DEBUG, "OUT : scroll_to_top()");
	return(0);
}
void show_result_in_popup()
{
	current_in_result = search_result;
	show_popup(current_in_result->data);
}

static void update_result(RESULT *result)
{
	gchar *text=NULL;
	GtkTextIter iter;
	CANVAS canvas;
	DRAW_TEXT l_text;
	gint length;
	gchar *euc_str;

	g_assert(result->type == RESULT_TYPE_EB);

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

	text = ebook_get_text(result->data.eb.book_info,
			      result->data.eb.pos_text.page, 
			      result->data.eb.pos_text.offset);
	if(text == NULL)
		return;

	// Prevent the window from growing.
	//if(text[strlen(text)-1] == '\n')
	//text[strlen(text)-1] = '\0';

	if(popup == NULL){
		create_popup_window();
	}

	// Program aborts if you enable this line.
	//gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_view), NULL);

	clear_text_buffer();

	gtk_text_buffer_get_start_iter (text_buffer, &iter);

	length = strlen(text);
	if(text[length-1] == '\n'){
		text[length-1] = '\0';
		length --;
	}

	l_text.text = text;
	l_text.length = length;

	canvas.buffer = text_buffer;
	canvas.iter = &iter;
	canvas.indent = 0;

	if(result->word != NULL){
		euc_str = iconv_convert("utf-8", "euc-jp", result->word);
		draw_content(&canvas, &l_text, result->data.eb.book_info, NULL, euc_str);
		g_free(euc_str);
	} else {
		draw_content(&canvas, &l_text, result->data.eb.book_info, NULL, NULL);
	}

	gtk_text_buffer_get_start_iter (text_buffer, &iter);
	gtk_text_buffer_place_cursor(text_buffer, &iter);

	if(bshow_popup_title){
		gchar title[256];
		sprintf(title, "%d of %d", 
			g_list_index(search_result, current_in_result->data) + 1,
			g_list_length(search_result));

		gtk_label_set_text(GTK_LABEL(title_label), title);

	}

	gtk_adjustment_set_value(
		gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(popup_scroll)), 0);

	gtk_adjustment_set_value(
		gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(popup_scroll)), 0);

	g_free(text);
	set_current_result(result);

	gtk_timeout_add(10, scroll_to_top, NULL);

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

void show_popup(RESULT *result)
{
	LOG(LOG_DEBUG, "IN : show_popup()");

	update_result(result);
	if(bpushpin_down == FALSE){
		move_popup_window();
	}
	gdk_window_show(GTK_WIDGET(popup)->window);
	gdk_window_focus(GTK_WIDGET(popup)->window, gtk_get_current_event_time());

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