gtk2/gtk/gtkfilesel.c
Havoc Pennington 9ae724bf4d Remove all references to offscreen flag which was no longer used.
Thu Jun  1 23:05:13 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkwidget.c: Remove all references to
	offscreen flag which was no longer used.

	* gtk/gtkprivate.h (enum): Remove unused flags and compress.

	* gtk/gtkframe.c (gtk_frame_set_label_widget): Check
	for non-null label_widget->parent.

	* gtk/gtkentry.c: Get rid of code to deal with PangoAttribute
	which no longer was used.

	* gdk/gdkpango.c (gdk_pango_context_get_info): make static.

	* gdk/gdkpango.c (gdk_draw_layout[_line]): Add checks
	for null arguments.

	* gdk/x11/gdkgeometry-x11.c (gdk_window_scroll): add
	check for destroyed windows.

Thu Jun  1 13:48:45 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkimmulticontext.c: Add a finalize method and unref
	the slave context there.

	* gtk/gtkinvisible.[ch]: Make reference counting behavior
	identical to GtkWindow.

Thu Jun  1 01:54:11 2000  Owen Taylor  <otaylor@redhat.com>

	* Makefile.am gdk/gdkpango.c: Copy the layout render function from
	pangox to here, so we can write them independent of rendering
	system, using GDK primitives.

	* gdk/gdkdrawable.h gdk/gdkdraw.c gdk/gdkwindow.c
	gdk/x11/gdkdrawable-x11.c: Remove draw_layout() from the vtable,
	since we have a rendering-system independent implementation in
	terms of draw_glyphs().

	* gdk/gdkpango.c gdkdrawable.h (gdk_draw_layout_line): New
	function to render a single line.

	* gdk/x11/gdkpango.c: Move the guts of this file mostly
	into ../gdkpango.c, which simplifies things, since we
	don't have to deal with raw X gc's.

Fri May 19 04:28:16 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextlayout.[ch]: Add get_log_attrs() function to
	get the logical attributes for a given GtkTextLine.

Tue May 30 16:05:39 2000  Owen Taylor  <otaylor@redhat.com>

	* gdk/x11/gdkfont-x11.c (gdk_font_charset_for_locale): Track
	g_locale_get_codeset() to g_get_codeset() change.

