1 /**********************************************************************
2  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 2, or (at your option)
6    any later version.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 ***********************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include <X11/Intrinsic.h>
22 #include <X11/StringDefs.h>
23 #include <X11/Xaw/Form.h>
24 #include <X11/Xaw/Label.h>
25 #include <X11/Xaw/Command.h>
26 #include <X11/Xaw/SimpleMenu.h>
27 #include <X11/Xaw/Scrollbar.h>
28 #include <X11/Xaw/Toggle.h>
29 #include <X11/Xaw/Viewport.h>
30 
31 #include "events.h"
32 #include "fcintl.h"
33 #include "game.h"
34 #include "packets.h"
35 #include "player.h"
36 #include "shared.h"
37 #include "support.h"
38 
39 #include "gui_main.h"
40 #include "gui_stuff.h"
41 #include "mapview.h"
42 #include "optiondlg.h"
43 #include "options.h"
44 
45 #include "messagedlg.h"
46 
47 /*************************************************************************/
48 Widget create_messageopt_dialog(void);
49 void messageopt_ok_command_callback(Widget w, XtPointer client_data,
50 			               XtPointer call_data);
51 void messageopt_cancel_command_callback(Widget w, XtPointer client_data,
52 					XtPointer call_data);
53 static Widget messageopt_toggles[E_COUNT][NUM_MW];
54 
55 /**************************************************************************
56 ...
57 **************************************************************************/
popup_messageopt_dialog(void)58 void popup_messageopt_dialog(void)
59 {
60   Widget shell;
61   int i, j, state;
62 
63   shell=create_messageopt_dialog();
64 
65   /* Doing this here makes the "No"'s centered consistently */
66   for(i = 0; i <= event_type_max(); i++) {
67     for(j=0; j<NUM_MW; j++) {
68       state = messages_where[i] & (1<<j);
69       XtVaSetValues(messageopt_toggles[i][j],
70 		    XtNstate, state,
71 		    XtNlabel, state? _("Yes") : _("No"), NULL);
72     }
73   }
74 
75   xaw_set_relative_position(toplevel, shell, 15, 0);
76   XtPopup(shell, XtGrabNone);
77   XtSetSensitive(main_form, FALSE);
78 }
79 
80 /**************************************************************************
81 ...
82 **************************************************************************/
create_messageopt_dialog(void)83 Widget create_messageopt_dialog(void)
84 {
85   Widget shell, form, title, scrolled, explanation, ok, cancel, col;
86   Widget colhead, space_head;
87   Widget label[E_COUNT];
88   Widget longest_label = 0;
89   Widget toggle = 0;
90   int i, longest_len = 0;
91   Dimension width;
92 
93   shell = I_T(XtCreatePopupShell("messageoptpopup", transientShellWidgetClass,
94 				 toplevel, NULL, 0));
95 
96   form = XtVaCreateManagedWidget("messageoptform", formWidgetClass,
97 				 shell, NULL);
98 
99   title = I_L(XtVaCreateManagedWidget("messageopttitle", labelWidgetClass,
100 				      form, NULL));
101 
102   explanation = I_L(XtVaCreateManagedWidget("messageoptexpl", labelWidgetClass,
103 					    form, NULL));
104 
105   scrolled = XtVaCreateManagedWidget("messageoptscroll", viewportWidgetClass,
106                                      form, NULL);
107 
108   col = XtVaCreateManagedWidget("messageoptcol", formWidgetClass,
109                                 scrolled, NULL);
110 
111   /* space_head labels are "empty" labels in column heading which are
112    * used so that we can arrange the constraints without loops.
113    * They essentially act as vertical filler.
114    */
115   space_head = XtVaCreateManagedWidget("messageoptspacehead", labelWidgetClass,
116                                        col, NULL);
117 
118   colhead = I_L(XtVaCreateManagedWidget("messageoptcolhead", labelWidgetClass,
119                                         col, NULL));
120 
121   for(i = 0; i <= event_type_max(); i++)  {
122     const char *text = get_event_message_text(sorted_events[i]);
123     int len = strlen(text);
124 
125     label[i] = XtVaCreateManagedWidget("label", labelWidgetClass, col,
126 				       XtNlabel, text, XtNfromVert,
127 				       (i == 0) ? space_head : label[i - 1],
128 				       NULL);
129 
130     if (len > longest_len) {
131       longest_len = len;
132       longest_label = label[i];
133     }
134 
135     /*
136      * The addition of a scrollbar screws things up. There must be a
137      * better way to do this.
138      */
139     XtVaGetValues(label[i], XtNwidth, &width, NULL);
140     XtVaSetValues(label[i], XtNwidth, width + 15, NULL);
141   }
142 
143   XtVaGetValues(longest_label, XtNwidth, &width, NULL);
144   XtVaSetValues(space_head, XtNwidth, width + 15, NULL);
145   XtVaSetValues(colhead, XtNfromHoriz, space_head, NULL);
146 
147   for (i = 0; i <= event_type_max(); i++) {
148     int j;
149 
150     for (j = 0; j < NUM_MW; j++) {
151       toggle = XtVaCreateManagedWidget("toggle", toggleWidgetClass, col,
152 				       XtNfromHoriz,
153 				       (j == 0 ? space_head : toggle),
154 				       XtNfromVert,
155 				       (i == 0) ? space_head : label[i - 1],
156 				       NULL);
157       XtAddCallback(toggle, XtNcallback, toggle_callback, NULL);
158       messageopt_toggles[sorted_events[i]][j] = toggle;
159     }
160   }
161 
162   ok = I_L(XtVaCreateManagedWidget("messageoptokcommand",
163 				   commandWidgetClass,
164 				   form, NULL));
165 
166   cancel = I_L(XtVaCreateManagedWidget("messageoptcancelcommand",
167 				       commandWidgetClass,
168 				       form, NULL));
169 
170   XtAddCallback(ok, XtNcallback, messageopt_ok_command_callback,
171                 (XtPointer)shell);
172   XtAddCallback(cancel, XtNcallback, messageopt_cancel_command_callback,
173                 (XtPointer)shell);
174 
175   XtRealizeWidget(shell);
176 
177   xaw_horiz_center(title);
178   xaw_horiz_center(explanation);
179 
180   return shell;
181 }
182 
183 /**************************************************************************
184 ...
185 **************************************************************************/
messageopt_cancel_command_callback(Widget w,XtPointer client_data,XtPointer call_data)186 void messageopt_cancel_command_callback(Widget w, XtPointer client_data,
187 					XtPointer call_data)
188 {
189   XtSetSensitive(main_form, TRUE);
190   XtDestroyWidget((Widget)client_data);
191 }
192 
193 /**************************************************************************
194 ...
195 **************************************************************************/
messageopt_ok_command_callback(Widget w,XtPointer client_data,XtPointer call_data)196 void messageopt_ok_command_callback(Widget w, XtPointer client_data,
197 			               XtPointer call_data)
198 {
199   int i, j;
200   Boolean b;
201 
202   XtSetSensitive(main_form, TRUE);
203 
204   for(i = 0; i <= event_type_max(); i++)  {
205     messages_where[i] = 0;
206     for(j=0; j<NUM_MW; j++) {
207       XtVaGetValues(messageopt_toggles[i][j], XtNstate, &b, NULL);
208       if (b) messages_where[i] |= (1<<j);
209     }
210   }
211 
212   XtDestroyWidget((Widget)client_data);
213 }
214