tk 0.1.5

Rust bindings for Tk GUI library
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use crate::{
    InterpResult,
    NOT_SEND_SYNC,
    PathOptsWidgets,
    Tk,
    TkInstance,
    TkOption,
    UpcastableWidget,
    Widget,
    CreatedWidgets,
    opt::{
        TkMenuOpt,
        TkMenuEntryOpt,
        OptPair,
    },
    range::{
        TkDefaultEnd,
        TkDefaultStart,
        TkRange,
    },
    traits::Delete,
};

use heredom::{DomForest, Visit};

use std::{
    ops::{
        Deref,
        RangeFrom,
        RangeInclusive,
        RangeToInclusive,
    },
    os::raw::{
        c_int,
        c_longlong,
    },
};

use tcl::Obj;

use tuplex::*;

#[derive( Copy, Clone )]
pub struct TkMenu<Inst:TkInstance>( pub(crate) Widget<Inst> );

/// Many of the methods of a menu take as one argument an indicator of which
/// entry of the menu to operate on. These indicators are called `Index`es
/// and may be specified in any of the following forms:
///
/// - `Index::Active`
///
/// Indicates the entry that is currently active. If no entry is active then
/// this form is equivalent to none.
///
/// - `Index::End`
///
/// Indicates the bottommost entry in the menu. If there are no entries in the
/// menu then this form is equivalent to none.
///
/// - `Index::None`
///
/// Indicates "no entry at all"; this is used most commonly with the activate
/// option to deactivate all the entries in the menu. In most cases the
/// specification of none causes nothing to happen in the widget command.
///
/// - `Index::At`
///
/// In this form, number is treated as a y-coordinate in the menu's window; the
/// entry closest to that y-coordinate is used. For example, `Index::At(0)`
/// indicates the top-most entry in the window.
///
/// - `Index::Number`
///
/// Specifies the entry numerically, where 0 corresponds to the top-most entry
/// of the menu, 1 to the entry below it, and so on.
///
/// - `Index::Pattern`
///
/// Pattern is pattern-matched against the label of each entry in the menu, in
/// order from the top down, until a matching entry is found. The rules of
/// string match are used.
#[derive( Clone, Debug, PartialEq )]
pub enum Index {
    Active,
    End,
    None,
    At( c_int ),
    Number( c_int ),
    Pattern( String ),
}

impl Index {
    pub fn pattern( pattern: &str ) -> Self { Index::Pattern( pattern.to_owned() )}
}

impl From<c_int> for Index {
    fn from( number: c_int ) -> Self { Index::Number( number )}
}

impl TkDefaultStart for Index {
    fn default_start() -> Self { Index::Number(0) }
}

impl TkDefaultEnd for Index {
    fn default_end() -> Self { Index::End }
}

impl From<RangeFrom<c_int>> for TkRange<Index> { // a..
    fn from( r: RangeFrom<c_int> ) -> Self {
        TkRange {
            start : Index::Number( r.start ),
            end   : Index::default_end()
        }
    }
}

impl From<RangeInclusive<c_int>> for TkRange<Index> { // a..=b
    fn from( r: RangeInclusive<c_int> ) -> Self {
        TkRange {
            start : Index::Number( *r.start() ),
            end   : Index::Number( *r.end() )
        }
    }
}

impl From<RangeToInclusive<c_int>> for TkRange<Index> { // ..=b
    fn from( r: RangeToInclusive<c_int> ) -> Self {
        TkRange {
            start : Index::default_start(),
            end   : Index::Number( r.end ),
        }
    }
}

impl From<Index> for Obj {
    fn from( index: Index ) -> Obj {
        use Index::*;
        match index {
            Active       => "active".into(),
            End          => "end".into(),
            None         => "none".into(),
            At( n )      => format!( "@{}", n ).into(),
            Number( n )  => n.into(),
            Pattern( p ) => p.into(),
        }
    }
}

macro_rules! pub_fn_add {
    ($name:expr, $ident:ident) => {
        pub fn $ident<Opts>( &self, opts: impl Into<PathOptsWidgets<Opts,()>> ) -> InterpResult<()>
            where Opts: IntoHomoTuple<TkMenuEntryOpt>
                      + IntoHomoTuple<OptPair>
        {
            let mut command = Vec::<Obj>::with_capacity( <<Opts as IntoHomoTuple<OptPair>>::Output as tuplex::Len>::LEN * 2 + 3 );
            command.push( self.path.into() );
            command.push( "add".into() );
            command.push( $name.into() );
            crate::cmd::append_opts( &mut command, opts.into().opts );
            self.tk().run( command )
        }
    };
}