Tue May 30 15:03:19 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/testcalendar.c (calendar_font_selection_ok): Use font
	descriptions.

	* gtk/gtkentry.c (gtk_entry_draw_text): Center text within
	the entry.

	* gtk/gtkfontsel.c (gtk_font_selection_dialog_init): Start of
	redoing (vastly simplifying) for Pango. Still needs quite
	a bit of work. (Size selection is currently poor. List of
	predefined sizes is not a good idea, since all of these
	sizes won't necessarily be distinct.)

Tue May 30 13:50:19 2000  Owen Taylor  <otaylor@redhat.com>

	* gdk/x11/gdkfont-x11.c (gdk_font_charset_for_locale): Handle
	CODESET results for LANG=C.

Mon May 29 15:49:10 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkrc.[ch]: Add a 'font_name' declaration to RC
	which takes a stringized pango font description;
	ignore the older 'font' and 'fontset' declarations.

	* gtk/gtkstyle.c gtk/gtkrc.c: Fill in the style->font
	field with a GdkFont derived via gdk_font_from_description(),
	for compatibility. (Should we just remove it entirely?
	Probably too much compatibility breakage, but people
	should be migrating to the new Pango stuff as quickly
	as possible.)

Mon May 29 15:47:41 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkentry.c gtk/gtkclist.c: s/pango_font_unref/g_object_unref/.

Mon May 29 15:44:46 2000  Owen Taylor  <otaylor@redhat.com>

        * gtk/gtkcalender.c: Roughly pango-ized. Really needs
	redoing; there are some bugs in size allocation right
	now, the semi-existant distinction between header / day
	fonts was removed, but, with Pango, could actually
	be made functional in a nice way.

	* gtk/testcalender: Move calender from examples into this
	directory as a test program. (We really need to restrcture
	testgtk into a whole directory full of tests for every
	widget or functionality group, separated into multiple .c
	files.)

Mon May 29 15:19:56 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/testgtk.c (file_exists): Fix stupid typo that
	was keeping RC file from being loaded.

	* gtk/testgtkrc gtk/testgtkrc2: Test new pango-ized
	RC file font code.

Mon May 29 14:31:27 2000  Owen Taylor  <otaylor@redhat.com>

	* gdk/gdkfont.h gdk/x11/gdkfont-x11.c (gdk_font_from_description):
	Add function to load a GdkFont from a PangoFontDescription.

Fri May 26 17:16:40 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/frame.[ch] gtkaspectframe.c: Make frame widgets able
	to have any widget for the label, use a GtkLabel widget
	to display the text. (Based partially on a patch from
	Anders Carlson.)

	(Quite a bit of code reorganization - strip 90% of the
	guts out of gtkaspectframe and add a single virtual
	func to GtkFrameClass - compute_child_allocation.)

Fri May 26 12:00:02 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkctree.c gtk/gtkclist.[ch]: Pangoized.
	(Removed clist->row_center_offset field because caching
	it wasn't saving time or code, added private function
	_gtk_clist_create_cell_layout()).

Wed May 24 15:59:37 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkaccellabel.c: Pangoized.

	* gtk/[hv]ruler.c: Pangoized

Mon May 22 19:23:59 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkfilesel.c (gtk_file_selection_init):
	Use gtk_clist_set_column_auto_resize() to remove need
	need for manual column width computations.

Mon May 22 18:50:26 2000  Owen Taylor  <otaylor@redhat.com>

        * gtk/gtktooltips.[ch]: Replace custom drawing with a GtkLabel,
	ensuring Pango correctness, and considerably simplifying the
	code.

	* gtk/gtklabel.c gtk[hv]scale.c: 1000 => PANGO_SCALE.

	* gtk/gtklabel.c (gtk_label_size_request): Fixed incorrect
	getting of numbers of lines.

	* gtk/gtklabel.c (gtk_label_size_request): Set the requisition
	to the actual requested width of the lable, not to the wrap
	width we set.

	* gtk/gtktextchild.h: Remove extraneous include of gtk/gtk.h.

	* gtk/gtktextbtree.c gtk/gtktextbuffer.c gtk/gtktextlayout.c
	  gtk/gtktextview.c gtk/gtktextview.[ch]: Fix up includes.

	* gtk/gtktextview.c: Fix structure inheritance.

	* gtk/gtkprogressbar.c: Pangoize.

Mon May 22 15:47:30 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextview.c (gtk_text_view_get_first_para_iter): Encapsulate
	in a function.

	* gtk/gtktextlayout.c (find_display_line_above): Fixed
	bug with computing line tops.

	* gtk/gtktextview.c (changed_handler): Fix < , <= confusion.

Thu May 18 18:53:31 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextdisplay.c (gtk_text_layout_draw): Fix up the x_offset
	and y_offset coordinates to do what we need now. (The offset between
	buffer and layout coordinates has been reintroduced, but is a
	bit different than before.)

	* gtk/gtktextview.[ch]: No longer inherit from GtkLayout; instead
	handle the adjustments ourselves, and scroll as necessary using
	the new gdk_window_scroll().

	The advantage of this is that when we are incrementally revalidating,
	we are essentially rearranging things around the visible portion
	of the screen. With the old setup, the visible portion of the
	screen was moved around in the layout, so scrolling and redrawing
	to track that caused jumping of the display. Since we now
	control the scrolling ourselves, we can suppress this and
	only redraw when things actually change.

Thu May 18 18:47:25 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextbtree.c (redisplay_mark): We need to invalidate
	the region not just redisplay it after-all, since we store the
	cursors in the LineDisplay. (Ugly interactions here between
	GtkLayout and GtkTextBTree here.)

	* gtk/gtktextbtree.c (redisplay_region): Fixed reversed comparison.

Thu May 18 18:43:21 2000  Owen Taylor  <otaylor@redhat.com>

	* gdk/gdkwindow.h gdk/x11/gdkgeometry-x11.c (gdk_window_scroll):
	Added function to scroll contents of a window while keeping the
	window constant. Works by XCopyArea or guffaw-scrolling depending
	on the details of how the window is set up. (guffaw-scrolling
	still needs to be filled in.)

Wed May 17 22:36:53 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextiter.c gtk/gtkmain.c: Add a debug key for the text widget,
	move the debugging that was tied to a global variable
	to that.

	* gtk/gtkmarshal.list: Add NONE:INT,INT,INT

	* gtk/gtktextbtree.[ch] gtk/gtktextlayout.c: Keep a separate
	validated flag, in line data instead of setting height/width to
	-1. This allows us to perform operations with partially invalid
	buffer (using the old size for invalid lines) and thus to do
	incremental vaidation. Keep height/width aggregates up to date
	when deleting text and rebalancing the tree.

	* gtk/gtktextbtree.[ch]: Add functions validate a line
	(gtk_text_btree_validate_line), and to validate up
	to a number of pixels (gtk_text_btree_validate).

	* gtk/gtktextlayout.[ch]: Add an ::invalidated signal
	that indicates that something is changed and a revalidation
	pass is needed. Change ::need_repaint to ::changed, and
	make it take old and new yranges instead of a rectangle.

	* gtk/gtktextbtree.[ch] gtk/gtktextlayout.[ch]: Move
	the line_data_destroy() function from
	gtk_text_btree_add_view() to a virtual function in
	GtkTextLayout

	* gtk/gtktextbtree.[ch]: Remove gtk_text_btree_get_damage_range(),
	since we are handling partial repaints in a different fashion
	now.

	* gtk/gtktextbtree.[ch]: Only repaint the changed portion
	of the selection instead of queueing a repaint on the
	entire widget.

	* gtk/gtktextbuffer.[ch] gtk/gtktextbtree.[ch]: Move
	get_selection_bounds() down to btree, make the function
	in buffer a wrapper around the btree function.

	* gtk/gtktextlayout.[ch]: Add functions to check if the
	layout is valid and to recompute either a range of pixels
	aroudn a line or a certain total number of pixels.

	* gtk/gtktextlayout.[ch]: Cache a single line display;
	now that we only redraw the needed portions, the hit rate
	for this cache is quite high.

	* gtk/gtktextview.[ch]: Keep track of the first paragraph
	on the screen so that when re-laying-out the buffer, we can
	keep the same place. This requires connecting to ::value_changed
	on the adjustments

	* gtk/gtktextview.[ch]: Add idle functions to revalidate
	the buffer after we receive an ::invalidated signal.

Wed May 17 22:10:47 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtklayout.c (gtk_layout_size_allocate): Set upper
	to max of allocation and layout size, not just to the
	layout size.

	* gtk/gtk[hv]scrollbar.c (gtk_[hv]scrollbar_calc_slider_size):
	Invalidate window so it gets redrawn properly.

	* gdk/gdkwindow.c (gdk_window_invalidate_rect): Allow rect == NULL
	to mean the entire window.

	* gdk/gdkevents.h: Move definition for GDK_PRIORITY_REDRAW
	into public header.

Mon May 15 14:51:31 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextmark.c (gtk_text_mark_get_name): Add function
	to get the name of a mark.

	* gtk/gtktextlayout.c (gtk_text_layout_get_line_at_y): Add a function
	to find the paragraph from a y position.

Thu May 11 12:57:20 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextbtree.c (gtk_text_btree_node_invalidate_upward): Valid
	nodes have width/height >= 0, not > 0.

Tue May  9 21:29:06 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextlayout.[ch] gtk/gtktextdisplay.c (gtk_text_layout_get_line_display):
	Add a size_only flag, so when we only need the size, we don't create
	useless appearance attributes.

	* gtk/gtktextview.c (gtk_text_view_ensure_layout): Remove
	duplicate setting of font description.

	* gtk/gtkscale.c: Use PANGO_SCALE instead of 1000

Wed Apr 26 01:53:23 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/Makefile.am (EXTRA_DIST): Add OLD_STAMP into
	EXTRA_DIST. It does not work well when the file that
	everything depends on is not in the tarball.

Wed Apr 26 00:56:14 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/testgtk.c: Some hacks and fixes so that it basically
	works when not sitting in the GTK+ build tree.

2000-05-03  Havoc Pennington  <hp@redhat.com>

	* gtk/gtktextbtree.c (gtk_text_line_next_could_contain_tag):
	Properly determine the ordering of the tag root and the current
	line within the tree. Previous algorithm only worked if the tag
	root's immediate parent was the common root of both the current
	line and the tag root.

Wed Apr 26 00:43:00 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextlayout.c (set_para_values): Fix some bugs in
	alignment.

	* gtk/gtktextview.c (gtk_text_view_ensure_layout): Track
	the widget text directional dynamically.

	* gtk/gtktextview.[ch]: Added functions to get and set default
	wrap mode.

Tue Apr 25 23:47:38 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextlayout.c (gtk_text_layout_get_iter_location): Fix bug
	in cursor location computation.

Tue Apr 25 23:22:59 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtklayout.c (gtk_layout_set_size): Clamp hadjustment/
	vadjusment values properly when layout gets smaller.

	* gtk/gtktextview.c (need_repaint_handler): Areas being
	passed in are far completely inaccurate, and sometimes
	too small, so, for now, just queue a redraw on the
	whole visible region.

2000-04-25  Havoc Pennington  <hp@redhat.com>

	* gtk/gtktextbtree.c (summary_destroy): new function to
	destroy tag summary nodes
	(gtk_text_line_next_could_contain_tag): this function was
	totally broken if the line passed in wasn't below the tag
	root. Fix it.
	(gtk_text_btree_first_could_contain_tag): In the tag == NULL
	"wildcard" case, we have to do a linear scan. Blah.
	(gtk_text_btree_last_could_contain_tag): In tag == NULL case,
	we have to do the linear scan
	(tag_removed_cb): When a tag is removed from the tag table,
	remove the GtkTextTagInfo node from the btree.
	(gtk_text_btree_spew): Implement the spew function, for
	our debugging pleasure.

Tue Apr 25 19:40:18 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextlayout.c (gtk_text_layout_set_buffer): Fix
	a problem with referring to the wrong buffer.

	* gtk/gtkentry.c: Fix focus-in/focus-out confusion.

	* gtk/gtkrc.c gtk/gtkstyle.c: Moving setting default
	font description to gtk_style_new() - otherwise things
	don't work without a .gtkrc file.

	* gtk/gtktextbuffer.c (gtk_text_buffer_new): Sink the
	tags table if we create it ourself, too.

	* gdk/gdktypes.h (enum): Move GDK_RELEASE_MASK, since
	it was conflicting with XKB modifiers.

	* gtk/gtktextview.[ch]: Add simple support for
	GtkIMContext.

Mon Apr 24 19:34:18 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkentry.c (gtk_entry_move_cursor_visually): Fix problem
	with deletion from last commit.

Mon Apr 24 19:29:40 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkwidget.c (gtk_widget_create_pango_context): Set the language
	in the context from the current locale.

	* gtk/gtkentry.c (gtk_entry_size_request): Use language from the
	context, not hardcoded value.

	* gtk/gtkentry.c (gtk_entry_move_cursor): Make character movement visual,
	not logical.

Sun Apr 23 23:39:18 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkentry.c gtk/gtktextdisplay.c: Don't handle selections as
	attributes - that doesn't handle partial-glyph selection
	properly. Instead use new pango_layout_line_get_x_ranges()
	functionality to draw the selection.

	* gtk/gtkentry.c: Simplify code since pango_layout_line_index_to_x()
	now properly handles out-of-range coordinates.

	* gtk/gtktextbuffer.c: Emit "mark_set" when the cursor is moved.

	* gtk/gtktextiter.h gtk/gtktextiterprivate.h: Make gtk_text_iter_get_line_byte()
	public.

	* gtk/gtktextlayout.[ch]: Properly set the direction in the PangoContext
	for paragraphs opposite to the base direction of the widget.

	* gtk/gtktextlayout.[ch] gtk/gtktextdisplay.c: Fixes for alignment.

	* gtk/gtktextlayout.c: Don't split segments on marks, since that
	causes Arabic words to reshape as you cursor through.

	* gtk/gtktextlayout.[ch] gtk/gtktextview.[ch]: Implement virtual
	cursor position when moving vertically with the arrow keys and
	scrolling with page-up/page-down. (Arrow keys save only the X,
	scrolling saves both X and Y.)

	This means you can line-up / line-down or page-up / page-down
	without losing your place, and also that moving vertically
	with the cursor keys keeps the same X position, not the same
	character count:

	* gtk/gtktextlayout.[ch] gtk/gtktextview.[ch]: Make vertical
	arrow keys move by display lines, not paragraphs.

Tue Apr 18 14:16:50 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtklayout.c: Make sure that the bin window is at least
	as big as the allocation. (Should we also make sure that the
	bin window is big enough to completely cover widget->window?)

	* gtk/gtktextview.c (gtk_text_view_get_visible_rect): Add
	function to get the onscreen rectangle.

	* gdk/x11/gdkwindow-x11.c (gdk_window_get_pointer): Correctly account
	for offsets in window coordinates.

Sun Apr 16 16:13:27 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkentry.c (gtk_entry_get_cursor_locations): Fix index/offset
	confusion.

	* gtk/gtktextview.c (gtk_text_view_ensure_layout): Set the default direction
	from the widget direction.

	* gtk/gtktexttag.c gtk/gtktexttagprivate.h (gtk_text_tag_set_arg):
	Add a "direction" attribute.

	* gtk/gtktextview.c: global s/tkxt/text_view/.

	* gtk/testtext.c: Added long block of text in Arabic, to test out
	the direction attributes. (Some problems with the shaping system
	for arabic become obvious - like the fact the cursor splits words
	into unjoined pieces.)

Fri Apr 14 12:54:34 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktextdisplay.c (render_layout): Add overstrike handling.

	* gtk/gtktextlayout.c: Fix up alignment.

	* gtk/testtext.c: Add some tests for centering, wrapping.

Fri Apr 14 09:26:22 2000  Owen Taylor  <otaylor@redhat.com>

	* gdk/gdkdrawable.h gdk/gdkdraw.c gdk/gdkwindow.c gdk/x11/gdkdrawable-x11.c:
	Add a draw_glyphs() operation to the drawable vtable and gdk_draw_glyphs().
	If we wrote GTK+-specific layout-render function this could just replace
	the draw_layout() operation in the vtable.

	* gtk/gtkentry.c: Move guts of gtk_entry_get_cursor_locations to
	pango_layout_get_cursor_pos() and use that function.

	* gtk/gtktextchild.[ch]: add gtk_ onto pixmap_segment_new(), since it
	is a non-static symbol.

	* gtk/gtktextbtree.[ch]: Replace gtk_text_btree_find_line_data_by_y()
	with gtk_text_btree_find_line_by_y()

	* gtk/gtktextdisplay.c: Rewrote for Pango - uses a custom layout
	renderer that handles GtkTextAppearance attributes.

	* gtk/gtktexttag.[ch] gtk/gtktexttagprivate.h:

	  - Move the values in the style that don't affect geometry into a
	    GtkTextAppearance structure.
	  - Change underline to take a PangoUnderline and "font" a string
	    representation of a font description
	  - Add a "font_desc" attribute which takes a FontDescription structure.

	* gtk/gtktextlayout.[ch]:

	  - Get rid of the display-line list per each line. Instead, we
	    generate, on demand, a GtkTextLineDisplay structure which]
	    contains a PangoLayout * and other necesary information
	    (offsets, cursor locations) for displaying a paragraph.
	  - Get rid of the code to wrap lines, create display chunks,
	    etc. Instead, we just go through a paragraph and convert
	    it into the necessary inputs to a PangoLayout.
	  - Implement a new attribute type, GtkTextAttrAppearance. This
	    holds a GtkTextAppearance, and is used to pass colors,
	    stipple, etc, through from the layout to the display without
	    having to use lots and lots of individual attributes.
	  - Reimplement gtk_layout_get_iter_at_pixel() gtk_layout_get_iter_pos()
	    in terms of PangoLayout functions.

	* gtk/gtktextview.c:

	  - Handle passing the necessary PangoContext to the layout
	  - Some fixups in painting to deal with the automatic backing store
	    and offsetting of GTK+-1.4
	  - Add a style_set handler so that the default style reacts
	    properly to theme changes.

	* gtk/gtktext?*.[ch]: Random code-style fixes.

	* gtk/testtext.c: Substitute in languages that Pango handles now for Thai

Mon Apr 10 10:33:45 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktext?*.[ch]: Check in Havoc's port of the Tk text widget,
	in original form preparatory to Pango-ization and gdkimcontext-ization.

Thu Apr  6 19:25:39 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkimcontext.c: Move default implementations to real_* vfuncs,
	so that we can derive from gtkimcontext in language bindings properly.

Thu Apr  6 16:02:52 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkimcontextsimple.[ch]: Use gdk_keyval_to_unicode to gdk_unicode_to_keyval.
	Add a compose table including (almost) all the compose combinations
	from X. This is 6k of static, shared data as opposed to 50k or so of dynamic
	data in the XIM implementation.

	* gdk/gdk.h gdk/gdkkeyuni.c gdk/win32/gdkevents-win32.c (gdk_keyval_to_unicode, gdk_unicode_to_keyval):
	Moved functions to convert keyvalues from and to unicode here from
	the win32 port and made them public.

Wed Apr  5 16:37:29 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkeditable.c (gtk_editable_insert_text): Allow new_text_length == -1.

Wed Apr  5 16:27:45 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtkimcontext.[ch]: Base class for new input context system

	* gtk/gtkimmulticontext.[ch]: Proxy input context that allows
	the real input context implementation to be loaded from modules
	and switched on the fly.

	* gtk/gtkcontextsimple.[ch]: Simple implementation of an input
	context that just does direct keysymbol => unicode translation.

	* gtk/gtkentry.[ch]: Start switching editing over to using
	GtkInputContext. (No handling of preedit yet.)

Wed Apr  5 15:48:41 2000  Owen Taylor  <otaylor@redhat.com>

	* gtk/gtktypeutils.h (GTK_CHECK_GET_CLASS): Fix problem with one too
	many substitutions. (klass should not be subsituted.)

