Re: Auto-indent with GtkText?



>>>>> "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;
}
============================================================



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]