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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* File list widget for the Midnight Commander
*
* Copyright (C) 1999, 2000 The Free Software Foundation
* Copyright (C) 2000 Eazel, Inc.
*
* Author: Federico Mena <federico@nuclecu.unam.mx>
* Modified by Ettore Perazzoli <ettore@gnu.org>
*/
/* FIXME this is a kludge to re-use broken CList. Instead, I'd like to have a
native List widget that uses a simple API similiar to the GnomeIconContainer
one. */
#include <config.h>
#include "gtkflist.h"
#include <gtk/gtkdnd.h>
#include "nautilus-glib-extensions.h"
#include "nautilus-gtk-macros.h"
#include "nautilus-background.h"
struct _GtkFListDetails
{
/* Preferences */
gboolean single_click_mode;
/* The anchor row for range selections */
int anchor_row;
/* Mouse information saved on button press */
int dnd_press_button;
int dnd_press_x, dnd_press_y;
int button_down_row;
guint32 button_down_time;
/* Delayed selection information */
int dnd_select_pending;
guint dnd_select_pending_state;
};
/* maximum amount of milliseconds the mouse button is allowed to stay down and still be considered a click */
#define MAX_CLICK_TIME 1500
enum {
CONTEXT_CLICK_ROW,
CONTEXT_CLICK_BACKGROUND,
ACTIVATE,
START_DRAG,
SELECTION_CHANGED,
LAST_SIGNAL
};
enum {
TARGET_COLOR
};
static GtkTargetEntry gtk_flist_dnd_target_table[] = {
{ "application/x-color", 0, TARGET_COLOR }
};
static void activate_row (GtkFList *flist, gint row);
static void gtk_flist_initialize_class (GtkFListClass *class);
static void gtk_flist_initialize (GtkFList *flist);
static gint gtk_flist_button_press (GtkWidget *widget, GdkEventButton *event);
static gint gtk_flist_button_release (GtkWidget *widget, GdkEventButton *event);
static gint gtk_flist_motion (GtkWidget *widget, GdkEventMotion *event);
static gint gtk_flist_key (GtkWidget *widget, GdkEventKey *event);
static void gtk_flist_drag_begin (GtkWidget *widget, GdkDragContext *context);
static void gtk_flist_drag_end (GtkWidget *widget, GdkDragContext *context);
static void gtk_flist_drag_data_get (GtkWidget *widget, GdkDragContext *context,
GtkSelectionData *data, guint info, guint time);
static void gtk_flist_drag_leave (GtkWidget *widget, GdkDragContext *context, guint time);
static gboolean gtk_flist_drag_motion (GtkWidget *widget, GdkDragContext *context,
gint x, gint y, guint time);
static gboolean gtk_flist_drag_drop (GtkWidget *widget, GdkDragContext *context,
gint x, gint y, guint time);
static void gtk_flist_drag_data_received (GtkWidget *widget, GdkDragContext *context,
gint x, gint y, GtkSelectionData *data,
guint info, guint time);
static void gtk_flist_clear (GtkCList *clist);
NAUTILUS_DEFINE_CLASS_BOILERPLATE (GtkFList, gtk_flist, GTK_TYPE_CLIST)
static guint flist_signals[LAST_SIGNAL];
/* Standard class initialization function */
static void
gtk_flist_initialize_class (GtkFListClass *class)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
GtkCListClass *clist_class;
object_class = (GtkObjectClass *) class;
widget_class = (GtkWidgetClass *) class;
clist_class = (GtkCListClass *) class;
flist_signals[CONTEXT_CLICK_ROW] =
gtk_signal_new ("context_click_row",
GTK_RUN_FIRST,
object_class->type,
GTK_SIGNAL_OFFSET (GtkFListClass, context_click_row),
gtk_marshal_NONE__INT,
GTK_TYPE_NONE, 1,
GTK_TYPE_INT);
flist_signals[CONTEXT_CLICK_BACKGROUND] =
gtk_signal_new ("context_click_background",
GTK_RUN_FIRST,
object_class->type,
GTK_SIGNAL_OFFSET (GtkFListClass, context_click_background),
gtk_marshal_NONE__NONE,
GTK_TYPE_NONE, 0);
flist_signals[ACTIVATE] =
gtk_signal_new ("activate",
GTK_RUN_FIRST,
object_class->type,
GTK_SIGNAL_OFFSET (GtkFListClass, activate),
gtk_marshal_NONE__POINTER,
GTK_TYPE_NONE, 1,
GTK_TYPE_POINTER);
flist_signals[START_DRAG] =
gtk_signal_new ("start_drag",
GTK_RUN_FIRST,
object_class->type,
GTK_SIGNAL_OFFSET (GtkFListClass, start_drag),
gtk_marshal_NONE__INT_POINTER,
GTK_TYPE_NONE, 2,
GTK_TYPE_INT,
GTK_TYPE_GDK_EVENT);
flist_signals[SELECTION_CHANGED] =
gtk_signal_new ("selection_changed",
GTK_RUN_FIRST,
object_class->type,
GTK_SIGNAL_OFFSET (GtkFListClass, start_drag),
gtk_marshal_NONE__NONE,
GTK_TYPE_NONE, 0);
gtk_object_class_add_signals (object_class, flist_signals, LAST_SIGNAL);
clist_class->clear = gtk_flist_clear;
widget_class->button_press_event = gtk_flist_button_press;
widget_class->button_release_event = gtk_flist_button_release;
widget_class->motion_notify_event = gtk_flist_motion;
widget_class->key_press_event = gtk_flist_key;
widget_class->key_release_event = gtk_flist_key;
widget_class->drag_begin = gtk_flist_drag_begin;
widget_class->drag_end = gtk_flist_drag_end;
widget_class->drag_data_get = gtk_flist_drag_data_get;
widget_class->drag_leave = gtk_flist_drag_leave;
widget_class->drag_motion = gtk_flist_drag_motion;
widget_class->drag_drop = gtk_flist_drag_drop;
widget_class->drag_data_received = gtk_flist_drag_data_received;
}
static void
select_or_unselect_row_cb (GtkCList *clist, gint row, gint column, GdkEvent *event)
{
/* This is the one bottleneck for all selection changes */
gtk_signal_emit (GTK_OBJECT (clist), flist_signals[SELECTION_CHANGED]);
}
/* Standard object initialization function */
static void
gtk_flist_initialize (GtkFList *flist)
{
flist->details = g_new0 (GtkFListDetails, 1);
flist->details->anchor_row = -1;
/* This should be read from preferences */
flist->details->single_click_mode = TRUE;
/* GtkCList does not specify pointer motion by default */
gtk_widget_add_events (GTK_WIDGET (flist), GDK_POINTER_MOTION_MASK);
/* Get ready to accept some dragged stuff. */
gtk_drag_dest_set (GTK_WIDGET (flist),
GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
gtk_flist_dnd_target_table,
NAUTILUS_N_ELEMENTS (gtk_flist_dnd_target_table),
GDK_ACTION_COPY);
/* Emit "selection changed" signal when parent class changes selection */
gtk_signal_connect (GTK_OBJECT (flist),
"select_row",
select_or_unselect_row_cb,
flist);
gtk_signal_connect (GTK_OBJECT (flist),
"unselect_row",
select_or_unselect_row_cb,
flist);
}
static void
activate_row (GtkFList *flist, gint row)
{
GtkCListRow *elem;
elem = g_list_nth (GTK_CLIST (flist)->row_list,
row)->data;
gtk_signal_emit (GTK_OBJECT (flist),
flist_signals[ACTIVATE],
elem->data);
}
static gboolean
row_selected (GtkFList *flist, gint row)
{
GtkCListRow *elem;
elem = g_list_nth (GTK_CLIST (flist)->row_list, row)->data;
return elem->state == GTK_STATE_SELECTED;
}
/* Selects the rows between the anchor to the specified row, inclusive. */
static void
select_range (GtkFList *flist, int row)
{
int min, max;
int i;
if (flist->details->anchor_row == -1)
flist->details->anchor_row = row;
if (row < flist->details->anchor_row) {
min = row;
max = flist->details->anchor_row;
} else {
min = flist->details->anchor_row;
max = row;
}
for (i = min; i <= max; i++)
gtk_clist_select_row (GTK_CLIST (flist), i, 0);
}
/* Handles row selection according to the specified modifier state */
static void
select_row (GtkFList *flist, int row, guint state)
{
int range, additive;
range = (state & GDK_SHIFT_MASK) != 0;
additive = (state & GDK_CONTROL_MASK) != 0;
if (!additive)
gtk_clist_unselect_all (GTK_CLIST (flist));
if (!range) {
if (additive) {
if (row_selected (flist, row))
gtk_clist_unselect_row
(GTK_CLIST (flist), row, 0);
else
gtk_clist_select_row
(GTK_CLIST (flist), row, 0);
} else {
gtk_clist_select_row (GTK_CLIST (flist), row, 0);
}
flist->details->anchor_row = row;
} else
select_range (flist, row);
}
/* Our handler for button_press events. We override all of GtkCList's broken
* behavior.
*/
static gint
gtk_flist_button_press (GtkWidget *widget, GdkEventButton *event)
{
GtkFList *flist;
GtkCList *clist;
int on_row;
gint row, col;
int retval;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (GTK_IS_FLIST (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
flist = GTK_FLIST (widget);
clist = GTK_CLIST (widget);
retval = FALSE;
if (event->window != clist->clist_window)
return NAUTILUS_CALL_PARENT_CLASS (GTK_WIDGET_CLASS, button_press_event, (widget, event));
on_row = gtk_clist_get_selection_info (clist, event->x, event->y, &row, &col);
flist->details->button_down_time = event->time;
switch (event->type) {
case GDK_BUTTON_PRESS:
if (event->button == 1 || event->button == 2) {
if (on_row) {
/* Save the clicked row for DnD and single-click activate */
flist->details->button_down_row = row;
/* Save the mouse info for DnD */
flist->details->dnd_press_button = event->button;
flist->details->dnd_press_x = event->x;
flist->details->dnd_press_y = event->y;
/* Handle selection */
if ((row_selected (flist, row)
&& !(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)))
|| ((event->state & GDK_CONTROL_MASK)
&& !(event->state & GDK_SHIFT_MASK))) {
flist->details->dnd_select_pending = TRUE;
flist->details->dnd_select_pending_state = event->state;
}
select_row (flist, row, event->state);
} else {
gtk_clist_unselect_all (clist);
}
retval = TRUE;
} else if (event->button == 3) {
if (on_row) {
/* Context menu applies to single item (at least
* for now). Select item first to make this obvious.
*/
gtk_clist_unselect_all (clist);
gtk_clist_select_row (clist, row, 0);
gtk_signal_emit (GTK_OBJECT (flist),
flist_signals[CONTEXT_CLICK_ROW],
row);
} else
gtk_signal_emit (GTK_OBJECT (flist),
flist_signals[CONTEXT_CLICK_BACKGROUND]);
retval = TRUE;
}
break;
case GDK_2BUTTON_PRESS:
if (event->button == 1) {
flist->details->dnd_select_pending = FALSE;
flist->details->dnd_select_pending_state = 0;
if (on_row) {
/* Activate on double-click even if single_click_mode
* is set, so second click doesn't get passed to child
* directory.
*/
activate_row (flist, row);
}
retval = TRUE;
break;
}
default:
break;
}
return retval;
}
/* Our handler for button_release events. We override all of GtkCList's broken
* behavior.
*/
static gint
gtk_flist_button_release (GtkWidget *widget, GdkEventButton *event)
{
GtkFList *flist;
GtkCList *clist;
int on_row;
gint row, col;
int retval;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (GTK_IS_FLIST (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
flist = GTK_FLIST (widget);
clist = GTK_CLIST (widget);
retval = FALSE;
if (event->window != clist->clist_window)
return NAUTILUS_CALL_PARENT_CLASS (GTK_WIDGET_CLASS, button_release_event, (widget, event));
on_row = gtk_clist_get_selection_info (clist, event->x, event->y, &row, &col);
if (!(event->button == 1 || event->button == 2))
return FALSE;
flist->details->dnd_press_button = 0;
flist->details->dnd_press_x = 0;
flist->details->dnd_press_y = 0;
if (on_row) {
/* Clean up after abortive drag-and-drop attempt (since user can't
* reorder list view items, releasing mouse in list view cancels
* drag-and-drop possibility).
*/
if (flist->details->dnd_select_pending) {
flist->details->dnd_select_pending = FALSE;
flist->details->dnd_select_pending_state = 0;
}
/* Activate on single click if not extending selection, mouse hasn't moved to
* a different row, and not too much time has passed.
*/
if (flist->details->single_click_mode &&
!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)))
{
gint elapsed_time = event->time - flist->details->button_down_time;
if (elapsed_time < MAX_CLICK_TIME && flist->details->button_down_row == row)
{
activate_row (flist, row);
}
}
retval = TRUE;
}
return retval;
}
/* Our handler for motion_notify events. We override all of GtkCList's broken
* behavior.
*/
static gint
gtk_flist_motion (GtkWidget *widget, GdkEventMotion *event)
{
GtkFList *flist;
GtkCList *clist;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (GTK_IS_FLIST (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
flist = GTK_FLIST (widget);
clist = GTK_CLIST (widget);
if (event->window != clist->clist_window)
return NAUTILUS_CALL_PARENT_CLASS (GTK_WIDGET_CLASS, motion_notify_event, (widget, event));
if (!((flist->details->dnd_press_button == 1 && (event->state & GDK_BUTTON1_MASK))
|| (flist->details->dnd_press_button == 2 && (event->state & GDK_BUTTON2_MASK))))
return FALSE;
/* This is the same threshold value that is used in gtkdnd.c */
if (MAX (abs (flist->details->dnd_press_x - event->x),
abs (flist->details->dnd_press_y - event->y)) <= 3)
return FALSE;
/* Handle any pending selections */
if (flist->details->dnd_select_pending) {
select_row (flist,
flist->details->button_down_row,
flist->details->dnd_select_pending_state);
flist->details->dnd_select_pending = FALSE;
flist->details->dnd_select_pending_state = 0;
}
gtk_signal_emit (GTK_OBJECT (flist),
flist_signals[START_DRAG],
flist->details->dnd_press_button,
event);
return TRUE;
}
/* Our handler for key_press and key_release events. We do nothing, and we do
* this to avoid GtkCList's broken behavior.
*/
static gint
gtk_flist_key (GtkWidget *widget, GdkEventKey *event)
{
return FALSE;
}
/* We override the drag_begin signal to do nothing */
static void
gtk_flist_drag_begin (GtkWidget *widget, GdkDragContext *context)
{
/* nothing */
}
/* We override the drag_end signal to do nothing */
static void
gtk_flist_drag_end (GtkWidget *widget, GdkDragContext *context)
{
/* nothing */
}
/* We override the drag_data_get signal to do nothing */
static void
gtk_flist_drag_data_get (GtkWidget *widget, GdkDragContext *context,
GtkSelectionData *data, guint info, guint time)
{
/* nothing */
}
/* We override the drag_leave signal to do nothing */
static void
gtk_flist_drag_leave (GtkWidget *widget, GdkDragContext *context, guint time)
{
/* nothing */
}
/* We override the drag_motion signal to do nothing */
static gboolean
gtk_flist_drag_motion (GtkWidget *widget, GdkDragContext *context,
gint x, gint y, guint time)
{
return FALSE;
}
/* We override the drag_drop signal to do nothing */
static gboolean
gtk_flist_drag_drop (GtkWidget *widget, GdkDragContext *context,
gint x, gint y, guint time)
{
return FALSE;
}
/* We override the drag_data_received signal to accept colors. */
static void
gtk_flist_drag_data_received (GtkWidget *widget, GdkDragContext *context,
gint x, gint y, GtkSelectionData *data,
guint info, guint time)
{
switch (info) {
case TARGET_COLOR:
nautilus_background_receive_dropped_color
(nautilus_get_widget_background (widget),
widget, x, y, data);
break;
default:
g_assert_not_reached ();
}
}
/* Our handler for the clear signal of the clist. We have to reset the anchor
* to null.
*/
static void
gtk_flist_clear (GtkCList *clist)
{
GtkFList *flist;
g_return_if_fail (clist != NULL);
g_return_if_fail (GTK_IS_FLIST (clist));
flist = GTK_FLIST (clist);
flist->details->anchor_row = -1;
NAUTILUS_CALL_PARENT_CLASS (GTK_CLIST_CLASS, clear, (clist));
}
/**
* gtk_flist_new_with_titles:
* @columns: The number of columns in the list
* @titles: The titles for the columns
*
* Return value: The newly-created file list.
**/
GtkWidget *
gtk_flist_new_with_titles (int columns, char **titles)
{
GtkFList *flist;
flist = gtk_type_new (gtk_flist_get_type ());
gtk_clist_construct (GTK_CLIST (flist), columns, titles);
gtk_clist_set_selection_mode (GTK_CLIST (flist),
GTK_SELECTION_MULTIPLE);
return GTK_WIDGET (flist);
}
GList *
gtk_flist_get_selection (GtkFList *flist)
{
GList *retval;
GList *p;
g_return_val_if_fail (flist != NULL, NULL);
g_return_val_if_fail (GTK_IS_FLIST (flist), NULL);
retval = NULL;
for (p = GTK_CLIST (flist)->row_list; p != NULL; p = p->next) {
GtkCListRow *row;
row = p->data;
if (row->state == GTK_STATE_SELECTED)
retval = g_list_prepend (retval, row->data);
}
return retval;
}
|