1 /*===========================================================================
2 *
3 * PUBLIC DOMAIN NOTICE
4 * National Center for Biotechnology Information
5 *
6 * This software/database is a "United States Government Work" under the
7 * terms of the United States Copyright Act. It was written as part of
8 * the author's official duties as a United States Government employee and
9 * thus cannot be copyrighted. This software/database is freely available
10 * to the public for use. The National Library of Medicine and the U.S.
11 * Government have not placed any restriction on its use or reproduction.
12 *
13 * Although all reasonable efforts have been taken to ensure the accuracy
14 * and reliability of the software and data, the NLM and the U.S.
15 * Government do not and cannot warrant the performance or results that
16 * may be obtained by using this software or data. The NLM and the U.S.
17 * Government disclaim all warranties, express or implied, including
18 * warranties of performance, merchantability or fitness for any particular
19 * purpose.
20 *
21 * Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26
27 #include <klib/rc.h>
28 #include <klib/text.h>
29 #include <tui/tui_dlg.h>
30 #include "tui_widget.h"
31
lenght_without_first_ampersand(const char * s)32 static uint32_t lenght_without_first_ampersand( const char * s )
33 {
34 size_t s_cap;
35 uint32_t l_cap = string_measure( s, &s_cap );
36 if ( l_cap > 0 )
37 {
38 char * ampersand = string_chr ( s, s_cap, '&' );
39 if ( ampersand != NULL ) l_cap--;
40 }
41 return l_cap;
42 }
43
draw_button(struct KTUIWidget * w)44 void draw_button( struct KTUIWidget * w )
45 {
46 tui_rect r;
47 rc_t rc = KTUIDlgAbsoluteRect ( w -> dlg, &r, &( w -> r ) );
48 if ( rc == 0 )
49 {
50 tui_ac ac;
51 rc = GetWidgetAc( w, ktuipa_button, &ac );
52 if ( rc == 0 )
53 {
54 struct KTUI * tui = w -> tui;
55 uint32_t ofs_y = r.h / 2;
56 uint32_t x = r . top_left . x;
57 uint32_t y = r . top_left . y + ofs_y;
58 uint32_t w1 = r . w;
59
60 rc = draw_background( tui, w -> focused, &( r . top_left ), w1, r . h, ac . bg );
61
62 if ( rc == 0 && w -> caption != NULL )
63 {
64 tui_ac hl_ac; // the highlighted style
65 rc = GetWidgetHlAc( w, ktuipa_button, &hl_ac );
66 if ( rc == 0 )
67 {
68 uint32_t x_txt = x + 2;
69 uint32_t w_txt = w1 - 4;
70 uint32_t l_cap = lenght_without_first_ampersand( w -> caption );
71 if ( l_cap < w_txt )
72 {
73 x_txt += ( ( w_txt - l_cap ) / 2 );
74 w_txt = l_cap;
75 }
76 rc = draw_highlighted( tui, x_txt, y, w_txt, &ac, &hl_ac, w -> caption );
77 }
78 }
79
80 if ( rc == 0 )
81 rc = DlgWrite( tui, x + 1, y, &ac, "[", 1 );
82 if ( rc == 0 )
83 rc = DlgWrite( tui, x + w1 - 2, y, &ac, "]", 1 );
84 }
85 }
86 }
87
event_button(struct KTUIWidget * w,tui_event * event,bool hotkey)88 bool event_button( struct KTUIWidget * w, tui_event * event, bool hotkey )
89 {
90 bool res = hotkey;
91 if ( !res && event -> event_type == ktui_event_kb )
92 {
93 switch( event -> data . kb_data . code )
94 {
95 case ktui_enter : res = true; break;
96 case ktui_alpha : res = event -> data . kb_data . key == ' '; break;
97 }
98 }
99
100 if ( res )
101 KTUIDlgPushEvent( w -> dlg, ktuidlg_event_select, w -> id, 0, 0, NULL );
102 return res;
103 }
104