Wed Apr  5 00:18:14 2000  Owen Taylor  <otaylor@redhat.com>

	* configure.in: Add checks for Pango

	* configure.in docs/Makefile.am: Add test for sgml2html
	and allow 'make dist' without building html, but print out
	warnings in that case. (For making snapshots)

	* gdk/Makefile.am gdk/x11/Makefile.am gtk/Makefile.am:
	Add Pango libraries and C flags

	* gdk/gdkdraw.c gdk/gdkdrawable.h gdkwindow.c gdk/x11/gdkdrawable-x11.c:
	Add function (gdk_draw_layout) to draw a pango layout.

	* gdk/gdkpango.h gdk/x11/gdkpango-x11.c: New file with functions
	for getting Pango contexts for GDK.

	* gtk/gtkeditable.c: Get rid of dead code gtk_editable_parent_set()

	* gtk/gtkentry.[ch]: Complete rewrite to use Pango, add bidirectional
	editing.

	* gtk/gtkentry.c: Hack in simple Hebrew input with direct
	keysym => unicode translations. More languages can be added
	here, but real input-method support is needed.

	* docs/Changes-1.4.txt: Added note about entry behavior.

	* gtk/gtkenums.h gtk/gtkwidget.[ch] testgtk.c gtkprivate.h: Add functions
	to set the reading direction for a widget and the global direction.
	Add test which allows toggling the global direction. Two private
	flags are used to store the direction. (GTK_DIRECTION_SET + GTK_DIRECTION_LTR)

	* gtk/gtkcheckbutton.c gtk/gtkframe.c gtk/gtkhbbox.c gtk/gtkhbox.c
	  gtk/gtkradiobutton.c gtk/gtkspinbutton.c gtk/gtktable.c

	* gtk/gtk[hv]scale.c gtk/gtkscale.[ch]: Draw numbers using Pango

	* gtk/gtklabel.[ch]: Moved to Pango and considerably rewritten. Line breaking,
	underlining now handled by Pango.

	* gtk/gtkstyle.[ch] gtk/gtkrc.[ch]: Add a PangoFontDescription
	to RCStyle and Style. (Having both this and the old font name and GdkFont
	is temporary.)

	* gtk/gtkwidget.[ch] (gtk_widget_create_pango_{context,layout}): Added
	convenience functions for creating contexts and layouts for widgets.

	* gtk/testgtk.c: Enhance label tests with multilingual labels.
2000-06-02 03:14:07 +00:00

2963 lines
76 KiB
C

