OSDN Git Service

A couple of bugfix in the Document. The version number is set to 1.0b1.
[molby/Molby.git] / wxSources / MySlider.cpp
1 /*
2  *  MySlider.cpp
3  *  Molby
4  *
5  *  Created by Toshi Nagata on 08/11/15.
6  *  Copyright 2008 Toshi Nagata. All rights reserved.
7  *
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation version 2 of the License.
11  
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  GNU General Public License for more details.
16  */
17
18 #include "MySlider.h"
19 #include "MyApp.h"
20
21 BEGIN_EVENT_TABLE(MySlider, wxToggleButton)
22 EVT_MOUSE_EVENTS(MySlider::OnMouseEvent)
23 // EVT_PAINT(MySlider::OnPaint)
24 EVT_MOUSE_CAPTURE_LOST(MySlider::OnCaptureLost)
25 END_EVENT_TABLE()
26
27 const wxEventType MySliderEvent = wxNewEventType();
28
29 MySlider::MySlider(wxWindow* parent, wxWindowID id, int direction, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator, const wxString& name):
30         wxToggleButton(parent, id, wxEmptyString, pos, size, style | wxTOGGLEBUTTON_STYLE, validator, name)
31 {
32         m_direction = direction;
33         SetCursor(wxCursor(wxCURSOR_HAND));
34         mouseStatus = 0;
35 }
36
37 //void
38 //MySlider::OnPaint(wxPaintEvent &event)
39 //{
40 //      wxToggleButton::OnPaint(event);
41 //}
42
43 void
44 MySlider::OnCaptureLost(wxMouseCaptureLostEvent &event)
45 {
46         mouseStatus = 0;
47 }
48
49 void
50 MySlider::OnMouseEvent(wxMouseEvent &event)
51 {
52         wxPoint pt(event.GetPosition());
53         bool sendAction = false;
54         
55         if (mouseStatus == 0) {
56                 if (event.LeftDown()) {
57                         mouseDownPoint = pt;
58                         mouseDragPoint = pt;
59                         mouseStatus = 1;
60                         sendAction = true;
61                         CaptureMouse();
62                 }
63         } else {
64                 if (event.Dragging()) {
65                         mouseDragPoint = pt;
66                         mouseStatus = 2;
67                         sendAction = true;
68                 } else if (event.LeftUp() /* || (event.Entering() && !event.LeftIsDown()) */) {
69                         mouseStatus = 0;
70                         sendAction = true;
71                         ReleaseMouse();
72         //      } else if (event.Leaving()) {
73         //              wxGetApp().NotifyMouseUpEvent(this);
74                 }
75         }
76         if (sendAction) {
77                 /*  Send a custom event  */
78                 wxCommandEvent event(MySliderEvent, GetId());
79                 event.SetEventObject(this);
80                 GetEventHandler()->ProcessEvent(event);
81         }
82 }
83
84 float
85 MySlider::GetFloatValue()
86 {
87         int width, height;
88         float f;
89         GetSize(&width, &height);
90         if (m_direction == wxHORIZONTAL) {
91                 f = (mouseDragPoint.x - mouseDownPoint.x) / (float)width;
92         } else {
93                 /*  Vertical  */
94                 /*  Note the flipped coordinate! (up is positive internally)  */
95                 f = -(mouseDragPoint.y - mouseDownPoint.y) / (float)height;
96         }
97         return f;
98 }