Tuesday, 14 December 2010

Button Bar

Currently, if you want to add a snippet, you need to click on the parent narrative, find the "new snippet" button, click on that, click on the new snippet to edit it, and and then back to the narrative to find the next one.

That would drive me nuts to use for anything seriuous. So what I want is a set of buttons that don't depend on the narrative item selected. So I made a button panel:

import System
import System.Drawing
import System.Windows.Forms

class ButtonPanel(Panel):
        [Property(narrative)]   _narrative      as Button
        [Property(snippet)]     _snippet        as Button
        [Property(background)]  _background     as Button
        [Property(choice)]      _choice         as Button
        [Property(func)]        _func           as Button
        [Property(chapter)]     _chapter        as Button
        [Property(thumbnail)]   _thumbnail      as Button

        [Property(delete)]      _delete         as Button
        [Property(shuffle_up)]  _shuffle_up     as Button
        [Property(shuffle_down)] _shuffle_down  as Button

        def do_button(xpix as int, row as int, text as string, width as int):
                b = Button()
                b.Text = text
                b.Size = Size(width,25)
                b.Location = Point(xpix, 5 + row * 35)
                Controls.Add(b)
                return b

        def col(col_num as int):
                return 20 + 93 * col_num

        def constructor():
                super()

                Size = Size(590,100)
                BorderStyle = BorderStyle.Fixed3D

                c = 0
                narratve        = do_button(col(c++), 0, "+Narrative",  80)
                snippet         = do_button(col(c++), 0, "+Snippet",    80)
                thumbnail       = do_button(col(c++), 0, "+Thumb",      80)
                background      = do_button(col(c++), 0, "+Background", 80)
                choice          = do_button(col(c++), 0, "+Choice",     80)
                func            = do_button(col(c++), 0, "+Func",       80)

                c = 0
                shuffle_up      = do_button(col(c++), 1, "Shuffle Up",  80)
                shuffle_down    = do_button(col(c++), 1, "Shuffle Down",80)
                c ++
                chapter         = do_button(col(c++), 1, "+Chapter",    80)
                c ++
                delete          = do_button(col(c++), 1, "Delete",      80)



And, on the basis that testing works better in small chunks, I added some stand-alone test code:

bp = ButtonPanel()
assert bp.snippet != null, "time the first"
f = Form()
f.Size = Size(590,100)
f.Controls.Add(bp)

assert bp != null
assert bp.snippet != null, "time the second"

bp.snippet.Click += def(o,e):
        print "snippet"

bp.delete.Click += def(o,e):
        if Control.ModifierKeys & Keys.Control:
                print "delete"
                return
        rc = MessageBox.Show(
                "Are You Sure?",
                "Warning",
                MessageBoxButtons.OKCancel
        )
        if rc == DialogResult.OK:
                print "delete"
                return
        print "delete cancelled"

Application.Run(f)


That gives me a window like this:







I've exposed the buttons to the outside world because it makes more sense to let the caller attach events directly, rather than setting local handlers, and then raising events to be caught again higher up. It's not like I'm going to use this in a lot of places, after all.

The other thing I thought I'd play with here is making the delete button safe. I've tried to put it next to the likely least-used buttons, +Func and +Choice. I also added an "Are You Sure?" popup, and, since that would also drive me mad in production use, you can skip the popup by holding down control when you click. All in the stand-alone test, but nice to get the code sorted out.

The other thing I might do with Delete is move it to the Tree panel.

Anyway, now to fix that thing onto the app form, and then get the other buttons working. Lots to do, still!

No comments: