xlwmenu.c 40.8 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Implements a lightweight menubar widget.  
   Copyright (C) 1992 Lucid, Inc.

This file is part of the Lucid Widget Library.

The Lucid Widget Library is free software; you can redistribute it and/or 
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

The Lucid Widget Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
Paul Reilly's avatar
Paul Reilly committed
17
Alongalong with GNU Emacs; see the file COPYING.  If not, write to
Richard M. Stallman's avatar
Richard M. Stallman committed
18 19 20 21 22 23 24 25 26
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Created by devin@lucid.com */

#include <stdio.h>

#include <sys/types.h>
#include <X11/Xos.h>
#include <X11/IntrinsicP.h>
Paul Reilly's avatar
Paul Reilly committed
27
#include <X11/ObjectP.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
28 29 30 31
#include <X11/StringDefs.h>
#include <X11/cursorfont.h>
#include <X11/bitmaps/gray>
#include "xlwmenuP.h"
Paul Reilly's avatar
Paul Reilly committed
32 33 34

static int pointer_grabbed;
static XEvent menu_post_event;
Richard M. Stallman's avatar
Richard M. Stallman committed
35

36 37
XFontStruct *xlwmenu_default_font;

Richard M. Stallman's avatar
Richard M. Stallman committed
38 39 40
static char 
xlwMenuTranslations [] = 
"<BtnDown>:	start()\n\
Paul Reilly's avatar
Paul Reilly committed
41
<Motion>:	drag()\n\
Richard M. Stallman's avatar
Richard M. Stallman committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
<BtnUp>:	select()\n\
";

