1/*
2 * Copyright (c) 2008 Apple Computer, Inc.  All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License").  You may not use this file except in compliance with the
9 * License.  Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <curses.h>
26#include "preferences.h"
27#include "userinput_secondary_order.h"
28
29static void order_completion(void *tinst, struct user_input_state *s) {
30    if(!strlen(s->buf)) {
31	/* Use the current order. */
32	user_input_set_state(NULL);
33	return;
34    }
35
36    if(top_prefs_set_secondary_sort(s->buf)) {
37	char errbuf[60];
38	if(-1 == snprintf(errbuf, sizeof(errbuf), "invalid order: %s\n",
39			  s->buf)) {
40	    user_input_set_error_state("order buffer overflow");
41	    return;
42	}
43	user_input_set_error_state(errbuf);
44	return;
45    }
46
47    user_input_set_state(NULL);
48}
49
50static void order_draw(void *tinst, struct user_input_state *s, WINDOW *win,
51		       int row, int column) {
52    char display[60];
53
54    if(-1 == snprintf(display, sizeof(display), "secondary key [%c%s]: %s\n",
55		      top_prefs_get_secondary_ascending() ? '+' : '-',
56		      top_prefs_get_secondary_sort_string(),
57		      s->buf))
58	return;
59
60    mvwaddstr(win, row, column, display);
61}
62
63struct user_input_state top_user_input_secondary_order_state = {
64    .offset = 0,
65    .completion = order_completion,
66    .draw = order_draw
67};
68