| 1 |
/* |
| 2 |
* Copyright (c) 2001 Marcin Dalecki and others |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to |
| 6 |
* deal in the Software without restriction, including without limitation the |
| 7 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM, |
| 18 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "xdvi-config.h" |
| 25 |
#include "xdvi.h" |
| 26 |
|
| 27 |
#include "xm_menu.h" |
| 28 |
#include "xm_toolbar.h" |
| 29 |
#include "my-snprintf.h" |
| 30 |
#include "util.h" |
| 31 |
#include "x_util.h" |
| 32 |
#include "version.h" |
| 33 |
#include "statusline.h" |
| 34 |
#include "pagehist.h" |
| 35 |
#include "dvi-init.h" |
| 36 |
#include "filehist.h" |
| 37 |
#include "c-openmx.h" |
| 38 |
#include "message-window.h" |
| 39 |
|
| 40 |
#ifdef MOTIF /* needed for `make depend' */ |
| 41 |
|
| 42 |
#include <Xm/RowColumn.h> |
| 43 |
#include <Xm/Frame.h> |
| 44 |
#include <Xm/PushB.h> |
| 45 |
#include <Xm/PushBG.h> |
| 46 |
#include <Xm/CascadeBG.h> |
| 47 |
#include <Xm/SeparatoG.h> |
| 48 |
#include <Xm/ToggleB.h> |
| 49 |
#include <Xm/ToggleBG.h> |
| 50 |
#include <Xm/Separator.h> |
| 51 |
|
| 52 |
|
| 53 |
/* |
| 54 |
* There's an apparent bug in Motif related to the interaction between |
| 55 |
* the menubar menus and the magnifier. |
| 56 |
* |
| 57 |
* If you click on a menu on the menubar and then click on the drawing |
| 58 |
* widget to pop up a magnifier, the keyboard and pointer are still |
| 59 |
* grabbed, leading to a weird situation in which the magnifier stays |
| 60 |
* around even after you release the pointer. The ungrab_serial |
| 61 |
* counter works around this bug by ignoring such pointer events. |
| 62 |
* |
| 63 |
* This bug occurs in Motif 1.2 and OpenMotif 2.2.2 (at least). It |
| 64 |
* does not occur in Lesstif. |
| 65 |
*/ |
| 66 |
static unsigned long ungrab_event_num = 0; |
| 67 |
|
| 68 |
void |
| 69 |
popdown_callback(Widget w, XtPointer client_data, XtPointer call_data) |
| 70 |
{ |
| 71 |
UNUSED(w); |
| 72 |
UNUSED(client_data); |
| 73 |
/* Lesstif gives call_data == NULL */ |
| 74 |
if (call_data != NULL && *((XtGrabKind *) call_data) != XtGrabNone) { |
| 75 |
ungrab_event_num = LastKnownRequestProcessed(DISP); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
Boolean |
| 80 |
pulldown_menu_active(unsigned long event_num) |
| 81 |
{ |
| 82 |
return event_num < ungrab_event_num; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
void |
| 87 |
toggle_menubar(void) |
| 88 |
{ |
| 89 |
if ((resource.expert_mode & XPRT_SHOW_MENUBAR) == 0) |
| 90 |
XtUnmanageChild(globals.widgets.menu_bar); |
| 91 |
else |
| 92 |
XtManageChild(globals.widgets.menu_bar); |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
update_menu_labels(Widget menu) |
| 97 |
{ |
| 98 |
WidgetList children; |
| 99 |
int num_children; |
| 100 |
int i; |
| 101 |
|
| 102 |
static char *buf = NULL; |
| 103 |
static size_t buf_len = 0; |
| 104 |
|
| 105 |
XtVaGetValues(menu, |
| 106 |
XmNnumChildren, &num_children, |
| 107 |
XmNchildren, &children, |
| 108 |
NULL); |
| 109 |
|
| 110 |
/* for (i = 0; i < num_children; i++) { */ |
| 111 |
for (i = 0; i < (int)file_history_size(); i++) { |
| 112 |
int dummy_page; |
| 113 |
char *filename; |
| 114 |
size_t new_len; |
| 115 |
XmString str; |
| 116 |
|
| 117 |
if ((filename = file_history_get_elem(i, &dummy_page)) == NULL) { |
| 118 |
XDVI_ERROR((stderr, "Error accessing element %d of file history", i)); |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
new_len = LENGTH_OF_INT + strlen(filename) + 1; |
| 123 |
if (new_len > buf_len) { |
| 124 |
buf = xrealloc(buf, new_len); |
| 125 |
buf_len = new_len; |
| 126 |
} |
| 127 |
|
| 128 |
sprintf(buf, "%d %s", i + 1, filename); |
| 129 |
str = XmStringCreateLocalized(buf); |
| 130 |
XtVaSetValues(children[i], |
| 131 |
XmNlabelString, str, |
| 132 |
NULL); |
| 133 |
if (i + 1 < 10) { |
| 134 |
XtVaSetValues(children[i], XmNmnemonic, buf[0], NULL); |
| 135 |
} |
| 136 |
XmStringFree(str); |
| 137 |
} |
| 138 |
|
| 139 |
/* if history size < number of menu entries, destroy excess menu entries */ |
| 140 |
for (; i < num_children; i++) { |
| 141 |
XtDestroyWidget(children[i]); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
static void |
| 146 |
filehist_select_cb(Widget w, XtPointer client_data, XtPointer call_data) |
| 147 |
{ |
| 148 |
Widget menu; |
| 149 |
XmString label; |
| 150 |
char *label_ptr; |
| 151 |
int pageno; |
| 152 |
char *fname; |
| 153 |
int idx; |
| 154 |
|
| 155 |
/* UNUSED(w); */ |
| 156 |
UNUSED(call_data); |
| 157 |
UNUSED(client_data); |
| 158 |
|
| 159 |
XtVaGetValues(w, |
| 160 |
XmNuserData, &menu, |
| 161 |
XmNlabelString, &label, |
| 162 |
NULL); |
| 163 |
|
| 164 |
XmStringGetLtoR(label, G_charset, &label_ptr); |
| 165 |
idx = strtol(label_ptr, (char **)NULL, 10) - 1; |
| 166 |
XtFree(label_ptr); |
| 167 |
|
| 168 |
ASSERT(menu != NULL, "XmNuserData in filehist_select_cb musn't be NULL!"); |
| 169 |
|
| 170 |
if (idx == 0) { /* user re-selected current file: reload */ |
| 171 |
globals.ev.flags |= EV_RELOAD; |
| 172 |
return; |
| 173 |
} |
| 174 |
if ((fname = file_history_get_elem(idx, &pageno)) == NULL) { |
| 175 |
statusline_error(STATUS_MEDIUM, "Error accessing file %d of history", idx); |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
file_history_open(fname); |
| 180 |
} |
| 181 |
|
| 182 |
void |
| 183 |
filehist_menu_refresh(void) |
| 184 |
{ |
| 185 |
Widget menu; |
| 186 |
if (get_widget_by_name(&menu, globals.widgets.menu_bar, Xdvi_FILEHIST_MENU, True)) { |
| 187 |
update_menu_labels(menu); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void |
| 192 |
filehist_menu_add_entry(const char *filename) |
| 193 |
{ |
| 194 |
Widget menu; |
| 195 |
|
| 196 |
static char *buf = NULL; |
| 197 |
static size_t buf_len = 0; |
| 198 |
size_t new_len = LENGTH_OF_INT + strlen(filename) + 1; |
| 199 |
|
| 200 |
if (get_widget_by_name(&menu, globals.widgets.menu_bar, Xdvi_FILEHIST_MENU, True)) { |
| 201 |
int num_children; |
| 202 |
Widget w; |
| 203 |
|
| 204 |
if (new_len > buf_len) { |
| 205 |
buf = xrealloc(buf, new_len); |
| 206 |
buf_len = new_len; |
| 207 |
} |
| 208 |
|
| 209 |
XtVaGetValues(menu, XmNnumChildren, &num_children, NULL); |
| 210 |
|
| 211 |
sprintf(buf, "%d %s", num_children + 1, filename); |
| 212 |
|
| 213 |
w = XtVaCreateManagedWidget(buf, xmPushButtonGadgetClass, menu, |
| 214 |
XmNuserData, menu, |
| 215 |
NULL); |
| 216 |
if (num_children + 1 < 10) { |
| 217 |
XtVaSetValues(w, XmNmnemonic, buf[0], NULL); |
| 218 |
} |
| 219 |
XtAddCallback(w, XmNactivateCallback, filehist_select_cb, cast_int_to_XtPointer(num_children + 1)); |
| 220 |
|
| 221 |
update_menu_labels(menu); |
| 222 |
|
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
static void |
| 227 |
filehist_submenu(int idx, const char *filename, int pageno, void *data) |
| 228 |
{ |
| 229 |
Widget menu = (Widget)data; |
| 230 |
Widget w; |
| 231 |
static char *buf = NULL; |
| 232 |
static size_t buf_len = 0; |
| 233 |
size_t new_len = LENGTH_OF_INT + strlen(filename) + 1; |
| 234 |
|
| 235 |
if (new_len > buf_len) { |
| 236 |
buf = xrealloc(buf, new_len); |
| 237 |
buf_len = new_len; |
| 238 |
} |
| 239 |
|
| 240 |
UNUSED(pageno); |
| 241 |
|
| 242 |
sprintf(buf, "%d %s", idx + 1, filename); |
| 243 |
TRACE_GUI((stderr, "Creating menu `%s'", buf)); |
| 244 |
w = XtVaCreateManagedWidget(buf, xmPushButtonGadgetClass, menu, |
| 245 |
/* XmNmnemonic, buf[0], */ |
| 246 |
XmNuserData, menu, |
| 247 |
NULL); |
| 248 |
if (idx + 1 < 10) { |
| 249 |
XtVaSetValues(w, XmNmnemonic, buf[0], NULL); |
| 250 |
} |
| 251 |
XtAddCallback(w, XmNactivateCallback, filehist_select_cb, cast_int_to_XtPointer(idx)); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
static Widget |
| 257 |
recent_files_submenu(Widget parent, char *title, char mnemonic) |
| 258 |
{ |
| 259 |
Widget menu = 0, cascade = 0; |
| 260 |
XmString str; |
| 261 |
|
| 262 |
menu = XmCreatePulldownMenu(parent, Xdvi_FILEHIST_MENU, NULL, 0); |
| 263 |
str = XmStringCreateLocalized(title); |
| 264 |
cascade = XtVaCreateManagedWidget(title, xmCascadeButtonGadgetClass, parent, |
| 265 |
XmNsubMenuId, menu, |
| 266 |
XmNlabelString, str, |
| 267 |
XmNmnemonic, mnemonic, |
| 268 |
NULL); |
| 269 |
XmStringFree(str); |
| 270 |
|
| 271 |
file_history_enumerate(filehist_submenu, menu); |
| 272 |
return cascade; |
| 273 |
} |
| 274 |
|
| 275 |
Widget |
| 276 |
xm_create_menu(Widget parent, char *title, char mnemonic, struct button_info *item) |
| 277 |
{ |
| 278 |
Widget menu = 0, cascade = 0, w = 0; |
| 279 |
size_t i; |
| 280 |
XmString str; |
| 281 |
|
| 282 |
menu = XmCreatePulldownMenu(parent, "_pulldown", NULL, 0); |
| 283 |
str = XmStringCreateLocalized(title); |
| 284 |
cascade = XtVaCreateManagedWidget(title, xmCascadeButtonGadgetClass, parent, |
| 285 |
XmNsubMenuId, menu, |
| 286 |
XmNlabelString, str, |
| 287 |
XmNmnemonic, mnemonic, |
| 288 |
NULL); |
| 289 |
XmStringFree(str); |
| 290 |
|
| 291 |
/* add the menu items */ |
| 292 |
for (i = 0; item != NULL && i < item->size; i++) { |
| 293 |
item->elems[i].widget = 0; |
| 294 |
/* if there's a subitem, call this function recursively */ |
| 295 |
if (item->elems[i].submenu != NULL) { |
| 296 |
w = xm_create_menu(menu, item->elems[i].title, |
| 297 |
item->elems[i].mnemonic, item->elems[i].submenu); |
| 298 |
/* |
| 299 |
workaround for pointer grabbing bug; add additional callback to all menus |
| 300 |
(they have the same parent) |
| 301 |
*/ |
| 302 |
XtAddCallback(XtParent(menu), XtNpopdownCallback, |
| 303 |
popdown_callback, (XtPointer) 0); |
| 304 |
} |
| 305 |
else if (item->elems[i].action != NULL && item->elems[i].action->proc == Act_recent_files) { |
| 306 |
/* special case: submenu with recent files */ |
| 307 |
w = recent_files_submenu(menu, item->elems[i].title, item->elems[i].mnemonic); |
| 308 |
XtAddCallback(XtParent(menu), XtNpopdownCallback, |
| 309 |
popdown_callback, (XtPointer) 0); |
| 310 |
} |
| 311 |
else { |
| 312 |
switch(item->elems[i].type) { |
| 313 |
case BT_PUSH: |
| 314 |
w = XtVaCreateManagedWidget(item->elems[i].title, xmPushButtonGadgetClass, menu, |
| 315 |
NULL); |
| 316 |
break; |
| 317 |
case BT_RADIO: |
| 318 |
w = XtVaCreateManagedWidget(item->elems[i].title, xmToggleButtonGadgetClass, menu, |
| 319 |
XmNindicatorType, XmONE_OF_MANY, |
| 320 |
NULL); |
| 321 |
break; |
| 322 |
case BT_CHECK: |
| 323 |
w = XtVaCreateManagedWidget(item->elems[i].title, xmToggleButtonGadgetClass, menu, |
| 324 |
XmNindicatorType, XmN_OF_MANY, |
| 325 |
NULL); |
| 326 |
break; |
| 327 |
case BT_SEP: |
| 328 |
w = XtVaCreateManagedWidget(item->elems[i].title, xmSeparatorGadgetClass, menu, |
| 329 |
NULL); |
| 330 |
break; |
| 331 |
default: |
| 332 |
XDVI_WARNING((stderr, "unrecognized button type %d in menu %s (skipping this item).\n", |
| 333 |
item->elems[i].type, item->elems[i].title)); |
| 334 |
break; |
| 335 |
} |
| 336 |
item->elems[i].widget = w; |
| 337 |
} |
| 338 |
|
| 339 |
if (item->elems[i].mnemonic != 0) |
| 340 |
XtVaSetValues(w, XmNmnemonic, item->elems[i].mnemonic, NULL); |
| 341 |
|
| 342 |
if (item->elems[i].accelerator != NULL) { |
| 343 |
str = XmStringCreateLocalized(item->elems[i].accelerator); |
| 344 |
XtVaSetValues(w, XmNacceleratorText, str, NULL); |
| 345 |
XmStringFree(str); |
| 346 |
} |
| 347 |
|
| 348 |
if (item->elems[i].action != NULL) { |
| 349 |
String cb_type; |
| 350 |
if (XmIsToggleButton(w) || XmIsToggleButtonGadget(w)) |
| 351 |
cb_type = XmNvalueChangedCallback; |
| 352 |
else |
| 353 |
cb_type = XmNactivateCallback; |
| 354 |
XtAddCallback(w, cb_type, handle_command, item->elems[i].action); |
| 355 |
} |
| 356 |
} |
| 357 |
return cascade; |
| 358 |
} |
| 359 |
|
| 360 |
#else |
| 361 |
/* silence `empty compilation unit' warnings */ |
| 362 |
static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); } |
| 363 |
#endif /* MOTIF */ |
| 364 |
|