File: test-widget.cc

package info (click to toggle)
inti-sourceview 0.6.0-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,220 kB
  • ctags: 967
  • sloc: sh: 64,518; cpp: 3,137; makefile: 142
file content (602 lines) | stat: -rw-r--r-- 15,155 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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
#include "test-widget.h"
#include <inti/gdk-pixbuf/pixbuf.h>
#include <inti/glib/error.h>
#include <inti/glib/iochannel.h>
#include <inti/gtk/accelgroup.h>
#include <inti/gtk/fileselection.h>
#include <inti/gtk/menu.h>
#include <inti/gtk/menubar.h>
#include <inti/gtk/checkmenuitem.h>
#include <inti/gtk/radiomenuitem.h>
#include <inti/gtk/messagedialog.h>
#include <inti/gtk/scrolledwindow.h>
#include <inti/pango/font.h>
#include <inti/bind.h>
#include <libgnomevfs/gnome-vfs-init.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomeprintui/gnome-print-job-preview.h>
#include <algorithm>

const int READ_BUFFER_SIZE = 4096;
const char *MARKER_TYPE_1 = "one";
const char *MARKER_TYPE_2 = "two";

/*  SourceView
 */

SourceView::SourceView(Gtk::SourceBuffer& buffer)
: Gtk::SourceView(buffer)
{
	// set font
	Pointer<Pango::FontDescription> font_desc = new Pango::FontDescription("monospace 10");
	if (font_desc)
		modify_font(font_desc);

	// add marker pixbufs
	Gdk::Pixbuf *pixbuf = new Gdk::Pixbuf("/usr/share/pixmaps/apple-green.png");
	set_marker_pixbuf(MARKER_TYPE_1, pixbuf);
	pixbuf = new Gdk::Pixbuf("/usr/share/pixmaps/apple-red.png");
	set_marker_pixbuf(MARKER_TYPE_2, pixbuf);
	pixbuf->unref();
}

SourceView::~SourceView()
{
}

bool
SourceView::on_button_press_event(const Gdk::EventButton& event)
{
	// check that the click was on the left gutter
	if (get_show_line_markers() && event.window() == get_window(Gtk::TEXT_WINDOW_LEFT))
	{
		int y_buf;
		const char *marker_type;

		if (event.button() == 1)
			marker_type = MARKER_TYPE_1;
		else
			marker_type = MARKER_TYPE_2;

		window_to_buffer_coords(Gtk::TEXT_WINDOW_LEFT, (int)event.x(), (int)event.y(), 0, &y_buf);
		Gtk::TextIter line_start = get_line_at_y(y_buf);
		Gtk::TextIter line_end = line_start;
		line_end.forward_to_line_end();

		// get the markers already in the line
		Gtk::SourceBuffer *buffer = get_source_buffer();
		std::vector<Gtk::SourceMarker*> markers = buffer->get_markers(line_start, line_end);

		// search for the marker corresponding to the button pressed
		Gtk::SourceMarker *marker = 0;
		int count = markers.size();

		for (int i = 0; i < count; i++)
		{
			String tmp_type = markers[i]->get_marker_type();
			if (!tmp_type.compare(marker_type))
				marker = markers[i];
		}

		if (marker)
		{
			// a marker was found, so delete it
			buffer->delete_marker(*marker);
		}
		else
		{
			// no marker found -> create one
			marker = buffer->create_marker(0, marker_type, line_start);
		}
	}
	return Gtk::SourceView::on_button_press_event(event);
}

/*  MainWindow
 */

