[gtkmm-documentation] Code style changes to the new keyboard events examples.



commit a7af3315d2e10b870624e955ada53c62e184e150
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Nov 18 10:06:28 2011 +0100

    Code style changes to the new keyboard events examples.
    
    * examples/book/keyboard_events: Rename files and change them to be consistent
      with the code style used in other examples.

 ChangeLog                                          |    7 +
 examples/Makefile.am                               |    8 +-
 examples/book/iconview/main.cc                     |    1 +
 .../propagation/event_propagation.cc               |  122 --------------------
 .../keyboard_events/propagation/examplewindow.cc   |   89 ++++++++++++++
 .../{event_propagation.h => examplewindow.h}       |   29 ++---
 examples/book/keyboard_events/propagation/main.cc  |   16 ++--
 .../book/keyboard_events/simple/examplewindow.cc   |   77 ++++++++++++
 .../simple/{keyboard_events.h => examplewindow.h}  |   33 ++---
 .../book/keyboard_events/simple/keyboard_events.cc |  100 ----------------
 examples/book/keyboard_events/simple/main.cc       |   16 ++--
 11 files changed, 219 insertions(+), 279 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index de4da74..8e69fdc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-11-18  Murray Cumming  <murrayc murrayc com>
+
+	Code style changes to the new keyboard events examples.
+
+	* examples/book/keyboard_events: Rename files and change them to be consistent
+  with the code style used in other examples.
+
 2011-11-01  Pedro Ferreira <darkiiiiii gmail com>
 
 	New chapter: Keyboard Events
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 498605c..20c0aa0 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -408,13 +408,13 @@ book_input_example_SOURCES = \
 	book/input/main.cc
 
 book_keyboard_events_simple_example_SOURCES =		\
-	book/keyboard_events/simple/keyboard_events.cc	\
-	book/keyboard_events/simple/keyboard_events.h	\
+	book/keyboard_events/simple/examplewindow.cc	\
+	book/keyboard_events/simple/examplewindow.h	\
 	book/keyboard_events/simple/main.cc
 
 book_keyboard_events_propagation_example_SOURCES =		\
-	book/keyboard_events/propagation/event_propagation.cc	\
-	book/keyboard_events/propagation/event_propagation.h	\
+	book/keyboard_events/propagation/examplewindow.cc	\
+	book/keyboard_events/propagation/examplewindow.h	\
 	book/keyboard_events/propagation/main.cc
 
 book_label_example_SOURCES =		\
diff --git a/examples/book/iconview/main.cc b/examples/book/iconview/main.cc
index 4620054..13ad846 100644
--- a/examples/book/iconview/main.cc
+++ b/examples/book/iconview/main.cc
@@ -24,6 +24,7 @@ int main(int argc, char *argv[])
   Gtk::Main kit(argc, argv);
 
   ExampleWindow window;
+
   //Shows the window and returns when it is closed.
   Gtk::Main::run(window);
 