#define offset(field) XtOffset(XlwMenuWidget, field)
static XtResource 
xlwMenuResources[] =
{ 
  {XtNfont,  XtCFont, XtRFontStruct, sizeof(XFontStruct *),
     offset(menu.font),XtRString, "XtDefaultFont"},
  {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
     offset(menu.foreground), XtRString, "XtDefaultForeground"},
  {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
     offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
  {XtNmargin, XtCMargin, XtRDimension,  sizeof(Dimension),
     offset(menu.margin), XtRImmediate, (XtPointer)0},
  {XtNhorizontalSpacing, XtCMargin, XtRDimension,  sizeof(Dimension),
     offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
  {XtNverticalSpacing, XtCMargin, XtRDimension,  sizeof(Dimension),
     offset(menu.vertical_spacing), XtRImmediate, (XtPointer)1},
  {XtNarrowSpacing, XtCMargin, XtRDimension,  sizeof(Dimension),
     offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},

Paul Reilly's avatar
Paul Reilly committed
64
  {XmNshadowThickness, XmCShadowThickness, XtRDimension,
Richard M. Stallman's avatar
Richard M. Stallman committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
     sizeof (Dimension), offset (menu.shadow_thickness),
     XtRImmediate, (XtPointer) 2},
  {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
     offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
  {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
     offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
  {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
     offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
  {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
     offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},

  {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer), 
     offset(menu.open), XtRCallback, (XtPointer)NULL},
  {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer), 
     offset(menu.select), XtRCallback, (XtPointer)NULL},
  {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
     offset(menu.contents), XtRImmediate, (XtPointer)NULL},
  {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
     offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
  {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
     offset(menu.horizontal), XtRImmediate, (XtPointer)True},
};
#undef offset

static Boolean XlwMenuSetValues();
static void XlwMenuRealize();
static void XlwMenuRedisplay();
static void XlwMenuResize();
static void XlwMenuInitialize();
static void XlwMenuRedisplay();
static void XlwMenuDestroy();
static void XlwMenuClassInitialize();
static void Start();
static void Drag();
static void Select();

static XtActionsRec 
xlwMenuActionsList [] =
{
  {"start",		Start},
  {"drag",		Drag},
  {"select",		Select},
};

#define SuperClass ((CoreWidgetClass)&coreClassRec)

XlwMenuClassRec xlwMenuClassRec =
{
  {  /* CoreClass fields initialization */
    (WidgetClass) SuperClass,		/* superclass		  */	
    "XlwMenu",				/* class_name		  */
    sizeof(XlwMenuRec),			/* size			  */
    XlwMenuClassInitialize,		/* class_initialize	  */
    NULL,				/* class_part_initialize  */
    FALSE,				/* class_inited		  */
    XlwMenuInitialize,			/* initialize		  */
    NULL,				/* initialize_hook	  */
    XlwMenuRealize,			/* realize		  */
    xlwMenuActionsList,			/* actions		  */
    XtNumber(xlwMenuActionsList),	/* num_actions		  */
    xlwMenuResources,			/* resources		  */
    XtNumber(xlwMenuResources),		/* resource_count	  */
    NULLQUARK,				/* xrm_class		  */
    TRUE,				/* compress_motion	  */
    TRUE,				/* compress_exposure	  */
    TRUE,				/* compress_enterleave    */
    FALSE,				/* visible_interest	  */
    XlwMenuDestroy,			/* destroy		  */
    XlwMenuResize,			/* resize		  */
    XlwMenuRedisplay,			/* expose		  */
    XlwMenuSetValues,			/* set_values		  */
    NULL,				/* set_values_hook	  */
    XtInheritSetValuesAlmost,		/* set_values_almost	  */
    NULL,				/* get_values_hook	  */
    NULL,				/* accept_focus		  */
    XtVersion,				/* version		  */
    NULL,				/* callback_private	  */
    xlwMenuTranslations,		/* tm_table		  */
    XtInheritQueryGeometry,		/* query_geometry	  */
    XtInheritDisplayAccelerator,	/* display_accelerator	  */
    NULL				/* extension		  */
  },  /* XlwMenuClass fields initialization */
  {
    0					/* dummy */
  },
};

WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;

Paul Reilly's avatar
Paul Reilly committed
154 155 156 157
int submenu_destroyed;

static int next_release_must_exit;

Richard M. Stallman's avatar
Richard M. Stallman committed
158 159
/* Utilities */
static void
Paul Reilly's avatar
Paul Reilly committed
160 161 162
push_new_stack (mw, val)
     XlwMenuWidget mw;
     widget_value* val;
Richard M. Stallman's avatar
Richard M. Stallman committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
{
  if (!mw->menu.new_stack)
    {
      mw->menu.new_stack_length = 10;
      mw->menu.new_stack =
	(widget_value**)XtCalloc (mw->menu.new_stack_length,
				  sizeof (widget_value*));
    }
  else if (mw->menu.new_depth == mw->menu.new_stack_length)
    {
      mw->menu.new_stack_length *= 2;
      mw->menu.new_stack =
	(widget_value**)XtRealloc ((char*)mw->menu.new_stack,
				   mw->menu.new_stack_length * sizeof (widget_value*));
    }
  mw->menu.new_stack [mw->menu.new_depth++] = val;
}

static void
Paul Reilly's avatar
Paul Reilly committed
182 183
pop_new_stack_if_no_contents (mw)
     XlwMenuWidget mw;
Richard M. Stallman's avatar
Richard M. Stallman committed
184 185 186 187 188 189 190 191 192
{
  if (mw->menu.new_depth)
    {
      if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
	mw->menu.new_depth -= 1;
    }
}

static void
Paul Reilly's avatar
Paul Reilly committed
193 194 195
make_old_stack_space (mw, n)
     XlwMenuWidget mw;
     int n;
Richard M. Stallman's avatar
Richard M. Stallman committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
{
  if (!mw->menu.old_stack)
    {
      mw->menu.old_stack_length = 10;
      mw->menu.old_stack =
	(widget_value**)XtCalloc (mw->menu.old_stack_length,
				  sizeof (widget_value*));
    }
  else if (mw->menu.old_stack_length < n)
    {
      mw->menu.old_stack_length *= 2;
      mw->menu.old_stack =
	(widget_value**)XtRealloc ((char*)mw->menu.old_stack,
				   mw->menu.old_stack_length * sizeof (widget_value*));
    }
}

/* Size code */
static Boolean
Paul Reilly's avatar
Paul Reilly committed
215 216
all_dashes_p (s)
     char *s;
Richard M. Stallman's avatar
Richard M. Stallman committed
217 218 219 220 221 222
{
  char* p;
  for (p = s; *p == '-'; p++);
  return !*p;
}

Paul Reilly's avatar
Paul Reilly committed
223 224 225 226
int
string_width (mw, s)
     XlwMenuWidget mw;
     char *s;
Richard M. Stallman's avatar
Richard M. Stallman committed
227 228 229 230 231 232 233 234 235
{
  XCharStruct xcs;
  int drop;
  
  XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
  return xcs.width;
}

static int
Paul Reilly's avatar
Paul Reilly committed
236 237
arrow_width (mw)
     XlwMenuWidget mw;
Richard M. Stallman's avatar
Richard M. Stallman committed
238 239 240 241 242 243 244 245 246 247 248 249
{
  return mw->menu.font->ascent / 2 | 1;
}

static XtResource
nameResource[] =
{ 
  {"labelString",  "LabelString", XtRString, sizeof(String),
     0, XtRImmediate, 0},
};

static char*
Paul Reilly's avatar
Paul Reilly committed
250 251 252
resource_widget_value (mw, val)
     XlwMenuWidget mw;
     widget_value *val;
Richard M. Stallman's avatar
Richard M. Stallman committed
253 254 255 256 257 258 259 260 261 262 263 264
{
  if (!val->toolkit_data)
    {
      char* resourced_name = NULL;
      char* complete_name;
      XtGetSubresources ((Widget) mw,
			 (XtPointer) &resourced_name,
			 val->name, val->name,
			 nameResource, 1, NULL, 0);
      if (!resourced_name)
	resourced_name = val->name;
      if (!val->value)
265 266 267 268
	{
	  complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
	  strcpy (complete_name, resourced_name);
	}
Richard M. Stallman's avatar
Richard M. Stallman committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
      else
	{
	  int complete_length =
	    strlen (resourced_name) + strlen (val->value) + 2;
	  complete_name = XtMalloc (complete_length);
	  *complete_name = 0;
	  strcat (complete_name, resourced_name);
	  strcat (complete_name, " ");
	  strcat (complete_name, val->value);
	}

      val->toolkit_data = complete_name;
      val->free_toolkit_data = True;
    }
  return (char*)val->toolkit_data;
}

/* Returns the sizes of an item */
static void
Paul Reilly's avatar
Paul Reilly committed
288 289 290 291 292 293 294
size_menu_item (mw, val, horizontal_p, label_width, rest_width, height)
     XlwMenuWidget mw;
     widget_value* val;
     int horizontal_p;
     int* label_width;
     int* rest_width;
     int* height;
Richard M. Stallman's avatar
Richard M. Stallman committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
{
  if (all_dashes_p (val->name))
    {
      *height = 2;
      *label_width = 1;
      *rest_width = 0;
    }
  else
    {
      *height =
	mw->menu.font->ascent + mw->menu.font->descent
	  + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness;
      
      *label_width =
	string_width (mw, resource_widget_value (mw, val))
	  + mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
      
      *rest_width =  mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
      if (!horizontal_p)
	{
	  if (val->contents)
	    *rest_width += arrow_width (mw) + mw->menu.arrow_spacing;
	  else if (val->key)
	    *rest_width +=
	      string_width (mw, val->key) + mw->menu.arrow_spacing;
	}
    }
}

static void
Paul Reilly's avatar
Paul Reilly committed
325 326 327
size_menu (mw, level)
     XlwMenuWidget mw;
     int level;
Richard M. Stallman's avatar
Richard M. Stallman committed
328
{
329
  unsigned int  label_width = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
330 331
  int		rest_width = 0;
  int		max_rest_width = 0;
332
  unsigned int  height = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  int		horizontal_p = mw->menu.horizontal && (level == 0);
  widget_value*	val;
  window_state*	ws;

  if (level >= mw->menu.old_depth)
    abort ();

  ws = &mw->menu.windows [level];  
  ws->width = 0;
  ws->height = 0;
  ws->label_width = 0;

  for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
    {
      size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
		      &height);
      if (horizontal_p)
	{
	  ws->width += label_width + rest_width;
	  if (height > ws->height)
	    ws->height = height;
	}
      else
	{
	  if (label_width > ws->label_width)
	    ws->label_width = label_width;
	  if (rest_width > max_rest_width)
	    max_rest_width = rest_width;
	  ws->height += height;
	}
    }
  
  if (horizontal_p)
    ws->label_width = 0;
  else
    ws->width = ws->label_width + max_rest_width;

  ws->width += 2 * mw->menu.shadow_thickness;
  ws->height += 2 * mw->menu.shadow_thickness;
}


/* Display code */
static void
Paul Reilly's avatar
Paul Reilly committed
377 378 379 380 381 382 383
draw_arrow (mw, window, gc, x, y, width)
     XlwMenuWidget mw;
     Window window;
     GC gc;
     int x;
     int y;
     int width;
Richard M. Stallman's avatar
Richard M. Stallman committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397
{
  XPoint points [3];
  points [0].x = x;
  points [0].y = y + mw->menu.font->ascent;
  points [1].x = x;
  points [1].y = y;
  points [2].x = x + width;
  points [2].y = y + mw->menu.font->ascent / 2;
  
  XFillPolygon (XtDisplay (mw), window, gc, points, 3, Convex,
		CoordModeOrigin);
}

static void
Paul Reilly's avatar
Paul Reilly committed
398 399 400 401 402 403 404 405
draw_shadow_rectangle (mw, window, x, y, width, height, erase_p)
     XlwMenuWidget mw;
     Window window;
     int x;
     int y;
     int width;
     int height;
     int erase_p;
Richard M. Stallman's avatar
Richard M. Stallman committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
{
  Display *dpy = XtDisplay (mw);
  GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
  GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
  int thickness = mw->menu.shadow_thickness;
  XPoint points [4];
  points [0].x = x;
  points [0].y = y;
  points [1].x = x + width;
  points [1].y = y;
  points [2].x = x + width - thickness;
  points [2].y = y + thickness;
  points [3].x = x;
  points [3].y = y + thickness;
  XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
  points [0].x = x;
  points [0].y = y + thickness;
  points [1].x = x;
  points [1].y = y + height;
  points [2].x = x + thickness;
  points [2].y = y + height - thickness;
  points [3].x = x + thickness;
  points [3].y = y + thickness;
  XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
  points [0].x = x + width;
  points [0].y = y;
  points [1].x = x + width - thickness;
  points [1].y = y + thickness;
  points [2].x = x + width - thickness;
  points [2].y = y + height - thickness;
  points [3].x = x + width;
  points [3].y = y + height - thickness;
  XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
  points [0].x = x;
  points [0].y = y + height;
  points [1].x = x + width;
  points [1].y = y + height;
  points [2].x = x + width;
  points [2].y = y + height - thickness;
  points [3].x = x + thickness;
  points [3].y = y + height - thickness;
  XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
}


/* Display the menu item and increment where.x and where.y to show how large
** the menu item was. 
*/
static void
Paul Reilly's avatar
Paul Reilly committed
455 456 457 458 459 460 461 462
display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p, just_compute_p)
     XlwMenuWidget mw;
     widget_value* val;
     window_state* ws;
     XPoint* where;
     Boolean highlighted_p;
     Boolean horizontal_p;
     Boolean just_compute_p;
Richard M. Stallman's avatar
Richard M. Stallman committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
{
  GC deco_gc;
  GC text_gc;
  int font_ascent = mw->menu.font->ascent;
  int font_descent = mw->menu.font->descent;
  int shadow = mw->menu.shadow_thickness;
  int separator_p = all_dashes_p (val->name);
  int h_spacing = mw->menu.horizontal_spacing;
  int v_spacing = mw->menu.vertical_spacing;
  int label_width;
  int rest_width;
  int height;
  int width;
  int button_p;

  /* compute the sizes of the item */
  size_menu_item (mw, val, horizontal_p, &label_width, &rest_width, &height);

  if (horizontal_p)
    width = label_width + rest_width;
  else
    {
      label_width = ws->label_width;
      width = ws->width - 2 * shadow;
    }

  /* see if it should be a button in the menubar */
  button_p = horizontal_p && val->call_data;

  /* Only highlight an enabled item that has a callback. */
  if (highlighted_p)
    if (!val->enabled || !(val->call_data || val->contents))
      highlighted_p = 0;

  /* do the drawing. */
  if (!just_compute_p)
    {
      /* Add the shadow border of the containing menu */
      int x = where->x + shadow;
      int y = where->y + shadow;

      /* pick the foreground and background GC. */
      if (val->enabled)
	text_gc = button_p ? mw->menu.button_gc : mw->menu.foreground_gc;
      else
	text_gc =
	  button_p ? mw->menu.inactive_button_gc : mw->menu.inactive_gc;
      deco_gc = mw->menu.foreground_gc;

      if (separator_p)
	{
	  XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_bottom_gc,
		     x, y, x + width, y);
	  XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_top_gc,
		     x, y + 1, x + width, y + 1);
	}
      else 
	{
521
	  int x_offset = x + h_spacing + shadow;
Richard M. Stallman's avatar
Richard M. Stallman committed
522 523
	  char* display_string = resource_widget_value (mw, val);
	  draw_shadow_rectangle (mw, ws->window, x, y, width, height, True);
524 525 526 527 528 529 530 531 532 533

	  /* Deal with centering a menu title. */
	  if (!horizontal_p && !val->contents && !val->call_data)
	    {
	      int l = string_width (mw, display_string);

	      if (width > l)
		x_offset = (width - l) >> 1;
	    }
          XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset,
Richard M. Stallman's avatar
Richard M. Stallman committed
534 535 536 537 538 539 540 541 542
		       y + v_spacing + shadow + font_ascent,
		       display_string, strlen (display_string));
	  
	  if (!horizontal_p)
	    {
	      if (val->contents)
		{
		  int a_w = arrow_width (mw);
		  draw_arrow (mw, ws->window, deco_gc,
Paul Reilly's avatar
Paul Reilly committed
543 544 545
			      x + width - arrow_width (mw)
			      - mw->menu.horizontal_spacing 
			      - mw->menu.shadow_thickness,
Richard M. Stallman's avatar
Richard M. Stallman committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
			      y + v_spacing + shadow, a_w);
		}
	      else if (val->key)
		{
		  XDrawString (XtDisplay (mw), ws->window, text_gc,
			       x + label_width + mw->menu.arrow_spacing,
			       y + v_spacing + shadow + font_ascent,
			       val->key, strlen (val->key));
		}
	    }

	  else if (button_p)
	    {
#if 1
	      XDrawRectangle (XtDisplay (mw), ws->window, deco_gc,
			      x + shadow, y + shadow,
			      label_width + h_spacing - 1,
			      font_ascent + font_descent + 2 * v_spacing - 1);
Paul Reilly's avatar
Paul Reilly committed
564 565
	      draw_shadow_rectangle (mw, ws->window, x, y, width, height,
				     False);
Richard M. Stallman's avatar
Richard M. Stallman committed
566 567 568 569
#else
	      highlighted_p = True;
#endif
	    }
Paul Reilly's avatar
Paul Reilly committed
570 571 572 573 574 575 576 577 578 579
	  else
	    {
	      XDrawRectangle (XtDisplay (mw), ws->window, 
			      mw->menu.background_gc,
			      x + shadow, y + shadow,
			      label_width + h_spacing - 1,
			      font_ascent + font_descent + 2 * v_spacing - 1);
	      draw_shadow_rectangle (mw, ws->window, x, y, width, height,
				     True);
	    }
Richard M. Stallman's avatar
Richard M. Stallman committed
580 581 582 583 584 585 586 587 588 589 590

	  if (highlighted_p)
	    draw_shadow_rectangle (mw, ws->window, x, y, width, height, False);
	}
    }
  
  where->x += width;
  where->y += height;
}

