11 #include <sys/types.h>
14 #include <X11/Intrinsic.h>
15 #include <X11/StringDefs.h>
16 #include <X11/Shell.h>
18 #include <X11/Xaw/Form.h>
19 #include <X11/Xaw/Viewport.h>
20 #include <X11/Xaw/Command.h>
21 #include <X11/Xaw/AsciiText.h>
22 #include <X11/Xaw/Label.h>
23 #include <X11/Xaw/Box.h>
25 #include "md5_utils.h"
26 #include "string_utils.h"
27 #include "file_utils.h"
30 #include "icons_utils.h"
32 #include "iv_widget.h"
37 XtAppContext ctx
; /* Application context */
38 Widget shell
; /* Application shell widget */
39 Widget form
; /* Application top Form widget */
42 static struct mail_list
*mlist
;
44 void list_add_from_file(const char *filename
)
47 struct mail_entry entry
;
49 /* Check if is a regular file */
50 if (file_type(filename
)) {
54 fh
= fopen(filename
, "r");
58 strncpy(entry
.hdr
.filename
, filename
, LINE_LENGTH
);
60 parse_mail_header(fh
, &entry
.hdr
);
65 if (list_add_entry(mlist
, &entry
)) {
66 fprintf(stderr
, "%s - No free slot for %s\n", __func__
, filename
);
68 /* fprintf(stderr, "%s - File %s added to list\n", __func__, filename); */
74 /*======================================================================
76 *======================================================================*/
78 /* The callback function for the quit button. */
79 void quit_callback(Widget w
,
80 __attribute__((unused
)) XtPointer client_data
,
81 __attribute__((unused
)) XtPointer call_data
)
85 XtDestroyApplicationContext(XtWidgetToApplicationContext(w
));
90 static void fix_scrollbar_translations(Widget w
, String name
)
92 Widget sb
= NULL
; /* scrollbar widget */
94 /* New scrollbar translation */
95 String trans
= "#override "
96 "<Btn1Down>: StartScroll(Forward)\n" /* Left button */
97 "<Btn2Down>: \n" /* Middle button */
98 "<Btn3Down>: StartScroll(Backward)\n" /* Right button */
99 "<Btn4Down>: StartScroll(Backward)\n" /* Wheel down */
100 "<Btn5Down>: StartScroll(Forward)\n" /* Wheel up */
101 "<Btn1Motion>: MoveThumb() NotifyThumb()\n"
102 "<BtnUp>: NotifyScroll(Proportional) EndScroll()";
107 sb
= XtNameToWidget(w
, name
);
109 XtOverrideTranslations(sb
, XtParseTranslationTable(trans
));
111 fprintf(stderr
, "%s: WARNING cannon set %s translation!\n", __func__
, name
);
115 /* destroy_popup: Destroys the popup box widget.
117 * w - *** UNUSED ***.
118 * box - the box widget. This widget is a direct child of the popup shell to destroy.
121 static void destroy_popup(__attribute__((unused
)) Widget w
,
123 __attribute__((unused
)) XtPointer p
)
127 popup
= XtParent((Widget
) box
);
129 XtDestroyWidget(popup
);
132 static void create_popup(Widget w
, XtPointer client_data
,
133 __attribute__((unused
)) XtPointer call_data
)
144 struct mail_entry
*me
= (struct mail_entry
*) client_data
;
146 popup
= XtVaCreatePopupShell("PopupText", transientShellWidgetClass
, w
,
153 /* PopupBox contains Label and Slider */
154 box
= XtVaCreateManagedWidget("PopupForm", formWidgetClass
, popup
,
158 address
= XtVaCreateManagedWidget("Address", labelWidgetClass
, box
,
161 XtNlabel
, me
->hdr
.from
,
162 XtNjustify
, XtJustifyLeft
,
165 date
= XtVaCreateManagedWidget("date", labelWidgetClass
, box
,
166 XtNfromHoriz
, address
,
169 XtNlabel
, rfc822_mkdt(me
->hdr
.date
),
170 XtNjustify
, XtJustifyLeft
,
173 subj
= XtVaCreateManagedWidget("subj", labelWidgetClass
, box
,
177 XtNlabel
, me
->hdr
.subject
,
178 XtNjustify
, XtJustifyLeft
,
181 text
= XtVaCreateManagedWidget("Text", asciiTextWidgetClass
, box
,
184 XtNtype
, XawAsciiFile
,
185 XtNstring
, me
->hdr
.filename
,
188 XtNdisplayCaret
, False
,
189 XtNscrollHorizontal
, XawtextScrollWhenNeeded
,
190 XtNscrollVertical
, XawtextScrollAlways
,
191 XtNeditType
, XawtextRead
,
195 ok_btn
= XtVaCreateManagedWidget("Ok", commandWidgetClass
, box
,
197 XtNleft
, XtChainLeft
,
198 XtNright
, XtChainLeft
,
199 XtNtop
, XtChainBottom
,
200 XtNbottom
, XtChainBottom
,
204 XtAddCallback(ok_btn
, XtNcallback
, destroy_popup
, (XtPointer
) box
);
207 XtPopup(popup
, XtGrabNone
);
209 fprintf(stderr
, "%s box=%p popup=%p\n", __func__
, (void *) box
, (void *) popup
);
213 static int create_applet(struct applet
*app
, int argc
, char **argv
)
215 XtSetLanguageProc(NULL
, (XtLanguageProc
) NULL
, NULL
);
217 /* Open Xt application shell widget */
218 app
->shell
= XtVaOpenApplication(&app
->ctx
, "TestListX11", NULL
, 0,
220 applicationShellWidgetClass
,
223 /* Create top form widget */
224 app
->form
= XtVaCreateManagedWidget("AppForm", formWidgetClass
,
229 /* The callback function for the quit button. */
230 void item_callback(Widget w
, XtPointer item_string
, XtPointer call_data
)
232 create_popup(w
, item_string
, call_data
);
235 static int list_add_item(Widget parent
, const struct mail_entry
*item
)
241 /* Depth of the root window */
253 d
= XtDisplay(parent
);
255 screen
= DefaultScreen(d
);
256 depth
= DisplayPlanes(d
, screen
);
257 visual
= XDefaultVisual(d
, screen
);
259 /* Allocated icon pathname */
260 icon
= icon_get_path((char *) item
->hdr
.addr_md5
);
262 /* Create XImage from PNG file */
263 pix
= create_ximage_from_PNG(d
, visual
, depth
, icon
, NULL
);
265 /* Free allocated icon pathname with 'icon_get_path()' */
266 icon_free_path(icon
);
268 box_h
= XtVaCreateManagedWidget("box_h", boxWidgetClass
, parent
,
269 XtNorientation
, XtorientHorizontal
,
272 iv
= XtVaCreateManagedWidget("IV", imageViewWidgetClass
, box_h
,
277 box_v
= XtVaCreateManagedWidget("box_v", boxWidgetClass
, box_h
,
279 XtNorientation
, XtorientVertical
,
282 addr
= XtVaCreateManagedWidget("from", labelWidgetClass
, box_v
,
285 XtNlabel
, item
->hdr
.from
,
286 XtNjustify
, XtJustifyLeft
,
289 subj
= XtVaCreateManagedWidget("subj", labelWidgetClass
, box_v
,
292 XtNlabel
, item
->hdr
.subject
,
293 XtNjustify
, XtJustifyLeft
,
296 /* Set handler for mouse button press on widgets
297 * Pass the file to the callbacks
303 * All the above ButtonPressMask
306 XtAddEventHandler(addr
, Button1Mask
, False
, (XtEventHandler
) item_callback
, (XtPointer
) item
);
307 XtAddEventHandler(subj
, Button1Mask
, False
, (XtEventHandler
) item_callback
, (XtPointer
) item
);
308 XtAddEventHandler(iv
, Button1Mask
, False
, (XtEventHandler
) item_callback
, (XtPointer
) item
);
310 XtAddEventHandler(addr
, ButtonPressMask
, False
, (XtEventHandler
) item_callback
, (XtPointer
) item
);
311 XtAddEventHandler(subj
, ButtonPressMask
, False
, (XtEventHandler
) item_callback
, (XtPointer
) item
);
312 XtAddEventHandler(iv
, ButtonPressMask
, False
, (XtEventHandler
) item_callback
, (XtPointer
) item
);
318 int xutil_main(struct mail_list
*lst
, int argc
, char **argv
)
324 Widget vport
; /* ViewPort (list container) */
328 rc
= create_applet(&app
, argc
, argv
);
330 vport
= XtVaCreateManagedWidget("vport", viewportWidgetClass
,
335 XtNallowVert
, True
, /* Display Vertical scrollbar */
336 XtNallowHoriz
, True
, /* Display Horizontal scrollbar */
337 XtNuseBottom
, True
, /* Horizontal scrollbar at bottom */
343 box
= XtVaCreateManagedWidget("box", boxWidgetClass
, vport
,
344 XtNorientation
, XtorientVertical
,
348 for (n
= 0; n
< lst
->n
; n
++) {
349 list_add_item(box
, &lst
->list
[n
]);
353 button
= XtVaCreateManagedWidget("quit", commandWidgetClass
,
356 XtNleft
, XtChainLeft
,
357 XtNright
, XtChainLeft
,
358 XtNtop
, XtChainBottom
,
359 XtNbottom
, XtChainBottom
,
363 XtAddCallback(button
, XtNcallback
, quit_callback
, NULL
);
365 XtRealizeWidget(app
.shell
);
367 /* Override default translations (A.k.a. bind actions to widgets) */
368 fix_scrollbar_translations(vport
, "horizontal");
369 fix_scrollbar_translations(vport
, "vertical");
371 XtAppMainLoop(app
.ctx
);
376 /*======================================================================*/
378 /*======================================================================*/
380 int main(int argc
, char **argv
)
383 char old_path
[PATH_MAX
];
390 path
= "/home/roberto/Maildir/cur/";
395 if (!getcwd(old_path
, PATH_MAX
)) {
396 fprintf(stderr
, "%s - GETCWD: %s\n", __func__
, strerror(errno
));
400 if (chdir(path
) < 0) {
401 fprintf(stderr
, "%s - CHDIR: %s\n", __func__
, strerror(errno
));
405 n
= dir_entries(path
);
407 fprintf(stderr
, "%s is empty!\n", path
);
413 mlist
= list_create(n
, LIST_LIMIT
);
419 fprintf(stderr
, "Try to process %lu files\n", (unsigned long) mlist
->n
);
421 rc
= scan_dir(path
, list_add_from_file
);
423 /*int*/ xutil_main(mlist
, argc
, argv
);
428 if (chdir(old_path
) < 0) {
429 fprintf(stderr
, "%s - Cannot restore path %s!\n", __func__
, strerror(errno
));