GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / dialogs / dialog-insert-cells.c
blob976419a3d0edb39d62bd724c2109ef3e2feec4a9
1 /*
2 * dialog-insert-cells.c: Insert a number of cells.
4 * Authors:
5 * Miguel de Icaza (miguel@gnu.org)
6 * Copyright (C) Andreas J. Guelzow (aguelzow@taliesin.ca)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <https://www.gnu.org/licenses/>.
21 #include <gnumeric-config.h>
22 #include <glib/gi18n-lib.h>
23 #include <gnumeric.h>
24 #include "dialogs.h"
25 #include "help.h"
27 #include <gui-util.h>
28 #include <selection.h>
29 #include <workbook.h>
30 #include <sheet.h>
31 #include <sheet-view.h>
32 #include <commands.h>
33 #include <ranges.h>
34 #include <cmd-edit.h>
35 #include <wbc-gtk.h>
36 #include <command-context.h>
38 #include <gtk/gtk.h>
40 #define INSERT_CELL_DIALOG_KEY "insert-cells-dialog"
42 typedef struct {
43 WBCGtk *wbcg;
44 GtkWidget *dialog;
45 GtkWidget *ok_button;
46 GtkWidget *cancel_button;
47 GnmRange const *sel;
48 Sheet *sheet;
49 GtkBuilder *gui;
50 } InsertCellState;
52 static void
53 cb_insert_cell_destroy (InsertCellState *state)
55 if (state->gui != NULL)
56 g_object_unref (state->gui);
57 g_free (state);
60 static void
61 cb_insert_cell_ok_clicked (G_GNUC_UNUSED GtkWidget *button,
62 InsertCellState *state)
64 WorkbookControl *wbc = GNM_WBC (state->wbcg);
65 GtkWidget *radio_0;
66 int cols, rows;
67 int i;
69 radio_0 = go_gtk_builder_get_widget (state->gui, "radio_0");
70 g_return_if_fail (radio_0 != NULL);
72 i = gnm_gtk_radio_group_get_selected
73 (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio_0)));
75 cols = state->sel->end.col - state->sel->start.col + 1;
76 rows = state->sel->end.row - state->sel->start.row + 1;
78 switch (i) {
79 case 0 :
80 cmd_shift_rows (wbc, state->sheet,
81 state->sel->start.col,
82 state->sel->start.row,
83 state->sel->end.row, cols);
84 break;
85 case 1 :
86 cmd_shift_cols (wbc, state->sheet,
87 state->sel->start.col,
88 state->sel->end.col,
89 state->sel->start.row, rows);
90 break;
91 case 2 :
92 cmd_insert_rows (wbc, state->sheet,
93 state->sel->start.row, rows);
94 break;
95 default :
96 cmd_insert_cols (wbc, state->sheet,
97 state->sel->start.col, cols);
98 break;
100 gtk_widget_destroy (state->dialog);
103 static void
104 cb_insert_cell_cancel_clicked (G_GNUC_UNUSED GtkWidget *button,
105 InsertCellState *state)
107 gtk_widget_destroy (state->dialog);
108 return;
111 void
112 dialog_insert_cells (WBCGtk *wbcg)
114 GtkBuilder *gui;
115 InsertCellState *state;
116 WorkbookControl *wbc = GNM_WBC (wbcg);
117 SheetView *sv = wb_control_cur_sheet_view (wbc);
118 Sheet *sheet = sv_sheet (sv);
119 GnmRange const *sel;
120 int cols, rows;
122 g_return_if_fail (wbcg != NULL);
124 if (!(sel = selection_first_range (sv, GO_CMD_CONTEXT (wbc), _("Insert"))))
125 return;
126 cols = sel->end.col - sel->start.col + 1;
127 rows = sel->end.row - sel->start.row + 1;
129 if (range_is_full (sel, sheet, FALSE)) {
130 cmd_insert_cols (wbc, sheet, sel->start.col, cols);
131 return;
133 if (range_is_full (sel, sheet, TRUE)) {
134 cmd_insert_rows (wbc, sheet, sel->start.row, rows);
135 return;
138 if (gnm_dialog_raise_if_exists (wbcg, INSERT_CELL_DIALOG_KEY))
139 return;
141 gui = gnm_gtk_builder_load ("res:ui/insert-cells.ui", NULL, GO_CMD_CONTEXT (wbcg));
142 if (gui == NULL)
143 return;
145 state = g_new (InsertCellState, 1);
146 state->wbcg = wbcg;
147 state->sel = sel;
148 state->sheet = sv_sheet (sv);
149 state->gui = gui;
150 state->dialog = go_gtk_builder_get_widget (state->gui, "Insert_cells");
151 if (state->dialog == NULL) {
152 go_gtk_notice_dialog (wbcg_toplevel (wbcg), GTK_MESSAGE_ERROR,
153 _("Could not create the Insert Cell dialog."));
154 g_free (state);
155 return ;
158 gnm_dialog_setup_destroy_handlers (GTK_DIALOG (state->dialog),
159 state->wbcg,
160 GNM_DIALOG_DESTROY_CURRENT_SHEET_REMOVED);
162 state->ok_button = go_gtk_builder_get_widget (state->gui, "okbutton");
163 g_signal_connect (G_OBJECT (state->ok_button),
164 "clicked",
165 G_CALLBACK (cb_insert_cell_ok_clicked), state);
167 state->cancel_button = go_gtk_builder_get_widget (state->gui, "cancelbutton");
168 g_signal_connect (G_OBJECT (state->cancel_button),
169 "clicked",
170 G_CALLBACK (cb_insert_cell_cancel_clicked), state);
172 gnm_init_help_button (
173 go_gtk_builder_get_widget (state->gui, "helpbutton"),
174 GNUMERIC_HELP_LINK_INSERT_CELS);
175 gtk_toggle_button_set_active
176 (GTK_TOGGLE_BUTTON (go_gtk_builder_get_widget
177 (state->gui, cols < rows
178 ? "radio_0" : "radio_1")),
179 TRUE);
181 wbc_gtk_attach_guru (state->wbcg, state->dialog);
182 g_object_set_data_full (G_OBJECT (state->dialog),
183 "state", state, (GDestroyNotify) cb_insert_cell_destroy);
185 gnm_keyed_dialog (wbcg, GTK_WINDOW (state->dialog),
186 INSERT_CELL_DIALOG_KEY);
187 gtk_widget_show (state->dialog);