-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotif5.c
211 lines (168 loc) · 5.26 KB
/
motif5.c
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
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <Xm/MainW.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
struct ui_main_window
{
Widget top_level_shell_widget;
Widget widget;
};
typedef struct ui_main_window *ui_main_window_t;
void ui_main_window_free(ui_main_window_t main_window);
/*******************************************************************************
* ui_application
******************************************************************************/
struct ui_application
{
XtAppContext xt_app_context;
Widget session_shell_widget;
ui_main_window_t main_window;
};
typedef struct ui_application *ui_application_t;
int ui_application_alloc(ui_application_t *application_p, const char *name, int argc, char *argv[])
{
if (application_p == NULL || name == NULL)
{
return EINVAL;
}
if ((*application_p = calloc(1, sizeof(struct ui_application))) == NULL)
{
return ENOMEM;
}
(*application_p)->session_shell_widget = XtOpenApplication(&((*application_p)->xt_app_context),
name, NULL, 0, &argc,
argv, NULL, sessionShellWidgetClass,
NULL, 0);
printf("ui_application_alloc\n");
return 0;
}
void ui_application_free(ui_application_t application)
{
if (application == NULL)
{
return;
}
free(application);
printf("ui_application_free\n");
}
void ui_application_exit(ui_application_t application)
{
if (application == NULL)
{
return;
}
XtAppSetExitFlag(application->xt_app_context);
if (application->main_window != NULL)
{
ui_main_window_free(application->main_window);
}
ui_application_free(application);
}
void ui_application_run(ui_application_t application)
{
XtAppMainLoop(application->xt_app_context);
}
void __ui_application_destroy_cb(Widget w, XtPointer clientData,
XtPointer callData)
{
ui_application_exit(g_ui_application_run);
}
int ui_application_register_main_window(ui_application_t application, ui_main_window_t main_window)
{
if (application == NULL || application->main_window != NULL)
{
return EINVAL;
}
application->main_window = main_window;
XtAddCallback(application->main_window->widget, XmNdestroyCallback, __ui_application_destroy_cb, NULL);
}
/*******************************************************************************
* ui_shared_application
******************************************************************************/
static ui_application_t s_ui_shared_application;
int ui_shared_application_init(const char *name, int argc, char *argv[])
{
if (s_ui_shared_application != NULL)
{
return 0;
}
return ui_application_alloc(&s_ui_shared_application,
name, argc, argv);
}
void ui_shared_application_run()
{
ui_application_run(s_ui_shared_application);
}
Widget ui_shared_application_widget()
{
if (s_ui_shared_application == NULL)
{
return NULL;
}
return s_ui_shared_application->session_shell_widget;
}
/*******************************************************************************
* ui_main_window
******************************************************************************/
int ui_main_window_alloc(ui_main_window_t *main_window_p, const char *name, const char *title)
{
if (main_window_p == NULL || name == NULL || title == NULL || g_ui_application_run == NULL)
{
return EINVAL;
}
if ((*main_window_p = calloc(1, sizeof(struct ui_main_window))) == NULL)
{
return ENOMEM;
}
printf("ui_main_window_alloc\n");
return 0;
}
int ui_main_window_init(ui_main_window_t *main_window_p, const char *name, const char *title)
{
int err = 0;
if ((err = ui_main_window_alloc(main_window_p, name, title)) != 0)
{
return err;
}
/* Create a top level shell for the window */
(*main_window_p)->top_level_shell_widget = XtVaCreatePopupShell(
"main_window_shell", topLevelShellWidgetClass, g_ui_application_run->session_shell_widget,
XmNtitle, title, NULL);
/* Create a new main window widget */
(*main_window_p)->widget = XmCreateMainWindow((*main_window_p)->top_level_shell_widget, (char *)name, NULL, 0);
XtManageChild((*main_window_p)->widget);
/* Add the new window to the application window list */
return ui_application_register_main_window(g_ui_application_run, *main_window_p);
}
void ui_main_window_free(ui_main_window_t main_window)
{
if (main_window == NULL)
{
return;
}
free(main_window);
printf("ui_main_window_free\n");
}
void ui_main_window_show(ui_main_window_t main_window)
{
XtPopup(main_window->top_level_shell_widget, XtGrabNonexclusive);
}
int main(int argc, char *argv[])
{
int err = 0;
ui_application_t app;
ui_main_window_t win;
if ((err = ui_application_init_shared(&app, "my_app", argc, argv)) != 0)
{
return err;
}
if ((err = ui_main_window_init(&win, "main_win", "Hello World!")) != 0)
{
return err;
}
ui_main_window_show(win);
ui_application_run(app);
return 0;
}