2 * dialog-sheet-rename.c: Dialog to rename current sheet.
5 * Morten Welinder <terra@gnome.org>
7 * (C) Copyright 2013 Morten Welinder <terra@gnome.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
31 #include <workbook-view.h>
36 #define RENAME_DIALOG_KEY "sheet-rename-dialog"
42 GtkWidget
*old_name
, *new_name
;
43 GtkWidget
*ok_button
, *cancel_button
;
44 gint signal_connect_id_cb_dialog_size_allocate
;
48 cb_name_changed (GtkEntry
*e
, RenameState
*state
)
50 const gchar
*name
= gtk_entry_get_text (e
);
51 Sheet
*sheet2
= workbook_sheet_by_name (state
->sheet
->workbook
, name
);
54 valid
= (*name
!= 0) && (sheet2
== NULL
|| sheet2
== state
->sheet
);
56 gtk_widget_set_sensitive (state
->ok_button
, valid
);
60 cb_ok_clicked (RenameState
*state
)
62 const gchar
*name
= gtk_entry_get_text (GTK_ENTRY (state
->new_name
));
64 if (! cmd_rename_sheet (GNM_WBC (state
->wbcg
),
67 gtk_widget_destroy (state
->dialog
);
71 gtk_entry_set_size_all_text_visible (GtkEntry
*entry
)
73 PangoContext
*context
;
74 PangoFontMetrics
*metrics
;
78 PangoLayout
*pango_layout
;
83 /* Logic borrowed from GtkEntry::gtk_entry_measure() */
84 context
= gtk_widget_get_pango_context (GTK_WIDGET (entry
));
85 metrics
= pango_context_get_metrics (context
,
86 pango_context_get_font_description (context
),
87 pango_context_get_language (context
));
89 char_width
= pango_font_metrics_get_approximate_char_width (metrics
);
90 digit_width
= pango_font_metrics_get_approximate_digit_width (metrics
);
91 char_pixels
= (MAX (char_width
, digit_width
) + PANGO_SCALE
- 1) / PANGO_SCALE
;
93 pango_layout
= gtk_entry_get_layout (entry
);
94 char_count
= pango_layout_get_character_count (pango_layout
);
95 min_width
= char_pixels
* char_count
;
96 actual_width
= gtk_widget_get_allocated_width (GTK_WIDGET (entry
));
98 if (actual_width
< min_width
)
99 gtk_entry_set_width_chars (entry
, char_count
);
103 cb_dialog_size_allocate (GtkWidget
*dialog
, GdkRectangle
*allocation
, RenameState
*state
)
107 g_signal_handler_disconnect (G_OBJECT (dialog
),
108 state
->signal_connect_id_cb_dialog_size_allocate
);
110 /* dummy values for min/max_width to not restrict horizontal resizing */
112 hints
.max_width
= G_MAXINT
;
113 /* do not allow vertial resizing */
114 hints
.min_height
= allocation
->height
;
115 hints
.max_height
= allocation
->height
;
116 gtk_window_set_geometry_hints (GTK_WINDOW (dialog
), (GtkWidget
*) NULL
,
118 (GdkWindowHints
) (GDK_HINT_MIN_SIZE
| GDK_HINT_MAX_SIZE
));
120 gtk_entry_set_size_all_text_visible (GTK_ENTRY (state
->new_name
));
124 dialog_sheet_rename (WBCGtk
*wbcg
, Sheet
*sheet
)
129 if (gnm_dialog_raise_if_exists (wbcg
, RENAME_DIALOG_KEY
))
131 gui
= gnm_gtk_builder_load ("res:ui/sheet-rename.ui", NULL
, GO_CMD_CONTEXT (wbcg
));
135 state
= g_new (RenameState
, 1);
137 state
->dialog
= go_gtk_builder_get_widget (gui
, "Rename");
138 state
->sheet
= sheet
;
139 g_return_if_fail (state
->dialog
!= NULL
);
141 state
->signal_connect_id_cb_dialog_size_allocate
=
142 g_signal_connect (G_OBJECT (state
->dialog
),
144 G_CALLBACK (cb_dialog_size_allocate
),
147 state
->old_name
= go_gtk_builder_get_widget (gui
, "old_name");
148 gtk_entry_set_text (GTK_ENTRY (state
->old_name
), sheet
->name_unquoted
);
150 state
->new_name
= go_gtk_builder_get_widget (gui
, "new_name");
151 gtk_entry_set_text (GTK_ENTRY (state
->new_name
), sheet
->name_unquoted
);
153 gtk_editable_select_region (GTK_EDITABLE (state
->new_name
), 0, -1);
154 gtk_widget_grab_focus (state
->new_name
);
155 g_signal_connect (G_OBJECT (state
->new_name
),
156 "changed", G_CALLBACK (cb_name_changed
),
158 gnm_editable_enters (GTK_WINDOW (state
->dialog
), state
->new_name
);
160 state
->ok_button
= go_gtk_builder_get_widget (gui
, "ok_button");
161 g_signal_connect_swapped (G_OBJECT (state
->ok_button
),
162 "clicked", G_CALLBACK (cb_ok_clicked
),
165 state
->cancel_button
= go_gtk_builder_get_widget (gui
, "cancel_button");
166 g_signal_connect_swapped (G_OBJECT (state
->cancel_button
),
167 "clicked", G_CALLBACK (gtk_widget_destroy
),
170 gnm_dialog_setup_destroy_handlers (GTK_DIALOG (state
->dialog
), wbcg
,
171 GNM_DIALOG_DESTROY_SHEET_REMOVED
);
173 gnm_keyed_dialog (wbcg
, GTK_WINDOW (state
->dialog
),
176 g_object_set_data_full (G_OBJECT (state
->dialog
),
178 (GDestroyNotify
) g_free
);
179 g_object_unref (gui
);
181 gtk_widget_show (state
->dialog
);