1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
6 * anjuta is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * anjuta is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "git-repository-selector.h"
22 struct _GitRepositorySelectorPriv
24 GtkWidget
*remote_toggle
;
25 GtkWidget
*url_toggle
;
27 GtkWidget
*selected_remote_label
;
29 GitRepositorySelectorMode mode
;
33 G_DEFINE_TYPE (GitRepositorySelector
, git_repository_selector
, GTK_TYPE_VBOX
);
36 on_mode_button_toggled (GtkToggleButton
*button
, GitRepositorySelector
*self
)
38 GitRepositorySelectorMode mode
;
40 if (gtk_toggle_button_get_active (button
))
42 /* Each mode corresponds to a page in the notebook */
43 mode
= GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button
), "mode"));
44 self
->priv
->mode
= mode
;
46 gtk_notebook_set_current_page (GTK_NOTEBOOK (self
->priv
->notebook
),
52 git_repository_selector_init (GitRepositorySelector
*self
)
54 GtkWidget
*button_hbox
;
55 GtkWidget
*remote_hbox
;
58 self
->priv
= g_new0 (GitRepositorySelectorPriv
, 1);
60 /* Mode selector buttons. Allows the user to use a selected remote or
62 button_hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 0);
63 gtk_box_set_homogeneous (GTK_BOX(button_hbox
), TRUE
);
65 /* Remote toggle button */
66 self
->priv
->remote_toggle
= gtk_radio_button_new_with_label (NULL
,
68 g_object_set (G_OBJECT (self
->priv
->remote_toggle
), "draw-indicator", FALSE
,
70 gtk_box_pack_start (GTK_BOX (button_hbox
), self
->priv
->remote_toggle
, TRUE
,
73 g_object_set_data (G_OBJECT (self
->priv
->remote_toggle
), "mode",
74 GINT_TO_POINTER (GIT_REPOSITORY_SELECTOR_REMOTE
));
76 g_signal_connect (G_OBJECT (self
->priv
->remote_toggle
), "toggled",
77 G_CALLBACK (on_mode_button_toggled
),
80 /* URL toggle button */
81 self
->priv
->url_toggle
= gtk_radio_button_new_with_label (gtk_radio_button_get_group (GTK_RADIO_BUTTON (self
->priv
->remote_toggle
)),
83 g_object_set (G_OBJECT (self
->priv
->url_toggle
), "draw-indicator", FALSE
,
85 gtk_box_pack_start (GTK_BOX (button_hbox
), self
->priv
->url_toggle
, TRUE
,
88 g_object_set_data (G_OBJECT (self
->priv
->url_toggle
), "mode",
89 GINT_TO_POINTER (GIT_REPOSITORY_SELECTOR_URL
));
91 g_signal_connect (G_OBJECT (self
->priv
->url_toggle
), "toggled",
92 G_CALLBACK (on_mode_button_toggled
),
95 gtk_box_pack_start (GTK_BOX (self
), button_hbox
, FALSE
, FALSE
, 0);
97 /* Selected remote label */
98 remote_hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 2);
100 label
= gtk_label_new (NULL
);
101 gtk_label_set_markup (GTK_LABEL (label
), _("<b>Selected Remote:</b>"));
102 gtk_box_pack_start (GTK_BOX (remote_hbox
), label
, FALSE
, FALSE
, 0);
104 self
->priv
->selected_remote_label
= gtk_label_new (NULL
);
105 g_object_set (G_OBJECT (self
->priv
->selected_remote_label
), "xalign",
107 gtk_box_pack_start (GTK_BOX (remote_hbox
),
108 self
->priv
->selected_remote_label
, TRUE
, TRUE
, 0);
111 self
->priv
->url_entry
= gtk_entry_new ();
114 self
->priv
->notebook
= gtk_notebook_new ();
115 gtk_notebook_set_show_border (GTK_NOTEBOOK (self
->priv
->notebook
), FALSE
);
116 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (self
->priv
->notebook
), FALSE
);
117 gtk_notebook_append_page (GTK_NOTEBOOK (self
->priv
->notebook
), remote_hbox
,
119 gtk_notebook_append_page (GTK_NOTEBOOK (self
->priv
->notebook
),
120 self
->priv
->url_entry
, NULL
);
122 gtk_box_pack_start (GTK_BOX (self
), self
->priv
->notebook
, TRUE
, TRUE
, 0);
124 /* Set the selected repository label to a resonable default. */
125 git_repository_selector_set_remote (self
, NULL
);
128 gtk_widget_set_can_focus (GTK_WIDGET (self
), TRUE
);
130 gtk_widget_show_all (GTK_WIDGET (self
));
134 git_repository_selector_finalize (GObject
*object
)
136 GitRepositorySelector
*self
;
138 self
= GIT_REPOSITORY_SELECTOR (object
);
140 g_free (self
->priv
->remote
);
143 G_OBJECT_CLASS (git_repository_selector_parent_class
)->finalize (object
);
147 git_repository_selector_grab_focus (GtkWidget
*widget
)
149 GitRepositorySelector
*self
;
151 self
= GIT_REPOSITORY_SELECTOR (widget
);
153 GTK_WIDGET_CLASS (git_repository_selector_parent_class
)->grab_focus (widget
);
155 /* Only the URL entry can grab focus */
156 gtk_notebook_set_current_page (GTK_NOTEBOOK (self
->priv
->notebook
),
157 GIT_REPOSITORY_SELECTOR_URL
);
158 gtk_widget_grab_focus (self
->priv
->url_entry
);
162 git_repository_selector_class_init (GitRepositorySelectorClass
*klass
)
164 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
165 GtkWidgetClass
*widget_class
= GTK_WIDGET_CLASS (klass
);
167 object_class
->finalize
= git_repository_selector_finalize
;
168 widget_class
->grab_focus
= git_repository_selector_grab_focus
;
173 git_repository_selector_new (void)
175 return g_object_new (GIT_TYPE_REPOSITORY_SELECTOR
, NULL
);
178 GitRepositorySelectorMode
179 git_repository_selector_get_mode (GitRepositorySelector
*self
)
181 return self
->priv
->mode
;
185 git_repository_selector_set_remote (GitRepositorySelector
*self
,
188 g_free (self
->priv
->remote
);
189 self
->priv
->remote
= NULL
;
193 self
->priv
->remote
= g_strdup (remote
);
194 gtk_label_set_text (GTK_LABEL (self
->priv
->selected_remote_label
),
199 gtk_label_set_text (GTK_LABEL (self
->priv
->selected_remote_label
),
200 _("No remote selected; using origin by default.\n"
201 "To push to a different remote, select one from "
202 "the Remotes list above."));
207 git_repository_selector_get_repository (GitRepositorySelector
*self
)
209 if (self
->priv
->mode
== GIT_REPOSITORY_SELECTOR_REMOTE
)
211 if (self
->priv
->remote
)
212 return g_strdup (self
->priv
->remote
);
214 return g_strdup ("origin");
218 return gtk_editable_get_chars (GTK_EDITABLE (self
->priv
->url_entry
), 0,