Permalink
Browse files

MenuBar and fixes in Menu, use a MenuBar in example puzzle

  • Loading branch information...
1 parent f374087 commit 5d14163f9faedbe6ae4ca65390206800dd499970 @NotFound committed Dec 11, 2011
Showing with 227 additions and 7 deletions.
  1. +1 −0 Guitor.winxhead
  2. +23 −6 examples/puzzle.winxed
  3. +200 −1 src/Guitor.winxed
  4. +3 −0 src/GuitorNci.winxed
View
@@ -16,6 +16,7 @@ class ChildWindow;
class TextButton;
class EditBox;
class Menu;
+class MenuBar;
}
View
@@ -92,6 +92,7 @@ class Puzzle : TopLevelWindow
var y;
var width;
var height;
+ var ypbase;
var wpiece;
var hpiece;
var pieces;
@@ -100,21 +101,35 @@ class Puzzle : TopLevelWindow
function Puzzle(controller)
{
int width = 764;
- int height = 420;
+ int height = 440;
int x = 0;
int y = 0;
self.x = x;
self.y = y;
self.TopLevelWindow(controller, "Puzzle", x, y, width, height);
- self.setdims(width, height);
self.SetWMProtocols( [ "WM_DELETE_WINDOW" ] );
self.OnConfigure += function (event) { self.onconfigure(event); };
self.OnClientMessage += function (event) { self.quit(); };
self.OnDestroy += function (event) { controller.Quit(); };
- var menu = new Menu(self.display, self.display.CreateFont("sans-14"));
+
+ var display = self.display;
+ var menufont = display.CreateFont("sans-12");
+ var menubar = new MenuBar(display, menufont);
+ var menufile = new Menu(display, menufont);
+ menufile.push("shuffle", function () { self.shuffle(); } );
+ menufile.push("quit", function () { self.quit(); } );
+ menubar.push("Game", menufile);
+ var menuwin = menubar.activate(self, 0, 0, width);
+ menuwin.Map();
+ int ypbase = menubar.getheight() + 2;
+ self.ypbase = ypbase;
+ self.setdims(width, height);
+
+ var menu = new Menu(display, menufont);
menu.push("shuffle", function () { self.shuffle(); } );
menu.push("quit", function () { self.quit(); } );
self.menu = menu;
+
self.OnButtonPress += function (event) { self.onbuttonpress(event); };
var grid = [];
for (int i = 0; i < VER; ++i) {
@@ -155,7 +170,7 @@ class Puzzle : TopLevelWindow
self.width = width;
self.height = height;
int wpiece = width / HOR;
- int hpiece = height / VER;
+ int hpiece = (height - self.ypbase) / VER;
self.wpiece = wpiece;
self.hpiece = hpiece;
var display = self.display;
@@ -184,9 +199,10 @@ class Puzzle : TopLevelWindow
}
function move(int n, int x, int y)
{
+ int ypbase = self.ypbase;
int wpiece = self.wpiece;
int hpiece = self.hpiece;
- self.pieces[n - 1].MoveWindow(x * wpiece + 2, y * hpiece + 2);
+ self.pieces[n - 1].MoveWindow(x * wpiece + 2, y * hpiece + 2 + ypbase);
}
function shuffle()
{
@@ -230,14 +246,15 @@ class Puzzle : TopLevelWindow
}
var pieces = self.pieces;
+ int ypbase = self.ypbase;
int wpiece = self.wpiece;
int hpiece = self.hpiece;
for (int i = 0; i < VER; ++i)
for (int j = 0; j < HOR; ++j) {
int pos = grid[i, j] - 1;
if (pos < 0)
continue;
- pieces[pos].MoveWindow(j * wpiece + 2, i * hpiece + 2);
+ pieces[pos].MoveWindow(j * wpiece + 2, i * hpiece + 2 + ypbase);
}
}
function click(int n)
View
@@ -2471,6 +2471,8 @@ class MenuWindow : ChildWindow
{
const int CURRENT_NONE = -1;
var menu;
+ var x;
+ var y;
var width;
var height;
var hbase;
@@ -2484,6 +2486,8 @@ class MenuWindow : ChildWindow
{
var display = parent.display;
self.menu = menu;
+ self.x = x;
+ self.y = y;
var items = menu.items;
int nitems = elements(items);
int hitem = menu.font.getHeight();
@@ -2509,6 +2513,7 @@ class MenuWindow : ChildWindow
self.current = CURRENT_NONE;
self.ChildWindow(parent, x + 1, y + 1, width, height,
{ "background-color" : bgcolor } );
+ self.SetFont(menu.font);
self.SetForeground(fgcolor);
self.SetBackground(bgcolor);
self.OnExpose += function (event) { self.onexpose(event); };
@@ -2623,10 +2628,15 @@ class Menu
{
push(self.items, new MenuItem(name, action));
}
- function _activate(parent, int x, int y, int time)
+ function open(parent, x, y)
{
var menuwindow = new MenuWindow(parent, x, y, self);
menuwindow.Map();
+ return menuwindow;
+ }
+ function _activate(parent, int x, int y, int time)
+ {
+ var menuwindow = self.open(parent, x, y);
const int mask = EnterWindowMask | LeaveWindowMask |
ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
int grab = menuwindow.GrabPointer(false, mask,
@@ -2649,6 +2659,195 @@ class Menu
}
}
+//**************************************************************
+
+// A barely functional menu bar to be used in examples and tests.
+// No keyboard interface, only mouse.
+// TODO: better integration with Menu and MenuWindow.
+
+const int BARSEP = 16;
+
+class MenuBarWindow : ChildWindow
+{
+ var parent;
+ var menubar;
+ var itempos;
+ var height;
+ var active;
+ var child;
+ function MenuBarWindow(parent, int x, int y, int width, menubar)
+ {
+ self.parent = parent;
+ self.menubar = menubar;
+ var display = menubar.display;
+ var font = menubar.font;
+ var fgcolor = display.ParseColor("black");
+ var bgcolor = display.ParseColor("grey90");
+ int height = font.getHeight() + 8;
+ self.height = height;
+ if (width <= 0)
+ width = menubar.maxWidth();
+ self.active = false;
+ self.ChildWindow(parent, x, y, width, height,
+ { "background-color" : bgcolor } );
+ self.SetFont(font);
+ self.SetForeground(fgcolor);
+ self.SetBackground(bgcolor);
+ var items = self.menubar.items;
+ int nitems = elements(items);
+ int itempos[nitems];
+ int xp = BARSEP / 2;
+ for (int i = 0; i < nitems; ++i) {
+ itempos[i] = xp;
+ xp += self.getTextWidth(items[i].name) + BARSEP;
+ }
+ self.itempos = itempos;
+ self.OnExpose += function (event) { self.onexpose(event); };
+ self.OnButtonPress += function (event) { self.onbuttonpress(event); };
+ self.OnButtonRelease += function (event) { self.onbuttonrelease(event); };
+ self.OnMotion += function (event) { self.onmotion(event); };
+ }
+ function itemAt(int x)
+ {
+ var items = self.menubar.items;
+ int n = elements(items);
+ int xpos = BARSEP / 2;
+ int pad = BARSEP / 4;
+ for (int i = 0; i < n; ++i) {
+ string str = items[i].name;
+ int w = self.getTextWidth(str);
+ if (x >= xpos - pad && x <= xpos + w + pad)
+ return i;
+ xpos += w + BARSEP;
+ }
+ return -1;
+ }
+ function onexpose(event)
+ {
+ int baseline = self.menubar.font.getAscent() + 2;
+ var items = self.menubar.items;
+ int n = elements(items);
+ var itempos = self.itempos;
+ for (int i = 0; i < n; ++i) {
+ string str = items[i].name;
+ self.DrawImageString(itempos[i], baseline, str);
+ }
+ }
+ function onbuttonpress(event)
+ {
+ if (event.button() != 1)
+ return;
+ self.active =: true;
+ int x = event.x();
+ int y = event.y();
+ int nitem = self.itemAt(x);
+ if (nitem != -1) {
+ int x = self.itempos[nitem];
+ var action = self.menubar.items[nitem].action;
+ self.child = action.open(self.parent, x, self.height - 4);
+ }
+ }
+ function onbuttonrelease(event)
+ {
+ self.active =: false;
+ var child = self.child;
+ if (child != null) {
+ int current = child.current;
+ var action;
+ if (current >= 0)
+ action = child.menu.items[current].action;
+ self.child = null;
+ self.controller.pushaction(function () { child.Destroy(); } );
+ if (action != null)
+ self.controller.pushaction(action);
+ }
+ }
+ function onmotion(event)
+ {
+ if (! self.active)
+ return;
+ var viewint = new ["StructView"] ( [ DATATYPE_STRUCT, 1, DATATYPE_INT ] );
+ var root_return = viewint.alloc();
+ var child_return = viewint.alloc();
+ var win_x_return = viewint.alloc();
+ var win_y_return = viewint.alloc();
+ var n = viewint.alloc();
+ int r = getfun("XQueryPointer")(self.display.xdisplay,
+ self.parent.xdrawable, root_return, child_return,
+ n, n, win_x_return, win_y_return, n);
+ int child_p = viewint[child_return, 0];
+ var child = self.child;
+ if (r && child_p != None) {
+ int childid = child == null ? None : child.xdrawable;
+ if (child != null && child_p == childid) {
+ int win_x = viewint[win_x_return, 0];
+ int win_y = viewint[win_y_return, 0];
+ child.setcurrent(win_x - child.x, win_y - child.y);
+ }
+ else {
+ if (child != null)
+ child.setcurrent(-1, -1);
+ }
+ }
+ else {
+ if (child != null)
+ child.setcurrent(-1, -1);
+ }
+ }
+}
+
+class MenuBarItem
+{
+ var name;
+ var action;
+ function MenuBarItem(string name, action)
+ {
+ self.name = name;
+ self.action = action;
+ }
+}
+
+class MenuBar
+{
+ var display;
+ var font;
+ var items;
+ function MenuBar(display, font)
+ {
+ self.display = display;
+ self.font = font;
+ self.items = [];
+ }
+ function getwidth()
+ {
+ var items = self.items;
+ int n = elements(items);
+ if (n == 0)
+ return 0;
+ var display = self.display;
+ var font = self.font;
+ int width = BARSEP / 2 + (n - 1) * BARSEP;
+ for (int i = 0; i < n; ++i) {
+ int w = font.getTextxOff(display, items[i].name);
+ width += w;
+ }
+ return width;
+ }
+ function getheight()
+ {
+ return int(self.font.getHeight()) + 8;
+ }
+ function push(string name, action)
+ {
+ push(self.items, new MenuBarItem(name, action));
+ }
+ function activate(parent, int x, int y, int width)
+ {
+ var window = new MenuBarWindow(parent, x, y, width, self);
+ return window;
+ }
+}
+
} // namespace Guitor
// End.
View
@@ -689,6 +689,9 @@ function create_function(string funcname)
case "XListProperties":
sig = "ppip";
break;
+ case "XQueryPointer":
+ sig = "ipippppppp";
+ break;
case "XWarpPointer":
sig = "vpiiiiiiii";
break;

0 comments on commit 5d14163

Please sign in to comment.