static void
Paul Reilly's avatar
Paul Reilly committed
591 592 593 594 595 596 597 598 599 600
display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
	      this, that)
     XlwMenuWidget mw;
     int level;
     Boolean just_compute_p;
     XPoint* highlighted_pos;
     XPoint* hit;
     widget_value** hit_return;
     widget_value* this;
     widget_value* that;
Richard M. Stallman's avatar
Richard M. Stallman committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
{
  widget_value*	val;
  widget_value* following_item;
  window_state* ws;
  XPoint	where;
  int horizontal_p = mw->menu.horizontal && (level == 0);
  int highlighted_p;
  int just_compute_this_one_p;

  if (level >= mw->menu.old_depth)
    abort ();

  if (level < mw->menu.old_depth - 1)
    following_item = mw->menu.old_stack [level + 1];
  else 
    following_item = NULL;

  if (hit)
    *hit_return = NULL;

  where.x = 0;
  where.y = 0;

  ws = &mw->menu.windows [level];
  for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
    {
      highlighted_p = val == following_item;
      if (highlighted_p && highlighted_pos)
	{
	  if (horizontal_p)
	    highlighted_pos->x = where.x;
	  else
	    highlighted_pos->y = where.y;
	}
      
      just_compute_this_one_p =
	just_compute_p || ((this || that) && val != this &&  val != that);

      display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
			 just_compute_this_one_p);

      if (highlighted_p && highlighted_pos)
	{
	  if (horizontal_p)
	    highlighted_pos->y = where.y;
	  else
	    highlighted_pos->x = where.x;
	}

      if (hit
	  && !*hit_return
	  && (horizontal_p ? hit->x < where.x : hit->y < where.y)
	  && !all_dashes_p (val->name))
	*hit_return = val;

      if (horizontal_p)
	where.y = 0;
      else
	where.x = 0;
    }
  
  if (!just_compute_p)
    draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height, False);
}

