+ switch (GetEventKind (event))
+ {
+ case kEventToolbarGetDefaultIdentifiers:
+ result = noErr;
+ break;
+
+ case kEventToolbarGetAllowedIdentifiers:
+ {
+ CFMutableArrayRef array;
+
+ GetEventParameter (event, kEventParamMutableArray,
+ typeCFMutableArrayRef, NULL,
+ sizeof (CFMutableArrayRef), NULL, &array);
+ CFArrayAppendValue (array, TOOLBAR_ICON_ITEM_IDENTIFIER);
+ result = noErr;
+ }
+ break;
+
+ case kEventToolbarCreateItemWithIdentifier:
+ {
+ CFStringRef identifier;
+ HIToolbarItemRef item = NULL;
+
+ GetEventParameter (event, kEventParamToolbarItemIdentifier,
+ typeCFStringRef, NULL,
+ sizeof (CFStringRef), NULL, &identifier);
+
+ if (CFStringCompare (identifier, TOOLBAR_ICON_ITEM_IDENTIFIER, 0)
+ == kCFCompareEqualTo)
+ HIToolbarItemCreate (identifier,
+ kHIToolbarItemAllowDuplicates
+ | kHIToolbarItemCantBeRemoved, &item);
+
+ if (item)
+ {
+ SetEventParameter (event, kEventParamToolbarItem,
+ typeHIToolbarItemRef,
+ sizeof (HIToolbarItemRef), &item);
+ result = noErr;
+ }
+ }
+ break;
+
+ default:
+ abort ();
+ }
+
+ return result;
+}
+
+static CGImageRef
+mac_image_spec_to_cg_image (f, image)
+ struct frame *f;
+ Lisp_Object image;
+{
+ if (!valid_image_p (image))
+ return NULL;
+ else
+ {
+ int img_id = lookup_image (f, image);
+ struct image *img = IMAGE_FROM_ID (f, img_id);
+
+ prepare_image_for_display (f, img);
+
+ return img->data.ptr_val;
+ }
+}
+
+/* Create a tool bar for frame F. */
+
+static OSStatus
+mac_create_frame_tool_bar (f)
+ FRAME_PTR f;
+{
+ OSStatus err;
+ HIToolbarRef toolbar;
+
+ err = HIToolbarCreate (TOOLBAR_IDENTIFIER, kHIToolbarNoAttributes,
+ &toolbar);
+ if (err == noErr)
+ {
+ static const EventTypeSpec specs[] =
+ {{kEventClassToolbar, kEventToolbarGetDefaultIdentifiers},
+ {kEventClassToolbar, kEventToolbarGetAllowedIdentifiers},
+ {kEventClassToolbar, kEventToolbarCreateItemWithIdentifier}};
+
+ err = InstallEventHandler (HIObjectGetEventTarget (toolbar),
+ mac_handle_toolbar_event,
+ GetEventTypeCount (specs), specs,
+ f, NULL);
+ }
+
+ if (err == noErr)
+ err = HIToolbarSetDisplayMode (toolbar, kHIToolbarDisplayModeIconOnly);
+ if (err == noErr)
+ {
+ static const EventTypeSpec specs[] =
+ {{kEventClassCommand, kEventCommandProcess}};
+
+ err = InstallWindowEventHandler (FRAME_MAC_WINDOW (f),
+ mac_handle_toolbar_command_event,
+ GetEventTypeCount (specs),
+ specs, f, NULL);
+ }
+ if (err == noErr)
+ err = SetWindowToolbar (FRAME_MAC_WINDOW (f), toolbar);
+
+ if (toolbar)
+ CFRelease (toolbar);
+
+ return err;
+}
+
+/* Update the tool bar for frame F. Add new buttons and remove old. */
+
+void
+update_frame_tool_bar (f)
+ FRAME_PTR f;
+{
+ HIToolbarRef toolbar = NULL;
+ short left, top;
+ CFArrayRef old_items = NULL;
+ CFIndex old_count;
+ int i, pos, win_gravity = f->output_data.mac->toolbar_win_gravity;
+ struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
+
+ BLOCK_INPUT;
+
+ GetWindowToolbar (FRAME_MAC_WINDOW (f), &toolbar);
+ if (toolbar == NULL)
+ {
+ mac_create_frame_tool_bar (f);
+ GetWindowToolbar (FRAME_MAC_WINDOW (f), &toolbar);
+ if (toolbar == NULL)
+ goto out;
+ if (win_gravity >= NorthWestGravity && win_gravity <= SouthEastGravity)
+ mac_get_window_origin_with_gravity (f, win_gravity, &left, &top);
+ }
+
+ HIToolbarCopyItems (toolbar, &old_items);
+ if (old_items == NULL)
+ goto out;
+
+ old_count = CFArrayGetCount (old_items);
+ pos = 0;
+ for (i = 0; i < f->n_tool_bar_items; ++i)
+ {
+#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
+
+ int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
+ int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
+ int idx;
+ Lisp_Object image;
+ CGImageRef cg_image;
+ CFStringRef label;
+ HIToolbarItemRef item;
+
+ /* If image is a vector, choose the image according to the
+ button state. */
+ image = PROP (TOOL_BAR_ITEM_IMAGES);
+ if (VECTORP (image))
+ {
+ if (enabled_p)
+ idx = (selected_p
+ ? TOOL_BAR_IMAGE_ENABLED_SELECTED
+ : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
+ else
+ idx = (selected_p
+ ? TOOL_BAR_IMAGE_DISABLED_SELECTED
+ : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
+
+ xassert (ASIZE (image) >= idx);
+ image = AREF (image, idx);
+ }
+ else
+ idx = -1;
+
+ cg_image = mac_image_spec_to_cg_image (f, image);
+ /* Ignore invalid image specifications. */
+ if (cg_image == NULL)
+ continue;
+
+ label = cfstring_create_with_string (PROP (TOOL_BAR_ITEM_CAPTION));
+ if (label == NULL)
+ label = CFSTR ("");
+
+ if (pos < old_count)
+ {
+ CGImageRef old_cg_image = NULL;
+ CFStringRef old_label = NULL;
+ Boolean old_enabled_p;
+
+ item = (HIToolbarItemRef) CFArrayGetValueAtIndex (old_items, pos);
+
+ HIToolbarItemCopyImage (item, &old_cg_image);
+ if (cg_image != old_cg_image)
+ HIToolbarItemSetImage (item, cg_image);
+ CGImageRelease (old_cg_image);
+
+ HIToolbarItemCopyLabel (item, &old_label);
+ if (CFStringCompare (label, old_label, 0) != kCFCompareEqualTo)
+ HIToolbarItemSetLabel (item, label);
+ CFRelease (old_label);
+
+ old_enabled_p = HIToolbarItemIsEnabled (item);
+ if ((enabled_p || idx >= 0) != old_enabled_p)
+ HIToolbarItemSetEnabled (item, (enabled_p || idx >= 0));
+ }
+ else
+ {
+ item = NULL;
+ HIToolbarCreateItemWithIdentifier (toolbar,
+ TOOLBAR_ICON_ITEM_IDENTIFIER,
+ NULL, &item);
+ if (item)
+ {
+ HIToolbarItemSetImage (item, cg_image);
+ HIToolbarItemSetLabel (item, label);
+ HIToolbarItemSetEnabled (item, (enabled_p || idx >= 0));
+ HIToolbarAppendItem (toolbar, item);
+ CFRelease (item);
+ }
+ }
+
+ CFRelease (label);
+ if (item)
+ {
+ HIToolbarItemSetCommandID (item, TOOLBAR_ITEM_MAKE_COMMAND_ID (i));
+ pos++;
+ }
+ }
+
+ CFRelease (old_items);
+
+ while (pos < old_count)
+ HIToolbarRemoveItemAtIndex (toolbar, --old_count);
+
+ ShowHideWindowToolbar (FRAME_MAC_WINDOW (f), true,
+ !win_gravity && f == mac_focus_frame (dpyinfo));
+ /* Mac OS X 10.3 does not issue kEventWindowBoundsChanged events on
+ toolbar visibility change. */
+ mac_handle_origin_change (f);
+ if (win_gravity >= NorthWestGravity && win_gravity <= SouthEastGravity)
+ {
+ mac_move_window_with_gravity (f, win_gravity, left, top);
+ /* If the title bar is completely outside the screen, adjust the
+ position. */
+ ConstrainWindowToScreen (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn,
+ kWindowConstrainMoveRegardlessOfFit
+ | kWindowConstrainAllowPartial, NULL, NULL);
+ f->output_data.mac->toolbar_win_gravity = 0;
+ }
+
+ out:
+ UNBLOCK_INPUT;
+}
+
+/* Hide the tool bar on frame F. Unlike the counterpart on GTK+, it
+ doesn't deallocate the resources. */
+
+void
+free_frame_tool_bar (f)
+ FRAME_PTR f;
+{
+ if (IsWindowToolbarVisible (FRAME_MAC_WINDOW (f)))
+ {
+ struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
+
+ BLOCK_INPUT;
+ ShowHideWindowToolbar (FRAME_MAC_WINDOW (f), false,
+ (NILP (Fsymbol_value
+ (intern ("frame-notice-user-settings")))
+ && f == mac_focus_frame (dpyinfo)));
+ /* Mac OS X 10.3 does not issue kEventWindowBoundsChanged events
+ on toolbar visibility change. */
+ mac_handle_origin_change (f);
+ UNBLOCK_INPUT;
+ }
+}
+
+static void
+mac_tool_bar_note_mouse_movement (f, event)
+ struct frame *f;
+ EventRef event;
+{
+ OSStatus err;
+ struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
+ int mouse_down_p;
+ HIViewRef item_view;
+ UInt32 command_id;
+
+ mouse_down_p = (dpyinfo->grabbed
+ && f == last_mouse_frame
+ && FRAME_LIVE_P (f));
+ if (mouse_down_p)
+ return;
+
+ err = HIViewGetViewForMouseEvent (HIViewGetRoot (FRAME_MAC_WINDOW (f)),
+ event, &item_view);
+ /* This doesn't work on Mac OS X 10.2. On Mac OS X 10.3 and 10.4, a
+ toolbar item view seems to have the same command ID with that of
+ the toolbar item. */
+ if (err == noErr)
+ err = GetControlCommandID (item_view, &command_id);
+ if (err == noErr && TOOLBAR_ITEM_COMMAND_ID_P (command_id))
+ {
+ int i = TOOLBAR_ITEM_COMMAND_ID_VALUE (command_id);
+
+ if (i < f->n_tool_bar_items)
+ {
+ HIRect bounds;
+ HIViewRef content_view;
+
+ err = HIViewGetBounds (item_view, &bounds);
+ if (err == noErr)
+ err = HIViewFindByID (HIViewGetRoot (FRAME_MAC_WINDOW (f)),
+ kHIViewWindowContentID, &content_view);
+ if (err == noErr)
+ err = HIViewConvertRect (&bounds, item_view, content_view);
+ if (err == noErr)
+ SetRect (&last_mouse_glyph,
+ CGRectGetMinX (bounds), CGRectGetMinY (bounds),
+ CGRectGetMaxX (bounds), CGRectGetMaxY (bounds));
+
+ help_echo_object = help_echo_window = Qnil;
+ help_echo_pos = -1;
+ help_echo_string = PROP (TOOL_BAR_ITEM_HELP);
+ if (NILP (help_echo_string))
+ help_echo_string = PROP (TOOL_BAR_ITEM_CAPTION);
+ }
+ }
+}
+
+static OSStatus
+mac_handle_toolbar_command_event (next_handler, event, data)
+ EventHandlerCallRef next_handler;
+ EventRef event;
+ void *data;
+{
+ OSStatus err, result = eventNotHandledErr;
+ struct frame *f = (struct frame *) data;
+ HICommand command;
+
+ err = GetEventParameter (event, kEventParamDirectObject,
+ typeHICommand, NULL,
+ sizeof (HICommand), NULL, &command);
+ if (err != noErr)
+ return result;
+
+ switch (GetEventKind (event))
+ {
+ case kEventCommandProcess:
+ if (!TOOLBAR_ITEM_COMMAND_ID_P (command.commandID))
+ result = CallNextEventHandler (next_handler, event);
+ else
+ {
+ int i = TOOLBAR_ITEM_COMMAND_ID_VALUE (command.commandID);
+
+ if (i < f->n_tool_bar_items
+ && !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P)))
+ {
+ Lisp_Object frame;
+ struct input_event buf;
+
+ EVENT_INIT (buf);
+
+ XSETFRAME (frame, f);
+ buf.kind = TOOL_BAR_EVENT;
+ buf.frame_or_window = frame;
+ buf.arg = frame;
+ kbd_buffer_store_event (&buf);
+
+ buf.kind = TOOL_BAR_EVENT;
+ buf.frame_or_window = frame;
+ buf.arg = PROP (TOOL_BAR_ITEM_KEY);
+ buf.modifiers = mac_event_to_emacs_modifiers (event);
+ kbd_buffer_store_event (&buf);
+
+ result = noErr;
+ }
+ }
+ break;
+
+ default:
+ abort ();
+ }
+#undef PROP
+
+ return result;
+}
+#endif /* USE_MAC_TOOLBAR */
+
+\f
+/***********************************************************************
+ Text Cursor
+ ***********************************************************************/
+
+/* Set clipping for output in glyph row ROW. W is the window in which
+ we operate. GC is the graphics context to set clipping in.
+
+ ROW may be a text row or, e.g., a mode line. Text rows must be
+ clipped to the interior of the window dedicated to text display,
+ mode lines must be clipped to the whole window. */
+
+static void
+x_clip_to_row (w, row, area, gc)
+ struct window *w;
+ struct glyph_row *row;
+ int area;
+ GC gc;
+{
+ struct frame *f = XFRAME (WINDOW_FRAME (w));
+ Rect clip_rect;
+ int window_x, window_y, window_width;
+
+ window_box (w, area, &window_x, &window_y, &window_width, 0);
+
+ clip_rect.left = window_x;
+ clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
+ clip_rect.top = max (clip_rect.top, window_y);
+ clip_rect.right = clip_rect.left + window_width;
+ clip_rect.bottom = clip_rect.top + row->visible_height;
+
+ mac_set_clip_rectangles (FRAME_MAC_DISPLAY (f), gc, &clip_rect, 1);
+}
+
+
+/* Draw a hollow box cursor on window W in glyph row ROW. */
+
+static void
+x_draw_hollow_cursor (w, row)
+ struct window *w;
+ struct glyph_row *row;
+{
+ struct frame *f = XFRAME (WINDOW_FRAME (w));
+ struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
+ Display *dpy = FRAME_MAC_DISPLAY (f);
+ int x, y, wd, h;
+ XGCValues xgcv;
+ struct glyph *cursor_glyph;
+ GC gc;
+
+ /* Get the glyph the cursor is on. If we can't tell because
+ the current matrix is invalid or such, give up. */
+ cursor_glyph = get_phys_cursor_glyph (w);
+ if (cursor_glyph == NULL)
+ return;
+
+ /* Compute frame-relative coordinates for phys cursor. */
+ get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
+ wd = w->phys_cursor_width;
+
+ /* The foreground of cursor_gc is typically the same as the normal
+ background color, which can cause the cursor box to be invisible. */
+ xgcv.foreground = f->output_data.mac->cursor_pixel;
+ if (dpyinfo->scratch_cursor_gc)
+ XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
+ else
+ dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_MAC_WINDOW (f),
+ GCForeground, &xgcv);
+ gc = dpyinfo->scratch_cursor_gc;
+
+ /* Set clipping, draw the rectangle, and reset clipping again. */
+ x_clip_to_row (w, row, TEXT_AREA, gc);
+ mac_draw_rectangle (f, gc, x, y, wd, h - 1);
+ mac_reset_clip_rectangles (dpy, gc);
+}
+
+
+/* Draw a bar cursor on window W in glyph row ROW.
+
+ Implementation note: One would like to draw a bar cursor with an
+ angle equal to the one given by the font property XA_ITALIC_ANGLE.
+ Unfortunately, I didn't find a font yet that has this property set.
+ --gerd. */
+
+static void
+x_draw_bar_cursor (w, row, width, kind)
+ struct window *w;
+ struct glyph_row *row;
+ int width;
+ enum text_cursor_kinds kind;
+{
+ struct frame *f = XFRAME (w->frame);
+ struct glyph *cursor_glyph;
+
+ /* If cursor is out of bounds, don't draw garbage. This can happen
+ in mini-buffer windows when switching between echo area glyphs
+ and mini-buffer. */
+ cursor_glyph = get_phys_cursor_glyph (w);
+ if (cursor_glyph == NULL)
+ return;
+
+ /* If on an image, draw like a normal cursor. That's usually better
+ visible than drawing a bar, esp. if the image is large so that
+ the bar might not be in the window. */
+ if (cursor_glyph->type == IMAGE_GLYPH)
+ {
+ struct glyph_row *row;
+ row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
+ draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
+ }
+ else
+ {
+ Display *dpy = FRAME_MAC_DISPLAY (f);
+ Window window = FRAME_MAC_WINDOW (f);
+ GC gc = FRAME_MAC_DISPLAY_INFO (f)->scratch_cursor_gc;
+ unsigned long mask = GCForeground | GCBackground;
+ struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
+ XGCValues xgcv;
+
+ /* If the glyph's background equals the color we normally draw
+ the bar cursor in, the bar cursor in its normal color is
+ invisible. Use the glyph's foreground color instead in this
+ case, on the assumption that the glyph's colors are chosen so
+ that the glyph is legible. */
+ if (face->background == f->output_data.mac->cursor_pixel)
+ xgcv.background = xgcv.foreground = face->foreground;
+ else
+ xgcv.background = xgcv.foreground = f->output_data.mac->cursor_pixel;
+
+ if (gc)
+ XChangeGC (dpy, gc, mask, &xgcv);
+ else
+ {
+ gc = XCreateGC (dpy, window, mask, &xgcv);
+ FRAME_MAC_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
+ }
+
+ if (width < 0)
+ width = FRAME_CURSOR_WIDTH (f);
+ width = min (cursor_glyph->pixel_width, width);
+
+ w->phys_cursor_width = width;
+ x_clip_to_row (w, row, TEXT_AREA, gc);
+
+ if (kind == BAR_CURSOR)
+ mac_fill_rectangle (f, gc,
+ WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
+ WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
+ width, row->height);
+ else
+ mac_fill_rectangle (f, gc,
+ WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
+ WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
+ row->height - width),
+ cursor_glyph->pixel_width,
+ width);
+
+ mac_reset_clip_rectangles (dpy, gc);
+ }
+}
+
+
+/* RIF: Define cursor CURSOR on frame F. */
+
+static void
+mac_define_frame_cursor (f, cursor)
+ struct frame *f;
+ Cursor cursor;
+{
+ struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
+
+ if (dpyinfo->x_focus_frame == f)
+ SetThemeCursor (cursor);
+}