File: gwwvmenubar.c

package info (click to toggle)
fontforge 1:20170731~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 67,252 kB
  • ctags: 42,582
  • sloc: ansic: 580,893; python: 5,476; sh: 3,081; makefile: 1,269; perl: 315; cpp: 176; ruby: 97; objc: 92; xml: 90; sed: 9
file content (257 lines) | stat: -rw-r--r-- 8,261 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <gtk/gtk.h>
#include "gwwvmenubar.h"

#define BORDER_SPACING	0		/* Must match value in gtkmenubar.c */

/* This implements a "variable-sized" menubar */
/* If the window shrinks below the size needed to display all the components   */
/*  then it will display as many components as it can, and then add a final    */
/*  arrow which will be attached to a pulldown menu containing any undisplayed */
/*  components. */

static GtkWidgetClass       *parent_class = NULL;

static GtkShadowType get_shadow_type (GtkMenuBar *menubar) {
  GtkShadowType shadow_type = GTK_SHADOW_OUT;
  
  gtk_widget_style_get (GTK_WIDGET (menubar),
			"shadow_type", &shadow_type,
			NULL);

  return shadow_type;
}

static void gww_vmenu_bar_size_request (GtkWidget      *widget,
				       GtkRequisition *requisition) {
    GwwVMenuBar  *menu_bar;
    GtkMenuShell *menu_shell;
    GtkRequisition child_requisition;
    gint ipadding;
    GtkWidget *child;
    GList *children;

    g_return_if_fail (GWW_IS_VMENU_BAR (widget));
    g_return_if_fail (requisition != NULL);

    requisition->width = 0;
    requisition->height = 0;
  
    if (GTK_WIDGET_VISIBLE (widget)) {
	menu_bar = GWW_VMENU_BAR (widget);
	menu_shell = GTK_MENU_SHELL (widget);

	gtk_widget_size_request (menu_bar->extra_arrow, requisition);

	for ( children = menu_bar->children; children!=NULL; children=children->next ) {
	    child = children->data;

	    if (GTK_WIDGET_VISIBLE (child)) {
		gtk_widget_size_request (child, &child_requisition);
		requisition->height = MAX (requisition->height, child_requisition.height);
	    }
	}

	gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
      
	requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
				ipadding + 
				BORDER_SPACING) * 2;
	requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
				ipadding +
				BORDER_SPACING) * 2;

	if (get_shadow_type ( GTK_MENU_BAR(menu_bar)) != GTK_SHADOW_NONE) {
	    requisition->width += widget->style->xthickness * 2;
	    requisition->height += widget->style->ythickness * 2;
	}
    }
}

static void gww_vmenu_bar_size_allocate (GtkWidget     *widget,
					GtkAllocation *allocation) {
    GwwVMenuBar *vmenu_bar;
    GtkRequisition arrow_requisition, child_requisition;
    GList *children;
    gint i, goodi, extra_active;
    gint width, total_width;
    gint ipadding;

    g_return_if_fail (GWW_IS_VMENU_BAR (widget));
    g_return_if_fail (allocation != NULL);

    vmenu_bar = GWW_VMENU_BAR (widget);

    /* First figure out what widgets we want to display under the new */
    /*  allocation scheme */
    gtk_widget_size_request (vmenu_bar->extra_arrow, &arrow_requisition);

    total_width = allocation->width;
    gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
    total_width -= (GTK_CONTAINER (vmenu_bar)->border_width +
				ipadding + 
				BORDER_SPACING) * 2; 
    if (get_shadow_type ( GTK_MENU_BAR(vmenu_bar)) != GTK_SHADOW_NONE)
	total_width -= widget->style->xthickness * 2;

    goodi = -1;
    width = 0;
    for ( i=0, children = vmenu_bar->children; children!=NULL ;
	    ++i, children=children->next ) {
	if (!GTK_WIDGET_VISIBLE (children->data))
    continue;
	if ( width+arrow_requisition.width <= total_width ||
		( children->next==NULL && width <= total_width ))
	    goodi = i-1;
	gtk_widget_size_request (children->data, &child_requisition);
	if ( width + child_requisition.width > total_width )
    break;
	width += child_requisition.width + 6;	/* Why do I need a fudge factor of 6? I'm doing something wrong */
    }
    if ( children==NULL ) ++goodi;
    extra_active = ( children!=NULL );
    if ( vmenu_bar->child_last==goodi &&
	    vmenu_bar->extra_active == extra_active ) {
	if ( widget->allocation.x == allocation->x &&
		widget->allocation.y == allocation->y &&
		widget->allocation.width == allocation->width &&
		widget->allocation.height == allocation->height )
return;
    } else {
	/* Then undo any previous allocation */
	for ( i=0, children = vmenu_bar->children; children!=NULL && i<=vmenu_bar->child_last;
		++i, children=children->next ) {
	    ((GtkContainerClass*) parent_class)->remove( GTK_CONTAINER(vmenu_bar),
			children->data );
	}

	if ( vmenu_bar->extra_active ) {
	    ((GtkContainerClass*) parent_class)->remove( GTK_CONTAINER(vmenu_bar),
			vmenu_bar->extra_arrow );
	    for ( ; children!=NULL ; children=children->next ) {
		gtk_container_remove( GTK_CONTAINER(vmenu_bar->extra_menu),
			    children->data );
	    }
	}

	/* Then configure the new scheme */
	vmenu_bar->extra_active = extra_active;
	vmenu_bar->child_last = goodi;

	for ( i=0, children = vmenu_bar->children; children!=NULL && i<=goodi;
		++i, children=children->next ) {
	    ((GtkContainerClass*) parent_class)->add( GTK_CONTAINER(vmenu_bar),
			children->data );
	}

	if ( vmenu_bar->extra_active ) {
	    ((GtkContainerClass*) parent_class)->add( GTK_CONTAINER(vmenu_bar),
			vmenu_bar->extra_arrow );
	    for ( ; children!=NULL ; children=children->next ) {
		gtk_container_add( GTK_CONTAINER(vmenu_bar->extra_menu),
			    children->data );
	    }
	}
    }

    /* Finally ask the underlying menubar to do whatever it needs to do */
    /*  with the widget set which we chose to be active */
    ((GtkWidgetClass*) parent_class)->size_allocate( GTK_WIDGET(vmenu_bar),
		allocation );
}

