1 /*
2  *  R : A Computer Language for Statistical Data Analysis
3  *  Copyright (C) 1998--1999  Guido Masarotto
4  *  Copyright (C) 2004 The R Foundation
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, a copy is available at
18  *  https://www.R-project.org/Licenses/
19  */
20 
21 /*
22    Pop-up menus support + convenience methods for definining
23    menubar (and pop-up) from static array of MenuItem (defined in
24    ga.h)
25  */
26 
27 #include "win-nls.h"
28 #include "internal.h"
29 
mdimenu(menuitem m)30 static void mdimenu(menuitem m)
31 {
32     switch (getvalue(m)) {
33     case 1:
34 	SendMessage(hwndClient,WM_MDICASCADE,0,0);
35 	break;
36     case 2:
37 	SendMessage(hwndClient,WM_MDITILE,MDITILE_HORIZONTAL,0);
38 	break;
39     case 3:
40 	SendMessage(hwndClient,WM_MDITILE,MDITILE_VERTICAL,0);
41 	break;
42     case 4:
43 	SendMessage(hwndClient,WM_MDIICONARRANGE,0,0);
44 	break;
45     }
46 }
47 
newmdimenu()48 menu newmdimenu()
49 {
50     menu m ;
51     if (!ismdi()) return NULL;
52     m = newmenu(G_("Windows"));
53     setvalue(newmenuitem(G_("Cascade"),0,mdimenu),1);
54     setvalue(newmenuitem(G_("Tile &Horizontally"),0,mdimenu),2);
55     setvalue(newmenuitem(G_("Tile &Vertically"),0,mdimenu),3);
56     setvalue(newmenuitem(G_("Arrange Icons"),0,mdimenu),4);
57     current_menubar->menubar = m;
58     return m;
59 }
60 
newpopup(actionfn fn)61 menu newpopup(actionfn fn)
62 {
63     menu mnew;
64     if (!current_window) return 0;
65     mnew = newsubmenu(current_window,"");
66     mnew->action = fn;
67     current_window->popup = mnew;
68     current_menu = mnew;
69     return mnew;
70 }
71 
gchangepopup(window w,menu m)72 void gchangepopup(window w, menu m)
73 {
74     w->popup = m;
75 }
76 
gchangemenubar(menubar mb)77 void gchangemenubar(menubar mb)
78 {
79     window w = current_window;
80     if (!w) return;
81     w->menubar = mb;
82     SetMenu(w->handle, mb->handle);
83     if (ismdi()) {
84 	menu mdi = (w->menubar)->menubar;
85 	SendMessage(hwndClient, WM_MDISETMENU,
86 		    (WPARAM)w->menubar->handle,
87 		    (LPARAM)(mdi ? (mdi->handle) : 0));
88 	DrawMenuBar(hwndFrame);
89     } else
90 	DrawMenuBar(w->handle);
91 }
92 
93 /* FIXME: only one level of sub-menu - no checks -*/
addmenuitemarray(menu m,MenuItem a[])94 static int addmenuitemarray(menu m,MenuItem a[])
95 {
96     menu ma  = m;
97     int i = 0;
98     while (a[i].nm) {
99 	if (!strcmp(a[i].nm,"@STARTMENU")) {
100 	    i += 1;
101 	    ma = newsubmenu(m, G_(a[i].nm));
102 	}
103 	else if (!strcmp(a[i].nm,"@ENDMENU")) {
104 	    i += 1;
105 	    ma = m;
106 	}
107 	else if (!strcmp(a[i].nm,"@STARTSUBMENU")) {
108 	    i += 1;
109 	    newsubmenu(ma,a[i].nm);
110 	}
111 	else if (!strcmp(a[i].nm,"@ENDSUBMENU")) {
112 	    i += 1;
113 	}
114 	else if (!strcmp(a[i].nm,"@MDIMENU")) {
115 	    if (!(a[i].m = newmdimenu())) return 0;
116 	    ma = a[i].m;
117 	}
118 	else {
119 	    if (!(a[i].m = newmenuitem(G_(a[i].nm), a[i].key, a[i].fn))) return 0;
120 	}
121 	i += 1;
122     }
123     return 1;
124 }
125 
gmenubar(actionfn fn,MenuItem a[])126 menubar gmenubar(actionfn fn, MenuItem a[])
127 {
128     menubar m = newmenubar(fn);
129     if (m)  {
130 	if (!addmenuitemarray(m,a)) {
131 	    del(m);
132 	    m = NULL;
133 	}
134     }
135     return m;
136 }
137 
138 
gpopup(actionfn fn,MenuItem a[])139 popup gpopup(actionfn fn, MenuItem a[])
140 {
141     popup m = newpopup(fn);
142     if (m)  {
143 	if (!addmenuitemarray(m,a)) {
144 	    del(m);
145 	    m = NULL;
146 	}
147     }
148     return m;
149 }
150