File: menu.cc

package info (click to toggle)
miwm 1.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,296 kB
  • ctags: 910
  • sloc: cpp: 8,179; sh: 231; makefile: 148
file content (551 lines) | stat: -rw-r--r-- 14,634 bytes parent folder | download | duplicates (3)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//-*-c++-*-
// ----------------------------------------------
// RCS data:
// $Date: 2003/07/09 09:07:36 $
// $Revision: 1.6 $
// $Source: /cvsroot/miwm/miwm/miwm/menu.cc,v $
// $Id: menu.cc,v 1.6 2003/07/09 09:07:36 sgbeal Exp $
// $RCSfile: menu.cc,v $
// -------------------------------------------
// Copyright by Ben Paul Wise.
// -------------------------------------------
// This program 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 2 of the License, or
// (at your option) any later version.
// 
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
// -------------------------------------------
// these are very simple menus: no icons, no cascading
// -------------------------------------------

#include "miwm.h"
#include "mimenu.h"
#include "ws.h"
#include "EPropertyList.h"
#include "miwm_framework.h"
// ----------------------------------------------

Menu::Menu() {
  enterAction = NULL;
  exitAction = NULL;
  releaseAction = NULL;

  items = new NodeList(); // data of type MenuItem

  // totally arbitrary start up defaults, but they are always
  // the most recent position of the popup menu
  x = 100;
  y = 100;
  iWidth = 60;
  iHeight = 10;
  length = 4;

  mHeight = iHeight * length;
  mWidth = iWidth;

  currItem = -1;
  prevItem = -1;
  currMI = NULL;
  prevMI = NULL;


}

Menu::~Menu() {
  if (items != NULL)
    obliterateNodeList(items);
}

// ----------------------------------------------

void
Menu::setDimensions() {
  int w = 0;	// Widest string so far.
  Node *miND = NULL;
  MenuItem *mi = NULL;
  iWidth = 0;
  iHeight = 0;
  length = items->length();

  if (0 == length)
    return;

  w = 0;
  for (miND = items->first;
       miND != NULL;
       miND = items->nextNode(miND)) {
    mi = ((MenuItem*) miND->data);
    w = imax(w, XTextWidth(popup_font, mi->label.c_str(), mi->label.size() + 1) );
  }

  iWidth = w + border;
  iHeight = popup_font->ascent + popup_font->descent + 1;

  mWidth = iWidth;
  mHeight = length * iHeight;
  return;
}

// ----------------------------------------------
// menu operations

void 
Menu::startMenuing(XButtonEvent *xbe) {
  //   cout << "Start menuing" << endl << flush;
  int xNew, yNew;
//   float N = 10;
  assert (wmMenuing == theWM->mode);
  if (-1 == currItem) // if user did not set anything, guess at a default
    currItem = 0;

  XChangeActivePointerGrab(dpy, 
			   theWM->moving_event_mask, 
			   menu_cursor, CurrentTime);


  if (items->length() > 0)
    currMI = ((MenuItem*) items->first->data);
  else
    currMI = NULL;
  
  prevItem = -1;
  prevMI = NULL;

  if (items->length() == 0) {
    //     cout << "No items to menu. Stopping menuing" << endl;
    theWM->mode = wmReady;
    return;
  }
  
  // we place the menu to the right, with the current item chosen
  y = ((int)(xbe->y_root - iHeight * (currItem + 0.5)));
//   x = ((int)(xbe->x_root - iWidth / N));
  x = ((int)(xbe->x_root - border/2));

  if ((x < border) || (x + iWidth > (display_width - border))) {
    // we adjust the menu to be completely on the screen horizontally.
    // this is not as critical as the vertical dimension, so
    // we do not bother warping the pointer
    x = imax(border, imin ( x, (display_width - border) - iWidth));
  }
      

  if ((y < border) || (y + ( iHeight * items->length() ) > (display_height - border))) {
    // we adjust the menu to be completely on the screen vertically,
    // and warp the pointer appropriately
    y = imax(border, imin( y, (display_height - border) -  iHeight * items->length()));
  }
    xNew = x + border/2;
    yNew = iround(y + iHeight * (currItem + 0.5));
    XWarpPointer(dpy, // obvious
		 None,  // unknown meaning and function
		 root,  // window coordinate system to use
		 0, 0, 0, 0, // unknown meaning and function
		 xNew, yNew);

  XMoveResizeWindow(dpy, popup, x, y, mWidth, mHeight);
  // do not highlight the chosen item - menu::expose will do that
  XMapRaised(dpy, popup);
  return;
}

