OSDN Git Service

The text edit in MyListCtrl seems weird. Hopefully fixed.
[molby/Molby.git] / wxSources / MyProgressIndicator.cpp
1 /*
2  *  MyProgressIndicator.cpp
3  *  Molby
4  *
5  *  Created by Toshi Nagata on 13/06/26.
6  *  Copyright 2013 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 "MyProgressIndicator.h"
19 #include "MyApp.h"
20
21 #include "wx/dcclient.h"
22 #include "wx/bitmap.h"
23 #include "wx/image.h"   //  If omitted, then new wxBitmap(char *, int) will fail
24
25 BEGIN_EVENT_TABLE(MyProgressIndicator, wxWindow)
26 EVT_MOUSE_EVENTS(MyProgressIndicator::OnMouseEvent)
27 EVT_PAINT(MyProgressIndicator::OnPaint)
28 EVT_MOUSE_CAPTURE_LOST(MyProgressIndicator::OnCaptureLost)
29 END_EVENT_TABLE()
30
31 static wxBitmap *sStopMiniIcons[3];
32 static wxBitmap *sProgressIndicatorIcons[12];
33
34 MyProgressIndicator::MyProgressIndicator(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) : wxWindow(parent, id, pos, size, style)
35 {
36         enabled = false;
37         pressed = false;
38         indicatorState = 0;
39
40         // Initialize icons for progress indicator
41         if (sStopMiniIcons[0] == NULL) {
42 #include "../bitmaps/stop_mini_grey.xpm"
43 #include "../bitmaps/stop_mini.xpm"
44 #include "../bitmaps/stop_mini_dark.xpm"
45 #include "../bitmaps/pi00.xpm"
46 #include "../bitmaps/pi01.xpm"
47 #include "../bitmaps/pi02.xpm"
48 #include "../bitmaps/pi03.xpm"
49 #include "../bitmaps/pi04.xpm"
50 #include "../bitmaps/pi05.xpm"
51 #include "../bitmaps/pi06.xpm"
52 #include "../bitmaps/pi07.xpm"
53 #include "../bitmaps/pi08.xpm"
54 #include "../bitmaps/pi09.xpm"
55 #include "../bitmaps/pi10.xpm"
56 #include "../bitmaps/pi11.xpm"
57                 sStopMiniIcons[0] = new wxBitmap(stop_mini_grey, wxBITMAP_TYPE_XPM);
58                 sStopMiniIcons[1] = new wxBitmap(stop_mini, wxBITMAP_TYPE_XPM);
59                 sStopMiniIcons[2] = new wxBitmap(stop_mini_dark, wxBITMAP_TYPE_XPM);
60                 sProgressIndicatorIcons[0] = new wxBitmap(pi00, wxBITMAP_TYPE_XPM);
61                 sProgressIndicatorIcons[1] = new wxBitmap(pi01, wxBITMAP_TYPE_XPM);
62                 sProgressIndicatorIcons[2] = new wxBitmap(pi02, wxBITMAP_TYPE_XPM);
63                 sProgressIndicatorIcons[3] = new wxBitmap(pi03, wxBITMAP_TYPE_XPM);
64                 sProgressIndicatorIcons[4] = new wxBitmap(pi04, wxBITMAP_TYPE_XPM);
65                 sProgressIndicatorIcons[5] = new wxBitmap(pi05, wxBITMAP_TYPE_XPM);
66                 sProgressIndicatorIcons[6] = new wxBitmap(pi06, wxBITMAP_TYPE_XPM);
67                 sProgressIndicatorIcons[7] = new wxBitmap(pi07, wxBITMAP_TYPE_XPM);
68                 sProgressIndicatorIcons[8] = new wxBitmap(pi08, wxBITMAP_TYPE_XPM);
69                 sProgressIndicatorIcons[9] = new wxBitmap(pi09, wxBITMAP_TYPE_XPM);
70                 sProgressIndicatorIcons[10] = new wxBitmap(pi10, wxBITMAP_TYPE_XPM);
71                 sProgressIndicatorIcons[11] = new wxBitmap(pi11, wxBITMAP_TYPE_XPM);
72         }
73 }
74
75 void
76 MyProgressIndicator::OnPaint(wxPaintEvent &event)
77 {
78         int stopState;
79         wxPaintDC dc(this);
80         
81         dc.Clear();
82         if (enabled)
83                 stopState = (pressed ? 2 : 1);
84         else stopState = 0;
85         dc.DrawBitmap(*sStopMiniIcons[stopState], 0, 0, 1);
86         if (enabled)
87                 dc.DrawBitmap(*sProgressIndicatorIcons[indicatorState % 12], 0, 15, 1);
88 }
89
90 void
91 MyProgressIndicator::OnMouseEvent(wxMouseEvent &event)
92 {
93         long x, y;
94         if (enabled) {
95                 event.GetPosition(&x, &y);
96                 if (event.LeftDown()) {
97                         if (x >= 0 && x <= 12 && y >= 0 && y <= 12) {
98                                 pressed = 1;
99                                 CaptureMouse();
100                         }
101                 } else if (event.Dragging()) {
102                         if (x >= 0 && x <= 12 && y >= 0 && y <= 12) {
103                                 pressed = 1;
104                         } else pressed = 0;
105                 } else if (event.LeftUp()) {
106                         if (pressed) {
107                                 /*  Send button action  */
108                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
109                                 event.SetEventObject(this);
110                                 GetEventHandler()->ProcessEvent(event);
111                         }
112                         if (HasCapture())
113                                 ReleaseMouse();
114                 }
115         }
116         if (event.LeftDown())
117                 event.Skip();
118 }
119
120 void
121 MyProgressIndicator::OnCaptureLost(wxMouseCaptureLostEvent &event)
122 {
123 }
124
125 void
126 MyProgressIndicator::SetEnabled(bool flag)
127 {
128         if (flag != enabled) {
129                 enabled = flag;
130                 Refresh();
131         }
132 }
133
134 void
135 MyProgressIndicator::SetPressed(bool flag)
136 {
137         if (flag != pressed) {
138                 pressed = flag;
139                 Refresh();
140         }
141 }
142
143 void
144 MyProgressIndicator::SetIndicatorState(int state)
145 {
146         if (state != indicatorState) {
147                 indicatorState = state;
148                 Refresh();
149         }
150 }
151
152 void
153 MyProgressIndicator::ProceedIndicatorState()
154 {
155         indicatorState = (indicatorState + 1) % 12;
156         Refresh();
157 }
158
159