macro_rules! pub_fn_insert {
    ($name:expr, $ident:ident) => {
        pub fn $ident<Opts>( &self, index: impl Into<Index>, opts: impl Into<PathOptsWidgets<Opts,()>> ) -> InterpResult<()>
            where Opts: IntoHomoTuple<TkMenuEntryOpt>
                      + IntoHomoTuple<OptPair>
        {
            let mut command = Vec::<Obj>::with_capacity( <<Opts as IntoHomoTuple<OptPair>>::Output as tuplex::Len>::LEN * 2 + 4 );
            command.push( self.path.into() );
            command.push( "insert".into() );
            command.push( index.into().into() );
            command.push( $name.into() );
            crate::cmd::append_opts( &mut command, opts.into().opts );
            self.tk().run( command )
        }
    };
}

/// Used in `TkMenu::clone()` method
pub enum TkMenuCloneType {
    Normal,
    MenuBar,
    TearOff,
}

impl From<TkMenuCloneType> for Obj {
    fn from( menu_clone_type: TkMenuCloneType ) -> Obj {
        match menu_clone_type {
            TkMenuCloneType::Normal  => "normal" .into(),
            TkMenuCloneType::MenuBar => "menubar".into(),
            TkMenuCloneType::TearOff => "tearoff".into(),
        }
    }
}

pub enum TkMenuEntryType {
    Cascade,
    CheckButton,
    Command,
    RadioButton,
    Separator,
    TearOff,
}

impl<Inst:TkInstance> self::TkMenu<Inst> {
    pub fn activate( &self, index: impl Into<Index> ) -> InterpResult<()> {
        self.0.tk().run(( self.0.path, "activate", index.into() ))
    }

    pub_fn_add!( "cascade"    , add_cascade     );
    pub_fn_add!( "checkbutton", add_checkbutton );
    pub_fn_add!( "command"    , add_command     );
    pub_fn_add!( "radiobutton", add_radiobutton );
    pub_fn_add!( "separator"  , add_separator   );

    pub fn clone( &self, new_path_name: String, menu_clone_type: TkMenuCloneType ) -> InterpResult<Self> {
        self.0.tk().run(( self.0.path, "clone", new_path_name.as_str(), menu_clone_type ))?;
        Ok( self::TkMenu( Widget{
            path : Tk::<Inst>::make_or_get_path( &new_path_name ),
            inst : self.0.inst,
            mark : NOT_SEND_SYNC,
        }))
    }

    pub fn entrycget<Opt>( &self, index: impl Into<Index>, _name_fn: fn(Obj)->Opt ) -> InterpResult<Obj>
        where Opt : TkOption
                  + Into<TkMenuEntryOpt>
    {
        self.0.tk().eval(( self.0.path, "entrycget", index.into(), <Opt as TkOption>::NAME ))
    }

    pub fn entryconfigure<Opts>( &self, index: impl Into<Index>, opts: impl Into<PathOptsWidgets<Opts,()>> ) -> InterpResult<()>
        where Opts: IntoHomoTuple<TkMenuEntryOpt>
                  + IntoHomoTuple<OptPair>
    {
        let mut command = Vec::<Obj>::with_capacity( <<Opts as IntoHomoTuple<OptPair>>::Output as tuplex::Len>::LEN * 2 + 3 );
        command.push( self.path.into() );
        command.push( "entryconfigure".into() );
        command.push( index.into().into() );
        crate::cmd::append_opts( &mut command, opts.into().opts );
        self.tk().run( command )
    }

    pub fn entryconfigure_options( &self, index: impl Into<Index> ) -> InterpResult<Obj> {
        self.tk().eval(( self.path, "entryconfigure", index.into() ))
    }

    pub fn index( &self, index: impl Into<Index> ) -> InterpResult<Option<c_longlong>> {
        let index = index.into();
        if index == Index::None {
            Ok( None )
        } else {
            let obj = self.0.tk().eval(( self.0.path, "index", index ))?;
            self.0.tk().longlong( obj ).map( Some )
        }
    }