void 
Menu::motionNotify(XEvent*) {
  assert (wmMenuing == theWM->mode);

  //   cout << "Continue menuing" << endl << flush;

  // a silly little demo to show I know what I am doing:
  //   mWidth = (mWidth + 1) % 20;
  //   mWidth = imax(5, mWidth);
  //   mHeight = (mHeight + 1) % 60;
  //   mHeight = imax(mHeight, 15);
  //   getMouse (&x, &y);
  //   XMoveResizeWindow(dpy, popup, x, y, mWidth, mHeight);
  Node *nd = NULL;
  Client *hc = NULL;
  int i = 0;
  prevItem = currItem;
  prevMI = currMI;
  currItem = whichMenuItem();
  currMI = NULL;
  if (currItem >= 0) { // on the menu
    for (nd = items->first, i = 0; 
	 ((nd != NULL) && (currMI == NULL)) ; 
	 nd = items->nextNode(nd), i = i+1) {
      if (i == currItem)
 	currMI = ((MenuItem*) nd->data);
    }
  }

  if (currItem != prevItem) {
    //      cout << "You are now on item " << currItem << endl;
    //      cout << "     " << currMI << endl;


    // Unhighlight previous item if on menu.
    if (prevItem >= 0 && prevItem < length) {
      XFillRectangle(dpy, popup, menu_gc, 0, prevItem * iHeight,
		     iWidth, iHeight);
    }

    // Highlight current item if on menu.
    if (currItem >= 0 && currItem < length) {
      XFillRectangle(dpy, popup, menu_gc, 0, currItem * iHeight,
		     iWidth, iHeight);
    }
  }

  if ((hiddenClientMenu == theWM->whichMenu) && (1 == theWM->previewCP)) {
    if (currItem != prevItem) {
      // exit action
      if (NULL != prevMI) {
	hc = ((Client*) prevMI->data);
	theWM->undoFullPreview(hc);
      }
      // enter action
      if (NULL != currMI) {
	hc = ((Client*) currMI->data);
	theWM->doFullPreview(hc);
      }
    }
  }

  return;
}


void 
Menu::buttonRelease() {
  assert (wmMenuing == theWM->mode);
  Client* c = NULL;
  Node* cND = NULL;
  WorkSpace* ws = NULL;
  WMOperation op = NULL;
  //   cout << "End menuing" << endl << flush;
  //   cout << "You released on item " << currItem << endl;

  if (NULL != currMI)  // we were actually on the menu
    switch (theWM->whichMenu) {
    case hiddenClientMenu:
      c = ((Client*) currMI->data);
      cND = theWM->hiddenClients->inList((void*) c);
      if (cND != NULL) 
	theWM->hiddenClients->remove(cND);
      theWM->unhideClient(c);
      break;

    case operationsMenu:
      op = ((WMOperation) currMI->data);
      op();
      break; 

    case changeWSMenu:
      ws =  ((WorkSpace*) currMI->data);
      theWM->changeWorkSpace(theWM->workSpace, ws);
			
      break;

    case sendCtoWSMenu:
      ws =  ((WorkSpace*) currMI->data);
      if (ws != theWM->workSpace)
	theWM->sendClientToWorkSpace(current, ws);
      break;

    case commandMenu:
      theWM->spawn((char*) currMI->data);
      break;

    default:
      break;
    }
//   cout << "Menuing mode released = " << theWM->whichMenu << endl << flush;
  theWM->clearMenu();  // wipe out that pesky data!
  obliterateNodeList(items);
  items = NULL;

  theWM->whichMenu = noMenu;

  // this is very important: you have to keep the popup displayed and
  // receiving XEvents until the release-action is completed.
  // For example, on sending a client to another WS:
  // if you pop it down right away, then the client grabs focus and
  // stays on the WS. This way, the client goes away first, THEN the
  // popup is unmapped.
  XUnmapWindow(dpy, popup);
  return;
}


