| 1 | /* Copyright Massachusetts Institute of Technology 1985 */ |
| 2 | |
| 3 | #include "copyright.h" |
| 4 | |
| 5 | /* |
| 6 | Copyright (C) 1993, 1996, 2001-2011 Free Software Foundation, Inc. |
| 7 | |
| 8 | This program is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | * XMenu: MIT Project Athena, X Window system menu package |
| 24 | * |
| 25 | * XMenuInternal.c - XMenu internal (not user visible) routines. |
| 26 | * |
| 27 | * Author: Tony Della Fera, DEC |
| 28 | * November, 1985 |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <config.h> |
| 33 | #include "XMenuInt.h" |
| 34 | |
| 35 | /* |
| 36 | * Internal Window creation queue sizes. |
| 37 | */ |
| 38 | #define S_QUE_SIZE 300 |
| 39 | #define P_QUE_SIZE 20 |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | * XMWinQue - Internal window creation queue datatype. |
| 44 | */ |
| 45 | typedef struct _xmwinquedef { |
| 46 | int sq_size; |
| 47 | XMSelect *sq[S_QUE_SIZE]; |
| 48 | XMSelect **sq_ptr; |
| 49 | int pq_size; |
| 50 | XMPane *pq[P_QUE_SIZE]; |
| 51 | XMPane **pq_ptr; |
| 52 | } XMWinQue; |
| 53 | |
| 54 | /* |
| 55 | * _XMWinQue - Internal static window creation queue. |
| 56 | */ |
| 57 | static Bool _XMWinQueIsInit = False; |
| 58 | static XMWinQue _XMWinQue; |
| 59 | |
| 60 | /* |
| 61 | * _XMErrorCode - Global XMenu error code. |
| 62 | */ |
| 63 | int _XMErrorCode = XME_NO_ERROR; |
| 64 | /* |
| 65 | * _XMErrorList - Global XMenu error code description strings. |
| 66 | */ |
| 67 | char const *const |
| 68 | _XMErrorList[XME_CODE_COUNT] = { |
| 69 | "No error", /* XME_NO_ERROR */ |
| 70 | "Menu not initialized", /* XME_NOT_INIT */ |
| 71 | "Argument out of bounds", /* XME_ARG_BOUNDS */ |
| 72 | "Pane not found", /* XME_P_NOT_FOUND */ |
| 73 | "Selection not found", /* XME_S_NOT_FOUND */ |
| 74 | "Invalid menu style parameter", /* XME_STYLE_PARAM */ |
| 75 | "Unable to grab mouse", /* XME_GRAB_MOUSE */ |
| 76 | "Unable to interpret locator", /* XME_INTERP_LOC */ |
| 77 | "Unable to calloc memory", /* XME_CALLOC */ |
| 78 | "Unable to create XAssocTable", /* XME_CREATE_ASSOC */ |
| 79 | "Unable to store bitmap", /* XME_STORE_BITMAP */ |
| 80 | "Unable to make tile pixmaps", /* XME_MAKE_TILES */ |
| 81 | "Unable to make pixmap", /* XME_MAKE_PIXMAP */ |
| 82 | "Unable to create cursor", /* XME_CREATE_CURSOR */ |
| 83 | "Unable to open font", /* XME_OPEN_FONT */ |
| 84 | "Unable to create windows", /* XME_CREATE_WINDOW */ |
| 85 | "Unable to create transparencies", /* XME_CREATE_TRANSP */ |
| 86 | }; |
| 87 | |
| 88 | /* |
| 89 | * _XMEventHandler - Internal event handler variable. |
| 90 | */ |
| 91 | int (*_XMEventHandler)(XEvent*) = NULL; |
| 92 | |
| 93 | |
| 94 | |
| 95 | /* |
| 96 | * _XMWinQueInit - Internal routine to initialize the window |
| 97 | * queue. |
| 98 | */ |
| 99 | void |
| 100 | _XMWinQueInit(void) |
| 101 | { |
| 102 | /* |
| 103 | * If the queue is not initialized initialize it. |
| 104 | */ |
| 105 | if (!_XMWinQueIsInit) { |
| 106 | /* |
| 107 | * Blank the queue structure. |
| 108 | */ |
| 109 | register int i; |
| 110 | |
| 111 | for (i = 0; i < S_QUE_SIZE; i++) |
| 112 | _XMWinQue.sq[i] = 0; |
| 113 | |
| 114 | for (i = 0; i < P_QUE_SIZE; i++) |
| 115 | _XMWinQue.pq[i] = 0; |
| 116 | |
| 117 | _XMWinQue.sq_size = _XMWinQue.pq_size = 0; |
| 118 | |
| 119 | /* |
| 120 | * Initialize the next free location pointers. |
| 121 | */ |
| 122 | _XMWinQue.sq_ptr = _XMWinQue.sq; |
| 123 | _XMWinQue.pq_ptr = _XMWinQue.pq; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | /* |
| 130 | * _XMWinQueAddPane - Internal routine to add a pane to the pane |
| 131 | * window queue. |
| 132 | */ |
| 133 | int |
| 134 | _XMWinQueAddPane(register Display *display, register XMenu *menu, register XMPane *p_ptr) |
| 135 | |
| 136 | /* Menu being manipulated. */ |
| 137 | /* XMPane being queued. */ |
| 138 | { |
| 139 | /* |
| 140 | * If the queue is currently full then flush it. |
| 141 | */ |
| 142 | if (_XMWinQue.pq_size == P_QUE_SIZE) { |
| 143 | if (_XMWinQueFlush(display, menu, 0, 0) == _FAILURE) return(_FAILURE); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Insert the new XMPane pointer and increment the queue pointer |
| 148 | * and the queue size. |
| 149 | */ |
| 150 | *_XMWinQue.pq_ptr = p_ptr; |
| 151 | _XMWinQue.pq_ptr++; |
| 152 | _XMWinQue.pq_size++; |
| 153 | |
| 154 | /* |
| 155 | * All went well, return successfully. |
| 156 | */ |
| 157 | _XMErrorCode = XME_NO_ERROR; |
| 158 | return(_SUCCESS); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | /* |
| 164 | * _XMWinQueAddSelection - Internal routine to add a selection to |
| 165 | * the selection window queue. |
| 166 | */ |
| 167 | int |
| 168 | _XMWinQueAddSelection(register Display *display, register XMenu *menu, register XMSelect *s_ptr) |
| 169 | |
| 170 | /* Menu being manipulated. */ |
| 171 | /* XMSelection being queued. */ |
| 172 | { |
| 173 | /* |
| 174 | * If this entry will overflow the queue then flush it. |
| 175 | */ |
| 176 | if (_XMWinQue.sq_size == S_QUE_SIZE) { |
| 177 | if (_XMWinQueFlush(display, menu, 0, 0) == _FAILURE) return(_FAILURE); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Insert the new XMSelect pointer and increment the queue pointer |
| 182 | * and the queue size. |
| 183 | */ |
| 184 | *_XMWinQue.sq_ptr = s_ptr; |
| 185 | _XMWinQue.sq_ptr++; |
| 186 | _XMWinQue.sq_size++; |
| 187 | |
| 188 | /* |
| 189 | * All went well, return successfully. |
| 190 | */ |
| 191 | _XMErrorCode = XME_NO_ERROR; |
| 192 | return(_SUCCESS); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |
| 197 | /* |
| 198 | * _XMWinQueFlush - Internal routine to flush the pane and |
| 199 | * selection window queues. |
| 200 | */ |
| 201 | int |
| 202 | _XMWinQueFlush(register Display *display, register XMenu *menu, register XMPane *pane, XMSelect *sel) |
| 203 | |
| 204 | /* Menu being manipulated. */ |
| 205 | /* Current pane. */ |
| 206 | { |
| 207 | register int pq_index; /* Pane queue index. */ |
| 208 | register int sq_index; /* Selection queue index. */ |
| 209 | register XMPane *p_ptr; /* XMPane pointer. */ |
| 210 | register XMSelect *s_ptr; /* XMSelect pointer. */ |
| 211 | unsigned long valuemask; /* Which attributes to set. */ |
| 212 | XSetWindowAttributes attributes_buf; /* Attributes to be set. */ |
| 213 | XSetWindowAttributes *attributes = &attributes_buf; |
| 214 | |
| 215 | /* |
| 216 | * If the pane window queue is not empty... |
| 217 | */ |
| 218 | |
| 219 | if (_XMWinQue.pq_size > 0) { |
| 220 | /* |
| 221 | * set up attributes for pane window to be created. |
| 222 | */ |
| 223 | valuemask = (CWBackPixmap | CWBorderPixel | CWOverrideRedirect); |
| 224 | attributes->border_pixel = menu->p_bdr_color; |
| 225 | attributes->background_pixmap = menu->inact_pixmap; |
| 226 | attributes->override_redirect = True; |
| 227 | |
| 228 | /* |
| 229 | * Create all the pending panes in order, so that the |
| 230 | * current pane will be on top, with the others |
| 231 | * stacked appropriately under it. |
| 232 | */ |
| 233 | for (pq_index = _XMWinQue.pq_size - 1; |
| 234 | pq_index >= 0; |
| 235 | pq_index--) |
| 236 | { |
| 237 | p_ptr = _XMWinQue.pq[pq_index]; /* Retrieve next pane. */ |
| 238 | if (p_ptr == pane) break; |
| 239 | p_ptr->window = XCreateWindow(display, |
| 240 | menu->parent, |
| 241 | p_ptr->window_x, |
| 242 | p_ptr->window_y, |
| 243 | p_ptr->window_w, |
| 244 | p_ptr->window_h, |
| 245 | menu->p_bdr_width, |
| 246 | CopyFromParent, |
| 247 | InputOutput, |
| 248 | CopyFromParent, |
| 249 | valuemask, |
| 250 | attributes); |
| 251 | XMakeAssoc(display, menu->assoc_tab, p_ptr->window, p_ptr); |
| 252 | XSelectInput(display, p_ptr->window, menu->p_events); |
| 253 | } |
| 254 | for (pq_index = 0; |
| 255 | pq_index < _XMWinQue.pq_size; |
| 256 | pq_index++) |
| 257 | { |
| 258 | p_ptr = _XMWinQue.pq[pq_index]; /* Retrieve next pane. */ |
| 259 | p_ptr->window = XCreateWindow(display, |
| 260 | menu->parent, |
| 261 | p_ptr->window_x, |
| 262 | p_ptr->window_y, |
| 263 | p_ptr->window_w, |
| 264 | p_ptr->window_h, |
| 265 | menu->p_bdr_width, |
| 266 | CopyFromParent, |
| 267 | InputOutput, |
| 268 | CopyFromParent, |
| 269 | valuemask, |
| 270 | attributes); |
| 271 | XMakeAssoc(display, menu->assoc_tab, p_ptr->window, p_ptr); |
| 272 | XSelectInput(display, p_ptr->window, menu->p_events); |
| 273 | if (p_ptr == pane) break; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Reset the pane queue pointer and size. |
| 278 | */ |
| 279 | _XMWinQue.pq_size = 0; |
| 280 | _XMWinQue.pq_ptr = _XMWinQue.pq; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * If the selection window queue is not empty... |
| 285 | */ |
| 286 | |
| 287 | if (_XMWinQue.sq_size > 0) { |
| 288 | |
| 289 | for (sq_index = 0; sq_index < _XMWinQue.sq_size; sq_index++) { |
| 290 | /* |
| 291 | * Retrieve the XMSelect pointer. |
| 292 | */ |
| 293 | s_ptr = _XMWinQue.sq[sq_index]; |
| 294 | s_ptr->window = XCreateWindow(display, |
| 295 | s_ptr->parent_p->window, |
| 296 | s_ptr->window_x, |
| 297 | s_ptr->window_y, |
| 298 | s_ptr->window_w, |
| 299 | s_ptr->window_h, |
| 300 | 0, /* border width*/ |
| 301 | CopyFromParent, |
| 302 | InputOnly, |
| 303 | CopyFromParent, |
| 304 | 0, |
| 305 | attributes); |
| 306 | |
| 307 | /* |
| 308 | * Insert the new window id and its |
| 309 | * associated XMSelect structure into the |
| 310 | * association table. |
| 311 | */ |
| 312 | XMakeAssoc(display, menu->assoc_tab, s_ptr->window, s_ptr); |
| 313 | XSelectInput(display, s_ptr->window, menu->s_events); |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Reset the selection queue pointer and size. |
| 318 | */ |
| 319 | _XMWinQue.sq_size = 0; |
| 320 | _XMWinQue.sq_ptr = _XMWinQue.sq; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Flush X's internal queues. |
| 325 | */ |
| 326 | XFlush(display); |
| 327 | |
| 328 | /* |
| 329 | * All went well, return successfully. |
| 330 | */ |
| 331 | _XMErrorCode = XME_NO_ERROR; |
| 332 | return(_SUCCESS); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | |
| 337 | /* |
| 338 | * _XMGetPanePtr - Given a menu pointer and a pane index number, return |
| 339 | * a pane pointer that points to the indexed pane. |
| 340 | */ |
| 341 | XMPane * |
| 342 | _XMGetPanePtr(register XMenu *menu, register int p_num) |
| 343 | /* Menu to find the pane in. */ |
| 344 | /* Index number of pane to find. */ |
| 345 | { |
| 346 | register XMPane *p_ptr; /* Pane pointer to be returned. */ |
| 347 | register int i; /* Loop counter. */ |
| 348 | |
| 349 | /* |
| 350 | * Is the pane number out of range? |
| 351 | */ |
| 352 | if ((p_num < 0) || (p_num > (menu->p_count - 1))) { |
| 353 | _XMErrorCode = XME_P_NOT_FOUND; |
| 354 | return(NULL); |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Find the right pane. |
| 359 | */ |
| 360 | p_ptr = menu->p_list->next; |
| 361 | for (i = 0; i < p_num; i++) p_ptr = p_ptr->next; |
| 362 | |
| 363 | /* |
| 364 | * Return successfully. |
| 365 | */ |
| 366 | _XMErrorCode = XME_NO_ERROR; |
| 367 | return(p_ptr); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | |
| 372 | /* |
| 373 | * _XMGetSelectionPtr - Given pane pointer and a selection index number, |
| 374 | * return a selection pointer that points to the |
| 375 | * indexed selection. |
| 376 | */ |
| 377 | XMSelect * |
| 378 | _XMGetSelectionPtr(register XMPane *p_ptr, register int s_num) |
| 379 | /* Pane to find the selection in. */ |
| 380 | /* Index number of the selection to find. */ |
| 381 | { |
| 382 | register XMSelect *s_ptr; /* Selection pointer to be returned. */ |
| 383 | register int i; /* Loop counter. */ |
| 384 | |
| 385 | /* |
| 386 | * Is the selection number out of range? |
| 387 | */ |
| 388 | if ((s_num < 0) || (s_num > (p_ptr->s_count - 1))) { |
| 389 | _XMErrorCode = XME_S_NOT_FOUND; |
| 390 | return(NULL); |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Find the right selection. |
| 395 | */ |
| 396 | s_ptr = p_ptr->s_list->next; |
| 397 | for (i = 0; i < s_num; i++) s_ptr = s_ptr->next; |
| 398 | |
| 399 | /* |
| 400 | * Return successfully. |
| 401 | */ |
| 402 | _XMErrorCode = XME_NO_ERROR; |
| 403 | return(s_ptr); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | |
| 408 | /* |
| 409 | * _XMRecomputeGlobals - Internal subroutine to recompute menu wide |
| 410 | * global values. |
| 411 | */ |
| 412 | void |
| 413 | _XMRecomputeGlobals(register Display *display, register XMenu *menu) |
| 414 | /*X11 display variable. */ |
| 415 | /* Menu object to compute from. */ |
| 416 | { |
| 417 | register XMPane *p_ptr; /* Pane pointer. */ |
| 418 | register XMSelect *s_ptr; /* Selection pointer. */ |
| 419 | |
| 420 | register int max_p_label = 0; /* Maximum pane label width. */ |
| 421 | register int max_s_label = 0; /* Maximum selection label width. */ |
| 422 | register int s_count = 0; /* Maximum selection count. */ |
| 423 | |
| 424 | int p_s_pad; /* Pane <-> selection padding. */ |
| 425 | int p_s_diff; /* Pane <-> selection separation. */ |
| 426 | |
| 427 | int p_height; /* Pane window height. */ |
| 428 | int p_width; /* Pane window width. */ |
| 429 | int s_width; /* Selection window width. */ |
| 430 | |
| 431 | int screen; /* DefaultScreen holder. */ |
| 432 | |
| 433 | /* |
| 434 | * For each pane... |
| 435 | */ |
| 436 | for ( |
| 437 | p_ptr = menu->p_list->next; |
| 438 | p_ptr != menu->p_list; |
| 439 | p_ptr = p_ptr->next |
| 440 | ){ |
| 441 | |
| 442 | /* |
| 443 | * Recompute maximum pane label width. |
| 444 | */ |
| 445 | max_p_label = max(max_p_label, p_ptr->label_width); |
| 446 | |
| 447 | /* |
| 448 | * Recompute maximum selection count. |
| 449 | */ |
| 450 | s_count = max(s_count, p_ptr->s_count); |
| 451 | |
| 452 | /* |
| 453 | * For each selection in the current pane... |
| 454 | */ |
| 455 | for ( |
| 456 | s_ptr = p_ptr->s_list->next; |
| 457 | s_ptr != p_ptr->s_list; |
| 458 | s_ptr = s_ptr->next |
| 459 | ){ |
| 460 | |
| 461 | /* |
| 462 | * Recompute maximum selection label width. |
| 463 | */ |
| 464 | max_s_label = max(max_s_label, s_ptr->label_width); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Recompute pane height. |
| 470 | */ |
| 471 | p_height = (menu->flag_height << 1) + (menu->s_y_off * s_count); |
| 472 | |
| 473 | /* |
| 474 | * Recompute horizontal padding between the pane window and the |
| 475 | * selection windows. |
| 476 | */ |
| 477 | p_s_pad = menu->p_x_off << 1; |
| 478 | |
| 479 | /* |
| 480 | * Recompute pane and selection window widths. |
| 481 | * This is done by first computing the window sizes from the maximum |
| 482 | * label widths. If the spacing between the selection window and the |
| 483 | * containing pane window is less than the pane selection padding value |
| 484 | * (twice the pane X offset) then change the size of the pane to be |
| 485 | * the size of the selection window plus the padding. If, however the |
| 486 | * spacing between the selection window and the containing pane window |
| 487 | * is more than the pane selection padding value increase the size of |
| 488 | * the selection to its maximum possible value (the pane width minus |
| 489 | * the pane selection padding value). |
| 490 | */ |
| 491 | p_width = max_p_label + p_s_pad; |
| 492 | s_width = max_s_label + (menu->s_fnt_pad << 1) + (menu->s_bdr_width << 1); |
| 493 | p_s_diff = p_width - s_width; |
| 494 | if (p_s_diff < p_s_pad) { |
| 495 | p_width = s_width + p_s_pad; |
| 496 | } |
| 497 | else if (p_s_diff > p_s_pad) { |
| 498 | s_width = p_width - p_s_pad; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Reset menu wide global values. |
| 503 | */ |
| 504 | menu->s_count = s_count; |
| 505 | menu->p_height = p_height; |
| 506 | menu->p_width = p_width; |
| 507 | menu->s_width = s_width; |
| 508 | |
| 509 | /* |
| 510 | * Ensure that the origin of the menu is placed so that |
| 511 | * None of the panes ore selections are off the screen. |
| 512 | */ |
| 513 | screen = DefaultScreen(display); |
| 514 | if (menu->x_pos + menu->width > DisplayWidth(display, screen)) |
| 515 | menu->x_pos = DisplayWidth(display, screen) - menu->width; |
| 516 | else if (menu->x_pos < 0) menu->x_pos = 0; |
| 517 | if(menu->y_pos + menu->height > DisplayHeight(display, screen)) |
| 518 | menu->y_pos = DisplayHeight(display, screen) - menu->height; |
| 519 | else if (menu->y_pos < 0) menu->y_pos = 0; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | /* |
| 524 | * _XMRecomputePane - Internal subroutine to recompute pane |
| 525 | * window dependencies. |
| 526 | */ |
| 527 | int |
| 528 | _XMRecomputePane(register Display *display, register XMenu *menu, register XMPane *p_ptr, register int p_num) |
| 529 | /* Standard X display variable. */ |
| 530 | /* Menu object being recomputed. */ |
| 531 | /* Pane pointer. */ |
| 532 | /* Pane sequence number. */ |
| 533 | { |
| 534 | register int window_x; /* Recomputed window X coordinate. */ |
| 535 | register int window_y; /* Recomputed window Y coordinate. */ |
| 536 | |
| 537 | unsigned long change_mask; /* Value mask to reconfigure window. */ |
| 538 | XWindowChanges *changes; /* Values to use in configure window. */ |
| 539 | |
| 540 | register Bool config_p = False; /* Reconfigure pane window? */ |
| 541 | |
| 542 | /* |
| 543 | * Update the pane serial number. |
| 544 | */ |
| 545 | p_ptr->serial = p_num; |
| 546 | |
| 547 | /* |
| 548 | * Recompute window X and Y coordinates. |
| 549 | */ |
| 550 | switch (menu->menu_style) { |
| 551 | case LEFT: |
| 552 | window_x = menu->p_x_off * ((menu->p_count - 1) - p_num); |
| 553 | window_y = menu->p_y_off * ((menu->p_count - 1) - p_num); |
| 554 | break; |
| 555 | case RIGHT: |
| 556 | window_x = menu->p_x_off * p_num; |
| 557 | window_y = menu->p_y_off * ((menu->p_count - 1) - p_num); |
| 558 | break; |
| 559 | case CENTER: |
| 560 | window_x = 0; |
| 561 | window_y = menu->p_y_off * ((menu->p_count - 1) - p_num); |
| 562 | break; |
| 563 | default: |
| 564 | /* Error! Invalid style parameter. */ |
| 565 | _XMErrorCode = XME_STYLE_PARAM; |
| 566 | return(_FAILURE); |
| 567 | } |
| 568 | window_x += menu->x_pos; |
| 569 | window_y += menu->y_pos; |
| 570 | |
| 571 | /* |
| 572 | * If the newly compute pane coordinates differ from the |
| 573 | * current coordinates, reset the current coordinates and |
| 574 | * reconfigure the pane. |
| 575 | */ |
| 576 | if ( |
| 577 | (window_x != p_ptr->window_x) || |
| 578 | (window_y != p_ptr->window_y) |
| 579 | ){ |
| 580 | /* |
| 581 | * Reset the coordinates and schedule |
| 582 | * the pane for reconfiguration. |
| 583 | */ |
| 584 | p_ptr->window_x = window_x; |
| 585 | p_ptr->window_y = window_y; |
| 586 | config_p = True; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * If the local pane width and height differs from the |
| 591 | * menu pane width and height, reset the local values. |
| 592 | */ |
| 593 | if ( |
| 594 | (p_ptr->window_w != menu->p_width) || |
| 595 | (p_ptr->window_h != menu->p_height) |
| 596 | ){ |
| 597 | /* |
| 598 | * Reset window width and height and schedule |
| 599 | * the pane for reconfiguration. |
| 600 | */ |
| 601 | p_ptr->window_w = menu->p_width; |
| 602 | p_ptr->window_h = menu->p_height; |
| 603 | config_p = True; |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * If we need to reconfigure the pane window do it now. |
| 608 | */ |
| 609 | if (config_p == True) { |
| 610 | /* |
| 611 | * If the pane window has already been created then |
| 612 | * reconfigure the existing window, otherwise queue |
| 613 | * it for creation with the new configuration. |
| 614 | */ |
| 615 | if (p_ptr->window) { |
| 616 | change_mask = (CWX | CWY | CWWidth | CWHeight); |
| 617 | changes = (XWindowChanges *)malloc(sizeof(XWindowChanges)); |
| 618 | changes->x = p_ptr->window_x; |
| 619 | changes->y = p_ptr->window_y; |
| 620 | changes->width = p_ptr->window_w; |
| 621 | changes->height = p_ptr->window_h; |
| 622 | |
| 623 | XConfigureWindow( |
| 624 | display, |
| 625 | p_ptr->window, |
| 626 | change_mask, |
| 627 | changes |
| 628 | ); |
| 629 | free(changes); |
| 630 | |
| 631 | } |
| 632 | else { |
| 633 | if (_XMWinQueAddPane(display, menu, p_ptr) == _FAILURE) { |
| 634 | return(_FAILURE); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * Recompute label X position. |
| 641 | */ |
| 642 | switch (menu->p_style) { |
| 643 | case LEFT: |
| 644 | p_ptr->label_x = menu->p_x_off + menu->p_fnt_pad; |
| 645 | break; |
| 646 | case RIGHT: |
| 647 | p_ptr->label_x = menu->p_width - |
| 648 | (p_ptr->label_width + menu->p_x_off + menu->p_fnt_pad); |
| 649 | break; |
| 650 | case CENTER: |
| 651 | p_ptr->label_x = (menu->p_width - p_ptr->label_width) >> 1; |
| 652 | break; |
| 653 | default: |
| 654 | /* Error! Invalid style parameter. */ |
| 655 | _XMErrorCode = XME_STYLE_PARAM; |
| 656 | return(_FAILURE); |
| 657 | } |
| 658 | /* |
| 659 | * Recompute label Y positions. |
| 660 | */ |
| 661 | p_ptr->label_uy = menu->p_fnt_pad + menu->p_fnt_info->max_bounds.ascent; |
| 662 | p_ptr->label_ly = (menu->p_height - menu->p_fnt_pad - menu->p_fnt_info->max_bounds.descent); |
| 663 | |
| 664 | /* |
| 665 | * All went well, return successfully. |
| 666 | */ |
| 667 | _XMErrorCode = XME_NO_ERROR; |
| 668 | return(_SUCCESS); |
| 669 | } |
| 670 | |
| 671 | |
| 672 | |
| 673 | /* |
| 674 | * _XMRecomputeSelection - Internal subroutine to recompute |
| 675 | * selection window dependencies. |
| 676 | */ |
| 677 | int |
| 678 | _XMRecomputeSelection(register Display *display, register XMenu *menu, register XMSelect *s_ptr, register int s_num) |
| 679 | |
| 680 | /* Menu object being recomputed. */ |
| 681 | /* Selection pointer. */ |
| 682 | /* Selection sequence number. */ |
| 683 | { |
| 684 | register Bool config_s = False; /* Reconfigure selection window? */ |
| 685 | XWindowChanges *changes; /* Values to change in configure. */ |
| 686 | unsigned long change_mask; /* Value mask for XConfigureWindow. */ |
| 687 | |
| 688 | /* |
| 689 | * If the selection serial numbers are out of order, begin |
| 690 | * resequencing selections. Recompute selection window coordinates |
| 691 | * and serial number. |
| 692 | * |
| 693 | * When selections are created they are given a serial number of |
| 694 | * -1, this causes this routine to give a new selection |
| 695 | * its initial coordinates and serial number. |
| 696 | */ |
| 697 | if (s_ptr->serial != s_num) { |
| 698 | /* |
| 699 | * Fix the sequence number. |
| 700 | */ |
| 701 | s_ptr->serial = s_num; |
| 702 | /* |
| 703 | * Recompute window X and Y coordinates. |
| 704 | */ |
| 705 | s_ptr->window_x = menu->s_x_off; |
| 706 | s_ptr->window_y = menu->flag_height + (menu->s_y_off * s_num); |
| 707 | /* |
| 708 | * We must reconfigure the window. |
| 709 | */ |
| 710 | config_s = True; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * If the local selection width and height differs from the |
| 715 | * menu selection width and height, reset the local values. |
| 716 | */ |
| 717 | if ( |
| 718 | (s_ptr->window_w != menu->s_width) || |
| 719 | (s_ptr->window_h != menu->s_height) |
| 720 | ){ |
| 721 | /* |
| 722 | * We must reconfigure the window. |
| 723 | */ |
| 724 | config_s = True; |
| 725 | /* |
| 726 | * Reset window width and height. |
| 727 | */ |
| 728 | s_ptr->window_w = menu->s_width; |
| 729 | s_ptr->window_h = menu->s_height; |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * If we need to reconfigure the selection window do it now. |
| 734 | */ |
| 735 | if (config_s == True) { |
| 736 | /* |
| 737 | * If the selection window has already been created then |
| 738 | * reconfigure the existing window, otherwise queue it |
| 739 | * for creation with the new configuration. |
| 740 | */ |
| 741 | if (s_ptr->window) { |
| 742 | changes = (XWindowChanges *)malloc(sizeof(XWindowChanges)); |
| 743 | change_mask = (CWX | CWY | CWWidth | CWHeight); |
| 744 | changes = (XWindowChanges *)malloc(sizeof(XWindowChanges)); |
| 745 | changes->x = s_ptr->window_x; |
| 746 | changes->y = s_ptr->window_y; |
| 747 | changes->width = s_ptr->window_w; |
| 748 | changes->height = s_ptr->window_h; |
| 749 | |
| 750 | XConfigureWindow( |
| 751 | display, |
| 752 | s_ptr->window, |
| 753 | change_mask, |
| 754 | changes |
| 755 | ); |
| 756 | free(changes); |
| 757 | |
| 758 | } |
| 759 | else { |
| 760 | if (_XMWinQueAddSelection(display, menu, s_ptr) == _FAILURE) { |
| 761 | return(_FAILURE); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Recompute label X position. |
| 768 | */ |
| 769 | switch (menu->s_style) { |
| 770 | case LEFT: |
| 771 | s_ptr->label_x = menu->s_bdr_width + menu->s_fnt_pad + s_ptr->window_x; |
| 772 | break; |
| 773 | case RIGHT: |
| 774 | s_ptr->label_x = s_ptr->window_x + menu->s_width - |
| 775 | (s_ptr->label_width + menu->s_bdr_width + menu->s_fnt_pad); |
| 776 | break; |
| 777 | case CENTER: |
| 778 | s_ptr->label_x = s_ptr->window_x + ((menu->s_width - s_ptr->label_width) >> 1); |
| 779 | break; |
| 780 | default: |
| 781 | /* Error! Invalid style parameter. */ |
| 782 | _XMErrorCode = XME_STYLE_PARAM; |
| 783 | return(_FAILURE); |
| 784 | } |
| 785 | /* |
| 786 | * Recompute label Y position. |
| 787 | */ |
| 788 | s_ptr->label_y = s_ptr->window_y + menu->s_fnt_info->max_bounds.ascent + menu->s_fnt_pad + menu->s_bdr_width; |
| 789 | |
| 790 | /* |
| 791 | * All went well, return successfully. |
| 792 | */ |
| 793 | _XMErrorCode = XME_NO_ERROR; |
| 794 | return(_SUCCESS); |
| 795 | } |
| 796 | |
| 797 | |
| 798 | |
| 799 | /* |
| 800 | * _XMTransToOrigin - Internal subroutine to translate the point at |
| 801 | * the center of the current pane and selection to the |
| 802 | * the menu origin. |
| 803 | * |
| 804 | * WARNING! ****** Be certain that all menu dependencies have been |
| 805 | * recomputed before calling this routine or |
| 806 | * unpredictable results will follow. |
| 807 | */ |
| 808 | void |
| 809 | _XMTransToOrigin(Display *display, register XMenu *menu, register XMPane *p_ptr, register XMSelect *s_ptr, int x_pos, int y_pos, int *orig_x, int *orig_y) |
| 810 | /* Not used. Included for consistency. */ |
| 811 | /* Menu being computed against. */ |
| 812 | /* Current pane pointer. */ |
| 813 | /* Current selection pointer. */ |
| 814 | /* X coordinate of point to translate. */ |
| 815 | /* Y coordinate of point to translate. */ |
| 816 | /* Return value X coord. of the menu origin. */ |
| 817 | /* Return value Y coord. of the menu origin. */ |
| 818 | { |
| 819 | register int l_orig_x; /* Local X coordinate of the menu origin. */ |
| 820 | register int l_orig_y; /* Local Y coordinate of the menu origin. */ |
| 821 | |
| 822 | /* |
| 823 | * Translate the menu origin such that the cursor hot point will be in the |
| 824 | * center of the desired current selection and pane. |
| 825 | * If the current selection pointer is NULL then assume that the hot point |
| 826 | * will be in the center of the current pane flag. |
| 827 | */ |
| 828 | |
| 829 | if (s_ptr == NULL) { |
| 830 | /* |
| 831 | * Translate from the center of the pane flag to the upper left |
| 832 | * of the current pane window. |
| 833 | */ |
| 834 | l_orig_x = x_pos - (menu->p_width >> 1) - menu->p_bdr_width; |
| 835 | l_orig_y = y_pos - (menu->flag_height >> 1) - menu->p_bdr_width; |
| 836 | } |
| 837 | else { |
| 838 | /* |
| 839 | * First translate from the center of the current selection |
| 840 | * to the upper left of the current selection window. |
| 841 | */ |
| 842 | l_orig_x = x_pos - (menu->s_width >> 1); |
| 843 | l_orig_y = y_pos - (menu->s_height >> 1); |
| 844 | |
| 845 | /* |
| 846 | * Then translate to the upper left of the current pane window. |
| 847 | */ |
| 848 | l_orig_x -= (s_ptr->window_x + menu->p_bdr_width); |
| 849 | l_orig_y -= (s_ptr->window_y + menu->p_bdr_width); |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Finally translate to the upper left of the menu. |
| 854 | */ |
| 855 | l_orig_x -= (p_ptr->window_x - menu->x_pos); |
| 856 | l_orig_y -= (p_ptr->window_y - menu->y_pos); |
| 857 | |
| 858 | /* |
| 859 | * Set the return values. |
| 860 | */ |
| 861 | *orig_x = l_orig_x; |
| 862 | *orig_y = l_orig_y; |
| 863 | } |
| 864 | |
| 865 | /* |
| 866 | * _XMRefreshPane - Internal subroutine to completely refresh |
| 867 | * the contents of a pane. |
| 868 | */ |
| 869 | void |
| 870 | _XMRefreshPane(register Display *display, register XMenu *menu, register XMPane *pane) |
| 871 | { |
| 872 | register XMSelect *s_list = pane->s_list; |
| 873 | register XMSelect *s_ptr; |
| 874 | |
| 875 | /* |
| 876 | * First clear the pane. |
| 877 | */ |
| 878 | XClearWindow(display, pane->window); |
| 879 | if (!pane->activated) { |
| 880 | XFillRectangle(display, |
| 881 | pane->window, |
| 882 | menu->inverse_select_GC, |
| 883 | pane->label_x - menu->p_fnt_pad, |
| 884 | pane->label_uy - menu->p_fnt_info->max_bounds.ascent - menu->p_fnt_pad, |
| 885 | pane->label_width + (menu->p_fnt_pad << 1), |
| 886 | menu->flag_height); |
| 887 | |
| 888 | XFillRectangle(display, |
| 889 | pane->window, |
| 890 | menu->inverse_select_GC, |
| 891 | pane->label_x - menu->p_fnt_pad, |
| 892 | pane->label_ly - menu->p_fnt_info->max_bounds.ascent - menu->p_fnt_pad, |
| 893 | pane->label_width + (menu->p_fnt_pad << 1), |
| 894 | menu->flag_height); |
| 895 | } |
| 896 | if (!pane->active) { |
| 897 | XDrawString(display, |
| 898 | pane->window, |
| 899 | menu->inact_GC, |
| 900 | pane->label_x, pane->label_uy, |
| 901 | pane->label, pane->label_length); |
| 902 | XDrawString(display, |
| 903 | pane->window, |
| 904 | menu->inact_GC, |
| 905 | pane->label_x, pane->label_ly, |
| 906 | pane->label, pane->label_length); |
| 907 | } |
| 908 | else { |
| 909 | XDrawString(display, |
| 910 | pane->window, |
| 911 | menu->pane_GC, |
| 912 | pane->label_x, pane->label_uy, |
| 913 | pane->label, pane->label_length); |
| 914 | XDrawString(display, |
| 915 | pane->window, |
| 916 | menu->pane_GC, |
| 917 | pane->label_x, pane->label_ly, |
| 918 | pane->label, pane->label_length); |
| 919 | |
| 920 | /* |
| 921 | * Finally refresh each selection if the pane is activated. |
| 922 | */ |
| 923 | if (pane->activated) { |
| 924 | for (s_ptr = s_list->next; s_ptr != s_list; s_ptr = s_ptr->next) |
| 925 | _XMRefreshSelection(display, menu, s_ptr); |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | /* |
| 934 | * _XMRefreshSelection - Internal subroutine that refreshes |
| 935 | * a single selection window. |
| 936 | */ |
| 937 | void |
| 938 | _XMRefreshSelection(register Display *display, register XMenu *menu, register XMSelect *sel) |
| 939 | { |
| 940 | register int width = sel->window_w; |
| 941 | register int height = sel->window_h; |
| 942 | register int bdr_width = menu->s_bdr_width; |
| 943 | |
| 944 | if (sel->type == SEPARATOR) { |
| 945 | XDrawLine(display, |
| 946 | sel->parent_p->window, |
| 947 | menu->normal_select_GC, |
| 948 | sel->window_x, |
| 949 | sel->window_y + height / 2, |
| 950 | sel->window_x + width, |
| 951 | sel->window_y + height / 2); |
| 952 | } |
| 953 | else if (sel->activated) { |
| 954 | if (menu->menu_mode == INVERT) { |
| 955 | XFillRectangle(display, |
| 956 | sel->parent_p->window, |
| 957 | menu->normal_select_GC, |
| 958 | sel->window_x, sel->window_y, |
| 959 | width, height); |
| 960 | XDrawString(display, |
| 961 | sel->parent_p->window, |
| 962 | menu->inverse_select_GC, |
| 963 | sel->label_x, |
| 964 | sel->label_y, |
| 965 | sel->label, sel->label_length); |
| 966 | } |
| 967 | else { |
| 968 | /* |
| 969 | * Using BOX mode. |
| 970 | * Since most drawing routines with arbitrary width lines |
| 971 | * are slow compared to raster-ops lets use a raster-op to |
| 972 | * draw the boxes. |
| 973 | */ |
| 974 | |
| 975 | XDrawRectangle(display, |
| 976 | sel->parent_p->window, |
| 977 | menu->normal_select_GC, |
| 978 | sel->window_x + (bdr_width >> 1), |
| 979 | sel->window_y + (bdr_width >> 1 ), |
| 980 | width - bdr_width, |
| 981 | height - bdr_width); |
| 982 | XDrawString(display, |
| 983 | sel->parent_p->window, |
| 984 | menu->normal_select_GC, |
| 985 | sel->label_x, |
| 986 | sel->label_y, |
| 987 | sel->label, sel->label_length); |
| 988 | } |
| 989 | } |
| 990 | else { |
| 991 | XClearArea(display, |
| 992 | sel->parent_p->window, |
| 993 | sel->window_x, sel->window_y, |
| 994 | width, height, |
| 995 | False); |
| 996 | if (sel->active) { |
| 997 | XDrawString(display, |
| 998 | sel->parent_p->window, |
| 999 | menu->normal_select_GC, |
| 1000 | sel->label_x, |
| 1001 | sel->label_y, |
| 1002 | sel->label, sel->label_length); |
| 1003 | } |
| 1004 | else { |
| 1005 | XDrawString(display, |
| 1006 | sel->parent_p->window, |
| 1007 | menu->inact_GC, |
| 1008 | sel->label_x, |
| 1009 | sel->label_y, |
| 1010 | sel->label, sel->label_length); |
| 1011 | } |
| 1012 | } |
| 1013 | } |