1 /*
2    gnauralnet_gui.c
3    Code that allows user to access info and features of GnauralNet with a GTK+ GUI.
4    Depends on:
5    gnauralnet.h
6    gnauralnet_clocksync.c
7    gnauralnet_lists.c
8    gnauralnet_main.c
9    gnauralnet_socket.c
10 
11    Copyright (C) 2008  Bret Logan
12 
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 2 of the License, or
16    (at your option) any later version.
17 
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22 
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27 
28 #include <gtk/gtk.h>
29 #include "gnauralnet.h"
30 #include "main.h"       //needed ONLY for main_MessageDialogBox()
31 
32 //Global variables:
33 GtkWidget *GN_gui_CreateGtkList (void);
34 
35 ///////////////////////////////////////////////////////////
36 //This creates a generic dialog box and puts a GtkTreeView
37 //containing all the GN data in to it.
GN_ShowInfo()38 void GN_ShowInfo ()
39 {
40  GtkWidget *dialog,
41  *label;
42  //char sTmp[256];
43 
44  if (GNAURALNET_RUNNING != GN_My.State)
45  {
46   main_MessageDialogBox
47    ("You're not running GnauralNet", GTK_MESSAGE_INFO, GTK_BUTTONS_OK);
48   return;
49  }
50 
51  dialog = gtk_dialog_new_with_buttons ("Message",
52                                        (GtkWindow *) NULL,
53                                        GTK_DIALOG_DESTROY_WITH_PARENT,
54                                        GTK_STOCK_OK, GTK_RESPONSE_NONE, NULL);
55  g_signal_connect_swapped (dialog,
56                            "response",
57                            G_CALLBACK (gtk_widget_destroy), dialog);
58 
59  //GN_Friend *curFriend = GN_My.FirstFriend;
60  //struct in_addr tmp_in_addr;
61 
62  if (NULL == GN_My.FirstFriend)
63  {
64   label = gtk_label_new ("No contacts to report");
65   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), label);
66  }
67  else
68  {
69   label = gtk_label_new ("Current Friends: (IP, Port, Schedule):");
70   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), label);
71 
72   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
73                      GN_gui_CreateGtkList ());
74  }
75  gtk_widget_show_all (dialog);
76 }
77 
78 enum
79 {
80  GNGUI_IP_COLUMN,
81  GNGUI_PORT_COLUMN,
82  GNGUI_SCHED_COLUMN,
83  GNGUI_N_COLUMNS
84 };
85 
86 ///////////////////////////////////////////////
87 //This creates and returns a GtkTreeView, to be added
88 //like any ordinary widget in to a vbox, etc. It does
89 //so by creating a GtkTreeStorefills, filling it with
90 //GN data, then using that to create the GtkTreeView
91 //(it then deletes the GtkTreeStore).
92 //see:
93 // http://library.gnome.org/devel/gtk/stable/TreeWidget.html
94 // http://scentric.net/tutorial/sec-treeview-col-example.html
GN_gui_CreateGtkList(void)95 GtkWidget *GN_gui_CreateGtkList (void)
96 {
97  GtkTreeStore *store;
98  GtkWidget *tree = NULL;
99  //GtkTreeViewColumn *column;
100  GtkCellRenderer *renderer;
101  GtkTreeIter iter;              //only need one unless doing a real tree view
102 
103  GN_Friend *curFriend = GN_My.FirstFriend;
104  struct in_addr tmp_in_addr;
105 
106  /* Create a model.  We are using the store model for now, though we
107   * could use any other GtkTreeModel */
108  store = gtk_tree_store_new (GNGUI_N_COLUMNS,
109                              G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);
110 
111  /* populate the model: */
112  while (curFriend != NULL)
113  {
114   tmp_in_addr.s_addr = curFriend->IP;
115   gtk_tree_store_append (store, &iter, NULL);   // Acquire an iterator for tree
116   gtk_tree_store_set (store, &iter,
117                       GNGUI_IP_COLUMN, inet_ntoa (tmp_in_addr),
118                       GNGUI_PORT_COLUMN, ntohs (curFriend->Port),
119                       GNGUI_SCHED_COLUMN,
120                       (curFriend->RunningID ==
121                        GN_My.RunningID) ? TRUE : FALSE, -1);
122 
123   //all list passes need this:
124   curFriend = curFriend->NextFriend;
125  }
126 
127  /* Create a view */
128  tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
129 
130  /* The view now holds a reference.  We can get rid of our own
131   * reference */
132  g_object_unref (G_OBJECT (store));
133 
134  //Create a cell render:
135  renderer = gtk_cell_renderer_text_new ();
136  // g_object_set (G_OBJECT (renderer), "foreground", "red", NULL);
137 
138  /* First column */
139  /* Create a column, associating the "text" attribute of the
140   * cell_renderer to the first column of the model */
141  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree),
142                                               -1,
143                                               "IP",
144                                               renderer,
145                                               "text", GNGUI_IP_COLUMN, NULL);
146 
147  /* Second column */
148  renderer = gtk_cell_renderer_text_new ();
149  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree),
150                                               -1,
151                                               "Port",
152                                               renderer,
153                                               "text", GNGUI_PORT_COLUMN,
154                                               NULL);
155 
156  /* Last column */
157  renderer = gtk_cell_renderer_toggle_new ();
158  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree),
159                                               -1,
160                                               "Schedule",
161                                               renderer,
162                                               "active", GNGUI_SCHED_COLUMN,
163                                               NULL);
164 
165  /* Now we can manipulate the view just like any other GTK widget */
166  return tree;
167 }
168