MainWindow::MainWindow()
{
	set_title("Inti SourceView Demo");
	set_default_size(500, 500);

	// vbox
	Gtk::VBox *vbox = new Gtk::VBox;
	add(*vbox);
	vbox->show();

	// scrolled window
	Gtk::ScrolledWindow *sw = new Gtk::ScrolledWindow;
	sw->set_shadow_type(Gtk::SHADOW_IN);
	vbox->pack_end(*sw);
	sw->show();

	// source view
	manager = new Gtk::SourceLanguagesManager;
	buffer = new Gtk::SourceBuffer;
	source_view = new SourceView(*buffer);
	sw->add(*source_view);
	source_view->set_show_line_markers(true);
	source_view->set_show_line_numbers(true);
	source_view->set_show_margin(true);
	source_view->set_auto_indent(true);
	source_view->set_tabs_width(8);
	views_list.push_back(source_view);
	source_view->show();

	// item factory
	Pointer<Gtk::AccelGroup> accel_group = new Gtk::AccelGroup;
	item_factory = new Gtk::ItemFactory(GTK_TYPE_MENU_BAR, "<main>", accel_group);
	add_accel_group(accel_group);
	item_factory->create_items(*this);

	// menubar
	Gtk::MenuBar *menu = item_factory->menu_bar();
	vbox->pack_start(*menu, false, false);
	menu->show();

	// preselect menu checkitems
	static_cast<Gtk::CheckMenuItem*>(item_factory->get_item("/View/Show Line Numbers"))->set_active(true);
	static_cast<Gtk::CheckMenuItem*>(item_factory->get_item("/View/Show Markers"))->set_active(true);
	static_cast<Gtk::CheckMenuItem*>(item_factory->get_item("/View/Show Margin"))->set_active(true);
	static_cast<Gtk::CheckMenuItem*>(item_factory->get_item("/View/Enable Auto Indent"))->set_active(true);
	static_cast<Gtk::RadioMenuItem*>(item_factory->get_item("/View/Tabs Width/8"))->set_active(true);

	// cursor position label
	pos_label = new Gtk::Label("label");
	vbox->pack_start(*pos_label, false, false);
	buffer->sig_mark_set().connect(slot(this, &MainWindow::on_move_cursor));
	buffer->sig_changed().connect(slot(this, &MainWindow::on_update_cursor_position));
	pos_label->show();
}

MainWindow::~MainWindow()
{
}

bool
MainWindow::on_view_delete_event(GdkEventAny *event, SourceView *view)
{
	std::vector<Gtk::SourceView*>::iterator i = std::find(views_list.begin(), views_list.end(), view);
	if (i != views_list.end())
	{
		views_list.erase(i);

		// Return false to destroy the window.
		return false;
	}
	return true;
}

void
MainWindow::on_move_cursor(const GtkTextIter *cursoriter, GtkTextMark *mark)
{
	if (mark != gtk_text_buffer_get_insert(*buffer))
		return;

	on_update_cursor_position();
}

void
MainWindow::on_update_cursor_position()
{
	Gtk::TextIter iter = buffer->get_iter_at_mark(*buffer->get_insert());
	int chars = iter.get_offset();
	int row = iter.get_line() + 1;

	Gtk::TextIter start = iter;
	start.set_line_offset(0);
	int col = 0;

	G::Unichar tab_char('\t');
	while (!iter.equal(start))
	{
		if (start.get_char() == tab_char)
		{
			int tab_stop = source_view->get_tabs_width();
			col += (tab_stop - (col % tab_stop));
		}
		else
			++col;

		start.forward_char();
	}

	String msg;
	msg.format("char: %d, line: %d, column: %d", chars, row, col);
	pos_label->set_text(msg);
}

namespace { // helper functions

void remove_all_markers(Gtk::SourceBuffer& buffer)
{
	Gtk::TextIter begin, end;
	buffer.get_bounds(begin, end);
	std::vector<Gtk::SourceMarker*> markers = buffer.get_markers(begin, end);

	int count = markers.size();
	for (int i = 0; i < count; i++)
	{
		buffer.delete_marker(*markers[i]);
	}
}

void error_dialog(const String& message)
{
	Gtk::MessageDialog dialog(Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);
	dialog.set_message(message);
	dialog.run();
	dialog.dispose();
}

bool buffer_load_with_encoding(Gtk::SourceBuffer& source_buffer, const String& filename, const char *encoding, G::Error *error)
{
	g_return_val_if_fail (!filename.empty(), false);

	Pointer<G::IOChannel> io = G::IOChannel::create(filename, "r", error);

	String msg;
	if (error->get())
	{
		msg.format("%s\nFile %s", error->message(), filename.c_str());
		error_dialog(msg);
		return false;
	}

	if (io->set_encoding(encoding, error) != G::IO_STATUS_NORMAL)
	{
		msg.format("Failed to set encoding:\n%s\n%s", filename.c_str(), error->message());
		error_dialog(msg);
		return false;
	}

	source_buffer.begin_not_undoable_action();
	source_buffer.set_text("");
	char *buffer = new char[READ_BUFFER_SIZE];
	bool reading = true;
	Gtk::TextIter iter;
	while (reading)
	{
		size_t bytes_read;
		G::IOStatus status = io->read(buffer, READ_BUFFER_SIZE, &bytes_read, error);
		switch (status)
		{
			case G::IO_STATUS_EOF:
				reading = false;
				// fall through

			case G::IO_STATUS_NORMAL:
				if (bytes_read == 0)
				{
					continue;
				}

				iter = source_buffer.get_end_iter();
				source_buffer.insert(iter, buffer, bytes_read);
				break;

			case G::IO_STATUS_AGAIN:
				continue;

			case G::IO_STATUS_ERROR:
			default:
				msg.format("%s\nFile %s", filename.c_str(), error->message());
				error_dialog(msg);

				// because of error in input we clear already loaded text
				source_buffer.set_text("");
				reading = false;
				break;
		}
	}

	delete buffer;
	source_buffer.end_not_undoable_action();

	if (error->get())
		return false;

	source_buffer.set_modified(false);

	// move cursor to the beginning
	iter = source_buffer.get_start_iter();
	source_buffer.place_cursor(iter);
	return true;
}

} // namespace

