gtktearoffmenuitem.c 7.16 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This 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
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "config.h"
28

29 30
#include "gtkmenu.h"
#include "gtktearoffmenuitem.h"
31
#include "gtkintl.h"
32
#include "gtkalias.h"
33 34 35 36 37 38 39 40 41 42

#define ARROW_SIZE 10
#define TEAR_LENGTH 5
#define BORDER_SPACING  3

static void gtk_tearoff_menu_item_size_request (GtkWidget             *widget,
				                GtkRequisition        *requisition);
static gint gtk_tearoff_menu_item_expose     (GtkWidget             *widget,
					      GdkEventExpose        *event);
static void gtk_tearoff_menu_item_activate   (GtkMenuItem           *menu_item);
43 44
static void gtk_tearoff_menu_item_parent_set (GtkWidget             *widget,
					      GtkWidget             *previous);
45

Matthias Clasen's avatar
Matthias Clasen committed
46
G_DEFINE_TYPE (GtkTearoffMenuItem, gtk_tearoff_menu_item, GTK_TYPE_MENU_ITEM)
47 48 49 50

GtkWidget*
gtk_tearoff_menu_item_new (void)
{
Manish Singh's avatar
Manish Singh committed
51
  return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
52 53 54 55 56 57 58 59 60 61 62 63 64
}

static void
gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
{
  GtkWidgetClass *widget_class;
  GtkMenuItemClass *menu_item_class;

  widget_class = (GtkWidgetClass*) klass;
  menu_item_class = (GtkMenuItemClass*) klass;

  widget_class->expose_event = gtk_tearoff_menu_item_expose;
  widget_class->size_request = gtk_tearoff_menu_item_size_request;
65
  widget_class->parent_set = gtk_tearoff_menu_item_parent_set;
66 67 68 69 70 71 72

  menu_item_class->activate = gtk_tearoff_menu_item_activate;
}

static void
gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
{
73
  tearoff_menu_item->torn_off = FALSE;
74 75 76 77 78 79 80
}

static void
gtk_tearoff_menu_item_size_request (GtkWidget      *widget,
				    GtkRequisition *requisition)
{
  requisition->width = (GTK_CONTAINER (widget)->border_width +
81
			widget->style->xthickness +
82 83
			BORDER_SPACING) * 2;
  requisition->height = (GTK_CONTAINER (widget)->border_width +
84
			 widget->style->ythickness) * 2;
85

86
  if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
87 88 89 90 91
    {
      requisition->height += ARROW_SIZE;
    }
  else
    {
92
      requisition->height += widget->style->ythickness + 4;
93 94 95 96 97 98 99 100 101 102 103 104
    }
}