void 
Menu::expose() {
  assert (theWM->mode == wmMenuing);
  Node *miND = NULL;
  MenuItem *mi = NULL;
  int tx, ty;
  int i = 0;
  for (miND = items->first; miND != NULL; miND = items->nextNode(miND)) {
    mi = ((MenuItem*) miND->data);
    tx = (iWidth - XTextWidth(popup_font, mi->label.c_str(),
				 mi->label.size())) / 2;
    ty = i * iHeight + popup_font->ascent + 1;
    i = i + 1; 

    XDrawString(dpy, popup, menu_gc, tx, ty, mi->label.c_str(),
		mi->label.size());
  }
  // Highlight current item if on menu. 
  if (currItem >= 0 && currItem < length)
    XFillRectangle(dpy, popup, menu_gc, 0, currItem * iHeight,
		   iWidth, iHeight);

  return;
}

int
Menu::whichMenuItem() {
  int mx, my;
  int item;
  getMouse (&mx, &my);  // root coords

  mx = mx - x; // now menu coords
  my = my - y; // now menu coords
  if ((mx < 0) || (my < 0) || (mx > mWidth) || (my >= mHeight))
    item =  -1;
  else
    item = my / iHeight;

//   cout << "you are on item " << item << endl << flush;
  return item;
 }

// ----------------------------------------------

MenuItem::MenuItem() {
  label = std::string();
  data = NULL;
}

MenuItem::MenuItem(const char* lbl, const void* dt) {
  label = std::string(lbl);
  data = dt;
}

MenuItem::~MenuItem() {
//   cout << "deleting menu item " << label << endl << flush;
  // leave data alone, except in one special case
  if ((wmMenuing == theWM->mode) && (commandMenu == theWM->whichMenu)) {
//     cout << "deleting data"<<endl << flush;
    // in this one special case, we know the 'data' is char*

    // this is 'const', and should not be deleted
    //    delete ((char*) data);

    data = NULL;
  }
}


// ----------------------------------------------

void
WindowManager::clearMenu() {
  Node* nd = NULL;
  MenuItem* mi = NULL;

  if (NULL == menu)
    menu = new Menu();

  if (NULL != menu->items) {
    for ( nd = menu->items->first; nd != NULL; nd = menu->items->nextNode(nd)) {
      mi = ((MenuItem*) nd->data);
      delete mi;
      nd->data = NULL;
    }
  }
  if (NULL != menu->items) {
    obliterateNodeList(menu->items);
    menu->items = new NodeList();
  }
  else
    menu->items = new NodeList();

  assert (menu->items->length() == 0);
  menu->currItem = 0; // point at top workspace, initially
  return;
}

void 
WindowManager::setWSMenu() {
  WorkSpace* ws;
  int i;
  MenuItem *mi;
  clearMenu();
  menu->currItem = 0; // point at top workspace, initially

  for (i=0; i<numWorkSpaces; i++) {
    ws = allWorkSpaces[i];
    if (ws == workSpace)
      menu->currItem = i; // point at current WS
    mi = new MenuItem (ws->label.c_str(), (void*) ws);
    menu->items->append((void*) mi);
  }

  menu->setDimensions();
  whichMenu = noMenu; // you must say which purpose it will serve!
  return;
}
// ----------------------------------------------

void 
WindowManager::setClientOpsMenu() {
  MenuItem* mi = NULL;
  clearMenu();
  menu->currItem = 0; // point at first operation

  if (0 == current->sticky)
    mi = new MenuItem("Make Sticky", (void*) toggleSticky);
  else
    mi = new MenuItem("Make Unsticky", (void*) toggleSticky);
  menu->items->append(mi);

  mi = new MenuItem("ID", (void*) idCurrent);
  menu->items->append(mi);

  mi = new MenuItem("Kill", (void*) terminateCurrent);
  menu->items->append(mi);

  menu->setDimensions();
  whichMenu = operationsMenu; 
  return;
}
// ----------------------------------------------

