[libvtemm] Added new example.



commit b3e0246c63244dae89ce374aeb1d88c8d6686434
Author: Krzesimir Nowak <krnowak svn gnome org>
Date:   Tue May 19 16:54:17 2009 +0200

    Added new example.
    
    * configure.ac: Added example/gettexter/Makefile entry to
    AC_CONFIG_FILES.
    * examples/Makefile.am: Added gettexter to SUBDIRS.
    * examples/README: Added description about new example.
    * examples/gettexter/Makefile.am:
    * examples/gettexter/gettexter.cc:
    * examples/gettexter/gettexter.h:
    * examples/gettexter/gtterminal.cc:
    * examples/gettexter/gtterminal.h:
    * examples/gettexter/main.cc: New example.
---
 configure.ac                     |    3 +-
 examples/Makefile.am             |    2 +-
 examples/README                  |    1 +
 examples/gettexter/Makefile.am   |   10 +++
 examples/gettexter/gettexter.cc  |  115 ++++++++++++++++++++++++++++++++++++++
 examples/gettexter/gettexter.h   |   51 +++++++++++++++++
 examples/gettexter/gtterminal.cc |   76 +++++++++++++++++++++++++
 examples/gettexter/gtterminal.h  |   40 +++++++++++++
 examples/gettexter/main.cc       |   33 +++++++++++
 9 files changed, 329 insertions(+), 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index 46350e3..0bd7443 100644
--- a/configure.ac
+++ b/configure.ac
@@ -143,11 +143,12 @@ AC_CONFIG_FILES([
   scripts/Makefile
 
   docs/Makefile
-  docs/reference/Makefile
+    docs/reference/Makefile
     docs/reference/Doxyfile
 
   examples/Makefile
     examples/simple/Makefile
+    examples/gettexter/Makefile
 
   old_news_and_changelogs/Makefile
 ])
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 34252df..ad8e05d 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,7 +1,7 @@
 ## Copyright (c) 2008
 ## The libvtemm development team. (based on libgdamm development team's work)
 
-SUBDIRS = simple
+SUBDIRS = simple gettexter
 
 EXTRA_DIST = Makefile.am_fragment README
 
diff --git a/examples/README b/examples/README
index 8b5d2da..69de8bd 100644
--- a/examples/README
+++ b/examples/README
@@ -1 +1,2 @@
 simple: Displays a terminal with a shell taken from user's passwd.