    pub_fn_insert!( "cascade"    , insert_cascade     );
    pub_fn_insert!( "checkbutton", insert_checkbutton );
    pub_fn_insert!( "command"    , insert_command     );
    pub_fn_insert!( "radiobutton", insert_radiobutton );
    pub_fn_insert!( "separator"  , insert_separator   );

    pub fn invoke( &self, index: impl Into<Index> ) -> InterpResult<Obj> {
        self.0.tk().eval(( self.0.path, "invoke", index.into() ))
    }

    pub fn post( &self, x: c_int, y: c_int ) -> InterpResult<()> {
        self.0.tk().run(( self.0.path, "post", x, y ))
    }

    pub fn post_entry( &self, x: c_int, y: c_int, index: impl Into<Index> ) -> InterpResult<()> {
        self.0.tk().run(( self.0.path, "post", x, y, index.into() ))
    }

    pub fn postcascade( &self, index: impl Into<Index> ) -> InterpResult<()> {
        self.0.tk().run(( self.0.path, "postcascade", index.into() ))
    }

    pub fn type_( &self, index: impl Into<Index> ) -> InterpResult<Option<TkMenuEntryType>> {
        Ok( match self.0.tk().eval(( self.0.path, "type", index.into() ))?.to_string().as_str() {
            "cascade"     => Some( TkMenuEntryType::Cascade ),
            "checkbutton" => Some( TkMenuEntryType::CheckButton ),
            "command"     => Some( TkMenuEntryType::Command ),
            "radiobutton" => Some( TkMenuEntryType::RadioButton ),
            "separator"   => Some( TkMenuEntryType::Separator ),
            "tearoff"     => Some( TkMenuEntryType::TearOff ),
            _             => None,
        })
    }

    /// Unmap the window so that it is no longer displayed. If a lower-level
    /// cascaded menu is posted, unpost that menu.
    /// This subcommand does not work on Windows and the Macintosh, as those
    /// platforms have their own way of unposting menus.
    pub fn unpost( &self ) -> InterpResult<()> {
        self.0.tk().run(( self.0.path, "unpost" ))
    }

    /// Returns the x-coordinate within the menu window of the leftmost pixel
    /// in the entry specified by index.
    pub fn xposition( &self, index: impl Into<Index> ) -> InterpResult<c_longlong> {
        let obj = self.0.tk().eval(( self.0.path, "xposition", index.into() ))?;
        self.0.tk().longlong( obj )
    }

    /// Returns the y-coordinate within the menu window of the topmost pixel
    /// in the entry specified by index.
    pub fn yposition( &self, index: impl Into<Index> ) -> InterpResult<c_longlong> {
        let obj = self.0.tk().eval(( self.0.path, "yposition", index.into() ))?;
        self.0.tk().longlong( obj )
    }
}

impl<Inst:TkInstance> Delete<Inst> for TkMenu<Inst> {
    type Index = Index;
}

def_functions! {
    cascade     CascadeFn       ;
    checkbutton CheckbuttonFn   ;
    command     CommandFn       ;
    radiobutton RadiobuttonFn   ;
    separator   SeparatorFn     ;
}

def_tuple_notation!( "menu::cascade"      => CascadeTup       CascadeFn       CascadeOpt      );
def_tuple_notation!( "menu::checkbutton"  => CheckbuttonTup   CheckbuttonFn   CheckbuttonOpt  );
def_tuple_notation!( "menu::command"      => CommandTup       CommandFn       CommandOpt      );
def_tuple_notation!( "menu::radiobutton"  => RadiobuttonTup   RadiobuttonFn   RadiobuttonOpt  );
def_tuple_notation!( "menu::separator"    => SeparatorTup     SeparatorFn     SeparatorOpt    );