static void
gtk_tearoff_menu_item_paint (GtkWidget   *widget,
			     GdkRectangle *area)
{
  GtkMenuItem *menu_item;
  GtkShadowType shadow_type;
  gint width, height;
  gint x, y;
  gint right_max;
105 106 107
  GtkArrowType arrow_type;
  GtkTextDirection direction;
  
108 109 110 111
  if (GTK_WIDGET_DRAWABLE (widget))
    {
      menu_item = GTK_MENU_ITEM (widget);

112 113
      direction = gtk_widget_get_direction (widget);

114 115
      x = widget->allocation.x + GTK_CONTAINER (menu_item)->border_width;
      y = widget->allocation.y + GTK_CONTAINER (menu_item)->border_width;
116 117
      width = widget->allocation.width - GTK_CONTAINER (menu_item)->border_width * 2;
      height = widget->allocation.height - GTK_CONTAINER (menu_item)->border_width * 2;
118 119 120
      right_max = x + width;

      if (widget->state == GTK_STATE_PRELIGHT)
121 122 123 124
	{
	  gint selected_shadow_type;
	  
	  gtk_widget_style_get (widget,
125
				"selected-shadow-type", &selected_shadow_type,
126 127 128 129 130 131 132 133 134 135
				NULL);
	  gtk_paint_box (widget->style,
			 widget->window,
			 GTK_STATE_PRELIGHT,
			 selected_shadow_type,
			 area, widget, "menuitem",
			 x, y, width, height);
	}
      else
	gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
136

137
      if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
138 139 140 141 142 143 144 145 146 147
	{
	  gint arrow_x;

	  if (widget->state == GTK_STATE_PRELIGHT)
	    shadow_type = GTK_SHADOW_IN;
	  else
	    shadow_type = GTK_SHADOW_OUT;

	  if (menu_item->toggle_size > ARROW_SIZE)
	    {
148 149 150 151 152 153 154 155
	      if (direction == GTK_TEXT_DIR_LTR) {
		arrow_x = x + (menu_item->toggle_size - ARROW_SIZE)/2;
		arrow_type = GTK_ARROW_LEFT;
	      }
	      else {
		arrow_x = x + width - menu_item->toggle_size + (menu_item->toggle_size - ARROW_SIZE)/2; 
		arrow_type = GTK_ARROW_RIGHT;	    
	      }
156 157 158 159
	      x += menu_item->toggle_size + BORDER_SPACING;
	    }
	  else
	    {
160 161 162 163 164 165 166 167
	      if (direction == GTK_TEXT_DIR_LTR) {
		arrow_x = ARROW_SIZE / 2;
		arrow_type = GTK_ARROW_LEFT;
	      }
	      else {
		arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2; 
		arrow_type = GTK_ARROW_RIGHT;	    
	      }
168 169
	      x += 2 * ARROW_SIZE;
	    }
170 171


Manish Singh's avatar
Manish Singh committed
172 173 174
	  gtk_paint_arrow (widget->style, widget->window,
			   widget->state, shadow_type,
			   NULL, widget, "tearoffmenuitem",
175
			   arrow_type, FALSE,
Manish Singh's avatar
Manish Singh committed
176 177
			   arrow_x, y + height / 2 - 5, 
			   ARROW_SIZE, ARROW_SIZE);
178 179 180 181
	}

      while (x < right_max)
	{
182 183 184 185 186 187 188 189 190 191 192
	  gint x1, x2;

	  if (direction == GTK_TEXT_DIR_LTR) {
	    x1 = x;
	    x2 = MIN (x + TEAR_LENGTH, right_max);
	  }
	  else {
	    x1 = right_max - x;
	    x2 = MAX (right_max - x - TEAR_LENGTH, 0);
	  }
	  
Manish Singh's avatar
Manish Singh committed
193 194
	  gtk_paint_hline (widget->style, widget->window, GTK_STATE_NORMAL,
			   NULL, widget, "tearoffmenuitem",
195
			   x1, x2, y + (height - widget->style->ythickness) / 2);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	  x += 2 * TEAR_LENGTH;
	}
    }
}

static gint
gtk_tearoff_menu_item_expose (GtkWidget      *widget,
			    GdkEventExpose *event)
{
  gtk_tearoff_menu_item_paint (widget, &event->area);

  return FALSE;
}

static void
gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
{
  if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
    {
      GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
      
217
      gtk_widget_queue_resize (GTK_WIDGET (menu_item));
218 219
      gtk_menu_set_tearoff_state (GTK_MENU (GTK_WIDGET (menu_item)->parent),
				  !menu->torn_off);
220
    }
221 222 223 224
}

static void
tearoff_state_changed (GtkMenu            *menu,
225 226
		       GParamSpec         *pspec,
		       gpointer            data)
227
{
228 229
  GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (data);

230 231
  tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
}
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static void
gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
				  GtkWidget *previous)
{
  GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (widget);
  GtkMenu *menu = GTK_IS_MENU (widget->parent) ? GTK_MENU (widget->parent) : NULL;

  if (previous)
    g_signal_handlers_disconnect_by_func (previous, 
					  tearoff_state_changed, 
					  tearoff_menu_item);
  
  if (menu)
    {
      tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
      g_signal_connect (menu, "notify::tearoff-state", 
			G_CALLBACK (tearoff_state_changed), 
			tearoff_menu_item);
    }  
}
253 254 255

#define __GTK_TEAROFF_MENU_ITEM_C__
#include "gtkaliasdef.c"