/* Motion code */
static void
Paul Reilly's avatar
Paul Reilly committed
668 669 670 671
set_new_state (mw, val, level)
     XlwMenuWidget mw;
     widget_value* val;
     int level;
Richard M. Stallman's avatar
Richard M. Stallman committed
672 673 674 675 676 677 678 679 680 681
{
  int i;
  
  mw->menu.new_depth = 0;
  for (i = 0; i < level; i++)
    push_new_stack (mw, mw->menu.old_stack [i]);
  push_new_stack (mw, val);
}

static void
Paul Reilly's avatar
Paul Reilly committed
682 683 684
make_windows_if_needed (mw, n)
     XlwMenuWidget mw;
     int n;
Richard M. Stallman's avatar
Richard M. Stallman committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
{
  int i;
  int start_at;
  XSetWindowAttributes xswa;
  int mask;
  Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
  window_state* windows;
  
  if (mw->menu.windows_length >= n)
    return;

  xswa.save_under = True;
  xswa.override_redirect = True;
  xswa.background_pixel = mw->core.background_pixel;
  xswa.border_pixel = mw->core.border_pixel;
  xswa.event_mask =
Paul Reilly's avatar
Paul Reilly committed
701
    ExposureMask | PointerMotionMask | PointerMotionHintMask
Richard M. Stallman's avatar
Richard M. Stallman committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
      | ButtonReleaseMask | ButtonPressMask;
  xswa.cursor = mw->menu.cursor_shape;
  mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
    | CWEventMask | CWCursor;
  
  if (!mw->menu.windows)
    {
      mw->menu.windows =
	(window_state*)XtMalloc (n * sizeof (window_state));
      start_at = 0;
    }
  else
    {
      mw->menu.windows =
	(window_state*)XtRealloc ((char*)mw->menu.windows,
				  n * sizeof (window_state));
      start_at = mw->menu.windows_length;
    }
  mw->menu.windows_length = n;

  windows = mw->menu.windows;

  for (i = start_at; i < n; i++)
   {
     windows [i].x = 0;
     windows [i].y = 0;
     windows [i].width = 1;
     windows [i].height = 1;
     windows [i].window =
       XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
		      0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
  }
}

/* Make the window fit in the screen */
static void
Paul Reilly's avatar
Paul Reilly committed
738 739 740 741 742
fit_to_screen (mw, ws, previous_ws, horizontal_p)
     XlwMenuWidget mw;
     window_state* ws;
     window_state* previous_ws;
     Boolean horizontal_p;
Richard M. Stallman's avatar
Richard M. Stallman committed
743
{
744 745
  unsigned int screen_width = WidthOfScreen (XtScreen (mw));
  unsigned int screen_height = HeightOfScreen (XtScreen (mw));
Richard M. Stallman's avatar
Richard M. Stallman committed
746 747 748 749 750 751 752 753 754

  if (ws->x < 0)
    ws->x = 0;
  else if (ws->x + ws->width > screen_width)
    {
      if (!horizontal_p)
	ws->x = previous_ws->x - ws->width;
      else
	ws->x = screen_width - ws->width;
755 756
      if (ws->x < 0)
        ws->x = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
757 758 759 760 761 762 763 764 765
    }
  if (ws->y < 0)
    ws->y = 0;
  else if (ws->y + ws->height > screen_height)
    {
      if (horizontal_p)
	ws->y = previous_ws->y - ws->height;
      else
	ws->y = screen_height - ws->height;
766 767
      if (ws->y < 0) 
        ws->y = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
768 769 770 771 772
    }
}

/* Updates old_stack from new_stack and redisplays. */
static void
Paul Reilly's avatar
Paul Reilly committed
773 774
remap_menubar (mw)
     XlwMenuWidget mw;
Richard M. Stallman's avatar
Richard M. Stallman committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
{
  int i;
  int last_same;
  XPoint selection_position;
  int old_depth = mw->menu.old_depth;
  int new_depth = mw->menu.new_depth;
  widget_value** old_stack;
  widget_value** new_stack;
  window_state* windows;
  widget_value* old_selection;
  widget_value* new_selection;

  /* Check that enough windows and old_stack are ready. */
  make_windows_if_needed (mw, new_depth);
  make_old_stack_space (mw, new_depth);
  windows = mw->menu.windows;
  old_stack = mw->menu.old_stack;
  new_stack = mw->menu.new_stack;

  /* compute the last identical different entry */
  for (i = 1; i < old_depth && i < new_depth; i++)
    if (old_stack [i] != new_stack [i])
      break;
  last_same = i - 1;

  /* Memorize the previously selected item to be able to refresh it */
  old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
  if (old_selection && !old_selection->enabled)
    old_selection = NULL;
  new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
  if (new_selection && !new_selection->enabled)
    new_selection = NULL;

  /* updates old_state from new_state.  It has to be done now because
     display_menu (called below) uses the old_stack to know what to display. */
  for (i = last_same + 1; i < new_depth; i++)
    old_stack [i] = new_stack [i];
  mw->menu.old_depth = new_depth;

  /* refresh the last seletion */
  selection_position.x = 0;
  selection_position.y = 0;
  display_menu (mw, last_same, new_selection == old_selection,
		&selection_position, NULL, NULL, old_selection, new_selection);

  /* Now popup the new menus */
  for (i = last_same + 1; i < new_depth && new_stack [i]->contents; i++)
    {
      window_state* previous_ws = &windows [i - 1];
      window_state* ws = &windows [i];

      ws->x =
	previous_ws->x + selection_position.x + mw->menu.shadow_thickness;
      if (!mw->menu.horizontal || i > 1)
	ws->x += mw->menu.shadow_thickness;
      ws->y =
	previous_ws->y + selection_position.y + mw->menu.shadow_thickness;

      size_menu (mw, i);

      fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);

      XClearWindow (XtDisplay (mw), ws->window);
      XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
			 ws->width, ws->height);
      XMapRaised (XtDisplay (mw), ws->window);
      display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
    }

  /* unmap the menus that popped down */
  for (i = new_depth - 1; i < old_depth; i++)
    if (i >= new_depth || !new_stack [i]->contents)
      XUnmapWindow (XtDisplay (mw), windows [i].window);
}