def_widget_opts! {
    CascadeOpt: (
        // standard
        crate::opt::TkActiveBackground,
        crate::opt::TkActiveBorderWidth,
        crate::opt::TkActiveForeground,
        crate::opt::TkBackground,
        crate::opt::TkBg,
        crate::opt::TkBorderWidth,
        crate::opt::TkBd,
        crate::opt::TkCursor,
        crate::opt::TkDisabledForeground,
        crate::opt::TkFont,
        crate::opt::TkForeground,
        crate::opt::TkRelief,
        crate::opt::TkTakeFocus,

        // widget-specific
        crate::opt::TkPostCommand,
        crate::opt::TkTearOff,
        crate::opt::TkTearOffCommand,
        crate::opt::TkTitle,
        crate::opt::TkType,

        // TkMenuEntryOpt
        crate::opt::TkAccelerator,
        crate::opt::TkBitmap,
        crate::opt::TkColumnBreak,
        crate::opt::TkCommand,
        crate::opt::TkCompound,
        crate::opt::TkHideMargin,
        crate::opt::TkImage,
        crate::opt::TkLabel,
        crate::opt::TkState,
        crate::opt::TkUnderline,
    ),
    CheckbuttonOpt: (
        crate::opt::TkAccelerator,
        crate::opt::TkActiveBackground,
        crate::opt::TkActiveForeground,
        crate::opt::TkBackground,
        crate::opt::TkBitmap,
        crate::opt::TkColumnBreak,
        crate::opt::TkCommand,
        crate::opt::TkCompound,
        crate::opt::TkFont,
        crate::opt::TkForeground,
        crate::opt::TkHideMargin,
        crate::opt::TkImage,
        crate::opt::TkIndicatorOn,
        crate::opt::TkLabel,
        crate::opt::TkOffValue,
        crate::opt::TkOnValue,
        crate::opt::TkSelectColor,
        crate::opt::TkSelectImage,
        crate::opt::TkState,
        crate::opt::TkUnderline,
        crate::opt::TkVariable,
    ),
    CommandOpt: (
        crate::opt::TkAccelerator,
        crate::opt::TkActiveBackground,
        crate::opt::TkActiveForeground,
        crate::opt::TkBackground,
        crate::opt::TkBitmap,
        crate::opt::TkColumnBreak,
        crate::opt::TkCommand,
        crate::opt::TkCompound,
        crate::opt::TkFont,
        crate::opt::TkForeground,
        crate::opt::TkHideMargin,
        crate::opt::TkImage,
        crate::opt::TkLabel,
        crate::opt::TkState,
        crate::opt::TkUnderline,
        crate::opt::TkVariable,
    ),
    RadiobuttonOpt: (
        crate::opt::TkAccelerator,
        crate::opt::TkActiveBackground,
        crate::opt::TkActiveForeground,
        crate::opt::TkBackground,
        crate::opt::TkBitmap,
        crate::opt::TkColumnBreak,
        crate::opt::TkCommand,
        crate::opt::TkCompound,
        crate::opt::TkFont,
        crate::opt::TkForeground,
        crate::opt::TkHideMargin,
        crate::opt::TkImage,
        crate::opt::TkIndicatorOn,
        crate::opt::TkLabel,
        crate::opt::TkSelectColor,
        crate::opt::TkSelectImage,
        crate::opt::TkState,
        crate::opt::TkUnderline,
        crate::opt::TkValue,
        crate::opt::TkVariable,
    ),
    SeparatorOpt: (
        crate::opt::TkColumnBreak,
        crate::opt::TkCompound,
        crate::opt::TkHideMargin,
    ),
}

impl<Inst:TkInstance> AddMenus<Inst> for TkMenu<Inst> {}
impl<Inst:TkInstance> AddMenus<Inst> for crate::TkMenubutton<Inst> {}
impl<Inst:TkInstance> AddMenus<Inst> for crate::TkToplevel<Inst> {}
impl<Inst:TkInstance> AddMenus<Inst> for crate::TkRoot<Inst> {}