/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include "config.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include <errno.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include "fnmatch.h"
#include "gdk/gdkkeysyms.h"
#include "gtkbutton.h"
#include "gtkentry.h"
#include "gtkfilesel.h"
#include "gtkhbox.h"
#include "gtkhbbox.h"
#include "gtklabel.h"
#include "gtklist.h"
#include "gtklistitem.h"
#include "gtkmain.h"
#include "gtkscrolledwindow.h"
#include "gtksignal.h"
#include "gtkvbox.h"
#include "gtkmenu.h"
#include "gtkmenuitem.h"
#include "gtkoptionmenu.h"
#include "gtkclist.h"
#include "gtkdialog.h"
#include "gtkintl.h"
#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
#define STRICT
#include <windows.h>
#ifdef G_OS_WIN32
#include <direct.h>
#include <io.h>
#define mkdir(p,m) _mkdir(p)
#ifndef S_ISDIR
#define S_ISDIR(mode) ((mode)&_S_IFDIR)
#endif
#endif /* G_OS_WIN32 */
#endif /* G_OS_WIN32 || G_WITH_CYGWIN */
#define DIR_LIST_WIDTH 180
#define DIR_LIST_HEIGHT 180
#define FILE_LIST_WIDTH 180
#define FILE_LIST_HEIGHT 180
/* I've put this here so it doesn't get confused with the
* file completion interface */
typedef struct _HistoryCallbackArg HistoryCallbackArg;
struct _HistoryCallbackArg
{
gchar *directory;
GtkWidget *menu_item;
};
typedef struct _CompletionState CompletionState;
typedef struct _CompletionDir CompletionDir;
typedef struct _CompletionDirSent CompletionDirSent;
typedef struct _CompletionDirEntry CompletionDirEntry;
typedef struct _CompletionUserDir CompletionUserDir;
typedef struct _PossibleCompletion PossibleCompletion;
/* Non-external file completion decls and structures */
/* A contant telling PRCS how many directories to cache. Its actually
* kept in a list, so the geometry isn't important. */
#define CMPL_DIRECTORY_CACHE_SIZE 10
/* A constant used to determine whether a substring was an exact
* match by first_diff_index()
*/
#define PATTERN_MATCH -1
/* The arguments used by all fnmatch() calls below
*/
#define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
#define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
/* This structure contains all the useful information about a directory
* for the purposes of filename completion. These structures are cached
* in the CompletionState struct. CompletionDir's are reference counted.
*/
struct _CompletionDirSent
{
ino_t inode;
time_t mtime;
dev_t device;
gint entry_count;
struct _CompletionDirEntry *entries;
};
struct _CompletionDir
{
CompletionDirSent *sent;
gchar *fullname;
gint fullname_len;
struct _CompletionDir *cmpl_parent;
gint cmpl_index;
gchar *cmpl_text;
};
/* This structure contains pairs of directory entry names with a flag saying
* whether or not they are a valid directory. NOTE: This information is used
* to provide the caller with information about whether to update its completions
* or try to open a file. Since directories are cached by the directory mtime,
* a symlink which points to an invalid file (which will not be a directory),
* will not be reevaluated if that file is created, unless the containing
* directory is touched. I consider this case to be worth ignoring (josh).
*/
struct _CompletionDirEntry
{
gboolean is_dir;
gchar *entry_name;
};
struct _CompletionUserDir
{
gchar *login;
gchar *homedir;
};
struct _PossibleCompletion
{
/* accessible fields, all are accessed externally by functions
* declared above
*/
gchar *text;
gint is_a_completion;
gboolean is_directory;
/* Private fields
*/
gint text_alloc;
};
struct _CompletionState
{
gint last_valid_char;
gchar *updated_text;
gint updated_text_len;
gint updated_text_alloc;
gboolean re_complete;
gchar *user_dir_name_buffer;
gint user_directories_len;
gchar *last_completion_text;
gint user_completion_index; /* if >= 0, currently completing ~user */
struct _CompletionDir *completion_dir; /* directory completing from */
struct _CompletionDir *active_completion_dir;
struct _PossibleCompletion the_completion;
struct _CompletionDir *reference_dir; /* initial directory */
GList* directory_storage;
GList* directory_sent_storage;
struct _CompletionUserDir *user_directories;
};
/* File completion functions which would be external, were they used
* outside of this file.
*/
static CompletionState* cmpl_init_state (void);
static void cmpl_free_state (CompletionState *cmpl_state);
static gint cmpl_state_okay (CompletionState* cmpl_state);
static gchar* cmpl_strerror (gint);
static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
gchar **remaining_text,
CompletionState *cmpl_state);
/* Returns a name for consideration, possibly a completion, this name
* will be invalid after the next call to cmpl_next_completion.
*/
static char* cmpl_this_completion (PossibleCompletion*);
/* True if this completion matches the given text. Otherwise, this
* output can be used to have a list of non-completions.
*/
static gint cmpl_is_a_completion (PossibleCompletion*);
/* True if the completion is a directory
*/
static gboolean cmpl_is_directory (PossibleCompletion*);
/* Obtains the next completion, or NULL
*/
static PossibleCompletion* cmpl_next_completion (CompletionState*);
/* Updating completions: the return value of cmpl_updated_text() will
* be text_to_complete completed as much as possible after the most
* recent call to cmpl_completion_matches. For the present
* application, this is the suggested replacement for the user's input
* string. You must CALL THIS AFTER ALL cmpl_text_completions have
* been received.
*/
static gchar* cmpl_updated_text (CompletionState* cmpl_state);
/* After updating, to see if the completion was a directory, call
* this. If it was, you should consider re-calling completion_matches.
*/
static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
/* Current location: if using file completion, return the current
* directory, from which file completion begins. More specifically,
* the cwd concatenated with all exact completions up to the last
* directory delimiter('/').
*/
static gchar* cmpl_reference_position (CompletionState* cmpl_state);
/* backing up: if cmpl_completion_matches returns NULL, you may query
* the index of the last completable character into cmpl_updated_text.
*/
static gint cmpl_last_valid_char (CompletionState* cmpl_state);
/* When the user selects a non-directory, call cmpl_completion_fullname
* to get the full name of the selected file.
*/
static gchar* cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
/* Directory operations. */
static CompletionDir* open_ref_dir (gchar* text_to_complete,
gchar** remaining_text,
CompletionState* cmpl_state);
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
static gboolean check_dir (gchar *dir_name,
struct stat *result,
gboolean *stat_subdirs);
#endif
static CompletionDir* open_dir (gchar* dir_name,
CompletionState* cmpl_state);
#ifdef HAVE_PWD_H
static CompletionDir* open_user_dir (gchar* text_to_complete,
CompletionState *cmpl_state);
#endif
static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
CompletionState *cmpl_state);
static CompletionDirSent* open_new_dir (gchar* dir_name,
struct stat* sbuf,
gboolean stat_subdirs);
static gint correct_dir_fullname (CompletionDir* cmpl_dir);
static gint correct_parent (CompletionDir* cmpl_dir,
struct stat *sbuf);
static gchar* find_parent_dir_fullname (gchar* dirname);
static CompletionDir* attach_dir (CompletionDirSent* sent,
gchar* dir_name,
CompletionState *cmpl_state);
static void free_dir_sent (CompletionDirSent* sent);
static void free_dir (CompletionDir *dir);
static void prune_memory_usage(CompletionState *cmpl_state);
/* Completion operations */
static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
CompletionState *cmpl_state);
static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
static CompletionDir* find_completion_dir(gchar* text_to_complete,
gchar** remaining_text,
CompletionState* cmpl_state);
static PossibleCompletion* append_completion_text(gchar* text,
CompletionState* cmpl_state);
#ifdef HAVE_PWD_H
static gint get_pwdb(CompletionState* cmpl_state);
static gint compare_user_dir(const void* a, const void* b);
#endif
static gint first_diff_index(gchar* pat, gchar* text);
static gint compare_cmpl_dir(const void* a, const void* b);
static void update_cmpl(PossibleCompletion* poss,
CompletionState* cmpl_state);
static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
static void gtk_file_selection_init (GtkFileSelection *filesel);
static void gtk_file_selection_destroy (GtkObject *object);
static gint gtk_file_selection_key_press (GtkWidget *widget,
GdkEventKey *event,
gpointer user_data);
static void gtk_file_selection_file_button (GtkWidget *widget,
gint row,
gint column,
GdkEventButton *bevent,
gpointer user_data);
static void gtk_file_selection_dir_button (GtkWidget *widget,
gint row,
gint column,
GdkEventButton *bevent,
gpointer data);
static void gtk_file_selection_populate (GtkFileSelection *fs,
gchar *rel_path,
gint try_complete);
static void gtk_file_selection_abort (GtkFileSelection *fs);
static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
gchar *current_dir);
static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
static GtkWindowClass *parent_class = NULL;
/* Saves errno when something cmpl does fails. */
static gint cmpl_errno;
#ifdef G_WITH_CYGWIN
/*
* Take the path currently in the file selection
* entry field and translate as necessary from
* a WIN32 style to CYGWIN32 style path. For
* instance translate:
* x:\somepath\file.jpg
* to:
* //x/somepath/file.jpg
*
* Replace the path in the selection text field.
* Return a boolean value concerning whether a
* translation had to be made.
*/
int
translate_win32_path(GtkFileSelection *filesel)
{
int updated = 0;
gchar *path;
/*
* Retrieve the current path
*/
path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
/*
* Translate only if this looks like a DOS-ish
* path... First handle any drive letters.
*/
if (isalpha(path[0]) && (path[1] == ':')) {
/*
* This part kind of stinks... It isn't possible
* to know if there is enough space in the current
* string for the extra character required in this
* conversion. Assume that there isn't enough space
* and use the set function on the text field to
* set the newly created string.
*/
gchar *newPath;
newPath = g_malloc(strlen(path) + 2);
sprintf(newPath, "//%c/%s", path[0], (path + 3));
gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
path = newPath;
updated = 1;
}
/*
* Now, replace backslashes with forward slashes
* if necessary.
*/
if (strchr(path, '\\')) {
int index;
for (index = 0; path[index] != '\0'; index++)
if (path[index] == '\\')
path[index] = '/';
updated = 1;
}
return updated;
}
#endif
GtkType
gtk_file_selection_get_type (void)
{
static GtkType file_selection_type = 0;
if (!file_selection_type)
{
static const GtkTypeInfo filesel_info =
{
"GtkFileSelection",
sizeof (GtkFileSelection),
sizeof (GtkFileSelectionClass),
(GtkClassInitFunc) gtk_file_selection_class_init,
(GtkObjectInitFunc) gtk_file_selection_init,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
(GtkClassInitFunc) NULL,
};
file_selection_type = gtk_type_unique (GTK_TYPE_WINDOW, &filesel_info);
}
return file_selection_type;
}
static void
gtk_file_selection_class_init (GtkFileSelectionClass *class)
{
GtkObjectClass *object_class;
object_class = (GtkObjectClass*) class;
parent_class = gtk_type_class (GTK_TYPE_WINDOW);
object_class->destroy = gtk_file_selection_destroy;
}
static void
gtk_file_selection_init (GtkFileSelection *filesel)
{
GtkWidget *entry_vbox;
GtkWidget *label;
GtkWidget *list_hbox;
GtkWidget *confirm_area;
GtkWidget *pulldown_hbox;
GtkWidget *scrolled_win;
char *dir_title [2];
char *file_title [2];
filesel->cmpl_state = cmpl_init_state ();
/* The dialog-sized vertical box */
filesel->main_vbox = gtk_vbox_new (FALSE, 10);
gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
gtk_container_add (GTK_CONTAINER (filesel), filesel->main_vbox);
gtk_widget_show (filesel->main_vbox);
/* The horizontal box containing create, rename etc. buttons */
filesel->button_area = gtk_hbutton_box_new ();
gtk_button_box_set_layout(GTK_BUTTON_BOX(filesel->button_area), GTK_BUTTONBOX_START);
gtk_button_box_set_spacing(GTK_BUTTON_BOX(filesel->button_area), 0);
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
FALSE, FALSE, 0);
gtk_widget_show (filesel->button_area);
gtk_file_selection_show_fileop_buttons(filesel);
/* hbox for pulldown menu */
pulldown_hbox = gtk_hbox_new (TRUE, 5);
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
gtk_widget_show (pulldown_hbox);
/* Pulldown menu */
filesel->history_pulldown = gtk_option_menu_new ();
gtk_widget_show (filesel->history_pulldown);
gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
FALSE, FALSE, 0);
/* The horizontal box containing the directory and file listboxes */
list_hbox = gtk_hbox_new (FALSE, 5);
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
gtk_widget_show (list_hbox);
/* The directories clist */
dir_title[0] = _("Directories");
dir_title[1] = NULL;
filesel->dir_list = gtk_clist_new_with_titles (1, (gchar**) dir_title);
gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
(GtkSignalFunc) gtk_file_selection_dir_button,
(gpointer) filesel);
gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->dir_list), 0, TRUE);
gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
gtk_widget_show (filesel->dir_list);
gtk_widget_show (scrolled_win);
/* The files clist */
file_title[0] = _("Files");
file_title[1] = NULL;
filesel->file_list = gtk_clist_new_with_titles (1, (gchar**) file_title);
gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
(GtkSignalFunc) gtk_file_selection_file_button,
(gpointer) filesel);
gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->file_list), 0, TRUE);
gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
gtk_widget_show (filesel->file_list);
gtk_widget_show (scrolled_win);
/* action area for packing buttons into. */
filesel->action_area = gtk_hbox_new (TRUE, 0);
gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
FALSE, FALSE, 0);
gtk_widget_show (filesel->action_area);
/* The OK/Cancel button area */
confirm_area = gtk_hbutton_box_new ();
gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area), GTK_BUTTONBOX_END);
gtk_button_box_set_spacing(GTK_BUTTON_BOX(confirm_area), 5);
gtk_box_pack_end (GTK_BOX (filesel->main_vbox), confirm_area, FALSE, FALSE, 0);
gtk_widget_show (confirm_area);
/* The OK button */
filesel->ok_button = gtk_button_new_with_label (_("OK"));
GTK_WIDGET_SET_FLAGS (filesel->ok_button, GTK_CAN_DEFAULT);
gtk_box_pack_start (GTK_BOX (confirm_area), filesel->ok_button, TRUE, TRUE, 0);
gtk_widget_grab_default (filesel->ok_button);
gtk_widget_show (filesel->ok_button);
/* The Cancel button */
filesel->cancel_button = gtk_button_new_with_label (_("Cancel"));
GTK_WIDGET_SET_FLAGS (filesel->cancel_button, GTK_CAN_DEFAULT);
gtk_box_pack_start (GTK_BOX (confirm_area), filesel->cancel_button, TRUE, TRUE, 0);
gtk_widget_show (filesel->cancel_button);
/* The selection entry widget */
entry_vbox = gtk_vbox_new (FALSE, 2);
gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 0);
gtk_widget_show (entry_vbox);
filesel->selection_text = label = gtk_label_new ("");
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
gtk_widget_show (label);
filesel->selection_entry = gtk_entry_new ();
gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
(GtkSignalFunc) gtk_file_selection_key_press, filesel);
gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
(GtkSignalFunc) gtk_widget_grab_default,
GTK_OBJECT (filesel->ok_button));
gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
(GtkSignalFunc) gtk_button_clicked,
GTK_OBJECT (filesel->ok_button));
gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
gtk_widget_show (filesel->selection_entry);
if (!cmpl_state_okay (filesel->cmpl_state))
{
gchar err_buf[256];
sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
}
else
{
gtk_file_selection_populate (filesel, "", FALSE);
}
gtk_widget_grab_focus (filesel->selection_entry);
}
GtkWidget*
gtk_file_selection_new (const gchar *title)
{
GtkFileSelection *filesel;
filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
gtk_window_set_title (GTK_WINDOW (filesel), title);
return GTK_WIDGET (filesel);
}
void
gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
{
g_return_if_fail (filesel != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
/* delete, create directory, and rename */
if (!filesel->fileop_c_dir)
{
filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
(GtkSignalFunc) gtk_file_selection_create_dir,
(gpointer) filesel);
gtk_box_pack_start (GTK_BOX (filesel->button_area),
filesel->fileop_c_dir, TRUE, TRUE, 0);
gtk_widget_show (filesel->fileop_c_dir);
}
if (!filesel->fileop_del_file)
{
filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
(GtkSignalFunc) gtk_file_selection_delete_file,
(gpointer) filesel);
gtk_box_pack_start (GTK_BOX (filesel->button_area),
filesel->fileop_del_file, TRUE, TRUE, 0);
gtk_widget_show (filesel->fileop_del_file);
}
if (!filesel->fileop_ren_file)
{
filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
(GtkSignalFunc) gtk_file_selection_rename_file,
(gpointer) filesel);
gtk_box_pack_start (GTK_BOX (filesel->button_area),
filesel->fileop_ren_file, TRUE, TRUE, 0);
gtk_widget_show (filesel->fileop_ren_file);
}
gtk_widget_queue_resize(GTK_WIDGET(filesel));
}
void
gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
{
g_return_if_fail (filesel != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
if (filesel->fileop_ren_file)
{
gtk_widget_destroy (filesel->fileop_ren_file);
filesel->fileop_ren_file = NULL;
}
if (filesel->fileop_del_file)
{
gtk_widget_destroy (filesel->fileop_del_file);
filesel->fileop_del_file = NULL;
}
if (filesel->fileop_c_dir)
{
gtk_widget_destroy (filesel->fileop_c_dir);
filesel->fileop_c_dir = NULL;
}
}
void
gtk_file_selection_set_filename (GtkFileSelection *filesel,
const gchar *filename)
{
gchar *buf;
const char *name, *last_slash;
g_return_if_fail (filesel != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
g_return_if_fail (filename != NULL);
last_slash = strrchr (filename, G_DIR_SEPARATOR);
if (!last_slash)
{
buf = g_strdup ("");
name = filename;
}
else
{
buf = g_strdup (filename);
buf[last_slash - filename + 1] = 0;
name = last_slash + 1;
}
gtk_file_selection_populate (filesel, buf, FALSE);
if (filesel->selection_entry)
gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
g_free (buf);
}
gchar*
gtk_file_selection_get_filename (GtkFileSelection *filesel)
{
static gchar nothing[2] = "";
static gchar something[MAXPATHLEN*2];
char *filename;
char *text;
g_return_val_if_fail (filesel != NULL, nothing);
g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
#ifdef G_WITH_CYGWIN
translate_win32_path(filesel);
#endif
text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
if (text)
{
filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state));
strncpy (something, filename, sizeof (something));
g_free (filename);
return something;
}
return nothing;
}
void
gtk_file_selection_complete (GtkFileSelection *filesel,
const gchar *pattern)
{
g_return_if_fail (filesel != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
g_return_if_fail (pattern != NULL);
if (filesel->selection_entry)
gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
}
static void
gtk_file_selection_destroy (GtkObject *object)
{
GtkFileSelection *filesel;
GList *list;
HistoryCallbackArg *callback_arg;
g_return_if_fail (GTK_IS_FILE_SELECTION (object));
filesel = GTK_FILE_SELECTION (object);
if (filesel->fileop_dialog)
{
gtk_widget_destroy (filesel->fileop_dialog);
filesel->fileop_dialog = NULL;
}
if (filesel->history_list)
{
list = filesel->history_list;
while (list)
{
callback_arg = list->data;
g_free (callback_arg->directory);
g_free (callback_arg);
list = list->next;
}
g_list_free (filesel->history_list);
filesel->history_list = NULL;
}
if (filesel->cmpl_state)
{
cmpl_free_state (filesel->cmpl_state);
filesel->cmpl_state = NULL;
}
GTK_OBJECT_CLASS (parent_class)->destroy (object);
}
/* Begin file operations callbacks */
static void
gtk_file_selection_fileop_error (GtkFileSelection *fs, gchar *error_message)
{
GtkWidget *label;
GtkWidget *vbox;
GtkWidget *button;
GtkWidget *dialog;
g_return_if_fail (error_message != NULL);
/* main dialog */
dialog = gtk_dialog_new ();
/*
gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
(GtkSignalFunc) gtk_file_selection_fileop_destroy,
(gpointer) fs);
*/
gtk_window_set_title (GTK_WINDOW (dialog), _("Error"));
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
/* If file dialog is grabbed, make this dialog modal too */
/* When error dialog is closed, file dialog will be grabbed again */
if (GTK_WINDOW(fs)->modal)
gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
FALSE, FALSE, 0);
gtk_widget_show(vbox);
label = gtk_label_new(error_message);
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
gtk_widget_show(label);
/* yes, we free it */
g_free (error_message);
/* close button */
button = gtk_button_new_with_label (_("Close"));
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
(GtkSignalFunc) gtk_widget_destroy,
(gpointer) dialog);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
gtk_widget_grab_default(button);
gtk_widget_show (button);
gtk_widget_show (dialog);
}
static void
gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
{
GtkFileSelection *fs = data;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
fs->fileop_dialog = NULL;
}
static void
gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
{
GtkFileSelection *fs = data;
gchar *dirname;
gchar *path;
gchar *full_path;
gchar *buf;
CompletionState *cmpl_state;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
cmpl_state = (CompletionState*) fs->cmpl_state;
path = cmpl_reference_position (cmpl_state);
full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
if ( (mkdir (full_path, 0755) < 0) )
{
buf = g_strconcat ("Error creating directory \"", dirname, "\": ",
g_strerror(errno), NULL);
gtk_file_selection_fileop_error (fs, buf);
}
g_free (full_path);
gtk_widget_destroy (fs->fileop_dialog);
gtk_file_selection_populate (fs, "", FALSE);
}
static void
gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
{
GtkFileSelection *fs = data;
GtkWidget *label;
GtkWidget *dialog;
GtkWidget *vbox;
GtkWidget *button;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
if (fs->fileop_dialog)
return;
/* main dialog */
dialog = gtk_dialog_new ();
fs->fileop_dialog = dialog;
gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
(GtkSignalFunc) gtk_file_selection_fileop_destroy,
(gpointer) fs);
gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
/* If file dialog is grabbed, grab option dialog */
/* When option dialog is closed, file dialog will be grabbed again */
if (GTK_WINDOW(fs)->modal)
gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
FALSE, FALSE, 0);
gtk_widget_show(vbox);
label = gtk_label_new(_("Directory name:"));
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
gtk_widget_show(label);
/* The directory entry widget */
fs->fileop_entry = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
TRUE, TRUE, 5);
GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
gtk_widget_show (fs->fileop_entry);
/* buttons */
button = gtk_button_new_with_label (_("Create"));
gtk_signal_connect (GTK_OBJECT (button), "clicked",
(GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
(gpointer) fs);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
gtk_widget_show(button);
button = gtk_button_new_with_label (_("Cancel"));
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
(GtkSignalFunc) gtk_widget_destroy,
(gpointer) dialog);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
gtk_widget_grab_default(button);
gtk_widget_show (button);
gtk_widget_show (dialog);
}
static void
gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
{
GtkFileSelection *fs = data;
CompletionState *cmpl_state;
gchar *path;
gchar *full_path;
gchar *buf;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
cmpl_state = (CompletionState*) fs->cmpl_state;
path = cmpl_reference_position (cmpl_state);
full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
if ( (unlink (full_path) < 0) )
{
buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\": ",
g_strerror(errno), NULL);
gtk_file_selection_fileop_error (fs, buf);
}
g_free (full_path);
gtk_widget_destroy (fs->fileop_dialog);
gtk_file_selection_populate (fs, "", FALSE);
}
static void
gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
{
GtkFileSelection *fs = data;
GtkWidget *label;
GtkWidget *vbox;
GtkWidget *button;
GtkWidget *dialog;
gchar *filename;
gchar *buf;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
if (fs->fileop_dialog)
return;
#ifdef G_WITH_CYGWIN
translate_win32_path(fs);
#endif
filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
if (strlen(filename) < 1)
return;
fs->fileop_file = filename;
/* main dialog */
fs->fileop_dialog = dialog = gtk_dialog_new ();
gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
(GtkSignalFunc) gtk_file_selection_fileop_destroy,
(gpointer) fs);
gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
/* If file dialog is grabbed, grab option dialog */
/* When option dialog is closed, file dialog will be grabbed again */
if (GTK_WINDOW(fs)->modal)
gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
FALSE, FALSE, 0);
gtk_widget_show(vbox);
buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
label = gtk_label_new(buf);
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
gtk_widget_show(label);
g_free(buf);
/* buttons */
button = gtk_button_new_with_label (_("Delete"));
gtk_signal_connect (GTK_OBJECT (button), "clicked",
(GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
(gpointer) fs);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
gtk_widget_show(button);
button = gtk_button_new_with_label (_("Cancel"));
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
(GtkSignalFunc) gtk_widget_destroy,
(gpointer) dialog);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
gtk_widget_grab_default(button);
gtk_widget_show (button);
gtk_widget_show (dialog);
}
static void
gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
{
GtkFileSelection *fs = data;
gchar *buf;
gchar *file;
gchar *path;
gchar *new_filename;
gchar *old_filename;
CompletionState *cmpl_state;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
cmpl_state = (CompletionState*) fs->cmpl_state;
path = cmpl_reference_position (cmpl_state);
new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
if ( (rename (old_filename, new_filename)) < 0)
{
buf = g_strconcat ("Error renaming file \"", file, "\": ",
g_strerror(errno), NULL);
gtk_file_selection_fileop_error (fs, buf);
}
g_free (new_filename);
g_free (old_filename);
gtk_widget_destroy (fs->fileop_dialog);
gtk_file_selection_populate (fs, "", FALSE);
}
static void
gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
{
GtkFileSelection *fs = data;
GtkWidget *label;
GtkWidget *dialog;
GtkWidget *vbox;
GtkWidget *button;
gchar *buf;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
if (fs->fileop_dialog)
return;
fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
if (strlen(fs->fileop_file) < 1)
return;
/* main dialog */
fs->fileop_dialog = dialog = gtk_dialog_new ();
gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
(GtkSignalFunc) gtk_file_selection_fileop_destroy,
(gpointer) fs);
gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
/* If file dialog is grabbed, grab option dialog */
/* When option dialog closed, file dialog will be grabbed again */
if (GTK_WINDOW(fs)->modal)
gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
FALSE, FALSE, 0);
gtk_widget_show(vbox);
buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
label = gtk_label_new(buf);
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
gtk_widget_show(label);
g_free(buf);
/* New filename entry */
fs->fileop_entry = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
TRUE, TRUE, 5);
GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
gtk_widget_show (fs->fileop_entry);
gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
0, strlen (fs->fileop_file));
/* buttons */
button = gtk_button_new_with_label (_("Rename"));
gtk_signal_connect (GTK_OBJECT (button), "clicked",
(GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
(gpointer) fs);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
gtk_widget_show(button);
button = gtk_button_new_with_label (_("Cancel"));
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
(GtkSignalFunc) gtk_widget_destroy,
(gpointer) dialog);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
gtk_widget_grab_default(button);
gtk_widget_show (button);
gtk_widget_show (dialog);
}
static gint
gtk_file_selection_key_press (GtkWidget *widget,
GdkEventKey *event,
gpointer user_data)
{
GtkFileSelection *fs;
char *text;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (event != NULL, FALSE);
if (event->keyval == GDK_Tab)
{
fs = GTK_FILE_SELECTION (user_data);
#ifdef G_WITH_CYGWIN
translate_win32_path(fs);
#endif
text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
text = g_strdup (text);
gtk_file_selection_populate (fs, text, TRUE);
g_free (text);
gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
return TRUE;
}
return FALSE;
}
static void
gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
{
GtkFileSelection *fs = data;
HistoryCallbackArg *callback_arg;
GList *list;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
list = fs->history_list;
while (list) {
callback_arg = list->data;
if (callback_arg->menu_item == widget)
{
gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
break;
}
list = list->next;
}
}
static void
gtk_file_selection_update_history_menu (GtkFileSelection *fs,
gchar *current_directory)
{
HistoryCallbackArg *callback_arg;
GtkWidget *menu_item;
GList *list;
gchar *current_dir;
gint dir_len;
gint i;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
g_return_if_fail (current_directory != NULL);
list = fs->history_list;
if (fs->history_menu)
{
while (list) {
callback_arg = list->data;
g_free (callback_arg->directory);
g_free (callback_arg);
list = list->next;
}
g_list_free (fs->history_list);
fs->history_list = NULL;
gtk_widget_destroy (fs->history_menu);
}
fs->history_menu = gtk_menu_new();
current_dir = g_strdup (current_directory);
dir_len = strlen (current_dir);
for (i = dir_len; i >= 0; i--)
{
/* the i == dir_len is to catch the full path for the first
* entry. */
if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
{
/* another small hack to catch the full path */
if (i != dir_len)
current_dir[i + 1] = '\0';
#ifdef G_WITH_CYGWIN
if (!strcmp(current_dir, "//"))
continue;
#endif
menu_item = gtk_menu_item_new_with_label (current_dir);
callback_arg = g_new (HistoryCallbackArg, 1);
callback_arg->menu_item = menu_item;
/* since the autocompletion gets confused if you don't
* supply a trailing '/' on a dir entry, set the full
* (current) path to "" which just refreshes the filesel */
if (dir_len == i)
{
callback_arg->directory = g_strdup ("");
}
else
{
callback_arg->directory = g_strdup (current_dir);
}
fs->history_list = g_list_append (fs->history_list, callback_arg);
gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
(GtkSignalFunc) gtk_file_selection_history_callback,
(gpointer) fs);
gtk_menu_append (GTK_MENU (fs->history_menu), menu_item);
gtk_widget_show (menu_item);
}
}
gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
fs->history_menu);
g_free (current_dir);
}
static void
gtk_file_selection_file_button (GtkWidget *widget,
gint row,
gint column,
GdkEventButton *bevent,
gpointer user_data)
{
GtkFileSelection *fs = NULL;
gchar *filename, *temp = NULL;
g_return_if_fail (GTK_IS_CLIST (widget));
fs = user_data;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
filename = g_strdup (temp);
#ifdef G_WITH_CYGWIN
/* Check to see if the selection was a drive selector */
if (isalpha(filename[0]) && (filename[1] == ':')) {
/* It is... map it to a CYGWIN32 drive */
char temp_filename[10];
sprintf(temp_filename, "//%c/", tolower(filename[0]));
g_free(filename);
filename = g_strdup(temp_filename);
}
#endif /* G_WITH_CYGWIN */
if (filename)
{
if (bevent)
switch (bevent->type)
{
case GDK_2BUTTON_PRESS:
gtk_button_clicked (GTK_BUTTON (fs->ok_button));
break;
default:
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
break;
}
else
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
g_free (filename);
}
}
static void
gtk_file_selection_dir_button (GtkWidget *widget,
gint row,
gint column,
GdkEventButton *bevent,
gpointer user_data)
{
GtkFileSelection *fs = NULL;
gchar *filename, *temp = NULL;
g_return_if_fail (GTK_IS_CLIST (widget));
fs = GTK_FILE_SELECTION (user_data);
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
filename = g_strdup (temp);
if (filename)
{
if (bevent)
switch (bevent->type)
{
case GDK_2BUTTON_PRESS:
gtk_file_selection_populate (fs, filename, FALSE);
break;
default:
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
break;
}
else
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
g_free (filename);
}
}
#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
static void
win32_gtk_add_drives_to_dir_list(GtkWidget *the_dir_list)
{
gchar *text[2], *textPtr;
gchar buffer[128];
char volumeNameBuf[128];
char formatBuffer[128];
text[1] = NULL;
/* Get the Drives string */
GetLogicalDriveStrings(sizeof(buffer), buffer);
/* Add the drives as necessary */
textPtr = buffer;
while (*textPtr != '\0') {
/* Get the volume information for this drive */
if ((tolower(textPtr[0]) != 'a') && (tolower(textPtr[0]) != 'b'))
{
/* Ignore floppies (?) */
DWORD maxComponentLength, flags;
GetVolumeInformation(textPtr,
volumeNameBuf, sizeof(volumeNameBuf),
NULL, &maxComponentLength,
&flags, NULL, 0);
/* Build the actual displayable string */
sprintf(formatBuffer, "%c:\\", toupper(textPtr[0]));
#if 0 /* HB: removed to allow drive change AND directory update with one click */
if (strlen(volumeNameBuf) > 0)
sprintf(formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
#endif
/* Add to the list */
text[0] = formatBuffer;
gtk_clist_append (GTK_CLIST (the_dir_list), text);
}
textPtr += (strlen(textPtr) + 1);
}
}
#endif
static void
gtk_file_selection_populate (GtkFileSelection *fs,
gchar *rel_path,
gint try_complete)
{
CompletionState *cmpl_state;
PossibleCompletion* poss;
gchar* filename;
gchar* rem_path = rel_path;
gchar* sel_text;
gchar* text[2];
gint did_recurse = FALSE;
gint possible_count = 0;
gint selection_index = -1;
g_return_if_fail (fs != NULL);
g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
cmpl_state = (CompletionState*) fs->cmpl_state;
poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
if (!cmpl_state_okay (cmpl_state))
{
/* Something went wrong. */
gtk_file_selection_abort (fs);
return;
}
g_assert (cmpl_state->reference_dir);
gtk_clist_freeze (GTK_CLIST (fs->dir_list));
gtk_clist_clear (GTK_CLIST (fs->dir_list));
gtk_clist_freeze (GTK_CLIST (fs->file_list));
gtk_clist_clear (GTK_CLIST (fs->file_list));
/* Set the dir_list to include ./ and ../ */
text[1] = NULL;
text[0] = "." G_DIR_SEPARATOR_S;
gtk_clist_append (GTK_CLIST (fs->dir_list), text);
text[0] = ".." G_DIR_SEPARATOR_S;
gtk_clist_append (GTK_CLIST (fs->dir_list), text);
while (poss)
{
if (cmpl_is_a_completion (poss))
{
possible_count += 1;
filename = cmpl_this_completion (poss);
text[0] = filename;
if (cmpl_is_directory (poss))
{
if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
gtk_clist_append (GTK_CLIST (fs->dir_list), text);
}
else
{
gtk_clist_append (GTK_CLIST (fs->file_list), text);
}
}
poss = cmpl_next_completion (cmpl_state);
}
#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
/* For Windows, add drives as potential selections */
win32_gtk_add_drives_to_dir_list (fs->dir_list);
#endif
gtk_clist_thaw (GTK_CLIST (fs->dir_list));
gtk_clist_thaw (GTK_CLIST (fs->file_list));
/* File lists are set. */
g_assert (cmpl_state->reference_dir);
if (try_complete)
{
/* User is trying to complete filenames, so advance the user's input
* string to the updated_text, which is the common leading substring
* of all possible completions, and if its a directory attempt
* attempt completions in it. */
if (cmpl_updated_text (cmpl_state)[0])
{
if (cmpl_updated_dir (cmpl_state))
{
gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
did_recurse = TRUE;
gtk_file_selection_populate (fs, dir_name, TRUE);
g_free (dir_name);
}
else
{
if (fs->selection_entry)
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
cmpl_updated_text (cmpl_state));
}
}
else
{
selection_index = cmpl_last_valid_char (cmpl_state) -
(strlen (rel_path) - strlen (rem_path));
if (fs->selection_entry)
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
}
}
else
{
if (fs->selection_entry)
gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
}
if (!did_recurse)
{
if (fs->selection_entry)
gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
if (fs->selection_entry)
{
sel_text = g_strconcat (_("Selection: "),
cmpl_reference_position (cmpl_state),
NULL);
gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
g_free (sel_text);
}
if (fs->history_pulldown)
{
gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
}
}
}
static void
gtk_file_selection_abort (GtkFileSelection *fs)
{
gchar err_buf[256];
sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
/* BEEP gdk_beep(); */
if (fs->selection_entry)
gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
}
/**********************************************************************/
/* External Interface */
/**********************************************************************/
/* The four completion state selectors
*/
static gchar*
cmpl_updated_text (CompletionState* cmpl_state)
{
return cmpl_state->updated_text;
}
static gboolean
cmpl_updated_dir (CompletionState* cmpl_state)
{
return cmpl_state->re_complete;
}
static gchar*
cmpl_reference_position (CompletionState* cmpl_state)
{
return cmpl_state->reference_dir->fullname;
}
static gint
cmpl_last_valid_char (CompletionState* cmpl_state)
{
return cmpl_state->last_valid_char;
}
static gchar*
cmpl_completion_fullname (gchar* text, CompletionState* cmpl_state)
{
static char nothing[2] = "";
if (!cmpl_state_okay (cmpl_state))
{
return nothing;
}
else if (g_path_is_absolute (text))
{
strcpy (cmpl_state->updated_text, text);
}
#ifdef HAVE_PWD_H
else if (text[0] == '~')
{
CompletionDir* dir;
char* slash;
dir = open_user_dir (text, cmpl_state);
if (!dir)
{
/* spencer says just return ~something, so
* for now just do it. */
strcpy (cmpl_state->updated_text, text);
}
else
{
strcpy (cmpl_state->updated_text, dir->fullname);
slash = strchr (text, G_DIR_SEPARATOR);
if (slash)
strcat (cmpl_state->updated_text, slash);
}
}
#endif
else
{
strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
strcat (cmpl_state->updated_text, text);
}
return cmpl_state->updated_text;
}
/* The three completion selectors
*/
static gchar*
cmpl_this_completion (PossibleCompletion* pc)
{
return pc->text;
}
static gboolean
cmpl_is_directory (PossibleCompletion* pc)
{
return pc->is_directory;
}
static gint
cmpl_is_a_completion (PossibleCompletion* pc)
{
return pc->is_a_completion;
}
/**********************************************************************/
/* Construction, deletion */
/**********************************************************************/
static CompletionState*
cmpl_init_state (void)
{
gchar *getcwd_buf;
CompletionState *new_state;
new_state = g_new (CompletionState, 1);
getcwd_buf = g_get_current_dir ();
tryagain:
new_state->reference_dir = NULL;
new_state->completion_dir = NULL;
new_state->active_completion_dir = NULL;
new_state->directory_storage = NULL;
new_state->directory_sent_storage = NULL;
new_state->last_valid_char = 0;
new_state->updated_text = g_new (gchar, MAXPATHLEN);
new_state->updated_text_alloc = MAXPATHLEN;
new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
new_state->the_completion.text_alloc = MAXPATHLEN;
new_state->user_dir_name_buffer = NULL;
new_state->user_directories = NULL;
new_state->reference_dir = open_dir (getcwd_buf, new_state);
if (!new_state->reference_dir)
{
/* Directories changing from underneath us, grumble */
strcpy (getcwd_buf, G_DIR_SEPARATOR_S);
goto tryagain;
}
g_free (getcwd_buf);
return new_state;
}
static void
cmpl_free_dir_list(GList* dp0)
{
GList *dp = dp0;
while (dp) {
free_dir (dp->data);
dp = dp->next;
}
g_list_free(dp0);
}
static void
cmpl_free_dir_sent_list(GList* dp0)
{
GList *dp = dp0;
while (dp) {
free_dir_sent (dp->data);
dp = dp->next;
}
g_list_free(dp0);
}
static void
cmpl_free_state (CompletionState* cmpl_state)
{
g_return_if_fail (cmpl_state != NULL);
cmpl_free_dir_list (cmpl_state->directory_storage);
cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
if (cmpl_state->user_dir_name_buffer)
g_free (cmpl_state->user_dir_name_buffer);
if (cmpl_state->user_directories)
g_free (cmpl_state->user_directories);
if (cmpl_state->the_completion.text)
g_free (cmpl_state->the_completion.text);
if (cmpl_state->updated_text)
g_free (cmpl_state->updated_text);
g_free (cmpl_state);
}
static void
free_dir(CompletionDir* dir)
{
g_free(dir->fullname);
g_free(dir);
}
static void
free_dir_sent(CompletionDirSent* sent)
{
gint i;
for (i = 0; i < sent->entry_count; i++)
g_free(sent->entries[i].entry_name);
g_free(sent->entries);
g_free(sent);
}
static void
prune_memory_usage(CompletionState *cmpl_state)
{
GList* cdsl = cmpl_state->directory_sent_storage;
GList* cdl = cmpl_state->directory_storage;
GList* cdl0 = cdl;
gint len = 0;
for(; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
cdsl = cdsl->next;
if (cdsl) {
cmpl_free_dir_sent_list(cdsl->next);
cdsl->next = NULL;
}
cmpl_state->directory_storage = NULL;
while (cdl) {
if (cdl->data == cmpl_state->reference_dir)
cmpl_state->directory_storage = g_list_prepend(NULL, cdl->data);
else
free_dir (cdl->data);
cdl = cdl->next;
}
g_list_free(cdl0);
}
/**********************************************************************/
/* The main entrances. */
/**********************************************************************/
static PossibleCompletion*
cmpl_completion_matches (gchar* text_to_complete,
gchar** remaining_text,
CompletionState* cmpl_state)
{
gchar* first_slash;
PossibleCompletion *poss;
prune_memory_usage(cmpl_state);
g_assert (text_to_complete != NULL);
cmpl_state->user_completion_index = -1;
cmpl_state->last_completion_text = text_to_complete;
cmpl_state->the_completion.text[0] = 0;
cmpl_state->last_valid_char = 0;
cmpl_state->updated_text_len = -1;
cmpl_state->updated_text[0] = 0;
cmpl_state->re_complete = FALSE;
#ifdef HAVE_PWD_H
first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
if (text_to_complete[0] == '~' && !first_slash)
{
/* Text starts with ~ and there is no slash, show all the
* home directory completions.
*/
poss = attempt_homedir_completion (text_to_complete, cmpl_state);
update_cmpl(poss, cmpl_state);
return poss;
}
#endif
cmpl_state->reference_dir =
open_ref_dir (text_to_complete, remaining_text, cmpl_state);
if(!cmpl_state->reference_dir)
return NULL;
cmpl_state->completion_dir =
find_completion_dir (*remaining_text, remaining_text, cmpl_state);
cmpl_state->last_valid_char = *remaining_text - text_to_complete;
if(!cmpl_state->completion_dir)
return NULL;
cmpl_state->completion_dir->cmpl_index = -1;
cmpl_state->completion_dir->cmpl_parent = NULL;
cmpl_state->completion_dir->cmpl_text = *remaining_text;
cmpl_state->active_completion_dir = cmpl_state->completion_dir;
cmpl_state->reference_dir = cmpl_state->completion_dir;
poss = attempt_file_completion(cmpl_state);
update_cmpl(poss, cmpl_state);
return poss;
}
static PossibleCompletion*
cmpl_next_completion (CompletionState* cmpl_state)
{
PossibleCompletion* poss = NULL;
cmpl_state->the_completion.text[0] = 0;
#ifdef HAVE_PWD_H
if(cmpl_state->user_completion_index >= 0)
poss = attempt_homedir_completion(cmpl_state->last_completion_text, cmpl_state);
else
poss = attempt_file_completion(cmpl_state);
#else
poss = attempt_file_completion(cmpl_state);
#endif
update_cmpl(poss, cmpl_state);
return poss;
}
/**********************************************************************/
/* Directory Operations */
/**********************************************************************/
/* Open the directory where completion will begin from, if possible. */
static CompletionDir*
open_ref_dir(gchar* text_to_complete,
gchar** remaining_text,
CompletionState* cmpl_state)
{
gchar* first_slash;
CompletionDir *new_dir;
first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
#ifdef G_WITH_CYGWIN
if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
{
char root_dir[5];
sprintf(root_dir, "//%c", text_to_complete[2]);
new_dir = open_dir(root_dir, cmpl_state);
if (new_dir) {
*remaining_text = text_to_complete + 4;
}
}
#else
if (FALSE)
;
#endif
#ifdef HAVE_PWD_H
else if (text_to_complete[0] == '~')
{
new_dir = open_user_dir(text_to_complete, cmpl_state);
if(new_dir)
{
if(first_slash)
*remaining_text = first_slash + 1;
else
*remaining_text = text_to_complete + strlen(text_to_complete);
}
else
{
return NULL;
}
}
#endif
else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
{
gchar *tmp = g_strdup(text_to_complete);
gchar *p;
p = tmp;
while (*p && *p != '*' && *p != '?')
p++;
*p = '\0';
p = strrchr(tmp, G_DIR_SEPARATOR);
if (p)
{
if (p == tmp)
p++;
*p = '\0';
new_dir = open_dir(tmp, cmpl_state);
if(new_dir)
*remaining_text = text_to_complete +
((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
}
else
{
/* If no possible candidates, use the cwd */
gchar *curdir = g_get_current_dir ();
new_dir = open_dir(curdir, cmpl_state);
if (new_dir)
*remaining_text = text_to_complete;
g_free (curdir);
}
g_free (tmp);
}
else
{
*remaining_text = text_to_complete;
new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
}
if(new_dir)
{
new_dir->cmpl_index = -1;
new_dir->cmpl_parent = NULL;
}
return new_dir;
}
#ifdef HAVE_PWD_H
/* open a directory by user name */
static CompletionDir*
open_user_dir(gchar* text_to_complete,
CompletionState *cmpl_state)
{
gchar *first_slash;
gint cmp_len;
g_assert(text_to_complete && text_to_complete[0] == '~');
first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
if (first_slash)
cmp_len = first_slash - text_to_complete - 1;
else
cmp_len = strlen(text_to_complete + 1);
if(!cmp_len)
{
/* ~/ */
gchar *homedir = g_get_home_dir ();
if (homedir)
return open_dir(homedir, cmpl_state);
else
return NULL;
}
else
{
/* ~user/ */
char* copy = g_new(char, cmp_len + 1);
struct passwd *pwd;
strncpy(copy, text_to_complete + 1, cmp_len);
copy[cmp_len] = 0;
pwd = getpwnam(copy);
g_free(copy);
if (!pwd)
{
cmpl_errno = errno;
return NULL;
}
return open_dir(pwd->pw_dir, cmpl_state);
}
}
#endif
/* open a directory relative the the current relative directory */
static CompletionDir*
open_relative_dir(gchar* dir_name,
CompletionDir* dir,
CompletionState *cmpl_state)
{
CompletionDir *result;
GString *path;
path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
g_string_assign (path, dir->fullname);
if(dir->fullname_len > 1
&& path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
g_string_append_c (path, G_DIR_SEPARATOR);
g_string_append (path, dir_name);
result = open_dir(path->str, cmpl_state);
g_string_free (path, TRUE);
return result;
}
/* after the cache lookup fails, really open a new directory */
static CompletionDirSent*
open_new_dir(gchar* dir_name, struct stat* sbuf, gboolean stat_subdirs)
{
CompletionDirSent* sent;
DIR* directory;
struct dirent *dirent_ptr;
gint entry_count = 0;
gint i;
struct stat ent_sbuf;
GString *path;
gchar *xdir;
sent = g_new(CompletionDirSent, 1);
sent->mtime = sbuf->st_mtime;
sent->inode = sbuf->st_ino;
sent->device = sbuf->st_dev;
path = g_string_sized_new (2*MAXPATHLEN + 10);
xdir = g_filename_from_utf8 (dir_name);
directory = opendir(xdir);
g_free (xdir);
if(!directory)
{
cmpl_errno = errno;
return NULL;
}
while((dirent_ptr = readdir(directory)) != NULL)
{
entry_count++;
}
sent->entries = g_new(CompletionDirEntry, entry_count);
sent->entry_count = entry_count;
rewinddir(directory);
for(i = 0; i < entry_count; i += 1)
{
dirent_ptr = readdir(directory);
if(!dirent_ptr)
{
cmpl_errno = errno;
closedir(directory);
return NULL;
}
sent->entries[i].entry_name = g_filename_to_utf8 (dirent_ptr->d_name);
g_string_assign (path, dir_name);
if (path->str[path->len-1] != G_DIR_SEPARATOR)
{
g_string_append_c (path, G_DIR_SEPARATOR);
}
g_string_append (path, dirent_ptr->d_name);
if (stat_subdirs)
{
if(stat(path->str, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
sent->entries[i].is_dir = TRUE;
else
/* stat may fail, and we don't mind, since it could be a
* dangling symlink. */
sent->entries[i].is_dir = FALSE;
}
else
sent->entries[i].is_dir = 1;
}
g_string_free (path, TRUE);
qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
closedir(directory);
return sent;
}
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
static gboolean
check_dir(gchar *dir_name, struct stat *result, gboolean *stat_subdirs)
{
/* A list of directories that we know only contain other directories.
* Trying to stat every file in these directories would be very
* expensive.
*/
static struct {
gchar *name;
gboolean present;
struct stat statbuf;
} no_stat_dirs[] = {
{ "/afs", FALSE, { 0 } },
{ "/net", FALSE, { 0 } }
};
static const gint n_no_stat_dirs = sizeof(no_stat_dirs) / sizeof(no_stat_dirs[0]);
static gboolean initialized = FALSE;
gint i;
if (!initialized)
{
initialized = TRUE;
for (i = 0; i < n_no_stat_dirs; i++)
{
if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
no_stat_dirs[i].present = TRUE;
}
}
if(stat(dir_name, result) < 0)
{
cmpl_errno = errno;
return FALSE;
}
*stat_subdirs = TRUE;
for (i=0; i<n_no_stat_dirs; i++)
{
if (no_stat_dirs[i].present &&
(no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
(no_stat_dirs[i].statbuf.st_ino == result->st_ino))
{
*stat_subdirs = FALSE;
break;
}
}
return TRUE;
}
#endif
/* open a directory by absolute pathname */
static CompletionDir*
open_dir(gchar* dir_name, CompletionState* cmpl_state)
{
struct stat sbuf;
gboolean stat_subdirs;
CompletionDirSent *sent;
GList* cdsl;
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
if (!check_dir (dir_name, &sbuf, &stat_subdirs))
return NULL;
cdsl = cmpl_state->directory_sent_storage;
while (cdsl)
{
sent = cdsl->data;
if(sent->inode == sbuf.st_ino &&
sent->mtime == sbuf.st_mtime &&
sent->device == sbuf.st_dev)
return attach_dir(sent, dir_name, cmpl_state);
cdsl = cdsl->next;
}
#else
stat_subdirs = TRUE;
#endif
sent = open_new_dir(dir_name, &sbuf, stat_subdirs);
if (sent) {
cmpl_state->directory_sent_storage =
g_list_prepend(cmpl_state->directory_sent_storage, sent);
return attach_dir(sent, dir_name, cmpl_state);
}
return NULL;
}
static CompletionDir*
attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
{
CompletionDir* new_dir;
new_dir = g_new(CompletionDir, 1);
cmpl_state->directory_storage =
g_list_prepend(cmpl_state->directory_storage, new_dir);
new_dir->sent = sent;
new_dir->fullname = g_strdup(dir_name);
new_dir->fullname_len = strlen(dir_name);
return new_dir;
}
static gint
correct_dir_fullname(CompletionDir* cmpl_dir)
{
gint length = strlen(cmpl_dir->fullname);
gchar *first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
struct stat sbuf;
/* Does it end with /. (\.) ? */
if (length >= 2 &&
strcmp(cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
{
/* Is it just the root directory (on a drive) ? */
if (cmpl_dir->fullname + length - 2 == first_slash)
{
cmpl_dir->fullname[length - 1] = 0;
cmpl_dir->fullname_len = length - 1;
return TRUE;
}
else
{
cmpl_dir->fullname[length - 2] = 0;
}
}
/* Ends with /./ (\.\)? */
else if (length >= 3 &&
strcmp(cmpl_dir->fullname + length - 3,
G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
cmpl_dir->fullname[length - 2] = 0;
/* Ends with /.. (\..) ? */
else if (length >= 3 &&
strcmp(cmpl_dir->fullname + length - 3,
G_DIR_SEPARATOR_S "..") == 0)
{
/* Is it just /.. (X:\..)? */
if(cmpl_dir->fullname + length - 3 == first_slash)
{
cmpl_dir->fullname[length - 2] = 0;
cmpl_dir->fullname_len = length - 2;
return TRUE;
}
if(stat(cmpl_dir->fullname, &sbuf) < 0)
{
cmpl_errno = errno;
return FALSE;
}
cmpl_dir->fullname[length - 3] = 0;
if(!correct_parent(cmpl_dir, &sbuf))
return FALSE;
}
/* Ends with /../ (\..\)? */
else if (length >= 4 &&
strcmp(cmpl_dir->fullname + length - 4,
G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
{
/* Is it just /../ (X:\..\)? */
if(cmpl_dir->fullname + length - 4 == first_slash)
{
cmpl_dir->fullname[length - 3] = 0;
cmpl_dir->fullname_len = length - 3;
return TRUE;
}
if(stat(cmpl_dir->fullname, &sbuf) < 0)
{
cmpl_errno = errno;
return FALSE;
}
cmpl_dir->fullname[length - 4] = 0;
if(!correct_parent(cmpl_dir, &sbuf))
return FALSE;
}
cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
return TRUE;
}
static gint
correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
{
struct stat parbuf;
gchar *last_slash;
gchar *first_slash;
gchar *new_name;
gchar c = 0;
last_slash = strrchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
g_assert(last_slash);
first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
/* Clever (?) way to check for top-level directory that works also on
* Win32, where there is a drive letter and colon prefixed...
*/
if (last_slash != first_slash)
{
last_slash[0] = 0;
}
else
{
c = last_slash[1];
last_slash[1] = 0;
}
if (stat(cmpl_dir->fullname, &parbuf) < 0)
{
cmpl_errno = errno;
if (!c)
last_slash[0] = G_DIR_SEPARATOR;
return FALSE;
}
#ifndef G_OS_WIN32 /* No inode numbers on Win32 */
if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
/* it wasn't a link */
return TRUE;
if(c)
last_slash[1] = c;
else
last_slash[0] = G_DIR_SEPARATOR;
/* it was a link, have to figure it out the hard way */
new_name = find_parent_dir_fullname(cmpl_dir->fullname);
if (!new_name)
return FALSE;
g_free(cmpl_dir->fullname);
cmpl_dir->fullname = new_name;
#endif
return TRUE;
}
#ifndef G_OS_WIN32
static gchar*
find_parent_dir_fullname(gchar* dirname)
{
gchar *orig_dir;
gchar *result;
orig_dir = g_get_current_dir ();
if(chdir(dirname) != 0 || chdir("..") != 0)
{
cmpl_errno = errno;
return NULL;
}
result = g_get_current_dir ();
if(chdir(orig_dir) != 0)
{
cmpl_errno = errno;
return NULL;
}
g_free (orig_dir);
return result;
}
#endif
/**********************************************************************/
/* Completion Operations */
/**********************************************************************/
#ifdef HAVE_PWD_H
static PossibleCompletion*
attempt_homedir_completion(gchar* text_to_complete,
CompletionState *cmpl_state)
{
gint index, length;
if (!cmpl_state->user_dir_name_buffer &&
!get_pwdb(cmpl_state))
return NULL;
length = strlen(text_to_complete) - 1;
cmpl_state->user_completion_index += 1;
while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
{
index = first_diff_index(text_to_complete + 1,
cmpl_state->user_directories
[cmpl_state->user_completion_index].login);
switch(index)
{
case PATTERN_MATCH:
break;
default:
if(cmpl_state->last_valid_char < (index + 1))
cmpl_state->last_valid_char = index + 1;
cmpl_state->user_completion_index += 1;
continue;
}
cmpl_state->the_completion.is_a_completion = 1;
cmpl_state->the_completion.is_directory = TRUE;
append_completion_text("~", cmpl_state);
append_completion_text(cmpl_state->
user_directories[cmpl_state->user_completion_index].login,
cmpl_state);
return append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
}
if(text_to_complete[1] ||
cmpl_state->user_completion_index > cmpl_state->user_directories_len)
{
cmpl_state->user_completion_index = -1;
return NULL;
}
else
{
cmpl_state->user_completion_index += 1;
cmpl_state->the_completion.is_a_completion = 1;
cmpl_state->the_completion.is_directory = TRUE;
return append_completion_text("~" G_DIR_SEPARATOR_S, cmpl_state);
}
}
#endif
#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
#define FOLD(c) (tolower(c))
#else
#define FOLD(c) (c)
#endif
/* returns the index (>= 0) of the first differing character,
* PATTERN_MATCH if the completion matches */
static gint
first_diff_index(gchar* pat, gchar* text)
{
gint diff = 0;
while(*pat && *text && FOLD(*text) == FOLD(*pat))
{
pat += 1;
text += 1;
diff += 1;
}
if(*pat)
return diff;
return PATTERN_MATCH;
}
static PossibleCompletion*
append_completion_text(gchar* text, CompletionState* cmpl_state)
{
gint len, i = 1;
if(!cmpl_state->the_completion.text)
return NULL;
len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
if(cmpl_state->the_completion.text_alloc > len)
{
strcat(cmpl_state->the_completion.text, text);
return &cmpl_state->the_completion;
}
while(i < len) { i <<= 1; }
cmpl_state->the_completion.text_alloc = i;
cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
if(!cmpl_state->the_completion.text)
return NULL;
else
{
strcat(cmpl_state->the_completion.text, text);
return &cmpl_state->the_completion;
}
}
static CompletionDir*
find_completion_dir(gchar* text_to_complete,
gchar** remaining_text,
CompletionState* cmpl_state)
{
gchar* first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
CompletionDir* dir = cmpl_state->reference_dir;
CompletionDir* next;
*remaining_text = text_to_complete;
while(first_slash)
{
gint len = first_slash - *remaining_text;
gint found = 0;
gchar *found_name = NULL; /* Quiet gcc */
gint i;
gchar* pat_buf = g_new (gchar, len + 1);
strncpy(pat_buf, *remaining_text, len);
pat_buf[len] = 0;
for(i = 0; i < dir->sent->entry_count; i += 1)
{
if(dir->sent->entries[i].is_dir &&
fnmatch(pat_buf, dir->sent->entries[i].entry_name,
FNMATCH_FLAGS)!= FNM_NOMATCH)
{
if(found)
{
g_free (pat_buf);
return dir;
}
else
{
found = 1;
found_name = dir->sent->entries[i].entry_name;
}
}
}
if (!found)
{
/* Perhaps we are trying to open an automount directory */
found_name = pat_buf;
}
next = open_relative_dir(found_name, dir, cmpl_state);
if(!next)
{
g_free (pat_buf);
return NULL;
}
next->cmpl_parent = dir;
dir = next;
if(!correct_dir_fullname(dir))
{
g_free(pat_buf);
return NULL;
}
*remaining_text = first_slash + 1;
first_slash = strchr(*remaining_text, G_DIR_SEPARATOR);
g_free (pat_buf);
}
return dir;
}
static void
update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
{
gint cmpl_len;
if(!poss || !cmpl_is_a_completion(poss))
return;
cmpl_len = strlen(cmpl_this_completion(poss));
if(cmpl_state->updated_text_alloc < cmpl_len + 1)
{
cmpl_state->updated_text =
(gchar*)g_realloc(cmpl_state->updated_text,
cmpl_state->updated_text_alloc);
cmpl_state->updated_text_alloc = 2*cmpl_len;
}
if(cmpl_state->updated_text_len < 0)
{
strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
cmpl_state->updated_text_len = cmpl_len;
cmpl_state->re_complete = cmpl_is_directory(poss);
}
else if(cmpl_state->updated_text_len == 0)
{
cmpl_state->re_complete = FALSE;
}
else
{
gint first_diff =
first_diff_index(cmpl_state->updated_text,
cmpl_this_completion(poss));
cmpl_state->re_complete = FALSE;
if(first_diff == PATTERN_MATCH)
return;
if(first_diff > cmpl_state->updated_text_len)
strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
cmpl_state->updated_text_len = first_diff;
cmpl_state->updated_text[first_diff] = 0;
}
}
static PossibleCompletion*
attempt_file_completion(CompletionState *cmpl_state)
{
gchar *pat_buf, *first_slash;
CompletionDir *dir = cmpl_state->active_completion_dir;
dir->cmpl_index += 1;
if(dir->cmpl_index == dir->sent->entry_count)
{
if(dir->cmpl_parent == NULL)
{
cmpl_state->active_completion_dir = NULL;
return NULL;
}
else
{
cmpl_state->active_completion_dir = dir->cmpl_parent;
return attempt_file_completion(cmpl_state);
}
}
g_assert(dir->cmpl_text);
first_slash = strchr(dir->cmpl_text, G_DIR_SEPARATOR);
if(first_slash)
{
gint len = first_slash - dir->cmpl_text;
pat_buf = g_new (gchar, len + 1);
strncpy(pat_buf, dir->cmpl_text, len);
pat_buf[len] = 0;
}
else
{
gint len = strlen(dir->cmpl_text);
pat_buf = g_new (gchar, len + 2);
strcpy(pat_buf, dir->cmpl_text);
/* Don't append a * if the user entered one herself.
* This way one can complete *.h and don't get matches
* on any .help files, for instance.
*/
if (strchr(pat_buf, '*') == NULL)
strcpy(pat_buf + len, "*");
}
if(first_slash)
{
if(dir->sent->entries[dir->cmpl_index].is_dir)
{
if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
FNMATCH_FLAGS) != FNM_NOMATCH)
{
CompletionDir* new_dir;
new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
dir, cmpl_state);
if(!new_dir)
{
g_free (pat_buf);
return NULL;
}
new_dir->cmpl_parent = dir;
new_dir->cmpl_index = -1;
new_dir->cmpl_text = first_slash + 1;
cmpl_state->active_completion_dir = new_dir;
g_free (pat_buf);
return attempt_file_completion(cmpl_state);
}
else
{
g_free (pat_buf);
return attempt_file_completion(cmpl_state);
}
}
else
{
g_free (pat_buf);
return attempt_file_completion(cmpl_state);
}
}
else
{
if(dir->cmpl_parent != NULL)
{
append_completion_text(dir->fullname +
strlen(cmpl_state->completion_dir->fullname) + 1,
cmpl_state);
append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
}
append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
cmpl_state->the_completion.is_a_completion =
(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
FNMATCH_FLAGS) != FNM_NOMATCH);
cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
if(dir->sent->entries[dir->cmpl_index].is_dir)
append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
g_free (pat_buf);
return &cmpl_state->the_completion;
}
}
#ifdef HAVE_PWD_H
static gint
get_pwdb(CompletionState* cmpl_state)
{
struct passwd *pwd_ptr;
gchar* buf_ptr;
gint len = 0, i, count = 0;
if(cmpl_state->user_dir_name_buffer)
return TRUE;
setpwent ();
while ((pwd_ptr = getpwent()) != NULL)
{
len += strlen(pwd_ptr->pw_name);
len += strlen(pwd_ptr->pw_dir);
len += 2;
count += 1;
}
setpwent ();
cmpl_state->user_dir_name_buffer = g_new(gchar, len);
cmpl_state->user_directories = g_new(CompletionUserDir, count);
cmpl_state->user_directories_len = count;
buf_ptr = cmpl_state->user_dir_name_buffer;
for(i = 0; i < count; i += 1)
{
pwd_ptr = getpwent();
if(!pwd_ptr)
{
cmpl_errno = errno;
goto error;
}
strcpy(buf_ptr, pwd_ptr->pw_name);
cmpl_state->user_directories[i].login = buf_ptr;
buf_ptr += strlen(buf_ptr);
buf_ptr += 1;
strcpy(buf_ptr, pwd_ptr->pw_dir);
cmpl_state->user_directories[i].homedir = buf_ptr;
buf_ptr += strlen(buf_ptr);
buf_ptr += 1;
}
qsort(cmpl_state->user_directories,
cmpl_state->user_directories_len,
sizeof(CompletionUserDir),
compare_user_dir);
endpwent();
return TRUE;
error:
if(cmpl_state->user_dir_name_buffer)
g_free(cmpl_state->user_dir_name_buffer);
if(cmpl_state->user_directories)
g_free(cmpl_state->user_directories);
cmpl_state->user_dir_name_buffer = NULL;
cmpl_state->user_directories = NULL;
return FALSE;
}
static gint
compare_user_dir(const void* a, const void* b)
{
return strcmp((((CompletionUserDir*)a))->login,
(((CompletionUserDir*)b))->login);
}
#endif
static gint
compare_cmpl_dir(const void* a, const void* b)
{
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
return strcmp((((CompletionDirEntry*)a))->entry_name,
(((CompletionDirEntry*)b))->entry_name);
#else
return g_strcasecmp((((CompletionDirEntry*)a))->entry_name,
(((CompletionDirEntry*)b))->entry_name);
#endif
}
static gint
cmpl_state_okay(CompletionState* cmpl_state)
{
return cmpl_state && cmpl_state->reference_dir;
}
static gchar*
cmpl_strerror(gint err)
{
if(err == CMPL_ERRNO_TOO_LONG)
return "Name too long";
else
return g_strerror (err);
}