diff --git a/examples/book/keyboard_events/propagation/examplewindow.cc b/examples/book/keyboard_events/propagation/examplewindow.cc
new file mode 100644
index 0000000..8e7d201
--- /dev/null
+++ b/examples/book/keyboard_events/propagation/examplewindow.cc
@@ -0,0 +1,89 @@
+/* gtkmm example Copyright (C) 2011 gtkmm development team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "examplewindow.h"
+
+ExampleWindow::ExampleWindow()
+{
+  add(m_container);
+  set_title("Event Propagation");
+  set_border_width(10);
+  
+  m_label.set_label("A label");
+  m_checkbutton_can_propagate.set_label("Can Propagate");
+  m_checkbutton_can_propagate.set_active();
+
+  // Main Container
+  m_container.set_orientation(Gtk::ORIENTATION_VERTICAL);
+  m_container.add(m_label);
+  m_container.add(m_entry);
+  m_container.add(m_checkbutton_can_propagate);
+
+  // Events
+  add_events(Gdk::KEY_RELEASE_MASK);
+
+  m_entry.signal_key_release_event().connect(
+    sigc::mem_fun(*this, &ExampleWindow::entryKeyRelease));
+
+  m_container.signal_key_release_event().connect(
+    sigc::mem_fun(*this, &ExampleWindow::gridKeyRelease));
+
+  signal_key_release_event().connect(
+    sigc::mem_fun(*this, &ExampleWindow::windowKeyRelease));
+
+  show_all_children();
+}
+
+//By changing the return value we allow, or don't allow, the event to propagate to other elements.
+bool ExampleWindow::entryKeyRelease(GdkEventKey* /* event */ )
+{
+  std::cout << "Entry" << std::endl;
+
+  if(m_checkbutton_can_propagate.get_active())
+  {
+    return false;
+  }
+
+  return true;
+}
+
+bool ExampleWindow::gridKeyRelease(GdkEventKey* /* event */ )
+{
+  std::cout << "Grid" << std::endl;
+
+  //Let it propagate:
+  return false;
+}
+
+// This will set the entry's text in the label, every time a key is pressed.
+bool ExampleWindow::windowKeyRelease(GdkEventKey* /* event */ )
+{
+  std::cout << "Window" << std::endl;
+
+  //checking if the entry is on focus, otherwise the label would get changed by pressing keys
+  //on the window (when the entry is not on focus), even if canPropagate wasn't active
+  if(m_entry.has_focus())
+  {
+    m_label.set_text(m_entry.get_text());
+  }
+
+  return true;
+}
+
+ExampleWindow::~ExampleWindow()
+{
+}
+
diff --git a/examples/book/keyboard_events/propagation/event_propagation.h b/examples/book/keyboard_events/propagation/examplewindow.h
similarity index 68%
rename from examples/book/keyboard_events/propagation/event_propagation.h
rename to examples/book/keyboard_events/propagation/examplewindow.h
index 8f8cd28..7fe0cde 100644
--- a/examples/book/keyboard_events/propagation/event_propagation.h
+++ b/examples/book/keyboard_events/propagation/examplewindow.h
@@ -20,30 +20,25 @@
 #include <gtkmm.h>
 #include <iostream>
 
-class EventPropagation : public Gtk::Window
+class ExampleWindow : public Gtk::Window
 {
-  public:
+public:
 
-    EventPropagation();
-    virtual ~EventPropagation();
+  ExampleWindow();
+  virtual ~ExampleWindow();
 
 
-  private:
+private:
 
-    bool entryKeyRelease (GdkEventKey *event);
+  bool entryKeyRelease(GdkEventKey *event);
+  bool gridKeyRelease(GdkEventKey *event);
+  bool windowKeyRelease(GdkEventKey *event);
 
-    bool gridKeyRelease (GdkEventKey *event);
-
-    bool windowKeyRelease (GdkEventKey *event);
-
-        //ui elements
-
-    Gtk::Grid m_container;
-
-    Gtk::Label m_label;
-    Gtk::Entry m_entry;
-    Gtk::CheckButton m_canPropagate;
+  Gtk::Grid m_container;
 
+  Gtk::Label m_label;
+  Gtk::Entry m_entry;
+  Gtk::CheckButton m_checkbutton_can_propagate;
 };
 
 
diff --git a/examples/book/keyboard_events/propagation/main.cc b/examples/book/keyboard_events/propagation/main.cc
index 80ccf93..a352431 100644
--- a/examples/book/keyboard_events/propagation/main.cc
+++ b/examples/book/keyboard_events/propagation/main.cc
@@ -11,20 +11,20 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
-#include "event_propagation.h"
+#include "examplewindow.h"
+#include <gtkmm/main.h>
 
-
-int main (int argc, char** argv)
+int main(int argc, char *argv[])
 {
-  Gtk::Main kit (argc, argv);
-
+  Gtk::Main kit(argc, argv);
 
-  EventPropagation mainWindow;
+  ExampleWindow window;
 
-  Gtk::Main::run (mainWindow);
+  //Shows the window and returns when it is closed.
+  Gtk::Main::run(window);
 
   return 0;
 }
