[9404] in The GTK GIMP ToolKit mailing list archive

home help back first fref pref prev next nref lref last post

[gtk-list] Re: Auto-indent with GtkText?

daemon@ATHENA.MIT.EDU (Thomas Mailund Jensen)
Mon Oct 19 07:21:57 1998

To: gtk-list@redhat.com
From: Thomas Mailund Jensen <mailund@daimi.au.dk>
Date: 19 Oct 1998 13:21:30 +0200
In-Reply-To: "Curtiss Howard"'s message of "Mon, 19 Oct 1998 03:36:52 PDT"
Resent-From: gtk-list@redhat.com
Reply-To: gtk-list@redhat.com

>>>>> "CH" == Curtiss Howard <po_boxx_823@hotmail.com> writes:

 CH> Does anyone know a simple way to do auto-indent with a GtkText
 CH> widget?

That depends on what you mean by "auto-indent".  If you simply mean
inserting one or more tabs at the beginning of each new line you could
catch the keyevent and listen for enter as in the attached example.

If on the other hand you're thinking of something like automatic
indention of sourcecode according to syntactic nesting of blocks and
the like you need to be a little more clever...

	/mailund


===File ~/tmp/bar.c=========================================
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>	/* to get key constants */

static gint
key_pressed (GtkWidget *text, GdkEventKey *ev)
{
  g_return_val_if_fail (text != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
  g_return_val_if_fail (ev != NULL, FALSE);


  if (ev->keyval == GDK_Return) {

    /* here you could do all kinds of clever stuff */
    gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL,
		     "\n\t", -1);

    gtk_signal_emit_stop_by_name (GTK_OBJECT (text), "key_press_event");
    return TRUE;
  }

  return FALSE;
}

static void
clicked (GtkWidget *wid, gchar *button)
{
  g_print ("%s was clicked\n", button);
}

int
main (int argc, char *argv[]) 
{
  GtkWidget *window;
  GtkWidget *text;

  /* initialize */
  gtk_init (&argc, &argv);

  /* create window */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  text = gtk_text_new (NULL, NULL);
  gtk_text_set_editable (GTK_TEXT (text), TRUE);
  gtk_container_add (GTK_CONTAINER (window), text);

  /* setup signal handler */
  gtk_signal_connect (GTK_OBJECT (text), "key_press_event",
		      GTK_SIGNAL_FUNC (key_pressed), NULL);

  /* show */
  gtk_widget_show_all (window);

  /* main loop */
  gtk_main ();
 
  return 0;
}
============================================================

-- 
To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null


home help back first fref pref prev next nref lref last post