Sophie

Sophie

distrib > Fedora > 14 > x86_64 > by-pkgid > 499a8681be2b8e9cf362cecc58358ed3 > files > 58

libvtemm-docs-0.23.1-1.fc13.x86_64.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>libvtemm: gettexter/main.cc</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.1 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="contents">
<h1>gettexter/main.cc</h1><p>A silly application getting text. </p>
<div class="fragment"><pre class="fragment"><span class="comment">/* gtterminal.h</span>
<span class="comment"> *</span>
<span class="comment"> * Copyright (C) 2008, 2009 libvtemm Development Team</span>
<span class="comment"> *</span>
<span class="comment"> * This file is part of GetTexter Example.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is free software: you can redistribute it and/or modify</span>
<span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<span class="comment"> * the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> * (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is distributed in the hope that it will be useful,</span>
<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span class="comment"> * GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> * You should have received a copy of the GNU General Public License</span>
<span class="comment"> * along with GetTexter Example. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="comment"> */</span>

<span class="preprocessor">#ifndef _LIBVTEMM_EXAMPLE_GTTERMINAL_H_</span>
<span class="preprocessor"></span><span class="preprocessor">#define _LIBVTEMM_EXAMPLE_GTTERMINAL_H_</span>
<span class="preprocessor"></span>
<span class="preprocessor">#include &lt;gtkmm.h&gt;</span>
<span class="preprocessor">#include &lt;libvtemm/terminal.h&gt;</span>

<span class="keyword">class </span>GtTerminal : <span class="keyword">public</span> Gnome::Vte::Terminal
{
<span class="keyword">public</span>:
  GtTerminal();
  <span class="keyword">virtual</span> ~GtTerminal();
  <a name="_a0"></a><a class="codeRef" href="../../../glibmm-2.4/reference/html/classGlib_1_1ustring.html">Glib::ustring</a> get_every_second();
<span class="keyword">protected</span>:
  <span class="comment">// Method passed to get_text method.</span>
  <span class="keyword">virtual</span> <span class="keywordtype">bool</span> on_get_every_second(<span class="keywordtype">long</span>, <span class="keywordtype">long</span>);
<span class="keyword">private</span>:
  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> counter;
};

<span class="preprocessor">#endif // _LIBVTEMM_EXAMPLE_GTTERMINAL_H_</span>
</pre></div> <div class="fragment"><pre class="fragment"><span class="comment">/* gtterminal.cc</span>
<span class="comment"> *</span>
<span class="comment"> * Copyright (C) 2008, 2009 libvtemm Development Team</span>
<span class="comment"> *</span>
<span class="comment"> * This file is part of GetTexter Example.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is free software: you can redistribute it and/or modify</span>
<span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<span class="comment"> * the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> * (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is distributed in the hope that it will be useful,</span>
<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span class="comment"> * GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> * You should have received a copy of the GNU General Public License</span>
<span class="comment"> * along with GetTexter Example. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &quot;gtterminal.h&quot;</span>

GtTerminal::GtTerminal()
:
  Gnome::Vte::Terminal(),
  counter(0)
{
  set_size(80, 24);
}

GtTerminal::~GtTerminal()
{}

<span class="comment">// Returns true for every second cell.</span>
<span class="keywordtype">bool</span>
GtTerminal::on_get_every_second(<span class="keywordtype">long</span>, <span class="keywordtype">long</span>)
{
  <span class="keywordtype">bool</span> odd = <span class="keyword">false</span>;
  <span class="keywordflow">if</span> (counter % 2)
  {
    odd = <span class="keyword">true</span>;
  }
  <span class="keywordflow">else</span>
  {
    odd = <span class="keyword">false</span>;
  }
  counter++;
  <span class="keywordflow">return</span> odd;
}

<a class="codeRef" href="../../../glibmm-2.4/reference/html/classGlib_1_1ustring.html">Glib::ustring</a>
GtTerminal::get_every_second()
{
  <a class="codeRef" href="../../../glibmm-2.4/reference/html/classGlib_1_1ustring.html">Glib::ustring</a> text(get_text(<a name="a1"></a><a class="codeRef" href="../../../libsigc++-2.0/reference/html/group__mem__fun.html#ga7d9ea809173f48bf5c76cf1989591602">sigc::mem_fun</a>(*<span class="keyword">this</span>, &amp;GtTerminal::on_get_every_second)).get_text());
  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> text_len = text.size();
  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> to_erase = 0;
  <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> iter = text_len - 1; <span class="keyword">true</span>; iter--)
  {
    gunichar uc = text[iter];
    <span class="keywordflow">if</span> ((uc == <span class="charliteral">&apos;\n&apos;</span>) || (uc == <span class="charliteral">&apos;\r&apos;</span>))
    {
        to_erase++;
    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (to_erase)
    {
        text.erase(iter + 1, to_erase);
        to_erase = 0;
    }
    <span class="keywordflow">if</span> (!iter)
    {
        <span class="keywordflow">break</span>;
    }
  }
  counter = 0;
  <span class="keywordflow">return</span> text;
}
</pre></div> <div class="fragment"><pre class="fragment"><span class="comment">/* gettexter.h</span>
<span class="comment"> *</span>
<span class="comment"> * Copyright (C) 2008, 2009 libvtemm Development Team</span>
<span class="comment"> *</span>
<span class="comment"> * This file is part of GetTexter Example.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is free software: you can redistribute it and/or modify</span>
<span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<span class="comment"> * the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> * (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is distributed in the hope that it will be useful,</span>
<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span class="comment"> * GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> * You should have received a copy of the GNU General Public License</span>
<span class="comment"> * along with GetTexter Example. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="comment"> */</span>