static Boolean
Paul Reilly's avatar
Paul Reilly committed
851 852 853 854 855
motion_event_is_in_menu (mw, ev, level, relative_pos)
     XlwMenuWidget mw;
     XMotionEvent* ev;
     int level;
     XPoint* relative_pos;
Richard M. Stallman's avatar
Richard M. Stallman committed
856 857
{
  window_state* ws = &mw->menu.windows [level];
858 859
  unsigned int x = level == 0 ? ws->x : ws->x + mw->menu.shadow_thickness;
  unsigned int y = level == 0 ? ws->y : ws->y + mw->menu.shadow_thickness;
Richard M. Stallman's avatar
Richard M. Stallman committed
860 861 862 863 864 865 866
  relative_pos->x = ev->x_root - x;
  relative_pos->y = ev->y_root - y;
  return (x < ev->x_root && ev->x_root < x + ws->width
	  && y < ev->y_root && ev->y_root < y + ws->height);
}

static Boolean
Paul Reilly's avatar
Paul Reilly committed
867 868 869 870 871
map_event_to_widget_value (mw, ev, val, level)
     XlwMenuWidget mw;
     XMotionEvent* ev;
     widget_value** val;
     int* level;
Richard M. Stallman's avatar
Richard M. Stallman committed
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
{
  int 		i;
  XPoint	relative_pos;
  window_state*	ws;

  *val = NULL;
  
  /* Find the window */
  for (i = mw->menu.old_depth - 1; i >= 0; i--)
    {
      ws = &mw->menu.windows [i];
      if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
	{
	  display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);

	  if (*val)
	    {
	      *level = i + 1;
	      return True;
	    }
	}
    }
  return False;
}

/* Procedures */
static void
Paul Reilly's avatar
Paul Reilly committed
899 900
make_drawing_gcs (mw)
     XlwMenuWidget mw;
Richard M. Stallman's avatar
Richard M. Stallman committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
{
  XGCValues xgcv;

  xgcv.font = mw->menu.font->fid;
  xgcv.foreground = mw->menu.foreground;
  xgcv.background = mw->core.background_pixel;
  mw->menu.foreground_gc = XtGetGC ((Widget)mw,
				    GCFont | GCForeground | GCBackground,
				    &xgcv);
  
  xgcv.font = mw->menu.font->fid;
  xgcv.foreground = mw->menu.button_foreground;
  xgcv.background = mw->core.background_pixel;
  mw->menu.button_gc = XtGetGC ((Widget)mw,
				GCFont | GCForeground | GCBackground,
				&xgcv);
  
  xgcv.font = mw->menu.font->fid;
  xgcv.foreground = mw->menu.foreground;
  xgcv.background = mw->core.background_pixel;
  xgcv.fill_style = FillStippled;
  xgcv.stipple = mw->menu.gray_pixmap;
  mw->menu.inactive_gc = XtGetGC ((Widget)mw,
				  (GCFont | GCForeground | GCBackground
				   | GCFillStyle | GCStipple), &xgcv);
  
  xgcv.font = mw->menu.font->fid;
  xgcv.foreground = mw->menu.button_foreground;
  xgcv.background = mw->core.background_pixel;
  xgcv.fill_style = FillStippled;
  xgcv.stipple = mw->menu.gray_pixmap;
  mw->menu.inactive_button_gc = XtGetGC ((Widget)mw,
				  (GCFont | GCForeground | GCBackground
				   | GCFillStyle | GCStipple), &xgcv);
  
  xgcv.font = mw->menu.font->fid;
  xgcv.foreground = mw->core.background_pixel;
  xgcv.background = mw->menu.foreground;
  mw->menu.background_gc = XtGetGC ((Widget)mw,
				    GCFont | GCForeground | GCBackground,
				    &xgcv);
}

static void
Paul Reilly's avatar
Paul Reilly committed
945 946
release_drawing_gcs (mw)
     XlwMenuWidget mw;
Richard M. Stallman's avatar
Richard M. Stallman committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
{
  XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
  XtReleaseGC ((Widget) mw, mw->menu.button_gc);
  XtReleaseGC ((Widget) mw, mw->menu.inactive_gc);
  XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
  XtReleaseGC ((Widget) mw, mw->menu.background_gc);
  /* let's get some segvs if we try to use these... */
  mw->menu.foreground_gc = (GC) -1;
  mw->menu.button_gc = (GC) -1;
  mw->menu.inactive_gc = (GC) -1;
  mw->menu.inactive_button_gc = (GC) -1;
  mw->menu.background_gc = (GC) -1;
}

#define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
		   ? ((unsigned long) (x)) : ((unsigned long) (y)))

static void
Paul Reilly's avatar
Paul Reilly committed
965 966
make_shadow_gcs (mw)
     XlwMenuWidget mw;
