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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/* -*-c-*- -------------- mixgtk_wm.c :
* Implementation of the functions declared in mixgtk_wm.h
* ------------------------------------------------------------------
* $Id: mixgtk_wm.c,v 1.9 2004/06/30 22:46:15 jao Exp $
* ------------------------------------------------------------------
* Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
*
* This program 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <string.h>
#include "mixgtk_wm.h"
#include "mixgtk_widgets.h"
#include "mixgtk_config.h"
#include "mixgtk_device.h"
#include "mixgtk_mixvm.h"
#include "mixgtk_mixal.h"
#include "mixgtk_widgets.h"
#include "mixgtk_cmd_dispatcher.h"
#include "mixgtk.h"
typedef struct window_info_t_
{
mixgtk_dialog_id_t dialog;
GtkWidget *widget;
GtkCheckMenuItem *menu;
const gchar *menu_name;
const gchar *config_key;
gboolean detached;
const gchar *toolbar_name;
void (*detach) (void);
void (*attach) (void);
} window_info_t_;
static const gchar *DETACH_YES_ = "Yes";
static const gchar *DETACH_NO_ = "No";
static GtkWidget *about_ = NULL;
static GtkContainer *mixvm_container_ = NULL;
static GtkContainer *mixal_container_ = NULL;
static GtkContainer *dev_container_ = NULL;
static const gchar *TB_MENU_NAME_ = "show_toolbars";
static GtkCheckMenuItem *tb_menu_ = NULL;
static GtkNotebook *notebook_ = NULL;
static mix_vm_t *vm_ = NULL;
static void init_info_ (void);
static void init_notebook_ (void);
static void init_dispatcher_ (void);
static void init_mixvm_ (void);
static void init_mixal_ (void);
static void init_dev_ (void);
static void init_signals_ (void);
static void init_visibility_ (void);
static void init_tb_ (void);
static void init_about_ (void);
static void init_autosave_ (void);
static void add_page_ (GtkWidget *w, mixgtk_window_id_t id);
static void mixvm_attach_ (void);
static void mixvm_detach_ (void);
static void mixal_attach_ (void);
static void mixal_detach_ (void);
static void dev_attach_ (void);
static void dev_detach_ (void);
static void on_nb_switch_ (GtkNotebook *notebook, GtkWidget *page,
guint page_num, gpointer user_data);
static window_info_t_ infos_[] = {
{MIXGTK_MIXVM_DIALOG, NULL, NULL, "detach_vm", "MIX.detach",
FALSE, "mixvm_toolbar", mixvm_detach_, mixvm_attach_},
{MIXGTK_MIXAL_DIALOG, NULL, NULL, "detach_source", "MIXAL.detach",
FALSE, "mixal_toolbar", mixal_detach_, mixal_attach_},
{MIXGTK_DEVICES_DIALOG, NULL, NULL, "detach_dev", "Devices.detach",
FALSE, "devices_toolbar", dev_detach_, dev_attach_}
};
static size_t INF_NO_ = sizeof (infos_) / sizeof (infos_[0]);
gboolean
mixgtk_wm_init (void)
{
gint k;
init_info_ ();
init_notebook_ ();
init_dispatcher_ ();
init_mixvm_ ();
init_mixal_ ();
init_dev_ ();
for (k = 0; k < INF_NO_; ++k)
{
if (infos_[k].detached)
mixgtk_wm_detach_window (k);
else
mixgtk_wm_attach_window (k);
}
init_tb_ ();
init_about_ ();
init_autosave_ ();
init_visibility_ ();
init_signals_ ();
return TRUE;
}
void
mixgtk_wm_detach_window (mixgtk_window_id_t w)
{
if (w < INF_NO_)
{
(*(infos_[w].detach)) ();
infos_[w].detached = TRUE;
gtk_check_menu_item_set_active (infos_[w].menu, TRUE);
mixgtk_config_update (infos_[w].config_key, DETACH_YES_);
if (infos_[w].widget != NULL) gtk_widget_show (infos_[w].widget);
if (gtk_notebook_get_n_pages (notebook_) < 1)
gtk_widget_hide (GTK_WIDGET (notebook_));
gtk_widget_show (mixgtk_widget_factory_get_dialog (infos_[w].dialog));
}
}
void
mixgtk_wm_attach_window (mixgtk_window_id_t w)
{
if (w < INF_NO_)
{
gint page = gtk_notebook_page_num (notebook_, infos_[w].widget);
gtk_widget_hide (mixgtk_widget_factory_get_dialog (infos_[w].dialog));
if (page < 0) (*(infos_[w].attach)) ();
infos_[w].detached = FALSE;
gtk_check_menu_item_set_active (infos_[w].menu, FALSE);
mixgtk_config_update (infos_[w].config_key, DETACH_NO_);
if (gtk_notebook_get_n_pages (notebook_) == 1)
gtk_widget_show (GTK_WIDGET (notebook_));
gtk_notebook_set_current_page (notebook_, -1);
}
}
void
mixgtk_wm_show_toolbars (gboolean show)
{
static const gchar *MAIN_TB_NAME = "main_toolbar";
int k;
GtkWidget *toolbar =
mixgtk_widget_factory_get_child_by_name (MIXGTK_MAIN, MAIN_TB_NAME);
if (toolbar != NULL)
{
if (show) gtk_widget_show (toolbar);
else gtk_widget_hide (toolbar);
}
for (k = 0; k < INF_NO_; ++k)
{
if (infos_[k].toolbar_name != NULL)
{
GtkWidget *tb = mixgtk_widget_factory_get_child_by_name
(infos_[k].dialog, infos_[k].toolbar_name);
if (tb)
{
if (show) gtk_widget_show (tb); else gtk_widget_hide (tb);
}
}
}
mixgtk_config_set_show_toolbars (show);
}
/* callbacks */
void
on_detach_clicked (GtkWidget *ignored)
{
gint page = gtk_notebook_get_current_page (notebook_);
if (page >= 0)
{
gint k;
GtkWidget *w = gtk_notebook_get_nth_page (notebook_, page);
for (k = 0; k < INF_NO_; ++k)
if (infos_[k].widget == w) mixgtk_wm_detach_window (k);
}
}
void
on_attach_toggled (GtkCheckMenuItem *item)
{
gint k;
for (k = 0; k < INF_NO_; ++k)
if (item == infos_[k].menu) break;
g_return_if_fail (k < INF_NO_);
if (item->active) mixgtk_wm_detach_window (k);
else mixgtk_wm_attach_window (k);
mixgtk_config_update (infos_[k].config_key,
(item->active)? DETACH_YES_ : DETACH_NO_);
}
void
on_window_hide (GtkWidget *w)
{
gint k;
for (k = 0; k < INF_NO_; ++k)
if (w == mixgtk_widget_factory_get_dialog (infos_[k].dialog)) break;
g_return_if_fail (k < INF_NO_);
mixgtk_wm_attach_window (k);
}
void
on_show_toolbars_toggled (GtkCheckMenuItem *item)
{
if (item->active != mixgtk_config_show_toolbars ())
mixgtk_wm_show_toolbars (item->active);
}
void
on_widget_attach (GtkWidget *ign, gpointer id)
{
mixgtk_wm_attach_window (GPOINTER_TO_INT (id));
}
/* about box */
void
on_about_activate (GtkWidget *w, gpointer data)
{
if (!about_) init_about_ ();
gtk_widget_show (about_);
}
static void
init_info_ (void)
{
gint k;
for (k = 0; k < INF_NO_; ++k)
{
const gchar *txt;
infos_[k].menu =
GTK_CHECK_MENU_ITEM
(mixgtk_widget_factory_get_child_by_name (MIXGTK_MAIN,
infos_[k].menu_name));
g_assert (infos_[k].menu != NULL);
txt = mixgtk_config_get (infos_[k].config_key);
infos_[k].detached = txt && !g_ascii_strcasecmp (txt, DETACH_YES_);
}
}
static void
init_notebook_ (void)
{
notebook_ =
GTK_NOTEBOOK (mixgtk_widget_factory_get (MIXGTK_MAIN,
MIXGTK_WIDGET_NOTEBOOK));
g_assert (notebook_ != NULL);
gtk_notebook_set_tab_pos (notebook_, GTK_POS_TOP);
gtk_notebook_popup_disable (notebook_);
gtk_notebook_remove_page (notebook_, 0);
gtk_widget_show (GTK_WIDGET (notebook_));
}
static void
add_page_ (GtkWidget *page, mixgtk_window_id_t id)
{
static const gchar *LABELS[] = {
N_("_Virtual machine"), N_("_Source"), N_("_Devices")
};
g_assert (page != NULL);
g_assert (id < INF_NO_);
g_assert (notebook_ != NULL);
gtk_notebook_append_page (notebook_, page,
gtk_label_new_with_mnemonic (LABELS[id]));
gtk_notebook_set_show_tabs (notebook_,
gtk_notebook_get_n_pages (notebook_) > 1);
gtk_widget_show (page);
gtk_widget_show (GTK_WIDGET (notebook_));
}
static void
init_signals_ (void)
{
gint k;
for (k = 0; k < INF_NO_; ++k)
{
GObject *dialog =
G_OBJECT (mixgtk_widget_factory_get_dialog (infos_[k].dialog));
GObject *button =
G_OBJECT (mixgtk_widget_factory_get (infos_[k].dialog,
MIXGTK_WIDGET_ATTACH_BUTTON));
g_assert (dialog != NULL);
g_assert (button != NULL);
g_signal_connect (button, "clicked",
G_CALLBACK (on_widget_attach), GINT_TO_POINTER (k));
g_signal_connect (dialog, "destroy",
G_CALLBACK (on_window_hide), NULL);
g_signal_connect (dialog, "destroy_event",
G_CALLBACK (on_window_hide), NULL);
g_signal_connect (dialog, "delete_event",
G_CALLBACK (on_window_hide), NULL);
g_signal_connect (G_OBJECT (infos_[k].menu), "toggled",
G_CALLBACK (on_attach_toggled), NULL);
}
}
static void
init_visibility_ (void)
{
gint k;
for (k = 0; k < INF_NO_; ++k)
if (infos_[k].detached)
gtk_widget_show (mixgtk_widget_factory_get_dialog (infos_[k].dialog));
if (gtk_notebook_get_n_pages (notebook_) > 0)
gtk_notebook_set_current_page (notebook_, 0);
gtk_widget_show (mixgtk_widget_factory_get_dialog (MIXGTK_MAIN));
}
static void
init_dispatcher_ (void)
{
if (!mixgtk_cmd_dispatcher_init (MIXGTK_MAIN))
g_assert (FALSE);
vm_ = mixgtk_cmd_dispatcher_get_vm ();
g_assert (vm_ != NULL);
}
static void
init_mixvm_ (void)
{
GtkContainer *vm =
GTK_CONTAINER
(mixgtk_widget_factory_get (MIXGTK_MIXVM_DIALOG, MIXGTK_WIDGET_MIXVM));
mixvm_container_ =
GTK_CONTAINER (mixgtk_widget_factory_get (MIXGTK_MIXVM_DIALOG,
MIXGTK_WIDGET_MIXVM_CONTAINER));
g_assert (mixvm_container_ != NULL);
g_assert (vm != NULL);
g_object_ref (G_OBJECT (vm));
infos_[MIXGTK_MIXVM_WINDOW].widget = GTK_WIDGET (vm);
mixgtk_mixvm_init (vm_);
mixgtk_mixvm_update_vm_widgets ();
gtk_widget_show (GTK_WIDGET (vm));
}
static void
init_mixal_ (void)
{
GtkWidget *page = gtk_scrolled_window_new (NULL, NULL);
GtkWidget *mixal = mixgtk_mixal_init (vm_);
g_assert (page != NULL);
g_assert (mixal != NULL);
g_object_ref (page);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (page),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (page), mixal);
infos_[MIXGTK_MIXAL_WINDOW].widget = page;
mixal_container_ =
GTK_CONTAINER (mixgtk_widget_factory_get (MIXGTK_MIXAL_DIALOG,
MIXGTK_WIDGET_MIXAL_CONTAINER));
g_assert (mixal_container_ != NULL);
gtk_container_add (mixal_container_, page);
gtk_widget_show (mixal);
gtk_widget_show (page);
}
static void
init_dev_ (void)
{
GtkWidget *page = gtk_scrolled_window_new (NULL, NULL);
GtkWidget *devs = mixgtk_device_init (vm_);
g_assert (page != NULL);
g_assert (devs != NULL);
g_object_ref (page);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (page),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (page), devs);
infos_[MIXGTK_DEVICES_WINDOW].widget = page;
dev_container_ =
GTK_CONTAINER (mixgtk_widget_factory_get (MIXGTK_DEVICES_DIALOG,
MIXGTK_WIDGET_DEV_CONTAINER));
g_assert (dev_container_ != NULL);
gtk_container_add (dev_container_, page);
gtk_widget_show (devs);
gtk_widget_show (page);
}
static void
init_tb_ (void)
{
tb_menu_ = GTK_CHECK_MENU_ITEM
(mixgtk_widget_factory_get_child_by_name (MIXGTK_MAIN,
TB_MENU_NAME_));
g_assert (tb_menu_ != NULL);
gtk_check_menu_item_set_active (tb_menu_, mixgtk_config_show_toolbars ());
mixgtk_wm_show_toolbars (mixgtk_config_show_toolbars ());
g_signal_connect (G_OBJECT (tb_menu_), "toggled",
G_CALLBACK (on_show_toolbars_toggled), NULL);
}
static void
init_autosave_ (void)
{
#define AUTOSAVE_ITEM_ "save_on_exit"
GtkCheckMenuItem *item = GTK_CHECK_MENU_ITEM
(mixgtk_widget_factory_get_child_by_name (MIXGTK_MAIN, AUTOSAVE_ITEM_));
if (item)
{
gtk_check_menu_item_set_active (item, mixgtk_config_is_autosave ());
}
}
static void
init_about_ (void)
{
#define VERSION_LABEL_ "version_label"
#define GPL_TEXT_ "gpl_text"
GtkWidget *label;
about_ = mixgtk_widget_factory_get_dialog (MIXGTK_ABOUT_DIALOG);
g_assert (about_ != NULL);
label = mixgtk_widget_factory_get_child_by_name (MIXGTK_ABOUT_DIALOG,
VERSION_LABEL_);
g_assert (label != NULL);
gtk_label_set_text (GTK_LABEL (label), VERSION);
gtk_widget_show (label);
}
static void
mixvm_attach_ (void)
{
gtk_container_remove (mixvm_container_, infos_[MIXGTK_MIXVM_WINDOW].widget);
add_page_ (infos_[MIXGTK_MIXVM_WINDOW].widget, MIXGTK_MIXVM_WINDOW);
}
static void
mixvm_detach_ (void)
{
gtk_widget_reparent (infos_[MIXGTK_MIXVM_WINDOW].widget,
GTK_WIDGET (mixvm_container_));
}
static void
mixal_attach_ (void)
{
static GtkStatusbar *stat = NULL;
if (stat == NULL)
{
stat =
GTK_STATUSBAR
(mixgtk_widget_factory_get (MIXGTK_MAIN, MIXGTK_WIDGET_STATUSBAR));
g_assert (stat);
g_signal_connect (G_OBJECT (notebook_), "switch-page",
G_CALLBACK (on_nb_switch_), NULL);
}
mixgtk_mixal_reparent (stat);
gtk_container_remove (mixal_container_, infos_[MIXGTK_MIXAL_WINDOW].widget);
add_page_ (infos_[MIXGTK_MIXAL_WINDOW].widget, MIXGTK_MIXAL_WINDOW);
}
static void
mixal_detach_ (void)
{
static GtkStatusbar *stat = NULL;
if (stat == NULL)
{
stat =
GTK_STATUSBAR
(mixgtk_widget_factory_get (MIXGTK_MIXAL_DIALOG,
MIXGTK_WIDGET_MIXAL_STATUSBAR));
g_assert (stat);
}
else
gtk_widget_reparent (infos_[MIXGTK_MIXAL_WINDOW].widget,
GTK_WIDGET (mixal_container_));
mixgtk_mixal_reparent (stat);
}
static void
dev_attach_ (void)
{
gtk_container_remove (dev_container_, infos_[MIXGTK_DEVICES_WINDOW].widget);
add_page_ (infos_[MIXGTK_DEVICES_WINDOW].widget, MIXGTK_DEVICES_WINDOW);
}
static void
dev_detach_ (void)
{
gtk_widget_reparent (infos_[MIXGTK_DEVICES_WINDOW].widget,
GTK_WIDGET (dev_container_));
}
static void
on_nb_switch_ (GtkNotebook *notebook, GtkWidget *page,
guint page_num, gpointer user_data)
{
if (!(infos_[MIXGTK_MIXAL_WINDOW].detached)
&& (page != infos_[MIXGTK_MIXAL_WINDOW].widget))
mixgtk_mixal_pop_status ();
}
|