<span class="preprocessor">#ifndef _LIBVTEMM_EXAMPLE_GETTEXTER_H_</span>
<span class="preprocessor"></span><span class="preprocessor">#define _LIBVTEMM_EXAMPLE_GETTEXTER_H_</span>
<span class="preprocessor"></span>
<span class="preprocessor">#include &lt;gtkmm.h&gt;</span>

<span class="preprocessor">#include &quot;gtterminal.h&quot;</span>

<span class="keyword">class </span>GetTexter : <span class="keyword">public</span> Gtk::Window
{
<span class="keyword">public</span>:
  GetTexter();
  <span class="keyword">virtual</span> ~GetTexter();
<span class="keyword">protected</span>:
  <span class="comment">// Signal handlers:</span>
  <span class="keyword">virtual</span> <span class="keywordtype">void</span> on_get_every_second_button_clicked();
  <span class="keyword">virtual</span> <span class="keywordtype">void</span> on_get_only_a_button_clicked();
  <span class="keyword">virtual</span> <span class="keywordtype">void</span> on_entry_activated();
<span class="keyword">private</span>:
  <span class="comment">// Member widgets:</span>
  <a name="_a2"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1VBox.html">Gtk::VBox</a> m_main_box;
  <a name="_a3"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1HBox.html">Gtk::HBox</a> m_term_box;
  GtTerminal m_terminal;
  <a name="_a4"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1VScrollbar.html">Gtk::VScrollbar</a> m_scrollbar;
  <a name="_a5"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1Label.html">Gtk::Label</a> m_label;
  <a name="_a6"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1Entry.html">Gtk::Entry</a> m_entry;
  <a name="_a7"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1HButtonBox.html">Gtk::HButtonBox</a> m_button_box;
  <a name="_a8"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1Button.html">Gtk::Button</a> m_get_every_second_button;
  <a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1Button.html">Gtk::Button</a> m_get_only_a_button;
};