+gettexter: Silly program showing how to use feed and get_text.
diff --git a/examples/gettexter/Makefile.am b/examples/gettexter/Makefile.am
new file mode 100644
index 0000000..7233232
--- /dev/null
+++ b/examples/gettexter/Makefile.am
@@ -0,0 +1,10 @@
+include $(top_srcdir)/examples/Makefile.am_fragment
+
+#Build the executable, but don't install it.
+noinst_PROGRAMS = example
+
+example_SOURCES = main.cc gettexter.cc gettexter.h gtterminal.cc gtterminal.h
+
+
+
+-include $(top_srcdir)/git.mk
diff --git a/examples/gettexter/gettexter.cc b/examples/gettexter/gettexter.cc
new file mode 100644
index 0000000..4e4cd2b
--- /dev/null
+++ b/examples/gettexter/gettexter.cc
@@ -0,0 +1,115 @@
+/* gettexter.cc
+ *
+ * Copyright (C) 2008, 2009 libvtemm Development Team
+ *
+ * This file is part of GetTexter Example.
+ *
+ * GetTexter Example is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GetTexter Example 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GetTexter Example.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <iostream>
+
+#include "gettexter.h"
+
+GetTexter::GetTexter()
+:
+  m_main_box(),
+  m_term_box(),
+  m_terminal(),
+  m_scrollbar(m_terminal.get_adjustment()),
+  m_label(),
+  m_entry(),
+  m_button_box(Gtk::BUTTONBOX_SPREAD),
+  m_get_every_second_button("Get every second"),
+  m_get_only_a_button("Get only 'a'")
+{
+  // Pack everything.
+  add(m_main_box);
+  
+  m_main_box.pack_start(m_term_box);
+  m_main_box.pack_start(m_label, false, true);
+  m_main_box.pack_start(m_entry, false, true);
+  m_main_box.pack_start(m_button_box, false, true);
+  
+  m_term_box.pack_start(m_terminal);
+  m_term_box.pack_start(m_scrollbar);
+  
+  m_button_box.pack_start(m_get_every_second_button);
+  m_button_box.pack_start(m_get_only_a_button);
+  // Set geometry hints, so resizing will work nicely.
+  Gdk::Geometry hints;
+  Gnome::Vte::Padding pads(m_terminal.get_padding());
+  hints.base_width = pads.get_x_pad();
+  hints.base_height = pads.get_y_pad();
+  hints.width_inc = m_terminal.get_char_width();
+  hints.height_inc = m_terminal.get_char_height();
+  const int min_width_chars = 4;
+  const int min_height_chars = 2;
+  hints.min_width = hints.base_width + hints.width_inc * min_width_chars;
+  hints.min_height = hints.base_height + hints.height_inc * min_height_chars;
+  set_geometry_hints(m_terminal, hints, Gdk::HINT_RESIZE_INC | Gdk::HINT_MIN_SIZE | Gdk::HINT_BASE_SIZE);
+  // Set focus and default action on entry.
+  m_entry.set_flags(Gtk::CAN_FOCUS | Gtk::CAN_DEFAULT);
+  m_entry.grab_focus();
+  m_entry.grab_default();
+  // Set activation signal on entry (it will be executed when enter or return
+  // is pressed).
+  m_entry.signal_activate().connect(sigc::mem_fun(*this, &GetTexter::on_entry_activated));
+  // Connect signals to buttons.
+  m_get_every_second_button.signal_clicked().connect(sigc::mem_fun(*this, &GetTexter::on_get_every_second_button_clicked));
+  m_get_only_a_button.signal_clicked().connect(sigc::mem_fun(*this, &GetTexter::on_get_only_a_button_clicked));
+  
+  set_title("GetTexter example");
+  
+  show_all_children();
+}
+
+GetTexter::~GetTexter()
+{}
+
+// Gnome::Vte::Terminal::get_text() gets whole screen, so it has lots of
+// newlines, which are stripped here.
+void
+GetTexter::on_get_every_second_button_clicked()
+{
+  m_label.set_text(m_terminal.get_every_second());
+}
+
+// This method use Gnome::Vte::Terminal::get_text and count number of 'a'
+// occurences and sets label with string consisting of that number of 'a's.
+void
+GetTexter::on_get_only_a_button_clicked()
+{
+  Glib::ustring text(m_terminal.get_text(sigc::ptr_fun(&Gnome::Vte::Terminal::always_selected)).get_text());
+  unsigned int counter = 0;
+  unsigned int text_len = text.size();
+  for (unsigned iter = 0; iter < text_len; iter++)
+  {
+    if (text[iter] == 'a')
+    {
+      counter++;
+    }
+  }
+  m_label.set_text(Glib::ustring(counter, 'a'));
+}
+
+// Appends carriage return and newline to text from entry and feeds the terminal
+// with it. Without carriage return cursor will move to cell (row + 1, column)
+// instead of cell (row + 1, 0).
+void
+GetTexter::on_entry_activated()
+{
+  m_terminal.feed(m_entry.get_text() + "\r\n");
+  m_entry.set_text(Glib::ustring());
+}
diff --git a/examples/gettexter/gettexter.h b/examples/gettexter/gettexter.h
new file mode 100644
index 0000000..f77a196
--- /dev/null
+++ b/examples/gettexter/gettexter.h
@@ -0,0 +1,51 @@
+/* gettexter.h
+ *
+ * Copyright (C) 2008, 2009 libvtemm Development Team
+ *
+ * This file is part of GetTexter Example.
+ *
+ * GetTexter Example is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GetTexter Example 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GetTexter Example.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _LIBVTEMM_EXAMPLE_GETTEXTER_H_
+#define _LIBVTEMM_EXAMPLE_GETTEXTER_H_
+
+#include <gtkmm.h>
+
+#include "gtterminal.h"
+
+class GetTexter : public Gtk::Window
+{
+public:
+  GetTexter();
+  virtual ~GetTexter();
+protected:
+  // Signal handlers:
+  virtual void on_get_every_second_button_clicked();
+  virtual void on_get_only_a_button_clicked();
+  virtual void on_entry_activated();
+private:
+  // Member widgets:
+  Gtk::VBox m_main_box;
+  Gtk::HBox m_term_box;
+  GtTerminal m_terminal;
+  Gtk::VScrollbar m_scrollbar;
+  Gtk::Label m_label;
+  Gtk::Entry m_entry;
+  Gtk::HButtonBox m_button_box;
+  Gtk::Button m_get_every_second_button;
+  Gtk::Button m_get_only_a_button;
+};
+
+#endif // _LIBVTEMM_EXAMPLE_GETTEXTER_H_
diff --git a/examples/gettexter/gtterminal.cc b/examples/gettexter/gtterminal.cc
new file mode 100644
index 0000000..e644448
--- /dev/null
+++ b/examples/gettexter/gtterminal.cc
@@ -0,0 +1,76 @@
+/* gtterminal.cc
+ *
+ * Copyright (C) 2008, 2009 libvtemm Development Team
+ *
+ * This file is part of GetTexter Example.
+ *
+ * GetTexter Example is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GetTexter Example 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GetTexter Example.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gtterminal.h"
+
+GtTerminal::GtTerminal()
+:
+  Gnome::Vte::Terminal(),
+  counter(0)
+{
+  set_size(80, 24);
+}
+
+GtTerminal::~GtTerminal()
+{}
+
+// Returns true for every second cell.
+bool
+GtTerminal::on_get_every_second(long, long)
+{
+  bool odd = false;
+  if (counter % 2)
+  {
+    odd = true;
+  }
+  else
+  {
+    odd = false;
+  }
+  counter++;
+  return odd;
+}
+
+Glib::ustring
+GtTerminal::get_every_second()
+{
+  Glib::ustring text(get_text(sigc::mem_fun(*this, &GtTerminal::on_get_every_second)).get_text());
+  unsigned int text_len = text.size();
+  unsigned int to_erase = 0;
+  for (unsigned int iter = text_len - 1; true; iter--)
+  {
+    gunichar uc = text[iter];
+    if ((uc == '\n') || (uc == '\r'))
+    {
+    	to_erase++;
+    }
+    else if (to_erase)
+    {
+    	text.erase(iter + 1, to_erase);
+    	to_erase = 0;
+    }
+    if (!iter)
+    {
+    	break;
+    }
+  }
+  counter = 0;
+  return text;
+}
diff --git a/examples/gettexter/gtterminal.h b/examples/gettexter/gtterminal.h
new file mode 100644
index 0000000..df47a96
--- /dev/null
+++ b/examples/gettexter/gtterminal.h
@@ -0,0 +1,40 @@
+/* gtterminal.h
+ *
+ * Copyright (C) 2008, 2009 libvtemm Development Team
+ *
+ * This file is part of GetTexter Example.
+ *
+ * GetTexter Example is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GetTexter Example 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GetTexter Example.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _LIBVTEMM_EXAMPLE_GTTERMINAL_H_
+#define _LIBVTEMM_EXAMPLE_GTTERMINAL_H_
+
+#include <gtkmm.h>
+#include <libvtemm/terminal.h>
+
+class GtTerminal : public Gnome::Vte::Terminal
+{
+public:
+  GtTerminal();
+  virtual ~GtTerminal();
+  Glib::ustring get_every_second();
+protected:
+  // Method passed to get_text method.
+  virtual bool on_get_every_second(long, long);
+private:
+  unsigned int counter;
+};
+
+#endif // _LIBVTEMM_EXAMPLE_GTTERMINAL_H_
diff --git a/examples/gettexter/main.cc b/examples/gettexter/main.cc
new file mode 100644
index 0000000..a9f9566
--- /dev/null
+++ b/examples/gettexter/main.cc
@@ -0,0 +1,33 @@
+/* main.cc
+ *
+ * Copyright (C) 2008, 2009 libvtemm Development Team
+ *
+ * This file is part of GetTexter Example.
+ *
+ * GetTexter Example is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GetTexter Example 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GetTexter Example.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtkmm.h>
+#include <libvtemm/init.h>
+
+#include "gettexter.h"
+
+int main(int argc, char *argv[])
+{
+  Gtk::Main kit(argc, argv);
+  Gnome::Vte::init();
+  GetTexter window;
+  Gtk::Main::run(window);
+  return 0;
+}



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