diff --git a/examples/book/keyboard_events/simple/examplewindow.cc b/examples/book/keyboard_events/simple/examplewindow.cc
new file mode 100644
index 0000000..0b60e69
--- /dev/null
+++ b/examples/book/keyboard_events/simple/examplewindow.cc
@@ -0,0 +1,77 @@
+/* gtkmm example Copyright (C) 2011 gtkmm development team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "examplewindow.h"
+
+ExampleWindow::ExampleWindow()
+{
+  set_title("Keyboard Events");
+  set_border_width(10);
+  add(m_container);
+  
+  // Radio buttons:
+  m_first.set_label("First");
+  m_second.set_label("Second");
+
+  Gtk::RadioButton::Group group = m_first.get_group();
+  m_second.set_group(group);
+  m_first.set_active();
+
+  // Main Container:
+  m_container.add(m_first);
+  m_container.add(m_second);
+
+  // Events
+  add_events(Gdk::KEY_PRESS_MASK);
+  signal_key_press_event().connect(sigc::mem_fun(*this,
+    &ExampleWindow::on_key_press_event));
+
+  show_all_children();
+}
+
+bool ExampleWindow::on_key_press_event(GdkEventKey *event)
+{
+  //GDK_MOD1_MASK -> the 'alt' key(mask)
+  //GDK_KEY_1 -> the '1' key
+  //GDK_KEY_2 -> the '2' key
+
+  //select the first radio button, when we press alt + 1
+  if((event->keyval == GDK_KEY_1) &&
+    (event->state &(GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) == GDK_MOD1_MASK)
+  {
+    m_first.set_active();
+  }
+  else if((event->keyval == GDK_KEY_2) &&
+    (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) == GDK_MOD1_MASK)
+  {
+    //and the second radio button, when we press alt + 2
+    m_second.set_active();
+  }
+  else if(event->keyval == GDK_KEY_Escape)
+  {
+    //close the window, when the 'esc' key is pressed
+    hide();
+  }
+
+  //returning true, cancels the propagation of the event
+  return true;
+}
+
+
+ExampleWindow::~ExampleWindow()
+{
+}
+
diff --git a/examples/book/keyboard_events/simple/keyboard_events.h b/examples/book/keyboard_events/simple/examplewindow.h
similarity index 60%
rename from examples/book/keyboard_events/simple/keyboard_events.h
rename to examples/book/keyboard_events/simple/examplewindow.h
index 7683933..3a40c79 100644
--- a/examples/book/keyboard_events/simple/keyboard_events.h
+++ b/examples/book/keyboard_events/simple/examplewindow.h
@@ -6,7 +6,7 @@
  *
  * This program 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
+ * 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
@@ -14,33 +14,26 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#ifndef GTKMM_KEYBOARD_EVENTS_H
-#define GTKMM_KEYBOARD_EVENTS_H
+#ifndef GTKMM_EXAMPLEWINDOW_H
+#define GTKMM_EXAMPLEWINDOW_H
 
 #include <gtkmm.h>
 
 
-class KeyboardEvents : public Gtk::Window
+class ExampleWindow : public Gtk::Window
 {
-  public:
+public:
 
-    KeyboardEvents();
-    virtual ~KeyboardEvents();
+  ExampleWindow();
+  virtual ~ExampleWindow();
 
+private:
+  bool on_key_press_event(GdkEventKey *event);
 
-  private:
-
-    bool onKeyPress (GdkEventKey *event);
-
-
-        //ui related elements
-
-    Gtk::Grid m_container;
-
-    Gtk::RadioButton m_first;
-    Gtk::RadioButton m_second;
-
+  Gtk::Grid m_container;
+  Gtk::RadioButton m_first;
+  Gtk::RadioButton m_second;
 };
 
 
-#endif //GTKMM_KEYBOARD_EVENTS_H
+#endif //GTKMM_EXAMPLEWINDOW_H
diff --git a/examples/book/keyboard_events/simple/main.cc b/examples/book/keyboard_events/simple/main.cc
index ca870a8..a352431 100644
--- a/examples/book/keyboard_events/simple/main.cc
+++ b/examples/book/keyboard_events/simple/main.cc
@@ -11,20 +11,20 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
-#include "keyboard_events.h"
+#include "examplewindow.h"
+#include <gtkmm/main.h>
 
-
-int main (int argc, char** argv)
+int main(int argc, char *argv[])
 {
-  Gtk::Main kit (argc, argv);
-
+  Gtk::Main kit(argc, argv);
 
-  KeyboardEvents mainWindow;
+  ExampleWindow window;
 
-  Gtk::Main::run (mainWindow);
+  //Shows the window and returns when it is closed.
+  Gtk::Main::run(window);
 
   return 0;
 }



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