Debian Codegraph @

texk/xdvik/gui/pagesel.c

raw
1 /*
2 * Page selector for xdvi
3 *
4 * Copyright (c) 2001-2004 xdvik development team
5 *
6 * This code is derived from the page selector in xdvik-j, and
7 * parts of it are Copyright (c) 1993, 1995
8 * MATSUURA Syun [email protected]
9 * HIRAHARA Atsushi [email protected]
10 * ONO Kouichi [email protected]
11 * All rights reserved.
12 *
13 *
14 * (SU: I was unsure how to interpret the `All rights reserved' in the
15 * previous line, so emailed Ono Kouichi about this. Here's a
16 * verbatim quote of the relevant part of his answer (which was CC'ed
17 * to Hirahara Atsushi - all three of them had left Waseda university
18 * around '95):
19 *
20 * You can modify, embed, copy and distribute a part of or the
21 * entire of our source code when you specify our copyright in your
22 * xdvik version.
23 *
24 * IANAL, but I think this is compatible with the X consortium
25 * license, which follows.)
26 *
27 * Permission is hereby granted, free of charge, to any person obtaining a copy
28 * of this software and associated documentation files (the "Software"), to
29 * deal in the Software without restriction, including without limitation the
30 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
31 * sell copies of the Software, and to permit persons to whom the Software is
32 * furnished to do so, subject to the following conditions:
33 *
34 * The above copyright notice and this permission notice shall be included in
35 * all copies or substantial portions of the Software.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
40 * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
41 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
42 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
43 * OTHER DEALINGS IN THE SOFTWARE.
44 *
45 */
46
47 /*
48 BUGS:
49
50 - with Xaw, the highlighting for the selected page (XawListHighlight)
51 vanishes when mouse is dragged outside the widget and then released
52 (but the respective page is selected, which is IMHO the desired behaviour).
53
54 - with Xaw, scrolling the list with PgUp/PgDown until the current page
55 gets `out of focus' should un-highlight the current page number
56
57 - The ASCII-based marks are *ugly*. Pixmaps (for the marked state)
58 would be better. The viewer gv has one (but it's Xaw only). Some
59 file directory widgets like
60 e.g. http://ftp.xfree86.org/pub/X.Org/contrib/widgets/ListTree-3.0b3.tar.gz
61 also have facilities for that, but most suffer from other
62 inadequacies (e.g. no such ting as browseSelection) and all kinds
63 of bitrot ... Another alternative would be using XmContainer
64 (see e.g. the `filemanager' example in demos/programs/filemanagers
65 in the openmotif distribution), but that's available for Motif >= 2.1 only.
66
67 */
68
69 #include "xdvi-config.h"
70 #include "xdvi.h"
71
72 #include <stdio.h>
73 #include <stdarg.h>
74 #include <stdlib.h>
75 #include <X11/Intrinsic.h>
76 #include <X11/StringDefs.h>
77 #include <X11/Shell.h>
78
79 #include "xm_toolbar.h"
80 #include "xm_menu.h"
81 #include "xaw_menu.h"
82
83 #include "x_util.h"
84
85 #ifdef MOTIF
86 # include <Xm/Xm.h>
87 # include <Xm/List.h>
88 # include <Xm/ScrollBar.h> /* for XmScrollBarGetValues */
89 #else /* MOTIF */
90 # include <X11/Xaw/Dialog.h>
91 # include <X11/Xaw/Cardinals.h>
92 # include <X11/Xaw/Command.h>
93 # include <X11/Xaw/List.h>
94 # include <X11/Xaw/Viewport.h>
95 #endif /* MOTIF */
96
97 #include "message-window.h"
98 #include "pagesel.h"
99 #include "util.h"
100 #include "string-utils.h"
101 #include "dvi-init.h"
102 #include "statusline.h"
103 #include "events.h"
104 #include "print-dialog.h"
105 #include "search-internal.h"
106 #include "pagehist.h"
107
108 #define PAGENUMLEN 128
109 #define SCROLL_LIST_SCROLLBAR 0
110
111 #ifndef MAX_PAGE
112 # define MAX_PAGE (1024)
113 #endif /* MAX_PAGE */
114
115 #define LONGESTPAGENUM 55
116
117
118 /* for saving the GC of the pagelist widget
119 (when un-highlighting items in highlight_page_callback, and
120 drawing the `current' marker)
121 */
122 static struct page_gc {
123 GC fore;
124 GC back;
125 } m_page_gc;
126
127 #define MOTIF_IDX_OFFSET 1 /* motif index starts at 1, not 0 */
128 #if !defined(LESSTIF_VERSION)
129 static Boolean my_list_pos_to_bounds(Widget widget, int idx, Position *x, Position *y, Dimension *w, Dimension *h);
130 static void refresh_highlight_marker(Widget widget, GC gc, Position x, Position y, Dimension w, Dimension h);
131 #endif /* !defined(LESSTIF_VERSION) */
132
133
134
135 #ifdef MOTIF
136
137 /* make button2 mark instead of drag&drop */
138 static void xm_list_set_mark(Widget widget, XEvent *event, String *params, Cardinal *num_params);
139 static void xm_list_drag_mark(Widget widget, XEvent *event, String *params, Cardinal *num_params);
140 static XtActionsRec CustomListActions[] = {
141 { "ListSetMark", xm_list_set_mark },
142 { "ListDragMark", xm_list_drag_mark },
143 };
144 static char *motif_custom_translations =
145 "#override \n"
146 "s <Btn2Down>: ListDragMark(ListButtonMotion)\n"
147 "<Btn2Motion>: ListSetMark(ListButtonMotion)\n"
148 "<Btn2Up>: ListSetMark(ListButtonMotion)\n"
149 "<Btn2Down>: ListSetMark(ListButtonMotion)\n"
150 "<Btn4Down>,<Btn4Up>: scroll-list-up()\n"
151 "<Btn5Down>,<Btn5Up>: scroll-list-down()\n"
152 /* /\* "s ~m ~a <Btn2Down>: ListMyProcessBtn2(ListBeginExtend)\n" *\/ */
153 /* /\* "s ~m ~a <Btn2Up>: ListMyProcessBtn2(ListEndExtend)\n" *\/ */
154 /* /\* "~c ~s ~m ~a <Btn2Down>: ListMyProcessBtn2(ListBeginSelect)\n" *\/ */
155 /* /\* "~c ~s ~m ~a <Btn2Up>: ListMyProcessBtn2(ListEndSelect)\n" *\/ */
156 /* /\* "c ~s ~m ~a <Btn2Down>: ListMyProcessBtn2(ListBeginToggle)\n" *\/ */
157 /* /\* "c ~s ~m ~a <Btn2Up>: ListMyProcessBtn2(ListEndToggle)\n" *\/ */
158 /* /\* "c ~s ~m a <Btn2Down>: ListProcessDrag()\n" *\/ */
159 /* /\* "~c s ~m a <Btn2Down>: ListProcessDrag()\n" *\/ */
160 ;
161
162 #define LIST_WIDGET page_list
163 /* motif pagenumber is a string */
164 static const char* const pageno_format = "%c %s ";
165
166 #else /* MOTIF */
167
168 static int view_y;
169 extern Widget panel_widget;
170 static Widget list_widget = NULL;
171 static Widget viewport = NULL;
172 #define LIST_WIDGET list_widget
173 /* Xaw pagenumber is an integer, and we need to left-pad it */
174 static const char* const pageno_format = "%c %*d ";
175
176 static int xaw_maybe_scroll_pagelist(int new_page, Boolean force_recenter, int old);
177
178 #define REDRAW_CURRENT_MARKER_HACK 1
179
180
181 /*
182 The following hack tries to address the following 2 bugs with the
183 self-made page highlighting marker:
184
185 - the marker overlaps with the ordinary XawListHighlight marker;
186 when un-highlighting a page, 1 pixel (vertically) at the edge of
187 the ordinary marker is overdrawn.
188
189 - When the XawListHighlight crosses the self-drawn rectangle,
190 the vertical bars remain visible, but the horizontal bars
191 are erased.
192
193 The hack just redraws the appropriate items whenever one of
194 the above can happen, i.e. when the two markers are 2 or less
195 pages apart from each other.
196 */
197 #if REDRAW_CURRENT_MARKER_HACK
198 /* Store index of currently marked (with our own marker) list
199 item, or -1 if none is marked. */
200 static int g_current_highlighted = -1;
201
202 /* redraw the default Xaw list highlight (XawListHighlight()) */
203 static void
204 xaw_maybe_redraw_highlight(int idx)
205 {
206 XawListReturnStruct *ret;
207 int high;
208
209 if (LIST_WIDGET == NULL)
210 return;
211
212 ret = XawListShowCurrent(LIST_WIDGET);
213 high = ret->list_index;
214 if (high != XAW_LIST_NONE && abs(idx - (high + MOTIF_IDX_OFFSET)) <= 2) {
215 /* re-highlight it */
216 XawListHighlight(LIST_WIDGET, high);
217 }
218 g_current_highlighted = -1;
219 }
220
221 /* redraw our own rectangle highlight marker: */
222 static void
223 xaw_maybe_redraw_current_marker(int idx)
224 {
225 Position x, y;
226 Dimension w, h;
227
228 /* fprintf(stderr, "idx: %d, high: %d; diff: %d\n", idx + MOTIF_IDX_OFFSET, g_current_highlighted, */
229 /* abs(idx + MOTIF_IDX_OFFSET - g_current_highlighted)); */
230 if (abs((idx + MOTIF_IDX_OFFSET) - g_current_highlighted) <= 2
231 && my_list_pos_to_bounds(LIST_WIDGET, g_current_highlighted, &x, &y, &w, &h)) {
232 refresh_highlight_marker(LIST_WIDGET, m_page_gc.fore, x, y, w, h);
233 }
234 }
235 #endif /* REDRAW_CURRENT_MARKER_HACK */
236
237 #endif /* MOTIF */
238
239 /*
240 * Table of page offsets in DVI file, indexed by page number - 1,
241 * marked pages, and page sizes.
242 * Initialized in prepare_pages().
243 */
244 struct page_index {
245 long offset;
246 int number;
247 Boolean marked;
248 unsigned int pw, ph; /* page size */
249 unsigned int ww, wh; /* window size */
250 };
251
252 struct page_index_info {
253 struct page_index *index; /* above struct */
254 size_t index_size; /* size of currently allocated index */
255 char **page_labels; /* label strings */
256 };
257
258 static struct page_index_info page_info;
259
260 /* access functions used by dvi-draw.c and dvi-init.c */
261 long
262 pageinfo_get_offset(int page)
263 {
264 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
265 /* fprintf(stderr, "offset for page %d is %ld\n", page, page_info.index[page].offset); */
266 return page_info.index[page].offset;
267 }
268
269 /* access functions used by dvi-draw.c and dvi-init.c */
270 unsigned int
271 pageinfo_get_page_width(int page)
272 {
273 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
274 return page_info.index[page].pw;
275 }
276
277 unsigned int
278 pageinfo_get_page_height(int page)
279 {
280 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
281 return page_info.index[page].ph;
282 }
283
284 unsigned int
285 pageinfo_get_window_width(int page)
286 {
287 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
288 return page_info.index[page].ww;
289 }
290
291 unsigned int
292 pageinfo_get_window_height(int page)
293 {
294 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
295 return page_info.index[page].wh;
296 }
297
298 void
299 pageinfo_set_page_width(int page, unsigned int width)
300 {
301 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
302 page_info.index[page].pw = width;
303 }
304
305 void
306 pageinfo_set_page_height(int page, unsigned int height)
307 {
308 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
309 page_info.index[page].ph = height;
310 }
311
312 void
313 pageinfo_set_window_width(int page, unsigned int width)
314 {
315 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
316 page_info.index[page].ww = width;
317 }
318
319 void
320 pageinfo_set_window_height(int page, unsigned int height)
321 {
322 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
323 page_info.index[page].wh = height;
324 }
325
326 int
327 pageinfo_get_number(int page)
328 {
329 ASSERT(page >= 0 && page < (int)page_info.index_size, "Page number out of range");
330 return page_info.index[page].number;
331 }
332
333 /* search for page with TeX number `number', and return its index, or -1 if it's not found. */
334 int
335 pageinfo_get_index_of_number(int number)
336 {
337 size_t i;
338 for (i = 0; i < page_info.index_size - 1; i++) {
339 if (number == page_info.index[i].number)
340 return i;
341 }
342 return -1;
343 }
344
345 void
346 pageinfo_set_offset(int index, long offset)
347 {
348 ASSERT(index >= 0 && index < (int)page_info.index_size, "");
349 page_info.index[index].offset = offset;
350 }
351
352 void
353 pageinfo_set_number(int index, int number)
354 {
355 ASSERT(index >= 0 && index < (int)page_info.index_size, "");
356 page_info.index[index].number = number;
357 }
358
359 void
360 pageinfo_allocate(int total_pages)
361 {
362 int i;
363 page_info.index = xmalloc(total_pages * sizeof *(page_info.index));
364 for (i = 0; i < total_pages; i++) {
365 page_info.index[i].marked = False;
366 }
367 /* following initializations are handled by the respective Motif/Xaw functions */
368 page_info.page_labels = NULL;
369 page_info.index_size = total_pages;
370 }
371
372 /*
373 Deallocate page_info. NOTE: We mustn't free the page_labels here,
374 since the page list might survive quite some time (e.g. while fonts
375 for the new file are being generated) and needs the labels.
376 */
377 void
378 pageinfo_deallocate(void)
379 {
380 free(page_info.index);
381 page_info.index_size = 0;
382 page_info.index = NULL;
383 }
384
385 #ifdef MOTIF
386 void
387 toggle_pagelist(void)
388 {
389 Dimension curr_w, curr_x;
390 XtVaGetValues(globals.widgets.main_window,
391 XmNwidth, &curr_w,
392 XmNx, &curr_x,
393 NULL);
394
395 if ((resource.expert_mode & XPRT_SHOW_PAGELIST) != 0) {
396 XtManageChild(XtParent(page_list));
397 XtManageChild(page_list);
398
399 curr_x += resource.pagelist_width;
400 curr_w -= resource.pagelist_width;
401 XtVaSetValues(globals.widgets.main_window,
402 XtNwidth, curr_w,
403 XtNx, curr_x,
404 XmNleftAttachment, XmATTACH_WIDGET,
405 XmNleftWidget, XtParent(page_list),
406 NULL);
407 }
408 else {
409 XtUnmanageChild(XtParent(page_list));
410 XtUnmanageChild(page_list);
411
412 curr_x -= resource.pagelist_width;
413 curr_w += resource.pagelist_width;
414 XtVaSetValues(globals.widgets.main_window,
415 XmNwidth, curr_w,
416 XmNx, curr_x,
417 XmNleftAttachment, XmATTACH_FORM,
418 NULL);
419 }
420
421 set_menu(&resource.expert_mode, Act_set_expert_mode, check_resource_expert);
422 }
423 #endif
424
425
426 Boolean
427 pageinfo_have_marked_pages(void)
428 {
429 int i;
430 for (i = 0; i < total_pages; i++) {
431 if (page_info.index[i].marked) {
432 return True;
433 }
434 }
435 return False;
436 }
437
438
439 /* return True if page i is marked, False else */
440 Boolean
441 pageinfo_is_marked(int i)
442 {
443 ASSERT(i <= (int)page_info.index_size, "");
444 return page_info.index[i].marked;
445 }
446
447
448 typedef enum { SCROLL_UP, SCROLL_DOWN, CLICK } saveCmdT;
449
450
451 static void internal_process_button2(Widget widget, XEvent *event);
452 static int get_item_index(Widget w, int mouse_y);
453
454 static int
455 get_page_size(void)
456 {
457 int offset = 0;
458 int min_page = 0;
459 int max_page = 0;
460 int min_pageno_len = 0;
461 int max_pageno_len = 0;
462 int i;
463
464 if (globals.dvi_file.bak_fp == NULL)
465 return 0;
466
467 for (i = 0; i < total_pages; i++) {
468 max_page = MAX(page_info.index[i].number, max_page);
469 min_page = MIN(page_info.index[i].number, min_page);
470 }
471
472 if (min_page >= 0) {
473 offset = 0; /* plus symbol is hidden */
474 } else {
475 offset = 1; /* offset for minus symbol */
476 min_page = -min_page;
477 }
478 for (min_pageno_len = offset; min_page > 0;
479 min_page /= 10, min_pageno_len++);
480
481 if (max_page >= 0) {
482 offset = 0; /* plus symbol is hidden */
483 } else {
484 offset = 1; /* offset for minus symbol */
485 max_page = -max_page;
486 }
487 for (max_pageno_len = offset; max_page > 0;
488 max_page /= 10, max_pageno_len++);
489
490 return MAX(min_pageno_len, max_pageno_len);
491 /* Plus 1 for minus symbol */
492 }
493
494 #if !defined(LESSTIF_VERSION)
495 /* draw or erase highlight marker at position x, y with widht w an height h,
496 using suitable offsets (for sake of consistency of the latter, and because
497 of the differences Xaw/Motif, this is a separate function).
498 */
499 static void
500 refresh_highlight_marker(Widget widget, GC gc,
501 Position x, Position y, Dimension w, Dimension h)
502 {
503 XDrawRectangle(XtDisplay(widget), XtWindow(widget), gc,
504 #ifdef MOTIF
505 x + 1, y + 1, w - 3, h - 3
506 #else
507 x + 2, y, w - 1, h - 1
508 #endif
509 );
510 }
511 #endif /* !defined(LESSTIF_VERSION) */
512
513 #ifndef MOTIF
514
515 /* update (redisplay) the list, saving the currently highlighted item.
516 * This is invoked whenever marking the page, and the re-construction of the
517 * entire list causes considerable flicker; but I guess that's unavoidable
518 * with the current simplistic labelling scheme (with changing the list items
519 * themselves). Gv does this considerably better (using custom widgets).
520 */
521 static void
522 xaw_update_list(void)
523 {
524 static int pagelist_width = -1;
525 static int total_pages_bak = -1;
526
527 XawListReturnStruct *ret;
528 int idx, button_width;
529
530 if (pagelist_width == -1 || total_pages != total_pages_bak) {
531 pagelist_width = xaw_get_pagelist_size();
532 total_pages_bak = total_pages;
533 }
534
535 /* save selected item */
536 ret = XawListShowCurrent(LIST_WIDGET);
537 idx = ret->list_index;
538 button_width = get_panel_width() - 2 * (resource.btn_side_spacing + resource.btn_border_width);
539 /* delete and re-create list */
540 ASSERT(total_pages <= (int)page_info.index_size, "");
541
542 /* The purpose of the (XAWLISTCHANGELISTTYPE) cast for page_labels
543 is to avoid a gcc incompatible-pointer-types error
544 on some systems, due to the lack and/or presence of const in the
545 XawListChange function parameter. Unfortunately different systems
546 evidently have different, and incompatible, types for that
547 parameter. Thus a cast to any single type will fail on some
548 systems. A (too) simplistic configure test to determine the
549 correct type is done in xdvi-check-xaw.m4. See more comments there.
550 xdvi bug report:
551 https://sourceforge.net/p/xdvi/bugs/406/
552 discussion of alternatives:
553 https://tug.org/pipermail/tex-k/2024-February/004008.html
554 */
555 #ifndef XAWLISTCHANGELISTTYPE
556 #define XAWLISTCHANGELISTTYPE char **
557 #endif
558 XawListChange(LIST_WIDGET,
559 (XAWLISTCHANGELISTTYPE) page_info.page_labels,
560 0,
561 MAX(button_width, pagelist_width), False);
562 /* restore selected item */
563 if (idx != XAW_LIST_NONE) {
564 XawListHighlight(LIST_WIDGET, idx);
565 }
566 }
567
568 /*
569 return height of a row in the list widget, and the initial offset of XtNinternalHeight
570 in parameters row_height and internal_h
571 */
572 static void
573 xaw_get_row_height(Widget w, Dimension *row_height, Dimension *internal_h)
574 {
575 Dimension row_space;
576 XFontStruct *font;
577 Arg arglist[5];
578 int i = 0;
579
580 if (w == NULL || !XtIsRealized(w))
581 return;
582
583 XtSetArg(arglist[i], XtNfont, &font); ++i;
584 XtSetArg(arglist[i], XtNrowSpacing, &row_space); ++i;
585 XtSetArg(arglist[i], XtNinternalHeight, internal_h); i++;
586 XtGetValues(w, arglist, i);
587 *row_height = font->max_bounds.ascent + font->max_bounds.descent + row_space;
588 }
589
590 /*
591 * Get pagelist width.
592 */
593 int
594 xaw_get_pagelist_size(void)
595 {
596 Widget w;
597 XFontStruct *font;
598
599 w = XtVaCreateWidget("list", listWidgetClass, globals.widgets.top_level, NULL);
600 XtVaGetValues(w, XtNfont, &font, NULL);
601 XtDestroyWidget(w);
602
603 /* have space for max. pageno + space + current-marker,
604 plus a few pixels for right margin */
605 return (get_page_size() + 2) * get_avg_font_width(font) + 6;
606 }
607
608 /* auto-scroll pagelist when mouse-1 is down and moved above top or below bottom of
609 page list.
610 */
611 static void
612 xaw_drag_page_callback(Widget widget, XtPointer data, XEvent *event, Boolean *cont)
613 {
614 int y, idx, actual_idx = 0;
615
616 UNUSED(data);
617 UNUSED(cont);
618
619 if (event->xany.type == ButtonPress || ((event->xbutton.state & Button1Mask) == 0))
620 return;
621 if (event->xany.type == MotionNotify)
622 y = (int)event->xmotion.y;
623 else
624 y = (int)event->xbutton.y;
625
626 idx = get_item_index(widget, y);
627
628 if (idx <= 0) {
629 idx = 1;
630 }
631 else if (idx > total_pages) {
632 idx = total_pages;
633 }
634
635 actual_idx = xaw_maybe_scroll_pagelist(idx, False, actual_idx);
636 XawListHighlight(LIST_WIDGET, idx - MOTIF_IDX_OFFSET);
637
638 /* if (event->xany.type == ButtonRelease) { */
639 /* fprintf(stderr, "1\n"); */
640 /* page_history_insert(idx - MOTIF_IDX_OFFSET); */
641 /* goto_page(idx - MOTIF_IDX_OFFSET, resource.keep_flag ? NULL : home); */
642 /* search_signal_page_changed(); */
643 /* } */
644 }
645
646 static void
647 xaw_SendReportProc(Widget w, XtPointer closure, XtPointer call_data)
648 {
649 XawPannerReport *rep = (XawPannerReport *) call_data;
650
651 UNUSED(w);
652 UNUSED(closure);
653
654 view_y = rep->slider_y;
655 }
656
657 #endif /* not MOTIF */
658
659
660
661 /* returns the index of the current item in `Motif'-style, i.e. first item has index 1, not 0 */
662 static int
663 get_item_index(Widget w, int mouse_y)
664 {
665 #ifdef MOTIF
666 return XmListYToPos(w, mouse_y);
667 #else
668 Dimension row_height, internal_height;
669
670 xaw_get_row_height(w, &row_height, &internal_height);
671 return (mouse_y - internal_height) / row_height + MOTIF_IDX_OFFSET;
672 #endif
673 }
674
675 #if !defined(LESSTIF_VERSION)
676 /* idx is Motif-style index, i.e. 1 for 1st item, not 0 */
677 static Boolean
678 my_list_pos_to_bounds(Widget widget, int idx, Position *x, Position *y, Dimension *w, Dimension *h)
679 {
680 #ifdef MOTIF
681 Position x1, y1;
682 Dimension w1, h1;
683 if (XmListPosToBounds(widget, idx, &x1, &y1, &w1, &h1)) {
684 *x = x1;
685 *y = y1;
686 *w = w1;
687 *h = h1;
688 return True;
689 }
690 return False;
691 #else
692 Dimension row_height, internal_height;
693 /* FIXME: Remove this hard-coded offset! */
694 const int X_OFFSET = 9;
695 const int RULE_OFFSET = 2;
696
697 if (idx <= 0 || idx > total_pages) {
698 return False;
699 }
700
701 if (viewport != NULL && XtIsRealized(viewport))
702 XtVaGetValues(viewport, XtNx, x, NULL);
703 xaw_get_row_height(widget, &row_height, &internal_height);
704 XtVaGetValues(widget, XtNwidth, w, NULL);
705
706 *x -= X_OFFSET;
707 *y = row_height * idx + internal_height - row_height - 1;
708 *w -= RULE_OFFSET;
709 *h = row_height + RULE_OFFSET;
710
711 return True;
712 #endif
713 }
714
715
716 /* draw a hightlight rectangle around the page the mouse is currently over, to
717 make e.g. marking easier.
718 */
719 static void
720 highlight_page_callback(Widget widget, XtPointer data, XEvent *event, Boolean *cont)
721 {
722 int curr_idx = get_item_index(widget, event->xmotion.y);
723 Position x, y;
724 Dimension w, h;
725 static int idx_bak = -1;
726
727 UNUSED(data);
728 UNUSED(cont);
729
730 switch(event->xany.type) {
731 case ButtonPress:
732 case ButtonRelease:
733 case MotionNotify:
734 /* might need to un-highlight previous one */
735 if (idx_bak >= 0 && idx_bak != curr_idx
736 && my_list_pos_to_bounds(widget, idx_bak, &x, &y, &w, &h)) {
737 /* fprintf(stderr, "index: %d, %d, h: %d, w: %d\n", x, y, h, w); */
738 refresh_highlight_marker(widget, m_page_gc.back, x, y, w, h);
739 #if REDRAW_CURRENT_MARKER_HACK
740 xaw_maybe_redraw_highlight(curr_idx);
741 #endif
742 }
743 idx_bak = curr_idx;
744 /* redraw unless out of bounds (when pagelist is shorter than view area) */
745 if (my_list_pos_to_bounds(widget, curr_idx, &x, &y, &w, &h)) {
746 refresh_highlight_marker(widget, m_page_gc.fore, x, y, w, h);
747 #if REDRAW_CURRENT_MARKER_HACK
748 g_current_highlighted = curr_idx;
749 #endif
750 }
751 break;
752 case LeaveNotify:
753 /* this might look overly complicated, but is neccessary to cover all
754 cases of 1-pixel movement up/down before leaving the list, or no
755 movement at all before leaving it. */
756 if ((idx_bak >= 0 && idx_bak != curr_idx && my_list_pos_to_bounds(widget, idx_bak, &x, &y, &w, &h))
757 || my_list_pos_to_bounds(widget, curr_idx, &x, &y, &w, &h)) {
758 refresh_highlight_marker(widget, m_page_gc.back, x, y, w, h);
759 #if REDRAW_CURRENT_MARKER_HACK
760 xaw_maybe_redraw_highlight(curr_idx);
761 #endif
762 }
763 break;
764 default:
765 break;
766 }
767 }
768 #endif /* !defined(LESSTIF_VERSION) */
769
770 /*
771 * invoked on Button-1 Down.
772 */
773 static void
774 select_page_callback(Widget w, XtPointer closure, XtPointer call_data)
775 {
776 #ifdef MOTIF
777 XmListCallbackStruct *cbs = (XmListCallbackStruct *) call_data;
778 int new = cbs->item_position;
779
780 UNUSED(w);
781 UNUSED(closure);
782
783 maybe_scroll_pagelist(new - MOTIF_IDX_OFFSET, False);
784 page_history_insert(new - MOTIF_IDX_OFFSET);
785 goto_page(new - MOTIF_IDX_OFFSET, resource.keep_flag ? NULL : home, False);
786 #else
787 XawListReturnStruct *item = (XawListReturnStruct *) call_data;
788 int new = item->list_index;
789
790 UNUSED(w);
791 UNUSED(closure);
792
793 if (globals.debug & DBG_EVENT)
794 fprintf(stderr, "got: button-1 for `%d'\n", new);
795
796 #if 0
797 fprintf(stderr, "select page: %d\n", new);
798 #endif
799 maybe_scroll_pagelist(new, False);
800 page_history_insert(new);
801 goto_page(new, resource.keep_flag ? NULL : home, False);
802 statusline_erase("Page history:");
803 #endif
804 search_signal_page_changed();
805 }
806
807
808 static void
809 init_pagelabels(int start, int end)
810 {
811 int i;
812 char s[PAGENUMLEN];
813 #if 0
814 fprintf(stderr, "===== init_pagelabels from %d to %d\n", start, end);
815 #endif
816 ASSERT(end < (int)page_info.index_size, "");
817
818 page_info.page_labels = xrealloc(page_info.page_labels, sizeof *(page_info.page_labels) * (end + 2));
819 for (i = start; i < end; i++) {
820 if (page_info.index[i].marked)
821 sprintf(s, "* %*d ", get_page_size(),
822 resource.use_tex_pages ? page_info.index[i].number : i + 1);
823 else
824 sprintf(s, " %*d ", get_page_size(),
825 resource.use_tex_pages ? page_info.index[i].number : i + 1);
826
827 page_info.page_labels[i] = xstrdup(s);
828 }
829 page_info.page_labels[i] = NULL; /* terminate - important for creating the widget. */
830 }
831
832
833 #ifdef MOTIF
834
835
836 static int
837 xm_get_top_visible(int start)
838 {
839 int top = start;
840 while (top < total_pages && !XmListPosToBounds(LIST_WIDGET, top, NULL, NULL, NULL, NULL))
841 top++;
842 return top;
843 }
844
845 static int
846 xm_get_bottom_visible(int start)
847 {
848 int bot = start;
849 while (bot < total_pages && XmListPosToBounds(LIST_WIDGET, bot, NULL, NULL, NULL, NULL))
850 bot++;
851 bot--;
852 return bot;
853 }
854
855 /* Scroll pagelist down or up if needed, and update top_visible and
856 bot_visible. List is always scrolled so that 1 element is still
857 visible below or above pointer, to make it possible to flip through
858 document by repeatedly clicking on first/last.
859 */
860 static void
861 xm_maybe_scroll_pagelist(int current, saveCmdT curr_cmd, int *top_visible, int *bot_visible)
862 {
863 #if 0
864 fprintf(stderr, "topmost visible: %d, bottom: %d, current: %d, total: %d\n",
865 top_visible, bottom_visible, current, total_pages);
866 #endif
867
868 if (current < *top_visible && curr_cmd != SCROLL_DOWN) {
869 XmListSetPos(LIST_WIDGET,
870 current < 1
871 ? 1
872 : current);
873 (*top_visible)--;
874 (*bot_visible)--;
875 }
876 else if (current + MOTIF_IDX_OFFSET >= *bot_visible && curr_cmd != SCROLL_UP) {
877 XmListSetBottomPos(LIST_WIDGET,
878 current + MOTIF_IDX_OFFSET >= total_pages
879 ? current + MOTIF_IDX_OFFSET
880 : current + MOTIF_IDX_OFFSET + 1);
881 (*top_visible)++;
882 (*bot_visible)++;
883 }
884 }
885
886 static void
887 xm_set_page_labels(void)
888 {
889 int i;
890 char buf[PAGENUMLEN];
891
892 XmString *motif_page_labels = xmalloc((total_pages + 2) * sizeof *motif_page_labels);
893
894 for (i = 0; i < total_pages; ++i) {
895 sprintf(buf, pageno_format, ' ', page_info.page_labels[i]);
896 motif_page_labels[i] = XmStringCreateLocalized(buf);
897 }
898 XmListDeleteAllItems(LIST_WIDGET);
899 XmListAddItems(LIST_WIDGET, motif_page_labels, total_pages, 0);
900
901 XmListSelectPos(LIST_WIDGET, current_page + MOTIF_IDX_OFFSET, False);
902 for (i = 0; i < total_pages; ++i) {
903 XmStringFree(motif_page_labels[i]);
904 }
905 free(motif_page_labels);
906 }
907
908 static void
909 xm_toggle_label(Widget widget, int idx, Boolean update)
910 {
911 /* TODO: use `update' to update all labels at once when toggling multiple */
912 XmString str;
913 char *mark_font, buf[128];
914
915 if (widget == NULL)
916 return;
917
918 UNUSED(update);
919
920 /* ensure_labelinfo_size(idx); */
921 ASSERT(idx < (int)page_info.index_size, "");
922 if (!page_info.index[idx].marked) {
923 sprintf(buf, pageno_format, '*', page_info.page_labels[idx]);
924 mark_font = "MARKED";
925 page_info.index[idx].marked = True;
926 }
927 else {
928 sprintf(buf, pageno_format, ' ', page_info.page_labels[idx]);
929 mark_font = "UNMARKED";
930 page_info.index[idx].marked = False;
931 }
932 /* str = XmStringCreateLocalized(buf); */
933 str = XmStringCreateLtoR(buf, mark_font);
934 XmListReplaceItemsPos(widget, &str, 1, idx + MOTIF_IDX_OFFSET);
935 XmStringFree(str);
936 }
937
938
939 #else /* MOTIF */
940
941 static void
942 mark_page_callback(Widget w, XtPointer data, XEvent *event, Boolean *cont)
943 {
944 UNUSED(data);
945 UNUSED(cont);
946
947 /* moving button2 generates MotionNotify events for button0 */
948 if (event->type == MotionNotify || event->xbutton.button == Button2)
949 internal_process_button2(w, event);
950
951 if (event->type != ButtonPress)
952 notify_print_dialog_have_marked();
953 }
954
955 void
956 xaw_create_pagelist_widgets(Dimension height, Dimension width, Position y, Widget parent)
957 {
958 viewport = XtVaCreateWidget("viewport",
959 viewportWidgetClass, parent,
960 XtNallowVert, True,
961 /* this is not related to the scroll bar: */
962 /* XtNforceBars, True, */
963 XtNx, resource.btn_side_spacing,
964 XtNy, y,
965 XtNheight, height,
966 XtNwidth, width,
967 NULL);
968 LIST_WIDGET = XtVaCreateWidget("list",
969 listWidgetClass, viewport,
970 XtNlist, page_info.page_labels,
971 XtNdefaultColumns, 1,
972 XtNforceColumns, True,
973 XtNx, 10,
974 XtNy, 10,
975 XtNheight, height,
976 XtNwidth, width - 10,
977 XtNlongest, LONGESTPAGENUM,
978 XtNverticalList, True,
979 NULL);
980 XtManageChild(LIST_WIDGET);
981 XtManageChild(viewport);
982 XtAddCallback(LIST_WIDGET, XtNcallback, select_page_callback,
983 (XtPointer) NULL);
984 /* for scrolling the list */
985 XtAddCallback(viewport, XtNreportCallback, xaw_SendReportProc,
986 (XtPointer) NULL);
987 XtAddEventHandler(LIST_WIDGET,
988 ButtonPressMask | ButtonReleaseMask | Button2MotionMask,
989 False, mark_page_callback, (XtPointer)NULL);
990
991 if (resource.pagelist_highlight_current)
992 XtAddEventHandler(LIST_WIDGET,
993 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | LeaveWindowMask,
994 False, highlight_page_callback, (XtPointer)NULL);
995
996
997 {
998 Widget y_bar;
999 XtTranslations xlats = XtParseTranslationTable(
1000 "<Btn4Down>,<Btn4Up>: scroll-list-up()\n"
1001 "<Btn5Down>,<Btn5Up>: scroll-list-down()\n");
1002
1003 XtOverrideTranslations(LIST_WIDGET, xlats);
1004
1005 y_bar = XtNameToWidget(viewport, "vertical");
1006 if (y_bar != NULL)
1007 XtOverrideTranslations(y_bar, xlats);
1008 }
1009
1010 XtAddEventHandler(LIST_WIDGET,
1011 /* FIXME: We should add PointerMotionMask here, but handling PointerMotionMask
1012 currently doesn't work with the Xaw list widget: the auto-scrolling code doesn't
1013 realize when the mouse direction of the pointer movement changes, and continues
1014 to scroll into the same direction. This will be rather annoying for users, so
1015 we disabled PointerMotionMask for the time being.
1016 */
1017 ButtonReleaseMask /* | PointerMotionMask */ | Button1MotionMask,
1018 False, xaw_drag_page_callback, (XtPointer)NULL);
1019 }
1020
1021 static void
1022 xaw_toggle_label(Widget w, int idx, Boolean update)
1023 {
1024 if (w == NULL)
1025 return;
1026
1027 /* ensure_labelinfo_size(idx); */
1028 ASSERT(idx < (int)page_info.index_size, "");
1029 if (!page_info.index[idx].marked) {
1030 /* sprintf(toc[idx], "* %*d ", get_page_size(), page_index[idx].number); */
1031 sprintf(page_info.page_labels[idx], pageno_format, '*', get_page_size(),
1032 resource.use_tex_pages ? page_info.index[idx].number : idx + 1);
1033 page_info.index[idx].marked = True;
1034 }
1035 else {
1036 sprintf(page_info.page_labels[idx], pageno_format, ' ', get_page_size(),
1037 resource.use_tex_pages ? page_info.index[idx].number : idx + 1);
1038 /* sprintf(toc[idx], " %*d ", get_page_size(), page_index[idx].number); */
1039 page_info.index[idx].marked = False;
1040 }
1041
1042 if (update)
1043 xaw_update_list();
1044 }
1045
1046 static int
1047 xaw_maybe_scroll_pagelist(int new_page, Boolean force_recenter, int idx_bak)
1048 {
1049 Position x;
1050 Position y, new_y, bot_y;
1051 Dimension view_height, row_height, internal_height;
1052 /* Dimension clip_height; */
1053 /* static Widget list_clip = 0; */
1054
1055 if (LIST_WIDGET == NULL || (resource.expert_mode & XPRT_SHOW_BUTTONS) == 0)
1056 return idx_bak;
1057
1058 /* if (list_clip == 0) { */
1059 /* list_clip = XtNameToWidget(viewport, "clip"); */
1060 /* } */
1061 /* if (XtIsRealized(list_clip)) */
1062 /* XtVaGetValues(list_clip, XtNheight, &clip_height, XtNx, &cx, XtNy, &y1, NULL); */
1063 if (viewport != NULL && XtIsRealized(viewport))
1064 XtVaGetValues(viewport, XtNheight, &view_height, XtNx, &x, NULL);
1065 /* fprintf(stderr, "diff: %d, %d, %d, %d, %d\n", cx - x, clip_height, view_height, y1, (int)y2); */
1066 xaw_get_row_height(LIST_WIDGET, &row_height, &internal_height);
1067 y = row_height * new_page;
1068
1069 #if DEBUG
1070 fprintf(stderr, "###### xaw_maybe_scroll_pagelist: y %d, view_y %d, view_height %d, row_height %d, internal_height %d; actual %d\n",
1071 y, view_y, view_height, row_height, internal_height, idx_bak);
1072 #endif
1073 /*FIXME: when page list is destroyed, view_y will be 0 until user scrolls page list */
1074 bot_y = view_y + view_height;
1075 if (force_recenter || ((y >= bot_y - row_height))) {
1076 #if DEBUG
1077 fprintf(stderr, "scrolled below bottom; incrementing %d to %d\n", view_y, view_y + row_height);
1078 #endif
1079 y += row_height;
1080 XawViewportSetCoordinates(viewport, x, y - view_height > 0 ? y - view_height : 0);
1081 return new_page + 1;
1082 }
1083 else if (force_recenter || ((y <= view_y + row_height + internal_height))) {
1084 #if DEBUG
1085 fprintf(stderr, "scrolled over top; new_y: %d\n", y - row_height);
1086 #endif
1087 new_y = y - 2 * row_height;
1088 XawViewportSetCoordinates(viewport, x, new_y);
1089 return new_page - 1;
1090 }
1091 /* not scrolled */
1092 return -2;
1093 }
1094
1095 #endif /* MOTIF */
1096
1097
1098 /* idx is C-style index (0-based), not Motif one (1-based) */
1099 static void
1100 toggle_label(Widget widget, int idx, Boolean update)
1101 {
1102 if (idx >= total_pages)
1103 return;
1104 ASSERT(idx < total_pages, "");
1105 ASSERT(idx >= 0, "");
1106 #ifdef MOTIF
1107 xm_toggle_label(widget, idx, update);
1108 #else
1109 xaw_toggle_label(widget, idx, update);
1110 #endif
1111 }
1112
1113
1114 void
1115 list_toggle_marks(int arg)
1116 {
1117 int i;
1118
1119 if (arg < 0) { /* mark all */
1120 for (i = 0; i < total_pages; i++) {
1121 ASSERT(i < (int)page_info.index_size, "");
1122 /* ensure_labelinfo_size(i); */
1123 if (!page_info.index[i].marked) {
1124 toggle_label(LIST_WIDGET, i, False);
1125 }
1126 }
1127 }
1128 else if (arg == 0) { /* unmark all */
1129 for (i = 0; i < total_pages; i++) {
1130 ASSERT(i < (int)page_info.index_size, "");
1131 /* ensure_labelinfo_size(i); */
1132 if (page_info.index[i].marked) {
1133 toggle_label(LIST_WIDGET, i, False);
1134 }
1135 }
1136 }
1137 else { /* toggle odd/even */
1138 if (arg == 2) /* toggle even */
1139 arg = 0;
1140 for (i = 0; i < total_pages; i++) {
1141 if ((i + 1) % 2 == arg) {
1142 toggle_label(LIST_WIDGET, i, False);
1143 }
1144 }
1145 }
1146 /* TODO: update widget once for Motif as well! */
1147 #ifndef MOTIF
1148 xaw_update_list();
1149 #endif
1150 notify_print_dialog_have_marked();
1151 }
1152
1153 static Boolean PagelistInitialized = False;
1154
1155 #ifndef MOTIF
1156 void
1157 handle_pagelist_resize(void)
1158 {
1159 /* TODO: the following will mess up the geometry of the list
1160 (doesn't increase height, and incrementally decreases width):
1161 if (list_widget) {
1162 Dimension height;
1163 --- without the (un)manage, I get an X Error:
1164 XtMakeGeometryRequest - parent has no geometry manager
1165 ---
1166 XtUnmanageChild(viewport);
1167 XtUnmanageChild(LIST_WIDGET);
1168 XtVaGetValues(globals.widgets.clip_widget, XtNheight, &height, NULL);
1169 height -= resource.btn_top_spacing + resource.btn_border_width + global_y_pos;
1170 XtVaSetValues(viewport, XtNheight, height, NULL);
1171 XtManageChild(LIST_WIDGET);
1172 XtManageChild(viewport);
1173 }
1174 ... so we use brute force instead: */
1175 handle_destroy_pagelist(LIST_WIDGET, NULL, NULL);
1176 create_pagelist();
1177 }
1178
1179 void
1180 handle_destroy_pagelist(Widget w, XtPointer client_data, XtPointer call_data)
1181 {
1182 UNUSED(w);
1183 UNUSED(client_data);
1184 UNUSED(call_data);
1185
1186 if (viewport != NULL) {
1187 XtDestroyWidget(viewport);
1188 viewport = NULL;
1189 LIST_WIDGET = NULL;
1190 }
1191 PagelistInitialized = False;
1192 }
1193 #endif /* MOTIF */
1194
1195 void
1196 create_pagelist(void)
1197 {
1198 Pixel background, foreground;
1199 #ifdef MOTIF
1200
1201 /* items = xrealloc(items, sizeof *items * (total_pages + 2)); */
1202 init_pagelabels(0, total_pages);
1203 xm_set_page_labels();
1204 if (!PagelistInitialized) {
1205 XtAppContext app;
1206
1207 XtVaGetValues(LIST_WIDGET, XmNforeground, &foreground, XmNbackground, &background, NULL);
1208 m_page_gc.back = set_or_make_gc(NULL, GXcopy, background, foreground);
1209 m_page_gc.fore = set_or_make_gc(NULL, GXcopy, foreground, background);
1210 XtManageChild(LIST_WIDGET);
1211
1212 XtAddCallback(LIST_WIDGET, XmNbrowseSelectionCallback, select_page_callback, NULL);
1213 #if !defined(LESSTIF_VERSION)
1214 /*
1215 Don't use the highlighting hack with LessTif, since its XmListPosToBounds()
1216 is too broken to be usable (as of 0.93.36):
1217 - it returns generally too low values, apparently it doesn't take
1218 XmNlistSpacing into account;
1219 - it doesn't take scrollbar position into account.
1220 */
1221 if (resource.pagelist_highlight_current)
1222 XtAddEventHandler(LIST_WIDGET,
1223 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | LeaveWindowMask,
1224 False, highlight_page_callback, (XtPointer)NULL);
1225 #endif /* !defined(LESSTIF_VERSION) */
1226 app = XtWidgetToApplicationContext(globals.widgets.top_level);
1227 XtAppAddActions(app, CustomListActions, XtNumber(CustomListActions));
1228 XtOverrideTranslations(LIST_WIDGET, XtParseTranslationTable(motif_custom_translations));
1229 PagelistInitialized = True;
1230 }
1231 #else /* MOTIF */
1232 if ((resource.expert_mode & XPRT_SHOW_BUTTONS) == 0) {
1233 PagelistInitialized = False; /* might need to re-create widgets in this case */
1234 return;
1235 }
1236
1237 if (globals.debug & DBG_GUI)
1238 fprintf(stderr, "allocating list with %d pages\n", total_pages);
1239
1240 init_pagelabels(0, total_pages);
1241 if (!PagelistInitialized) {
1242 xaw_create_pagelist();
1243 XtVaGetValues(LIST_WIDGET, XtNforeground, &foreground, XtNbackground, &background, NULL);
1244 m_page_gc.back = set_or_make_gc(NULL, GXcopy, background, foreground);
1245 m_page_gc.fore = set_or_make_gc(NULL, GXcopy, foreground, background);
1246 PagelistInitialized = True;
1247 }
1248 #endif /* MOTIF */
1249 /* scroll to the current page if needed */
1250 maybe_scroll_pagelist(current_page, False);
1251 }
1252
1253 #ifndef MOTIF
1254 static void
1255 free_pagelabels(void)
1256 {
1257 int i;
1258 for (i = 0; page_info.page_labels != NULL && page_info.page_labels[i] != NULL; i++) {
1259 free(page_info.page_labels[i]);
1260 }
1261 free(page_info.page_labels);
1262 page_info.page_labels = NULL;
1263 }
1264 #endif /* not MOTIF */
1265
1266
1267 void
1268 refresh_pagelist(int newsize, int newpage)
1269 {
1270 if (
1271 #ifndef MOTIF
1272 (resource.expert_mode & XPRT_SHOW_BUTTONS) == 0 ||
1273 #endif
1274 !XtIsRealized(globals.widgets.top_level))
1275 return;
1276
1277 #ifdef DEBUG
1278 fprintf(stderr, "=== refresh_pagelist: newsize %d, newpage %d\n", newsize, newpage);
1279 #endif
1280 #ifdef MOTIF
1281 /* items = xrealloc(items, sizeof *items * (newsize + 2)); */
1282 init_pagelabels(0, newsize);
1283 xm_set_page_labels();
1284 #else /* MOTIF */
1285 if ((resource.expert_mode & XPRT_SHOW_BUTTONS) == 0)
1286 return;
1287
1288 /* FIXME - is this really neccessary?? The alternative:
1289 XawListChange(LIST_WIDGET, page_info.page_labels, newsize, 0, True);
1290 has problems when freeing the page labels afterwards.
1291 */
1292 handle_destroy_pagelist(LIST_WIDGET, NULL, NULL);
1293
1294 free_pagelabels();
1295 init_pagelabels(0, newsize);
1296
1297 xaw_create_pagelist();
1298 #endif /* MOTIF */
1299 /* `True' since the pagelist is newly created */
1300 maybe_scroll_pagelist(newpage, True);
1301 }
1302
1303 void
1304 maybe_scroll_pagelist(int newpage, Boolean force_recenter)
1305 {
1306 #ifdef MOTIF
1307 int top_visible, bot_visible;
1308 UNUSED(force_recenter);
1309 #endif
1310
1311 if (
1312 #ifndef MOTIF
1313 (resource.expert_mode & XPRT_SHOW_BUTTONS) == 0 ||
1314 #endif
1315 !XtIsRealized(globals.widgets.top_level))
1316 return;
1317
1318 #ifdef MOTIF
1319 XmListSelectPos(LIST_WIDGET, newpage + MOTIF_IDX_OFFSET, False);
1320
1321 top_visible = xm_get_top_visible(1);
1322 bot_visible = xm_get_bottom_visible(top_visible);
1323
1324 xm_maybe_scroll_pagelist(newpage, CLICK, &top_visible, &bot_visible);
1325 #if HAVE_XPM
1326 tb_check_navigation_sensitivity(current_page);
1327 #endif
1328 #else
1329
1330 if (LIST_WIDGET == NULL)
1331 return;
1332
1333 (void)xaw_maybe_scroll_pagelist(newpage + 1, force_recenter, 0);
1334
1335 XawListHighlight(LIST_WIDGET, newpage);
1336 #if REDRAW_CURRENT_MARKER_HACK
1337 /* if the XawListHighlight happens adjacent to the page that was
1338 last highlighted with our home-made `current selected'
1339 rectangle, it might overdraw that rectangle. In this case,
1340 restore it:
1341 */
1342 xaw_maybe_redraw_current_marker(newpage);
1343 #endif
1344 #endif
1345 }
1346
1347 #ifdef MOTIF
1348 static void
1349 set_all_marks(int from, int to)
1350 {
1351 int i;
1352 for (i = from; i < to; i++) {
1353 /* ensure_labelinfo_size(i); */
1354 page_info.index[i].marked = True;
1355 toggle_label(LIST_WIDGET, i, False);
1356 }
1357 }
1358
1359 static void
1360 internal_process_button2_drag(Widget widget, XEvent *event)
1361 {
1362 int i, idx, min = total_pages, max = 0;
1363 idx = get_item_index(widget, event->xbutton.y);
1364
1365 for (i = 0; i < total_pages; i++) {
1366 /* ensure_labelinfo_size(i); */
1367 if (page_info.index[i].marked && i > max)
1368 max = i;
1369 }
1370 for (i = total_pages; i > 0; i--) {
1371 /* ensure_labelinfo_size(i); */
1372 if (page_info.index[i].marked && i < min)
1373 min = i;
1374 }
1375
1376 if (min == total_pages)
1377 min = 0;
1378 if (max == 0)
1379 max = total_pages;
1380
1381 if (idx < min) {
1382 set_all_marks(idx, min);
1383 }
1384 else if (idx > max) {
1385 set_all_marks(max + 1, idx);
1386 }
1387 else {
1388 set_all_marks(0, idx);
1389 }
1390 }
1391 #endif /* MOTIF */
1392
1393 static void
1394 internal_process_button2(Widget widget, XEvent *event)
1395 {
1396 int curr_idx;
1397 static int prev_idx = 0;
1398 #ifndef MOTIF
1399 static int actual_idx = -2;
1400 #endif
1401 static int top_visible = 0, bot_visible = 0;
1402 static int prev_y = 0, curr_y = 0;
1403 static saveCmdT prev_cmd = 0; /* previous command (CLICK/SCROLL_UP/SCROLL_DOWN) */
1404 static saveCmdT curr_cmd = 0; /* current command (CLICK/SCROLL_UP/SCROLL_DOWN) */
1405 static saveCmdT bak_cmd = 0; /* last command that started inside the pagelist (CLICK/SCROLL_UP/SCROLL_DOWN) */
1406 static Boolean change_scroll_direction = False;
1407
1408 switch(event->xany.type) {
1409 case ButtonPress:
1410 prev_y = event->xbutton.y;
1411 prev_idx = curr_idx = get_item_index(widget, prev_y);
1412 #ifdef MOTIF
1413 top_visible = xm_get_top_visible(1);
1414 bot_visible = xm_get_bottom_visible(top_visible);
1415 #endif
1416 toggle_label(widget, curr_idx - 1, True);
1417 prev_cmd = CLICK;
1418 #ifdef MOTIF
1419 xm_maybe_scroll_pagelist(curr_idx - 1, CLICK, &top_visible, &bot_visible);
1420 #else
1421 actual_idx = xaw_maybe_scroll_pagelist(curr_idx, False, actual_idx);
1422 #endif
1423 break;
1424 case ButtonRelease:
1425 prev_cmd = CLICK;
1426 break;
1427 case MotionNotify:
1428 curr_y = (int)event->xmotion.y;
1429 curr_idx = get_item_index(widget, event->xmotion.y);
1430 #ifndef MOTIF
1431 if (actual_idx > 0) {
1432 curr_idx = actual_idx;
1433 }
1434 #endif
1435
1436 if (curr_y < prev_y)
1437 curr_cmd = SCROLL_UP;
1438 else if (curr_y > prev_y)
1439 curr_cmd = SCROLL_DOWN;
1440 prev_y = curr_y;
1441
1442 if (prev_cmd != CLICK && curr_cmd != prev_cmd)
1443 change_scroll_direction = True;
1444
1445 if ((curr_idx != prev_idx && !change_scroll_direction)
1446 || (change_scroll_direction
1447 /* last or first visible are always spared, unless they are really
1448 the first or last page; this way, always 1 more page is visible
1449 than is currently marked/selected
1450 */
1451 && !(curr_idx == top_visible) && !(curr_idx == bot_visible))) {
1452 if (curr_idx <= 0) {
1453 /* When user has scrolled off, mark this by setting curr_idx to 1 more or less
1454 than the pagelist has so that the last/first page don't oscillate between
1455 marked/unmarked state when user continues to scroll.
1456 Also, we continue scrolling as long as user drags in the same direction as
1457 the last `real' scrolling event (saved as bak_cmd).
1458 */
1459 if (curr_cmd == SCROLL_DOWN && bak_cmd == SCROLL_DOWN && prev_idx <= total_pages) {
1460 curr_idx = prev_idx + 1;
1461 }
1462 else if (curr_cmd == SCROLL_UP && bak_cmd == SCROLL_UP && prev_idx > 0) {
1463 curr_idx = prev_idx - 1;
1464 }
1465 }
1466
1467
1468 if (curr_idx > 0 && curr_idx <= total_pages) {
1469 toggle_label(widget, curr_idx - 1, True);
1470 #ifdef MOTIF
1471 xm_maybe_scroll_pagelist(curr_idx - 1, curr_cmd, &top_visible, &bot_visible);
1472 #else
1473 actual_idx = xaw_maybe_scroll_pagelist(curr_idx, False, actual_idx);
1474 #endif
1475 prev_idx = curr_idx;
1476 bak_cmd = curr_cmd;
1477 change_scroll_direction = False;
1478 }
1479 else {
1480 #ifndef MOTIF
1481 if (curr_idx > total_pages)
1482 actual_idx = -2;
1483 else
1484 actual_idx = xaw_maybe_scroll_pagelist(curr_idx, False, actual_idx);
1485 #endif
1486 }
1487 prev_cmd = curr_cmd;
1488 }
1489 break;
1490 default:
1491 break;
1492 }
1493 }
1494
1495 void
1496 list_toggle_current(int arg)
1497 {
1498 toggle_label(LIST_WIDGET, arg, True);
1499
1500 notify_print_dialog_have_marked();
1501 }
1502
1503 #ifdef MOTIF
1504
1505 static void
1506 xm_list_set_mark(Widget widget,
1507 XEvent *event,
1508 String *params,
1509 Cardinal *num_params)
1510 {
1511 UNUSED(params);
1512
1513 if ((*num_params != 1) || !XmIsList(widget))
1514 return;
1515
1516 internal_process_button2(widget, event);
1517
1518 if (event->type != ButtonPress)
1519 notify_print_dialog_have_marked();
1520 }
1521
1522 static void
1523 xm_list_drag_mark(Widget widget,
1524 XEvent *event,
1525 String *params,
1526 Cardinal *num_params)
1527 {
1528 UNUSED(params);
1529
1530 if ((*num_params != 1) || !XmIsList(widget))
1531 return;
1532
1533 internal_process_button2_drag(widget, event);
1534
1535 if (event->type != ButtonPress)
1536 notify_print_dialog_have_marked();
1537 }
1538
1539 #endif /* MOTIF */