User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 373,565 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,828 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Jul 12th, 2007
Views: 1,919
A small demo about how to use GTK label as a simple console. To compile GTK program, `pkg-config gtk+-2.0 --cflags --libs` should finally be somewhere in the compiler command line in the bash-like shell.
c Syntax | 5 stars
  1. #include <gtk/gtk.h>
  2. #include <gdk/gdkkeysyms.h>
  3. #include <string.h>
  4. #define MX 41
  5. #define MY 12
  6.  
  7. int keyb [MX], quit;
  8.  
  9. gint key_press_event (GtkWidget *widget, GdkEventKey *event)
  10. {
  11. int i = MX - 1;
  12.  
  13. if (event->keyval && !keyb [MX - 1]) {
  14. for (; i; i--) keyb [i] = keyb [i - 1];
  15. *keyb = event->keyval;
  16. }
  17. return 1;
  18. }
  19.  
  20. gint delete_event (GtkWidget *widget, gpointer data)
  21. {
  22. widget = widget;
  23. data = data;
  24. return 0;
  25. }
  26.  
  27. void destroy (GtkWidget *widget, gpointer data)
  28. {
  29. widget = widget;
  30. data = data;
  31. quit = 1;
  32. }
  33.  
  34. int main (int argc, char **argv)
  35. {
  36. char buf [MY] [MX];
  37. int x = 0, y = 0, i = 0, j;
  38. GdkColor white = {0, 0xffff, 0xffff, 0xffff},
  39. blue = {0, 0, 0, 0xffff};
  40. GtkWidget *window, *label;
  41.  
  42. gtk_init (&argc, &argv);
  43. window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  44. gtk_widget_set_size_request (window, 800, 500);
  45. gtk_window_set_title (GTK_WINDOW (window), "GTK Label");
  46. gtk_window_set_resizable (GTK_WINDOW (window), 0);
  47. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  48. gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &white);
  49. g_signal_connect (window, "delete_event",
  50. G_CALLBACK (delete_event), NULL);
  51. g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
  52. gtk_widget_set_events (window, GDK_KEY_PRESS_MASK);
  53. g_signal_connect (G_OBJECT (window), "key_press_event",
  54. G_CALLBACK (key_press_event), NULL);
  55. label = gtk_label_new ("");
  56. gtk_container_add (GTK_CONTAINER (window), label);
  57. gtk_widget_modify_font (label,
  58. pango_font_description_from_string ("dejavu sans mono 24"));
  59. gtk_widget_modify_fg (label, GTK_STATE_NORMAL, &blue);
  60. gtk_widget_show_all (window);
  61. for (memset (*buf, ' ', MX * MY); i < MY; i++)
  62. buf [i] [MX - 1] = '\n';
  63. buf [MY - 1] [MX - 1] = 0;
  64. for (**buf = '_'; !quit; ) {
  65. if (*keyb) {
  66. for (i = 0; keyb [i] && i < MX; i++);
  67. if (keyb [--i] >= 0x020 && keyb [i] < 0x0b0) {
  68. buf [y] [x] = keyb [i];
  69. if (x < MX - 2) x++;
  70. } else
  71. buf [y] [x] = ' ';
  72. switch (keyb [i]) {
  73. case GDK_Left:
  74. x && x--;
  75. break;
  76. case GDK_Right:
  77. if (x < MX - 2) x++;
  78. break;
  79. case GDK_Up:
  80. y && y--;
  81. break;
  82. case GDK_Return:
  83. x = 0;
  84. case GDK_Down:
  85. if (y < MY - 1) y++;
  86. break;
  87. }
  88. keyb [i] = 0;
  89. buf [y] [x] = '_';
  90. gtk_label_set_text (GTK_LABEL (label), *buf);
  91. }
  92. g_usleep (100000l);
  93. while (g_main_context_iteration (NULL, 0));
  94. }
  95. return 0;
  96. }
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 7:28 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC