1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
12 #include "liblj/editfriends.h"
20 #include "friendedit.h"
25 GtkWidget
*eusername
, *ebgcolor
, *efgcolor
;
28 JamAccountLJ
*account
;
32 update_preview(FriendEditUI
*feui
) {
35 gdk_color_parse(gtk_entry_get_text(GTK_ENTRY(feui
->efgcolor
)), &fg
);
36 gdk_color_parse(gtk_entry_get_text(GTK_ENTRY(feui
->ebgcolor
)), &bg
);
38 gtk_widget_modify_text(feui
->eusername
, GTK_STATE_NORMAL
, &fg
);
39 gtk_widget_modify_base(feui
->eusername
, GTK_STATE_NORMAL
, &bg
);
43 color_entry_changed(GtkEntry
*e
, FriendEditUI
*feui
) {
44 if (strlen(gtk_entry_get_text(e
)) == 7)
49 change_entry_color_dlg(FriendEditUI
*feui
, GtkWidget
*toedit
, const char *title
) {
51 GtkColorSelection
*csel
;
56 dlg
= gtk_color_selection_dialog_new(title
);
57 gtk_window_set_transient_for(GTK_WINDOW(dlg
), GTK_WINDOW(feui
->win
));
58 gtk_widget_hide(GTK_COLOR_SELECTION_DIALOG(dlg
)->help_button
);
60 csel
= GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(dlg
)->colorsel
);
62 curcolor
= gtk_entry_get_text(GTK_ENTRY(toedit
));
64 /* convert existing hex color to the color selection's color */
65 if (strlen(curcolor
) == 7 && curcolor
[0] == '#') {
66 gdk_color_parse(curcolor
, &color
);
67 gtk_color_selection_set_current_color(csel
, &color
);
70 if (gtk_dialog_run(GTK_DIALOG(dlg
)) == GTK_RESPONSE_OK
) {
71 gtk_color_selection_get_current_color(csel
, &color
);
73 gdkcolor_to_hex(&color
, new_hex
);
75 gtk_entry_set_text(GTK_ENTRY(toedit
), new_hex
);
77 gtk_widget_destroy(dlg
);
83 change_col_bg(GtkWidget
*w
, FriendEditUI
*feui
) {
84 change_entry_color_dlg(feui
, feui
->ebgcolor
, _("Select Background Color"));
88 change_col_fg(GtkWidget
*w
, FriendEditUI
*feui
) {
89 change_entry_color_dlg(feui
, feui
->efgcolor
, _("Select Foreground Color"));
93 add_the_friend(FriendEditUI
*feui
) {
96 gchar
*username
, *name
;
98 ctx
= net_ctx_gtk_new(GTK_WINDOW(feui
->win
), _("Adding Friend"));
100 ef
= lj_editfriends_new(jam_account_lj_get_user(feui
->account
));
101 lj_editfriends_add_friend(ef
,
102 gtk_entry_get_text(GTK_ENTRY(feui
->eusername
)),
103 gtk_entry_get_text(GTK_ENTRY(feui
->efgcolor
)),
104 gtk_entry_get_text(GTK_ENTRY(feui
->ebgcolor
)));
106 if (!net_run_verb_ctx((LJVerb
*)ef
, ctx
, NULL
) || ef
->addcount
!= 1) {
107 lj_editfriends_free(ef
);
108 net_ctx_gtk_free(ctx
);
112 name
= ef
->added
[0].fullname
;
113 username
= ef
->added
[0].username
;
115 if (feui
->editfriend
== NULL
||
116 strcmp(feui
->editfriend
->username
, username
) != 0) {
117 /* we must create a new friend */
118 feui
->editfriend
= lj_friend_new();
119 feui
->editfriend
->conn
= LJ_FRIEND_CONN_MY
;
122 string_replace(&feui
->editfriend
->username
, g_strdup(username
));
123 string_replace(&feui
->editfriend
->fullname
, g_strdup(name
));
124 feui
->editfriend
->foreground
= lj_color_to_int(
125 gtk_entry_get_text(GTK_ENTRY(feui
->efgcolor
)));
126 feui
->editfriend
->background
= lj_color_to_int(
127 gtk_entry_get_text(GTK_ENTRY(feui
->ebgcolor
)));
129 lj_editfriends_free(ef
);
130 net_ctx_gtk_free(ctx
);
135 entry_changed(GtkEntry
*entry
, FriendEditUI
* feui
) {
136 gtk_dialog_set_response_sensitive(GTK_DIALOG(feui
->win
),
138 (strlen(gtk_entry_get_text(entry
)) > 0));
142 friend_edit_dlg_run(GtkWindow
*parent
, JamAccountLJ
*acc
, gboolean edit
, LJFriend
*f
) {
143 FriendEditUI feui_actual
= { 0 };
144 FriendEditUI
*feui
= &feui_actual
;
149 feui
->editfriend
= f
;
151 feui
->win
= gtk_dialog_new_with_buttons(
152 edit
? _("Edit Friend") :
153 (f
? _("Add This Friend") : _("Add a Friend")),
154 parent
, GTK_DIALOG_MODAL
,
155 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
156 edit
? _("Change") : _("Add"), GTK_RESPONSE_OK
,
158 gtk_dialog_set_default_response(GTK_DIALOG(feui
->win
), GTK_RESPONSE_OK
);
160 table
= jam_table_new(3, 3);
162 /* make the labels/entries */
163 feui
->eusername
= gtk_entry_new();
164 gtk_entry_set_activates_default(GTK_ENTRY(feui
->eusername
), TRUE
);
165 gtk_entry_set_max_length(GTK_ENTRY(feui
->eusername
), 18);
167 gtk_editable_set_editable(GTK_EDITABLE(feui
->eusername
), FALSE
);
169 /* enable/disable the button based on name text */
170 g_signal_connect(G_OBJECT(feui
->eusername
), "changed",
171 G_CALLBACK(entry_changed
), feui
);
173 gtk_widget_set_size_request(feui
->eusername
, 100, -1);
174 jam_table_label_content(GTK_TABLE(table
), 0,
175 _("Friend's _username:"), feui
->eusername
);
177 feui
->efgcolor
= gtk_entry_new();
178 gtk_entry_set_activates_default(GTK_ENTRY(feui
->efgcolor
), TRUE
);
179 gtk_entry_set_max_length(GTK_ENTRY(feui
->efgcolor
), 7);
180 g_signal_connect(G_OBJECT(feui
->efgcolor
), "changed",
181 G_CALLBACK(color_entry_changed
), feui
);
182 gtk_widget_set_size_request(feui
->efgcolor
, 100, -1);
183 jam_table_label_content(GTK_TABLE(table
), 1,
184 _("_Text color:"), feui
->efgcolor
);
186 feui
->ebgcolor
= gtk_entry_new();
187 gtk_entry_set_activates_default(GTK_ENTRY(feui
->ebgcolor
), TRUE
);
188 gtk_entry_set_max_length(GTK_ENTRY(feui
->ebgcolor
), 7);
189 g_signal_connect(G_OBJECT(feui
->ebgcolor
), "changed",
190 G_CALLBACK(color_entry_changed
), feui
);
191 gtk_widget_set_size_request(feui
->ebgcolor
, 100, -1);
192 jam_table_label_content(GTK_TABLE(table
), 2,
193 _("_Background color:"), feui
->ebgcolor
);
195 /* make the color selector buttons */
196 button
= gtk_button_new_with_label(" ... ");
197 gtk_table_attach(GTK_TABLE(table
), button
, 2, 3, 1, 2, GTK_FILL
, 0, 2, 2);
198 g_signal_connect(G_OBJECT(button
), "clicked",
199 G_CALLBACK(change_col_fg
), feui
);
200 button
= gtk_button_new_with_label(" ... ");
201 gtk_table_attach(GTK_TABLE(table
), button
, 2, 3, 2, 3, GTK_FILL
, 0, 2, 2);
202 g_signal_connect(G_OBJECT(button
), "clicked",
203 G_CALLBACK(change_col_bg
), feui
);
205 jam_dialog_set_contents(GTK_DIALOG(feui
->win
), table
);
207 /* fill in default values. */
209 gtk_entry_set_text(GTK_ENTRY(feui
->eusername
), f
->username
);
211 /* emit the "changed" signal, in any case. */
212 g_signal_emit_by_name(G_OBJECT(feui
->eusername
), "changed");
216 gtk_entry_set_text(GTK_ENTRY(feui
->efgcolor
), "#000000");
217 gtk_entry_set_text(GTK_ENTRY(feui
->ebgcolor
), "#FFFFFF");
220 lj_int_to_color(f
->foreground
, color
);
221 gtk_entry_set_text(GTK_ENTRY(feui
->efgcolor
), color
);
222 lj_int_to_color(f
->background
, color
);
223 gtk_entry_set_text(GTK_ENTRY(feui
->ebgcolor
), color
);
226 while (gtk_dialog_run(GTK_DIALOG(feui
->win
)) == GTK_RESPONSE_OK
) {
227 if (add_the_friend(feui
)) {
228 gtk_widget_destroy(feui
->win
);
229 return feui
->editfriend
;
232 gtk_widget_destroy(feui
->win
);