medit/moo/mooterm/mootermwindow.c

237 lines
8.2 KiB
C
Raw Normal View History

2005-07-23 21:58:57 -07:00
/*
* mooterm/mootermwindow.c
*
* Copyright (C) 2004-2006 by Yevgen Muntyan <muntyan@math.tamu.edu>
2005-07-23 21:58:57 -07:00
*
* 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.
*
* See COPYING file that comes with this distribution.
*/
#include "mooterm/mootermwindow.h"
#include "mooterm/mooterm-prefs.h"
#include "mooutils/moocompat.h"
#include "mooutils/moodialogs.h"
2005-10-31 14:58:01 -08:00
#include "mooutils/mooutils-misc.h"
2005-07-23 21:58:57 -07:00
#include "mooutils/moostock.h"
#include "mooutils/mooprefs.h"
static void moo_term_window_class_init (MooTermWindowClass *klass);
2006-02-27 18:04:38 -08:00
static void moo_term_window_init (MooTermWindow *window);
static GObject *moo_term_window_constructor (GType type,
guint n_props,
2005-07-23 21:58:57 -07:00
GObjectConstructParam *props);
2006-02-27 18:04:38 -08:00
static void moo_term_window_save_selection (MooTermWindow *window);
static void copy_clipboard (MooTerm *term);
static void paste_clipboard (MooTerm *term);
2005-07-23 21:58:57 -07:00
/* MOO_TYPE_TERM_WINDOW */
G_DEFINE_TYPE (MooTermWindow, moo_term_window, MOO_TYPE_WINDOW)
static void moo_term_window_class_init (MooTermWindowClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
MooWindowClass *window_class = MOO_WINDOW_CLASS (klass);
2005-07-23 21:58:57 -07:00
gobject_class->constructor = moo_term_window_constructor;
moo_window_class_set_id (window_class, "Terminal", "Terminal");
moo_window_class_new_action (window_class, "SaveSelection",
"name", "Save Selection",
"label", "_Save Selection",
"tooltip", "Save selected text to a file",
"icon-stock-id", GTK_STOCK_SAVE,
"accel", "<alt>S",
2005-11-19 19:59:01 -08:00
"closure-callback", moo_term_window_save_selection,
NULL);
moo_window_class_new_action (window_class, "Copy",
"name", "Copy",
"label", "_Copy",
"tooltip", "Copy",
"icon-stock-id", GTK_STOCK_COPY,
"accel", "<alt>C",
2006-02-27 18:04:38 -08:00
"closure-callback", copy_clipboard,
2005-11-19 19:59:01 -08:00
"closure-proxy-func", moo_term_window_get_term,
NULL);
moo_window_class_new_action (window_class, "Paste",
"name", "Paste",
"label", "_Paste",
"tooltip", "Paste",
"icon-stock-id", GTK_STOCK_PASTE,
"accel", "<alt>V",
2006-02-27 18:04:38 -08:00
"closure-callback", paste_clipboard,
2005-11-19 19:59:01 -08:00
"closure-proxy-func", moo_term_window_get_term,
NULL);
moo_window_class_new_action (window_class, "SelectAll",
"name", "Select All",
"label", "Select _All",
"tooltip", "Select all",
"accel", "<alt>A",
2005-11-19 19:59:01 -08:00
"closure-callback", moo_term_select_all,
"closure-proxy-func", moo_term_window_get_term,
NULL);
2005-07-23 21:58:57 -07:00
}
static void moo_term_window_init (MooTermWindow *window)
{
g_object_set (G_OBJECT (window),
"menubar-ui-name", "Terminal/Menubar",
"toolbar-ui-name", "Terminal/Toolbar",
NULL);
}
static GObject *moo_term_window_constructor (GType type,
guint n_props,
GObjectConstructParam *props)
{
MooTermWindow *window;
GtkWidget *scrolledwindow;
GtkWidget *terminal;
GObject *object =
G_OBJECT_CLASS(moo_term_window_parent_class)->constructor (type, n_props, props);
window = MOO_TERM_WINDOW (object);
/************************************************************************/
/* Gui */
/***/
scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_show (scrolledwindow);
gtk_box_pack_start (GTK_BOX (MOO_WINDOW(window)->vbox), scrolledwindow, TRUE, TRUE, 0);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
GTK_POLICY_NEVER,
GTK_POLICY_ALWAYS);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow),
GTK_SHADOW_ETCHED_OUT);
if (window->term_type)
{
if (!g_type_is_a (window->term_type, MOO_TYPE_TERM))
{
g_critical ("%s: oops", G_STRLOC);
window->term_type = MOO_TYPE_TERM;
}
}
else
{
window->term_type = MOO_TYPE_TERM;
}
terminal = g_object_new (window->term_type, NULL);
2005-07-23 21:58:57 -07:00
gtk_widget_show (terminal);
gtk_container_add (GTK_CONTAINER (scrolledwindow), terminal);
GTK_WIDGET_SET_FLAGS (terminal, GTK_CAN_FOCUS);
GTK_WIDGET_SET_FLAGS (terminal, GTK_CAN_DEFAULT);
gtk_widget_grab_focus (terminal);
gtk_widget_grab_default (terminal);
/************************************************************************/
/* MOO_TERM_WINDOW stuff */
/***/
window->terminal = MOO_TERM (terminal);
gtk_widget_show (MOO_WINDOW(window)->vbox);
#if 0
TODO TODO
// GtkWidget *popup =
// moo_ui_xml_create_widget (xml, "Terminal/Popup",
// moo_ui_object_get_actions (MOO_UI_OBJECT (window)),
// MOO_WINDOW (window)->accel_group,
// MOO_WINDOW (window)->tooltips);
// if (popup) moo_term_set_popup_menu (window->terminal, popup);
#endif
return object;
}
void moo_term_window_apply_settings (MooTermWindow *window)
{
g_return_if_fail (MOO_IS_TERM_WINDOW (window));
g_signal_emit_by_name (window->terminal, "apply_settings", NULL);
}
GtkWidget *moo_term_window_new (void)
{
return GTK_WIDGET (g_object_new (MOO_TYPE_TERM_WINDOW, NULL));
}
static void moo_term_window_save_selection (MooTermWindow *self)
{
char *text = moo_term_get_selection (self->terminal);
if (!text)
text = moo_term_get_content (self->terminal);
if (text)
{
const char *filename =
moo_file_dialogp (GTK_WIDGET (self),
MOO_DIALOG_FILE_SAVE,
"Save As",
MOO_TERM_PREFS_SAVE_SELECTION_DIR,
NULL);
if (filename)
{
char *new_start = g_path_get_dirname (filename);
moo_prefs_set_string (MOO_TERM_PREFS_SAVE_SELECTION_DIR, new_start);
2005-07-23 21:58:57 -07:00
g_free (new_start);
moo_save_file_utf8 (filename, text, -1, NULL);
}
g_free (text);
}
}
MooTerm *moo_term_window_get_term (MooTermWindow *window)
{
g_return_val_if_fail (MOO_IS_TERM_WINDOW (window), NULL);
return window->terminal;
}
void
moo_term_window_set_term_type (MooTermWindow *window,
GType type)
{
g_return_if_fail (MOO_IS_TERM_WINDOW (window));
g_return_if_fail (g_type_is_a (type, MOO_TYPE_TERM));
g_return_if_fail (window->terminal == NULL);
window->term_type = type;
}
2006-02-27 18:04:38 -08:00
static void
copy_clipboard (MooTerm *term)
{
moo_term_copy_clipboard (term, GDK_SELECTION_CLIPBOARD);
}
static void
paste_clipboard (MooTerm *term)
{
moo_term_paste_clipboard (term, GDK_SELECTION_CLIPBOARD);
}