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 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
|
/*
** 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 1, 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.
*/
/*
* Author : Alexandre Parenteau <aubonbeurre@hotmail.com> --- May 1999
*/
#include "stdafx.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <float.h>
#include <vector>
#include <map>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "umenu.h"
#include "uconsole.h"
#ifdef WIN32
#include "dib.h"
#endif
#ifdef qMacAPP
#include "UMenuMgr.h"
#include "UDispatcher.h"
#endif
#if qMacPP
#include <UConditionalMenus.h>
#include <UAppearanceMenus.h>
#endif
#ifdef qGTK
extern "C"
{
// from glade's support.h
GtkWidget *create_pixmap(GtkWidget *widget, const gchar *filename);
}
#endif
#if defined(_DEBUG) && defined(WIN32)
# define new DEBUG_NEW
# undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UIMPLEMENT_DYNAMIC(UMenu, UWidget)
UBEGIN_MESSAGE_MAP(UMenu, UWidget)
ON_UPDATEUI(UMenu)
ON_UCOMMAND_RANGE(0, 0xFFFF, UMenu::OnCommands)
UEND_MESSAGE_MAP()
UMenu::UMenu(int widid) : UWidget(widid)
#if qGTK
, fAccelGroup(0L)
#endif
{
#if qGTK
fAccelGroup = gtk_accel_group_new ();
#endif
}
UMenu::~UMenu()
{
}
#ifdef WIN32
// create a HBITMAP from a pixmap
HBITMAP UMakeBitmap(char **icon, int & width, int & height, COLORREF back)
{
BYTE **src = (BYTE **)icon;
// read the header
int w, h, nc, cpp, i, x, y;
if(sscanf((char *)*src++, "%d%d%d%d", &w, &h, &nc, &cpp) != 4)
return 0L;
width = w;
height = h;
HBITMAP hbitmap = CreateDIB(w, h, 8, 0L);
LPBITMAPINFO header = (LPBITMAPINFO)GlobalLock(hbitmap);
BYTE *image = (LPBYTE)header + header->bmiHeader.biSize + PaletteSize(header);
RGBQUAD *pRgb = (RGBQUAD *)((LPBYTE)header + header->bmiHeader.biSize);
// read the colormap
std::map<short, BYTE> colors;
for(i = 0; i < nc; i++)
{
const BYTE *line = *src++;
short aref;
if(cpp == 1)
{
aref = line[0];
line += 4;
}
else if(cpp == 2)
{
aref = ((short)line[1] << 8) | line[0];
line += 5;
}
else
ASSERT(0);
COLORREF color;
if(strcmp((char *)line, "None") == 0)
{
color = back;
}
else
{
if(*line++ != '#')
return 0L;
if(strlen((char *)line) < 6)
return 0L;
int comp[6], c;
int j;
for(j = 0; j < 6; j++)
{
c = *line++;
if(c >= '0' && c <= '9')
comp[j] = c - '0';
else if(c >= 'a' && c <= 'f')
comp[j] = c - 'a' + 10;
else if(c >= 'A' && c <= 'F')
comp[j] = c - 'A' + 10;
else
return 0L;
}
color = RGB(comp[0]*16+comp[1],comp[2]*16+comp[3],comp[4]*16+comp[5]);
}
colors.insert(std::make_pair(aref, i));
pRgb->rgbRed = GetRValue(color);
pRgb->rgbGreen = GetGValue(color);
pRgb->rgbBlue = GetBValue(color);
pRgb->rgbReserved = 0;
pRgb++;
}
if(nc != colors.size() || nc > 256)
return 0L;
// make the bitmap
for(y = 0; y < h; y++)
{
const BYTE *line = *src++;
BYTE *ptr = image + WIDTHBYTES(w * 8) * (h - y - 1);
for(x = 0; x < w; x++)
{
short aref;
if(cpp == 1)
{
aref = line[0];
line++;
}
else if(cpp == 2)
{
aref = ((short)line[1] << 8) | line[0];
line += 2;
}
std::map<short, BYTE>::iterator pos = colors.find(aref);
if(pos == colors.end())
return 0L;
*ptr++ = (*pos).second;
}
}
GlobalUnlock(hbitmap);
HBITMAP ddbbmap = DIBToBitmap(hbitmap);
GlobalFree((HGLOBAL)hbitmap);
return ddbbmap;
}
HBITMAP UMakeBitmap(char **icon)
{
int w, h;
return UMakeBitmap(icon, w, h, ::GetSysColor(COLOR_BTNFACE));
}
UEXPORT HBITMAP UMakeBitmap(const char *icon)
{
std::vector<UStr> icstr;
const char *tmp;
while((tmp = strchr(icon, '\n')) != 0L)
{
UStr str;
str.set(icon, tmp - icon);
icon = tmp + 1;
icstr.push_back(str);
}
if(icon[0] != '\0')
{
icstr.push_back(UStr(icon));
}
std::vector<UStr>::const_iterator i;
std::vector<const char *> ic;
for(i = icstr.begin(); i != icstr.end(); ++i)
{
ic.push_back((*i).c_str());
}
return UMakeBitmap((char **)&ic[0]);
}
#endif
#define CTRLALT_STROKE "Ctrl+Alt+"
#define CTRL_STROKE "Ctrl+"
#define ALT_STROKE "Alt+"
static void GetMenuTitle(const char *name, UStr & title, int cmd,
char & mnemonic, int & mod, int & key)
{
title = "";
mod = 0;
key = 0;
mnemonic = '\0';
// get the accelerator
UStr copy(name);
char *tmp;
if((tmp = strchr(copy, '\t')) != 0L)
{
*tmp++ = '\0';
if(strncmp(tmp, CTRLALT_STROKE, strlen(CTRLALT_STROKE)) == 0)
{
mod = UK_CONTROL | UK_ALT;
key = tmp[strlen(CTRLALT_STROKE)];
}
else if(strncmp(tmp, CTRL_STROKE, strlen(CTRL_STROKE)) == 0)
{
mod = UK_CONTROL;
key = tmp[strlen(CTRL_STROKE)];
}
else if(strncmp(tmp, ALT_STROKE, strlen(ALT_STROKE)) == 0)
{
mod = UK_ALT;
key = tmp[strlen(ALT_STROKE)];
}
name = copy;
}
title = name;
#ifndef WIN32
if((tmp = strchr(title, '&')) != 0L)
{
size_t l = tmp - (const char *)title;
memmove(&title[l], &title[l + 1], title.length() - l);
}
#endif
}
UMENU UMenuAddPulldown(int widid, const char *name, UMENU top)
{
UStr title;
int mod, key;
char mnemonic;
GetMenuTitle(name, title, 0, mnemonic, mod, key);
#ifdef qMacAPP
return UMENU_NULL;
#endif
#ifdef qMacPP
if(top == UMENU_NULL)
{
Assert_(0); // TODO
}
else
{
LMenuBar *menuBar = LMenuBar::GetCurrentMenuBar();
LMenu *menu = menuBar->FetchMenu(top);
if(menu == 0L)
return UMENU_NULL;
LStr255 pstr(name);
LMenu *newMenu = new LMenu(::UniqueID('MENU'), pstr);
menuBar->InstallMenu(newMenu, hierMenu);
menu->InsertCommand(pstr, cmd_UseMenuItem, max_Int16 - 1);
#if TARGET_API_MAC_CARBON
UAppearanceMenus::SetItemHierID(menu->GetMacMenuH(), ::CountMenuItems(menu->GetMacMenuH()),
newMenu->GetMenuID());
#else
UConditionalMenus::SetItemHierID(menu->GetMacMenuH(), ::CountMenuItems(menu->GetMacMenuH()),
newMenu->GetMenuID());
#endif
return newMenu->GetMenuID();
}
return UMENU_NULL;
#endif
#ifdef qGTK
GtkWidget *parent = 0L;
if(top == UMENU_NULL)
{
// try to find the menubar : the menu bar should be mapped
// to a UMenu object as the main widget
const UCmdTarget *target = UEventQueryWidget(widid);
UMenu *menu = 0L;
if(target == 0L || !target->IsKindOf(URUNTIME_CLASS(UMenu)))
return UMENU_NULL;
menu = (UMenu *)target;
if(menu == 0L)
return UMENU_NULL;
GtkWidget *wid = (GtkWidget *)menu->GetWidget(kUMainWidget);
if(wid == 0L || !GTK_IS_MENU_SHELL(wid))
return UMENU_NULL;
parent = wid;
}
else
{
if(!GTK_IS_MENU_SHELL(top))
return UMENU_NULL;
parent = (GtkWidget *)top;
}
GtkWidget *menuitem = gtk_menu_item_new_with_label(title);
gtk_widget_show(menuitem);
gtk_container_add(GTK_CONTAINER (parent), menuitem);
GtkWidget *menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM (menuitem), menu);
/*GtkAccelGroup *accel_group = */gtk_menu_ensure_uline_accel_group (GTK_MENU (menu));
return (UMENU)menu;
#endif
#ifdef WIN32
if(top == UMENU_NULL)
{
CMenu* pTopMenu = AfxGetMainWnd()->GetMenu();
CMenu menu;
menu.CreatePopupMenu();
pTopMenu->AppendMenu(MF_POPUP, (UINT)menu.GetSafeHmenu(), name);
static bool firsttime = true;
if(firsttime)
{
pTopMenu->DeleteMenu(0, MF_BYPOSITION);
firsttime = false;
}
return (UMENU)menu.Detach();
}
else
{
HMENU menuhdl = (HMENU)top;
CMenu parent;
CMenu menu;
menu.CreatePopupMenu();
VERIFY(parent.Attach(menuhdl));
parent.AppendMenu(MF_POPUP, (UINT)menu.GetSafeHmenu(), name);
parent.Detach();
return (UMENU)menu.Detach();
}
#endif
}
#ifdef WIN32
static void addAccel(int cmd, int mod, int key)
{
if(key == 0)
return;
// Save the current accelerator table.
CWnd *wnd = AfxGetMainWnd();
CFrameWnd *frame = 0L;
if(wnd->IsKindOf(RUNTIME_CLASS(CFrameWnd)))
frame = (CFrameWnd *)wnd;
else
frame = wnd->GetParentFrame();
if(frame == 0L)
return;
HACCEL haccelOld = frame->m_hAccelTable;
// Count the number of entries in the current
// table, allocate a buffer for the table, and
// then copy the table into the buffer.
int cAccelerators = CopyAcceleratorTable(haccelOld, NULL, 0);
LPACCEL lpaccelNew = (LPACCEL) LocalAlloc(LPTR, (cAccelerators + 1) * sizeof(ACCEL));
if (lpaccelNew == NULL)
return;
CopyAcceleratorTable(haccelOld, lpaccelNew, cAccelerators);
// Find the accelerator that the user modified
// and change its flags and virtual-key code
// as appropriate.
for (int i = 0; i < cAccelerators; i++)
{
if(lpaccelNew[i].cmd == cmd)
{
lpaccelNew[i].fVirt = FVIRTKEY;
if(mod & UK_ALT)
lpaccelNew[i].fVirt |= FALT;
if(mod & UK_CONTROL)
lpaccelNew[i].fVirt |= FCONTROL;
lpaccelNew[i].key = key;
break;
}
}
if(i == cAccelerators)
{
lpaccelNew[i].cmd = cmd;
lpaccelNew[i].fVirt = FVIRTKEY;
if(mod & UK_ALT)
lpaccelNew[i].fVirt |= FALT;
if(mod & UK_CONTROL)
lpaccelNew[i].fVirt |= FCONTROL;
lpaccelNew[i].key = key;
cAccelerators++;
}
// Create the new accelerator table, and
// destroy the old one.
DestroyAcceleratorTable(haccelOld);
frame->m_hAccelTable = CreateAcceleratorTable(lpaccelNew, cAccelerators);
LocalFree(lpaccelNew);
}
#endif
#ifdef qGTK
static void addToAccelGroup(GtkAccelGroup *accelGroup, GtkWidget *menuitem, int mod, int key)
{
if(key == 0 || accelGroup == 0L)
return;
int gdkmod = 0;
if(mod & UK_CONTROL)
gdkmod |= GDK_CONTROL_MASK;
if(mod & UK_ALT)
gdkmod |= GDK_MOD1_MASK;
gtk_widget_add_accelerator (menuitem, "activate",
accelGroup, key, gdkmod, GTK_ACCEL_VISIBLE);
}
static GtkWidget* gtk_menu_item_new_with_label_pixmap(const gchar *label, const void *pixmap)
{
// FIX ME !
#if 1
return gtk_menu_item_new_with_label(label);
#else
if(pixmap == 0L)
return gtk_menu_item_new_with_label(label);
GtkWidget *menu_item = gtk_menu_item_new ();
GtkPixmap *gtkpix = GTK_PIXMAP(create_pixmap(menu_item, (const gchar *)pixmap));
gtk_misc_set_alignment (GTK_MISC (gtkpix), 0.0, 0.5);
gtk_container_add (GTK_CONTAINER (menu_item), GTK_WIDGET(gtkpix));
gtk_widget_show (GTK_WIDGET(gtkpix));
GtkWidget *accel_label = gtk_accel_label_new (label);
gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), menu_item);
gtk_widget_show (accel_label);
return menu_item;
#endif
}
#endif // qGTK
void UMenuDelete(UMENU top, int afterIndex)
{
UASSERT(top != UMENU_NULL);
#ifdef qMacAPP
MenuRef menu = MAGetMenu(top);
if(menu)
{
short numItems = ::CountMenuItems(menu);
afterIndex++;
while (numItems >= afterIndex)
{
::DeleteMenuItem(menu, numItems);
--numItems;
}
}
#endif
#ifdef qMacPP
LMenuBar *menuBar = LMenuBar::GetCurrentMenuBar();
LMenu *menu = menuBar->FetchMenu(top);
if(menu)
{
short numItems = ::CountMenuItems(menu->GetMacMenuH());
afterIndex++;
while (numItems >= afterIndex)
{
menu->RemoveItem(numItems);
--numItems;
}
}
else
{
MenuHandle mhdl = ::GetMenu(top);
if(mhdl != 0L)
{
short numItems = ::CountMenuItems(mhdl);
afterIndex++;
while (numItems >= afterIndex)
{
::DeleteMenuItem(mhdl, numItems);
--numItems;
}
}
}
#endif
#ifdef WIN32
CMenu parent;
HMENU menuhdl = (HMENU)top;
VERIFY(parent.Attach(menuhdl));
int numItems = (int)parent.GetMenuItemCount() - 1;
while (numItems >= afterIndex)
{
VERIFY(parent.DeleteMenu(numItems, MF_BYPOSITION));
--numItems;
}
parent.Detach();
#endif
}
int UMenuCount(UMENU top)
{
UASSERT(top != UMENU_NULL);
#ifdef qMacAPP
MenuRef menu = MAGetMenu(top);
if(menu)
{
return ::CountMenuItems(menu);
}
#endif
#ifdef qMacPP
LMenuBar *menuBar = LMenuBar::GetCurrentMenuBar();
LMenu *menu = menuBar->FetchMenu(top);
if(menu)
{
return ::CountMenuItems(menu->GetMacMenuH());
}
else
{
MenuHandle mhdl = ::GetMenu(top);
if(mhdl != 0L)
{
return ::CountMenuItems(mhdl);
}
}
#endif
#ifdef WIN32
CMenu parent;
HMENU menuhdl = (HMENU)top;
VERIFY(parent.Attach(menuhdl));
UINT numItems = parent.GetMenuItemCount();
parent.Detach();
return numItems;
#endif
return 0;
}
void UMenuDeleteByCmd(UMENU top, int cmd)
{
UASSERT(top != UMENU_NULL);
#ifdef qMacAPP
MenuID menuNo;
short itemNo;
MenuRef menu = CommandToComponents(cmd, menuNo, itemNo);
if(menu)
{
::DeleteMenuItem(menu, itemNo);
}
#endif
#ifdef qMacPP
LMenuBar *menuBar = LMenuBar::GetCurrentMenuBar();
LMenu *menu = menuBar->FetchMenu(top);
if(menu)
{
menu->RemoveCommand(cmd);
}
#endif
#ifdef WIN32
CMenu parent;
HMENU menuhdl = (HMENU)top;
VERIFY(parent.Attach(menuhdl));
VERIFY(parent.DeleteMenu(cmd, MF_BYCOMMAND));
parent.Detach();
#endif
}
UMENU UMenuAddButton(int widid, const char *name, UMENU top, const void *pixmap, int cmd)
{
UStr title;
int mod, key;
char mnemonic;
GetMenuTitle(name, title, cmd, mnemonic, mod, key);
UASSERT(top != UMENU_NULL);
#ifdef qMacPP
LMenuBar *menuBar = LMenuBar::GetCurrentMenuBar();
LMenu *menu = menuBar->FetchMenu(top);
if(menu)
{
LStr255 pstr(title);
menu->InsertCommand(pstr, cmd, max_Int16 - 1);
}
else
{
MenuHandle mhdl = ::GetMenu(top);
if(mhdl != 0L)
{
SInt16 menuLength = ::CountMenuItems(mhdl);
LStr255 pstr(title);
::MacInsertMenuItem(mhdl, pstr, menuLength);
}
}
return UMENU_NULL;
#endif
#ifdef qMacAPP
MenuRef menu = MAGetMenu(top);
if(menu)
{
CStr255_AC pstr(title);
gDispatcher->DoMakeMenuBarManager();
TMenuBarManager::fgMenuBarManager->AddMenuItem(pstr, top, ::CountMenuItems(menu), cmd);
}
return UMENU_NULL;
#endif
#ifdef qGTK
UCmdTarget *target = UEventQueryWidget(widid);
if(target == 0L || !target->IsKindOf(URUNTIME_CLASS(UMenu)))
return UMENU_NULL;
UMenu *pmenu = (UMenu *)target;
if(!GTK_IS_MENU_SHELL(top))
return UMENU_NULL;
GtkWidget *parent = (GtkWidget *)top;
GtkWidget *menuitem = gtk_menu_item_new_with_label_pixmap(title, pixmap);
gtk_widget_show (menuitem);
gtk_container_add (GTK_CONTAINER (parent), menuitem);
addToAccelGroup((GtkAccelGroup *)pmenu->GetAccelGroup(), menuitem, mod, key);
// gtk_tooltips_set_tip (tooltips, quit1, "Quit the application", NULL);
UEventSendMessage(widid, EV_INIT_WIDGET, cmd, menuitem);
return UMENU_NULL;
#endif
#ifdef WIN32
HMENU menuhdl = (HMENU)top;
CMenu parent;
VERIFY(parent.Attach(menuhdl));
parent.AppendMenu(MF_STRING, UCMD_to_WINCMD(cmd), name);
addAccel(UCMD_to_WINCMD(cmd), mod, key);
if(pixmap != 0L && strlen((const char *)pixmap))
{
HBITMAP bmap = UMakeBitmap((const char *)pixmap);
CBitmap b;
VERIFY(b.Attach(bmap));
parent.SetMenuItemBitmaps(UCMD_to_WINCMD(cmd), MF_BYCOMMAND, &b, &b);
b.Detach();
}
parent.Detach();
return UMENU_NULL;
#endif
}
UMENU UMenuAddToggle(int widid, const char *name, UMENU top, const void *pixmap, int cmd)
{
UStr title;
int mod, key;
char mnemonic;
GetMenuTitle(name, title, cmd, mnemonic, mod, key);
#ifdef qMacPP
LMenuBar *menuBar = LMenuBar::GetCurrentMenuBar();
LMenu *menu = menuBar->FetchMenu(top);
if(menu)
{
LStr255 pstr(title);
menu->InsertCommand(pstr, cmd, max_Int16 - 1);
}
else
{
MenuHandle mhdl = ::GetMenu(top);
if(mhdl != 0L)
{
SInt16 menuLength = ::CountMenuItems(mhdl);
LStr255 pstr(title);
::MacInsertMenuItem(mhdl, pstr, menuLength);
}
}
return UMENU_NULL;
#endif
#ifdef qMacAPP
MenuRef menu = MAGetMenu(top);
if(menu)
{
CStr255_AC pstr(title);
gDispatcher->DoMakeMenuBarManager();
TMenuBarManager::fgMenuBarManager->AddMenuItem(pstr, top, ::CountMenuItems(menu), cmd);
}
return UMENU_NULL;
#endif
#ifdef qGTK
UCmdTarget *target = UEventQueryWidget(widid);
if(target == 0L || !target->IsKindOf(URUNTIME_CLASS(UMenu)))
return UMENU_NULL;
UMenu *pmenu = (UMenu *)target;
if(!GTK_IS_MENU_SHELL(top))
return UMENU_NULL;
GtkWidget *parent = (GtkWidget *)top;
GtkWidget *menuitem = gtk_check_menu_item_new_with_label(title);
gtk_widget_show(menuitem);
gtk_container_add(GTK_CONTAINER (parent), menuitem);
addToAccelGroup((GtkAccelGroup *)pmenu->GetAccelGroup(), menuitem, mod, key);
// gtk_tooltips_set_tip (tooltips, quit1, "Quit the application", NULL);
UEventSendMessage(widid, EV_INIT_WIDGET, cmd, menuitem);
return UMENU_NULL;
#endif
#ifdef WIN32
ASSERT(top != UMENU_NULL);
HMENU menuhdl = (HMENU)top;
CMenu parent;
VERIFY(parent.Attach(menuhdl));
parent.AppendMenu(MF_STRING, UCMD_to_WINCMD(cmd), name);
addAccel(UCMD_to_WINCMD(cmd), mod, key);
if(pixmap != 0L && strlen((const char *)pixmap))
{
HBITMAP bmap = UMakeBitmap((const char *)pixmap);
CBitmap b;
VERIFY(b.Attach(bmap));
parent.SetMenuItemBitmaps(UCMD_to_WINCMD(cmd), MF_BYCOMMAND, &b, &b);
b.Detach();
}
parent.Detach();
return UMENU_NULL;
#endif
}
UMENU UMenuAddSeparator(int widid, UMENU top)
{
UASSERT(top != UMENU_NULL);
#ifdef qMacPP
LMenuBar *menuBar = LMenuBar::GetCurrentMenuBar();
LMenu *menu = menuBar->FetchMenu(top);
if(menu)
{
menu->InsertCommand("\p-", 0, max_Int16 - 1);
}
else
{
MenuHandle mhdl = ::GetMenu(top);
if(mhdl != 0L)
{
SInt16 menuLength = ::CountMenuItems(mhdl);
::MacInsertMenuItem(mhdl, "\p-", menuLength);
}
}
return UMENU_NULL;
#endif
#ifdef qMacAPP
MenuRef menu = MAGetMenu(top);
if(menu)
{
CStr255_AC pstr("-");
gDispatcher->DoMakeMenuBarManager();
TMenuBarManager::fgMenuBarManager->AddMenuItem(pstr, top, ::CountMenuItems(menu), 0);
}
return UMENU_NULL;
#endif
#ifdef qGTK
if(!GTK_IS_MENU_SHELL(top))
return UMENU_NULL;
GtkWidget *parent = (GtkWidget *)top;
GtkWidget *menuitem = gtk_menu_item_new();
gtk_widget_show(menuitem);
gtk_container_add(GTK_CONTAINER (parent), menuitem);
gtk_widget_set_sensitive(menuitem, FALSE);
return UMENU_NULL;
#endif
#ifdef WIN32
HMENU menuhdl = (HMENU)top;
CMenu parent;
VERIFY(parent.Attach(menuhdl));
parent.AppendMenu(MF_SEPARATOR);
parent.Detach();
return UMENU_NULL;
#endif
}
void UMenu::OnUpdateUI()
{
// for WIN32, the UpdateUI on the menus is better handled by MFC
#ifndef WIN32
static bool sSemaphore = false;
USemaphore policeman(sSemaphore);
if(policeman.IsEnteredTwice())
return;
// this is the interesting thing about the toolbar : it resends the
// update UI to other widgets according to the focus information and
// allows the update to be handled several times if the handler asks for it
// (by using UCmdUI::SetContinue)
const STD map<int, UCmdTarget *> & targets = UEventGetTargets();
std::map<int, void *>::iterator w;
int cmdid;
for(w = m_widgets.begin(); w != m_widgets.end(); ++w)
{
void *widget = (*w).second;
cmdid = (*w).first;
if(cmdid == kUMainWidget)
continue;
#if qGTK
if(!GTK_IS_MENU_ITEM(widget))
continue;
#endif
// first forward the command to the focused widget : if it doesn't want it
// we give it to the next pseudo widget.
if(sFocusWidID != -1 && sFocusWidID != GetWidID())
{
UCmdUI pUI(cmdid);
pUI.m_widget = widget;
UEventSendMessage(sFocusWidID, EV_UPDTCMD, pUI.m_nID, &pUI);
if(!pUI.CanContinue())
continue;
}
STD map<int, UCmdTarget *>::const_iterator i;
for(i = targets.begin(); i != targets.end(); ++i)
{
int widid = (*i).first;
if(widid == sFocusWidID || widid == GetWidID())
continue;
UCmdUI pUI(cmdid);
pUI.m_widget = widget;
UEventSendMessage(widid, EV_UPDTCMD, pUI.m_nID, &pUI);
if(!pUI.CanContinue())
break;
}
if(i == targets.end())
{
//UAppConsole("The cmd %d of the menubar has no update handler, it's gonna be disabled\n", cmdid);
UCmdUI pUI(cmdid);
pUI.m_widget = widget;
pUI.Enable(false);
}
}
#endif
}
int UMenu::OnCommands(int cmd)
{
static bool sSemaphore = false;
USemaphore policeman(sSemaphore);
if(policeman.IsEnteredTwice())
return 1;
// this is the interesting thing about the toolbar : it resends the
// commands to other widgets according to the focus information and
// allows the command to be handled several times if the handler return 1
const STD map<int, UCmdTarget *> & targets = UEventGetTargets();
int res = 0;
int handledFlag;
// first forward the command to the focused widget : if it doesn't want it
// we give it to the next pseudo widget.
if(sFocusWidID != -1 && sFocusWidID != GetWidID())
{
res = UEventSendMessageExt(sFocusWidID, EV_CMD, cmd, 0L, &handledFlag);
if(res == 0 && handledFlag)
return 0;
}
STD map<int, UCmdTarget *>::const_iterator i;
for(i = targets.begin(); i != targets.end(); ++i)
{
int widid = (*i).first;
if(widid == sFocusWidID || widid == GetWidID())
continue;
res = UEventSendMessageExt(widid, EV_CMD, cmd, 0L, &handledFlag);
if(res == 0 && handledFlag)
return 0;
}
//UAppConsole("The cmd %d of the menubar has no command handler\n", cmd);
return 1;
}
|