void 
WindowManager::setWMOpsMenu() {
  MenuItem* mi = NULL;
  clearMenu();
  menu->currItem = 0; // point at first operation

  mi = new MenuItem("Retile WS", (void*) retileWS);
  menu->items->append(mi);
  mi = new MenuItem("Contract WS", (void*) contractWS);
  menu->items->append(mi);
  mi = new MenuItem("Step WS", (void*) stepWSup);
  menu->items->append(mi);
  mi = new MenuItem("Rotate Clients", (void*) rotateClients);
  menu->items->append(mi);

  if (1 == theWM->protectTopLeft)
    mi = new MenuItem("Allow off-screen", (void*) toggleTopLeft);
  else
    mi = new MenuItem("Force on-screen", (void*) toggleTopLeft);
  menu->items->append(mi);

  if (1 == theWM->raiseOnFocusP)
    mi = new MenuItem("No Autoraise", (void*) toggleRaiseOnFocus);
  else
    mi = new MenuItem("Raise on Focus", (void*) toggleRaiseOnFocus);
  menu->items->append(mi);

#ifdef USE_PIXMAPS
  if (1 == theWM->usePixmapsP)
    mi = new MenuItem("No Pixmaps", (void*) togglePixmaps);
  else
    mi = new MenuItem("Use Pixmaps", (void*) togglePixmaps);
  menu->items->append(mi);
#endif


  if (1 == theWM->tileP)
    mi = new MenuItem("No Tiling", (void*) toggleTiling);
  else
    mi = new MenuItem("Tile New", (void*) toggleTiling);
  menu->items->append(mi);

  if (1 == theWM->previewCP)
    mi = new MenuItem("No Preview Clients", (void*) toggleClientPreview);
  else 
    mi = new MenuItem("Preview Clients", (void*) toggleClientPreview);
  menu->items->append(mi);
  mi = new MenuItem("Restart miwm", (void*) restartWM);
  menu->items->append(mi);
  mi = new MenuItem("QUIT miwm", (void*) terminateWM);
  menu->items->append(mi);

  menu->setDimensions();
  whichMenu = operationsMenu; 
  return;
}
// ----------------------------------------------

void 
WindowManager::setHiddenClientMenu() {
  Node *cND;
  Client *c;
  MenuItem *mi;
  clearMenu();
  menu->currItem = 0; // point at last one hidden

  if (hiddenClients->length() > 0) {
    for (cND = hiddenClients->first; cND != NULL;	 cND = hiddenClients->nextNode(cND)) {
      c = ((Client*) cND->data);
      if (c->workSpace == workSpace) {
	getWindowName(c);
 	mi = new MenuItem(choose_label(c), ((void*) c));
	menu->items->append((void*) mi);
      }
    }
  }

  menu->setDimensions();

  assert (menu->items->length() <= hiddenClients->length());
  whichMenu = hiddenClientMenu;
  return;
}


// ----------------------------------------------


void 
WindowManager::setShellCmdMenu() {
        MenuItem *mi = NULL;
        clearMenu();
        menu->currItem = 0; // point at top command
        EPropertyList list;
        if( ! list.load( miwm::homePath() + "/" + "miwm.usermenu" ) ) return;
        typedef EPropertyList::string_list PSLIST;

        PSLIST::const_iterator it = list.orderedKeys().begin();
        PSLIST::const_iterator eit = list.orderedKeys().end();
        std::string val;
        while( it != eit )
        {
                val = list.getString( (*it) );
                if( val.empty() ) { ++it; continue; } // no point, is there?
                mi = new MenuItem();
                mi->label = (*it);
                mi->data = (const void *)  val.c_str();
                menu->items->append((void*) mi);
                ++it;
        }
        menu->setDimensions();
        whichMenu = commandMenu;
        return;
}


// ----------------------------------------------
// end of menu.cc
// ----------------------------------------------