Richard M. Stallman's avatar
Richard M. Stallman committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
{
  XGCValues xgcv;
  unsigned long pm = 0;
  Display *dpy = XtDisplay ((Widget) mw);
  Colormap cmap = DefaultColormapOfScreen (XtScreen ((Widget) mw));
  XColor topc, botc;
  int top_frobbed = 0, bottom_frobbed = 0;

  if (mw->menu.top_shadow_color == -1)
    mw->menu.top_shadow_color = mw->core.background_pixel;
  if (mw->menu.bottom_shadow_color == -1)
    mw->menu.bottom_shadow_color = mw->menu.foreground;

  if (mw->menu.top_shadow_color == mw->core.background_pixel ||
      mw->menu.top_shadow_color == mw->menu.foreground)
    {
      topc.pixel = mw->core.background_pixel;
      XQueryColor (dpy, cmap, &topc);
      /* don't overflow/wrap! */
      topc.red   = MINL (65535, topc.red   * 1.2);
      topc.green = MINL (65535, topc.green * 1.2);
      topc.blue  = MINL (65535, topc.blue  * 1.2);
      if (XAllocColor (dpy, cmap, &topc))
	{
	  mw->menu.top_shadow_color = topc.pixel;
	  top_frobbed = 1;
	}
    }
  if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
      mw->menu.bottom_shadow_color == mw->core.background_pixel)
    {
      botc.pixel = mw->core.background_pixel;
      XQueryColor (dpy, cmap, &botc);
      botc.red   *= 0.6;
      botc.green *= 0.6;
      botc.blue  *= 0.6;
      if (XAllocColor (dpy, cmap, &botc))
	{
	  mw->menu.bottom_shadow_color = botc.pixel;
	  bottom_frobbed = 1;
	}
    }

  if (top_frobbed && bottom_frobbed)
    {
      int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
      int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
      if (bot_avg > top_avg)
	{
	  Pixel tmp = mw->menu.top_shadow_color;
	  mw->menu.top_shadow_color = mw->menu.bottom_shadow_color;
	  mw->menu.bottom_shadow_color = tmp;
	}
      else if (topc.pixel == botc.pixel)
	{
	  if (botc.pixel == mw->menu.foreground)
	    mw->menu.top_shadow_color = mw->core.background_pixel;
	  else
	    mw->menu.bottom_shadow_color = mw->menu.foreground;
	}
    }

  if (!mw->menu.top_shadow_pixmap &&
      mw->menu.top_shadow_color == mw->core.background_pixel)
    {
      mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
      mw->menu.top_shadow_color = mw->menu.foreground;
    }
  if (!mw->menu.bottom_shadow_pixmap &&
      mw->menu.bottom_shadow_color == mw->core.background_pixel)
    {
      mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
      mw->menu.bottom_shadow_color = mw->menu.foreground;
    }

  xgcv.fill_style = FillStippled;
  xgcv.foreground = mw->menu.top_shadow_color;
  xgcv.stipple = mw->menu.top_shadow_pixmap;
  pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
  mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);

  xgcv.foreground = mw->menu.bottom_shadow_color;
  xgcv.stipple = mw->menu.bottom_shadow_pixmap;
  pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
  mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
}


static void
Paul Reilly's avatar
Paul Reilly committed
1056 1057
release_shadow_gcs (mw)
     XlwMenuWidget mw;
Richard M. Stallman's avatar
Richard M. Stallman committed
1058 1059 1060 1061 1062 1063
{
  XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
  XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
}

static void
1064
XlwMenuInitialize (request, mw, args, num_args)
Paul Reilly's avatar
Paul Reilly committed
1065
     Widget request;
1066
     XlwMenuWidget mw;
Paul Reilly's avatar
Paul Reilly committed
1067 1068
     ArgList args;
     Cardinal *num_args;
Richard M. Stallman's avatar
Richard M. Stallman committed
1069 1070 1071 1072 1073 1074 1075 1076
{
  /* Get the GCs and the widget size */
  XSetWindowAttributes xswa;
  int mask;
  
  Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
  Display* display = XtDisplay (mw);
  
Paul Reilly's avatar
Paul Reilly committed
1077 1078 1079 1080 1081 1082 1083 1084 1085
#if 0
  widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));

  /* _XtCreate is freeing the object that was passed to us,
     so make a copy that we will actually keep.  */
  lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
  mw->menu.contents = tem;
#endif

Richard M. Stallman's avatar
Richard M. Stallman committed
1086 1087 1088
/*  mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
  mw->menu.cursor = mw->menu.cursor_shape;
  
1089 1090 1091 1092
  mw->menu.gray_pixmap
    = XCreatePixmapFromBitmapData (display, window, gray_bits,
				   gray_width, gray_height,
				   (unsigned long)1, (unsigned long)0, 1);
Richard M. Stallman's avatar
Richard M. Stallman committed
1093
  
1094 1095 1096 1097 1098 1099
  /* I don't understand why this ends up 0 sometimes,
     but it does.  This kludge works around it.
     Can anyone find a real fix?   -- rms.  */
  if (mw->menu.font == 0)
    mw->menu.font = xlwmenu_default_font;

Richard M. Stallman's avatar
Richard M. Stallman committed
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
  make_drawing_gcs (mw);
  make_shadow_gcs (mw);
  
  xswa.background_pixel = mw->core.background_pixel;
  xswa.border_pixel = mw->core.border_pixel;
  mask = CWBackPixel | CWBorderPixel;
  
  mw->menu.popped_up = False;
  
  mw->menu.old_depth = 1;
  mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
  mw->menu.old_stack_length = 1;
  mw->menu.old_stack [0] = mw->menu.contents;
  
  mw->menu.new_depth = 0;
  mw->menu.new_stack = 0;
  mw->menu.new_stack_length = 0;
  push_new_stack (mw, mw->menu.contents);
  
  mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
  mw->menu.windows_length = 1;
  mw->menu.windows [0].x = 0;
  mw->menu.windows [0].y = 0;
  mw->menu.windows [0].width = 0;
  mw->menu.windows [0].height = 0;
  size_menu (mw, 0);
  
  mw->core.width = mw->menu.windows [0].width;
  mw->core.height = mw->menu.windows [0].height;
}

static void
XlwMenuClassInitialize ()
{
}

static void
Paul Reilly's avatar
Paul Reilly committed
1137 1138 1139 1140
XlwMenuRealize (w, valueMask, attributes)
     Widget w;
     Mask *valueMask;
     XSetWindowAttributes *attributes;
Richard M. Stallman's avatar
Richard M. Stallman committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
{
  XlwMenuWidget mw = (XlwMenuWidget)w;
  XSetWindowAttributes xswa;
  int mask;

  (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
    (w, valueMask, attributes);

  xswa.save_under = True;
  xswa.cursor = mw->menu.cursor_shape;
  mask = CWSaveUnder | CWCursor;
  XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);

  mw->menu.windows [0].window = XtWindow (w);
  mw->menu.windows [0].x = w->core.x;
  mw->menu.windows [0].y = w->core.y;
  mw->menu.windows [0].width = w->core.width;
  mw->menu.windows [0].height = w->core.height;
}

/* Only the toplevel menubar/popup is a widget so it's the only one that
   receives expose events through Xt.  So we repaint all the other panes
   when receiving an Expose event. */
static void 
Paul Reilly's avatar
Paul Reilly committed
1165 1166 1167 1168
XlwMenuRedisplay (w, ev, region)
     Widget w;
     XEvent* ev;
     Region region;