<span class="preprocessor">#endif // _LIBVTEMM_EXAMPLE_GETTEXTER_H_</span>
</pre></div> <div class="fragment"><pre class="fragment"><span class="comment">/* gettexter.cc</span>
<span class="comment"> *</span>
<span class="comment"> * Copyright (C) 2008, 2009 libvtemm Development Team</span>
<span class="comment"> *</span>
<span class="comment"> * This file is part of GetTexter Example.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is free software: you can redistribute it and/or modify</span>
<span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<span class="comment"> * the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> * (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is distributed in the hope that it will be useful,</span>
<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span class="comment"> * GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> * You should have received a copy of the GNU General Public License</span>
<span class="comment"> * along with GetTexter Example. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &lt;<a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00901.html">iostream</a>&gt;</span>

<span class="preprocessor">#include &quot;gettexter.h&quot;</span>

GetTexter::GetTexter()
:
  m_main_box(),
  m_term_box(),
  m_terminal(),
  m_scrollbar(m_terminal.get_adjustment()),
  m_label(),
  m_entry(),
  m_button_box(Gtk::<a class="codeRef" href="../../../gtkmm-2.4/reference/html/namespaceGtk.html#af8380aa1633c0cc444fa28086237e056aedf018450094d8289348faa15491d6b2">BUTTONBOX_SPREAD</a>),
  m_get_every_second_button(<span class="stringliteral">&quot;Get every second&quot;</span>),
  m_get_only_a_button(<span class="stringliteral">&quot;Get only &apos;a&apos;&quot;</span>)
{
  <span class="comment">// Pack everything.</span>
  <a class="codeRef" href="../../../gtkmm-2.4/reference/html/namespaceGtk_1_1Stock.html#af5a3d62d679e3dd162bd77ac76205105">add</a>(m_main_box);
  
  m_main_box.pack_start(m_term_box);
  m_main_box.pack_start(m_label, <span class="keyword">false</span>, <span class="keyword">true</span>);
  m_main_box.pack_start(m_entry, <span class="keyword">false</span>, <span class="keyword">true</span>);
  m_main_box.pack_start(m_button_box, <span class="keyword">false</span>, <span class="keyword">true</span>);
  
  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);
  <span class="comment">// Set geometry hints, so resizing will work nicely.</span>
  <a class="codeRef" href="../../../gtkmm-2.4/reference/html/namespaceGdk.html#af6948e754cab4e79c6430fb94e438d83">Gdk::Geometry</a> hints;
  <a class="codeRef" href="../../../gtkmm-2.4/reference/html/namespaceGtk.html#a0aed0ea6fbd347e76872f23f9313545e">Gtk::Border</a> inner_border;
  m_terminal.get_style_property(<span class="stringliteral">&quot;inner-border&quot;</span>, inner_border);
  hints.base_width = inner_border.left + inner_border.right;
  hints.base_height = inner_border.top + inner_border.bottom;
  hints.width_inc = m_terminal.get_char_width();
  hints.height_inc = m_terminal.get_char_height();
  <span class="keyword">const</span> <span class="keywordtype">int</span> min_width_chars = 4;
  <span class="keyword">const</span> <span class="keywordtype">int</span> 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, <a name="a9"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/group__gdkmmEnums.html#gga7dd31e80216f3452a1e080bd22fcfd9cae225c378cd4334c2615a8435d7d0ad6a">Gdk::HINT_RESIZE_INC</a> | <a name="a10"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/group__gdkmmEnums.html#gga7dd31e80216f3452a1e080bd22fcfd9ca2acd5d248d5280290d4ef8a1669578af">Gdk::HINT_MIN_SIZE</a> | <a name="a11"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/group__gdkmmEnums.html#gga7dd31e80216f3452a1e080bd22fcfd9ca2ec7605e5e173f09ac7cc9c31e8f0c38">Gdk::HINT_BASE_SIZE</a>);
  <span class="comment">// Set focus and default action on entry.</span>
  m_entry.set_flags(<a name="a12"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/group__gtkmmEnums.html#gga3d42e9e6671e48764b3b42bcc8da0b47adb2ae38c34591e0b352336f576033ffe">Gtk::CAN_FOCUS</a> | <a name="a13"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/group__gtkmmEnums.html#gga3d42e9e6671e48764b3b42bcc8da0b47af4d68bafb695d9cdbfd69add0cdf475f">Gtk::CAN_DEFAULT</a>);
  m_entry.grab_focus();
  m_entry.grab_default();
  <span class="comment">// Set activation signal on entry (it will be executed when enter or return</span>
  <span class="comment">// is pressed).</span>
  m_entry.signal_activate().connect(<a class="codeRef" href="../../../libsigc++-2.0/reference/html/group__mem__fun.html#ga7d9ea809173f48bf5c76cf1989591602">sigc::mem_fun</a>(*<span class="keyword">this</span>, &amp;GetTexter::on_entry_activated));
  <span class="comment">// Connect signals to buttons.</span>
  m_get_every_second_button.signal_clicked().connect(<a class="codeRef" href="../../../libsigc++-2.0/reference/html/group__mem__fun.html#ga7d9ea809173f48bf5c76cf1989591602">sigc::mem_fun</a>(*<span class="keyword">this</span>, &amp;GetTexter::on_get_every_second_button_clicked));
  m_get_only_a_button.signal_clicked().connect(<a class="codeRef" href="../../../libsigc++-2.0/reference/html/group__mem__fun.html#ga7d9ea809173f48bf5c76cf1989591602">sigc::mem_fun</a>(*<span class="keyword">this</span>, &amp;GetTexter::on_get_only_a_button_clicked));
  
  set_title(<span class="stringliteral">&quot;GetTexter example&quot;</span>);
  
  show_all_children();
}

GetTexter::~GetTexter()
{}

<span class="comment">// Gnome::Vte::Terminal::get_text() gets whole screen, so it has lots of</span>
<span class="comment">// newlines, which are stripped here.</span>
<span class="keywordtype">void</span>
GetTexter::on_get_every_second_button_clicked()
{
  m_label.set_text(m_terminal.get_every_second());
}

<span class="comment">// This method use Gnome::Vte::Terminal::get_text and count number of &apos;a&apos;</span>
<span class="comment">// occurences and sets label with string consisting of that number of &apos;a&apos;s.</span>
<span class="keywordtype">void</span>
GetTexter::on_get_only_a_button_clicked()
{
  <a class="codeRef" href="../../../glibmm-2.4/reference/html/classGlib_1_1ustring.html">Glib::ustring</a> text(m_terminal.get_text(<a name="a14"></a><a class="codeRef" href="../../../libsigc++-2.0/reference/html/group__ptr__fun.html#gada8b678665c14dc85eb32d25b7299465">sigc::ptr_fun</a>(&amp;<a name="a15"></a><a class="code" href="classGnome_1_1Vte_1_1Terminal.html#a1dc87fc8645d9385f40c049ee6f2baa3" title="Convenience callback, which always returns true.">Gnome::Vte::Terminal::always_selected</a>)).get_text());
  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> counter = 0;
  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> text_len = text.size();
  <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> iter = 0; iter &lt; text_len; iter++)
  {
    <span class="keywordflow">if</span> (text[iter] == <span class="charliteral">&apos;a&apos;</span>)
    {
      counter++;
    }
  }
  m_label.set_text(<a class="codeRef" href="../../../glibmm-2.4/reference/html/classGlib_1_1ustring.html">Glib::ustring</a>(counter, <span class="charliteral">&apos;a&apos;</span>));
}

<span class="comment">// Appends carriage return and newline to text from entry and feeds the terminal</span>
<span class="comment">// with it. Without carriage return cursor will move to cell (row + 1, column)</span>
<span class="comment">// instead of cell (row + 1, 0).</span>
<span class="keywordtype">void</span>
GetTexter::on_entry_activated()
{
  m_terminal.feed(m_entry.get_text() + <span class="stringliteral">&quot;\r\n&quot;</span>);
  m_entry.set_text(<a class="codeRef" href="../../../glibmm-2.4/reference/html/classGlib_1_1ustring.html">Glib::ustring</a>());
}
</pre></div><div class="fragment"><pre class="fragment"><span class="comment">/* main.cc</span>
<span class="comment"> *</span>
<span class="comment"> * Copyright (C) 2008, 2009 libvtemm Development Team</span>
<span class="comment"> *</span>
<span class="comment"> * This file is part of GetTexter Example.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is free software: you can redistribute it and/or modify</span>
<span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<span class="comment"> * the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> * (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> * GetTexter Example is distributed in the hope that it will be useful,</span>
<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span class="comment"> * GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> * You should have received a copy of the GNU General Public License</span>
<span class="comment"> * along with GetTexter Example. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &lt;gtkmm.h&gt;</span>
<span class="preprocessor">#include &lt;libvtemm/init.h&gt;</span>

<span class="preprocessor">#include &quot;gettexter.h&quot;</span>

<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])
{
  <a name="_a16"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1Main.html">Gtk::Main</a> kit(argc, argv);
  Gnome::Vte::init();
  GetTexter window;
  <a name="a17"></a><a class="codeRef" href="../../../gtkmm-2.4/reference/html/classGtk_1_1Main.html#a6a8904c35952006d13cb07c1d0c16c10">Gtk::Main::run</a>(window);
  <span class="keywordflow">return</span> 0;
}
</pre></div> </div>
<hr size="1"/><address style="text-align: right;"><small>Generated on 14 Jan 2010 for libvtemm by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>
</body>
</html>