forked from akkana/pho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeydialog.c
323 lines (267 loc) · 9.46 KB
/
keydialog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* keydialog.c: the pho keywords dialog.
*
* Copyright 2007 by Akkana Peck.
* You are free to use or modify this code under the Gnu Public License.
*/
#include "pho.h"
#include "dialogs.h"
#include <gdk/gdkkeysyms.h>
#include <stdio.h> /* needed on Mac, not on Linux, for sprintf */
#include <ctype.h>
#include <string.h>
#include <stdlib.h> /* for malloc() and free() */
static GtkWidget* KeywordsDialog = 0;
static GtkWidget* KeywordsCaption = 0;
static GtkWidget* KeywordsDEntry[NUM_NOTES] = {0};
static GtkWidget* KeywordsDToggle[NUM_NOTES] = {0};
static GtkWidget* KeywordsDImgName = 0;
static GtkWidget* KeywordsContainer = 0; /* where the Entries live */
static PhoImage* sLastImage = 0;
static void LeaveKeywordsMode()
{
SetViewModes(PHO_DISPLAY_NORMAL, PHO_SCALE_NORMAL, 1.0);
}
void ToggleKeywordsMode()
{
if (gDisplayMode == PHO_DISPLAY_KEYWORDS)
LeaveKeywordsMode();
else {
SetViewModes(PHO_DISPLAY_KEYWORDS, PHO_SCALE_FIXED, 0.0);
ThisImage();
}
}
/* Make sure we remember any changes that have been made in the dialog */
void RememberKeywords()
{
int i, mask, flags;
if (!sLastImage)
return;
flags = 0;
for (i=0, mask=1; i < NUM_NOTES; ++i, mask <<= 1)
{
if (KeywordsDToggle[i] &&
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(KeywordsDToggle[i])))
flags |= mask;
}
sLastImage->noteFlags = flags;
/* and save a caption, if any */
if (sLastImage->caption)
free(sLastImage->caption);
sLastImage->caption = strdup((char*)gtk_entry_get_text(
(GtkEntry*)KeywordsCaption));
}
/* When deleting an image, we need to clear any notion of sLastImage
* or else we'll crash trying to access it.
*/
void NoCurrentKeywords()
{
sLastImage = 0;
}
void SetKeywordsDialogToggle(int which, int newval)
{
if (KeywordsDToggle[which])
gtk_toggle_button_set_active((GtkToggleButton*)KeywordsDToggle[which],
newval ? TRUE : FALSE);
}
void UpdateKeywordsDialog()
{
char buffer[256];
char* s;
int i, mask, flags;
if (!gCurImage || !KeywordsDialog || gDisplayMode != PHO_DISPLAY_KEYWORDS)
return;
if (!IsVisible(KeywordsDialog)) return;
if (gCurImage != sLastImage)
sLastImage = gCurImage;
sprintf(buffer, "pho keywords (%s)", gCurImage->filename);
gtk_window_set_title(GTK_WINDOW(KeywordsDialog), buffer);
s = gCurImage->caption;
gtk_entry_set_text(GTK_ENTRY(KeywordsCaption), s ? s : "");
gtk_label_set_text(GTK_LABEL(KeywordsDImgName), gCurImage->filename);
/* Update the flags fields */
flags = gCurImage->noteFlags;
for (i=0, mask=1; i < NUM_NOTES; ++i, mask <<= 1)
{
if (KeywordsDToggle[i])
SetKeywordsDialogToggle(i, flags & mask);
}
}
char* KeywordString(int notenum)
{
if (! KeywordsDEntry[notenum])
return 0;
return (char*)gtk_entry_get_text((GtkEntry*)KeywordsDEntry[notenum]);
}
static void AddNewKeywordField();
/* When the user hits return in the last keyword field,
* add a new one, assuming we don't already have too many.
*/
static void activate(GtkEntry *entry, int which)
{
if (which < NUM_NOTES-1 && KeywordsDEntry[which+1] == 0)
AddNewKeywordField();
}
static gint handleKeywordsKeyPress(GtkWidget* widget, GdkEventKey* event)
{
/* We only handle a few key events that aren't shifted: */
switch (event->keyval)
{
case GDK_Escape:
LeaveKeywordsMode();
return TRUE;
}
/* But we handle certain other events if a modifier key is down */
if (! (event->state & GDK_MODIFIER_MASK))
return FALSE;
/* But don't just pass all modifier events -- many of them are
* meaningful when editing a keyword in a text field!
*/
/* Shifted printable keys should probably stay here */
if (event->state & GDK_SHIFT_MASK && (event->length > 0)
&& isprint(event->string[0]))
return FALSE;
/* Emacs/readline editing keys are also useful */
if (event->state & GDK_CONTROL_MASK) {
switch (event->keyval)
{
case GDK_a:
case GDK_e:
case GDK_u:
case GDK_h:
case GDK_w:
case GDK_k:
case GDK_d:
return FALSE;
}
}
/* Otherwise, it's probably a pho key binding, so pass it on: */
return HandleGlobalKeys(widget, event);
}
/* Add a new keyword field to the dialog */
static void AddNewKeywordField()
{
long i;
GtkWidget* label;
char buf[BUFSIZ];
for (i=0; i<NUM_NOTES; ++i)
{
if (KeywordsDEntry[i] == 0)
{
GtkWidget* hbox = gtk_hbox_new(FALSE, 3);
gtk_box_pack_start(GTK_BOX(KeywordsContainer), hbox,
TRUE, TRUE, 4);
sprintf(buf, "%-2ld", i);
KeywordsDToggle[i] = gtk_toggle_button_new_with_label(buf);
gtk_box_pack_start(GTK_BOX(hbox), KeywordsDToggle[i],
FALSE, FALSE, 4);
gtk_toggle_button_set_active((GtkToggleButton*)KeywordsDToggle[i],
TRUE);
gtk_widget_show(KeywordsDToggle[i]);
KeywordsDEntry[i] = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(hbox), KeywordsDEntry[i],
TRUE, TRUE, 4);
gtk_signal_connect(GTK_OBJECT(KeywordsDEntry[i]), "activate",
(GtkSignalFunc)activate, (gpointer)i);
gtk_widget_show(KeywordsDEntry[i]);
gtk_widget_show(hbox);
gtk_widget_grab_focus(KeywordsDEntry[i]);
return;
}
}
/* If we got here, we've overflowed */
sprintf(buf, "That's all: sorry, only %ld keywords at once", NUM_NOTES);
label = gtk_label_new(buf);
gtk_box_pack_start(GTK_BOX(KeywordsContainer), label, TRUE, TRUE, 4);
gtk_widget_show(label);
}
static void MakeNewKeywordsDialog()
{
GtkWidget *ok, *label;
GtkWidget *dlg_vbox, *sep, *btn_box, *hbox;
int i;
/* Use a toplevel window, so it won't pop up centered on the image win */
KeywordsDialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* Unfortunately that means we have to make everything inside. */
/* Tried making it a utility win for focus issues, but it didn't help:
gtk_window_set_type_hint (GTK_WINDOW(KeywordsDialog),
GDK_WINDOW_TYPE_HINT_UTILITY);
*/
dlg_vbox = gtk_vbox_new(FALSE, 3);
gtk_container_add(GTK_CONTAINER(KeywordsDialog), dlg_vbox);
gtk_widget_show(dlg_vbox);
gtk_signal_connect(GTK_OBJECT(KeywordsDialog), "key_press_event",
(GtkSignalFunc)handleKeywordsKeyPress, 0);
KeywordsContainer = gtk_vbox_new(FALSE, 3);
gtk_container_add(GTK_CONTAINER(dlg_vbox), KeywordsContainer);
gtk_widget_show(KeywordsContainer);
sep = gtk_hseparator_new();
gtk_container_add(GTK_CONTAINER(dlg_vbox), sep);
gtk_widget_show(sep);
btn_box = gtk_hbox_new(FALSE, 3);
gtk_container_add(GTK_CONTAINER(dlg_vbox), btn_box);
gtk_container_set_border_width(GTK_CONTAINER(btn_box), 20);
gtk_widget_show(btn_box);
gtk_container_set_border_width(GTK_CONTAINER(KeywordsContainer), 8);
/* Make the button */
ok = gtk_button_new_with_label("Leave Keywords Mode");
gtk_box_pack_start(GTK_BOX(btn_box), ok, TRUE, TRUE, 0);
gtk_signal_connect(GTK_OBJECT(ok), "clicked",
(GtkSignalFunc)LeaveKeywordsMode, 0);
gtk_widget_show(ok);
KeywordsDImgName = gtk_label_new("imgName");
gtk_box_pack_start(GTK_BOX(KeywordsContainer), KeywordsDImgName,
TRUE, TRUE, 4);
gtk_widget_show(KeywordsDImgName);
label = gtk_label_new("To add a new keyword, hit Enter in the last box:");
gtk_box_pack_start(GTK_BOX(KeywordsContainer), label, TRUE, TRUE, 4);
gtk_widget_show(label);
/* Add the caption field */
hbox = gtk_hbox_new(FALSE, 3);
gtk_box_pack_start(GTK_BOX(KeywordsContainer), hbox,
TRUE, TRUE, 4);
label = gtk_label_new("Caption:");
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
gtk_widget_show(label);
KeywordsCaption = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(hbox), KeywordsCaption, TRUE, TRUE, 4);
gtk_widget_show(KeywordsCaption);
gtk_widget_show(hbox);
/* Make sure all the entries are initialized */
for (i=0; i < NUM_NOTES; ++i)
KeywordsDEntry[i] = 0;
/* Add the first keywords field. Others will be added as needed */
AddNewKeywordField();
gtk_widget_show(KeywordsDialog);
}
/* Make sure the dialog is showing */
void ShowKeywordsDialog()
{
if (!gWin)
return;
if (!KeywordsDialog)
MakeNewKeywordsDialog();
else if (!IsVisible(KeywordsDialog))
gtk_widget_show(KeywordsDialog);
/* else it's already showing */
/* Calling this from UpdateKeywordsDialog somehow sends focus
* back to the image window.
KeepOnTop(KeywordsDialog);
*/
/*
gdk_window_raise(GTK_WIDGET(KeywordsDialog)->window);
* XXX Why does raise raise forever? Isn't there some way
* to raise once but still let the user control the stacking??
*/
/* Save any state we have from the previous image */
RememberKeywords();
/* update to show the state of the new image */
UpdateKeywordsDialog();
}
void HideKeywordsDialog()
{
RememberKeywords();
if (IsVisible(KeywordsDialog))
gtk_widget_hide(KeywordsDialog);
sLastImage = 0;
}