pub trait AddMenus<Inst>
    where Self : Deref<Target=Widget<Inst>>
        , Inst : TkInstance
{
    fn add_menus<Widgs,Opts,Shape>( &self, path_opts_widgets: PathOptsWidgets<Opts,Widgs> )
        -> InterpResult<CreatedWidgets<Inst>>
        where Opts: IntoHomoTuple<TkMenuOpt>
                  + IntoHomoTuple<OptPair>
            , Widgs: ConvertTuple
            , <Widgs as ConvertTuple>::Output: DomForest::<(&'static str,&'static str),OptPair,Shape>
    {
        let path = path_opts_widgets.path;
        let opts = path_opts_widgets.opts;
        let widgets = path_opts_widgets.widgets.convert_tuple();

        let mut created_widgets = CreatedWidgets::new( self.deref().path );

        let top_menu = self.deref().add( "menu", PathOptsWidgets{ path, opts, widgets: () }).map( |w| TkMenu( w ))?;
        self.deref().tk().run(( self.deref().path, "configure", "-menu", top_menu.0.path ))?;

        let mut current_path = top_menu.0.path.to_owned();
        let mut menu_cmd = Vec::<Obj>::new();
        let mut add_cmd = Vec::<Obj>::new();
        let mut is_cascade = false;
        let mut is_branch = false;

        DomForest::<(&'static str,&'static str),OptPair,Shape>::try_preorder( widgets, &mut |visit| -> InterpResult<()> {
            match visit {
                Visit::Branch( (cmd, path) ) => {
                    is_branch = true;

                    if cmd == "menu::cascade" {
                        is_cascade = true;

                        let parent_path = current_path.clone();
                        current_path = self.deref().tk().next_path( &current_path, path );

                        menu_cmd.push( "menu".into() );
                        menu_cmd.push( current_path.as_str().into() );

                        add_cmd.push( parent_path.as_str().into() );
                        add_cmd.push( "add".into() );
                        add_cmd.push( "cascade".into() );
                        add_cmd.push( "-menu".into() );
                        add_cmd.push( current_path.as_str().into() );
                    } else {
                        // should be an error
                    }
                },
                Visit::Leaf( (mut cmd, path) ) => {
                    is_branch = false;

                    match cmd {
                        "menu::cascade"     => is_cascade = true,
                        "menu::command"     => { cmd = "command"    ; is_cascade = false; },
                        "menu::separator"   => { cmd = "separator"  ; is_cascade = false; },
                        "menu::checkbutton" => { cmd = "checkbutton"; is_cascade = false; },
                        "menu::radiobutton" => { cmd = "radiobutton"; is_cascade = false; },
                        _ => (),
                    }
                    if is_cascade {
                        let parent_path = current_path.clone();
                        current_path = self.deref().tk().next_path( &current_path, path );

                        menu_cmd.push( "menu".into() );
                        menu_cmd.push( current_path.as_str().into() );

                        add_cmd.push( parent_path.as_str().into() );
                        add_cmd.push( "add".into() );
                        add_cmd.push( "cascade".into() );
                        add_cmd.push( "-menu".into() );
                        add_cmd.push( current_path.as_str().into() );
                    } else {
                        add_cmd.push( current_path.as_str().into() );
                        add_cmd.push( "add".into() );
                        add_cmd.push( cmd.into() );
                    }
                },
                Visit::Frame => {
                    current_path = Widget::<Inst>::compute_parent_path( &current_path );
                },
                Visit::AttrsStart( _len ) => (),
                Visit::Attr( opt_pair ) => {
                    let menu_opts = [
                        "-activebackground", "-activeborderwidth", "-activeforeground",
                        "-background", "-bg", "-borderwidth", "-bd",
                        "-cursor", "-disabledforeground", "-font", "-foreground", "-relief",
                        "-takefocus", "-postcommand", "-selectcolor", "-tearoff", "-tearoffcommand",
                        "-title", "-type",
                    ];
                    let command = if is_cascade && menu_opts.contains( &opt_pair.name ) {
                        &mut menu_cmd
                    } else {
                        &mut add_cmd
                    };
                    if opt_pair.name.len() > 0 {
                        command.push( opt_pair.name.into() );
                    }
                    command.push( opt_pair.value );
                },
                Visit::AttrsEnd => {
                    if !menu_cmd.is_empty() {
                        self.tk().run( &*menu_cmd )?;
                        menu_cmd.clear();
                        created_widgets.widgets.push( UpcastableWidget {
                            widget : Widget::from_name_unchecked( &current_path, self.tk().inst ),
                            name   : "menu",
                        });
                        if !is_branch {
                            // No `Visit::Frame` for `Visit::Leaf`
                            current_path = Widget::<Inst>::compute_parent_path( &current_path );
                        }
                    }
                    if !add_cmd.is_empty() {
                        self.tk().run( &*add_cmd )?;
                        add_cmd.clear();
                    }
                },
            }
            Ok(())
        })?;

        Ok( created_widgets )
    }
}