File: message.h

package info (click to toggle)
aseprite 1.0.5+ds-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,504 kB
  • ctags: 18,296
  • sloc: cpp: 84,144; ansic: 49,119; xml: 1,971; objc: 1,211; asm: 117; makefile: 45
file content (157 lines) | stat: -rw-r--r-- 4,863 bytes parent folder | download
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
// Aseprite UI Library
// Copyright (C) 2001-2013  David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.

#ifndef UI_MESSAGE_H_INCLUDED
#define UI_MESSAGE_H_INCLUDED
#pragma once

#include "gfx/point.h"
#include "gfx/rect.h"
#include "ui/base.h"
#include "ui/keys.h"
#include "ui/message_type.h"
#include "ui/mouse_buttons.h"
#include "ui/widgets_list.h"

#include <string>
#include <vector>

namespace ui {

  class Timer;
  class Widget;

  class Message
  {
  public:
    typedef WidgetsList::iterator& recipients_iterator;

    Message(MessageType type);
    virtual ~Message();

    MessageType type() const { return m_type; }
    const WidgetsList& recipients() const { return m_recipients; }
    bool hasRecipients() const { return !m_recipients.empty(); }
    bool isUsed() const { return m_used; }
    void markAsUsed() { m_used = true; }
    KeyModifiers keyModifiers() const { return m_modifiers; }
    bool shiftPressed() const { return (m_modifiers & kKeyShiftModifier) == kKeyShiftModifier; }
    bool ctrlPressed() const { return (m_modifiers & kKeyCtrlModifier) == kKeyCtrlModifier; }
    bool altPressed() const { return (m_modifiers & kKeyAltModifier) == kKeyAltModifier; }
    bool cmdPressed() const { return (m_modifiers & kKeyCmdModifier) == kKeyCmdModifier; }
    bool onlyShiftPressed() const { return m_modifiers == kKeyShiftModifier; }
    bool onlyCtrlPressed() const { return m_modifiers == kKeyCtrlModifier; }
    bool onlyAltPressed() const { return m_modifiers == kKeyAltModifier; }
    bool onlyCmdPressed() const { return m_modifiers == kKeyCmdModifier; }

    void addRecipient(Widget* widget);
    void prependRecipient(Widget* widget);
    void removeRecipient(Widget* widget);

    void broadcastToChildren(Widget* widget);

  private:
    MessageType m_type;         // Type of message
    WidgetsList m_recipients; // List of recipients of the message
    bool m_used;              // Was used
    KeyModifiers m_modifiers; // Key modifiers pressed when message was created
  };

  class KeyMessage : public Message
  {
  public:
    KeyMessage(MessageType type, KeyScancode scancode, int unicodeChar, int repeat);

    KeyScancode scancode() const { return m_scancode; }
    int unicodeChar() const { return m_unicodeChar; }
    int repeat() const { return m_repeat; }
    bool propagateToChildren() const { return m_propagate_to_children; }
    bool propagateToParent() const { return m_propagate_to_parent; }
    void setPropagateToChildren(bool flag) { m_propagate_to_children = flag; }
    void setPropagateToParent(bool flag) { m_propagate_to_parent = flag; }

  private:
    KeyScancode m_scancode;
    int m_unicodeChar;
    int m_repeat; // repeat=0 means the first time the key is pressed
    bool m_propagate_to_children : 1;
    bool m_propagate_to_parent : 1;
  };

  class PaintMessage : public Message
  {
  public:
    PaintMessage(int count, const gfx::Rect& rect)
      : Message(kPaintMessage), m_count(count), m_rect(rect) {
    }

    int count() const { return m_count; }
    const gfx::Rect& rect() const { return m_rect; }

  private:
    int m_count;             // Cound=0 if it's last msg of draw-chain
    gfx::Rect m_rect;        // Area to draw
  };

  class MouseMessage : public Message
  {
  public:
    MouseMessage(MessageType type, MouseButtons buttons,
      const gfx::Point& pos,
      const gfx::Point& wheelDelta = gfx::Point(0, 0))
      : Message(type),
        m_buttons(buttons),
        m_pos(pos),
        m_wheelDelta(wheelDelta) {
    }

    MouseButtons buttons() const { return m_buttons; }
    bool left() const { return (m_buttons & kButtonLeft) == kButtonLeft; }
    bool right() const { return (m_buttons & kButtonRight) == kButtonRight; }
    bool middle() const { return (m_buttons & kButtonMiddle) == kButtonMiddle; }
    gfx::Point wheelDelta() const { return m_wheelDelta; }

    const gfx::Point& position() const { return m_pos; }

  private:
    MouseButtons m_buttons;     // Pressed buttons
    gfx::Point m_pos;           // Mouse position
    gfx::Point m_wheelDelta;    // Wheel axis variation
  };

  class TimerMessage : public Message
  {
  public:
    TimerMessage(int count, Timer* timer)
      : Message(kTimerMessage), m_count(count), m_timer(timer) {
    }

    int count() const { return m_count; }
    Timer* timer() { return m_timer; }

  private:
    int m_count;                    // Accumulated calls
    Timer* m_timer;                 // Timer handle
  };

  class DropFilesMessage : public Message
  {
  public:
    typedef std::vector<std::string> Files;

    DropFilesMessage(const Files& files)
      : Message(kDropFilesMessage), m_files(files) {
    }

    const Files& files() const { return m_files; }

  private:
    Files m_files;
  };

} // namespace ui

#endif