bool
MainWindow::open_file(const String& filename)
{
	String uri;
	if (g_path_is_absolute(filename.c_str()))
	{
		uri = gnome_vfs_get_uri_from_local_path(filename.c_str());
	}
	else
	{
		String path(g_get_current_dir());
		path += "/";
		path += filename;
		uri = gnome_vfs_get_uri_from_local_path(path.c_str());
	}

	String mime_type = gnome_vfs_get_mime_type(uri.c_str());
	if (!mime_type.null())
	{
		Gtk::SourceLanguage *language = manager->get_language_from_mime_type(mime_type);

		if (!language)
		{
			g_print("No language found for mime type `%s'\n", mime_type.c_str());
			buffer->set_highlight(false);
		}
		else
		{
			buffer->set_highlight(true);
			buffer->set_language(language);
		}
	}
	else
	{
		buffer->set_highlight(false);
		g_warning ("Couldn't get mime type for file `%s'", filename.c_str());
	}

	remove_all_markers(*buffer);
	G::Error error;
	buffer_load_with_encoding(*buffer, filename, "utf-8", &error);
	current_file = filename;

	return error.get() ? false : true;
}

void
MainWindow::print_job_begin_page(Gtk::SourcePrintJob *job)
{
	g_print("Printing %.2f%%    \r", 100.0 * job->get_page() / job->get_page_count());
}

void MainWindow::print_job_finished(Gtk::SourcePrintJob *job)
{
	g_print ("\n");
	GnomePrintJob *gjob = job->get_print_job();
	GtkWidget *preview = gnome_print_job_preview_new(gjob, (unsigned char*)"test-widget print preview");
 	g_object_unref (gjob);
 	job->unref();

	gtk_widget_show(preview);
}

void
MainWindow::on_file_open()
{
	Gtk::FileSelection file_sel("Open file...");
	if (file_sel.run() == Gtk::RESPONSE_OK)
	{
		open_file(file_sel.get_filename());
	}
	file_sel.dispose();
}

void
MainWindow::on_file_print_preview()
{
	Gtk::SourcePrintJob *job = new Gtk::SourcePrintJob(*source_view);
	job->set_wrap_mode(Gtk::WRAP_CHAR);
	job->set_highlight(true);
	job->set_print_numbers(5);
	job->set_header_format("Printed on %A", 0, "%F", true);
	job->set_footer_format("%T", current_file, "Page %N/%Q", true);
	job->set_print_header(true);
	job->set_print_footer(true);

	Gtk::TextIter start, end;
	buffer->get_bounds(start, end);
	if (job->print_async(start, end))
	{
		job->sig_begin_page().connect(bind(slot(this, &MainWindow::print_job_begin_page), job));
		job->sig_finished().connect(bind(slot(this, &MainWindow::print_job_finished), job));
	}
	else
		g_warning("Async print failed");
}

void
MainWindow::on_file_quit()
{
	Inti::Main::quit();
}

void
MainWindow::on_view_new()
{
	Gtk::Window *window = new Gtk::Window;
	window->set_default_size(400, 400);
	window->set_title(get_title());
	window->set_border_width(0);

	// vbox
	Gtk::VBox *vbox = new Gtk::VBox;
	window->add(*vbox);
	vbox->show();

	// scrolled window
	Gtk::ScrolledWindow *sw = new Gtk::ScrolledWindow;
	sw->set_shadow_type(Gtk::SHADOW_IN);
	vbox->pack_end(*sw);
	sw->show();

	// source view
	SourceView *view = new SourceView(*buffer);
	sw->add(*view);
	view->set_show_line_markers(source_view->get_show_line_markers());
	view->set_show_line_numbers(source_view->get_show_line_numbers());
	view->set_show_margin(source_view->get_show_margin());
	view->set_auto_indent(source_view->get_auto_indent());
	view->set_insert_spaces_instead_of_tabs(source_view->get_insert_spaces_instead_of_tabs());
	view->set_tabs_width(source_view->get_tabs_width());
	views_list.push_back(view);
	view->show();

	window->sig_delete_event().connect(bind(slot(this, &MainWindow::on_view_delete_event), view));
	window->show();
}