Richard M. Stallman's avatar
Richard M. Stallman committed
1169 1170 1171 1172
{
  XlwMenuWidget mw = (XlwMenuWidget)w;
  int i;

Paul Reilly's avatar
Paul Reilly committed
1173 1174 1175 1176 1177 1178 1179 1180
  /* If we have a depth beyond 1, it's because a submenu was displayed.
     If the submenu has been destroyed, set the depth back to 1.  */
  if (submenu_destroyed)
    {
      mw->menu.old_depth = 1;
      submenu_destroyed = 0;
    }

Richard M. Stallman's avatar
Richard M. Stallman committed
1181 1182 1183 1184 1185
  for (i = 0; i < mw->menu.old_depth; i++)
    display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
}

static void 
Paul Reilly's avatar
Paul Reilly committed
1186 1187
XlwMenuDestroy (w)
     Widget w;
Richard M. Stallman's avatar
Richard M. Stallman committed
1188 1189 1190 1191
{
  int i;
  XlwMenuWidget mw = (XlwMenuWidget) w;

Paul Reilly's avatar
Paul Reilly committed
1192 1193 1194 1195 1196 1197
  if (pointer_grabbed)
    XtUngrabPointer ((Widget)w, CurrentTime);
  pointer_grabbed = 0;

  submenu_destroyed = 1;

Richard M. Stallman's avatar
Richard M. Stallman committed
1198 1199 1200 1201 1202 1203 1204 1205
  release_drawing_gcs (mw);
  release_shadow_gcs (mw);

  /* this doesn't come from the resource db but is created explicitly
     so we must free it ourselves. */
  XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
  mw->menu.gray_pixmap = (Pixmap) -1;

Paul Reilly's avatar
Paul Reilly committed
1206 1207 1208 1209 1210 1211
#if 0
  /* Do free mw->menu.contents because nowadays we copy it
     during initialization.  */
  XtFree (mw->menu.contents);
#endif

Richard M. Stallman's avatar
Richard M. Stallman committed
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
  /* Don't free mw->menu.contents because that comes from our creator.
     The `*_stack' elements are just pointers into `contents' so leave
     that alone too.  But free the stacks themselves. */
  if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
  if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);

  /* Remember, you can't free anything that came from the resource
     database.  This includes:
         mw->menu.cursor
         mw->menu.top_shadow_pixmap
         mw->menu.bottom_shadow_pixmap
         mw->menu.font
     Also the color cells of top_shadow_color, bottom_shadow_color,
     foreground, and button_foreground will never be freed until this
     client exits.  Nice, eh?
   */

  /* start from 1 because the one in slot 0 is w->core.window */
  for (i = 1; i < mw->menu.windows_length; i++)
    XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
  if (mw->menu.windows)
    XtFree ((char *) mw->menu.windows);
}

static Boolean 
Paul Reilly's avatar
Paul Reilly committed
1237 1238 1239 1240
XlwMenuSetValues (current, request, new)
     Widget current;
     Widget request;
     Widget new;
Richard M. Stallman's avatar
Richard M. Stallman committed
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
{
  XlwMenuWidget oldmw = (XlwMenuWidget)current;
  XlwMenuWidget newmw = (XlwMenuWidget)new;
  Boolean redisplay = False;
  int i;

  if (newmw->menu.contents
      && newmw->menu.contents->contents
      && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
    redisplay = True;

  if (newmw->core.background_pixel != oldmw->core.background_pixel
Paul Reilly's avatar
Paul Reilly committed
1253 1254
      || newmw->menu.foreground != oldmw->menu.foreground
      || newmw->menu.font != oldmw->menu.font)
Richard M. Stallman's avatar
Richard M. Stallman committed
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
    {
      release_drawing_gcs (newmw);
      make_drawing_gcs (newmw);
      redisplay = True;
      
      for (i = 0; i < oldmw->menu.windows_length; i++)
	{
	  XSetWindowBackground (XtDisplay (oldmw),
				oldmw->menu.windows [i].window,
				newmw->core.background_pixel);
	  /* clear windows and generate expose events */
	  XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
		      0, 0, 0, 0, True);
	}
    }

  return redisplay;
}

static void 
Paul Reilly's avatar
Paul Reilly committed
1275 1276
XlwMenuResize (w)
     Widget w;
