summaryrefslogtreecommitdiff
path: root/java/org/gnu/emacs/EmacsDialogButtonLayout.java
blob: da57d1c4404698546355901a4edfc6f5cf9d3a96 (plain)
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
/* Communication module for Android terminals.  -*- c-file-style: "GNU" -*-

Copyright (C) 2023-2024 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs 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 3 of the License, or (at
your option) any later version.

GNU Emacs 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
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

package org.gnu.emacs;



import android.content.Context;

import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;



/* This ``view group'' implements a container widget for multiple
   buttons of the type found in pop-up dialogs.  It is used when
   displaying a dialog box that contains more than three buttons, as
   the default dialog box widget is not capable of holding more than
   that many.  */



public final class EmacsDialogButtonLayout extends ViewGroup
{
  public
  EmacsDialogButtonLayout (Context context)
  {
    super (context);
  }

  @Override
  protected void
  onMeasure (int widthMeasureSpec, int heightMeasureSpec)
  {
    int width, count, i, x, y, height, spec, tempSpec;
    View view;

    /* Obtain the width of this widget and create the measure
       specification used to measure children.  */

    width = MeasureSpec.getSize (widthMeasureSpec);
    spec = MeasureSpec.makeMeasureSpec (0, MeasureSpec.UNSPECIFIED);
    tempSpec
      = MeasureSpec.makeMeasureSpec (width, MeasureSpec.AT_MOST);
    x = y = height = 0;

    /* Run through each widget.  */

    count = getChildCount ();

    for (i = 0; i < count; ++i)
      {
	view = getChildAt (i);

	/* Measure this view.  */
	view.measure (spec, spec);

	if (width - x < view.getMeasuredWidth ())
	  {
	    /* Move onto the next line, unless this line is empty.  */

	    if (x != 0)
	      {
		y += height;
		height = x = 0;
	      }

	    if (view.getMeasuredWidth () > width)
	      /* Measure the view again, this time forcing it to be at
		 most width wide, if it is not already.  */
	      view.measure (tempSpec, spec);
	  }

	height = Math.max (height, view.getMeasuredHeight ());
	x += view.getMeasuredWidth ();
      }

    /* Now set the measured size of this widget.  */
    setMeasuredDimension (width, y + height);
  }

  @Override
  protected void
  onLayout (boolean changed, int left, int top, int right,
	    int bottom)
  {
    int width, count, i, x, y, height, spec, tempSpec;
    View view;

    /* Obtain the width of this widget and create the measure
       specification used to measure children.  */

    width = getMeasuredWidth ();
    spec = MeasureSpec.makeMeasureSpec (0, MeasureSpec.UNSPECIFIED);
    tempSpec
      = MeasureSpec.makeMeasureSpec (width, MeasureSpec.AT_MOST);
    x = y = height = 0;

    /* Run through each widget.  */

    count = getChildCount ();

    for (i = 0; i < count; ++i)
      {
	view = getChildAt (i);

	/* Measure this view.  */
	view.measure (spec, spec);

	if (width - x < view.getMeasuredWidth ())
	  {
	    /* Move onto the next line, unless this line is empty.  */

	    if (x != 0)
	      {
		y += height;
		height = x = 0;
	      }

	    if (view.getMeasuredWidth () > width)
	      /* Measure the view again, this time forcing it to be at
		 most width wide, if it is not already.  */
	      view.measure (tempSpec, spec);
	  }

	/* Now assign this view its position.  */
	view.layout (x, y, x + view.getMeasuredWidth (),
		     y + view.getMeasuredHeight ());

	/* And move on to the next widget.  */
	height = Math.max (height, view.getMeasuredHeight ());
	x += view.getMeasuredWidth ();
      }
  }
};