void
MainWindow::on_view_show_markers()
{
	int count = views_list.size();
	bool check = static_cast<Gtk::CheckMenuItem*>(item_factory->get_item("/View/Show Markers"))->get_active();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_show_line_markers(check);
	}
}

void
MainWindow::on_view_show_line_numbers()
{
	int count = views_list.size();
	bool check = static_cast<Gtk::CheckMenuItem*>(item_factory->get_item("/View/Show Line Numbers"))->get_active();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_show_line_numbers(check);
	}
}

void
MainWindow::on_show_margin()
{
	int count = views_list.size();
	bool check = static_cast<Gtk::CheckMenuItem*>(item_factory->get_item("/View/Show Margin"))->get_active();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_show_margin(check);
	}
}

void
MainWindow::on_view_enable_auto_indent()
{
	int count = views_list.size();
	bool check = static_cast<Gtk::CheckMenuItem*>(item_factory->get_item("/View/Enable Auto Indent"))->get_active();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_auto_indent(check);
	}
}

void
MainWindow::on_view_insert_spaces()
{
	int count = views_list.size();
	bool check = static_cast<Gtk::CheckMenuItem*>(item_factory->get_item("/View/Insert Spaces Instead of Tabs"))->get_active();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_insert_spaces_instead_of_tabs(check);
	}
}

void
MainWindow::on_view_tabs_width_4()
{
	int count = views_list.size();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_tabs_width(4);
	}
}

void
MainWindow::on_view_tabs_width_6()
{
	int count = views_list.size();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_tabs_width(6);
	}
}

void
MainWindow::on_view_tabs_width_8()
{
	int count = views_list.size();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_tabs_width(8);
	}
}

void
MainWindow::on_view_tabs_width_10()
{
	int count = views_list.size();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_tabs_width(10);
	}
}

void
MainWindow::on_view_tabs_width_12()
{
	int count = views_list.size();
	for (int i = 0; i < count; i++)
	{
		views_list[i]->set_tabs_width(12);
	}
}

BEGIN_ITEM_FACTORY_MAP(MainWindow)
	IFM_BRANCH("/_File"),
	IFM_STOCK_ITEM("/File/_Open", "<control>O", on_file_open, GTK_STOCK_OPEN),
	IFM_STOCK_ITEM("/File/_Print Preview", "<control>P", on_file_print_preview, GTK_STOCK_PRINT),
	IFM_SEPARATOR("/File/sep1"),
	IFM_STOCK_ITEM("/File/Quit", "<control>Q", on_file_quit, GTK_STOCK_QUIT),

	IFM_BRANCH("/_View"),
	IFM_STOCK_ITEM("/View/_New View", 0, on_view_new, GTK_STOCK_NEW),
	IFM_SEPARATOR("/View/sep1"),
	IFM_CHECK_ITEM("/View/Show _Line Numbers", 0, on_view_show_line_numbers),
	IFM_CHECK_ITEM("/View/Show _Markers", 0, on_view_show_markers),
	IFM_CHECK_ITEM("/View/Show M_argin", 0, on_show_margin),
	IFM_SEPARATOR("/View/sep2"),
	IFM_CHECK_ITEM("/View/Enable _Auto Indent", 0, on_view_enable_auto_indent),
	IFM_CHECK_ITEM("/View/Insert _Spaces Instead of Tabs", 0, on_view_insert_spaces),
	IFM_SEPARATOR("/View/sep3"),

	IFM_BRANCH("/_View/_Tabs Width"),
	IFM_RADIO_ITEM("/View/Tabs Width/4", 0, on_view_tabs_width_4),
	IFM_RADIO_ITEM_LINK("/View/Tabs Width/6", on_view_tabs_width_6, "/View/Tabs Width/4"),
	IFM_RADIO_ITEM_LINK("/View/Tabs Width/8", on_view_tabs_width_8, "/View/Tabs Width/4"),
	IFM_RADIO_ITEM_LINK("/View/Tabs Width/10", on_view_tabs_width_10, "/View/Tabs Width/4"),
	IFM_RADIO_ITEM_LINK("/View/Tabs Width/12", on_view_tabs_width_12, "/View/Tabs Width/4"),
END_ITEM_FACTORY_MAP

int main (int argc, char *argv[])
{
	using namespace Main;

	init(&argc, &argv);
	gnome_vfs_init ();

	MainWindow window;
	window.sig_destroy().connect(slot(&Inti::Main::quit));

	if (argc > 1)
		window.open_file(argv [1]);
	else
		window.open_file("../inti/gtk-sourceview/sourcebuffer.cc");

	window.show();

	run();
	gnome_vfs_shutdown ();
	return 0;
}