Richard M. Stallman's avatar
Richard M. Stallman committed
1277 1278 1279
{
  XlwMenuWidget mw = (XlwMenuWidget)w;

Paul Reilly's avatar
Paul Reilly committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
  if (mw->menu.popped_up)
    {
      /* Don't allow the popup menu to resize itself.  */
      mw->core.width = mw->menu.windows [0].width;
      mw->core.height = mw->menu.windows [0].height;
      mw->core.parent->core.width = mw->core.width ;
      mw->core.parent->core.height = mw->core.height ;
    }
  else
    {
      mw->menu.windows [0].width = mw->core.width;
      mw->menu.windows [0].height = mw->core.height;
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
1293 1294 1295 1296
}

/* Action procedures */
static void
Paul Reilly's avatar
Paul Reilly committed
1297 1298 1299
handle_single_motion_event (mw, ev)
     XlwMenuWidget mw;
     XMotionEvent* ev;
Richard M. Stallman's avatar
Richard M. Stallman committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
{
  widget_value*	val;
  int 		level;

  if (!map_event_to_widget_value (mw, ev, &val, &level))
    pop_new_stack_if_no_contents (mw);
  else
    set_new_state (mw, val, level);
  remap_menubar (mw);
  
  /* Sync with the display.  Makes it feel better on X terms. */
  XSync (XtDisplay (mw), False);
}

static void
Paul Reilly's avatar
Paul Reilly committed
1315 1316 1317
handle_motion_event (mw, ev)
     XlwMenuWidget mw;
     XMotionEvent* ev;
Richard M. Stallman's avatar
Richard M. Stallman committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
{
  int x = ev->x_root;
  int y = ev->y_root;
  int state = ev->state;

  handle_single_motion_event (mw, ev);

  /* allow motion events to be generated again */
  if (ev->is_hint
      && XQueryPointer (XtDisplay (mw), ev->window,
			&ev->root, &ev->subwindow,
			&ev->x_root, &ev->y_root,
			&ev->x, &ev->y,
			&ev->state)
      && ev->state == state
      && (ev->x_root != x || ev->y_root != y))
    handle_single_motion_event (mw, ev);
}

static void 
Paul Reilly's avatar
Paul Reilly committed
1338 1339 1340 1341 1342
Start (w, ev, params, num_params)
     Widget w;
     XEvent *ev;
     String *params;
     Cardinal *num_params;
Richard M. Stallman's avatar
Richard M. Stallman committed
1343 1344 1345
{
  XlwMenuWidget mw = (XlwMenuWidget)w;

Paul Reilly's avatar
Paul Reilly committed
1346 1347 1348
  if (!mw->menu.popped_up)
    {
      menu_post_event = *ev;
Paul Reilly's avatar
Paul Reilly committed
1349
      pop_up_menu (mw, ev);
Paul Reilly's avatar
Paul Reilly committed
1350 1351
    }
  else
Paul Reilly's avatar
Paul Reilly committed
1352 1353 1354 1355
    {
      /* If we push a button while the menu is posted semipermanently,
	 releasing the button should always pop the menu down.  */
      next_release_must_exit = 1;
Paul Reilly's avatar
Paul Reilly committed
1356

Paul Reilly's avatar
Paul Reilly committed
1357 1358 1359
      /* notes the absolute position of the menubar window */
      mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
      mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
Richard M. Stallman's avatar
Richard M. Stallman committed
1360

Paul Reilly's avatar
Paul Reilly committed
1361 1362 1363
      /* handles the down like a move, slots are compatible */
      handle_motion_event (mw, &ev->xmotion);
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
1364 1365 1366
}

static void 
Paul Reilly's avatar
Paul Reilly committed
1367 1368 1369 1370 1371
Drag (w, ev, params, num_params)
     Widget w;
     XEvent *ev;
     String *params;
     Cardinal *num_params;
Richard M. Stallman's avatar
Richard M. Stallman committed
1372 1373
{
  XlwMenuWidget mw = (XlwMenuWidget)w;
1374 1375
  if (mw->menu.popped_up)
    handle_motion_event (mw, &ev->xmotion);
Richard M. Stallman's avatar
Richard M. Stallman committed
1376 1377 1378
}

static void 
Paul Reilly's avatar
Paul Reilly committed
1379 1380 1381 1382 1383
Select (w, ev, params, num_params)
     Widget w;
     XEvent *ev;
     String *params;
     Cardinal *num_params;
Richard M. Stallman's avatar
Richard M. Stallman committed
1384 1385 1386 1387
{
  XlwMenuWidget mw = (XlwMenuWidget)w;
  widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
  
Paul Reilly's avatar
Paul Reilly committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
  /* If user releases the button quickly, without selecting anything,
     after the initial down-click that brought the menu up,
     do nothing.  */
  if ((selected_item == 0
       || ((widget_value *) selected_item)->call_data == 0)
      && !next_release_must_exit
      && (ev->xbutton.time - menu_post_event.xbutton.time
	  < XtGetMultiClickTime (XtDisplay (w))))
    return;

  /* pop down everything.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
1399 1400 1401 1402 1403 1404 1405
  mw->menu.new_depth = 1;
  remap_menubar (mw);

  if (mw->menu.popped_up)
    {
      mw->menu.popped_up = False;
      XtUngrabPointer ((Widget)mw, ev->xmotion.time);
Paul Reilly's avatar
Paul Reilly committed
1406 1407
      if (XtIsShell (XtParent ((Widget) mw)))
	XtPopdown (XtParent ((Widget) mw));
Paul Reilly's avatar
Paul Reilly committed
1408 1409 1410 1411 1412
      else
	{
	  XtRemoveGrab ((Widget) mw);
	  display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
	}
Richard M. Stallman's avatar
Richard M. Stallman committed
1413 1414 1415 1416 1417 1418 1419 1420 1421
    }

  /* callback */
  XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
}


/* Special code to pop-up a menu */
void
Paul Reilly's avatar
Paul Reilly committed
1422 1423 1424
pop_up_menu (mw, event)
     XlwMenuWidget mw;
     XButtonPressedEvent* event;
Richard M. Stallman's avatar
Richard M. Stallman committed
1425 1426 1427 1428 1429 1430 1431
{
  int		x = event->x_root;
  int		y = event->y_root;
  int		w;
  int		h;
  int		borderwidth = mw->menu.shadow_thickness;
  Screen*	screen = XtScreen (mw);
1432
  Display       *display = XtDisplay (mw);
Richard M. Stallman's avatar
Richard M. Stallman committed
1433

Paul Reilly's avatar
Paul Reilly committed
1434 1435
  next_release_must_exit = 0;

Richard M. Stallman's avatar
Richard M. Stallman committed
1436 1437
  XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);

Paul Reilly's avatar
Paul Reilly committed
1438
  if (XtIsShell (XtParent ((Widget)mw)))
Paul Reilly's avatar
Paul Reilly committed
1439
    size_menu (mw, 0);
Richard M. Stallman's avatar
Richard M. Stallman committed
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455

  w = mw->menu.windows [0].width;
  h = mw->menu.windows [0].height;

  x -= borderwidth;
  y -= borderwidth;
  if (x < borderwidth)
    x = borderwidth;
  if (x + w + 2 * borderwidth > WidthOfScreen (screen))
    x = WidthOfScreen (screen) - w - 2 * borderwidth;
  if (y < borderwidth)
    y = borderwidth;
  if (y + h + 2 * borderwidth> HeightOfScreen (screen))
    y = HeightOfScreen (screen) - h - 2 * borderwidth;

  mw->menu.popped_up = True;
Paul Reilly's avatar
Paul Reilly committed
1456
  if (XtIsShell (XtParent ((Widget)mw)))
Paul Reilly's avatar
Paul Reilly committed
1457
    {
Paul Reilly's avatar
Paul Reilly committed
1458 1459 1460
      XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
			 XtParent ((Widget)mw)->core.border_width);
      XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
Paul Reilly's avatar
Paul Reilly committed
1461 1462 1463 1464 1465 1466 1467 1468
      display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
      mw->menu.windows [0].x = x + borderwidth;
      mw->menu.windows [0].y = y + borderwidth;
    }
  else
    {
      XEvent *ev = (XEvent *) event;

1469
      XtAddGrab ((Widget) mw, True, True);
Paul Reilly's avatar
Paul Reilly committed
1470 1471 1472 1473 1474 1475

      /* notes the absolute position of the menubar window */
      mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
      mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
    }

Paul Reilly's avatar
Paul Reilly committed
1476
#ifdef emacs
1477
  x_catch_errors (display);
Paul Reilly's avatar
Paul Reilly committed
1478
#endif
1479 1480 1481 1482 1483 1484 1485 1486 1487
  XtGrabPointer ((Widget)mw, False,
		 (PointerMotionMask
		  | PointerMotionHintMask
		  | ButtonReleaseMask
		  | ButtonPressMask),
		 GrabModeAsync, GrabModeAsync, None,
		 mw->menu.cursor_shape,
		 event->time);
  pointer_grabbed = 1;
Paul Reilly's avatar
Paul Reilly committed
1488
#ifdef emacs
1489
  if (x_had_errors_p (display))
Paul Reilly's avatar
Paul Reilly committed
1490 1491 1492 1493
    {
      pointer_grabbed = 0;
      XtUngrabPointer ((Widget)mw, event->time);
    }
1494
  x_uncatch_errors (display);
Paul Reilly's avatar
Paul Reilly committed
1495
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
1496 1497 1498

  handle_motion_event (mw, (XMotionEvent*)event);
}