static void gww_vmenu_bar_add(  GtkContainer *widget,
				GtkWidget    *child) {
    GwwVMenuBar *vmenu_bar;

    g_return_if_fail (GWW_IS_VMENU_BAR (widget));
    vmenu_bar = GWW_VMENU_BAR (widget);

    vmenu_bar->children = g_list_insert (vmenu_bar->children, child, -1);	/* Add to end */
    g_object_ref( G_OBJECT(child));

    /*gtk_widget_set_parent (child, GTK_WIDGET (vmenu_bar));*/
    /* Don't set the parent until we actually display it */

    /* BUG!!! I am presuming that any adds/removes will be done before */
    /*  the widget is actually displayed */
}

static void gww_vmenu_bar_remove( GtkContainer *widget,
				  GtkWidget    *child) {
    GwwVMenuBar *vmenu_bar;

    g_return_if_fail (GWW_IS_VMENU_BAR (widget));
    if ( child->parent!=NULL )
	((GtkContainerClass*) parent_class)->remove( GTK_CONTAINER(child->parent),
		    child );
    vmenu_bar = GWW_VMENU_BAR (widget);

    vmenu_bar->children = g_list_remove (vmenu_bar->children, child);
    /* BUG!!! I am presuming that any adds/removes will be done before */
    /*  the widget is actually displayed */
    g_object_unref( G_OBJECT(child));
}


static void gww_vmenu_bar_class_init (GwwVMenuBarClass *class) {
    GtkWidgetClass    *widget_class;
    GtkContainerClass *container_class;

    parent_class = g_type_class_peek_parent (class);
  
    widget_class    = (GtkWidgetClass*) class;
    container_class = (GtkContainerClass*) class;

    widget_class->size_request = gww_vmenu_bar_size_request;
    widget_class->size_allocate = gww_vmenu_bar_size_allocate;

    container_class->add    = gww_vmenu_bar_add;
    container_class->remove = gww_vmenu_bar_remove;
}

static void gww_vmenu_bar_init (GwwVMenuBar *mbar) {
    GtkWidget *arrow;

    arrow = gtk_arrow_new(GTK_ARROW_DOWN,GTK_SHADOW_OUT);
    gtk_widget_show( arrow );
    mbar->extra_arrow = gtk_menu_item_new();
    gtk_widget_show( mbar->extra_arrow );
    gtk_container_add ( GTK_CONTAINER(mbar->extra_arrow), arrow);
    mbar->extra_menu = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (mbar->extra_arrow), mbar->extra_menu);
    g_object_ref( G_OBJECT( mbar->extra_arrow ));

    mbar->children     = NULL;
    mbar->child_last   = -1;
    mbar->extra_active = FALSE;
}

GType gww_vmenu_bar_get_type (void) {
    static GType vmenu_bar_type = 0;

  if (!vmenu_bar_type)
    {
      static const GTypeInfo vmenu_bar_info =
      {
	sizeof (GtkMenuBarClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gww_vmenu_bar_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GwwVMenuBar),
	0,		/* n_preallocs */
	(GtkObjectInitFunc) gww_vmenu_bar_init,
      };

      vmenu_bar_type = g_type_register_static (GTK_TYPE_MENU_BAR, "GwwVMenuBar",
					      &vmenu_bar_info, 0);
    }

  return vmenu_bar_type;
}

GtkWidget *gww_vmenu_bar_new (void)
{
  return g_object_new (GWW_TYPE_VMENU_BAR, NULL);
}