File: gtk_appgui.cc

package info (click to toggle)
morris 0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,664 kB
  • sloc: cpp: 6,383; sh: 4,322; xml: 137; makefile: 46
file content (224 lines) | stat: -rw-r--r-- 6,136 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/***************************************************************************
  This file is part of Morris.
  Copyright (C) 2009 Dirk Farin <dirk.farin@gmail.com>

  Morris 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.

  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, see <http://www.gnu.org/licenses/>.
***************************************************************************/

#include "config.h"
#include "gtkcairo_boardgui.hh"
#include "gtk_menutoolbar.hh"
#include "gtk_appgui.hh"
#include "mainapp.hh"
#include "gtk_threadtunnel.hh"
#include "gtk_prefDisplay.hh"
#include "appgtk_configmgr.hh"

#include <assert.h>
#include <iostream>
#include <boost/bind.hpp>
#include <glib/gthread.h>


static gboolean delete_callback(GtkWidget *widget, GdkEvent* event, gpointer data)
{
  return FALSE;
}


static void cb_destroy(GtkWidget *widget, gpointer data)
{
  ((ApplicationGUI_Gtk*)(data))->quitApplication();
}


/* This function should also work if MainApp is not fully functional yet.
 */
ApplicationGUI_Gtk::ApplicationGUI_Gtk()
{
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  main_vbox = gtk_vbox_new(false,0);
  gtk_container_add (GTK_CONTAINER (window), main_vbox);

  gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
  gtk_widget_realize(window);

  // statusbar

  statusbar = gtk_statusbar_new();
  statusbar_context = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), "mainapp");
  gtk_box_pack_end(GTK_BOX(main_vbox), statusbar, false,true,0);

  progressbar = gtk_progress_bar_new();
  gtk_box_pack_start(GTK_BOX(statusbar), progressbar, false,true,0);
  gtk_box_reorder_child(GTK_BOX(statusbar), progressbar, 0);

  // signals

  gtk_signal_connect(GTK_OBJECT(window), "destroy",      GTK_SIGNAL_FUNC(cb_destroy), this);
  gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(delete_callback), this);
}


ApplicationGUI_Gtk::~ApplicationGUI_Gtk()
{
}


void ApplicationGUI_Gtk::initApplicationGUI(int& argc, char**& argv)
{
  gdk_threads_init();

  gtk_init(&argc,&argv);

  // attach app-GUI and board-GUI to main application

  ApplicationGUI_Gtk* appgui = new ApplicationGUI_Gtk;
  MainApp::app().setApplicationGUI( appgui_ptr(appgui) );
  MainApp::app().setThreadTunnel( getThreadTunnel_Gtk() );

  BoardGUI_GtkCairo* boardgui = new BoardGUI_GtkCairo;
  appgui->setBoardGUI( boardgui_ptr(boardgui));

  gtk_box_pack_end(GTK_BOX(appgui->getMainWindowVBox()), boardgui->getBoardWidget(), true,true,0);

  ConfigManager_Chained* config = new ConfigManager_Application();
  config->setDelegate(configmanager_ptr(new ConfigManager_AppGtk));

  MainApp::app().registerConfigManager(configmanager_ptr(config));
}


void ApplicationGUI_Gtk::startApplication()
{
  GameControl& control = MainApp::app().getControl();

  control.getSignal_changeState().connect(boost::bind(&ApplicationGUI_Gtk::changeState, this, _1));
  control.getSignal_changeBoard().connect(boost::bind(&ApplicationGUI_Gtk::changeBoard, this));
  control.getSignal_startMove()  .connect(boost::bind(&ApplicationGUI_Gtk::startMove,   this));

  gtk_initMainMenu(main_vbox, window);
  gtk_connectMenuSignals();

  showWindow();

  MainApp::app().getConfigManager()->readInitialValues(); // TODO: this should probably be in MainApp.init()

  control.startNextMove();

  // === main loop ===

  gdk_threads_enter();
  gtk_main();
  gdk_threads_leave();
}


void ApplicationGUI_Gtk::quitApplication()
{
  showMoveLog(false, true);
  MainApp::app().getControl().stopThreads();
  gtk_main_quit();
}


void ApplicationGUI_Gtk::changeState(const GameState& state)
{
}


void ApplicationGUI_Gtk::changeBoard()
{
}


void ApplicationGUI_Gtk::startMove()
{
}


void ApplicationGUI_Gtk::preferencesDialog_Display()
{
  ::preferencesDialog_Display();
  gui_board->resetDisplay();
}


void ApplicationGUI_Gtk::showMoveLog(bool enable, bool quitApp)
{
  if (enable)
    {
      gui_movelog = boost::shared_ptr<MoveLog>(new MoveLog_Gtk);

      gui_movelog->getSignal_windowClosed().connect(boost::bind(&ApplicationGUI_Gtk::showMoveLog, this,false,false));
      gui_movelog->getSignal_windowClosed().connect(boost::bind(menu_closedMoveLog, false));
    }
  else
    {
      gui_movelog.reset();
    }

  if (!quitApp)
    {
      configmanager_ptr config = MainApp::app().getConfigManager();
      config->store(config->main(), ConfigManager::itemPref_showLogOfMoves, enable);
    }
}


void ApplicationGUI_Gtk::showGameOverDialog(Player winner)
{
  if (!options.showGameOverRequester)
    { return; }

  const char* text;
  switch (winner)
    {
    case PL_White: text=_("White has won."); break;
    case PL_Black: text=_("Black has won."); break;
    case PL_None:  text=_("Tie between both players."); break;
    }

  GtkWidget* dialog = gtk_message_dialog_new(GTK_WINDOW(window),
					     GTK_DIALOG_DESTROY_WITH_PARENT,
					     GTK_MESSAGE_INFO,
					     GTK_BUTTONS_OK,
					     "%s",
					     _("Game over"));
  gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", text);

  gtk_dialog_run(GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}


void ApplicationGUI_Gtk::showAboutDialog()
{
  const char* authors[] =
    {
      "Dirk Farin <dirk.farin@gmail.com>",
      NULL
    };

  gtk_show_about_dialog (GTK_WINDOW(window),
			 "program-name", PACKAGE_NAME,
			 //"logo", example_logo,
			 //"title", _("About Gnome Nine-Mens-Morris"),  // tag does not exist ?
			 "authors", authors,
			 "version", VERSION,
			 "comments", _("A computer adaptation of the Nine-Mens-Morris board game and its variants."),
			 "copyright", "Copyright (c) 2009 Dirk Farin",
			 //"website", "morris.org",
			 NULL);
}