begin of simulator: window skeleton
Stefan Schuermans

Stefan Schuermans commited on 2017-06-07 21:38:03
Showing 8 changed files, with 475 additions and 0 deletions.

... ...
@@ -0,0 +1,5 @@
1
+/*~
2
+/*.d
3
+/*.glade.h
4
+/*.o
5
+/etherpix_sim
... ...
@@ -0,0 +1,51 @@
1
+# EtherPix simulator
2
+#
3
+# Copyright 2017 Stefan Schuermans <stefan schuermans info>
4
+
5
+SRCS_GLADE := $(wildcard *.glade)
6
+SRCS_CXX := $(wildcard *.cpp)
7
+TARGET := etherpix_sim
8
+
9
+PACKAGES := gtkmm-3.0
10
+
11
+CXX := g++
12
+
13
+DEFINES :=
14
+INCLUDE :=
15
+CFLAGS := -Wall -Wextra -Werror -g -O2 $(shell pkg-config --cflags $(PACKAGES))
16
+LDFLAGS := $(shell pkg-config --libs-only-L $(PACKAGES))
17
+LIBS := $(shell pkg-config --libs-only-l $(PACKAGES))
18
+
19
+GEN_HDRS=$(addsuffix .h,$(SRCS_GLADE))
20
+
21
+SRCS_BASE := $(SRCS_CXX:.cpp=)
22
+DEPS := $(addsuffix .d,$(SRCS_BASE))
23
+OBJS := $(addsuffix .o,$(SRCS_BASE))
24
+
25
+.PHONY: all clean
26
+.SUFFIXES:
27
+.SECONDARY:
28
+
29
+all: $(GEN_HDRS) $(DEPS) $(TARGET)
30
+
31
+ifneq ($(MAKECMDGOALS),clean)
32
+  -include $(DEPS)
33
+endif
34
+
35
+%.glade.h: %.glade Makefile
36
+	echo "static const char *widgets_$* =" >$@
37
+	sed 's/["]/\\&/g;s/^/  "/;s/$$/\\n"/' <$< >>$@
38
+	echo "  ;" >>$@
39
+
40
+%.d: %.cpp Makefile | $(GEN_HDRS)
41
+	$(CXX) $(DEFINES) $(INCLUDE) $(CFLAGS) -M -o $@ $<
42
+
43
+%.o: %.cpp Makefile
44
+	$(CXX) $(DEFINES) $(INCLUDE) $(CFLAGS) -c -o $@ $<
45
+
46
+$(TARGET): $(OBJS)
47
+	$(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)
48
+
49
+clean:
50
+	rm -f $(GEN_HDRS) $(DEPS) $(OBJS) $(TARGET)
51
+
... ...
@@ -0,0 +1,119 @@
1
+/*
2
+ * EtherPix simulator
3
+ *
4
+ * Copyright 2017 Stefan Schuermans <stefan schuermans info>
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, version 3 of the License.
9
+ *
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+#include <gtkmm.h>
21
+
22
+#include "draw.h"
23
+
24
+/**
25
+ * @brief constructor
26
+ * @param[in] Gtk Window object (a C object)
27
+ * @param[in] builder reference to GTK builder object
28
+ */
29
+Draw::Draw(BaseObjectType *cobject,
30
+           const Glib::RefPtr<Gtk::Builder> &builder):
31
+  Gtk::DrawingArea(cobject),
32
+  m_builder(builder)
33
+{
34
+  /*
35
+  TODO
36
+  // connect callbacks (seems to be needed for gtkmm-2.4 < V2.16)
37
+  signal_configure_event().connect(
38
+    sigc::mem_fun(*this, &Draw::on_configure_event));
39
+  signal_expose_event().connect(
40
+    sigc::mem_fun(*this, &Draw::on_expose_event));
41
+  */
42
+
43
+  // initialize image
44
+  updateImage();
45
+  queue_draw();
46
+}
47
+
48
+/// virtual destructor
49
+Draw::~Draw()
50
+{
51
+}
52
+
53
+
54
+/**
55
+ * @brief widget is (re)configured
56
+ * @param[in] cfg configuration event
57
+ * @return if callback was completely handled
58
+ */
59
+bool Draw::on_configure_event(GdkEventConfigure *cfg)
60
+{
61
+  // update image
62
+  updateImage();
63
+  queue_draw();
64
+
65
+  return true;
66
+  (void)cfg;
67
+}
68
+
69
+/**
70
+ * @brief widget is being drawn
71
+ * @param[in] cairo cairo context for drawing
72
+ * @return if callback was completely handled
73
+ */
74
+bool Draw::on_draw(const Cairo::RefPtr<Cairo::Context> &cairo)
75
+{
76
+  // draw image
77
+  if (m_image) {
78
+    cairo->set_source(m_image, 0, 0);
79
+    cairo->rectangle(0, 0, m_image->get_width(), m_image->get_height());
80
+    cairo->fill();
81
+  }
82
+  return true;
83
+}
84
+
85
+/// update image
86
+void Draw::updateImage()
87
+{
88
+  // get size of drawing area
89
+  Gtk::Allocation allocation = get_allocation();
90
+  int width = allocation.get_width();
91
+  int height = allocation.get_height();
92
+
93
+  // create new image
94
+  m_image = Cairo::ImageSurface::create(Cairo::FORMAT_RGB24, width, height);
95
+
96
+  // create ciaro context for drawing
97
+  Cairo::RefPtr<Cairo::Context> cairo = Cairo::Context::create(m_image);
98
+
99
+  // draw background
100
+  cairo->set_source_rgb(0.0, 0.0, 0.0);
101
+  cairo->rectangle(0, 0, width, height);
102
+  cairo->fill();
103
+
104
+  // TODO
105
+  // draw circles
106
+  cairo->set_source_rgb(1.0, 0.0, 0.0);
107
+  cairo->arc(0.5 * width, 0.5 * height, 0.03 * (width + height),
108
+             0.0, 2.0 * M_PI);
109
+  cairo->fill();
110
+  cairo->set_source_rgb(0.0, 1.0, 0.0);
111
+  cairo->arc(0.7 * width, 0.3 * height, 0.03 * (width + height),
112
+             0.0, 2.0 * M_PI);
113
+  cairo->fill();
114
+  cairo->set_source_rgb(0.0, 0.0, 1.0);
115
+  cairo->arc(0.7 * width, 0.7 * height, 0.03 * (width + height),
116
+             0.0, 2.0 * M_PI);
117
+  cairo->fill();
118
+}
119
+
... ...
@@ -0,0 +1,68 @@
1
+/*
2
+ * EtherPix simulator
3
+ *
4
+ * Copyright 2017 Stefan Schuermans <stefan schuermans info>
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, version 3 of the License.
9
+ *
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+#ifndef DRAW_H
21
+#define DRAW_H
22
+
23
+#include <gtkmm.h>
24
+
25
+/// drawing area
26
+class Draw: public Gtk::DrawingArea
27
+{
28
+public:
29
+  /**
30
+   * @brief constructor
31
+   * @param[in] Gtk Window object (a C object)
32
+   * @param[in] builder reference to GTK builder object
33
+   */
34
+  Draw(BaseObjectType *cobject,
35
+       const Glib::RefPtr<Gtk::Builder> &builder);
36
+
37
+  /// virtual destructor
38
+  virtual ~Draw();
39
+
40
+
41
+protected:
42
+  /**
43
+   * @brief widget is (re)configured
44
+   * @param[in] cfg configuration event
45
+   * @return if callback was completely handled
46
+   */
47
+  virtual bool on_configure_event(GdkEventConfigure *cfg);
48
+
49
+  /**
50
+   * @brief widget is being drawn
51
+   * @param[in] cairo cairo context for drawing
52
+   * @return if callback was completely handled
53
+   */
54
+  virtual bool on_draw(const Cairo::RefPtr<Cairo::Context> &cairo);
55
+
56
+  /// update image
57
+  void updateImage();
58
+
59
+protected:
60
+  /// reference to GTK builder object
61
+  Glib::RefPtr<Gtk::Builder> m_builder;
62
+
63
+  /// cairo surface with current simulator image
64
+  Cairo::RefPtr<Cairo::ImageSurface> m_image;
65
+};
66
+
67
+#endif // #ifndef DRAW_H
68
+
... ...
@@ -0,0 +1,49 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!-- Generated with glade 3.18.3 -->
3
+<interface>
4
+  <requires lib="gtk+" version="3.0"/>
5
+  <object class="GtkWindow" id="MainWindow">
6
+    <property name="visible">True</property>
7
+    <property name="can_focus">False</property>
8
+    <property name="title" translatable="yes">EtherPix Simulator</property>
9
+    <signal name="destroy" handler="onDestroy" swapped="no"/>
10
+    <child>
11
+      <object class="GtkBox" id="MainVBox">
12
+        <property name="visible">True</property>
13
+        <property name="can_focus">False</property>
14
+        <property name="orientation">vertical</property>
15
+        <child>
16
+          <object class="GtkDrawingArea" id="Draw">
17
+            <property name="width_request">240</property>
18
+            <property name="height_request">240</property>
19
+            <property name="visible">True</property>
20
+            <property name="can_focus">False</property>
21
+            <property name="margin_left">5</property>
22
+            <property name="margin_right">5</property>
23
+            <property name="margin_top">5</property>
24
+            <property name="margin_bottom">5</property>
25
+            <signal name="draw" handler="onImageDraw" swapped="no"/>
26
+          </object>
27
+          <packing>
28
+            <property name="expand">True</property>
29
+            <property name="fill">True</property>
30
+            <property name="position">0</property>
31
+          </packing>
32
+        </child>
33
+        <child>
34
+          <object class="GtkStatusbar" id="Status">
35
+            <property name="visible">True</property>
36
+            <property name="can_focus">False</property>
37
+            <property name="orientation">vertical</property>
38
+            <property name="spacing">2</property>
39
+          </object>
40
+          <packing>
41
+            <property name="expand">False</property>
42
+            <property name="fill">False</property>
43
+            <property name="position">1</property>
44
+          </packing>
45
+        </child>
46
+      </object>
47
+    </child>
48
+  </object>
49
+</interface>
... ...
@@ -0,0 +1,62 @@
1
+/*
2
+ * EtherPix simulator
3
+ *
4
+ * Copyright 2017 Stefan Schuermans <stefan schuermans info>
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, version 3 of the License.
9
+ *
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+#include <gtkmm.h>
21
+#include <iostream>
22
+#include <string.h>
23
+
24
+#include "etherpix_sim.glade.h"
25
+#include "main_window.h"
26
+
27
+int main(int argc, char *argv[])
28
+{
29
+  // instantiate GTK main loop
30
+  Gtk::Main kit(argc, argv);
31
+
32
+  // parse arguments
33
+  if (argc < 2) {
34
+    std::cerr << "usage: " << argv[0] << " <sim_config.etps>" << std::endl;
35
+    return 2;
36
+  }
37
+  char *config_file_name = argv[1];
38
+
39
+  try {
40
+
41
+    // create window
42
+    Glib::RefPtr<Gtk::Builder> builder =
43
+      Gtk::Builder::create_from_string(widgets_etherpix_sim);
44
+
45
+    // connect main window to its class
46
+    MainWindow *mainWindow;
47
+    builder->get_widget_derived("MainWindow", mainWindow);
48
+
49
+    // append config file name to window title
50
+    mainWindow->set_title(mainWindow->get_title() + " - " + config_file_name);
51
+
52
+    // run GTK main loop
53
+    Gtk::Main::run();
54
+
55
+  } catch (std::exception &ex) {
56
+    std::cerr << ex.what() << std::endl;
57
+    return 3;
58
+  }
59
+
60
+  return 0;
61
+}
62
+
... ...
@@ -0,0 +1,61 @@
1
+/*
2
+ * EtherPix simulator
3
+ *
4
+ * Copyright 2017 Stefan Schuermans <stefan schuermans info>
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, version 3 of the License.
9
+ *
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+#include <gtkmm.h>
21
+//#include <libglademm/xml.h>
22
+
23
+#include "main_window.h"
24
+
25
+/**
26
+ * @brief constructor
27
+ * @param[in] Gtk Window object (a C object)
28
+ * @param[in] builder reference to GTK builder object
29
+ */
30
+MainWindow::MainWindow(BaseObjectType *cobject,
31
+                       const Glib::RefPtr<Gtk::Builder> &builder):
32
+  Gtk::Window(cobject),
33
+  m_builder(builder)
34
+{
35
+  // get widgets
36
+  builder->get_widget_derived("Draw", m_draw);
37
+  builder->get_widget("Status", m_status);
38
+
39
+  // connect callbacks
40
+  signal_hide().connect(sigc::mem_fun(*this, &MainWindow::on_hide));
41
+}
42
+
43
+/// virtual destructor
44
+MainWindow::~MainWindow()
45
+{
46
+}
47
+
48
+/// window is being hidden
49
+void MainWindow::on_hide()
50
+{
51
+  // leave main loop
52
+  Gtk::Main::quit();
53
+}
54
+
55
+/// set status bar text
56
+void MainWindow::set_status(std::string const &txt)
57
+{
58
+  m_status->pop();
59
+  m_status->push(txt);
60
+}
61
+
... ...
@@ -0,0 +1,60 @@
1
+/*
2
+ * EtherPix simulator
3
+ *
4
+ * Copyright 2017 Stefan Schuermans <stefan schuermans info>
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, version 3 of the License.
9
+ *
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+#ifndef MAIN_WINDOW_H
21
+#define MAIN_WINDOW_H
22
+
23
+#include <gtkmm.h>
24
+
25
+#include "draw.h"
26
+
27
+/// main window of StateTracker GUI
28
+class MainWindow: public Gtk::Window
29
+{
30
+public:
31
+  /**
32
+   * @brief constructor
33
+   * @param[in] Gtk Window object (a C object)
34
+   * @param[in] builder reference to GTK builder object
35
+   */
36
+  MainWindow(BaseObjectType *cobject,
37
+             const Glib::RefPtr<Gtk::Builder> &builder);
38
+
39
+  /// virtual destructor
40
+  virtual ~MainWindow();
41
+
42
+protected:
43
+  /// window is being hidden
44
+  void on_hide();
45
+
46
+  /// set status bar text
47
+  void set_status(std::string const &txt);
48
+
49
+protected:
50
+  /// reference to GTK builder object
51
+  Glib::RefPtr<Gtk::Builder> m_builder;
52
+
53
+  /// drawing area
54
+  Draw *m_draw;
55
+  /// status bar
56
+  Gtk::Statusbar *m_status;
57
+};
58
+
59
+#endif // #ifndef MAIN_WINDOW_H
60
+
0 61